import numpy as np
from ruopus import Bandwidth, OpusDecoder, OpusEncoder
SR = 48000
FRAME = 960
def speech_like() -> np.ndarray:
t = np.arange(FRAME) / SR
sig = 0.3 * np.sin(2 * np.pi * 150 * t) + 0.1 * np.sin(2 * np.pi * 900 * t)
return sig.astype(np.float32).reshape(FRAME, 1)
def main() -> None:
frame = speech_like()
enc = OpusEncoder(1, bitrate=24000)
enc.complexity = 6
enc.dtx = False
print(f"configured: {enc!r}")
print(f" complexity={enc.complexity}, bitrate={enc.bitrate}")
enc.bandwidth = Bandwidth.WideBand
auto = enc.encode_auto(frame) silk = enc.encode_silk(frame) celt = enc.encode(frame) enc.bandwidth = Bandwidth.FullBand
hybrid = enc.encode_hybrid(frame)
for name, packet in [("auto", auto), ("silk", silk), ("hybrid", hybrid), ("celt", celt)]:
dec = OpusDecoder(1)
pcm = dec.decode_packet(packet)
print(f" {name:6s}: {len(packet):4d}-byte packet -> {pcm.shape}")
enc.dtx = True
silence = np.zeros((FRAME, 1), dtype=np.float32)
sizes = [len(enc.encode_auto(silence)) for _ in range(20)]
print(f" DTX over 20 silent frames -> packet sizes settle to {min(sizes)} byte(s)")
if __name__ == "__main__":
main()