Implement audio normalization and trimming functions
- Normalize audio to the range [-1, 1] - Trim silence from audio - Add test cases for both functions
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
import librosa
|
||||
import numpy as np
|
||||
|
||||
def normalize_audio(audio: np.ndarray) -> np.ndarray:
|
||||
"""
|
||||
Normalize the audio to a range of [-1, 1].
|
||||
|
||||
Args:
|
||||
- audio (np.ndarray): The audio time series to normalize.
|
||||
|
||||
Returns:
|
||||
- np.ndarray: The normalized audio time series.
|
||||
"""
|
||||
return librosa.util.normalize(audio)
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
import librosa
|
||||
import numpy as np
|
||||
|
||||
def trim_audio(audio:np.ndarray, sr:int) -> np.ndarray:
|
||||
"""
|
||||
Trim leading and trailing silence from the audio.
|
||||
|
||||
Args:
|
||||
- audio (np.ndarray): The audio time series.
|
||||
- sr (int): The sample rate of the audio.
|
||||
|
||||
Returns:
|
||||
- np.ndarray: The trimmed audio time series.
|
||||
"""
|
||||
|
||||
audio_trimmed, _ = librosa.effects.trim(audio)
|
||||
return audio_trimmed
|
||||
Reference in New Issue
Block a user