Nyquist Manual SND-CONST Function Question

<>

Which version of the manual are you looking at?
Here it is written as: (snd-const value t0 srate duration)
http://www.audacity-forum.de/download/edgar/nyquist/nyquist-doc/manual/part6.html#index476

A standard feature in Nyquist (and Lisp) is that functions are written inside parentheses, followed by their arguments or parameters
(function-name argument-1 argument-2 …)

For getting to grips with the syntax, the XLisp manual is really helpful: XLisp
particularly in that you can look things up in the language reference section XLISP Language Reference and it gives examples.

I’m not sure what you mean there.
If you write:

(snd-const 0.5 0 44100 1)

That means, generate a constant value of 0.5, starting at time 0.0 with a sample rate of 44100Hz for a duration of 1 second

The problem here is that the result of the Nyquist function is sent back into an Audacity track. If you set t0 to 2, and duration to 3, then Nyquist will generate a constant signal with a duration of 3 seconds and a start time of 2 seconds, but when it gets put into an Audacity track, there is nothing to fill that first two seconds - not even silence, it’s a like vacuum, and so the signal in the Audacity track will start at 0 (or the beginning of the selection) regardless of the start time in Nyquist.

If you actually have something at t0=0, and then something at t0=2, then there is something for the later sound to be referenced to. For example, try running this:

(sim
(snd-const 0.3 0 44100 6)
(snd-const 0.6 2 44100 3)
)

<>

<>

Actually, commas can be used in Nyquist in special situations, but more usually they are typo errors that mess up your program :wink:

in the example (snd-const 0.5 0 44100 1), the 0.5 refers to the “value” (level) of the constant signal level.

Run this code, then look at the output against the vertical track scale:

(seq
  (cue (snd-const 0.5 0 44100 1))
  (cue (snd-const -0.5 0 44100 1))
  (cue (snd-const 0.8 0 44100 1))
  (cue (snd-const -0.8 0 44100 1))
)

What is produced here is 1 second at 0.5, followed (in sequence) by one second at -0.5, followed by 1 second at 0.8, followed by 1 second at -0.8 (all with sample rates of 44100Hz).

Copy and pasting is quite different from the way that Nyquist works in Audacity (and I agree that some of the things that happen when using Nyquist in Audacity appear to be quite bizarre and illogical).

Nyquist was originally written (by Rodger Dannemberg) as a stand-alone programming language for working with sound. Back in the early days of Audacity, the Nyquist language was shoe-horned into Audacity to provide a way for users to write their own plug-ins. See here for a longer version of the story: http://audacityteam.org/help/nyquist

When running Nyquist functions, sound from one track (mono or stereo) can be passed from Audacity to Nyquist within a special “Global” variable “s”. Other than whatever “s” is set to, Nyquist has no information about anything in Audacity. If you select from time=0 to time=3 seconds, or if you select from time=105 to time=107 seconds, all that Nyquist knows is that it has 2 seconds of audio. Nyquist has no idea of the context within Audacity of where that sound came from. Neither does it have any idea of where that sound goes when it is sent back to Audacity.

When Nyquist “returns” (produces a result that is) a sound, that sound is passed back into Audacity and Audacity writes it back into the selected track.

One of the most confusing parts of programming with Nyquist (in Audacity) is the way that time is handled. I don’t think that I can explain this very clearly as I’m only just getting to grips with it myself, but basically, there are two types of time. There is “absolute” time (1 second = 1 second) and there is “logical” time. In logical time, Nyquist uses the length of the selected track as “1 unit of time”.

This is further complicated by the fact that when audio is passed back to Audacity, Audacity has to fit that sound into the current track and does so using the track sample rate. What this means is that if Nyquist returns a sound that is at a different sample rate to the Audacity track, then the sound in Audacity will not be the same duration as it was in Nyquist.

Need some examples here I think…

If we have an Audacity track that has a sample rate of 44100 Hz, that means that each individual sample value is 1/44100 second away from its neighbour.
If we generate a sound in Nyquist that has a sample rate of 22050 Hz and a length of 1 second, then there are 22050 samples that make up that sound.
When that sound is passed back to Audacity, it is put into the track, but the track has a sample rate of 44100 Hz and 22050 samples will only occupy half a second when it has a sample rate of 44100 Hz.

Let’s try this with code.
When using (snd-const value t0 srate duration) the “srate” parameter is the sample rate.
If we use the following code with a track that has a sample rate of 44100 Hz, we will (correctly) expect to produce a sound that is 3 seconds duration:

(snd-const 0.5 0 44100 3)

However, if we create the sound with a sample rate of 22050 Hz, then there will only be half the number of samples returned from Nyquist, so it will become 1.5 seconds when place into a 44100 Hz track.

(snd-const 0.5 0 22050 3)

The situation can get even more confusing if you are using Audacity 1.2 because of the very limited manner that it handles audio clips.
If you’re not using Audacity 1.3, I would highly recommend that you upgrade to 1.3.12.

So that I can follow your descriptions better, how are you running your test scripts? from the “Nyquist Prompt” effect?
Are you using a Windows computer or do you have access to a Linux computer? (there’s a really good tool to help write Nyquist scripts called “Nyquist Workbench” but it’s only available as source code. Building the Nyquist Workbench on Linux is pretty easy, but much harder on Windows (and I know even less about building from source on Mac computers).