AudioHelper Module

Provides utilities for audio processing and manipulation.

class HMB.AudioHelper.AudiosHelper[source]

Bases: object

AudiosHelper: Convenience methods for audio I/O and feature extraction.

This helper provides wrappers around librosa and spafe feature extractors as well as parselmouth-based voice feature extraction. Methods try to be thin wrappers and keep the original behavior; spafe-based methods accept a sample-rate (sr / fs) and forward it to the underlying library.

Notes

  • Librosa is used for audio I/O and many feature extractions.

  • Spafe is used for additional feature extractions not covered by librosa.

  • You can install librosa and spafe via pip if not already installed: pip install librosa spafe praat-parselmouth

GetDuration(filePath)[source]

Get the duration (in seconds) of an audio file.

Parameters:

filePath (str) – Path to the audio file.

Returns:

Duration in seconds as reported by librosa.

Return type:

float

References

GetSegmentDuration(y, sr, roundTo=3)[source]

Compute the duration of an audio signal array.

Parameters:
  • y (numpy.ndarray) – 1-D audio time series.

  • sr (int) – Sampling rate of y (Hz).

  • roundTo (int) – Number of decimals to round the result to (default 3).

Returns:

Rounded duration in seconds.

Return type:

float

References

Load(filePath, conversionType=True, offset=0, segmentDuration=1, isReversed=False)[source]

Load an audio file segment using librosa.

Parameters:
  • filePath (str) – Path to the audio file.

  • conversionType (bool) – If True, force mono. Matches librosa’s mono parameter.

  • offset (float) – Start reading after this time (in seconds).

  • segmentDuration (float) – Duration to load (in seconds). If 0 or None, librosa loads full file.

  • isReversed (bool) – If True, reverse the returned signal (y[::-1]).

Returns:

(y, sr) where y is a 1-D numpy array and sr is the sampling rate (int).
  • numpy.ndarray: Audio time series.

  • int: Sampling rate of y.

Return type:

tuple

References

GetSTFT(y)[source]

Compute the short-time Fourier transform (STFT) of a signal.

Parameters:

y (numpy.ndarray) – 1-D audio time series.

Returns:

Complex-valued STFT matrix as returned by librosa.stft.

Return type:

numpy.ndarray

References

GetAbsoluteSTFT(y)[source]

Compute the magnitude (absolute value) spectrogram from the STFT.

Parameters:

y (numpy.ndarray) – 1-D audio time series.

Returns:

Magnitude spectrogram (non-negative floats).

Return type:

numpy.ndarray

References

  • STFT magnitude and spectrogram conversions (librosa).

GetHarmonicEffect(y)[source]

Extract the harmonic component of a signal using librosa.effects.harmonic.

Parameters:

y (numpy.ndarray) – 1-D audio time series.

Returns:

Harmonic component of the signal.

Return type:

numpy.ndarray

References

GetPercussiveEffect(y)[source]

Extract the percussive component of a signal using librosa.effects.percussive.

Parameters:

y (numpy.ndarray) – 1-D audio time series.

Returns:

Percussive component of the signal.

Return type:

numpy.ndarray

References

GetSlaneyMFCC(y, sr=22050, nMFCC=None)[source]

Compute Slaney-style MFCCs using librosa.

Parameters:
  • y (numpy.ndarray) – 1-D audio time series.

  • sr (int) – Sampling rate of y (Hz). Default is 22050.

  • nMFCC (int, optional) – Number of MFCC coefficients to return. If None uses librosa default.

Returns:

MFCC matrix (n_mfcc x frames).

Return type:

numpy.ndarray

References

GetMeanSlaneyMFCC(y, sr, nMFCC=None)[source]

Compute the mean (per-coefficient) Slaney MFCC over time.

Parameters:
  • y (numpy.ndarray) – 1-D audio time series.

  • sr (int) – Sampling rate of y (Hz).

  • nMFCC (int, optional) – Number of MFCC coefficients to average.

Returns:

1-D array of length n_mfcc containing the mean over frames.

Return type:

numpy.ndarray

References

GetHtkMFCC(y, sr, nMFCC=None)[source]

Compute HTK-style MFCCs using librosa with dct_type=3.

