92 lines
2.5 KiB
JavaScript
92 lines
2.5 KiB
JavaScript
const WebSocket = require('ws');
|
|
const wss = new WebSocket.Server({ port: 8081 });
|
|
console.log('WebSocket server started on port 8081');
|
|
|
|
|
|
|
|
//load .env file
|
|
if (require('dotenv')) {
|
|
require('dotenv').config()
|
|
}
|
|
|
|
console.log(process.env)
|
|
console.log(process.env.TTS_BACKEND_URL)
|
|
console.log(process.env.WS_URL)
|
|
|
|
//we use https://hub.docker.com/r/onerahmet/openai-whisper-asr-webservice to transcribe the audio
|
|
//docker run -p 9009:9009 -d onerahmet/openai-whisper-asr-webservice
|
|
|
|
wss.on('connection', (ws) => {
|
|
console.log('Client ' + ws._socket.remoteAddress + ' connected');
|
|
ws.on('message', (data) => {
|
|
//show the size of the audio data as 0.000 MB
|
|
console.log('Received data from client: ' + (data.length / 1024 / 1024).toFixed(3) + ' MB');
|
|
var request = require('request');
|
|
var formData = {
|
|
task: 'transcribe',
|
|
language: 'en-US', //bg-BG|en-US
|
|
output: 'json',
|
|
audio_file: {
|
|
value: data,
|
|
options: {
|
|
filename: 'audio.ogg',
|
|
contentType: 'audio/ogg'
|
|
}
|
|
}
|
|
};
|
|
|
|
|
|
var timestampfilename = Date.now("yyyymmdd-hhMMss");
|
|
|
|
//save the audio data to a file to /rec subfolder
|
|
var fs = require('fs');
|
|
fs.mkdir('rec', { recursive: true }, (err) => {
|
|
if (err) throw err;
|
|
});
|
|
|
|
fs.writeFile('rec/audio' + timestampfilename + '.ogg', data, function (err) {
|
|
if (err) {
|
|
return console.log(err);
|
|
}
|
|
console.log('Audio data saved to audio.ogg');
|
|
});
|
|
|
|
|
|
|
|
request.post({ url: process.env.TTS_BACKEND_URL, formData: formData }, function optionalCallback(err, httpResponse, body) {
|
|
if (err) {
|
|
return console.error('upload failed:', err);
|
|
}
|
|
console.log('Whisper decoded:', body);
|
|
ws.send(body);
|
|
});
|
|
});
|
|
});
|
|
|
|
function transcribeAudio(audioData) {
|
|
// Use a speech-to-text library to transcribe the audio data
|
|
//return transcription;
|
|
return "TEST";
|
|
}
|
|
|
|
|
|
// --- web server that servers client.html
|
|
const express = require('express');
|
|
const app = express();
|
|
const path = require('path');
|
|
|
|
app.get('/', (req, res) => {
|
|
res.sendFile(path.join(__dirname, 'client.html'));
|
|
});
|
|
|
|
|
|
//get WS url from .env file
|
|
app.get('/wsurl', (req, res) => {
|
|
res.send(process.env.WS_URL, 200, { 'Content-Type': 'text/plain' });
|
|
});
|
|
|
|
|
|
app.listen(8080, () => {
|
|
console.log('Server listening on port 8080');
|
|
});
|