Skip to content

Commit

Permalink
support speechmatics region authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
xquanluu authored and davehorton committed Oct 11, 2024
1 parent 02ee5ef commit 26d8287
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 19 deletions.
4 changes: 3 additions & 1 deletion lib/routes/api/speech-credentials.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ const encryptCredential = (obj) => {
secret,
nuance_tts_uri,
nuance_stt_uri,
speechmatics_stt_uri,
deepgram_stt_uri,
deepgram_stt_use_tls,
deepgram_tts_uri,
Expand Down Expand Up @@ -239,7 +240,8 @@ const encryptCredential = (obj) => {

case 'speechmatics':
assert(api_key, 'invalid speechmatics speech credential: api_key is required');
const speechmaticsData = JSON.stringify({api_key, options});
assert(speechmatics_stt_uri, 'invalid speechmatics speech credential: speechmatics_stt_uri is required');
const speechmaticsData = JSON.stringify({api_key, speechmatics_stt_uri, options});
return encrypt(speechmaticsData);

case 'playht':
Expand Down
68 changes: 50 additions & 18 deletions lib/utils/speech-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const bent = require('bent');
const fs = require('fs');
const { AssemblyAI } = require('assemblyai');
const {decrypt, obscureKey} = require('./encrypt-decrypt');
const { Speechmatics } = require('speechmatics');
const { RealtimeSession } = require('speechmatics');

const TtsGoogleLanguagesVoices = require('./speech-data/tts-google');
const TtsAwsLanguagesVoices = require('./speech-data/tts-aws');
Expand Down Expand Up @@ -56,25 +56,53 @@ const testSonioxStt = async(logger, credentials) => {
};

const testSpeechmaticsStt = async(logger, credentials) => {
const {api_key} = credentials;
const sm = new Speechmatics(api_key);

const {api_key, speechmatics_stt_uri} = credentials;
return new Promise(async(resolve, reject) => {
try {
const inputFile = new Blob([
fs.readFileSync(`${__dirname}/../../data/test_audio.wav`),
]);
const result = await sm.batch.transcribe({
data: inputFile,
fileName: 'test_audio.wav'
},
{
transcription_config: {
language: 'en'
}},
'text');
if (result.length > 0) resolve(result);
else reject(new Error('no transcript returned'));
const session = new RealtimeSession({ apiKey: api_key, realtimeUrl: speechmatics_stt_uri });
let transcription = '';
session.addListener('Error', (error) => {
reject(error);
});

session.addListener('AddTranscript', (message) => {
transcription += message.metadata.transcript;
});

session.addListener('EndOfTranscript', () => {
resolve(transcription);
});

session
.start({
transcription_config: {
language: 'en',
operating_point: 'enhanced',
enable_partials: true,
max_delay: 2,
},
audio_format: { type: 'file' },
})
.then(() => {
//prepare file stream
const fileStream = fs.createReadStream(`${__dirname}/../../data/test_audio.wav`);

//send it
fileStream.on('data', (sample) => {
session.sendAudio(sample);
});

//end the session
fileStream.on('end', () => {
session.stop();
});

return;

})
.catch((error) => {
reject(error);
});
} catch (error) {
logger.info({error}, 'failed to get speechmatics transcript');
reject(error);
Expand Down Expand Up @@ -560,6 +588,10 @@ function decryptCredential(obj, credential, logger, isObscureKey = true) {
} else if ('soniox' === obj.vendor) {
const o = JSON.parse(decrypt(credential));
obj.api_key = isObscureKey ? obscureKey(o.api_key) : o.api_key;
} else if ('speechmatics' === obj.vendor) {
const o = JSON.parse(decrypt(credential));
obj.api_key = isObscureKey ? obscureKey(o.api_key) : o.api_key;
obj.speechmatics_stt_uri = o.speechmatics_stt_uri;
} else if ('elevenlabs' === obj.vendor) {
const o = JSON.parse(decrypt(credential));
obj.api_key = isObscureKey ? obscureKey(o.api_key) : o.api_key;
Expand Down

0 comments on commit 26d8287

Please sign in to comment.