Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Quentin Aristote
Soundscapes
Commits
41683156
Commit
41683156
authored
Dec 10, 2019
by
Quentin Aristote
Browse files
use torchaudio instead of librosa
parent
587ad623
Changes
3
Hide whitespace changes
Inline
Side-by-side
data/get_sounds.py
View file @
41683156
...
...
@@ -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
get
Recording
s
(
directory
=
'.'
,
soundscape
=
False
,
species
=
False
,
validated
=
False
,
not_validated
=
False
,
sort_by
=
None
,
since
=
None
,
overwrite
=
False
)
:
"""Download all the
recording
s corresponding to specific filters.
def
get
Sound
s
(
directory
=
'.'
,
soundscape
=
False
,
species
=
False
,
validated
=
False
,
not_validated
=
False
,
sort_by
=
None
,
since
=
None
,
overwrite
=
False
)
:
"""Download all the
sound
s 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__'
:
get
Recording
s
(
directory
=
'sounds'
,
soundscape
=
True
,
validated
=
True
)
get
Sound
s
(
directory
=
'sounds'
,
soundscape
=
True
,
validated
=
True
)
data/get_spectrograms.py
View file @
41683156
# -*- 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'
)
data/requirements.txt
View file @
41683156
bs4
librosa
torch
torchaudio
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment