Repository: huggingface/datasets
File: tests/features/test_audio.py (Python)

Code before:
            assert audio == {"path": audio_path, "bytes": None}

    dset.map(assert_audio_batch_undecoded, batched=True)


def test_audio_embed_storage(shared_datadir):
    audio_path = str(shared_datadir / "test_audio_44100.wav")
    example = {"bytes": None, "path": audio_path}
    storage = pa.array([example], type=pa.struct({"bytes": pa.binary(), "path": pa.string()}))
    embedded_storage = Audio().embed_storage(storage)
    embedded_example = embedded_storage.to_pylist()[0]
    assert embedded_example == {"bytes": open(audio_path, "rb").read(), "path": "test_audio_44100.wav"}


@require_torchcodec
def test_audio_decode_example_opus_convert_to_stereo(shared_datadir):
    # GH 7837
    from torchcodec.decoders import AudioDecoder

    audio_path = str(shared_datadir / "test_audio_48000.opus")
    audio = Audio(num_channels=2)
    decoded_example = audio.decode_example(audio.encode_example(audio_path))
    assert isinstance(decoded_example, AudioDecoder)
    samples = decoded_example.get_all_samples()
    assert samples.sample_rate == 48000
    assert samples.data.shape == (2, 48000)


Review comment (performance):
added a test from stereo to mono as well :)

```suggestion
@require_torchcodec
def test_audio_decode_example_opus_convert_to_stereo(shared_datadir):
    # GH 7837
    from torchcodec.decoders import AudioDecoder

    audio_path = str(shared_datadir / "test_audio_48000.opus")  # mono file
    audio = Audio(num_channels=2)
    decoded_example = audio.decode_example(audio.encode_example(audio_path))
    assert isinstance(decoded_example, AudioDecoder)
    samples = decoded_example.get_all_samples()
    assert samples.sample_rate == 48000
    assert samples.data.shape == (2, 48000)


@require_torchcodec
def test_audio_decode_example_opus_convert_to_mono(shared_datadir):
    # GH 7837
    from torchcodec.decoders import AudioDecoder

    audio_path = str(shared_datadir / "test_audio_44100.wav")  # stereo file
    audio = Audio(num_channels=1)
    decoded_example = audio.decode_example(audio.encode_example(audio_path))
    assert isinstance(decoded_example, AudioDecoder)
    samples = decoded_example.get_all_samples()
    assert samples.sample_rate == 44100
    assert samples.data.shape == (1, 202311)
```

Code after:
            assert audio == {"path": audio_path, "bytes": None}

    dset.map(assert_audio_batch_undecoded, batched=True)


def test_audio_embed_storage(shared_datadir):
    audio_path = str(shared_datadir / "test_audio_44100.wav")
    example = {"bytes": None, "path": audio_path}
    storage = pa.array([example], type=pa.struct({"bytes": pa.binary(), "path": pa.string()}))
    embedded_storage = Audio().embed_storage(storage)
    embedded_example = embedded_storage.to_pylist()[0]
    assert embedded_example == {"bytes": open(audio_path, "rb").read(), "path": "test_audio_44100.wav"}


@require_torchcodec
def test_audio_decode_example_opus_convert_to_stereo(shared_datadir):
    # GH 7837
    from torchcodec.decoders import AudioDecoder

    audio_path = str(shared_datadir / "test_audio_48000.opus")  # mono file
    audio = Audio(num_channels=2)
    decoded_example = audio.decode_example(audio.encode_example(audio_path))
    assert isinstance(decoded_example, AudioDecoder)
    samples = decoded_example.get_all_samples()
    assert samples.sample_rate == 48000
    assert samples.data.shape == (2, 48000)


@require_torchcodec
def test_audio_decode_example_opus_convert_to_mono(shared_datadir):
    # GH 7837
    from torchcodec.decoders import AudioDecoder

    audio_path = str(shared_datadir / "test_audio_44100.wav")  # stereo file
    audio = Audio(num_channels=1)
    decoded_example = audio.decode_example(audio.encode_example(audio_path))
    assert isinstance(decoded_example, AudioDecoder)
    samples = decoded_example.get_all_samples()
    assert samples.sample_rate == 44100
    assert samples.data.shape == (1, 202311)