Skip to content

Commit

Permalink
Merge pull request #19 from thunn/thunn/update_load_to_convert_stereo…
Browse files Browse the repository at this point in the history
…_to_mono

update load fn to convert stereo to mono
  • Loading branch information
maxrmorrison authored Jul 15, 2024
2 parents 8b1c039 + 802fd20 commit f1f0462
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 0 deletions.
6 changes: 6 additions & 0 deletions penn/load.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import warnings

import torchaudio

Expand All @@ -9,6 +10,11 @@ def audio(file):
"""Load audio from disk"""
audio, sample_rate = torchaudio.load(file)

# if audio is stereo convert to mono
if audio.size(0) == 2:
warnings.warn(f'Converting stereo audio to mono: {file}')
audio = audio.mean(dim=0, keepdim=True)

# Maybe resample
return penn.resample(audio, sample_rate)

Expand Down
Binary file added test/assets/500Hz_stereo.wav
Binary file not shown.
5 changes: 5 additions & 0 deletions test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,8 @@
def audio():
"""Retrieve the test audio"""
return penn.load.audio(Path(__file__).parent / 'assets' / 'gershwin.wav')

@pytest.fixture(scope='session')
def audio_stereo():
"""Retrieve the test audio"""
return penn.load.audio(Path(__file__).parent / 'assets' / '500Hz_stereo.wav')
9 changes: 9 additions & 0 deletions test/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,12 @@ def test_infer(audio):
center='half-hop')
shape = (1, audio.shape[1] // penn.HOPSIZE)
assert pitch.shape == periodicity.shape == shape

def test_infer_stereo(audio_stereo):
"""Test that inference on stereo audio produces the correct shape"""
pitch, periodicity = penn.from_audio(
audio_stereo,
penn.SAMPLE_RATE,
center='half-hop')
shape = (1, audio_stereo.shape[1] // penn.HOPSIZE)
assert pitch.shape == periodicity.shape == shape

0 comments on commit f1f0462

Please sign in to comment.