Parameters:
  • y (numpy.ndarray) – 1-D audio time series.

  • sr (int) – Sampling rate of y (Hz).

  • nMFCC (int, optional) – Number of MFCC coefficients to return.

Returns:

MFCC matrix (n_mfcc x frames).

Return type:

numpy.ndarray

References

GetMeanHtkMFCC(y, sr, nMFCC=None)[source]

Return mean HTK MFCC coefficients across time.

Parameters:
  • y (numpy.ndarray) – 1-D audio time series.

  • sr (int) – Sampling rate of y (Hz).

  • nMFCC (int, optional) – Number of MFCC coefficients to average.

Returns:

1-D array with mean HTK MFCC coefficients.

Return type:

numpy.ndarray

References

  • HTK MFCC conventions and Librosa documentation.

GetMeanChroma(y, sr)[source]

Compute mean chroma_stft features across time.

Parameters:
  • y (numpy.ndarray) – 1-D audio time series.

  • sr (int) – Sampling rate of y (Hz).

Returns:

1-D array with 12 chroma mean values.

Return type:

numpy.ndarray

References

GetMeanChromaSTFT(y, sr)[source]

Compute mean chroma using the magnitude STFT as input.

Parameters:
Returns:

Mean chroma vector.

Return type:

numpy.ndarray

References

GetMeanChromaCqt(y, sr)[source]

Compute mean chroma from the constant-Q transform.

Parameters:
Returns:

Mean chroma vector from CQT.

Return type:

numpy.ndarray

References

  • Chroma CQT implementation: Librosa chroma_cqt docs.

  • Muller, M. & Ewert, S. (2011) Chroma Toolbox reference.

GetMeanChromaCens(y, sr)[source]

Compute chroma CENS mean across frames.

Parameters:
Returns:

Mean chroma CENS vector.

Return type:

numpy.ndarray

References

  • Chroma CENS method: Chroma Toolbox and related publications.

GetMeanMelSpectrogram(y, sr)[source]

Compute mean Mel spectrogram across time.

Parameters:
Returns:

Mean Mel spectrogram vector across frames.

Return type:

numpy.ndarray

GetMeanSpectralContrast(y, sr)[source]

Compute mean spectral contrast across time.

Parameters:
Returns:

Mean spectral contrast vector across frames.

Return type:

numpy.ndarray

References

GetMeanHarmonicTonnetz(y, sr)[source]

Compute mean Tonnetz on the harmonic component.

Parameters:
Returns:

Mean Tonnetz vector computed on harmonic component.

Return type:

numpy.ndarray

References

GetMeanTonnetz(y, sr)[source]

Compute mean Tonnetz features across time.

Parameters:
Returns:

Mean Tonnetz vector across frames.

Return type:

numpy.ndarray

References

  • Tonnetz: Harte et al. (2006) and Librosa documentation.

GetMeanRMS(y, sr)[source]

Compute mean root-mean-square energy across frames.

Parameters:
Returns:

Mean RMS energy per frame.

Return type:

numpy.ndarray

GetMeanSpectralCentroid(y, sr)[source]

Compute mean spectral centroid across frames.

Parameters:
Returns:

Mean spectral centroid per frame.

Return type:

numpy.ndarray

References

GetMeanSpectralBandwidth(y, sr)[source]

Compute mean spectral bandwidth across frames.

Parameters:
Returns:

Mean spectral bandwidth per frame.

Return type:

numpy.ndarray

References

GetMeanSpectralRolloff(y, sr)[source]

Compute mean spectral rolloff across frames.

Parameters:
Returns:

Mean spectral rolloff per frame.

Return type:

numpy.ndarray

References

GetMeanSpectralFlatness(y, sr)[source]

Compute mean spectral flatness across frames.

Parameters:
Returns:

Mean spectral flatness per frame.

Return type:

numpy.ndarray

References

  • Spectral flatness literature and Librosa docs.

  • Dubnov et al. (2004) on spectral flatness.

GetMeanZCR(y, sr)[source]

Compute mean zero-crossing rate across frames.

Parameters:
Returns:

Mean zero-crossing rate per frame.

Return type:

