Help Needed: Generate Audio Track : Automatically Increment Amplitude of Tone:



  • Only Audacity can create an audio track, but when Audacity runs a “generate” type plug-in, it will add an empty mono track to receive the generated
  • audio unless an audio track is already selected.
    If an audio track is already selected, Audacity will use the selected track to receive the generated audio.

A “generate” type plug-in has a “magic comment” at the beginning:

;type generate

The semicolon tells Nyquist to ignore everything on the line after the semicolon (it’s a “code comment”)
As with all comments, this comment is ignored by Nyquist, but “magic comments” are special in that they are understood by Audacity and pass information to Audacity.
In this case, the magic comment “;type” tells Audacity what kind of plug-in it is. The keyword “generate” specifies that it is a generator kind of plug-in.


The examples below may be run in the Nyquist Prompt effect (Nyquist Prompt - Audacity Manual)
Tip. We can see if the code returns any error messages by running the Nyquist Prompt with the Debug button instead of the OK button.


The easiest way to generate a tone of a specified length is with the OSC function (Nyquist Functions)
Example: Generate 1 second tone at note “C4”

;type generate

(osc c4)

Nyquist is clever enough to know that “C4” is MIDI note 60. In fact we can demonstrate that:

(print C4)  ;returns 60

An alternative way to generate C4 (MIDI note 60):

;type generate

(osc 60)

But what if we want to generate a tone specified by frequency (Hz) rather than note name or MIDI number?
The OSC function only understands note numbers, which it calls “steps” (Note names like “A4”, “D5” etc represent note numbers / “step” numbers), so we have to convert from Hz to steps. Fortunately there is a function to do that, called: HZ-TO-STEP.
We can use it like this to generate note 72 (A4) which has a frequency of 440 Hz.

;type generate

(osc (hz-to-step 440))

The second (optional) argument for the OSC function is the duration of the sound. For generate type plug-ins, times / durations are in seconds, which makes this very easy :wink:

Example: Create a tone at 440 Hz that is 2.5 seconds duration:

;type generate

(osc (hz-to-step 440) 2.5)

Note that all the tones that we have generated so far have been “full scale” (amplitude = 1).
To scale the amplitude, we can multiply the tone by a number.
Example, to generate a 440 Hz that is 2.5 seconds duration and amplitude 0.8:

;type generate

(scale 0.8
       (osc (hz-to-step 440) 2.5))

or written another way (this is identical to the previous example, but uses the MULT function instead of the SCALE function:

;type generate

(mult 0.8
      (osc (hz-to-step 440) 2.5))

Look these up:
MULT: Nyquist Functions
SCALE: Nyquist Functions

The main difference between using SCALE or MULT, is that SCALE requires at least one of the arguments to be a sound or multi-channel sound, whereas MULT works with sounds, multi-channel sounds or numbers. I generally prefer to use MULT, but that’s just my preference.


But what if we want to scale the sound by a varying amount?
For that, we will need to create some kind of control signal that varies over time.
Traditionally, Nyquist uses low sample rate sounds as control signals, where the control signal sample rate is 1/20th of the track sample rate.

A good way to create simple control signals is with “piece-wise approximations”.
Nyquist provides an entire family of piece-wise approximations (Nyquist Functions)
The one that we shall be using is a “linear” piece-wise approximation with values for each time position. The envelope interpolates linearly between each time / value point.

Example:
Create a control signal that rises from zero to full scale (“1”) over a period of 5 seconds, then falls back to zero over the next 10 seconds.
For this, we need to specify control points at:

  • time= 0, value=0
  • time=5, value=1
  • time=15, value=0

If we look in the Nyquist manual (Nyquist Functions), we can see that the command is in the form:

(pwlv l1 t2 l2 t3 l3 ... tn ln)

(“SAL” is an alternative way of writing Nyquist code, but I only know the original “LISP” syntax. Most of Audacity’s documentation is written using LISP syntax, and most Nyquist plug-ins use LISP syntax).

Note that in the PWLV command, the initial time value is omitted - it is assumed to be zero.
So the command that we need is:

;type generate

(pwlv 0 5 1 15 0)

Were you surprised by the result? Can you explain why it is only 0.75 seconds long? (hint: the answer has already been described earlier in this post)

Now lets try amplifying a 15 second tone by this envelope:

;type generate

(mult (pwlv 0 5 1 15 0)
      (osc A4 15))

What about silence?
The easiest way to think of silence in this case is as sound that has zero amplitude.

Example:
Create 2 seconds of silence, followed by 1 second tone that starts at amplitude 1 and goes down to zero at time=4

To do this, our control points need to be:

  • time=0 (omitted), value=0
  • time=2, value=0 (keep the first 2 seconds at zero amplitude)
  • time=2, value= 1
  • time=4, value=0

and here is our code:

;type generate

(mult (pwlv 0 2 0 2 1 4 0)
      (osc A4 4))

Have a play with this and ensure that you understand what we have done so far.
Feel free to ask questions.


There’s a list of reference material here: Manuals and reference material
and an increasing amount of stuff in the Audacity wiki: Missing features - Audacity Support
and of course the Audacity manual: Nyquist - Audacity Manual

Mostly you will learn by looking at examples, trying out commands from the Nyquist manual, trying out examples from the wiki, and generally playing with it.