by [Theo Verelst] Musical instruments, and in fact all sound sources, can be analized by plotting the amplitude of the sound against time. That plot of evolution of that amplitude is in synthesizer terminology called the 'envelope' of a sound. To create a synthetic sound, the making of such an envelope is one of the ingredients for a succesfull sound imitation, which since the first analog electronic synthesizers is done in ways which can also be simulated by computers. amplitude ^ | /-- | / \ D | / \ S | / -------------//---- | A / \ | / \ R | / \ | / \ +--------------------------//------------> time The above is a schematic representation of how the loudness of a sound can vary from the start to the end of the sound, using what is called a ADSR curve, which we will simulate in tcl. ADSR stands for Attack, Decay, Sustain, and release, corresponding with the phases of a sound coming up, decaying, being sustained (for instance like the key on an organ bein held down), and finally dies away. On traditional synthesizers, there are knobs for these 3 rates (ADR) and level (S), so that each phase of the sound can be made to take as long as seems fitting to the synthesizer programmer. The envelope is not the waveshape of the actual sound, it's only an approximation of the amplitude of the sound, because a sound is always made of vibrating waves, in the range of 20 Herz to 20kHz, so that the amplitude is always an approximate of some kind, for instance the average root mean square power of the sound signal (in electrical or sound wave form), or the height at the top of the bottom of the wave (at the zero crossing of the derivative). The amplitude gaph can be seen as multiplied with the waveform of the sound source at each point in time. ---- ''Am I the only one who finds pages like these pretty far off-topic for a '''Tcl'''er's Wiki? -[jcw]'' (Well, I don't determine that eventually, but it doesn't feel nice to be cut short before I bring in both Snack (an important tcl/tk package), programs that are written in tcl and graphs and interesting interactors written in Tk, and even a relevant and interesting hardware link with direc tcl/tk interaction. Am I the only one to feel miserable about such comments? Are we forgetting where tcl/tk comes from and what a tool command language is for, or all we all to get lost in this sort of attitude?) ---- Using [snack] (version 2.2), I recorded a piano sound , a single note, which you can listen to from here [http://82.168.209.239/Wiki/piano1.wav] , and of which the (meanwhile well known) waves in the snack main 'xs' window look like this: [http://82.168.209.239/Wiki/pianowave1.jpg] The reason for the double shape of the 'envelope' above is that we actually see the whole wave being drawn in snack, where everywhere the waves goes up or down, black gets drawn, so the boundaries of the black both up and down indicate the envelope. Assuming we want to mimic the envelope of this example in tcl, we could use the above graph from snack and for instance some form of [Polynomial fitting], which then of course would have to do it's job right. And, it's a good practica exercise to make sure that the resulting solutio is numerically decent, and well-controllable, and tcl is of course a good way to test all these things interactively, it being an interpreted language with math built in. we could take a quadric function for the first segment, so: proc attack {npoints} { set o {} for {set i 0} {$i < $npoints} {incr i} { lappend o [expr 1-pow($npoints-$i,2)/pow($npoints,2) ] } } The quadric is simple, and mainly is constructed by thinking about it's top being at point ($npoints,1.0), and it having a reasonable weight factor for the quadric. Later on I'll go into more elaborate synthesis of such practically usefull waveshapes in overseeable tcl code. I'm not sure it's a good idea to take pow(..,2) as pow(...,2.0), though at some point it might be good to make sure all math is done in floating point. GIven time, I'll to some [binary] stuff to generate waveforms in tcl, so that no C program is needed to generate example files like wav files. IIRC snack too has binary access possibilities built in, though I'm not experienced with version 2.2 . Isn't tcl great? Well, it depends on how it is used, but it can be a hell of a laboratory language, as we'll see in other page, too.