Nyquist Basics: The Audacity Nyquist Prompt

From Audacity Wiki
Jump to: navigation, search

This page explains how to use the Audacity Nyquist Prompt to test-run Nyquist code snippets.
The "Nyquist Basics" tutorials start with very simple examples at the top of each page with increasing complexity towards the end. You do not necessarily need to work through to the end of each page to be able to understand the other tutorials. If you feel confused, try any of the other Nyquist Basics tutorials, in any order. Every "Basics" page contains lots of notes and hints where you can find extra information.
Related article(s):
Please note: Nyquist supports both a LISP syntax and a more conventional syntax called SAL. You can choose a manual from a link above based on which syntax you are using.
Steve 07Apr18 ToDo-1 Images need updating

Setting up

This article uses LISP syntax for Nyquist commands. For an introduction using SAL syntax, see Nyquist Basics: The Audacity Nyquist Prompt With SAL.

The Audacity Nyquist prompt appears in Audacity's "Effect" menu, which is activated only if an Audacity audio track exists and at least some audio in it is selected.

For testing generate commands in Nyquist, you can install the Nyquist Generate Prompt plug-In, or simulate the behavior of a generate type plug-in in the Nyquist Prompt effect.

Load a Sound File

Sound files are imported into Audacity via:

File > Import > Audio or the shortcut CTRL + SHIFT + I

If you have no pre-existing sound files to work with, you can create your own mono or stereo tracks via the Audacity "Generate" menu.

Create a Mono Track

Click Generate > Tone. Leave everything as-is and just click "OK". A mono track will appear in the Audacity window:

Audacity mono track

Create a Stereo Track

1. Create an empty stereo track.

  • In Audacity 1.2.x, click Project > New Stereo Track.
  • In Audacity 1.3.x-beta, click Track > Add New... > Stereo Track.

An empty stereo track will appear in the Audacity window.

2. Now click Generate > Tone. Leave everything as-is and just click "OK". The stereo tracks will become filled with the generated sound.

Audacity stereo track

The Nyquist Prompt

Select the track(s) and click Effect > Nyquist Prompt. Effects are not activated until at least a portion of the track(s) have been selected. You can select all tracks by pressing CTRL + A on the keyboard.

The Nyquist Prompt appears like this:

Audacity Nyquist prompt window

The Nyquist Interpreter

In the Nyquist Prompt window, type (print "hello") into the text field with the parentheses and quotes, as follows:

Audacity Nyquist prompt window print with quoted hello

NOTE: The Nyquist Prompt must see an open parenthesis as the first non-space character; otherwise it will assume this is written using SAL syntax. Otherwise, we might have just typed "hello", which worked in earlier versions of Audacity.

When you click OK, you should get a message box displaying the word hello:

Window displaying the word hello

The message box disappears when you click OK.

This, as you have learned, is how you can send messages to the user.

The Nyquist Debugger

After clicking OK in the "hello" message box, go back to Effect > Nyquist Prompt, and this time, type (print hello) with the parentheses but without the quotes:

Audacity Nyquist prompt window print with unquoted hello

Important: This time, click "Debug" instead of "OK".

You should first get a window displaying a "Nyquist did not return audio" or a similar message:

Window displaying Nyquist did not return audio

After clicking OK in the window above, a second window appears, displaying the error messages that Nyquist returned:

Window displaying Nyquist error: unbound variable

NOTE: The "Nyquist output" window only appears if you click "Debug" in the "Nyquist Prompt" window. If you click "OK", then, even with a Nyquist error, only the first window will appear.

Basic Nyquist Commands

The Nyquist manual entirely using LISP syntax is here: Nyquist Reference Manual.

The current Nyquist manual introducing SAL syntax is here: Nyquist 3.02 Reference Manual.

Audacity uses the 's' variable to reference the current audio file/selection. Thus, you can use basic commands such as 'mult' or 'sum' with 's' and the Nyquist prompt will replace the file/selection with the result (or as Audacity calls it, "returned audio").

Simple Examples

NOTE: These examples are focused upon using Audacity to manipulate digital signals (clearly Audacity is better suited to audio, but features such as Nyquist can open many other uses). For those interested, the signal used is an infra red (IR) sample from a remote control.

NOTE: Each code example will be shown once using LISP syntax, and again using SAL syntax. Either form can be used, but never try to mix them in a single program or prompt! Prior to Audacity Version 1.3.13 (Beta), only LISP syntax works.

Applying a DC offset to a signal

Original Signal before command.

Original Signal before command.

Type the following into the Nyquist Prompt (using LISP syntax):

 (sum s 1)

Or type the following equivalent SAL command:

 return s + 1

Resultant Signal after command.

The whole signal has now moved up to above zero.

Modulating with a carrier frequency

To multiply a signal with a generated carrier signal, you can use the following commands:

 (mult s (hzosc 19000))

Or the SAL equivalent:

 return s * hzosc(19000)

The (hzosc 19000) produces 19kHz sine wave carrier.

 (mult s (osc-pulse 19000 0))

Or the SAL equivalent:

 return s * osc-pulse(19000, 0)

The (osc-pulse 19000 0) produces 19kHz square wave carrier (note the 0 is the bias or 50/50 duty cycle, -1 to 1 = 0%-100% pulse-width ). Applying the 19kHz square wave carrier obtains this result.

Resultant Signal after command.

The top and bottoms of the signal can then be clipped using the Hard Limiter option from the effects menu (0dB limit and Wet level 1) if required.

The above examples show how you can use the many Nyquist commands to perform basic signal processing without using scripts.

NOTE: Unfortunately, this isn't the end of the road for this sample; it is near, but the curved "head/tail" of the signal causes a problem for the digital signal being produced [and it was also upside-down, too...]. This will hopefully form the basis of some more complex examples, since I shall need to use Nyquist to:
  1. Find the zero crossing points
  2. Then only apply the carrier frequency to those regions above zero.
Or find another suitable command...

Thanks to Forum moderators stevethefiddle & kozikowski for their help with Audacity & Nyquist.