1import array
2import lc3
3import pytest
4
5
6@pytest.mark.parametrize("bit_depth,decoded_length", [(16, 960), (24, 1440)])
7def test_decode_with_bit_depth(bit_depth, decoded_length) -> None:
8    decoder = lc3.Decoder(frame_duration_us=10000, sample_rate_hz=48000)
9    decoded_frame = decoder.decode(bytes(120), bit_depth=bit_depth)
10    assert isinstance(decoded_frame, bytes)
11    assert len(decoded_frame) == decoded_length
12
13
14def test_decode_without_bit_depth() -> None:
15    decoder = lc3.Decoder(frame_duration_us=10000, sample_rate_hz=48000)
16    decoded_frame = decoder.decode(bytes(120))
17    assert isinstance(decoded_frame, array.array)
18    assert len(decoded_frame) == 480
19    assert all(isinstance(e, float) for e in decoded_frame)
20
21
22def test_decode_with_bad_bit_depth() -> None:
23    decoder = lc3.Decoder(frame_duration_us=10000, sample_rate_hz=48000)
24    with pytest.raises(lc3.InvalidArgumentError):
25        decoder.decode(bytes(120), bit_depth=128)
26
27
28@pytest.mark.parametrize("bit_depth", [16, 24])
29def test_encode_with_bit_depth(bit_depth) -> None:
30    encoder = lc3.Encoder(frame_duration_us=10000, sample_rate_hz=48000)
31    encoded_frame = encoder.encode(bytes(1920), num_bytes=120, bit_depth=bit_depth)
32    assert isinstance(encoded_frame, bytes)
33    assert len(encoded_frame) == 120
34
35
36@pytest.mark.parametrize("pcm", [bytes(1920), [0.0] * 1920])
37def test_encode_without_bit_depth(pcm) -> None:
38    encoder = lc3.Encoder(frame_duration_us=10000, sample_rate_hz=48000)
39    encoded_frame = encoder.encode(pcm, num_bytes=120, bit_depth=None)
40    assert isinstance(encoded_frame, bytes)
41    assert len(encoded_frame) == 120
42
43
44def test_encode_with_bad_bit_depth() -> None:
45    encoder = lc3.Encoder(frame_duration_us=10000, sample_rate_hz=48000)
46    with pytest.raises(lc3.InvalidArgumentError):
47        encoder.encode(bytes(1920), num_bytes=120, bit_depth=128)
48