AudioHelper Module¶
Provides utilities for audio processing and manipulation.
- class HMB.AudioHelper.AudiosHelper[source]¶
Bases:
objectAudiosHelper: 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:
References
Librosa get_duration documentation: https://librosa.org/doc/latest/generated/librosa.get_duration.html
- 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:
References
Librosa get_duration documentation: https://librosa.org/doc/latest/generated/librosa.get_duration.html
- 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:
References
Librosa load documentation: https://librosa.org/doc/latest/generated/librosa.load.html
- 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:
References
Librosa STFT: https://librosa.org/doc/latest/generated/librosa.stft.html
General STFT description: short-time Fourier transform literature.
- 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:
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:
References
Librosa harmonic/percussive separation: https://librosa.org/doc/latest/generated/librosa.effects.harmonic.html
- 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:
References
Librosa harmonic/percussive separation: https://librosa.org/doc/latest/generated/librosa.effects.percussive.html
- 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:
References
MFCC background and Slaney implementation: Milner & Shao (2006) and Librosa MFCC docs.
Librosa mfcc: https://librosa.org/doc/latest/generated/librosa.feature.mfcc.html
Milner, B., & Shao, X. (2006). Clean speech reconstruction from MFCC vectors and fundamental frequency using an integrated front-end.
- 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_mfcccontaining the mean over frames.- Return type:
References
Librosa MFCC and averaging practices: https://librosa.org/doc/latest/generated/librosa.feature.mfcc.html
- 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:
References
HTK MFCC conventions (DCT type 3) and Librosa MFCC settings.
Librosa mfcc: https://librosa.org/doc/latest/generated/librosa.feature.mfcc.html
- 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:
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:
References
Chroma feature analysis: Ellis (2007). See Librosa chroma_stft docs.
Librosa chroma_stft: https://librosa.org/doc/latest/generated/librosa.feature.chroma_stft.html
- GetMeanChromaSTFT(y, sr)[source]¶
Compute mean chroma using the magnitude STFT as input.
- Parameters:
y (numpy.ndarray) – 1-D audio time series.
sr (int) – Sampling rate.
- Returns:
Mean chroma vector.
- Return type:
References
Chroma via STFT: Librosa documentation and chroma literature.
Librosa chroma_stft: https://librosa.org/doc/latest/generated/librosa.feature.chroma_stft.html
- GetMeanChromaCqt(y, sr)[source]¶
Compute mean chroma from the constant-Q transform.
- Parameters:
y (numpy.ndarray) – 1-D audio time series.
sr (int) – Sampling rate.
- Returns:
Mean chroma vector from CQT.
- Return type:
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:
y (numpy.ndarray) – 1-D audio time series.
sr (int) – Sampling rate.
- Returns:
Mean chroma CENS vector.
- Return type:
References
Chroma CENS method: Chroma Toolbox and related publications.
- GetMeanMelSpectrogram(y, sr)[source]¶
Compute mean Mel spectrogram across time.
- Parameters:
y (numpy.ndarray) – 1-D audio time series.
sr (int) – Sampling rate.
- Returns:
Mean Mel spectrogram vector across frames.
- Return type:
References
Librosa melspectrogram: https://librosa.org/doc/latest/generated/librosa.feature.melspectrogram.html
- GetMeanSpectralContrast(y, sr)[source]¶
Compute mean spectral contrast across time.
- Parameters:
y (numpy.ndarray) – 1-D audio time series.
sr (int) – Sampling rate.
- Returns:
Mean spectral contrast vector across frames.
- Return type:
References
Spectral contrast: Jiang et al. (2002). See Librosa spectral_contrast docs.
Librosa spectral_contrast: https://librosa.org/doc/latest/generated/librosa.feature.spectral_contrast.html
- GetMeanHarmonicTonnetz(y, sr)[source]¶
Compute mean Tonnetz on the harmonic component.
- Parameters:
y (numpy.ndarray) – 1-D audio time series.
sr (int) – Sampling rate.
- Returns:
Mean Tonnetz vector computed on harmonic component.
- Return type:
References
Tonnetz features: Harte et al. (2006). See Librosa tonnetz docs.
Librosa tonnetz: https://librosa.org/doc/latest/generated/librosa.feature.tonnetz.html
- GetMeanTonnetz(y, sr)[source]¶
Compute mean Tonnetz features across time.
- Parameters:
y (numpy.ndarray) – 1-D audio time series.
sr (int) – Sampling rate.
- Returns:
Mean Tonnetz vector across frames.
- Return type:
References
Tonnetz: Harte et al. (2006) and Librosa documentation.
- GetMeanRMS(y, sr)[source]¶
Compute mean root-mean-square energy across frames.
- Parameters:
y (numpy.ndarray) – 1-D audio time series.
sr (int) – Sampling rate.
- Returns:
Mean RMS energy per frame.
- Return type:
References
RMS energy and librosa RMS: https://librosa.org/doc/latest/generated/librosa.feature.rms.html
- GetMeanSpectralCentroid(y, sr)[source]¶
Compute mean spectral centroid across frames.
- Parameters:
y (numpy.ndarray) – 1-D audio time series.
sr (int) – Sampling rate.
- Returns:
Mean spectral centroid per frame.
- Return type:
References
Spectral centroid and bandwidth: Klapuri & Davy (2007). See Librosa docs.
Librosa spectral_centroid: https://librosa.org/doc/latest/generated/librosa.feature.spectral_centroid.html
- GetMeanSpectralBandwidth(y, sr)[source]¶
Compute mean spectral bandwidth across frames.
- Parameters:
y (numpy.ndarray) – 1-D audio time series.
sr (int) – Sampling rate.
- Returns:
Mean spectral bandwidth per frame.
- Return type:
References
Spectral bandwidth discussion and Librosa docs.
Librosa spectral_bandwidth: https://librosa.org/doc/latest/generated/librosa.feature.spectral_bandwidth.html
- GetMeanSpectralRolloff(y, sr)[source]¶
Compute mean spectral rolloff across frames.
- Parameters:
y (numpy.ndarray) – 1-D audio time series.
sr (int) – Sampling rate.
- Returns:
Mean spectral rolloff per frame.
- Return type:
References
Spectral rolloff: Librosa documentation.
Librosa spectral_rolloff: https://librosa.org/doc/latest/generated/librosa.feature.spectral_rolloff.html
- GetMeanSpectralFlatness(y, sr)[source]¶
Compute mean spectral flatness across frames.
- Parameters:
y (numpy.ndarray) – 1-D audio time series.
sr (int) – Sampling rate.
- Returns:
Mean spectral flatness per frame.
- Return type:
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:
y (numpy.ndarray) – 1-D audio time series.
sr (int) – Sampling rate.
- Returns:
Mean zero-crossing rate per frame.
- Return type:
References
Zero-crossing Rate features and Librosa docs.
Librosa zero_crossing_rate: https://librosa.org/doc/latest/generated/librosa.feature.zero_crossing_rate.html
- 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:
References
Mel spectrogram and power_to_db: Librosa documentation.
Librosa melspectrogram: https://librosa.org/doc/latest/generated/librosa.feature.melspectrogram.html
Librosa power_to_db: https://librosa.org/doc/latest/generated/librosa.power_to_db.html
- 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:
References
STFT and amplitude-to-db: Librosa documentation.
Librosa amplitude_to_db: https://librosa.org/doc/latest/generated/librosa.amplitude_to_db.html
- 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:
References
- GetGFCC(y, sr=16000)[source]¶
Compute GFCC (gammatone-frequency cepstral coefficients) using spafe.
- Parameters:
y (numpy.ndarray) – Signal.
sr (int) – Sample rate.
- Returns:
GFCC feature matrix.
- Return type:
References
- GetLFCC(y, sr=16000)[source]¶
Compute LFCC (linear-frequency cepstral coefficients) using spafe.
- Parameters:
y (numpy.ndarray) – Signal.
sr (int) – Sample rate.
- Returns:
LFCC feature matrix.
- Return type:
References
- GetLPC(y, sr=16000)[source]¶
Compute LPC coefficients using spafe.
- Parameters:
y (numpy.ndarray) – Signal.
sr (int) – Sample rate.
- Returns:
LPC coefficient matrix.
- Return type:
References
Linear predictive coding (LPC) literature and implementations.
- GetLPCC(y, sr=16000)[source]¶
Compute LPCC coefficients using spafe.
- Parameters:
y (numpy.ndarray) – Signal.
sr (int) – Sample rate.
- Returns:
LPCC coefficient matrix.
- Return type:
References
LPCC and LP-based cepstral literature.
- GetMFCC(y, sr=16000)[source]¶
Compute MFCC using spafe (not librosa’s MFCC).
- Parameters:
y (numpy.ndarray) – Signal.
sr (int) – Sample rate.
- Returns:
MFCC feature matrix from spafe.
- Return type:
References
MFCC literature and implementations. See Milner & Shao (2006) and Librosa docs.
- GetIMFCC(y, sr=16000)[source]¶
Compute IMFCC (inverse MFCC) using spafe.
- Parameters:
y (numpy.ndarray) – Signal.
sr (int) – Sample rate.
- Returns:
IMFCC feature matrix.
- Return type:
References
IMFCC reference literature and spafe implementation.
- GetMSRCC(y, sr=16000)[source]¶
Compute MSRCC features using spafe.
- Parameters:
y (numpy.ndarray) – Signal.
sr (int) – Sample rate.
- Returns:
MSRCC feature matrix.
- Return type:
References
MSRCC reference: http://www.apsipa.org/proceedings/2018/pdfs/0001945.pdf
- GetNGCC(y, sr=16000)[source]¶
Compute NGCC features using spafe.
- Parameters:
y (numpy.ndarray) – Signal.
sr (int) – Sample rate.
- Returns:
NGCC feature matrix.
- Return type:
References
NGCC references and related publications.
- GetPNCC(y, sr=16000)[source]¶
Compute PNCC features using spafe.
- Parameters:
y (numpy.ndarray) – Signal.
sr (int) – Sample rate.
- Returns:
PNCC feature matrix.
- Return type:
References
PNCC implementation reference: https://github.com/supikiti/PNCC/blob/master/pncc.py
- GetPSRCC(y, sr=16000)[source]¶
Compute PSRCC features using spafe.
- Parameters:
y (numpy.ndarray) – Signal.
sr (int) – Sample rate.
- Returns:
PSRCC feature matrix.
- Return type:
References
PSRCC reference: http://www.apsipa.org/proceedings/2018/pdfs/0001945.pdf
- GetPLP(y, sr)[source]¶
Compute PLP features using spafe.
- Parameters:
y (numpy.ndarray) – Signal.
sr (int) – Sample rate.
- Returns:
PLP feature matrix.
- Return type:
References
PLP literature and spafe usage notes.
- GetRPLP(y, sr)[source]¶
Compute RPLP features using spafe.
- Parameters:
y (numpy.ndarray) – Signal.
sr (int) – Sample rate.
- Returns:
RPLP feature matrix.
- Return type:
References
RPLP references and spafe implementation.
- GetMeanBFCC(y, sr=16000)[source]¶
Return mean BFCC coefficients across time (per-coefficient mean).
- Parameters:
y (numpy.ndarray) – Signal.
sr (int) – Sample rate.
- Returns:
Mean BFCC vector.
- Return type:
References
- GetMeanGFCC(y, sr=16000)[source]¶
Return mean GFCC coefficients across time.
- Parameters:
y (numpy.ndarray) – Signal.
sr (int) – Sample rate.
- Returns:
Mean GFCC vector.
- Return type:
References
- GetMeanLFCC(y, sr=16000)[source]¶
Return mean LFCC coefficients across time.
- Parameters:
y (numpy.ndarray) – Signal.
sr (int) – Sample rate.
- Returns:
Mean LFCC vector.
- Return type:
References
- GetMeanLPC(y, sr=16000)[source]¶
Return mean LPC coefficients across time.
- Parameters:
y (numpy.ndarray) – Signal.
sr (int) – Sample rate.
- Returns:
Mean LPC vector.
- Return type:
References
LPC refernece: https://superkogito.github.io/spafe/features/lpc.html
- GetMeanLPCC(y, sr=16000)[source]¶
Return mean LPCC coefficients across time.
- Parameters:
y (numpy.ndarray) – Signal.
sr (int) – Sample rate.
- Returns:
Mean LPCC vector.
- Return type:
References
LPCC literature on cepstral analysis.
- GetMeanMFCC(y, sr=16000)[source]¶
Return mean MFCC coefficients (from spafe-mfcc) across time.
- Parameters:
y (numpy.ndarray) – Signal.
sr (int) – Sample rate.
- Returns:
Mean MFCC vector.
- Return type:
References
MFCC reference: https://spafe.readthedocs.io/en/latest/features/mfcc.html
Milner & Shao (2006) reference for MFCC reconstruction.
- GetMeanIMFCC(y, sr=16000)[source]¶
Return mean IMFCC coefficients across time.
- Parameters:
y (numpy.ndarray) – Signal.
sr (int) – Sample rate.
- Returns:
Mean IMFCC vector.
- Return type:
References
IMFCC literature and spafe docs.
- GetMeanMSRCC(y, sr=16000)[source]¶
Return mean MSRCC coefficients across time.
- Parameters:
y (numpy.ndarray) – Signal.
sr (int) – Sample rate.
- Returns:
Mean MSRCC vector.
- Return type:
References
MSRCC reference: http://www.apsipa.org/proceedings/2018/pdfs/0001945.pdf
- GetMeanNGCC(y, sr=16000)[source]¶
Return mean NGCC coefficients across time.
- Parameters:
y (numpy.ndarray) – Signal.
sr (int) – Sample rate.
- Returns:
Mean NGCC vector.
- Return type:
References
NGCC references in the literature.
- GetMeanPNCC(y, sr=16000)[source]¶
Return mean PNCC coefficients across time.
- Parameters:
y (numpy.ndarray) – Signal.
sr (int) – Sample rate.
- Returns:
Mean PNCC vector.
- Return type:
References
PNCC implementation: https://github.com/supikiti/PNCC/blob/master/pncc.py
- GetMeanPSRCC(y, sr=16000)[source]¶
Return mean PSRCC coefficients across time.
- Parameters:
y (numpy.ndarray) – Signal.
sr (int) – Sample rate.
- Returns:
Mean PSRCC vector.
- Return type:
References
PSRCC reference: http://www.apsipa.org/proceedings/2018/pdfs/0001945.pdf
- GetMeanPLP(y, sr)[source]¶
Return mean PLP coefficients across time.
- Parameters:
y (numpy.ndarray) – Signal.
sr (int) – Sample rate.
- Returns:
Mean PLP vector.
- Return type:
References
PLP literature.
- GetMeanRPLP(y, sr)[source]¶
Return mean RPLP coefficients across time.
- Parameters:
y (numpy.ndarray) – Signal.
sr (int) – Sample rate.
- Returns:
Mean RPLP vector.
- Return type:
References
RPLP and rasta-PLP references.
- GetAllMeanAudioFeatures(y, sr=16000)[source]¶
Compute all mean audio features available in this class.
- Parameters:
y (numpy.ndarray) – Signal.
sr (int) – Sample rate.
- Returns:
Dictionary with mean feature vectors for all available features.
- Return type:
- 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:
References
Parselmouth (Praat bindings) documentation: https://parselmouth.readthedocs.io/en/stable/
- 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:
- Returns:
Mel spectrogram image with shape (numMels, outFrames, 3) if axisLast is True, else (3, numMels, outFrames).
- Return type: