Peak Amplitude (Analyze plug-in)

This has come up a few times on the Audacity4Blind mailing list, so I thought I’d write a plug-in for it.

The issue is to show the peak amplitude of a track.
There is a “trick” for getting the peak amplitude using the Amplify effect, but it would probably be easier to just have a tool in the Analyze menu that will give a direct reading of the peak amplitude, so here is such a tool.
show-amplitude.ny (2.4 KB)
To install the plug-in, place the show-amplitude.ny file in the Plug-Ins folder inside the Audacity installation folder.

  • On Windows computers, this is usually at C:Program FilesAudacity. On 64-bit Windows this directory will be “Program Files (x86)” then as appropriate.
  • On Mac OS X, it is usually under /Applications.
  • On Linux this is usually /usr/share/audacity/ if you installed a repository package of Audacity or /usr/local/share/audacity if you compiled Audacity. Note: You must log in as root to access /usr. Repository packages of Audacity may overwrite this folder. An easier alternative may be to create ~/.audacity-files/plug-ins/ in your home folder (the “.” in the path means it is a hidden folder).

Restart Audacity, then the plug-ins will appear as "Amplitude…" underneath the divider in the “Analyze” menus.

To use the plug-in, Select the audio that you want to analyze, then from the Analyze menu select “Amplitude…”
There are two available controls:
Show amplitude as: [choice: Peak dB, Peak Linear, or dB and Linear] The default is to show the peak amplitude in both dB and as a linear value.
Decimal Places: [0 to 5] The default is 2 decimal places.
Note that setting decimal places to zero is not likely to be useful when displaying the amplitude on a linear scale (0 to 1).

If the peak level is equal or greater than 0 dB, a warning will be shown to alert the user that the audio may be clipped.

Advanced Options:
This plug-in can also be customised. Open the show-amplitude.ny file in a text editor for details.

On Windows it is not possible to copy the display text. If you need to copy the display text, use the “Debug” button instead of the “OK” button to run the plug-in. When the display message shows, activate the “OK” button and a “debug window” will then appear with the same text as the display message. The text can be copied from the debug window in the usual way (Ctrl+A to select the text them Ctrl+C to copy).

Here is an alternative version of the plug-in.
This version has no controls. As soon as it is opened from the Analyze menu it will analyze and display the peak amplitude of the selected audio.
show-amplitude.ny (2.37 KB)
It is essentially the same plug-in as in the previous post, but with the controls commented out.

That’s better.

Hi, just wanted to check if this plug-in still works with the latest version of audacity? I followed the instructions, but it doesn’t seem to show up anywhere.


Thanks!

DGB

It needs to be installed and enabled according to the instructions in the manual (Mac instructions for installing Nyquist plug-ins: https://manual.audacityteam.org/man/installing_effect_generator_and_analyzer_plug_ins_on_mac_os_x.html#nyquist_install)

When installed and enabled, it will appear as “Amplitude…” in the “Analyze” menu.

thanks Steve! I finally got it to work. Now I’m just trying to put it in a chain to batch process a bunch of files.

I’m trying to:

open a file from a folder
run Amplitude
pull the amplitude number
save the file with the amplitude number added to the filename

I just can’t seem to figure out how to script it, or set it up though. Would love if someone has any insight into where I’d look to figure it out.

Thanks!

DGB

I don’t think that can be done in a normal Macro.
Macros are not intended to be a full programming language, they are just a list of single commands that run in sequence.

You could do it with a “Nyquist-Macro” (Nyquist-Macros - Audacity Manual); something like this:

;nyquist plug-in
;version 4
;type analyze
;name "Export-by-amp"

(setf command "Export2: Filename=")
(setf dir "<full export path>")
(setf ext ".wav")

(setf command (format nil "~a~a~a~a"
                      command
                      dir
                      (get '*selection* 'peak-level)
                      ext))
(aud-do command)

Awesome. Thanks so much for the quick answer!

I looked over the documentation to try and figure out what, if anything I needed to change in that nyquist macro you wrote to get it to work on my end, but sadly, I’m a bit lost and was only able to save it as a plugin and enable it, but other than that I couldn’t get it to do anything.

Sorry if it’s a total noob question, but do I need to change variables in that script? And if so, which ones? I’m assuming something in there looks into a folder for a file, opens it, checks the peak value and then saves it with that value added to the name in a different folder? I just couldn’t figure out which bit of code was supposed to do what.

I tried a bit to sort it out by looking up each part of each line, but I couldn’t really sort it out.

Thanks again

DGB

This (below) is a bare bones plug-in that will find the selected track’s amplitude, and export the selected audio, using the measured amplitude as the file name, (with “.wav” appended to the name).

Note that this plug-in has some serious shortcomings:

  1. The plug-in expects there to be exactly one track in the project.
  • If there is more than one track in the project, the exported file will be a mix of the selected audio in all tracks.
    • If more than one track is selected, the plug-in will attempt to export once for each selected track.
      The file name will be based on the amplitude of the “current” track, as the plug-in iterates over each track.
      Thus, if you have 3 tracks with amplitudes of 0.5, 0.6 and 0.7, you will get 3 exported tracks called “0.5000.wav”, “0.6000.wav” and “0.7000.wav”, and each file will be an identical mix of all of the tracks.
  1. The file names are not guaranteed to be unique.
    When used in a Macro for batch processing, if there are two files with identical peak amplitudes, the first exported file will be overwritten by the other.
  2. There is no check that the location is writeable.
    If you set the export path to a location that does not exist, the plug-in will attempt to write the file, but will fail. No error is generated, it just fails.
  3. If you run the plug-in twice, the first files will be overwritten without warning.
  4. The file names include a dot, which is not recommended except for the dot before the file name extension.


;nyquist plug-in
;version 4
;type analyze
;name "Export-by-amp"

(setf command "Export2: Filename=")
(setf dir "<full export path>")
(setf ext ".wav")

;; Construct the Macro command.
(setf command (format nil "~a\"~a~a~a\""
                      command
                      dir
                      (get '*selection* 'peak-level)
                      ext))

(aud-do command)
(format nil "Attempted to save file to:~%~s" command))

