Skip to content

Commit

Permalink
Fix AudioEffector for mulaw (#3372)
Browse files Browse the repository at this point in the history
Summary:
When encoding audio with mulaw, the resulting data does not have header, and the StreamReader defaults to 16k Hz, which can strech/shrink the resulting waveform.

Pull Request resolved: #3372

Reviewed By: hwangjeff

Differential Revision: D46234772

Pulled By: mthrok

fbshipit-source-id: 942c89a8cfe29b0b6f57b3e5b6c9dfd3524ca552
  • Loading branch information
mthrok authored and facebook-github-bot committed May 27, 2023
1 parent 1b05ca7 commit af932cc
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
7 changes: 5 additions & 2 deletions test/torchaudio_unittest/io/effector_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,17 @@ def test_null(self):
("ogg", "flac"), # flac only supports s16 and s32
("ogg", "opus"), # opus only supports 48k Hz
("ogg", "vorbis"), # vorbis only supports stereo
# ("ogg", "vorbis", 44100),
# this fails with small descrepancy; 441024 vs 441000
# TODO: investigate
("wav", None),
("wav", "pcm_u8"),
("mp3", None),
("mulaw", None, 44100), # mulaw is encoded without header
]
)
def test_formats(self, format, encoder):
def test_formats(self, format, encoder, sample_rate=8000):
"""Formats (some with restrictions) just work without an issue in effector"""
sample_rate = 8000

effector = AudioEffector(format=format, encoder=encoder)
original = get_sinusoid(n_channels=3, sample_rate=sample_rate, channels_first=False)
Expand Down
4 changes: 4 additions & 0 deletions torchaudio/io/_effector.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,10 @@ def _get_reader(self, waveform, sample_rate, frames_per_chunk=None):
muxer = self.format
encoder = self.encoder
option = {}
# Some formats are headerless, so need to provide these infomation.
if self.format == "mulaw":
option = {"sample_rate": f"{sample_rate}", "channels": f"{num_channels}"}

else: # PCM
muxer = _get_muxer(waveform.dtype)
encoder = None
Expand Down

0 comments on commit af932cc

Please sign in to comment.