Skip to content
Open
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
2 changes: 1 addition & 1 deletion python/lc3.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ def encode(self, pcm, num_bytes: int, bit_depth: int | None = None) -> bytes:
if not abs(sum(pcm_buffer)) / frame_samples < 2:
raise InvalidArgumentError("Out of range PCM input")

padding = max(pcm_len - frame_samples, 0)
padding = max(pcm_len - len(pcm_buffer), 0)
pcm_buffer.extend(array.array("f", [0] * padding))

else:
Expand Down
25 changes: 25 additions & 0 deletions python/tests/basic_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,31 @@ def test_silence_roundtrip(bit_depth: int | None) -> None:
assert rms < 1e-4, f"Expected silence, but got RMS {rms:.6f}"


@pytest.mark.parametrize("num_channels", [1, 2])
@pytest.mark.parametrize("bit_depth", [None, 16, 24])
def test_encode_pads_short_input(num_channels: int, bit_depth: int | None) -> None:
"""Verifies that PCM input shorter than a frame is padded with zeros."""
spf = lc3.Encoder(
frame_duration_us=10000, sample_rate_hz=48000, num_channels=num_channels
).get_frame_samples()
pcm_len = num_channels * spf

short = _generate_sine(pcm_len // 3, 48000, freq_hz=440.0)
padded = short + [0.0] * (pcm_len - len(short))

encoded = []
for samples in (short, padded):
encoder = lc3.Encoder(
frame_duration_us=10000, sample_rate_hz=48000, num_channels=num_channels
)
pcm_in = _floats_to_pcm(samples, bit_depth)
encoded.append(
encoder.encode(pcm_in, num_bytes=120 * num_channels, bit_depth=bit_depth)
)

assert encoded[0] == encoded[1]


@pytest.mark.parametrize("bit_depth", [None, 16, 24])
def test_packet_loss_concealment(bit_depth: int | None) -> None:
"""Verifies PLC generates smooth concealed frames when packet loss occurs."""
Expand Down
Loading