Repository navigation
Replies: 2 comments
|
Two details matter for this streaming case: keep decoder/resampler state across chunks, and export the actual audio samples rather than the whole padded plane buffer. For raw G.722 payloads, a persistent decoder can look like: import av
decoder = av.CodecContext.create("g722", "r")
decoder.sample_rate = 16000
decoder.layout = "mono"
resampler = av.AudioResampler(format="s16", layout="mono", rate=16000)
def convert_chunk(chunk):
# chunk=None only when the stream ends, to flush the decoder.
packet = None if chunk is None else av.Packet(chunk)
for frame in decoder.decode(packet):
for out in resampler.resample(frame):
yield out.to_ndarray().astype("<i2", copy=False).tobytes()
if chunk is None:
for out in resampler.resample(None):
yield out.to_ndarray().astype("<i2", copy=False).tobytes()Create these objects once per incoming stream, not on every chunk. If receiving RTP, remove its transport header first; the decoder needs G.722 payload data. G.722 audio is 16 kHz even though its RTP timestamp clock is conventionally 8 kHz, so those two rates must not be conflated.
I tested this decoder/resampler pattern on a synthetic one-second G.722 stream with PyAV 19.0.1: it produced exactly 32,000 bytes of mono s16le at 16 kHz. See the AudioFrame/AudioResampler APIs. |
|
Thanks for your answer 🙏 . I will give it a try. In the meantime ALaw STT providers got much better so it might not be needed, but still a good thing to have the option. |
Uh oh!
There was an error while loading. Please reload this page.
Hello,
I'm trying to build a telco app and I need to convert audio codecs used in telco to raw pcm s16le for processing using voice-activity-detection, speech-to-text and other technologies. I managed to build a converter using libav from ALaw to PCM like this:
I'm not sure why, but I had to strip few bytes of the end of each packet:
But it works and the output audio is clear and VAD/STT works without any issue.
Now I'm trying to build the same for G722 codec, but I haven't been able to get the code to work. My first attempt was simply replacing the decoder in the code above, but that did not work. So I tried to use LLM to generate the code for me, but that also resulted in non working solution:
Any ideas how to get this to work?
I also tried running ffmpeg as a subprocess and simply sending audio to STDIN and getting it back from STDOUT like this
But that caused delay and I haven't been able to figure out where the delay comes from.
Thanks for any ideas 🙏
All reactions