# Arithmetic track mix operations like: X = Y (+-*/) Z

I’d like to take two tracks and mix them with a simple arithmetic expression like:

X = Y + Z
X = Y - Z
X = Y / Z
X = Y * Z

Of course it would be great to permit constants in place of one of the tracks as well.

Is this something I’d have to write myself in Nyquist or is there some Nyquist plugin that does something like this?

“X = Y + Z” is just a normal mix-down.
“X = Y - Z” is mix-down after inverting one track (to make it -ve).
“X = Y / Z” will inevitably involve division by zero
“X = Y * Z” can be done using this Nyquist code on a stereo track …

``````(mult (aref *track* 1) 0.5 (sum 1 (aref *track* 0)))
``````

There is an expression generator plugin, but it doesn’t modify existing audio,
it generates a waveform from a mathematical expression (formula).

Nyquist can only access one track at a time, so the easiest way to experiment with this is to create a stereo track with sound “Y” in one channel and sound “Z” in the other (see: https://manual.audacityteam.org/man/splitting_and_joining_stereo_tracks.html) You can then run your experiments in the “Nyquist Prompt” effect (see: https://manual.audacityteam.org/man/nyquist_prompt.html)

In Audacity, the variable TRACK is used to pass the selected audio from the audio track to Nyquist.
The left channel of a stereo track can be accessed with:

``````(aref *track* 0)
``````

and the right channel with:

``````(aref *track* 1)
``````

The arithmetic operators “+ - * /” only work with numbers, not sounds. For sounds, use the functions “sum diff mult”.

Division is problematic because sounds usually oscillate around zero, and division by zero is an error. Even if you can guarantee that no sample values will be zero, it is still problematic because sample values should be in the floating point range +/- 1, so dividing one sound by another will usually produce extremely large values that are massively off the scale. If you can guarantee that the divisor is never zero, and that the result of your function will be in the range +/- 1, then you can use the Nyquist function “recip”, which computes 1/x for each sample x.

Some examples. In each case the result is returned to both left and right channels.

To get the sum of the left and right channels (a simple mix)

``````(sum (aref *track* 0) (aref *track* 1))
``````

To get “left - right”

``````(diff (aref *track* 0) (aref *track* 1))
``````

To get “left x right”

``````(mult (aref *track* 0) (aref *track* 1))
``````