Skip to content

Commit 081d5e4

Browse files
committed
python: Add PLC interface for the python wrapper
1 parent 9f1e206 commit 081d5e4

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

python/lc3.py

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -472,21 +472,23 @@ def __del__(self) -> None:
472472

473473
@typing.overload
474474
def decode(
475-
self, data: bytes | bytearray | memoryview, bit_depth: None = None
475+
self, data: bytes | bytearray | memoryview | None, bit_depth: None = None
476476
) -> array.array[float]: ...
477477

478478
@typing.overload
479-
def decode(self, data: bytes | bytearray | memoryview, bit_depth: int) -> bytes: ...
479+
def decode(self, data: bytes | bytearray | memoryview | None, bit_depth: int) -> bytes: ...
480480

481481
def decode(
482-
self, data: bytes | bytearray | memoryview, bit_depth: int | None = None
482+
self, data: bytes | bytearray | memoryview | None, bit_depth: int | None = None
483483
) -> bytes | array.array[float]:
484484
"""
485485
Decodes an LC3 frame.
486486
487487
The input `data` is the channels concatenation of LC3 frames in a
488488
byte-like object. Interleaved PCM samples are returned according to
489489
the `bit_depth` indication.
490+
Setting `data` to `None` enables PLC (Packet Loss Concealment)
491+
reconstruction for the block of LC3 frames.
490492
When no `bit_depth` is defined, it's a vector of floating point values
491493
from -1 to 1, coding the sample levels. When `bit_depth` is defined,
492494
it returns a byte array, each sample coded on `bit_depth` bits.
@@ -500,22 +502,28 @@ def decode(
500502
pcm_len = num_channels * self.get_frame_samples()
501503
pcm_buffer = (pcm_t * pcm_len)()
502504

503-
data_buffer = bytearray(data)
504-
data_offset = 0
505+
if data is not None:
506+
data_buffer = bytearray(data)
507+
data_offset = 0
505508

506509
for ich, decoder in enumerate(self.__decoders):
507510
pcm_offset = ich * ctypes.sizeof(pcm_t)
508511
pcm = (pcm_t * (pcm_len - ich)).from_buffer(pcm_buffer, pcm_offset)
509512

510-
data_size = len(data_buffer) // num_channels + int(
511-
ich < len(data_buffer) % num_channels
512-
)
513-
buf = (c_byte * data_size).from_buffer(data_buffer, data_offset)
514-
data_offset += data_size
513+
if data is None:
514+
ret = self.lib.lc3_decode(
515+
decoder, None, 0, pcm_fmt, pcm, self.num_channels
516+
)
517+
else:
518+
data_size = len(data_buffer) // num_channels + int(
519+
ich < len(data_buffer) % num_channels
520+
)
521+
buf = (c_byte * data_size).from_buffer(data_buffer, data_offset)
522+
data_offset += data_size
523+
ret = self.lib.lc3_decode(
524+
decoder, buf, len(buf), pcm_fmt, pcm, self.num_channels
525+
)
515526

516-
ret = self.lib.lc3_decode(
517-
decoder, buf, len(buf), pcm_fmt, pcm, self.num_channels
518-
)
519527
if ret < 0:
520528
raise InvalidArgumentError("Bad parameters")
521529

0 commit comments

Comments
 (0)