DEV Community

Tim Dang
Tim Dang

Posted on

1

How to play Streaming Audio?

I have a live call system, and backend receives data from Twilio, with payload is base64 string, sequence of payload and send to me via webSocket. The sound could be played but the quality is not good.
I'm not sure about the reason for it, Internet Connection or my code. I want the quality of sound better. How can i do? Thank!

subject.subscribe(async (msg: any) => {
      if (msg.event === 'media') {
        if ( !this.audioBuffers[msg.streamSid]) {
            this.audioBuffers[msg.streamSid] = {
            timestamp: '',
            phone: '',
            buffer: [],
          };

          tempAudio[msg.streamSid] = {
            timestamp: [],
            payload: [],
            phone: '',

          };

        }
          tempAudio[msg.streamSid].payload[msg.sequenceNumber] =
            await this.audio_ctx.decodeAudioData(
              this.base64ToBuffer(msg.media)
            );
          var buffer = tempAudio[msg.streamSid].payload.filter(function (el) {
            return el != null;
          });
          this.audioBuffers[msg.streamSid].buffer = buffer;
            }      
    });
Enter fullscreen mode Exit fullscreen mode
this.audio_ctx.resume()
 const gainNode = this.audio_ctx.createGain();
while (current < this.data.length) {
        const source = this.audio_ctx.createBufferSource();
        source.buffer = this.data[current];
        gainNode.gain.value = this.isMute ? 0 : this.volume;
        gainNode.connect(this.audio_ctx.destination);
        source.connect(gainNode);
        const will_stop = new Promise((res) => (source.onended = res));

        source.start(0);
        await will_stop;
        current++;

      }
Enter fullscreen mode Exit fullscreen mode

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

nextjs tutorial video

📺 Youtube Tutorial Series

So you built a Next.js app, but you need a clear view of the entire operation flow to be able to identify performance bottlenecks before you launch. But how do you get started? Get the essentials on tracing for Next.js from @nikolovlazar in this video series 👀

Watch the Youtube series

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay