Skip to content

Commit

Permalink
Fix AudioEffector for mulaw
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mthrok committed May 26, 2023
1 parent c6624fa commit 93536ca
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
9 changes: 7 additions & 2 deletions test/torchaudio_unittest/io/effector_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,24 @@ 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)

output = effector.apply(original, sample_rate)
print(original.shape)
print(output.shape)

# On 4.1 OPUS produces 8020 samples (extra 20)
# this has been fixed on 4.2+
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 93536ca

Please sign in to comment.