thedesk/app/js/tl/speech.js

108 lines
3.5 KiB
JavaScript
Raw Normal View History

2019-05-19 17:39:30 +10:00
$voise = null;
$voiseName = lang.lang_speech;
$voices = speechSynthesis.getVoices();
$synthes = new SpeechSynthesisUtterance();
$voise = $.grep($voices, function (n, i) { return n.name == $voiseName })[0];
$synthes.voice = $voise; // 音声の設定
localStorage.removeItem("voicebank");
speechSynthesis.cancel()
if (!localStorage.getItem("voice_vol")) {
localStorage.setItem("voice_vol", 1)
}
$synthes.rate = localStorage.getItem("voice_speed");
$synthes.pitch = localStorage.getItem("voice_pitch");
$synthes.volume = localStorage.getItem("voice_vol");
function say(msgr) {
msg = voiceParse(msgr);
var voice = localStorage.getItem("voicebank");
2018-07-22 23:03:46 +10:00
var obj = JSON.parse(voice);
2019-05-19 17:39:30 +10:00
if (!obj) {
var json = JSON.stringify([msg]);
localStorage.setItem("voicebank", json);
} else {
obj.push([msg]);
var json = JSON.stringify(obj);
localStorage.setItem("voicebank", json);
}
2018-07-22 23:03:46 +10:00
}
2019-05-19 17:39:30 +10:00
$repeat = setInterval(function () {
if (!speechSynthesis.speaking) {
var voice = localStorage.getItem("voicebank");
if (voice) {
2018-07-22 23:03:46 +10:00
var obj = JSON.parse(voice);
2019-05-19 17:39:30 +10:00
if (obj[0]) {
$synthes.text = obj[0];
2018-07-22 23:03:46 +10:00
speechSynthesis.speak($synthes);
obj.splice(0, 1);
var json = JSON.stringify(obj);
localStorage.setItem("voicebank", json);
}
}
}
}, 300);
2019-05-19 17:39:30 +10:00
function voiceParse(msg) {
2018-09-17 21:55:00 +10:00
msg = $.strip_tags(msg);
2018-07-22 23:03:46 +10:00
msg = msg.replace(/#/g, "");
msg = msg.replace(/'/g, "");
msg = msg.replace(/"/g, "");
2018-09-17 21:55:00 +10:00
msg = msg.replace(/https?:\/\/[a-zA-Z0-9./-@_=?%&-]+/g, "");
2019-05-19 17:39:30 +10:00
return msg;
2018-07-22 23:03:46 +10:00
}
function voiceToggle(tlid) {
2019-05-19 17:39:30 +10:00
var voiceck = localStorage.getItem("voice_" + tlid);
if (voiceck) {
2018-07-22 23:03:46 +10:00
localStorage.removeItem("voice_" + tlid);
speechSynthesis.cancel()
2019-05-19 17:39:30 +10:00
$("#sta-voice-" + tlid).text("Off");
$("#sta-voice-" + tlid).css("color", 'red');
parseColumn();
} else {
localStorage.setItem("voice_" + tlid, "true");
$("#sta-voice-" + tlid).text("On");
$("#sta-voice-" + tlid).css("color", '#009688');
2018-07-22 23:03:46 +10:00
parseColumn();
2019-05-19 17:39:30 +10:00
}
2018-07-22 23:03:46 +10:00
}
function voiceCheck(tlid) {
var voiceck = localStorage.getItem("voice_" + tlid);
2019-05-19 17:39:30 +10:00
if (voiceck) {
$("#sta-voice-" + tlid).text("On");
$("#sta-voice-" + tlid).css("color", '#009688');
} else {
$("#sta-voice-" + tlid).text("Off");
$("#sta-voice-" + tlid).css("color", 'red');
}
2018-07-22 23:03:46 +10:00
}
2019-05-19 17:39:30 +10:00
function voicePlay() {
if (speechSynthesis.speaking) {
2018-07-22 23:03:46 +10:00
speechSynthesis.cancel()
2019-05-19 17:39:30 +10:00
} else {
$synthes.text = $("#voicetxt").val();
$synthes.rate = $("#voicespeed").val() / 10;
$synthes.pitch = $("#voicepitch").val() / 50;
$synthes.volume = $("#voicevol").val() / 100;
2018-07-22 23:03:46 +10:00
speechSynthesis.speak($synthes);
}
}
2019-05-19 17:39:30 +10:00
function voiceSettings() {
localStorage.setItem("voice_speed", $("#voicespeed").val() / 10);
localStorage.setItem("voice_pitch", $("#voicepitch").val() / 50);
localStorage.setItem("voice_vol", $("#voicevol").val() / 100);
2019-01-26 14:24:26 +11:00
Materialize.toast(lang.lang_speech_refresh, 3000);
2018-07-22 23:03:46 +10:00
}
2019-05-19 17:39:30 +10:00
function voiceSettingLoad() {
var speed = localStorage.getItem("voice_speed");
var pitch = localStorage.getItem("voice_pitch");
var vol = localStorage.getItem("voice_vol");
if (speed) {
$("#voicespeed").val(speed * 10);
2018-07-22 23:03:46 +10:00
}
2019-05-19 17:39:30 +10:00
if (pitch) {
$("#voicepitch").val(pitch * 50);
2018-07-22 23:03:46 +10:00
}
2019-05-19 17:39:30 +10:00
if (vol) {
$("#voicevol").val(vol * 100);
2018-07-22 23:03:46 +10:00
}
}