Measuring Loudness in Sub-Mixes: A Guide to Using the ‘LoudnessInfo()’ Class

Here’s how to use it: first, import the necessary libraries and load up your audio file using Python’s built-in library for handling sound files called “librosa”. Then, create a function that takes in an input file path and returns the loudness values for each sub-mix.

Here’s what the code might look like:

# Import necessary libraries
import librosa # Library for handling audio files
from scipy import signal # Library for signal processing
import numpy as np # Library for numerical operations
import matplotlib.pyplot as plt # Library for plotting graphs

# Define function to get loudness values for each sub-mix
def get_loudness(input_file):
    # Load audio file using Librosa library
    y, sr = librosa.load(input_file)
    
    # Define window size and overlap for spectrogram calculation
    win_length = 2048 # Window size for spectrogram calculation
    hop_length = int(sr / 16) # Overlap between windows for spectrogram calculation
    
    # Calculate spectrogram using Librosa library's "spectogram()" function
    S = librosa.feature.melspectrogram(y=y, sr=sr, n_fft=win_length, hop_length=hop_length)
    
    # Calculate RMS (root mean square) values for each sub-mix using scipy library's "signal.rms()" function
    rms = []
    for i in range(S.shape[0]):
        # Get windowed data for each sub-mix
        windowed_data = y[(i*hop_length):((i+1)*hop_length)]
        # Multiply windowed spectrogram by hanning window to reduce spectral leakage
        windowed_spectrogram = S[i,:] * np.hanning(win_length)
        # Calculate RMS value using scipy library's "signal.rms()" function and squaring it to get the power (loudness)
        rms_value = signal.rms(windowed_data) ** 2 
        # Calculate mean of windowed spectrogram values for each sub-mix using numpy library's "np.mean()" function, then take square root to convert back to RMS value (loudness)
        rms.append(np.sqrt(np.mean(windowed_spectrogram))) 
    
    return rms # Return list of loudness values for each sub-mix

So basically what this code does is load up your audio file, calculate a spectrogram (which shows the frequency content of each sub-mix), and then calculates the RMS values for each windowed segment using scipy library’s “signal.rms()” function. The resulting list contains the loudness values for each sub-mix in order from start to finish, which you can use to make sure everything sounds balanced and not too overpowering.

Hope that helps! Let me know if you have any questions or need further clarification.

SICORPS