Regular Interval Region Labels

I wrote this to help with this post: https://forum.audacityteam.org/t/how-to-remove-some-portion-of-audio-for-some-seconds/60298/1
Although this can already be done using Audacity’s included “Regular Interval Labels” (https://manual.audacityteam.org/man/regular_interval_labels.html) this version is simpler, so probably easier to use. For coders, it’s probably easier to understand too.

Here’s the plug-in:
RegularIntervalRegionLabels.ny (871 Bytes)
It can be installed in the usual way (see: https://manual.audacityteam.org/man/customization.html#plug-ins), and will appear in the “Tools” menu.

For coders, here’s the code:

;nyquist plug-in
;version 4
;type analyze tool
;debugbutton false
;debugflags trace
;name "Regular Interval Region Labels"
;author "Steve Daulton"
;release 2.4.2
;copyright "Released under terms of the GNU General Public License version 2 or later."

;; http://www.gnu.org/licenses/old-licenses/gpl-2.0.html

;control dur "Label length" float "seconds" 3 0 100
;control skip "Distance between labels" float "seconds" 10 0 100

(defun make-labels ()
  (let ((dur (/ len *sound-srate*))
        (labels ())
        (start skip)
        (end (+ skip dur))
        repeats)
    (setf repeats (truncate (/ dur (+ dur skip))))
    (dotimes (i repeats labels)
      (push (list start end "") labels)
      (setf start (+ start dur skip))
      (setf end (+ start dur)))))

(if (or (=  dur 0) (= skip 0))
    "Error.\nBoth controls must be greater than zero."
    (make-labels))

The first part is the usual “plug-in headers” (see: https://wiki.audacityteam.org/wiki/Nyquist_Plug-ins_Reference#header)
and the license notification.


The effect has two controls:

;control dur "Label length" float "seconds" 3 0 100
;control skip "Distance between labels" float "seconds" 10 0 100

These set the values of DUR (length of each region label) and SKIP (distance between consecutive labels).

The main function is MAKE-LABELS, which I’ll come back to in a minute.

At the end we have a quick sanity check of the slider values, and if OK, the main function is called:

(if (or (=  dur 0) (= skip 0))
    "Error.\nBoth controls must be greater than zero."
    (make-labels))

In plain English:

IF "dur" OR "skip" are zero,
THEN return an error message,
ELSE run the "make-labels" function.

The main function:

(defun make-labels ()
  (let ((dur (/ len *sound-srate*))
        (labels ())
        (start skip)
        (end (+ skip dur))
        repeats)
    (setf repeats (truncate (/ dur (+ dur skip))))
    (dotimes (i repeats labels)
      (push (list start end "") labels)
      (setf start (+ start dur skip))
      (setf end (+ start dur)))))



(defun make-labels ()

The name of the function is “make-labels”. It takes no “arguments” (parameters).

  (let ((dur (/ len *sound-srate*))
        (labels ())
        (start skip)
        (end (+ skip dur))
        repeats)

Begin a block of code, with local variables: DUR, LABELS, START, END, REPEATS
(see: https://www.audacity-forum.de/download/edgar/nyquist/nyquist-doc/xlisp/xlisp-ref/xlisp-ref-148.htm)

LEN is a special variable that is pre-set as the length of the selection.
SOUND-SRATE is a special variable that is preset as the track sample rate.
See: https://wiki.audacityteam.org/wiki/Nyquist_Plug-ins_Reference#globals

First we determine how many times to repeat (the number of labels to produce).
We use TRUNCATE because we need an integer (whole number) value.
The sum of DUR and SKIP is the total length of a label plus the space between labels.

(setf repeats (truncate (/ dur (+ dur skip))))

Finally we loop for the calculated number of times, and PUSH the labels onto the LABELS list.

    (dotimes (i repeats labels)
      (push (list start end "") labels)
      (setf start (+ start dur skip))
      (setf end (+ start dur)))))

See: https://www.audacity-forum.de/download/edgar/nyquist/nyquist-doc/xlisp/xlisp-ref/xlisp-ref-096.htm
and: http://www.cs.cmu.edu/~rbd/doc/nyquist/part14.html#index1112
and: https://wiki.audacityteam.org/wiki/Nyquist_Plug-ins_Reference#Return_Values