ddf / Minim

A Java audio library, designed to be used with Processing.
http://code.compartmental.net/tools/minim
GNU Lesser General Public License v3.0
668 stars 136 forks source link

How can I force a waveform to be updated only when it has finished playing #121

Open misterbo94 opened 2 years ago

misterbo94 commented 2 years ago

When using an Oscil, the waveform can simply be a float array, which is not copied but referred to, so in order to swap the waveform it is sufficient to change every element of the array. However, if the waveform needs to be updated frequently, this creates a lot of discontinuities in the signal, resulting in a popping sound for each swap. This happens also for all the wavetable modifiers like flip(), invert(), ... I would like to have a sort of "protection" when updating the waveform, so that it will be effectively changed only when the current one has finished playing. In that way, as long as the last value of a waveform and the first value of the next one are similar, there should be no popping sound. I've left a simple processing sketch to show the problem. wavetableproblem.txt

Is there a way to do that with existing methods? I've tried with Audiolisteners but without any luck. If not, how can I solve this problem considering I'm not a super-advanced coder? Thank you

ddf commented 2 years ago

Ah, yeah, that's tricky. The trouble is that the Waveform used by Oscil is sampled using a 0-1 phase value, so there isn't a reliable moment when the waveform "finishes" playing. The moment where is wraps could happen at any point in the buffer provided to AudioListener but that information is not really available from there. What I would do in this case is to use multiple Oscil objects that each reference a different Waveform. Run them all continuously and when you want to swap to a particular Waveform fade out the currently audible Oscil with a Line patched to its amplitude property and fade in the Oscil you want to hear with a different Line patched to its amplitude. You could make these fades extremely short so that it sounds more like a swap. If you want to directly update the Waveform used by a particular Oscil you could do so while its amplitude is 0.

misterbo94 commented 2 years ago

Thank you, that's a very good idea. However, if I'm correct, for each Oscil I'd need two Lines patched to the same UGenInput (for fading in and out), but that's not possible. Is there something I'm missing?

ddf commented 2 years ago

You only need one Line. When you want to fade out an Oscil you'd do something like call activate(0.05,1,0) on the attached Line and then later when you want to fade that Oscil back in, you'd call activate(0.05, 0, 1) on that same Line.