listeningpy.audiotools

This module contains functions for describing audio signals.

 1"""
 2This module contains functions for describing audio signals.
 3"""
 4import pyloudnorm as pyln
 5import numpy as np
 6import soundfile as sf
 7import os
 8
 9def audio_stats(audio: np.ndarray, fs: int):
10    """
11    Calculate various audio statistics.
12
13    Parameters
14    ----------
15    audio : np.ndarray
16        The audio signal as a numpy array.
17    fs : int
18        The sample rate of the audio signal.
19
20    Returns
21    -------
22    tuple
23        A tuple containing the peak amplitude, RMS amplitude, and integrated loudness of the audio signal.
24    """
25    peak = 20*np.log10(abs(audio).max())
26    rms = 20*np.log10(np.sqrt(np.mean(audio**2)))
27    meter = pyln.Meter(fs)
28    loudness = meter.integrated_loudness(audio)
29    return peak, rms, loudness
30
31def write_audio(
32        audio: np.ndarray, 
33        fs: int, 
34        destination: str, 
35        filename: str
36        ) -> None:
37    """
38    Write audio data to a file.
39
40    Parameters
41    ----------
42    audio : np.ndarray
43        The audio data to be written.
44    fs : int
45        The sample rate of the audio data.
46    destination : str
47        The destination directory where the file will be saved.
48    filename : str
49        The name of the file.
50
51    Returns
52    -------
53    None
54    """
55    if not os.path.exists(destination):
56        os.makedirs(destination)
57    sf.write(os.path.join(destination, filename), audio, fs)
def audio_stats(audio: numpy.ndarray, fs: int):
10def audio_stats(audio: np.ndarray, fs: int):
11    """
12    Calculate various audio statistics.
13
14    Parameters
15    ----------
16    audio : np.ndarray
17        The audio signal as a numpy array.
18    fs : int
19        The sample rate of the audio signal.
20
21    Returns
22    -------
23    tuple
24        A tuple containing the peak amplitude, RMS amplitude, and integrated loudness of the audio signal.
25    """
26    peak = 20*np.log10(abs(audio).max())
27    rms = 20*np.log10(np.sqrt(np.mean(audio**2)))
28    meter = pyln.Meter(fs)
29    loudness = meter.integrated_loudness(audio)
30    return peak, rms, loudness

Calculate various audio statistics.

Parameters
  • audio (np.ndarray): The audio signal as a numpy array.
  • fs (int): The sample rate of the audio signal.
Returns
  • tuple: A tuple containing the peak amplitude, RMS amplitude, and integrated loudness of the audio signal.
def write_audio(audio: numpy.ndarray, fs: int, destination: str, filename: str) -> None:
32def write_audio(
33        audio: np.ndarray, 
34        fs: int, 
35        destination: str, 
36        filename: str
37        ) -> None:
38    """
39    Write audio data to a file.
40
41    Parameters
42    ----------
43    audio : np.ndarray
44        The audio data to be written.
45    fs : int
46        The sample rate of the audio data.
47    destination : str
48        The destination directory where the file will be saved.
49    filename : str
50        The name of the file.
51
52    Returns
53    -------
54    None
55    """
56    if not os.path.exists(destination):
57        os.makedirs(destination)
58    sf.write(os.path.join(destination, filename), audio, fs)

Write audio data to a file.

Parameters
  • audio (np.ndarray): The audio data to be written.
  • fs (int): The sample rate of the audio data.
  • destination (str): The destination directory where the file will be saved.
  • filename (str): The name of the file.
Returns
  • None