Skip to content

Commit

Permalink
Merge pull request #843 from kitzeslab/issue_837_from_url
Browse files Browse the repository at this point in the history
fix Audio.from_url raising AxisError #837
  • Loading branch information
sammlapp authored Oct 4, 2023
2 parents 9d05f10 + 810b9c6 commit d2db671
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
6 changes: 5 additions & 1 deletion opensoundscape/audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import scipy

import librosa
import librosa.core.audio
import soundfile
import IPython.display
from aru_metadata_parser.parse import parse_audiomoth_metadata
Expand Down Expand Up @@ -407,7 +408,10 @@ def from_url(cls, url, sample_rate=None, resample_type="kaiser_fast"):
samples, original_sample_rate = soundfile.read(
io.BytesIO(urllib.request.urlopen(url).read())
)
samples = samples.mean(1) # sum to mono

# sum to mono, if multiple channels
samples = librosa.core.audio.to_mono(samples.transpose())

if sample_rate is not None and sample_rate != original_sample_rate:
samples = librosa.resample(
samples,
Expand Down
9 changes: 9 additions & 0 deletions tests/test_audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -851,3 +851,12 @@ def test_estimate_delay_return_cc_max(veryshort_audio):
ccmax, sum(section_used.samples * section_used.samples), abs_tol=1e-5
)
assert math.isclose(delay, 0, abs_tol=1e-6)


def test_from_url_multichannel_to_mono():
"""note: test will fail if the file is removed from xeno-canto
or is inaccessible at this url
downloads a 2-channel audio file and sums to 1, ensuring resolution of #837
"""
Audio.from_url("https://xeno-canto.org/830406/download")

0 comments on commit d2db671

Please sign in to comment.