Script to toggle mute/unmute for a clip?

Questions, discussion and recipes for scripting and batch processing in Audacity.
See also the "Scripting" section of the Audacity manual.
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
ratzefummel60
Posts: 4
Joined: Tue Feb 09, 2021 9:28 pm
Operating System: Windows 10

Script to toggle mute/unmute for a clip?

Post by ratzefummel60 » Tue Feb 09, 2021 9:48 pm

Not having furthered my basic programming skills since 1978, and hoping someone could help with a script:

1) to select the current clip (left arrow, right arrow, ctrl+,)
2) determine if that clip is muted or not (content and/or amplitude check)
3) if amplitude > 0 set entire clip volume to 0% using adjustable fade
3) if amplitude = 0 set entire clip volume to 100% using adjustable fade

ctrl+L is similar, but seems to preclude resurrecting the clip down the road...

Cheers from Munich, Germany!

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

Re: Script to toggle mute/unmute for a clip?

Post by steve » Tue Feb 09, 2021 9:57 pm

ratzefummel60 wrote:
Tue Feb 09, 2021 9:48 pm
1) to select the current clip (left arrow, right arrow, ctrl+,)
The easiest way to select an audio clip is to double click on it.
ratzefummel60 wrote:
Tue Feb 09, 2021 9:48 pm
2) determine if that clip is muted or not (content and/or amplitude check)
That's quite tricky with macros. Can't you just look to see if the clip is grayed out?

ratzefummel60 wrote:
Tue Feb 09, 2021 9:48 pm
3) if amplitude > 0 set entire clip volume to 0% using adjustable fade
3) if amplitude = 0 set entire clip volume to 100% using adjustable fade
Much easier to just "Normalize" to 0 dB (See: https://manual.audacityteam.org/man/normalize.html)

If you need to do this frequently, you could create a Macro with just the Normalize effect, and then create a keyboard shortcut to run the Macro. (See: https://manual.audacityteam.org/man/key ... ences.html). All you would then need to do is:

1. Double click on the clip
2. Shortcut for the "Normalize to 0 dB" macro.
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

ratzefummel60
Posts: 4
Joined: Tue Feb 09, 2021 9:28 pm
Operating System: Windows 10

Re: Script to toggle mute/unmute for a clip?

Post by ratzefummel60 » Wed Feb 10, 2021 11:25 am

Thanks for the quick reply! I learned I need to check out keyboard shortcuts since I want to run this on individual clips. The macros are done, but it is about individual clips and not complete projects or files.

My amplitude check idea is simply to have one shortcut instead of making two -- I will survive that.

Normalize to 0 dB would likely restore the clip hotter than the original. How can I restore to the original amplitude? (BTW - I am dealing with live brass recordings which are to be mixed together)

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

Re: Script to toggle mute/unmute for a clip?

Post by steve » Wed Feb 10, 2021 12:08 pm

ratzefummel60 wrote:
Wed Feb 10, 2021 11:25 am
Normalize to 0 dB would likely restore the clip hotter than the original.
You wrote:

3) if amplitude > 0 set entire clip volume to 0% using adjustable fade
3) if amplitude = 0 set entire clip volume to 100% using adjustable fade



If the original peak amplitude is 0 dB, then "Normalize" to 0 dB will effectively do nothing - the result will have a peak amplitude of 0 dB, so you don't need the conditional logic.
Try it :)
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

ratzefummel60
Posts: 4
Joined: Tue Feb 09, 2021 9:28 pm
Operating System: Windows 10

Re: Script to toggle mute/unmute for a clip?

Post by ratzefummel60 » Wed Feb 10, 2021 12:30 pm

Normalize to 0 dB makes my clip louder than original, and means the dynamic relation to other clips in that track are lost. I don't want to normalize the tracks until later in the project, after I have heard and selected the final clips.

dB is in minus, whereas I used "amplitude" for waveform size/volume. I want to be able to both mute and restore clips to original volume at will, independent of ctrl+z order.

My macro to mute (now key-associated) works. Fading that clip back in to 100%, however doesn't even work manually...

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

Re: Script to toggle mute/unmute for a clip?

Post by steve » Wed Feb 10, 2021 12:41 pm

To answer your "title" question:

In a macro, if you add the command "UnmuteTracks:" it will un-mute the current track if was muted, and do nothing if it was not muted.


Macros can be very useful for chaining multiple commands, but they don't do logical operations. They are just a simple list of commands.

There's no facility in Audacity macros for:

IF <something>
THEN <do something>
ELSE <do something else>

When considering how to implement a chain of actions, ask yourself if it is possible to do what you want without using "conditional" statements. If it is, then Macros are probably the best choice.

If you really must have conditional statements, then you need to use a proper scripting language, such as "Nyquist" (Audacity's built-in scripting language: https://manual.audacityteam.org/man/nyquist.html

Nyquist scripts can be made into plug-ins that can be installed, though for simple scripts it is easier to just run the script in the "Nyquist Prompt" effect (see: https://manual.audacityteam.org/man/nyquist_prompt.html)


A simple example of a Nyquist script that can be run in the Nyquist Prompt:

Say that you want:
If peak level < 0 dB, scale to half amplitude,
else if peak level >= 0 dB, scale to 10% amplitude.

This example is not possible without conditional logic, so we can't use a Macro, but we can use Nyquist. For simplicity, this script is for mono tracks only.
Here's the script:

Code: Select all

;version 4

(setf peak (get '*selection* 'peak))

;; 0 dB = +/- 1.0 on linear scale
(if (< peak 1)
    (mult *track* 0.5)
    (mult *track* 0.1))

9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

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

Re: Script to toggle mute/unmute for a clip?

Post by steve » Wed Feb 10, 2021 12:43 pm

ratzefummel60 wrote:
Wed Feb 10, 2021 12:30 pm
Normalize to 0 dB makes my clip louder than original
Then the clip had a peak level below 0 dB.
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

ratzefummel60
Posts: 4
Joined: Tue Feb 09, 2021 9:28 pm
Operating System: Windows 10

Re: Script to toggle mute/unmute for a clip?

Post by ratzefummel60 » Wed Feb 10, 2021 5:05 pm

Sorry we are circling around the meaning of 0 dB. I want to take clips with a visible waveform, flat-line them and retain the possibility of later, making them audible again to the original recorded volume. It is about being able to mute clips, then as desired, very likely in a different order, un-mute to original volume. I can live with two shortcuts and then there is no need for a conditional statement.

Right now I am only able to return clips to the original volume using ctrl+z - adjustable fade-in doesn't later restore the beginning of any clip that was set at 0% back to 100%. I had thought clips would retain the original amplitude as a memory characteristic, but fade-out seems to override it in the longer term.

The possibilities in audacity are really amazing, really mind-boggling in my case, as you certainly can tell. Will be reverting to cut and paste for this project. Thanks for your ideas and help Steve!

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

Re: Script to toggle mute/unmute for a clip?

Post by steve » Wed Feb 10, 2021 5:24 pm

ratzefummel60 wrote:
Wed Feb 10, 2021 5:05 pm
Sorry we are circling around the meaning of 0 dB
Let's clear that up to start.
  • The dB scale (for signals) uses "full scale" as the reference level. Thus a waveform that is full track height is said to have a "peak amplitude of 0 dB".
  • Most normal signal levels are less than "full scale", so being less that 0 dB they have negative numbers.
  • "dB" is a logarithmic scale: Half track height is about -6 dB, 1/4 track height is about -12 dB, 1/8th track height is about -18 dB ... (a factor of 2 is approximately 6 dB).
  • "Silence" is negative infinity dB.
The vertical scale of an audio track in Audacity is +/- 1, which is a linear scale.
If a waveform touches either the top (+1 linear scale) or the bottom (-1 linear scale), then it has a peak amplitude of 0 dB.
On the linear scale, silence has an amplitude of zero.
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

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

Re: Script to toggle mute/unmute for a clip?

Post by steve » Wed Feb 10, 2021 5:34 pm

ratzefummel60 wrote:
Wed Feb 10, 2021 5:05 pm
It is about being able to mute clips, then as desired, very likely in a different order, un-mute to original volume.
There's no really good way to do that right now, but please raise this question again when the next version of Audacity comes out. There is a good way to do it in the next version :)

ratzefummel60 wrote:
Wed Feb 10, 2021 5:05 pm
adjustable fade-in doesn't later restore the beginning of any clip that was set at 0% back to 100%.
You are correct. When an effect is applied in Audacity, it is directly applied to the track audio. This is called "destructive editing" or "WYSIWYG editing".

---

A little "trick" that I sometimes use is to have a muted track (I usually put it at the top of the project) to hold audio clips that I don't need right away. That track stays muted all the time, so it does not play and is not included when exporting the project.

If I have an audio clip that I want to temporarily silence, I do these steps:
  1. Double click on the clip to select it.
  2. "Ctrl + C" (Copy)
  3. "Ctrl + L" (Silence the selection) - this now acts as a placeholder.
  4. Click on the muted "dummy" track at the top
  5. "Ctrl + V" (paste)
When I want to put that clip back:
  1. Double click on the clip in the muted "dummy" track
  2. "Ctrl + C"
  3. Double click on the silenced clip (the "placeholder")
  4. "Ctrl + V"
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

Post Reply