processing / p5.js

p5.js is a client-side JS platform that empowers artists, designers, students, and anyone to learn to code and express themselves creatively on the web. It is based on the core principles of Processing. http://twitter.com/p5xjs —
http://p5js.org/
GNU Lesser General Public License v2.1
21.61k stars 3.31k forks source link

SoundFile duration or currentTime incorrect #1063

Closed crhallberg closed 8 years ago

crhallberg commented 8 years ago

v0.4.17 - currentTime() doesn't exceed 92% of duration().

In the picture below, next to each waveform: track.duration(), the maximum track.currentTime() after playing twice, and the ration of maxTime/duration.

tracks

I discovered this bug playing around with sound recording: if I use currentTime() to draw a tracker while playing an audio file, it never quite reached the end. I found that it ends in between 90% (shorter files) and 92% of the reported duration(). In the picture above, the red bar (bottom track) corrects by 91.5% and the red line is much closer to the sounds' waveforms. The blue bar doesn't and is behind in each case. The first track is a sound file, the rest are recordings (using SoundRecorder) of microphone tappings.

Link to the sketch: https://www.dropbox.com/s/thyb0umynpe9sly/bug.zip?dl=0.

Hold down any key to record, click any sound to play it.

therewasaguy commented 8 years ago

@crhallberg thanks for the detailed bug report and test.

SoundFile.currentTime() goes back to zero once it reaches the max time. And currentTime can only be called during draw(). So it makes sense that there would be a difference between the highest currentTime you can read during the draw loop, and the actual maximum time (duration) of the file. The red line appears to be less accurate—when i run your test, it ends up outside the bounding box of the waveform. Are you seeing this to?

I'm going to close the issue because p5.sound has its own repo. Feel free to re-open this issue or related issues over here: https://github.com/processing/p5.js-sound/issues

A couple potential related issues:

It's worth mentioning that the Web Audio API doesn't have any built in methods for getting the currentTime/frame of a buffer source node. And it was surprisingly difficult to implement currentTime that would be sample-accurate, even if the playback rate of a soundfile is changed. The current implementation is a hack that has some definite performance tradeoffs.

crhallberg commented 8 years ago

Thank you for the detailed response! Shall I answer your questions here or in a new issue over at p5-sound?

therewasaguy commented 8 years ago

At p5-sound would be best, and you can link to this issue from there. Thanks, @crhallberg!