1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-06-17 00:20:06 +02:00

Nyquist: Fix delay importing LISPy Scripting Commands

This commit is contained in:
SteveDaulton 2020-02-16 12:42:07 +00:00
parent 60a113da26
commit b7db5af4c5

View File

@ -1,7 +1,7 @@
;;; A collection of helper functions and macros to make scripting Audacity commands ;;; A collection of helper functions and macros to make scripting Audacity commands
;;; easier and more Lisp-like. ;;; easier and more Lisp-like.
;;; ;;;
;;; Copyright 2018 - 2019 Audacity Team ;;; Copyright 2018 - 2020 Audacity Team
;;; Steve Daulton ;;; Steve Daulton
;;; Released under terms of the GNU General Public License version 2: ;;; Released under terms of the GNU General Public License version 2:
;;; http://www.gnu.org/licenses/old-licenses/gpl-2.0.html ;;; http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
@ -22,31 +22,38 @@
(defun aud-get-command (id) (defun aud-get-command (id)
;;; Return command signature from id string ;;; Return command signature from id string or NIL.
(let* ((helpstr (format nil "Help: Command=~s Format=LISP" id)) (let* ((helpstr (format nil "Help: Command=~s Format=LISP" id))
(cmd-sig (first (aud-do helpstr)))) (cmd-sig (aud-do helpstr)))
(eval-string (quote-string cmd-sig)))) (cond
((not (listp cmd-sig)) (error "Unknown error in aud-do" cmd-sig))
((string-equal (first cmd-sig) "Command not found") nil)
(t (setf cmd-sig (first cmd-sig))
(eval-string (quote-string cmd-sig))))))
(defun aud-import-command (command-sig &optional func-name) (defun aud-import-command (cmd &optional func-name)
;;; Generate a LISP function from Audacity command signature. ;;; Generate a LISP function from Audacity command ID or signature.
;;; If supplied, the generated function name will be 'func-name', otherwise ;;; If supplied, the generated function name will be 'func-name', otherwise
;;; it will be the command id, preceeded by 'aud-'. ;;; it will be the command id, preceeded by 'aud-'.
(let ((id (second (assoc 'id command-sig))) (when (stringp cmd)
(params (second (assoc 'params command-sig))) ;; cmd is the id, so get the command signature
(let ((id cmd))
(setf cmd (aud-get-command id))
(if cmd
(aud-import-command cmd func-name)
(error "in aud-import-command, invalid argument" id))))
(let ((id (second (assoc 'id cmd)))
(params (second (assoc 'params cmd)))
(func-def "(defun aud-") (func-def "(defun aud-")
(func-kwargs "(&key ") (func-kwargs "(&key ")
(func-body "") (func-body ""))
(aud-do-params ())
(validate-func "")
(aud-do-command ""))
(if func-name (if func-name
(setf func-def (format nil "(defun ~a " func-name)) (setf func-def (format nil "(defun ~a " func-name))
(string-append func-def id " ")) (string-append func-def id " "))
(dolist (p params) (dolist (p params)
(let* ((key (second (assoc 'key p))) (let* ((key (second (assoc 'key p)))
(type (second (assoc 'type p))) (type (second (assoc 'type p)))
(default (second (assoc 'default p)))
(enums (second (assoc 'enum p))) (enums (second (assoc 'enum p)))
; The kwarg value must be a valid Lisp variable name (no spaces). ; The kwarg value must be a valid Lisp variable name (no spaces).
(val (char-remove #\Space key))) (val (char-remove #\Space key)))
@ -63,7 +70,6 @@
" (when " val " " (when " val "
(unless (validate " val " \"" type "\" " enums ")(error \"bad argument type\" " val ")) (unless (validate " val " \"" type "\" " enums ")(error \"bad argument type\" " val "))
(push (format nil \"\\\"" key "\\\"=~s \" " val ") params))\n"))) (push (format nil \"\\\"" key "\\\"=~s \" " val ") params))\n")))
;; concatenate strings to build the complete function. ;; concatenate strings to build the complete function.
(string-append func-def func-kwargs "&aux (params ()))\n" (string-append func-def func-kwargs "&aux (params ()))\n"
" ;; Push validated 'val's onto 'params' list " ;; Push validated 'val's onto 'params' list
@ -90,33 +96,39 @@
(eval-string func-def))) (eval-string func-def)))
(defun aud-import-commands () (defun aud-generate-command-stubs (cmd-list)
;;; Import all Audacity "Commands" as LISP functions ;; Generate one stub for each function.
;;; Function names prefix the command id with "aud-". ;; Stubs check that command is actually available before
(dolist (command (aud-get-info "Commands")) ;; generating the Lisp function.
(aud-import-command command))) ;; This function is for internal use only.
(dolist (cmd-id cmd-list)
(let ((func-def (format nil
"(defun aud-~a (&rest args)
(if (string-equal (first (aud-do \"Help: Command=~a\")) \"Command not found\")
(error \"Command unavailable\" ~s))
(aud-import-command ~s)
(let ((arg-string \"\") (cmd-string \"(aud-~a \"))
(dolist (arg args)
(setf arg-string (format nil \"~a ~a\" arg-string arg)))
(setf cmd-string (format nil \"~a~a)\" cmd-string arg-string))
(eval-string cmd-string)))"
cmd-id cmd-id cmd-id cmd-id cmd-id "~a" "~s" "~a" "~a")))
(eval-string func-def))))
(defun aud-import (id &optional func-name) ;; Hard coded list because "GetInfo:" is slow and we can't yet exclude
;;; Import one Command by ID. ;; Nyquist plug-ins (Nyquist plug-ins can't run from Nyquist Macros).
;;; Example (aud-import "tone") ;; TODO: Create a fast scripting command to return this list instead of relying on hard coded.
;;; Creates a function (aud-tone :frequency 440 :amplitude 0.8 :waveform "Sine") (aud-generate-command-stubs
(let ((cmd (aud-get-command id))) (list "Amplify" "AutoDuck" "BassAndTreble" "ChangePitch" "ChangeSpeed"
(aud-import-command cmd func-name))) "ChangeTempo" "Chirp" "ClickRemoval" "Compressor" "DtmfTones"
"Distortion" "Echo" "FadeIn" "FadeOut" "FilterCurve" "FindClipping"
"GraphicEq" "Invert" "LoudnessNormalization" "Noise" "Normalize"
(defun aud-import-effects () "Paulstretch" "Phaser" "Repeat" "Repair" "Reverb" "Reverse"
;;; Import built-in effect commands "Silence" "SlidingStretch" "Tone" "TruncateSilence" "Wahwah"
(let ((common (list "Amplify" "AutoDuck" "BassAndTreble" "ChangePitch" "ChangeSpeed" ;; Scriptable Commands
"ChangeTempo" "Chirp" "ClickRemoval" "Compressor" "DtmfTones" "CompareAudio" "Demo" "Export2" "GetInfo" "GetPreference" "Help"
"Distortion" "Echo" "FadeIn" "FadeOut" "FilterCurve" "FindClipping" "Import2" "Message" "OpenProject2" "SaveProject2" "Screenshot"
"GraphicEq" "Invert" "LoudnessNormalization" "Noise" "Normalize" "SelectFrequencies" "SelectTime" "SelectTracks" "Select" "SetClip"
"Paulstretch" "Phaser" "Repeat" "Repair" "Reverb" "Reverse" "SetEnvelope" "SetLabel" "SetPreference" "SetProject" "SetTrackAudio"
"Silence" "SlidingStretch" "Tone" "TruncateSilence" "Wahwah"))) "SetTrackStatus" "SetTrackVisuals" "SetTrack"))
(dolist (cmd common)
(aud-import cmd))))
(aud-import-effects)
;;; Uncomment the next line to load all "AUD-" commands.
;(aud-import-commands)