Regular Interval Region Labels

Share your Audacity/Nyquist plug-ins here, or test drive the latest plug-ins submitted by Audacity users.

After testing a plug-in from this forum, please post feedback for the plug-in author.
Post Reply
steve
Site Admin
Posts: 81385
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

Regular Interval Region Labels

Post by steve » Thu Feb 11, 2021 2:24 pm

I wrote this to help with this post: viewtopic.php?p=416529#p416529
Although this can already be done using Audacity's included "Regular Interval Labels" (https://manual.audacityteam.org/man/reg ... abels.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) Downloaded 44 times
It can be installed in the usual way (see: https://manual.audacityteam.org/man/cus ... l#plug-ins), and will appear in the "Tools" menu.

For coders, here's the code:

Code: Select all

;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/Nyqu ... nce#header)
and the license notification.


The effect has two controls:

Code: Select all

;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:

Code: Select all

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

Code: Select all

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

The main function:

Code: Select all

(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)))))

Code: Select all

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

Code: Select all

  (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/ ... ef-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/Nyqu ... ce#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.

Code: Select all

(setf repeats (truncate (/ dur (+ dur skip))))
Finally we loop for the calculated number of times, and PUSH the labels onto the LABELS list.

Code: Select all

    (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/ ... ef-096.htm
and: http://www.cs.cmu.edu/~rbd/doc/nyquist/ ... #index1112
and: https://wiki.audacityteam.org/wiki/Nyqu ... urn_Values
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

Post Reply