Change Trim End plug-in into Trim Start?

Effects, Recipes, Interfacing with other software, etc.
Forum rules
If you require help using Audacity, please post on the forum board relevant to your operating system:
Windows
Mac OS X
GNU/Linux and Unix-like
Post Reply
JayAudacity
Posts: 2
Joined: Tue Dec 28, 2010 5:49 am
Operating System: Please select

Change Trim End plug-in into Trim Start?

Post by JayAudacity » Tue Dec 28, 2010 4:00 pm

Hey,

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

Code: Select all

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

steve
Site Admin
Posts: 81653
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

Re: Change Trim End plug-in into Trim Start?

Post by steve » Tue Dec 28, 2010 6:38 pm

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/e ... l#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/e ... t3.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

Code: Select all

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

Code: Select all

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

Code: Select all

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

Code: Select all

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

Code: Select all

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

Code: Select all

;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
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

JayAudacity
Posts: 2
Joined: Tue Dec 28, 2010 5:49 am
Operating System: Please select

Re: Change Trim End plug-in into Trim Start?

Post by JayAudacity » Tue Dec 28, 2010 6:51 pm

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

Liqui
Posts: 4
Joined: Sun Sep 01, 2013 5:24 pm
Operating System: Please select

Re: Change Trim End plug-in into Trim Start?

Post by Liqui » Sun Sep 01, 2013 5:34 pm

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?

steve
Site Admin
Posts: 81653
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

Re: Change Trim End plug-in into Trim Start?

Post by steve » Sun Sep 01, 2013 11:33 pm

Liqui wrote: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/ ... l#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.
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

Liqui
Posts: 4
Joined: Sun Sep 01, 2013 5:24 pm
Operating System: Please select

Re: Change Trim End plug-in into Trim Start?

Post by Liqui » Tue Sep 03, 2013 6:59 pm

Code: Select all

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

steve
Site Admin
Posts: 81653
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

Re: Change Trim End plug-in into Trim Start?

Post by steve » Tue Sep 03, 2013 9:40 pm

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/ny ... rompt.html) and click the "Debug" button rather than the OK button.

In this case the active part of the code is:

Code: Select all

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

Code: Select all

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/Nyqui ... reo_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. ;)
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

Post Reply