Nyquist functions for selection and cut/paste

Hello,

Beginner in Nyquist langage, I would like to program an effect of “time fragmentation” of the sound.

For that, I need fonctions of time selection, and cutting and pasting.

Do these fonctions exists ? And if yes what are their syntax ?

Thank you in advance.

Nyquist is a programming (scripting) language that has been shoehorned into Audacity. The way that Audacity and Nyquist interact is that Audacity makes some variables available as Nyquist variables, notably the variable “S” which is used to pass audio from the track selection to Nyquist. When a Nyquist effect is run, the value of “S” is the audio from the selected Audacity track that is currently being processed.

Note that when a Nyquist script is applied to multiple audio tracks, Nyquist processes each track in sequence (starting from the top track). For each track the “S” variable is re-initialised with the selected audio from the track that is currently being processed.

In the case of mono tracks, the value of “S” is a sound. Nyquist treats sounds as a data type (just as integers, characters and strings are other “data types”).
In the case of stereo tracks, the value of “S” is an array with two elements.
The first element (aref s 0) is the sound from the left channel
and the second element (aref s 1) is the sound from the right channel.

When the Nyquist script has completed, it will (generally) return a value [the “return value”].
If the return value is text, a character, or a number, Audacity will create a message box to display the value.
If the returned value is a sound, Audacity will replace the current audio selection with the sound.

Generally it is easier to work with mono tracks. How to work with stereo tracks is described in the Nyquist documentation (see: Missing features - Audacity Support)

One of the limitations of this system is that Nyquist is unaware of “empty space” in a track. If you want gaps in the returned sound, the best that can currently be done is to use silence where you want the gap to be. (Silences may be removed manually using the “Detach at silences” command in Audacity: Audacity Manual).


You have picked quite a tricky project for your first Nyquist effect. Handling “time” is a little complex due to the way that Nyquist handles “absolute time” and “relative time”.
In Effect plugins (“process” type plugins), the length of the track selection is taken to be “1 unit of time”. Thus, if Nyquist returns half a unit of time, the duration of the returned audio will be half the length of the original selection. This is different from how time is handled in Generate plugins. In Generate type plugins, 1 unit of time is 1 second. This is a very important distinction when working with time based effects.

As an example, if you run the code (osc 60 2.0) in a Generate type plugin, it will generate a sine wave with a note pitch of 60 and a duration of 2.0 seconds.
On the other hand, if you run the same code in a “process” (Effect) type plugin, it will generate a sine wave with a note pitch of 60 and a duration 2.0 times the duration of the original selection. In short, times/durations in Generate plugins use “absolute time” whereas times/durations in Effects (“process” type) use “relative time”.

In most cases, to specify absolute time within a process type effect is simply a matter of wrapping the function within the command ABS-ENV [short for “absolute” “environment”].

The Nyquist Prompt provides a quick and easy interface for running simple Nyquist commands as a “process” type effect. Thus, if you run the code:

(osc 60 2.0)

in the Nyquist prompt, it returns a sine wave with a note pitch of 60 and a duration 2.0 times the duration of the original selection.
If you want it to return a sine wave with a note pitch of 60 and a duration of 2.0 seconds, then you would wrap the OSC function inside ABS-ENV like this:

(abs-env
  (osc 60 2.0))

There are a number of functions relating to accessing sounds which you can read about here: Nyquist Functions
Note that the Nyquist manual is written for the (standalone) Nyquist manual and NOT for Nyquist running within Audacity, thus the Nyquist manual makes no distinction between “generate plugins” and “process plugins”, as those are constructs specific to Audacity.

Probably the most useful function for your purpose is the EXTRACT function.
Running as a “process” type effect (for example, in the Nyquist Prompt), the following command will extract the middle 1/3rd of the selected audio:

(extract 0.333 0.667 s)

To extract from 2.0 seconds to 5.0 seconds, you could force the command to use “absolute” time like this:

(abs-env
  (extract 2.0 5.0 s))

Alternatively (and more simply), there is an “absolute” version of the EXTRACT function that works exactly the same as the above:

(extract-abs 2.0 5.0 s)

For a fairly simple example of working with time selections, have a look at this effect: Missing features - Audacity Support

So, Nyquist cannot access the data structures of Audacity, permitting direct modification of tracks, audio/label clips - or how is it?

If I have understood correctly, there is still need for scripting at data structure level. Nyquist feels like signal processor, not direct tool to do edits. The wished scripting system could allow addition of new interactive tools and data layers (in addition to tracks, clips, spectrograms, labels).

Python could work better than Nyquist for that purpose.

No. The scripting API is not currently exposed to Nyquist.

The specific language is not really important. The crucial factor is whether the language has access to the the scripting API.
Currently Python and any other language that supports named pipes can access the scripting API if mod-script-pipe is built and enabled. See: Audacity Manual
mod-script-pipe is not built or enabled by default as it potentially exposes security vulnerabilities.