Change Trim End plug-in into Trim Start?

Hey,

I’m wanting to change this plug-in to trim the start instead of the end, but not sure how.

;nyquist plug-in
;type process
;version 1
;type process
;name "Trim Track"
;action "Trimming end of track..."
;info "By Steve Daulton. Released under GPL v2.nTrims selected length from end of selection"

;control trm-time "How much to trim" real "milliseconds" 150 0 1000

(defun trim (s-out cut)
   (setq cut (/ cut 1000.0))
   (extract-abs 0 (- (get-duration 1) cut)(cue s-out)))

(multichan-expand #' trim s trm-time)

Any help greatly appreciated,

Thanks.

Depending on exactly what you are doing there may be easier ways to trim the start of a selection, but in answer to your question:

The “trimming” is done with the function (extract-abs) http://www.audacity-forum.de/download/edgar/nyquist/nyquist-doc/manual/part6.html#index405
The syntax is: (extract-abs start stop beh) where start and stop are in seconds.

Because we are trimming a sound, we must convert this to a behaviour (extract only works on behaviours and not directly on sounds - see here for more information: http://www.audacity-forum.de/download/edgar/nyquist/nyquist-doc/manual/part3.html#23 ) and we do this using the function (cue sound)

In the original code, we extract from time=0 to time=(duration-t) where “duration” is the original length of the selection and “t” is the amount being trimmed.
To trim from the beginning of the track rather than the end, we need to make start=(the amount of time to trim) and stop=(total length of the selection).

So instead of

(extract-abs 0 (- (get-duration 1) cut)(cue s-out))

we would have

(extract-abs cut (get-duration 1) (cue s-out))

If we are going this far, then we may as well allow both options like this:

;control trm-start "Trim from start" real "milliseconds" 150 0 1000
;control trm-end "Trim from end" real "milliseconds" 150 0 1000

(defun trim (s-out scut ecut)
   (setq scut (/ scut 1000.0))
   (setq ecut (/ ecut 1000.0))
   (extract-abs scut (- (get-duration 1) ecut)(cue s-out)))

(multichan-expand #' trim s trm-start trm-end)

One slight problem with this is; what happens if the total amount being trimmed is greater than the length of the selection?
In such a case it would be useful to have a meaningful error message, so as a matter of good practice we should test the input data (from the slider controls) to ensure that it is valid, and output a meaningful error message if there’s a problem.

To do this we can initialise a variable to an empty string, and add text to it if there are errors.
Let’s call this variable “err-msg”
(any words after a semi-colon are comments and are ignored by Nyquist)

(setq scut (/ scut 1000.0)) ; convert to seconds
(setq ecut (/ ecut 1000.0)) ; convert to seconds
(setq err-msg "") ; initialise error message to empty string

; trimmed amounts scut and ecut cannot be negative
(if (or (< scut 0)(< ecut 0))
  (setq err-msg (strcat err-msg "Trim amounts cannot be negative")))

;trimmed length must be greater than zero
(if (>= (+ scut ecut)(get-duration 1))
  (setq err-msg (strcat err-msg "Total amount being trimmed must be less than length of selection.n")))

Then before processing the audio, test the length of the error message. If it is zero, no errors have occurred and we can process the audio. If the length is greater than 0 then errors have occurred and we need to output an error message:

(if (> (length err-msg) 0) ; if length of error message greater than 0
  (format nil (strcat "Errorn" err-msg)) ; output error message
  (multichan-expand #' trim s scut ecut)) ; else process audio

Putting it all together:

;nyquist plug-in
;type process
;version 1
;type process
;name "Trim Selection"
;action "Trimming end(s) of selection..."
;info "By Steve Daulton. Released under GPL v2.nTrims selected length from end(s) of selection."

;; By Steve Daulton. September 2010.
;; Released under terms of the GNU General Public License version 2 
;; http://www.opensource.org/licenses/gpl-license.php

;control scut "Trim from start" real "milliseconds" 0 0 1000
;control ecut "Trim from end" real "milliseconds" 0 0 1000

(setq scut (/ scut 1000.0)) ; convert to seconds
(setq ecut (/ ecut 1000.0)) ; convert to seconds
(setq err-msg "") ; initialise error message to empty string

; trimmed amounts scut and ecut cannot be negative
(if (or (< scut 0)(< ecut 0))
  (setq err-msg (strcat err-msg "Trim amounts cannot be negative.n")))

;trimmed length must be greater than zero
(if (>= (+ scut ecut)(get-duration 1))
  (setq err-msg (strcat err-msg "Total amount being trimmed must be less than length of selection.n")))

(defun trim (s-out scut ecut)
   (extract-abs scut (- (get-duration 1) ecut)(cue s-out)))

(if (> (length err-msg) 0) ; if length of error message greater than 0
  (format nil (strcat "Errorn" err-msg)) ; output error message
  (multichan-expand #' trim s scut ecut)) ; else process audio

and here it is as a plug-in: trim.ny

Thanks for the detailed response Steve really appreciate it, works great.

Hello Steve,

since wednesday I try to modify different trim scipts I found @ this forum (trim, trim extended).
I want to cut / trim 30 seconds of ca. 60 mp3 files.
I´d like to make 30 seconds sample starting from the beginning. So the “rest”/ everything after the first 30 seconds need to be cut out.

Can you help me?

For a mono track:
(extract-abs 0 30 (cue s))
http://www.cs.cmu.edu/~rbd/doc/nyquist/part8.html#index563

“cue” is used so that the sound “s” is evaluated as a “behaviour” - that’s a bit hard to explain, but generally if you want to use a “sound” in a function that acts on a “behaviour” (shown as beh in the manual) then you will usually need to use “cue” with the sound as its argument. This is described here: http://www.cs.cmu.edu/~rbd/doc/nyquist/part4.html#28 (but don’t worry if you don’t fully understand this yet - just note cases where “cue” is used.

;nyquist plug-in
;type process
;version 1
;type process
;name "Trim Selection"
;action "Trimming end(s) of selection..."
;; Released under terms of the GNU General Public License version 2 
;; http://www.opensource.org/licenses/gpl-license.php

(extract-abs 0 30 (cue s))

I tried this code but get “Nyquist did not return audio” back. :frowning:

I’m guessing that you are trying to apply that plug-in code to a stereo track?
“extract” can only work on a “sound” (mono).

A good way to debug these sort of things, when there are no user controls, is to run the active part of the code in the Nyquist Prompt effect (http://manual.audacityteam.org/o/man/nyquist_prompt.html) and click the “Debug” button rather than the OK button.

In this case the active part of the code is:

(extract-abs 0 30 (cue s))

If you enter that in the Nyquist Prompt and apply it to a mono track, it should work.
Does it?

If you try applying that to a stereo track and use the Debug button you should see an error output something like:

error: bad argument type - #(#<Sound: #b108d558> #<Sound: #b108d5a0>)
...
...

Is that what you get?

Stereo sounds are handled as an array with two elements. Each element is a sound. The first element is the left channel and the second element is the right channel.
Audacity passes the audio data to Nyquist as the value of “S”, which is either a “sound” (if it is a mono track) or an array of two sounds (if it is a stereo track).

The (somewhat cryptic) error message is telling you that #(#<Sound: #b108d558> #<Sound: #b108d5a0>) is the wrong type of variable.
That is because #(#<Sound: #b108d558> #<Sound: #b108d5a0>) is an array of two sounds, which we have passed to Nyquist in the variable “S”, but “extract” requires a “sound” not an “array”.

The Nyquist Plug-In reference on the wiki describes how to handle stereo sounds: http://wiki.audacityteam.org/wiki/Nyquist_Plug-ins_Reference#Stereo_Tracks
Have a crack at it and see if you can work out how to make your plug-in work. Give a shout if you get stuck, and share your plug-in with the world (post it here) if you succeed. :wink: