xrsh-buildroot/buildroot-v86/board/v86/rootfs_overlay/root/bin/say

58 lines
1.7 KiB
Plaintext
Raw Normal View History

#!/bin/js
window.speak = async function(str, opts){
if( !str) return
let defaultOpts
let term = document.querySelector("[isoterminal]").components.isoterminal.term
try{
let buf = await term.worker.read_file("root/.config/webspeech/settings.json")
let jsonstr = new TextDecoder().decode(buf);
defaultOpts = JSON.parse(jsonstr)
}catch(e){
defaultOpts = {
speaksigns:true,
override:true,
speak_rate: 1,
speak_pitch: 0.01,
speak_volume: 1,
speak_voice: -1,
speak_voices: 0
}
}
opts = { ...defaultOpts, ...opts, speak: {} }
if( opts.speaksigns ){
str = str.replace(/\/\//,' ')
.replace(/:/,'')
.replace(/\//,' slash ')
.replace(/\./,' dot ')
.replace(/#/,' hash ')
.replace(/&/,' and ')
.replace(/=/,' is ')
}
if( str == opts.speak.lastStr ) return // no duplicates
opts.speak.lastStr = str
let speech = window.speechSynthesis
let utterance = new SpeechSynthesisUtterance( str )
opts.speak_voices = speech.getVoices().length
if( opts.speak_voice != -1 && opts.speak_voice < opts.speak_voices ){
utterance.voice = speech.getVoices()[ opts.speak_voice ];
}else{
let voices = speech.getVoices()
for(let i = 0; i < voices.length; i++ ){
if( voices[i].lang == navigator.lang ) opts.speak_voice = i;
}
}
utterance.rate = opts.speak_rate
utterance.pitch = opts.speak_pitch
utterance.volume = opts.speak_volume
if( opts.override ) speech.cancel()
speech.speak(utterance)
}
window.speak( args.slice(1).join(' ') )
return ""