bastibe / PySoundCard

PySoundCard is an audio library based on PortAudio, CFFI and NumPy
BSD 3-Clause "New" or "Revised" License
87 stars 9 forks source link

Playing a lot of short sounds in short periods of time #58

Open trueneu opened 8 years ago

trueneu commented 8 years ago

This is not an issue actually, but more of a question. I'm trying to develop a metronome app. Thus I need it to play a lot of short sounds (metronome division/beat clicks) with a short delay between them (say, in a tempo of 250 bpm, the delay between the closest division clicks should be 60 ms, not counting the length of click sound itself). Here's the problem I'm having: if I open the stream and just write to it, I'm getting "Output underflowed" between each pair of clicks, which is understandable as they're just a few milliseconds long. If I close/reopen the stream after/before each write, I'm facing a performance impact, and the playback gets all glitchy. The only workaround I came with so far is to add just a perfect amount of silence to each click sound sample, but I don't like it as it's really a hack, as the needed amount depends on tempo and sample length, and I'm not sure it'll work perfectly. My understanding of how underlying things work is too thin to come up with something beautiful, unfortunately. Is there a chance you could recommend something?

faroit commented 8 years ago

I would recommend, for a metronome, you'd better synthesise the sounds in a continuous callback.

bastibe commented 8 years ago

I think playing a click and some silence is the way to go. This is the only way to ensure perfect-length silence.