nimbleape / asterisk-dialogflow-rtp-audioserver

MIT License
43 stars 18 forks source link

How do I debug corrupted audio playback with ulaw encoding? #26

Closed vcidst closed 2 years ago

vcidst commented 2 years ago

I'm trying to get this repo to work with OUTPUT_AUDIO_ENCODING_MULAW encoding of dialogflow. However the audio playback to Asterisk is corrupted.

There's a lot of things going on in _sendAudioToAsterisk() method. How did you debug/figure out this sorcery? I believe you're splitting the audio data into 320 byte packets and then playing them back in 20ms intervals (320 bytes/0.02s = 16k audio). Since I'm dealing with 8k audio, I tried changing the audioByteSize to 160 with 20ms intervals however I still hear corrupted audio.

https://github.com/nimbleape/asterisk-dialogflow-rtp-audioserver/blob/5e5d014a7fcfeb526b9640d0963c3cc510d6775d/lib/DialogFlowConnector.js#L162-L191

I saved the audio data as binary file and was able to play it back correctly from sox (rate 8k, 8 bit, single channel, ulaw encoding)

❯ sox -t raw -r 8000 -b 8 -c 1 -e u-law 1649411700451.sln bot-response.wav
❯ play bot-response.wav

bot-response.wav:

 File Size: 24.5k     Bit Rate: 64.2k
  Encoding: u-law
  Channels: 1 @ 14-bit
Samplerate: 8000Hz
Replaygain: off
  Duration: 00:00:03.05

In:100%  00:00:03.05 [00:00:00.00] Out:146k  [      |      ] Hd:0.0 Clip:23
play WARN rate: rate clipped 23 samples; decrease volume?
Done.
vcidst commented 2 years ago

Hey, for anyone debugging this. Two things helped me working with G711 u-law audio,

  1. type variable should be 0. reference
  2. Since the buf.length in my case was 160. I was observing overlapping audio playback, changing that to timestamp += 160 helped for smooth playback.

I'm not precisely working with dialogflow so YMMV.

danjenkins commented 2 years ago

hey @vcidst ! Sorry for no reply over the weekend, I was ill.

I remember having this exact problem with ulaw and never put any real effort into solving it from memory. So you got further than me!

You think we could make this configurable? Would you be interested in doing a PR?

Thanks for following this up on the issue so others can take advantage of it :)

vcidst commented 2 years ago

Hey Dan, hope you're doing well now. I'll share a PR soon with some configuration changes