numpy.ndarray

References

GenerateScaledMelSpectrogram(y, sr, hopLength=512, nFFT=2048, numMels=128)[source]

Create a log-scaled Mel spectrogram (dB) suitable for visualization or model input.

Parameters:
  • y (numpy.ndarray) – Audio time series.

  • sr (int) – Sampling rate.

  • hopLength (int) – Hop length for STFT.

  • nFFT (int) – FFT size.

  • numMels (int) – Number of Mel bands to generate.

Returns:

Mel spectrogram in decibels (shape: n_mels x frames).

Return type:

numpy.ndarray

References

GenerateSTFT(y, sr, hopLength=512, nFFT=2048)[source]

Compute a log-amplitude STFT spectrogram.

Parameters:
  • y (numpy.ndarray) – Audio time series.

  • sr (int) – Sampling rate.

  • hopLength (int) – Hop length for STFT.

  • nFFT (int) – FFT size.

Returns:

Log-amplitude STFT spectrogram.

Return type:

numpy.ndarray

References

GetBFCC(y, sr=16000)[source]

Compute BFCC (bark-frequency cepstral coefficients) using spafe.

Parameters:
  • y (numpy.ndarray) – Signal.

  • sr (int) – Sample rate (fs) forwarded to spafe.bfcc.

Returns:

BFCC feature matrix.

Return type:

numpy.ndarray

GetGFCC(y, sr=16000)[source]

Compute GFCC (gammatone-frequency cepstral coefficients) using spafe.

Parameters:
Returns:

GFCC feature matrix.

Return type:

numpy.ndarray

GetLFCC(y, sr=16000)[source]

Compute LFCC (linear-frequency cepstral coefficients) using spafe.

Parameters:
Returns:

LFCC feature matrix.

Return type:

numpy.ndarray

GetLPC(y, sr=16000)[source]

Compute LPC coefficients using spafe.

Parameters:
Returns:

LPC coefficient matrix.

Return type:

numpy.ndarray

References

  • Linear predictive coding (LPC) literature and implementations.

GetLPCC(y, sr=16000)[source]

Compute LPCC coefficients using spafe.

Parameters:
Returns:

LPCC coefficient matrix.

Return type:

numpy.ndarray

References

  • LPCC and LP-based cepstral literature.

GetMFCC(y, sr=16000)[source]

Compute MFCC using spafe (not librosa’s MFCC).

Parameters:
Returns:

MFCC feature matrix from spafe.

Return type:

numpy.ndarray

References

  • MFCC literature and implementations. See Milner & Shao (2006) and Librosa docs.

GetIMFCC(y, sr=16000)[source]

Compute IMFCC (inverse MFCC) using spafe.

Parameters:
Returns:

IMFCC feature matrix.

Return type:

numpy.ndarray

References

  • IMFCC reference literature and spafe implementation.

GetMSRCC(y, sr=16000)[source]

Compute MSRCC features using spafe.

Parameters:
Returns:

MSRCC feature matrix.

Return type:

numpy.ndarray

GetNGCC(y, sr=16000)[source]

Compute NGCC features using spafe.

Parameters:
Returns:

NGCC feature matrix.

Return type:

numpy.ndarray

References

  • NGCC references and related publications.

GetPNCC(y, sr=16000)[source]

Compute PNCC features using spafe.

Parameters:
Returns:

PNCC feature matrix.

Return type:

numpy.ndarray

References

GetPSRCC(y, sr=16000)[source]

Compute PSRCC features using spafe.

Parameters:
Returns:

PSRCC feature matrix.

Return type:

numpy.ndarray

GetPLP(y, sr)[source]

Compute PLP features using spafe.

Parameters:
Returns:

PLP feature matrix.

Return type:

numpy.ndarray

References

  • PLP literature and spafe usage notes.

GetRPLP(y, sr)[source]

Compute RPLP features using spafe.

Parameters:
Returns:

RPLP feature matrix.

Return type:

numpy.ndarray

References

  • RPLP references and spafe implementation.

GetMeanBFCC(y, sr=16000)[source]

Return mean BFCC coefficients across time (per-coefficient mean).

Parameters:
Returns:

Mean BFCC vector.

