deepgram / deepgram-js-sdk

Official JavaScript SDK for Deepgram's automated speech recognition APIs.
https://developers.deepgram.com
MIT License
127 stars 45 forks source link

Send Audio Data for Live Streaming Audio Transcription but no response. #259

Closed hieuminh65 closed 1 month ago

hieuminh65 commented 3 months ago

System

Bug

Code

LiveAudioStream.on('data', (data) => {
        if (connection && connection.getReadyState() === 1) {
          console.log("Connection established, starting recording..");
          var chunk = Buffer.from(data, 'base64');
          connection.send(chunk);
        }
      });
lukeocodes commented 3 months ago

Our API does not support base64, sorry.

You might try decoding it before sending. Something like this may work? (Untested code)

const bytes = atob(base64);
const byteArrays = [];

for (let i = 0; i < bytes.length; i++) {
    byteArrays.push(bytes.charCodeAt(i));
}

const byteArray = new Uint8Array(byteArrays);
hieuminh65 commented 3 months ago

Hi @lukeocodes,

I was able to send raw audio but still no response. I wonder if you know why:

const startConnection = async () => {
    const deepgram = createClient(process.env.EXPO_PUBLIC_DEEPGRAM_API_KEY);

    const connection = deepgram.listen.live({ model: "nova-2-conversationalai" });

    connection.on(LiveTranscriptionEvents.Open, async () => {
      connection.getReadyState() ? console.log("Connection opened") : console.error("Connection failed to open");
      setButtonRecording("Start");
      LiveAudioStream.on('data', (data) => {
        if (connection && connection.getReadyState() === 1) {
         // convert back to raw audio
          var chunk = Buffer.from(data, 'base64');
          connection.send(chunk);
        }
      });

      await startRecording();
    });

    connection.on(LiveTranscriptionEvents.Close, (event) => {
      console.log("Connection closed", event);
    });

    connection.on(LiveTranscriptionEvents.Transcript, (results) => {
      console.log("Received transcription results", results);
    });

    connection.on(LiveTranscriptionEvents.Metadata, (metadata) => {
      console.log("Received metadata", metadata);
    });

    connection.on(LiveTranscriptionEvents.Error, (error) => {
      console.error("An error occurred", error);
    });

    connection.on(LiveTranscriptionEvents.Warning, (warning) => {
      console.warn("Received a warning", warning);
    });

    return connection;
  };
lukeocodes commented 2 months ago

Can you confirm the data in the buffer is indeed the audio type you're expecting?