Running a plug-in once only

Yes I know the code is not very practical as is. It was intended just as a demonstration of ways to return one result from multiple selected tracks.

Something a little more practical:

;nyquist plug-in
;version 3
;type process
;name "Cross-over split..."
;info "Select one mono audio tracks and two empty mono tracks below it."

;control xfreq "Split at (Hz)" real "" 2000 500 5500

(setq amplitude (peak s ny:all))
(when (not (get '*scratch* 'scross-over-split-count))
  (putprop '*scratch* 0 'scross-over-split-count))

(cond
  ((> amplitude 0)
    (format t "amplitude > 0~%")
    (putprop '*scratch* s 'scross-over-split-sound)
    (putprop '*scratch* 2 'scross-over-split-count)
    (s-rest 0))
  ((= (get '*scratch* 'scross-over-split-count) 2)
    (format t "count = 2~%")
    (putprop '*scratch* 1 'scross-over-split-count)
    (highpass2 (get '*scratch* 'scross-over-split-sound) xfreq))
  ((= (get '*scratch* 'scross-over-split-count) 1)
    (format t "count = 1~%")
    (putprop '*scratch* 0 'scross-over-split-count)
    (let ((output (lowpass2 (get '*scratch* 'scross-over-split-sound) xfreq)))
      (remprop '*scratch* 'scross-over-split-sound)
      output))
  (t (format t "nil~%")
	nil))

An interesting (and somewhat annoying) thing here is that if applied to exactly 1 audio track with sound and 2 silent tracks below it, then the plug-in works as expected. High frequencies are returned to the first empty track, low frequencies to the second empty track and the debug window shows:

amplitude > 0
count = 2
count = 1

Now the annoyance; If more than two empty/silent tracks below the audio track are selected, then the debug window still displays:

amplitude > 0
count = 2
count = 1

which shows that the audio has indeed been processed, but because the final track returns nil, the plug-in exits and returns nothing to Audacity.
The problem can be worked around by replacing the last couple of lines with something like:

  (t (format t "nil~%")
	"Too many tracksn"))

but then Audacity shows “Too many tracks” for each additional empty track.

Two features that I think it would be very useful to have:

  1. A symbol, which like scratch survives from one track to the next, but unlike scratch does not survive beyond one run of the plug-in. This would provide a simple test from which the track number could be counted.
  2. For Nyquist to be aware of how many tracks have been selected.