processing / processing-sound

Audio library for Processing built with JSyn
https://processing.org/reference/libraries/sound/
GNU Lesser General Public License v2.1
149 stars 50 forks source link

jumpFrame() in the Processing Sound Library sets the frame for the play head at 0 no matter what #83

Closed Basiljet closed 1 year ago

Basiljet commented 1 year ago
## Description make it so that the variable works ## Expected Behaviour should go to a specified frame in an audio file when a value is put into it ## Current Behaviour sets the frame to 0 no matter what ## Steps to Reproduce lots of unnecessary code for the behaviour to be exhibited but here you go: import processing.sound.*; int j = 0; int speed = 1; FFT fft; SoundFile file; int bands = 2048; float[] spectrum = new float[bands]; FloatList pitches = new FloatList(); float totalfreq; void setup() { size(1024, 360); background(255); file = new SoundFile(this, "song.mp3"); fft = new FFT(this, bands); file.play(); file.rate(speed); fft.input(file); frameRate(file.sampleRate()); } void draw() { background(255); if (keyPressed) { file.jump(j); fft.analyze(spectrum); getPitch(); } } void getPitch() { float maxVal = 0; int maxIndex = 0; for (int i = 0; i < bands; i++) { if (spectrum[i] > maxVal) { maxVal = spectrum[i]; maxIndex = i; } line(i, height, i, height - spectrum[i]*height*10 ); } // Print the most abundant frequency float freq = (maxIndex * (file.sampleRate() / bands))/speed; pitches.append(freq); totalfreq += freq; j ++; println(freq+" " + frameRate+" "+ file.positionFrame(), j ); } ## Your Environment

Possible Causes / Solutions

just use jump()

kevinstadler commented 1 year ago

When running your sketch code with jumpFrame(), the positionFrame() just after the jump is as expected, i.e. the same or slightly higher than the frame jumped to. It might just sound like it is always playing from the start of the file because your j counter is increasing very slowly, while your soundfile (I don't have access to the one you used) presumably has a sample rate of 44100, so j needs to rise all the way to 44100 to jump one second into the file! There is some confusing naming here with Processing's (visual) frameRate which is quite different from the 'frames' of audio samples, maybe this should be clarified in the documentation...