How To Get Selection Values (Nyquist) ?

Hello :stuck_out_tongue: ,

This code returns the value “0.000000”, the code should return “3.0”.
How to get the value “3.0” ?

(AUD-DO "SelectTime: Start=2.0 End=5.0")
(setf duration (- (get '*selection* 'end) (get '*selection* 'start)))
(list (list 0 duration txt))

Nyquist is included in Audacity, but is separate from Audacity. It “knows” about the Audacity project from data that is passed to it just before the script runs.

When a Nyquist plug-in is launched, (including Nyquist code in the Nyquist Prompt), Audacity passes many property lists and other data to Nyquist. Some of the data depends on the plug-in type.

In addition to normal Nyquist functions, it is also able to pass “Macro” commands to Audacity. Macro commands are actioned by Audacity, not by Nyquist. This is an important distinction.

(AUD-DO "SelectTime: Start=2.0 End=5.0")

Here you are sending a Macro command “SelectTime:” from Nyquist to Audacity. Audacity will then create a time selection from 2 to 5 seconds. It is important to note that Nyquist is just the messenger and has no knowledge of what Audacity does with that command. Nyquist does not know that the selection has been modified.

If the selection was 0 to 10 seconds when Nyquist was launched, then Nyquist knows that 10 seconds of audio is selected. If you change that selection, Nyquist does not know that it has been changed, and assumes that 0 to 10 seconds is still selected. However, if you send another Macro command to do something with the selection (for example, to delete the selection), then Audacity will apply that command to the updated selection.

The important thing to remember is that from Nyquist code you can modify the project with Nyquist return values (typically by returning modified audio to the selected track), OR by sending Macro commands to tell Audacity to modify the project. You CANNOT change the project with Nyquist AND Macros from within a Nyquist plug-in.


Create a Macro.
The first command in the Macro: “SelectTime: Start=2.0 End=5.0”
The second command in the Macro: Call the Nyquist code:

(setf duration (- (get '*selection* 'end) (get '*selection* 'start)))
(list (list 0 duration txt))

I’ve just learned Lisp and just trying to code.
Thanks.

Did my reply make sense and answer your question?

For information:
The easiest way to get the length of the current selection in a Nyquist “Effect” (;type process) is with “GET-DURATION”.

Example: Select some audio, then open the Nyquist Prompt effect, and run this code:

;type process
(print (get-duration 1))

The printed value is the length of the selection in seconds.
This also works for ;type analyze

The reason that it works is that for “process” and “analyze” type plug-ins, Nyquist treats the selection as one “unit” of time.

For “generate” and “tool” plug-ins, Nyquist uses seconds as the unit of time, so (get-duration 1) returns 1, and (get-duration 2) returns 2.