want to remove *SCRATCH* property after last track processed

Pitch Report plugin is progressing well – thanks for the help Steve!

I’m selecting all 12 tracks and running the PitchReport plugin.

The first time the plugin is run it looks for the existence of a PitchReport property on SCRATCH and if not found creates it with a value of t and prints a project header.

All the subsequent 11 invocations skip printing the project header.

The problem occurs if I run the plugin again. The PitchReport property on SCRATCH is still set.

Is there a way the plugin can tell when it is invoked on the last track? If so I could then remove the property then.

If not I’ll probably need to create a separate plugin just to remove that property.

Here’s what the current code looks like:

(defun print-project-header ()
  (putprop '*SCRATCH* t 'PitchReport)
  (format t "~a~%window-time\t~a~%sample-time\t~a~%sample-step-time\t~a~%~%"
    (get '*project* 'name )
    window-time
    sample-time
    sample-step-time))

(defun generate-frequency-table ()
  (if (not (get '*SCRATCH* 'PitchReport)) (print-project-header))
  ...

(generate-frequency-table)

Here’s how I solved the problem:

selection has a tracks property that contains a list of track numbers selected I’m saving the length of this list as a SCRATCH property named pitch-report-track-count and incrementing another property named pitch-report-track-number every time the plugin is invoked.

After the plugin has finished an invocation I check if track-number = track-count. If so the plugin is processing the last track and removes the pitch-report-* properties from SCRATCH.

This allows the plugin to generate a correct report when re-run with different initial settings as well as after updates to the plugin code.

Here’s an outline of the code:

;nyquist plug-in
;type analyze
;name "Pitch Report"

...

(setq track-count 1)
(setq track-number 1)

...

(defun print-project-header ()
  (putprop '*SCRATCH* (length (get '*selection* 'tracks )) 'pitch-report-track-count)
  (putprop '*SCRATCH* 0 'pitch-report-track-number)
  (format t "~a~%window-time\t~a~%sample-time\t~a~%sample-step-time\t~a~%tracks\t~a~%~%"
    (get '*project* 'name )
    window-time
    sample-time
    sample-step-time
    (length (get '*selection* 'tracks ))))

(defun remove-pitch-report-scratch-properties ()
  (remprop '*SCRATCH* 'pitch-report-track-count)
  (remprop '*SCRATCH* 'pitch-report-track-number))

(defun generate-frequency-table ()
  (if (not (get '*SCRATCH* 'pitch-report-track-count)) (print-project-header))
  (setq track-count (get '*SCRATCH* 'pitch-report-track-count))
  (setq track-number (+ 1 (get '*SCRATCH* 'pitch-report-track-number)))
  (putprop '*SCRATCH* track-number 'pitch-report-track-number)

  ...

  (if (= track-count track-number) (remove-pitch-report-scratch-properties)))

(generate-frequency-table)

Probably the simplest way:

(if (= (length (get '*selection* 'tracks)) (get '*track* 'index))
    (noise)       ;last track
    (hzosc 440))  ;other tracks

Yes, that’s much simpler. Printing project header JUST once now works fine without accessing SCRATCH. Thanks!