More about clicks with audio library

N5NHJ

Member
I'm working on a Teensy 4.1 project that generates sine audio over a PWM pin using the audio library. The project aims to send Morse code based on the status of an input pin, alongside other network-related functions.

Speed is crucial; the loop's primary function is to monitor the input pin's status and perform actions accordingly. The transition between the sine wave turning on and off can be as short as 30 ms. However, controlling this transition with the amplitude() function, as reported already, generates clicks.

The Envelope object is unsuitable because it introduces too much delay and exhibits some strange behavior, sometimes not controlling the sinewave at all.
I tried several settings unsuccessfully.

I have achieved better results by controlling the ON/OFF status using the frequency() function. To stop the wave, I use frequency(0.0), and to start it, I use frequency(MyFrequency).

I'm not sure if this is best practice, but it is working to some extent.

Max
 
In spite of your previous unsuccessful attempt, to eliminate "clicks" when transitioning a note from OFF-to-ON, or from ON-to-OFF, the envelope object really does work using the following settings:

DELAY: 0.0 // start the note processing without delay upon noteOn()
ATTACK: 5.0 // ramp the note up in 5ms from noteOn()
HOLD: 0.0 // don't artificially hold the note on
DECAY: 0.0 // while the note is playing, don't diminish the amplitude
SUSTAIN: 1.0 // as long as the note is on, continue to play
RELEASE: 5.0 // ramp the note down in 5ms from noteOff()

I have successfully used these settings in my TeensyMIDIPolySynth (TMPS) to get rid of clicks on waveform transitions, so they should work equivalently well for DITs & DAHs.

Mark J Culross
KD5RXT
 
exhibits some strange behavior, sometimes not controlling the sinewave at all.
The envelope effect does have a long-standing bug which means if you stop feeding it audio, it stops its ramps. I have a pull request in which fixes this, and has been pending adoption for coming up to two years now… (The fade effect has the exact same problem. The PR for that is only 18 months old.)

The bug will manifest if you call noteOff() and then frequency(0.0) before the decay time has expired.

As ever, if you post a short complete sketch to demonstrate “strange behaviour“, then we can help a lot better. Otherwise we’re basically guessing … though we are quite good at that :)
 
effect fade is what you want I think...
Thanks Mark, I'm not sure why using fade never shown up during my searches, but it did the trick. Even only 2 ms In and Out are enough to reduce the clicks to an acceptable level.
 
The envelope effect does have a long-standing bug which means if you stop feeding it audio, it stops its ramps. I have a pull request in which fixes this, and has been pending adoption for coming up to two years now… (The fade effect has the exact same problem. The PR for that is only 18 months old.)

The bug will manifest if you call noteOff() and then frequency(0.0) before the decay time has expired.

As ever, if you post a short complete sketch to demonstrate “strange behaviour“, then we can help a lot better. Otherwise we’re basically guessing … though we are quite good at that :)
Thanks, h4. I was not using noteOff() and frequency (0.0) at the same time. The options I tested were amplitude(), without using Envelope, or noteOn()/noteOff() along with Envelope. Neither worked well.
Frequency(0.0) was a workaround option without Envelope and it was acceptable.
Fade is the best option so far.
 
In spite of your previous unsuccessful attempt, to eliminate "clicks" when transitioning a note from OFF-to-ON, or from ON-to-OFF, the envelope object really does work using the following settings:

DELAY: 0.0 // start the note processing without delay upon noteOn()
ATTACK: 5.0 // ramp the note up in 5ms from noteOn()
HOLD: 0.0 // don't artificially hold the note on
DECAY: 0.0 // while the note is playing, don't diminish the amplitude
SUSTAIN: 1.0 // as long as the note is on, continue to play
RELEASE: 5.0 // ramp the note down in 5ms from noteOff()

I have successfully used these settings in my TeensyMIDIPolySynth (TMPS) to get rid of clicks on waveform transitions, so they should work equivalently well for DITs & DAHs.

Mark J Culross
KD5RXT
Thanks Mark. In my case Envelope is not an option, even using your parameters.
73, Max
 
Back
Top