Return type:

numpy.ndarray

GetMeanGFCC(y, sr=16000)[source]

Return mean GFCC coefficients across time.

Parameters:
Returns:

Mean GFCC vector.

Return type:

numpy.ndarray

GetMeanLFCC(y, sr=16000)[source]

Return mean LFCC coefficients across time.

Parameters:
Returns:

Mean LFCC vector.

Return type:

numpy.ndarray

GetMeanLPC(y, sr=16000)[source]

Return mean LPC coefficients across time.

Parameters:
Returns:

Mean LPC vector.

Return type:

numpy.ndarray

GetMeanLPCC(y, sr=16000)[source]

Return mean LPCC coefficients across time.

Parameters:
Returns:

Mean LPCC vector.

Return type:

numpy.ndarray

References

  • LPCC literature on cepstral analysis.

GetMeanMFCC(y, sr=16000)[source]

Return mean MFCC coefficients (from spafe-mfcc) across time.

Parameters:
Returns:

Mean MFCC vector.

Return type:

numpy.ndarray

References

GetMeanIMFCC(y, sr=16000)[source]

Return mean IMFCC coefficients across time.

Parameters:
Returns:

Mean IMFCC vector.

Return type:

numpy.ndarray

References

  • IMFCC literature and spafe docs.

GetMeanMSRCC(y, sr=16000)[source]

Return mean MSRCC coefficients across time.

Parameters:
Returns:

Mean MSRCC vector.

Return type:

numpy.ndarray

GetMeanNGCC(y, sr=16000)[source]

Return mean NGCC coefficients across time.

Parameters:
Returns:

Mean NGCC vector.

Return type:

numpy.ndarray

References

  • NGCC references in the literature.

GetMeanPNCC(y, sr=16000)[source]

Return mean PNCC coefficients across time.

Parameters:
Returns:

Mean PNCC vector.

Return type:

numpy.ndarray

GetMeanPSRCC(y, sr=16000)[source]

Return mean PSRCC coefficients across time.

Parameters:
Returns:

Mean PSRCC vector.

Return type:

numpy.ndarray

GetMeanPLP(y, sr)[source]

Return mean PLP coefficients across time.

Parameters:
Returns:

Mean PLP vector.

Return type:

numpy.ndarray

References

  • PLP literature.

GetMeanRPLP(y, sr)[source]

Return mean RPLP coefficients across time.

Parameters:
Returns:

Mean RPLP vector.

Return type:

numpy.ndarray

References

  • RPLP and rasta-PLP references.

GetAllMeanAudioFeatures(y, sr=16000)[source]

Compute all mean audio features available in this class.

Parameters:
Returns:

Dictionary with mean feature vectors for all available features.

Return type:

dict

ExtractAudioFeaturesViaParselmouth(voiceSample, f0Min, f0Max, unit)[source]

Extract voice features (pitch, jitter, shimmer, HNR, MFCC means) using parselmouth (Praat bindings).

Parameters:
  • voiceSample (str or numpy.ndarray) – Path to audio file or a compatible array accepted by parselmouth.Sound.

  • f0Min (float) – Minimum pitch (Hz) for pitch extraction.

  • f0Max (float) – Maximum pitch (Hz) for pitch extraction.

  • unit (str) – Unit used by Praat for mean/std extraction (e.g., ‘Hertz’).

Returns:

Dictionary with keys for f0 mean/std, hnr, jitter/shimmer measures, duration and mfccMean coefficients.

Return type:

dict

References

GetMelImage(y, sr=16000, numMels=128, hopLength=512, nFFT=2048, outFrames=128, axisLast=True)[source]

Generate a Mel spectrogram image (2D numpy array) from audio time series.

Parameters:
  • y (numpy.ndarray) – Audio time series.

  • sr (int) – Sampling rate.

  • numMels (int) – Number of Mel bands.

  • hopLength (int) – Hop length for STFT.

  • nFFT (int) – FFT size.

  • outFrames (int) – Desired number of output frames (time dimension).

Returns:

Mel spectrogram image with shape (numMels, outFrames, 3) if axisLast is True, else (3, numMels, outFrames).

Return type:

numpy.ndarray