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

Using Nyquist scripts in Audacity.
Forum rules
If you require help using Audacity, please post on the forum board relevant to your operating system:
Windows
Mac OS X
GNU/Linux and Unix-like
jabowery
Posts: 11
Joined: Tue Jul 16, 2013 11:22 pm

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?

Trebor
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

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

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

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

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/spl ... racks.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:

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