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

Using Nyquist scripts in Audacity.
Post and download new plug-ins.
Forum rules
If you require help using Audacity, please post on the forum board relevant to your operating system:
Mac OS X
GNU/Linux and Unix-like
Post Reply
Posts: 11
Joined: Tue Jul 16, 2013 11:22 pm
Operating System: Please select

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

Post by jabowery » Fri Mar 27, 2020 1:26 am

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?

Posts: 9876
Joined: Sat Dec 27, 2008 5:22 pm
Operating System: Windows 8 or 8.1

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

Post by Trebor » Fri Mar 27, 2020 2:15 am

"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 ...

Code: Select all

(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).

Site Admin
Posts: 81228
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

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

Post by steve » Fri Mar 27, 2020 3:06 pm

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: ... racks.html) You can then run your experiments in the "Nyquist Prompt" effect (see:

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:

Code: Select all

(aref *track* 0)
and the right channel with:

Code: Select all

(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)

Code: Select all

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

To get "left - right"

Code: Select all

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

To get "left x right"

Code: Select all

(mult (aref *track* 0) (aref *track* 1))
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

Post Reply