rename module
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
import librosa
|
import librosa
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
||||||
def generate_spectogram(audio_file: str, spectogram_type: str = 'mel', sr: int = 22050):
|
def generate_spectrogram(audio_file: str, spectrogram_type: str = 'mel', sr: int = 22050):
|
||||||
"""
|
"""
|
||||||
Generates a spectrogram array from an audio file.
|
Generates a spectrogram array from an audio file.
|
||||||
|
|
||||||
@@ -19,16 +19,16 @@ def generate_spectogram(audio_file: str, spectogram_type: str = 'mel', sr: int =
|
|||||||
# Load the audio file
|
# Load the audio file
|
||||||
waveform, sr = librosa.load(audio_file, sr=sr)
|
waveform, sr = librosa.load(audio_file, sr=sr)
|
||||||
|
|
||||||
# Create the spectogram
|
# Create the spectrogram
|
||||||
if spectogram_type == 'mel':
|
if spectrogram_type == 'mel':
|
||||||
spec = librosa.feature.melspectrogram(y=waveform, sr=sr)
|
spec = librosa.feature.melspectrogram(y=waveform, sr=sr)
|
||||||
spec_db = librosa.power_to_db(spec, ref=np.max) # Convert to decibels
|
spec_db = librosa.power_to_db(spec, ref=np.max) # Convert to decibels
|
||||||
plot_data = {'sr': sr, 'x_axis': 'time', 'y_axis': 'mel'}
|
plot_data = {'sr': sr, 'x_axis': 'time', 'y_axis': 'mel'}
|
||||||
elif spectogram_type == 'stft':
|
elif spectrogram_type == 'stft':
|
||||||
spec = np.abs(librosa.stft(waveform))
|
spec = np.abs(librosa.stft(waveform))
|
||||||
spec_db = librosa.amplitude_to_db(spec, ref=np.max)
|
spec_db = librosa.amplitude_to_db(spec, ref=np.max)
|
||||||
plot_data = {'sr': sr, 'x_axis': 'time', 'y_axis': 'log'}
|
plot_data = {'sr': sr, 'x_axis': 'time', 'y_axis': 'log'}
|
||||||
else:
|
else:
|
||||||
raise ValueError(f"Unsupported spectogram type: {spectogram_type}")
|
raise ValueError(f"Unsupported spectrogram type: {spectrogram_type}")
|
||||||
|
|
||||||
return spec_db, plot_data
|
return spec_db, plot_data
|
||||||
Reference in New Issue
Block a user