Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
2.1 kB
1
Indexable
Never
import {AssemblyAI, RealtimeTranscriber} from "assemblyai";
import {emit} from "../ipc/ipcApi";
import {ipcMain} from "electron";

export namespace sttApi {
  let realtimeTranscriber: RealtimeTranscriber;

  export const subscribeToIpc = () => {
    console.log('[IPC][STT] init ipc handlers');
    ipcMain.handle('stt:init',async ()=> {
      console.log('[IPC][STT] stt:init')
      await init();
    })
    ipcMain.handle('stt:audio', (event, audio ) => {
      console.log('[IPC][STT] stt:audio');
      processSttAudio(audio);
    })
  }

  const init = async () => {
    try{
      const assemblyAI = new AssemblyAI({
        apiKey: process.env.ASSEMBLY_AI_KEY ?? '',
      });
      const token = await assemblyAI.realtime.createTemporaryToken({
        expires_in: 3600,
      });
      console.log(
        '[STT]: initializing realtime audio service with token: ',
        token,
      );
      realtimeTranscriber = assemblyAI.realtime.transcriber({
        token,
        wordBoost: ['vatsim', 'vfr', 'ifr', 'atc', 'atis'],
      });
      console.log(
        '[STT]: transcriber created',
      );

      void realtimeTranscriber.on('transcript.final', (message) => {
        emit('stt:text', message.text);
      });

      void realtimeTranscriber.on('error', (error) => {
        emit('stt:error', error);
      });

      void realtimeTranscriber.on('close', (event) => {
        emit('stt:close', event);
      });

      console.log(
        '[STT]: handlers added',
      );

      await realtimeTranscriber.connect();

      console.log(
        '[STT]: service connected',
      );

      setInterval(async () => {
        // send empty buffer to keep connection alive
        realtimeTranscriber?.sendAudio(new ArrayBuffer(0));
      }, 800);
    } catch (e) {
      console.error('[STT]: error initializing stt', e);
      return false;
    }
    return true;
  }
  const processSttAudio = (audio: ArrayBuffer) => {
    if(realtimeTranscriber){
      realtimeTranscriber.sendAudio(audio);
    }
  }
}

Leave a Comment