This page explains how to use Nyquist to change the volume of left and right stereo channels independently.
As discussed previously, the audio data <sound>
from an Audacity track is passed to Nyquist in a variable '*track*
'. If the track is mono (just one channel) then the <sound>
is simply the value given to the variable '*track*
'. However, for a stereo track there are two sounds, and these are passed to Nyquist as two elements of an array. The name of the array is '*track*'
.
To access a specific element of an array we use the (aref) command.
As a stereo track has 2 channels, the array '*track*
' has two elements which are numbered 0 and 1. the 0th element is the audio data from the left channel and the other element contains the audio data from the right channel.
To access the audio data from the left channel we can use:
To access the audio data from the right channel we can use:
As described in the previous tutorial we can amplify a sound using the (scale) function. For a mono track if we want to halve the amplitude of the sound we simply type
If we use this instruction on a stereo track the function is applied to each element of '*track*' in turn, so both channels are amplified to half of their original amplitude. However if we want to access the channels individually then we also need to know how to send two different sounds back to the same track in Audacity.
To send two different sounds to a stereo track in Audacity we must create an array with two elements. The first element will contain the <sound>
{=html} for the left channel and the second element will contain the sound for the right channel. The easiest way to do this is to use the vector function.
<left channel>
and <right channel>
will be the sounds that we are sending from Nyquist.
To try this out we will amplify the left channel only of a stereo track.
Open a short stereo track.
From the Effect menu select "Nyquist Prompt" and enter the following code:
Notice that the two elements are within the ellipses of the "vector" function.
The 0th element in this array is the original left channel (aref *track* 0)
that has been scaled [amplified] by 2. This will be the new left channel. The next element in this array is the original right channel (aref *track* 1)
which is sent back unaltered as the new right channel.
Instead of using the function (scale)
we could use the function mult. This is virtually identical to using the function scale except that we do not need to specify the multiplication factor first. (mult 2.0 *track*)
is identical to (mult *track* 2.0)
.
For our second example we will amplify the left channel to half of its original amplitude and the right channel to double its original amplitude:
This page explains how to use Nyquist to add a feedback delay effect in Audacity.
Note: All [comments] and [explanations] are written in square brackets, so they cannot be confused with (Lisp code).
To add a feedback delay effect to an Audacity track with Nyquist, the easiest way is to use the Nyquist "feedback-delay" function:
(feedback-delay sound delay feedback)
The "feedback-delay" function applies feedback delay to sound. The delay must be a number (in seconds). The sample rate is the maximum from sound and feedback (if feedback is also a sound). The amount of feedback should be less than 1 to avoid an exponential increase in amplitude. Also since output is truncated at the stop time of sound, you may want to append some silence to sound to give the filter time to decay.
Example:
1. First either load a sound file into Audacity or record some.
2. Now click Tools -> Nyquist Prompt. A window with a text field will appear where you can type in:
Do not forget to type the parens. The parens are part of the Lisp language Nyquist is based on. Without the parens the Nyquist Lisp interpreter will not be able to understand your code
See Prompt Basics for more info about the prompt.
After clicking "OK" in the "Nyquist Prompt" window the "feedback-delay" function will take the Audacity sound and return a output sound with a feedback delay of 0.7s throughout the sound. The result of the last computation of the Nyquist code always gets automatically returned to Audacity.
The ''*TRACK*''' variable is the Audacity "sound" [the selected part of the Audacity track]. Nyquist in Audacity always understands ''*TRACK*'' as the Audacity sound variable.
Try "feedback-delay" with longer or shorter delay times as well as different sounds for feedback. Nyquist provides many more functions to generate sounds besides the simple "sine" function. Look at Functions: Sound Synthesis for the complete list of these functions.
This page explains how to use Nyquist to change the volume of Audacity tracks in different ways.
Note: All [comments] and [explanations] are written in square brackets, so they cannot be confused with (Lisp code).
SAL is a new alternative syntax to LISP. Although Nyquist is based on the LISP programming language, you can write almost any Nyquist program in SAL. Most people prefer SAL syntax to LISP syntax because SAL is a bit more like popular programming languages that include Java, C, and Python.
You can specify which one you're using with the respective codetype header:
You can only use one or the other in your script.
To change the volume of an Audacity track with Nyquist, the easiest way is to use the Nyquist SCALE function:
scale(number, sound)
The "scale" function multiplies the amplitude [volume] of the "sound" by the given "number". A number of 0.5 will make the sound become only half as loud as before, while a number of 2 will make the sound become double as loud as before.
Example:
See Prompt Basics for an explanation how the Nyquist prompt works.
To run this example in the Nyquist Prompt, ensure that "Use legacy (version 3) syntax" is not selected.
For more information about the *track*** ** keyword, refer to the Plugin Reference
1. Create an audio track with some audio [eg a short recording]
2. Now click Tools > Nyquist Prompt. A window with a text field will appear where you can type in:
Important: Do not forget to type the parentheses. The parentheses are part of the Lisp language Nyquist is based on. Without the parentheses the Nyquist Lisp interpreter will not be able to understand your code.
Important: Do not forget to type the parens and comma. These are part of the SAL language.
After clicking "OK" in the "Nyquist Prompt" window the "scale" function will take the Audacity sound and return a "scaled-down" sound with half the volume to Audacity. The result of the last computation of the Nyquist code always gets automatically returned to Audacity.
If you try "scale" with big numbers you will notice that you can return sounds with volumes taller than the Audacity track which will sound very distorted if you play them afterwards in Audacity. So an important lesson to learn is that Nyquist gives you the freedom to do whatever you want but it's now on you to take care that the result will still sound good afterwards.
An alternative command for amplifying a sound is the MULT command.
MULT may be used with numbers, sounds or multi-channel sounds. When using MULT with a sound and a number, each sample value in the selected sound is multiplied by the number, which is essentially the same as using the SCALE command.
This page explains how to use the Audacity Nyquist Prompt to test-run Nyquist code snippets.
The Audacity Nyquist prompt appears in Audacity's "Tools" menu.
For testing generate-type commands in Nyquist, you can use the Generate Prompt plugin instead.
Sound files are imported into Audacity via: File > Import > Audio or the shortcut CTRL + SHIFT + I
.
If you have no pre-existing sound files to work with, you can create your own mono or stereo tracks via the Audacity "Generate" menu.
Select the track(s) and click Tools > Nyquist Prompt.
The Nyquist Prompt appears like this:
Simply enter the code below and press Apply to receive a "hello world" prompt:
The programming language used above is Lisp. The Nyquist prompt automatically assumes you're using Lisp if the first character is an (
. If it isn't, the prompt assumes you are writing SAL, which is more similar to C-like syntax. If you prefer SAL, a "hello world" program looks like this:
The prompt also comes with a debug button. It shows you various warnings and non-fatal errors after the program finished. For example, if you forget the quotation marks around "
hello world
"
, just hitting apply will just do nothing, whereas hitting debug will tell you where you went wrong:
The current Nyquist manual is here: Nyquist Reference Manual.
A useful index of Nyquist commands is here: Nyquist Language reference
Audacity uses the *TRACK* variable to reference the current audio file/selection. Thus, you can use basic commands such as mult or sum with *track* and the Nyquist prompt will replace the file/selection with the result (or as Audacity calls it, "returned audio").
Note: Prior to version 4 syntax, the vaiable S
was used instead of *TRACK*
. You may encounter this in some old plug-ins or old documentation. The old syntax is obsolete and should not be used in new code
Note: These examples are focused upon using Audacity to manipulate digital signals (clearly Audacity is better suited to audio, but features such as Nyquist can open many other uses). For those interested, the signal used is an infra red (IR) sample from a remote control.
Type the following into the Nyquist Prompt (using LISP syntax): (sum *track* 1)
``Or type the following equivalent SAL command: return *track* + 1
The whole signal has now moved up to above zero.
To multiply a signal with a generated carrier signal, you can use the following commands:
The (hzosc 19000) produces 19kHz sine wave carrier.
The (osc-pulse 19000 0)
produces 19kHz square wave carrier (note the 0 is the bias or 50/50 duty cycle, -1 to 1 = 0%-100% pulse-width ). Applying the 19kHz square wave carrier obtains this result.
The top and bottoms of the signal can then be clipped using the Hard Limiter option from the effects menu (0dB limit and Wet level 1) if required.
The above examples show how you can use the many Nyquist commands to perform basic signal processing without using scripts.
Note: Unfortunately, this isn't the end of the road for this sample; it is near, but the curved "head/tail" of the signal causes a problem for the digital signal being produced [and it was also upside-down, too...]. This will hopefully form the basis of some more complex examples, since I shall need to use Nyquist to:
Find the zero crossing points
Then only apply the carrier frequency to those regions above zero. Or find another suitable command...