cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Submit your abstract to the call for content for Discovery Summit Americas by April 23. Selected abstracts will be presented at Discovery Summit, Oct. 21- 24.
Discovery is online this week, April 16 and 18. Join us for these exciting interactive sessions.
Choose Language Hide Translation Bar
Craige_Hales
Super User
Audio Note in an Envelope

I've been experimenting with FM synthesis to make audio files, and using ADSR envelopes to shape the resulting sound. FM means frequency modulation.  ADSR means Attack, Decay, Sustain, and Release. ADSR describes a curve that determines how quickly a note's volume increases (attack) and decreases (decay) when a key is pressed, what the sustained volume is as the key is held, and how the note's volume fades when the key is released. A typical picture of an ADSR curve has four straight lines:

A, D, R are time; S is a levelA, D, R are time; S is a level

The waveform below has no modulation (all the peaks and valleys are equally spaced.)


 

If you look at the top of the first cycle in the graph, you'll see a small glitch. Since I was writing the code, I assumed I'd made an error; zooming it makes it look like a significant error that might impact the sound a bit.

 

Actually, two glitchesActually, two glitches

The attack was set for 0.001 seconds and the decay for 0.003; at 44,100 samples per second the attack ends at sample 44 and the decay ends around sample 176. I built a data table to make a simplified version and eventually realized the glitch is caused by the sharp corners in the ADSR curve. To fix it, I fit a spline to the ADSR curve. As long as the spline's tension is high enough, the glitch is too subtle to notice. (This is simplified data, not the data you heard above and further below.)

 

The orange spline-based curves are hiding behind the purple curves; at this zoom they are nearly identical.The orange spline-based curves are hiding behind the purple curves; at this zoom they are nearly identical.

Zoom in a bit at the corner between the decay and sustain curves:

 

The solid sine curve is rising, but slowing. The dotted purple decay curve is falling, but about to become flat (sustain.)The solid sine curve is rising, but slowing. The dotted purple decay curve is falling, but about to become flat (sustain.)

The dotted orange curve is a spline interpolation of the points in the dotted purple curve. The solid orange curve is the original sine wave multiplied by the orange envelope to scale it down. The purple curve's error appears to be about 1 part in 1000; I'm not sure if that will be audible or not. In the +/- 32767 range of wave file data, that would be about 32, 2^5, maybe in the 5th bit of a 16 bit number, maybe 60 dB down. I think with good equipment and young ears it should be audible.

 

 

Zoomed in, it looks like a nicely distorted sine wave.

 

The glitch is gone. The leading part of the sine wave is flattened by the rising gain of the attack.The glitch is gone. The leading part of the sine wave is flattened by the rising gain of the attack.

File attached. On windows it uses PlaySound in the multimedia DLL . There are a couple of switches at the top. It isn't using any of the FM code, and the more I think about it, it probably implements phase modulation rather than frequency modulation. It is not ready for more than this demo.

Last Modified: Jul 27, 2020 7:11 AM