Skip to content

Commit

Permalink
added soundfile as primary loader, fixes #845
Browse files Browse the repository at this point in the history
  • Loading branch information
bmcfee committed Mar 11, 2019
1 parent be3c320 commit 47c41fc
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .travis_dependencies.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ if [ ! -d "$src" ]; then

source activate $ENV_NAME

conda install -c conda-forge ffmpeg
conda install -c conda-forge ffmpeg pysoundfile

pip install python-coveralls

Expand Down
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,17 @@ librosa (0.x.x, /path/to/librosa)

### Hints for the Installation

#### audioread
#### audioread and MP3 support

*Note that `audioread` needs at least one of the programs to work properly.*

`librosa` uses `audioread` to load audio files.
`librosa` uses `soundfile` and `audioread` to load audio files.
Note that `soundfile` does not currently support MP3, which will cause librosa to
fall back on the `audioread` library.

To fuel `audioread` with more audio-decoding power (e. g. for reading MP3 files),
To fuel `audioread` with more audio-decoding power (e.g., for reading MP3 files),
you can either install *ffmpeg* or *GStreamer*.

*Note that `audioread` needs at least one of the programs to work properly.*

If you are using Anaconda, install *ffmpeg* by calling
```
conda install -c conda-forge ffmpeg
Expand Down
60 changes: 44 additions & 16 deletions librosa/core/audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import os
import six

import soundfile as sf
import audioread
import numpy as np
import scipy.signal
Expand Down Expand Up @@ -69,7 +70,7 @@ def load(path, sr=22050, mono=True, offset=0.0, duration=None,
To use a faster method, set `res_type='kaiser_fast'`.
To use `scipy.signal.resample`, set `res_type='scipy'`.
.. note::
This uses `audioread`, which may truncate the precision of the
audio data to 16 bits.
Expand Down Expand Up @@ -115,6 +116,40 @@ def load(path, sr=22050, mono=True, offset=0.0, duration=None,
"""

try:
with sf.SoundFile(path) as sf_desc:
sr_native = sf_desc.samplerate
if offset:
# Seek to the start of the target read
sf_desc.seek(offset * sr_native)
if duration:
frame_duration = duration * sr_native

# Load the target number of frames, and transpose to match librosa form
y = sf_desc.read(frames=frame_duration, dtype=dtype, always_2d=False).T
except:
# If soundfile failed, fall back to the audioread loader
y, sr_native = __audioread_load(path, offset, duration, dtype)

# Final cleanup for dtype and contiguity
if mono:
y = to_mono(y)

if sr is not None:
y = resample(y, sr_native, sr, res_type=res_type)

else:
sr = sr_native

return y, sr


def __audioread_load(path, offset, duration, dtype):
'''Load an audio buffer using audioread.
This loads one block at a time, and then concatenates the results.
'''

y = []
with audioread.audio_open(path) as input_file:
sr_native = input_file.samplerate
Expand Down Expand Up @@ -157,22 +192,12 @@ def load(path, sr=22050, mono=True, offset=0.0, duration=None,

if y:
y = np.concatenate(y)

if n_channels > 1:
y = y.reshape((-1, n_channels)).T
if mono:
y = to_mono(y)

if sr is not None:
y = resample(y, sr_native, sr, res_type=res_type)

else:
sr = sr_native

# Final cleanup for dtype and contiguity
y = np.ascontiguousarray(y, dtype=dtype)
else:
y = np.empty(0, dtype=dtype)

return (y, sr)
return y, sr_native


@cache(level=20)
Expand Down Expand Up @@ -380,8 +405,11 @@ def get_duration(y=None, sr=22050, S=None, n_fft=2048, hop_length=512,
"""

if filename is not None:
with audioread.audio_open(filename) as fdesc:
return fdesc.duration
try:
return sf.info(filename).duration
except:
with audioread.audio_open(filename) as fdesc:
return fdesc.duration

if y is None:
if S is None:
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
'six >= 1.3',
'resampy >= 0.2.0',
'numba >= 0.38.0',
'soundfile >= 0.9.0',
],
extras_require={
'docs': ['numpydoc', 'sphinx!=1.3.1', 'sphinx_rtd_theme',
Expand Down

0 comments on commit 47c41fc

Please sign in to comment.