Walking through the code:

This is the plug-in “header”. It tells Audacity that this file is a version 4, “Analyze” Nyquist plug-in called “Export-by-amp” (see also: https://wiki.audacityteam.org/wiki/Nyquist_Plug-ins_Reference#header)

;nyquist plug-in
;version 4
;type analyze
;name "Export-by-amp"

These three lines set some variables:

  • The first line creates a variable called “command”, which is set to a string (text) value of “Export2: Filename=”. This is the basis of the “Export2” command that is shown in this section of the manual: https://manual.audacityteam.org/man/scripting_reference.html#extra_scriptables_ii_submenu
  • The second line creates a variable called “dir”. Here you must replace “” with the actual export path that you wish to use, for example: “C:/Users/DGByrned /Desktop/”.
  • The third line creates a string variable called “ext” to the value “.wav”. This will be the file extension, and it tells the Export2 command to export as a 16-bit WAV file.


(setf command "Export2: Filename=")
(setf dir "<full export path>")
(setf ext ".wav")

Now we can get the peak amplitude of the selected audio, and construct the full command.
The “format” command replaces each occurrence of “~a” with the value of a variable. For example, the first “~a” is replaced with “Export2: Filename=”.
If “dir” was set to “C:/Users/DGByrned /Desktop/”, and the peak amplitude was 0.6234, then “command” will be set to:
Export2: Filename="C:/Users/DGByrned /Desktop/0.6234.wav"
The backslash before the quote characters tells the code to treat it as a literal quote, not the end of the string.
The forward slashes in the path should be valid on all platforms - the Windows style backslash in the path won’t work, because backslash is an “escape character”.

;; Construct the Macro command.
(setf command (format nil "~a\"~a~a~a\""
                      command
                      dir
                      (get '*selection* 'peak-level)
                      ext))

Finally, we send the command to Audacity, using the “aud-do” function (see also: https://manual.audacityteam.org/man/nyquist_macros.html#AUD-DO)

(aud-do command)

and print a message so that you can see what the plug-in tried to do:

(format nil "Attempted to save file to:~%~s" command))

AMAZING!!! Thank you so much for taking the time to post that incredibly clear and thorough answer! I seriously appreciate the kindness and hard work. I’m going to try this right now!

Let us know how you get on. We could perhaps look at improving some of the plug-in’s shortcomings.

Out of interest, why do you want this?

I want it because I have a ton, a really huge amount, of percussion hits I recorded and I’m trying to organize them by ascending volume for a sampling project. Doing it manually has been a serious chore, and I’m trying to find a method for automating the naming of the files w/ the amplitude so i can sort them really easily. The code you provide is a huge part of the puzzle!

Thanks again for everything!

DGB

Hi Steve,
this is very useful.
Is it possibile to have the measurement in dBFS?

Change the line:

(get '*selection* 'peak-level)

to:

(linear-to-db (get '*selection* 'peak-level))

I mean in show-amplitude.ny.
I tried the code lines in Nyquist Prompt and it returns the same values.
The other strange thing in Audacity is that 0dB is -16dBFS.

Everything above that 0dB (-16dBFS) is 0 for Audacity.
So the plugin does its work until the level exceeds that threshold.
I always prefer to work in dBFS for obvious reasons.

show-amplitude.ny already shows dBFS.

Screenshot_2022-11-09_16-04-17.png

I’ve no idea what you mean :confused:

I had the meter set on VU, reading different levels.
Anyway, why can’t we see levels above 0 like in any other meter?

Ah, can I have a text output when I use Amplitude or ACX Check as macro?
Otherwise I have to write down the levels by hand…

Because we’re working with digital (PCM) audio. Integer format PCM encoded audio (such as normal WAV files) can’t go over 0 dB.

Audacity can go over 0 dB because it uses a special kind of digital audio (32-bit float PCM), but you should avoid going over 0 dB because it will clip when you export it to a common format such as 16-bit WAV, and sound cards also clip at 0 dB.