Commit 41683156 authored by Quentin Aristote's avatar Quentin Aristote
Browse files

use torchaudio instead of librosa

parent 587ad623
......@@ -51,7 +51,7 @@ def getBaseURL(soundscape = False, species = False,
elif sort_by == 'title' :
sort_by = 't'
else :
raise NotImplementedError("sort_by cannot be set to " + sort_by)
raise NotImplementedError("sort_by cannot be set to " + str(sort_by))
filters.append(SORT_BY_PATTERN.format(sort_by = sort_by))
if since :
if since == 'day' :
......@@ -63,7 +63,7 @@ def getBaseURL(soundscape = False, species = False,
elif since == 'year' :
since = 'y'
else :
raise NotImplementedError("since cannot be set to " + since)
raise NotImplementedError("since cannot be set to " + str(since))
filters.append(SINCE_PATTERN.format(since = since))
url = URL_SEARCH_PATTERN.format(filters = ','.join(filters))
......@@ -155,13 +155,13 @@ def getSoundMetadata(html) :
raise NotImplementedError('todo')
def getRecordings(directory = '.',
soundscape = False, species = False,
validated = False, not_validated = False,
sort_by = None,
since = None,
overwrite = False) :
"""Download all the recordings corresponding to specific filters.
def getSounds(directory = '.',
soundscape = False, species = False,
validated = False, not_validated = False,
sort_by = None,
since = None,
overwrite = False) :
"""Download all the sounds corresponding to specific filters.
:param directory: the path to save the recordings to.
:param soundscape: Whether to get ambient sounds. If ambiant and species are False,
......@@ -200,4 +200,4 @@ def getRecordings(directory = '.',
return None
if __name__ == '__main__' :
getRecordings(directory = 'sounds', soundscape = True, validated = True)
getSounds(directory = 'sounds', soundscape = True, validated = True)
# -*- coding: utf-8 -*-
import librosa
import os.path
import pickle
import warnings
import torch
import torchaudio
import torchaudio.transforms as transforms
def soundToSpectrogram(path, samplerate = 16000) :
"""Return the Mel Spectrogram of an audio file.
def soundToSpectrogram(path, sample_rate = 16000, duration = 30) :
r"""Return the Mel Spectrogram of an audio file.
:param filename: the path to the file.
:param samplerate: the sample rate to use when getting the spectrogram."""
Args :
path (str): the path to the file.
sample_rate (int, optional) : the sample rate in Hz to use when getting the spectrogram. Default : 16000 Hz
duration (int, optional) : the number of seconds the recording should last. Default : 30s
Returns :
torch.Tensor : Mel frequency spectrogram of size (2, 128, time)"""
# Load the recording
waveform, orig_freq = torchaudio.load(path)
# Convert to mono
waveform[:] = waveform.mean(dim = 0)
# Resample
resample = transforms.Resample(orig_freq = orig_freq, new_freq = sample_rate)
waveform = resample(waveform)
# Extract a certain duration
try :
waveform = waveform[:, :duration * sample_rate]
except :
raise TypeError('the recording is too long or too short')
# Compute the Mel-spectrogram
melspectrogram = transforms.MelSpectrogram(sample_rate = sample_rate)
spectrogram = melspectrogram(waveform)
with warnings.catch_warnings() :
warnings.filterwarnings('ignore', category = UserWarning)
signal, samplerate = librosa.load(path, sr = samplerate)
spectrogram = librosa.feature.melspectrogram(y = signal, sr = samplerate)
return spectrogram
def getSpectrograms(source_directory, target_directory, samplerate = 16000, overwrite = False) :
"""Compute the Mel spectrograms of all the sound files.
def getSpectrograms(dir_source, dir_target,
sample_rate = 16000,
overwrite = False,
duration = 30) :
r"""Compute the Mel spectrograms of all the sound files and write them to memory.
:param source_path: the directory where the sound files are.
:param target_path: the directory to save the the spectrograms to.
:param samplerate: the sample rate to use when getting the spectrogram."""
Args :
dir_source (str) : the directory where the sound files are.
dir_target (str) : the directory to save the the spectrograms to.
sample_rate (int, optional) : the sample rate in Hz to use when getting the spectrogram. Default : 16000 Hz
overwrite (bool, optional) : whether to compute all spectrograms again. Default : False
duration (int, optional) : the number of seconds the recording should last. Default : 30s"""
for filename in os.listdir(source_directory) :
for filename in os.listdir(dir_source) :
title, _ = os.path.splitext(filename)
source_path = os.path.join(source_directory, filename)
target_path = os.path.join(target_directory, title + '.bin')
if overwrite :
mode = 'w+b'
else :
mode = 'x+b'
print('Computing the Mel spectrogram of {filename} ...'.format(filename = filename))
try :
with open(target_path, mode) as file :
spectrogram = soundToSpectrogram(source_path, samplerate = samplerate)
spectrogram.tofile(file)
print('Success.')
except FileExistsError as exception :
print('Failure : {exception}'.format(exception = exception))
path_source = os.path.join(dir_source, filename)
path_target = os.path.join(dir_target, title + '.pt')
print('Computing the Mel-spectrogram of {filename} ...'.format(filename = filename))
if overwrite or not(os.path.isfile(path_target)) :
try :
spectrogram = soundToSpectrogram(path_source, sample_rate = sample_rate, duration = duration)
torch.save(spectrogram, path_target)
print('Success.')
except Exception as exception :
print('Failure : {exception}.'.format(exception = exception))
print('Mel-spectrogram already computed.')
if __name__ == '__main__' :
getSpectrograms('sounds', 'spectrograms')
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment