Trim Silence

Right, sorry, I meant Audacity itself…

And set to use 4GB of RAM to get the crash…

I understand it’s not supported and have set back to 1GB as to not crash…

I made a cool modification, check it out. I added “Leave at start (s)” and “Leave at end (s)” to the gui. These control how much sound to leave at the beginning of the sound and how much sound to leave at the end. It won’t expand your sound beyond what is there, so you have to have at least the amount of silence you want to keep. But what’s nice about this is that you can specify how much sound to keep. And it keeps the original sound. I found with the original TrimSilence plugin some quiet sounds at the beginning of the sound or at the end of the sound would get cut off, leaving a “clipped” beginning or end. In my voice recordings for telephone systems, I always leave 250ms at the beginning and end of every sound so that they don’t sound bunched up when played together. You can put a value of 0.250 to get 250ms, for example.

Enjoy, and please feel free to use and distribute this under the original GPLv2 license.

Sean Lazar
Square, Inc.
TrimSilence.ny (3.69 KB)

Hi.
First of all, let me thank Steve for this excellent plug-in. It made the difference in the way I process my digital library before making it ready to be actually used by myself. And this was the reason why I started using Audacity. I think Steve will be glad to know this. :slight_smile:

When I first saw this plug-in, around two years ago, there was no leading or trailing sound cut-off.
But I needed that badly, so I tried to learn this script language and made that improvement. I did it, but I wasn’t sure if this would work, since this was totally new to me, and Audacity 2.0.3 (?) was in a beta stage as were the codecs I was using (FLAC mainly) and there were several issues with the new ones.
So I decided not to publish this until I was sure this was working and that required a stable version of audacity which took about a year since that!
I’m very lazy when it comes to subscribe in forums. I’m not much of a writer…
But then I didn’t find the time and only now I managed to get back to this, since I’m about to do a major change on my audio processing workflow.
Meanwhile I already used this on thousands of files (mainly FLACs, MP3s) and it worked fine, so I’m now sure this improvement does work.

I had this on my Todo list since then! eheheh
And now that I finally came here to release it (I was not even registered in this forum), I checked that Sean Lazar has just released an improvement that does exactly I did. :slight_smile:
It’s good, altough frustrating because I could have spared him the trouble. :stuck_out_tongue:
So I own him an apology for being so late. But thanks for that big contribution, which was just what I needed when I first saw this plug-in.

So I’m not releasing it, although I may send it to both of you if you want to check it.
Apart from a small check that I do in order to assure there is a starting Leading space, everything is the same. I don’t even remember why I did this check…
I already tested both versions of the plug-in on a track with and without leading silence and they seem to work exactly the same way, under the same parameters. So, I don’t think there is any for “my version” of it since Sean is doing a fine job and I recommend it.

However, at least, I may take the oppotunity to thank Steve, and now also Sean, for their huge contribution. :slight_smile:
It has been very useful to me.

Is there any chance we could test and decide on a definitive version, ideally with the extra option to retain a fixed amount of leading/trailing silence, and post it to http://wiki.audacityteam.org/wiki/Nyquist_Effect_Plug-ins ?

I have to find this topic and suggest it to users at least once a month. Clearly the “demand” is there.


Gale

Sorry about the late reply, I must have missed seeing your post.

I like the idea, and yes I think it’s cool :sunglasses:
My only real criticism is that I think that allowing negative values for “how much silence to leave” is potentially dangerous. On first trying your modified version I misunderstood what the controls did - I confused it with the “Trim Extend” plug-in (Missing features - Audacity Support). Fortunately because I was only running tests, no harm was done.

If I understand correctly, your “use case”, and that of AudioSpirit, only requires positive values for the amount of silence to leave. I would be happier for these slider ranges to allow positive values only. There is also a slight technical problem (bug) when allowing negative values, in that it can cause Nyquist to try and delete more audio than there is. In this case the plug-in fails to do anything, and returns the error: “Nyquist did not return audio.” (but no indication to the user as to what went wrong). This error condition was previously avoided by these lines:

      ;; ensure at least 1 sample remains
      (if (>= start (get-duration 1))
        (setq start (/ (1- len) *sound-srate*)))

but that gets overridden if the “Leave at start (s)” setting is negative.

Another minor problem with this plug-in (all versions), is that “Preview” does not work correctly. I think that the best we can do about that at the moment is to disable Preview.

Other than that, it looks fine to me.

Generally I prefer short names for controls, but in this case I think it may be worth using a few more words to describe the controls. Rather than just “Leave at start (s)” and “Leave at end (s)”, how about “Amount of silence to leave at start (s)” and “Amount of silence to leave at end (s)” ?

I’m also thinking that 30 seconds might be big enough as a maximum. This will allow the time to be selected via the cursor keys or mouse wheel with a resolution of 0.1 seconds, rather than the current 1 second resolution, and provides 2 decimal places by default rather than the current 1 decimal place. I’d imagine that in most use cases only a fraction of a second would be required, with some cases requiring a “few” seconds. so I think that half a minute seems like plenty.

One of the limitations of this effect at the moment is that it can only be used on one track at a time (unless all the selected tracks happen to have exactly the same start and end times). Now that we have “version 4” plug-ins, I think that we can overcome that limitation. It’s rather late now, so I’ll look at that tomorrow.

An example to clarify this point:

Remember that this effect is called “Trim silence”. It should “trim” excess silence, and that is all it should do. (If the user wants to extend silence, then there is a plug-in designed to do that here: Missing features - Audacity Support)

If we have 2 tracks - the first track ends at 1 minute and the second track ends at 2 minutes.
Lets say that the first track includes 10 second of silence at the end, and the second track includes 1 second of silence.
If we select both tracks, then the selection ends at 2 minutes.
If we then Trim Silence and set the “Amount of silence to leave at end (s)” to 2 seconds:

  • Track 1 will be trimmed correctly (to 32 seconds)
  • Track 2 has “whitespace” trimmed off down to 2 minute 1 second. 1 second of “whitespace” will be rendered as silence. The silence at the end of track 2 has not been trimmed, but has been extended by 1 second of absolute silence. The plug-in has not done what it was designed to do (a “bug”).

I think that the solution is to first calculate how much whitespace needs trimming, and then take that into account when calculating the start and end times for “extract-abs”.

This function is perhaps a bit over the top for this need, but perhaps can also be used elsewhere. It looks for valid start and end times for trimming the current track:

