keolis-services
Version:
Keolis services includes Microphone, Location, Voice Activity Detector, Heart rate through web bluetooth API, text to speech, stop watch with our own cloud.
175 lines (149 loc) • 5.91 kB
JavaScript
const $ = require('jquery');
import Artyom from 'artyom.js';
const MicRecorder = require('mic-recorder-to-mp3');
const { api_url } = require('../variables/constants');
// Artyom Configuration
let VoiceActivationTimeout;
let IsVoiceActivated = false;
const artyom = new Artyom();
const recorder = new MicRecorder({
bitRate: 128,
encoder: 'mp3',
sampleRate: 44100
});
const myGroup = [{
//smart:true,
indexes: ["hi", "hey", "hello", "I hate this song", "I love this song", "i love this", "i hate this"],
action: function(i) { // var i returns the index of the recognized command in the previous array
if (i <= 2) {
IsVoiceActivated = true;
if (VoiceActivationTimeout) clearInterval(VoiceActivationTimeout);
startRecording();
VoiceActivationTimeout = setTimeout(() => {
IsVoiceActivated = false;
//artyom.say("you didn't say anything.");
stopRecording();
}, 7000);
startContinuousArtyom();
} else {
if (IsVoiceActivated) {
IsVoiceActivated = false;
$(".tabToRecord").show();
clearInterval(VoiceActivationTimeout);
stopRecording();
}
}
}
}];
artyom.addCommands(myGroup);
export const startContinuousArtyom = () => {
artyom.fatality(); // use this to stop any of
setTimeout(function() { // if you use artyom.fatality , wait 250 ms to initialize again.
artyom.initialize({
lang: "en-GB", // A lot of languages are supported. Read the docs !
continuous: false, // Artyom will listen forever
listen: true, // Start recognizing
debug: false, // Show everything in the console
speed: 1, // talk normally
voice: ['Google US English', 'Alex'],
}).then(function() {
console.log("TextToSpeech ready to work!");
});
}, 250);
}
function SendTheRecordToCloud(blob) {
global.isUploading = true;
var form = new FormData();
form.append("Response_Voice_Wav", blob, "input.wav");
form.append("Beats_Per_Minute", global.RealtimeHeartRate);
form.append("Time_Taken_To_Response", "1");
form.append("Interaction_Session", localStorage.getItem("interaction_session"));
form.append("IsTesting", false);
const payloadArray = [];
var recurringUser = localStorage.getItem("user_recurring");
payloadArray.push({ "name": "RecurringUser", "value": recurringUser });
payloadArray.push({ "name": "spotify_playlist", "value": localStorage.getItem("spotify_playlistid") });
payloadArray.push({ "name": "spotify_user", "value": localStorage.getItem("spotify_userid") });
payloadArray.push({ "name": "spotify_top10songs", "value": localStorage.getItem("spotify_top10songs") ? JSON.stringify(localStorage.getItem("spotify_top10songs").split(',')) : null });
payloadArray.push({ "name": "spotify_currentPlayingUri", "value": getCurrentPlayingSong() });
form.append("JsonPayload", JSON.stringify(payloadArray));
var settings = {
"url": api_url + "/api/Interaction/Interact",
"method": "POST",
"timeout": 0,
"processData": false,
"mimeType": "multipart/form-data",
"contentType": false,
"data": form,
"dataType": "json",
"beforeSend": function() {
$(".tabToRecord").hide();
},
"error": function(data) {
TextToSpeach("I didn't hear that can you repeat");
$(".tabToRecord").show();
}
};
$.ajax(settings).done(function(response) {
if (response.result.stage >= 13) {
// Start listening. You can call this here, or attach this call to an event, button, etc.
global.PassiveModeOn = true;
}
if (!response.result.statement) {
TextToSpeach("I didn't hear that can you repeat");
$(".tabToRecord").show();
} else {
TextToSpeach(response.result.statement);
}
});
}
export function startRecording() {
return new Promise((resolve, reject) => {
global.isRecording = true;
recorder.start().then(() => {
resolve({ success: true, started: true, stopped: false });
}).catch((e) => {
resolve({ success: false, message: e.message });
});
});
}
export function stopRecording() {
if (global.isRecording) {
return new Promise((resolve, reject) => {
global.isRecording = false;
$(".tabToRecord").css("opacity", "1");
recorder.stop().getMp3().then(([buffer, blob]) => {
$(".audioWrapper .loader").removeClass("listening").addClass("idle");
if (localStorage.getItem('spotify_userid')) SendTheRecordToCloud(blob);
resolve({ success: true, started: false, stopped: true, file: blob })
}).catch((e) => {
resolve({ success: false, message: e.message })
});
})
} else {
return { success: false, message: 'Recording not started' }
}
}
// Text to speech
const onPlaying = () => {
global.isTalking = true;
global.isUploading = true;
}
const onStop = () => {
global.isTalking = false;
if (global.PassiveModeOn) {
navigator.permissions.query({ name: 'microphone' }).then(function(permissionStatus) {
startContinuousArtyom();
});
}
}
export const TextToSpeach = (Text) => {
artyom.say(Text, {
onStart: function() {
onPlaying();
},
onEnd: function() {
onStop();
}
});
}