One-shot trimmer (Script attached)

Heyo,

Here is the script I talked about in some of the other threads. I really needed a tool to sample-accurately trim sounds from sound design sessions, so it’s a very “specialized” tool that fits my workflow and I have no idea if it’s of any use for anyone else. But, after seeing people wanting some examples, and in an effort to try and bring something to the table I’m posting it here just in case. I think the fact that WaveLab has scripting capabilities is genius, and something that could and should be expanded upon a lot! Thanks to my brother Michael for turning my n00b code into way smoother running code :laughing:

It detects L/R independently, and the first channel that exceeds the threshold (set to -45dBFS, you can change this) will trigger the cut. It also normalizes the file at the very end, but it’s easy to comment out if you prefer that. The screenshot says it took 115ms to process; a lot of that is the Normalizer. Without the normalizer execution time is around 40ms, but increases the more empty space you have before/after the sample.

Oh and you might need to save the Normalizer factory preset “Maximum” as a user preset in order for the script to find it…


// PEGBOARD NERDS one-shot trimmer & normalizer tool
// OddProd - May 2019
// Written by Alex & Michael Odden

var t0 = Date.now(); // Start timer

// Threshold
var dB = -45; // Threshold in dBFS, edit to your preference
var threshold = (Math.pow(10, dB / 20)); // Convert dB to float

logWindow.clear(); // Clear log window
application.setResponsiveUi(0); // Turn off user interaction for speed
print = logWindow.printInfo; // Easier printing to log window

var length_s = (Math.round((activeWave.size() / activeWave.sampleRate()) * 100) / 100); // File length in seconds
var length_ms = (Math.round(length_s * 1000 * 100) / 100); // File length in milliseconds, two decimal places

// Print basic file info to log window:

print("This file has " + activeWave.size() + " samples"); // Total number of samples
print("The samplerate is " + activeWave.sampleRate() + " Hz"); // Samplerate

// If file is shorter than 1 second, display length in milliseconds:

if (length_s > 1) {
    print("The length of the file is " + length_s + " seconds");
} else {
    print("The length of the file is " + length_ms + " ms");
}

print("");
print("Threshold is set to " + dB + "dB, value " + threshold + ".");
print("");

activeWave.select(0, -1); // Deselect everything
activeWave.setCursorChannel(allCh);

var Lbuffer = (activeWave.readSamples(0, 0, activeWave.size())); //  Left channel sample buffer
var Rbuffer = (activeWave.readSamples(1, 0, activeWave.size())); // Right channel sample buffer

var startI = 0; // Will, after first loop is finished, contain the first sample index over treshold
var endI = activeWave.size() - 1; // Will, after second loop is finished, contain the last sample index over treshold

// Detect number of samples to remove from beginning
for (var i = 0; i < activeWave.size(); i++) {
    var Lbufferabs = (Math.abs(Lbuffer[i])); // Left channel absolute value
    var Rbufferabs = (Math.abs(Rbuffer[i])); // Right channel absolute value

    if ((Lbufferabs > threshold) || (Rbufferabs > threshold)) {
        startI = i;
        break;
    }
}

// Detect number of samples to remove from end
for (var i = activeWave.size() - 1; i > 0; i--) {
    var Lbufferabs = (Math.abs(Lbuffer[i])); // Left channel absolute value
    var Rbufferabs = (Math.abs(Rbuffer[i])); // Right channel absolute value
    if ((Lbufferabs > threshold) || (Rbufferabs > threshold)) {
        endI = i;
        break;
    }
}

activeWave.select(startI, endI - startI);
activeWave.trim();

// Normalize to 0dBFS

activeWave.normalize("Maximum.dat");
print("Normalizing...Done!");

// Print "Stats"

var t1 = Date.now(); // End timer..
print("Execution time: " + (t1 - t0) + " milliseconds.");