dotenv if exists;

auto create rec folder;
select audio device on client;
This commit is contained in:
Dobromir Popov 2023-03-07 11:41:50 +02:00
parent 2f56d76042
commit 6429852505
2 changed files with 75 additions and 14 deletions

View File

@ -18,6 +18,10 @@
id="autosend" />
<span class="slider">Continious</span>
</label>
<select id="input-devices">
<option value="default">Default</option>
</select>
<button id="record-button"
disabled>Start Recording</button>
<span id="connection-status"></span>
@ -29,6 +33,7 @@
width="500"
height="500"></canvas>
<script>
let selectedDeviceId = "default";
let socket;
let audioRecorder;
let recording = false;
@ -93,7 +98,7 @@
// count speaking and silence
if (averageVolume > threshold) {
if (speakingCount == 0 && audioRecorder) {
if (autosend.checked && speakingCount == 0 && audioRecorder) {
console.log("startint new recording");
audioRecorder.stop();
audioRecorder.start();
@ -249,6 +254,57 @@
}
}
function enumerateDevices() {
// Enumerate the available audio input devices
navigator.mediaDevices.enumerateDevices()
.then(function (devices) {
var audioInputDevices = devices.filter(function (device) {
return device.kind === 'audioinput';
});
console.log(audioInputDevices.length + ' audio input devices found');
// If more than one audio input device is available, populate the select list
if (audioInputDevices.length > 1) {
audioInputDevices.forEach(function (device) {
var option = document.createElement('option');
option.value = device.deviceId;
option.text = device.label || 'Device ' + device.deviceId;
inputDevices.appendChild(option);
});
// Listen for changes to the select list and connect to the selected audio input device
inputDevices.addEventListener('change', function (event) {
var selectedDeviceId = event.target.value;
var constraints = { audio: { deviceId: selectedDeviceId } };
navigator.mediaDevices.getUserMedia(constraints)
.then(function (stream) {
// Handle the audio stream from the selected device here
})
.catch(function (error) {
console.log('Error accessing audio stream:', error);
});
});
}
// If only one audio input device is available, connect to it automatically
else if (audioInputDevices.length === 1) {
var constraints = { audio: { deviceId: audioInputDevices[0].deviceId } };
navigator.mediaDevices.getUserMedia(constraints)
.then(function (stream) {
// Handle the audio stream from the selected device here
})
.catch(function (error) {
console.log('Error accessing audio stream:', error);
});
}
// If no audio input devices are available, show an error message
else {
console.log('No audio input devices available');
}
})
.catch(function (error) {
console.log('Error listing audio input devices:', error);
});
}
window.onload = () => {
recordButton = document.getElementById("record-button");
recordButton.addEventListener("click", toggleListening);
@ -256,9 +312,8 @@
//transcription = document.getElementById("transcription");
//autosend = document.getElementById("autosend");
statusRecording = document.getElementById("status-recording");
enumerateDevices();
connect(socket);
};
</script>

View File

@ -4,8 +4,10 @@ console.log('WebSocket server started on port 8081');
//load TTS_BACHEND_URL from .env file
//require('dotenv').config();
//load .env file
if (require('dotenv')) {
require('dotenv').config()
}
console.log(process.env)
console.log(process.env.TTS_BACKEND_URL)
@ -32,13 +34,17 @@ wss.on('connection', (ws) => {
}
}
};
//save the audio data to a file to /rec folder
var fs = require('fs');
var timestampfilename = Date.now();
fs.writeFile('./rec/audio' + timestampfilename + '.ogg', data, function (err) {
//fs.writeFile('audio' + timestampfilename + '.ogg', data, function (err) {
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);
}
@ -47,7 +53,7 @@ wss.on('connection', (ws) => {
request.post({url:'http://192.168.0.10:9008/asr', formData: formData}, function optionalCallback(err, httpResponse, body) {
request.post({ url: process.env.TTS_BACKEND_URL, formData: formData }, function optionalCallback(err, httpResponse, body) {
if (err) {
return console.error('upload failed:', err);
}
@ -76,7 +82,7 @@ app.get('/', (req, res) => {
//get WS url from .env file
app.get('/wsurl', (req, res) => {
res.send(process.env.WS_URL, 200, {'Content-Type': 'text/plain'});
res.send(process.env.WS_URL, 200, { 'Content-Type': 'text/plain' });
});