Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix encoding g722 format #3373

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions test/torchaudio_unittest/io/stream_writer_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,22 @@ def test_audio_num_frames_lossy(self, ext, num_channels, sample_rate):
return
self.assertEqual(saved.shape, data.shape)

def test_g722_sample_rate(self):
"""Encoding G.722 properly converts sample rate to 16k"""
filename = "test.g722"
sample_rate = 41000
data = get_sinusoid(sample_rate=sample_rate, n_channels=1, channels_first=False)

# write data
dst = self.get_temp_path(filename)
w = StreamWriter(dst, format="g722")
w.add_audio_stream(sample_rate=sample_rate, num_channels=1)
with w.open():
w.write_audio_chunk(0, data)

r = StreamReader(src=self.get_temp_path(filename))
self.assertEqual(r.get_src_stream_info(0).sample_rate, 16000)

def test_preserve_fps(self):
"""Decimal point frame rate is properly saved

Expand Down
14 changes: 14 additions & 0 deletions torchaudio/csrc/ffmpeg/stream_writer/encode_process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,20 @@ int get_enc_sr(
int src_sample_rate,
const c10::optional<int>& encoder_sample_rate,
const AVCodec* codec) {
// G.722 only supports 16000 Hz, but it does not list the sample rate in
// supported_samplerates so we hard code it here.
if (codec->id == AV_CODEC_ID_ADPCM_G722) {
if (encoder_sample_rate) {
auto val = encoder_sample_rate.value();
TORCH_CHECK(
val == 16'000,
codec->name,
" does not support sample rate ",
val,
". Supported values are; 16000.");
}
return 16'000;
}
if (encoder_sample_rate) {
const int& encoder_sr = encoder_sample_rate.value();
TORCH_CHECK(
Expand Down