;;; Find the earliest valid start time and
;;; latest valid end time.
(defun get-clip-limits ()
"Return a list (start time, end time)"
  (let ((clips (get '*track* 'clips))
        (channels (get '*track* 'channels))
        (start (get '*selection* 'start))
        (end (get '*selection* 'end))
        (limits (list nil nil)))
    (if (< start 0) (throw 'error "Selection cannot start before time=0."))
    (dotimes (i channels limits) ;;;;;;;;;;;;;;;;;;;;;;;;
      (if (> channels 1)
          (setf channel-clips (aref clips i))
          (setf channel-clips clips))
      (setf update-limits
        (do ((j 0 (1+ j))
             start-limit end-limit)
            ((= j (length channel-clips))
             (list start-limit (if (and end-limit (> end-limit 0)) end-limit nil)))
          ;; look for start
          (cond
            ((> (first (nth j channel-clips)) end)  ; reached end of selection.
              (return (list start-limit (if (and end-limit (> end-limit 0)) end-limit nil))))
            ((and (not start-limit) (>= (first (nth j channel-clips)) start))
              (if (not end-limit)   ; if we've found a clip end, start is not valid
                  (setf start-limit (first (nth j channel-clips))))))
          ;; now look for end
          (if (and (>= (second (nth j channel-clips)) start)
                   (<= (second (nth j channel-clips)) end))
              (setf end-limit  (second (nth j channel-clips))))
          (if (and end-limit (> (second (nth j channel-clips)) end))
              ; the end of this clip is not valid, 
              ; but we need to know that an end has been found.
              (setf end-limit -1))))
          ;; Update limits
          (case i
            (0 (setf limits update-limits))
            (T
              (if (or (not (nth 0 limits)) (not (nth 0 update-limits)))
                  (setf (nth 0 limits) nil)
                  (setf (nth 0 limits)(min (nth 0 limits)(nth 0 update-limits))))
              (if (or (not (nth 1 limits))(not (nth 1 update-limits)))
                  (setf (nth 1 limits) nil)
                  (setf (nth 1 limits)(max (nth 1 limits)(nth 1 update-limits)))))))))

(print (catch 'error (get-clip-limits)))

Sticking my oar in, “Silence to leave at start (s)” and “Silence to leave at end (s)”.

Would it make the effect too complex to add a control to Trim/Extend Silence to trim/extend based on “Threshold & amount” or “Amount”, then a “Threshold” control? I suppose your answer will be “Yes” but with two plugins doing something “similar”, one here in limbo and one on Wiki, it really gets confusing. :confused:


Gale

I can go with that :wink:
Thanks for the suggestion.

This one will soon not be “in limbo”, but on the wiki.

Yes there are similarities between this effect and “Trim / Extend”. There are also differences.

Trim Silence: determines time coordinates based on the first and last time that the audio crosses a specified dB threshold,
Trim / Extend: determines its time coordinates based on the selection start and end times.

When trimming:
Trim Silence: You select how much silence to leave.
Trim / Extend: You select how much to remove.

When extending:
Trim Silence: N/A
Trim / Extend: Extends the selection by padding with absolute silence.

When running in Chains:
Trim Silence: Invalid input should be impossible.
Trim / Extend: Error messages may be suppressed (on error, the plug-in does nothing).

If you want to trim silence from the start / end of a track, then “Trim Silence” is the plug-in to use.

If you want to extend the length of the selected audio by padding with silence, then “Trim / Extend” is the plug-in to use.

For other uses, you may be able to use either, or neither.


Trim Silence also has similarities with other effects, such as “Truncate Silence”, “Noise Gate”, “Time Shifter”, and others.
Nyquist plug-ins still have many interface limitations, and we have frequently run into problems when attempting to bundle lots of features into one plug-in. The current Nyquist plug-in interface does not lend itself well to “multi-function” effects, so my preference now is to make plug-ins that do one job well.

As we now have a larger interface, and your suggested shorter wording, I think it looks OK to make the range explicit:

“Silence to leave at start (0 to 30 seconds):”
“Silence to leave at end (0 to 30 seconds):”

Getting this to deal correctly with fringe cases was a bit tricky, so this version still has debug information in it. I’ll remove that before it is published to the wiki.

There is a limitation in Nyquist plug-ins that I was not happy with, which is that Audacity interprets returned audio of zero length as an error. Thus, if the threshold is set higher than the peak level of a selected track, the logically correct behaviour (deleting the entire track) is not possible. Unless handled by the plug-in code, this would give an unhelpful “Nyquist did not return audio” error. The earlier versions of Trim Silence handled this by trimming down to 1 silent sample (the closest that Nyquist plug-ins can come to returning “no audio”). I don’t think that it is really useful to leave the user with a single silent sample, so I’ve decided to replace that behaviour with a custom error message that tells the user that the selection is all below the threshold and suggests lower the silence threshold.

Note when testing that the purpose of this effect is to trim silence from the end(s) of track (or ends of audio clips) and not affect silences that occur “within” a track (within an audio clip). Thus, if the selection starts within an audio clip and ends beyond the end of the audio clip, the start should not be trimmed (even if below the threshold) because it is not “silence at the start”. In this case, only the end of the clip should be trimmed.

When installed (installation instructions) the plug-in will appear in the Effects menu as “Trim Silence 4…”. When published to the wiki the name will be changed to “Trim Silence…”
TrimSilence-4.ny (9.99 KB)
.

Steve asked me to test this to see if I could break it - I can’t (testing on W7-HP 64-bit audacity-win-r96d2e66-2.1.2-alpha-10-oct-15)

It seems very straightforward and understandable - tested

  1. with no silence at either end
  2. shorter silences than the required trim
  3. longer silences than the required trim (what it’s really intended for)
  4. tested the user and factory presets too.

The only query I have is: why does it have a grayed out preview button? If preview is not appropriate for this plug-in then why don’t we just lose it from the dialog?

Peter.

Thanks for testing.

I agree that it would be preferable to lose the Preview button completely rather than just grey it out, but that’s not handled by the plug-in code. The plug-in code tells Audacity that it doesn’t want Preview for this effect, and Audacity does what it does, which in the current version is to grey out the Preview button. After 2.1.2 has been released I’ll ask Leland if the Preview button can be omitted when not appropriate for the effect.

This plug-in is now available on the Audacity wiki: http://wiki.audacityteam.org/wiki/Nyquist_Effect_Plug-ins#Trim_Silence