Normalizing asymmetric audio

Have an urge to learn, or a calling to teach? Want to share some useful Youtube videos? Do it here!
User avatar
jappe
Moderator
Posts: 2447
Joined: 19 Jan 2015

Post 14 Jun 2024

I was fixing some vocal takes and after normalizing a clip I saw it was very unbalanced: The positive period reached 0dB but the negative period had maximum peak at -4 dB.
And it was HP filtered below 150Hz so no static component left making it unbalanced.

A quick fix to deal with that would be to compress it, but then the positive peaks would cause distortion I'd like to avoid if I can.
What I'd want to do is to just amplify the negative period so that it too peaks at 0dB.

But I found no way to achieve that in my old Adobe Audition 3.

So I figured, why don't I just ask chatGPT to make a Python script that does it, and it did it well.

In case anyone has the same need, here it is:
1. Install Python if you don't have it already: https://www.python.org/downloads/
2. Install the required lib:

Code: Select all

pip install numpy librosa soundfile
And put this in a file "normalize_wav.py":

Code: Select all

import numpy as np
import librosa
import soundfile as sf
import tkinter as tk
from tkinter import filedialog, messagebox

def normalize_separate(audio):
    positive_part = np.maximum(0, audio)
    negative_part = np.minimum(0, audio)

    max_positive = np.max(positive_part)
    max_negative = np.min(negative_part)

    if max_positive != 0:
        positive_part /= max_positive
    if max_negative != 0:
        negative_part /= np.abs(max_negative)

    return positive_part + negative_part

def process_file(filepath):
    try:
        audio, sr = librosa.load(filepath, sr=None, mono=True)
        normalized_audio = normalize_separate(audio)
        output_path = filedialog.asksaveasfilename(defaultextension=".wav", filetypes=[("WAV files", "*.wav")])
        if output_path:
            sf.write(output_path, normalized_audio, sr)
            messagebox.showinfo("Success", f"Normalized audio saved to {output_path}")
    except Exception as e:
        messagebox.showerror("Error", str(e))

def select_file():
    filepath = filedialog.askopenfilename(filetypes=[("WAV files", "*.wav")])
    if filepath:
        process_file(filepath)

# Set up the GUI
root = tk.Tk()
root.title("Normalize Positive and Negative Periods")

canvas = tk.Canvas(root, height=200, width=300)
canvas.pack()

frame = tk.Frame(root, bg="#f0f0f0")
frame.place(relwidth=1, relheight=1)

label = tk.Label(frame, text="Select a mono WAV file to normalize:", bg="#f0f0f0")
label.pack(pady=20)

button = tk.Button(frame, text="Select File", command=select_file)
button.pack()

root.mainloop()
And run it:

Code: Select all

python normalize_wav.py
The chatGPT prompt was:

Code: Select all

Can you give me python code for a windows application that lets me open a dialogue to select a mono .WAV file which the program would analyze and normalize positive and negative period independently

  • Information
  • Who is online

    Users browsing this forum: CommonCrawl [Bot] and 1 guest