Nyquist Basics: Changing the volume of the left and right stereo channels independently

Changing the volume of the left and right stereo channels independently
As discussed previously, the audio data from an Audacity track is passed to Nyquist in a variable 's'. If the track is mono (just one channel) then the is simply the value given to the variable 's'. 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 's'.

To access a specific element of an array we use the (aref) command.

As a stereo track has 2 channels, the array 's' 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: (aref s 0) To access the audio data from the right channel we can use: (aref s 1)

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 (scale 0.5 s)

If we use this instruction on a stereo track the function is applied to each element of 's' 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 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. (vector ) and 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. 1) Open a short stereo track. 2) From the Effect menu select "Nyquist Prompt" and enter the following code: (vector  (scale 2.0 (aref s 0))   (aref s 1) )

The 0th element in this array is the original left channel (aref s 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 s 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 s) is identical to (mult s 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: (vector  (mult (aref s 0) 0.5)   (mult (aref s 1) 2.0))