sound_generator icon indicating copy to clipboard operation
sound_generator copied to clipboard

add option to start with a clean new frequency on play()

Open nyxkn opened this issue 2 years ago • 4 comments

Hey. This PR enables us to start with a clean new frequency when calling play(), rather than letting the sound glide smoothly to the new pitch.

For example: when we stop the generator, then change frequency, and then play again, the sound will be at the new frequency from the start, rather than gliding up to the new pitch.

I've implemented it so that it's only enabled once we set cleanStart to true through setCleanStart. But in my opinion this seems to be a saner behaviour, and it could possibly be the default. The gliding when changing frequency is very neat for changes while the sound is already playing, but it seems a bit strange if it happens when you change frequency while the sound is stopped.

Overall this certainly seems like a useful feature; to be able to play a clean tone at the desired frequency. Let me know what you think!

nyxkn avatar Jul 22 '22 21:07 nyxkn

Hey, this is exactly what I was looking for. Thank you! I would be happy, if this PR would be merged into main, so I can simply use it.

timoffenhaeusser avatar Jun 21 '23 09:06 timoffenhaeusser

Is this working for you? I tried it, but I still have the gliding between the frequencys.

This is what I do:

SoundGenerator.init(44100);
SoundGenerator.setCleanStart(true);

And before I start playing, I set a new Frequency:

SoundGenerator.setFrequency(myNewFrequency);
SoundGenerator.play();

Am I missing something? Any help is aprechiated.

timoffenhaeusser avatar Jun 21 '23 10:06 timoffenhaeusser

Hey! Haven't used this in a while but I know it is working. Here's some sample (incomplete) code of how I'm using it.

SoundGenerator.init(sampleRate);
SoundGenerator.setAutoUpdateOneCycleSample(true);
SoundGenerator.refreshOneCycleData();

playTone() {
  SoundGenerator.setFrequency(frequency.toDouble());
  SoundGenerator.play();
  await Future.delayed(Duration(milliseconds: (duration * 1000).toInt()));
  SoundGenerator.stop();
}

nyxkn avatar Jun 21 '23 11:06 nyxkn

All the standard functions are also working for me. But I tried to use this new function of yours: setCleanStart(true)

I forked this repo and on my fork, I merged this PR into main, so I can use it in my flutter project. But the sound is still gliding from one frequency to the next, so I just wanted to know if this new function "setCleanStart" is working on your side.

Best wishes

timoffenhaeusser avatar Jun 21 '23 14:06 timoffenhaeusser