Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
misterbo94 opened this issue Aug 17, 2022 · 3 comments

Comments

@misterbo94
Copy link

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

@misterbo94 misterbo94 changed the title How can I force a wavetable update only after current one has finished playing How can I force a waveform to be updated only when it has finished playing Aug 17, 2022
@ddf
Copy link
Owner

ddf commented Aug 18, 2022

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
Copy link
Author

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
Copy link
Owner

ddf commented Aug 19, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants