Part 2 - “sounds”.
The way that Nyquist gets sound from Audacity, is that Audacity passes the selected audio to Nyquist as the value of a symbol “S”.
A “symbol” can be thought of as the name of a “thing”. It does not really matter what that “thing” is, and that “thing” may change. The key concept is that we have a “thing” and it has a “value” and a “name”. It sounds really unprofessional to keep writing “thing”, so let’s use the proper term: a “variable”. As the name implies, the value of a “variable” may change.
The name of a variable is a single string of letters. Nyquist is not “case sensitive”, that is, it ignores upper/lower case distinctions in variable names (and commands).
Names should be one word (no spaces) starting with a letter, and made of only letters and numbers. Normally we would choose names that are reasonably short but meaningful.
Unlike some other computer languages, Nyquist does not care what “type” of thing the variable is. Whether it is a numeric value, a string of letters (called a “string”), or even a “sound”, it is just a “variable” to Nyquist.
To set the value of a variable, we use the command (the “function”) SETQ or the function SETF.
SETF is the more powerful of the two, so we shall use that for now.
As Nyquist is based on the LISP programming language, it follows the same rules of always placing functions in parentheses (“brackets”). Thus to create a variable called “my-number” and give it the value “6”, we can write:
(setf my-number 6)
Because Nyquist is case insensitive, this is identical to writing:
(SETF My-NuMbER 6)
but generally we would want to be consistent with our use of upper and lower case characters.
The number “6” is a type of variable called an “integer” (a whole number). Fractional numbers are referred to as “floats” (short for “floating point number”).
Thus you can also have
(setf my-number 6.2)
which sets the variable called “my-number” to the fractional (“float”) value “6.2”.
Another type of “thing” is a “character”. A “character” is a single text element, like the letter “A” or “a” or “3” or “@”. Note that the “character” 3 is not the same as the integer number 3 - one is a text element and the other is a number.
We could set a variable called “my-character” to have the value of #C. The “#” indicates that “C” means “character C”.
(setf my-character #C)
Another type of “thing” is a “string” (a “string of characters”). Normally we would put a string in double quotes to indicate that it is a string. For example:
(setf my-string "this is a string")
Yes I know, I said we would be dealing with “sounds” - let’s do that now:
The reason that I’ve been going on about different “data types”, is that Nyquist treats “sounds” as just another “type” of data. Just as we can have a variable called “my-variable” and it can have a value that is an integer, or a float, or a character, or a string, it could have a value that is a “sound”.
In version 3 effect plugins, there is one pre-defined variable that has the name “S” (or “s”, upper or lower case does not matter) that has the value which is the “sound” that is passed from the selected track in Audacity to Nyquist.
Stereo sounds are a bit different and we will come to that later - for now we will deal just with single track mono sounds.
In the Nyquist Prompt effect, if you select some audio and then run the code:
(print s)
You will probably notice that Nyquist is doing something (if enough audio is selected then you will see a progress bar), and then it finishes. Nothing appears to have happened.
Now try doing the same, but use the Debug button. The Debug button can be very useful for showing what is going on, especially if there are errors.
After running with Debug, you will see in the debug window, something like:
#<Sound: #b1b732d0>
That is a printed representation of the “sound” object that was the value of “S”.
Let’s do something with a sound. What happens if we multiply a sound by a numerical value?
Try this in the Nyquist Prompt:
(mult s 0.3)
Try that with different values. Can you see what is happening?
What happens if we add a numerical value to a sound?
Try this in the Nyquist Prompt:
(sum s 0.3)
Try that with different values. Can you see what is happening?
“SUM” is a function that can add sounds, or numbers, or both.
There is also a function called “SIM” (from the word “simultaneous”), which is identical to the function “SUM”.
(sim s -0.2)
(sum 3 4 5)
(sim 1 2 20)
To complete this section, let’s go back to our delay effect and see if we can create a simple delay.
(sim
(mult 0.8 s)
(at-abs 0.5 (cue (mult 0.5 s))))
Can you see what is happening?
Each of the functions used in this code is defined in the Nyquist manual - do look them up:
SUM http://www.cs.cmu.edu/~rbd/doc/nyquist/part8.html#index591
MULT http://www.cs.cmu.edu/~rbd/doc/nyquist/part8.html#index593
AT-ABS http://www.cs.cmu.edu/~rbd/doc/nyquist/part8.html#index560
CUE http://www.cs.cmu.edu/~rbd/doc/nyquist/part8.html#index328 (don’t worry about this one just yet)
Try that code in the Nyquist prompt with different numeric values. Can you see what it is doing?
Finally (for this section), lets put that code into out plugin.
If you recall, we created a control:
;control delay "Delay by" real "seconds" 1 0 10
That creates a variable called “delay” and gives it a value between 0 and 10 (default value “1”). We can use that value in our code by replacing the “absolute time value” (which was originally 0.5) with our “variable”:
;nyquist plugin
;version 3
;type process
;name "A Delay..."
;control delay "Delay by" real "seconds" 1 0 10
(sim
(mult 0.8 s)
(at-abs delay (cue (mult 0.5 s))))
Thus ends part 2. How’s it going? Any questions? Can you add controls to set the levels for the original sound and the delayed sound?