Generating chords

Could you add piano chords as plugin ?

In Audacity you can easily generate a wide range of sounds, but generating a realistic piano sound would be extremely difficult. Most high quality piano “synthesizers” are actually “samplers” that use a large collection of audio files containing single piano notes. A piano “sample pack” library could be many MB, or even GB in size.

To generate a cord with simple tone sounds can be done like this:

;version 4
;type generate

(defun note (pitch duration)
  (stretch duration
    (mult (env 0.05 0.1 0.5 1.0 0.5 0.4)
          (osc pitch))))

(mult 0.3 ;scaling factor
  (sim (note c4 1.4)
       (note e4 1.4)
       (note g4 1.4)))

Yes, I didn’t mean realistic only small sample of key note tones combined together. Where do I input this ?
What does 1.4 mean ?

You can run that code in the “Nyquist Prompt” Nyquist Prompt - Audacity Manual


That’s the duration in seconds.

A quick explanation of the code:

;version 4
;type generate

This tells Audacity to use “version 4” syntax (there were earlier versions, but all new code should use the most recent version. Most of the documentation uses the latest version.
It also tells Audacity to treat the code as a “Generator” type effect (which we want because we are generating sounds). Generator effects treat durations as absolute lengths, so generating 1 second of sound will actually be 1 second long.


(defun note (pitch duration)
  (stretch duration
    (mult (env 0.05 0.1 0.5 1.0 0.5 0.4)
          (osc pitch))))

This defines a function called “note”.
The function takes two arguments (two “parameters”): “pitch” and “duration”.
Our “note” functions uses four built-in Nyquist functions:

  • STRETCH stretches a sound by a given amount Nyquist Functions
  • MULT multiplies numbers and/or sounds. In this case we are multiplying two sounds, an “envelope” and a tone.
  • ENV generates a control envelope. Introduction and Overview
  • OSC is a tone generator (an “oscillator”). Here we use the “pitch” argument to set the pitch of the oscillator. Pitch may be given in traditional “note + octave” format, like “C4” for middle C, or as a MIDI note value (for example, MIDI note 60 is middle C) Nyquist Functions

Notice that function names come first within a pair of parentheses (function-name arguments).


(mult 0.3 ;scaling factor
  (sim (note c4 1.4)
       (note e4 1.4)
       (note g4 1.4)))

This creates our chord sound.

The first line simply multiplies the sound that we are generating by a factor of 0.3 (each tone that we generate has an amplitude of 1.0, and we will be adding 3 notes together, so we need to scale it down to avoid distortion.
Notice that the text after the semicolon in the first line is just a comment. Everything on the line after the semicolon is ignored by Nyquist.

The “SIM” function (also called “SUM”) adds together sounds or numbers. In the context of sounds, it means “simultaneously”. Addition (summing) sounds is identical to “mixing” sounds.

We then have three calls to out “NOTE” function. Each call has two arguments, the pitch, and the duration in seconds.

There’s quite a lot of information about Nyquist programming in the Audacity manual: Nyquist - Audacity Manual

Here’s the code with a C major chord:

;version 4
;type generate

(defun note (pitch duration)
  (stretch duration
    (mult (env 0.05 0.1 0.5 1.0 0.5 0.4)
          (osc pitch))))

(mult 0.3 ;scaling factor
  (sim (note c5 1.4)
       (note e5 1.4)
       (note g5 1.4)))

and here’s the code with a C minor chord:

;version 4
;type generate

(defun note (pitch duration)
  (stretch duration
    (mult (env 0.05 0.1 0.5 1.0 0.5 0.4)
          (osc pitch))))

(mult 0.3 ;scaling factor
  (sim (note c5 1.4)
       (note ef5 1.4)
       (note g5 1.4)))

Note the “E5” is the note “E” octave 5,
“EF5” is the note E flat octave 5,
“CS5” (not used in the above example) would be the note C sharp octave 5.

This is what I need. Thank you!

Is it possible to implement graphical UI for easier and faster input of notes, basically graphical UI Nyquist Prompt ?

The available note names are:
A, B, C,D, E, F, G
S = sharp
F = flat
Characters are not case sensitive (you can use capital letters or lower case letters)

The German notation “H” (“B” for other countries) is not used.

Yes, you can design a GUI using various “widgets”.
To create a widget, you add a specially formed “comment” at the top of the Nyquist script. Nyquist ignores all comments, but Audacity looks for the “special” comments.
More information about widgets and Nyquist plug-in GUI’s here:

and here:

As an example that can run in the Nyquist Prompt:

;version 4
;type generate

;control pitch1 "First note name" choice "A,A#/Bb,B,C,C#/Db,D,D#/Eb,E,F,F#/Gb,G,G#/Ab" 3
;control  oct1 "First note octave" int "" 5 1 8
;control pitch2 "First note name" choice "A,A#/Bb,B,C,C#/Db,D,D#/Eb,E,F,F#/Gb,G,G#/Ab" 7
;control  oct2 "First note octave" int "" 5 1 8
;control pitch3 "First note name" choice "A,A#/Bb,B,C,C#/Db,D,D#/Eb,E,F,F#/Gb,G,G#/Ab" 10
;control  oct3 "First note octave" int "" 5 1 8
;control duration "Duration" float "seconds" 1.5 0.1 10

(defun note (pitch duration)
  (stretch duration
    (mult (env 0.05 0.1 0.5 1.0 0.5 0.4)
          (osc pitch))))

(defun chord (p1 p2 p3 duration)
  (mult 0.3
    (sim (note p1 duration)
         (note p2 duration)
         (note p3 duration))))

(defun note-symbol (note octave)
  ;; Return a note symbol from name and octave
  (setf note-name
      (case note
        (0 "A")
        (1 "AS")
        (2 "B")
        (3 "C")
        (4 "CS")
        (5 "D")
        (6 "DS")
        (7 "E")
        (8 "F")
        (9 "FS")
        (10 "G")
        (11 "GS")))
  (eval-string (format nil "~a~a" note-name octave)))

;; Call the "chord" function
(chord (note-symbol pitch1 oct1)
       (note-symbol pitch2 oct2)
       (note-symbol pitch3 oct3)
       duration)

steve, this is great, thank you!.
Could how create separate standalone script for 2.4.0 so that one doesn’t have to run the script every time, go through the debug window for the preview…
For the faster workflow.

Nyquist scripts can be turned into installable Nyquist plug-ins by adding a few “header” commands. No need to wait for Audacity 2.4.0, you could write an installable plug-in version for the current Audacity 2.3.3.

Several of the effects that are included in Audacity are “Nyquist plug-ins” (Limiter, Adjustable Fade, High Pass Filter, Low Pass filter, Notch Filter, Delay, Tremolo, Rhythm Track, Pluck, Risset Drum, Sample Data Import, Sample Data Export, and others).

This page explains which headers are necessary: Missing features - Audacity Support
Have a go, and give a shout if you get stuck.