@huluvu424242/honey-speech
Version:
Text to Speech component wich is reading texts from DOM elements.
1 lines • 6.92 kB
JavaScript
import{r as t,c as e,h as s,H as i,g as h}from"./p-2d838630.js";class r{constructor(t){r.isLoggingActive=t}static disableLogging(){this.isLoggingActive=!1}static enableLogging(){this.isLoggingActive=!0}static toggleLogging(t){t?r.enableLogging():r.disableLogging()}static logMessage(t){console&&this.isLoggingActive&&console.log(t)}static debugMessage(t){console&&this.isLoggingActive&&console.debug(t)}static errorMessage(t){console&&this.isLoggingActive&&console.error(t)}static infoMessage(t){console&&this.isLoggingActive&&console.info(t)}}r.isLoggingActive=!0;class o{constructor(t,e,s,i,h,o,a,n,c,l){this.onSpeakerStarted=e=>t(e),this.onSpeakerFinished=t=>e(t),this.onSpeakerPaused=t=>s(t),this.onSpeakerResume=t=>i(t),this.onSpeakerFailed=t=>h(t),this.audioLang=o,this.audioPitch=a,this.audioRate=n,this.audioVolume=c,this.voiceName=l,this.stimme=void 0,r.infoMessage("####constructor finished")}getDefaultStimme(){var t,e,s,i=[];const h=o.synthese.getVoices();if(r.infoMessage("Found voices:"+JSON.stringify(h)),!h)return null;for(var a=0;a<h.length;a++)(h[a].name===this.voiceName||h[a].lang===this.audioLang||h[a].default)&&(r.debugMessage("voice matched:"+h[a].name+h[a].lang),h[a].name===this.voiceName&&(t=h[a]),h[a].lang===this.audioLang&&h[a].default&&(e=h[a]),h[a].lang===this.audioLang&&i.push(h[a]),h[a].default&&(s=h[a]));return t||e||(i&&i.length>0?i[0]:s||h[0])}erzeugeVorleser(t){r.infoMessage("erzeugeVorleser started");const e=new SpeechSynthesisUtterance(t);return e.onend=this.onSpeakerFinished,e.onstart=this.onSpeakerStarted,e.onpause=this.onSpeakerPaused,e.onresume=this.onSpeakerResume,e.onerror=this.onSpeakerFailed,e.pitch=this.audioPitch,e.rate=this.audioRate,e.volume=this.audioVolume,e.voice=this.stimme,e.lang=this.audioLang,e}textVorlesen(t){this.stimme||(this.stimme=this.getDefaultStimme(),r.infoMessage("set default voice to "+this.stimme)),t&&t.match(/(\S+\s){1,20}/g).forEach((e=>{const s=this.erzeugeVorleser(e);r.infoMessage("speaker lang used:"+s.lang),s.voice?(r.infoMessage("speaker voice used:"+s.voice.name),r.infoMessage("speaker voice lang:"+s.voice.lang)):r.infoMessage("no voice matched for text: "+t),o.synthese.sprachSynthese.speak(s)}))}cancel(){o.synthese.sprachSynthese.cancel()}}o.synthese=new class{constructor(){this.sprachSynthese=window.speechSynthesis,this.sprachSynthese.onvoiceschanged=()=>{!this.voices||this.voices.length<1?(this.voices=this.sprachSynthese.getVoices(),r.debugMessage("voices changed to: "+this.voices.join(","))):r.debugMessage("voices alraedy initialized")},r.debugMessage("call getVoices()"),this.sprachSynthese.getVoices()}getVoices(){return this.voices}cancel(){this.sprachSynthese.cancel()}};const a=class{constructor(s){t(this,s),this.honeySpeakerStarted=e(this,"honeySpeakerStarted",7),this.honeySpeakerFinished=e(this,"honeySpeakerFinished",7),this.honeySpeakerPaused=e(this,"honeySpeakerPaused",7),this.honeySpeakerResume=e(this,"honeySpeakerResume",7),this.honeySpeakerFailed=e(this,"honeySpeakerFailed",7),this.createAltText=!1,this.createTitleText=!1,this.taborder="0",this.isPressed=!1,this.pure=!1,this.verbose=!1,this.iconwidth="36",this.iconheight="36",this.audiolang="de-DE",this.audiopitch=1,this.audiorate=1,this.audiovolume=1,this.voicename=void 0}connectedCallback(){this.ident=this.hostElement.id?this.hostElement.id:Math.random().toString(36).substring(7),this.createTitleText=!this.hostElement.title,this.createAltText=!this.hostElement.alt,this.taborder=this.hostElement.getAttribute("tabindex")?this.hostElement.tabIndex+"":"0",r.toggleLogging(this.verbose)}componentWillLoad(){this.sprachAusgabe=new o((()=>{this.honeySpeakerStarted.emit(this.ident),this.isPressed=!0,r.debugMessage("Vorlesen gestartet")}),(()=>{this.honeySpeakerFinished.emit(this.ident),this.isPressed=!1,r.debugMessage("Vorlesen beendet")}),(()=>{this.honeySpeakerPaused.emit(this.ident),this.isPressed=!1,r.debugMessage("Pause mit Vorlesen")}),(()=>{this.honeySpeakerResume.emit(this.ident),this.isPressed=!0,r.debugMessage("Fortsetzen mit Vorlesen")}),(t=>{this.honeySpeakerFailed.emit(this.ident),this.isPressed=!1,r.errorMessage("Fehler beim Vorlesen"+JSON.stringify(t))}),this.audiolang,this.audiopitch,this.audiorate,this.audiovolume,this.voicename)}createNewTitleText(){return this.isPressed?"Liest gerade vor":"Vorlesen"}getTitleText(){return this.createTitleText?this.createNewTitleText():this.hostElement.title}createNewAltText(){return this.isPressed?"Symbol eines stummen Lautsprechers":"Symbol eines tönenden Lautsprechers"}getAltText(){return this.createAltText?this.createNewAltText():this.hostElement.getAttribute("alt")}getTexte(){if(this.textids){const t=this.textids.split(","),e=[];return t.forEach((t=>{const s=document.getElementById(t);s?e.push(s.innerText):r.errorMessage("text to speak not found of DOM element with id "+t)})),e}return["Kein Text vorhanden, daher keine Ausgabe möglich."]}async textVorlesen(t){this.isPressed=!0,await this.sprachAusgabe.textVorlesen(t+" ")}toggleAction(){r.debugMessage("###TOGGLE TO"+this.isPressed),this.isPressed=!this.isPressed,this.isPressed?this.getTexte().forEach((async t=>{this.textVorlesen(t)})):this.sprachAusgabe.cancel()}onClick(){this.toggleAction()}onKeyDown(t){"Enter"!==t.key&&" "!==t.key||(t.preventDefault(),this.toggleAction())}render(){return r.debugMessage("##RENDER##"),s(i,{title:this.getTitleText(),alt:this.getAltText(),role:"button",tabindex:this.taborder,"aria-pressed":this.isPressed?"true":"false"},s("svg",{id:this.ident+"-svg",xmlns:"http://www.w3.org/2000/svg",width:this.iconwidth,height:this.iconheight,role:"img","aria-label":this.getAltText(),class:"speakerimage",viewBox:"0 0 75 75"},s("path",{"stroke-width":"5","stroke-linejoin":"round",d:"M39.389,13.769 L22.235,28.606 L6,28.606 L6,47.699 L21.989,47.699 L39.389,62.75 L39.389,13.769z"}),this.isPressed?s("path",{id:"air",stroke:"var(--honey-speaker-color,black);",fill:"none","stroke-width":"5","stroke-linecap":"round",d:"M48,27.6a19.5,19.5 0 0 1 0,21.4M55.1,20.5a30,30 0 0 1 0,35.6M61.6,14a38.8,38.8 0 0 1 0,48.6"},s("animate",{id:"airanimation",attributeType:"CSS",attributeName:"opacity",from:"1",to:"0",dur:"1s",repeatCount:"indefinite"})):this.pure?s("text",{id:"eins",x:"60%",y:"55%"},"OFF"):s("path",{id:"air",stroke:"var(--honey-speaker-color,black);",fill:"none","stroke-width":"5","stroke-linecap":"round",d:"M48,27.6a19.5,19.5 0 0 1 0,21.4M55.1,20.5a30,30 0 0 1 0,35.6M61.6,14a38.8,38.8 0 0 1 0,48.6"})))}static get assetsDirs(){return["assets"]}get hostElement(){return h(this)}};a.style=".speakerimage{background:var(--honey-speaker-background, transparent);stroke:var(--honey-speaker-color, blue);fill:var(--honey-speaker-color, blue);padding:var(--honey-speaker-padding, 5px);font-size:var(--honey-speaker-font-size, medium);border:var(--honey-speaker-border, 0);width:var(--honey-speaker-width, 36px);height:var(--honey-speaker-height, 36px)}";export{a as honey_speech}