fmode-ng 0.0.44 → 0.0.45

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,6 +5,6 @@
5
5
  * 保留所有权利 All Rights Reserved.
6
6
  * /home/ryan/workspace/nova/nova-admin/dist/fmode-ng/esm2022/lib/aigc/voice/audio.player.mjs
7
7
  */
8
- export class AudioPlayer{static{this.instance=null}constructor(){this.audio=new Audio}static getInstance(){return AudioPlayer.instance||(AudioPlayer.instance=new AudioPlayer),AudioPlayer.instance}setAudioEvent(t,i){this.audio[t]=i}get duration(){return this.audio.duration}get src(){return this.audio.src}play(t){return new Promise(((i,a)=>{this.audio.src!==t?(this.audio.src=t,this.audio.play().then((()=>{i()})).catch((()=>{a()}))):this.audio.paused?this.audio.play().then((()=>{i()})).catch((()=>{a()})):this.audio.pause()}))}}
8
+ export class AudioPlayer{static{this.instance=null}constructor(){this.audio=new Audio}static getInstance(){return AudioPlayer.instance||(AudioPlayer.instance=new AudioPlayer),AudioPlayer.instance}setAudioEvent(t,i){this.audio[t]=i}get duration(){return this.audio.duration}get src(){return this.audio.src}async play(t){return new Promise(((i,a)=>{this.audio.src!==t?(this.audio.src=t,this.audio.play().then((()=>{i()})).catch((()=>{a()}))):this.audio.paused?this.audio.play().then((()=>{i()})).catch((()=>{a()})):this.audio.pause()}))}}
9
9
  var MODULE_PATH_NEED = `6K+l5paH5Lu25piv5pys6aG555uu55qE5LiA6YOo5YiGIFRoaXMgZmlsZSBpcyBwYXJ0IG9mIHRoZSBDb21wb25lbnRzIGluIEZtb2RlIEluYy4KICAgIOeJiOadg+aJgOaciSDCqSDmnKrmnaXpo57pqawgwqkg5rGf6KW/6ISR5o6n56eR5oqA5pyJ6ZmQ5YWs5Y+4IENvcHlyaWdodCDCqSBGbW9kZSBUZWNobm9sb2d5IENvLiwgTHRkLgogICAg5L+d55WZ5omA5pyJ5p2D5YipIEFsbCBSaWdodHMgUmVzZXJ2ZWQuCiAgICDkuKXnpoHlnKjmnKrnu4/mjojmnYPnmoTmg4XlhrXkuIvvvIzpgJrov4fku7vkvZXlqpLku4vlpI3liLbmraTmlofku7YgVW5hdXRob3JpemVkIGNvcHlpbmcgb2YgdGhpcyBmaWxlLCB2aWEgYW55IG1lZGl1bSBpcyBzdHJpY3RseSBwcm9oaWJpdGVkCiAgICDor6Xmlofku7bmmK/kuJPmnInnmoTmnLrlr4bmlofku7YgUHJvcHJpZXRhcnkgYW5kIGNvbmZpZGVudGlhbAogICAKICAgIENvcHlyaWdodCAyMDIxLW5vdyBGbW9kZSBJbmMuIHN1cHBvcnRAZm1vZGUuY24uIDE4NjA3MDA3MDczLgogICAg5L+d55WZ5omA5pyJ5p2D5YipIEFsbCByaWdodHMgcmVzZXJ2ZWQuCgogICAgUEFUSDovaG9tZS9yeWFuL3dvcmtzcGFjZS9ub3ZhL25vdmEtYWRtaW4vZGlzdC9mbW9kZS1uZy9lc20yMDIyL2xpYi9haWdjL3ZvaWNlL2F1ZGlvLnBsYXllci5tanM=`
10
10
 
@@ -5,6 +5,6 @@
5
5
  * 保留所有权利 All Rights Reserved.
6
6
  * /home/ryan/workspace/nova/nova-admin/dist/fmode-ng/esm2022/lib/aigc/voice/fmode-voice.service.mjs
7
7
  */
8
- import{Injectable}from"@angular/core";import Recorder from"recorder-core";import"recorder-core/src/engine/pcm";import"recorder-core/src/engine/wav";import"recorder-core/src/extensions/waveview";import CryptoJS from"crypto-js";import{pcmtoWav}from"./lib/pcm2wav";import{convertFrameBufferToBase64,resampleBuffer}from"./lib/resample";import{WebSpeech}from"./class-asr";import{Platform}from"@ionic/angular";import{Diagnostic}from"@awesome-cordova-plugins/diagnostic/ngx";import*as i0 from"@angular/core";import*as i1 from"@ionic/angular";import*as i2 from"@awesome-cordova-plugins/diagnostic/ngx";export class FmodeVoiceService{constructor(t,e){this.platform=t,this.diagnostic=e,this.webSpeech=WebSpeech,this.isUserFinish=!1,this.recordWavBlob=null,this.recordPcmBlob=null,this.recordDuration=0,this.recordType="pcm",this.encodingType="raw",this.connStatus="",this.btnStatus="UNDEFINED",this.resultText="",this.resultTextTemp="",this.APPID="50f4a46c",this.API_SECRET="NzFlNmFhZDJjMDNkZGM3NzI0Mzg2OGNm",this.API_KEY="106ddc40dfd4b9ca6d7b47c70fada749",this.durationStr="00:00",this.duration=0,this.requestPermission()}toggleRecord(){console.log(this.btnStatus),"UNDEFINED"===this.btnStatus||"CLOSED"===this.btnStatus?this.startTalk():"CONNECTING"!==this.btnStatus&&"OPEN"!==this.btnStatus||this.finishTalk()}finishTalk(){this.isUserFinish=!0,this.onBeforeFinishTalk&&this.onBeforeFinishTalk(),this.recordStop()}async startTalk(t){this.resultText="",this.resultTextTemp="",this.onBeforeStartTalk&&this.onBeforeStartTalk(),event?.preventDefault(),await this.openWithPriviledge(),setTimeout((()=>{this.connectWebSocket()}),100),this.startCountdown(),this.onAfterStartTalk&&this.onAfterStartTalk()}cancelTalk(){this.onBeforeCancelTalk&&this.onBeforeCancelTalk(),this.recordStop(),this.iatWS?.close(),this.resultText=null,this.onAfterCancelTalk&&this.onAfterCancelTalk()}async recordStart(){this.createRecorder(),await this.openWithPriviledge(),this.recorder.start(),this.changeBtnStatus("OPEN"),this.onAfterRecordStart&&this.onAfterRecordStart()}recordStop(){return new Promise((t=>{clearInterval(this.countdownInterval),this.changeBtnStatus("CLOSED"),this.recorder?.stop((async(e,i)=>{try{this.iatWS.send(JSON.stringify({data:{status:2,format:"audio/L16;rate=16000",encoding:this.encodingType,audio:""}}))}catch(t){}let o=(window.URL||webkitURL).createObjectURL(e);console.log(e,o,"时长:"+i+"ms"),this.recordPcmBlob=e,this.recordWavBlob=await this.pcmBlobToWavBlob(e,44100),this.recordDuration=i,console.log("this.recordWavBlob",this.recordWavBlob),setTimeout((()=>{this.isUserFinish&&(this.onAfterFinishTalk&&this.onAfterFinishTalk(),this.isUserFinish=!1)}),2e3),this.recorder?.close(),this.recorder=null,console.log("localUrl",o),t(!0)}),(e=>{console.log("录音失败:"+e),this.recorder.close(),this.recorder=null,t(null)}))}))}playRecord(){this.playPCM(this.recordPcmBlob,44100)}async pcmBlobToWavBlob(t,e){return new Promise((i=>{let o=new FileReader;o.onload=function(t){let o=t.target.result,s=pcmtoWav(o,e,1,16);i(s)},o.readAsArrayBuffer(t)}))}async playPCM(t,e){let i=await this.pcmBlobToWavBlob(t,e),o=window.URL.createObjectURL(i),s=new Audio;s.src=o,s.play()}async playBuffers(){let t=await this.BuffersToBlob(this.buffers);this.playPCM(t,44100)}BuffersToBlob(t){let e=[];return t.forEach((t=>{t.forEach((t=>{e.push(t)}))})),new Blob([e],{type:"audio/pcm"})}splitAudioData(t){const e=1280,i=Math.ceil(t.length/e),o=[];for(let s=0;s<i;s++){const i=s*e,r=i+e,a=t.slice(i,r);o.push(a)}return o}BufferToBlob(t){return new Blob([t],{type:"audio/pcm"})}createRecorder(){this.recorder||(this.recorder=Recorder({type:this.recordType,sampleRate:44100,bitRate:16,onProcess:(t,e,i,o,s,r)=>{let a=t.length&&t[t.length-1];if(this.buffers=t,a=resampleBuffer(a,44100,16e3),this.iatWS.readyState===this.iatWS.OPEN){if(this.disableASR)return;this.iatWS.send(JSON.stringify({data:{status:1,format:"audio/L16;rate=16000",encoding:this.encodingType,audio:convertFrameBufferToBase64(a)}}))}this.waveClient?.input(t[t.length-1],e,o)}}))}async openWithPriviledge(){return console.log(this.btnStatus),await this.requestPermission(),this.createRecorder(),!!Recorder.IsOpen()||new Promise((t=>{this.recorder.open((()=>{let e=document.querySelector(".record-wave");e&&(console.log(e),Recorder.WaveView&&(this.waveClient=Recorder.WaveView({elem:".record-wave"}))),t(!0)}),((t,e)=>{console.log((e?"UserNotAllow,":"")+"无法录音:"+t)}))}))}getWebSocketUrl(){let t="wss://iat-api.xfyun.cn/v2/iat",e="iat-api.xfyun.cn",i=this.API_KEY,o=this.API_SECRET,s=(new Date).toUTCString(),r=`host: ${e}\ndate: ${s}\nGET /v2/iat HTTP/1.1`,a=CryptoJS.HmacSHA256(r,o),n=CryptoJS.enc.Base64.stringify(a);return t=`${t}?authorization=${btoa(`api_key="${i}", algorithm="hmac-sha256", headers="host date request-line", signature="${n}"`)}&date=${s}&host=${e}`,t}toBase64(t){for(var e="",i=new Uint8Array(t),o=i.byteLength,s=0;s<o;s++)e+=String.fromCharCode(i[s]);return window.btoa(e)}countTimer(){this.duration++;let t=String(parseInt(String(this.duration/60))).padStart(2,"0"),e=String((this.duration%60).toFixed(0)).padStart(2,"0"),i=t+":"+e;this.durationStr=i,this.connStatus=`录音中(${this.durationStr})`,console.log(this.duration,t,e),console.log(this.duration),console.log(i),this.onDurationStrChange&&this.onDurationStrChange(i)}startCountdown(){this.now=new Date,this.duration=0,this.countdownInterval&&clearInterval(this.countdownInterval),this.countdownInterval=setInterval((()=>{this.countTimer()}),1e3)}changeBtnStatus(t){this.btnStatus=t,"CONNECTING"===t?this.connStatus="建立连接中":"OPEN"===t||("CLOSING"===t?this.connStatus="关闭连接中":"CLOSED"===t&&(this.connStatus="开始录音"))}renderResult(t){let e=JSON.parse(t);if(e.data&&e.data.result){let t=e.data.result,i="",o=t.ws;for(let t=0;t<o.length;t++)i+=o[t].cw[0].w,console.log(i);t.pgs?("apd"===t.pgs&&(this.resultText=this.resultTextTemp),this.resultTextTemp=this.resultText+i):this.resultText=this.resultText+i,this.resultTextTemp||this.resultText,console.log("diff temp",this.resultTextTemp),console.log("diff result",this.resultText),this.onInputChange&&this.onInputChange(this.getUserInput())}0===e.code&&2===e.data.status&&this.iatWS.close(),0!==e.code&&(this.iatWS.close(),console.error(e))}getUserInput(){return""+(this.resultTextTemp||this.resultText)}connectWebSocket(){console.log("connectWebSocket");const t=this.getWebSocketUrl();if("WebSocket"in window)this.iatWS=new WebSocket(t);else if(!("MozWebSocket"in window))return void alert("浏览器不支持WebSocket");console.log("connectWebSocket",this.btnStatus),this.changeBtnStatus("CONNECTING"),this.iatWS.onopen=t=>{this.recordStart();var e={common:{app_id:this.APPID},business:{language:"zh_cn",domain:"iat",accent:"mandarin",vad_eos:5e3,dwa:"wpgs"},data:{status:0,format:"audio/L16;rate=16000",encoding:this.encodingType}};this.iatWS.send(JSON.stringify(e))},this.iatWS.onmessage=t=>{console.log("onmessage"+this.resultText),this.renderResult(t.data)},this.iatWS.onerror=t=>{console.error("error",t),this.recordStop(),this.changeBtnStatus("CLOSED")},this.iatWS.onclose=async t=>{console.log("onclose"+this.resultText),this.reconnectWebsocket()}}async reconnectWebsocket(){this.connectWebSocket()}isCapacitor(){return this.platform.is("capacitor")||this.platform.is("cordova")}async requestPermission(){if(this.isCapacitor())try{await this.requestStoagePermission(),await this.requestCameraPermission(),await this.requestMicPermission(),await this.requestRecordAudioPermission()}catch(t){console.error(t)}}async requestRecordAudioPermission(){let t=await this.diagnostic.requestRuntimePermissions([this.diagnostic.permission.RECORD_AUDIO]);console.log("record permission request:",t)}async requestMicPermission(){let t=await this.diagnostic.isMicrophoneAuthorized();if(console.log("permisson_MIC:",t),!t){await this.diagnostic.requestMicrophoneAuthorization()}}async requestStoagePermission(){let t=await this.diagnostic.isExternalStorageAuthorized();if(console.log("permisson_STORAGE:",t),!t){await this.diagnostic.requestExternalStorageAuthorization()}}async requestCameraPermission(){let t=await this.diagnostic.isCameraAuthorized();if(console.log("permisson_Camera:",t),!t){await this.diagnostic.requestCameraAuthorization()}}static{this.ɵfac=i0.ɵɵngDeclareFactory({minVersion:"12.0.0",version:"17.3.12",ngImport:i0,type:FmodeVoiceService,deps:[{token:i1.Platform},{token:i2.Diagnostic}],target:i0.ɵɵFactoryTarget.Injectable})}static{this.ɵprov=i0.ɵɵngDeclareInjectable({minVersion:"12.0.0",version:"17.3.12",ngImport:i0,type:FmodeVoiceService,providedIn:"root"})}}i0.ɵɵngDeclareClassMetadata({minVersion:"12.0.0",version:"17.3.12",ngImport:i0,type:FmodeVoiceService,decorators:[{type:Injectable,args:[{providedIn:"root"}]}],ctorParameters:()=>[{type:i1.Platform},{type:i2.Diagnostic}]});
8
+ import{Injectable}from"@angular/core";import Recorder from"recorder-core";import"recorder-core/src/engine/pcm";import"recorder-core/src/engine/wav";import"recorder-core/src/extensions/waveview";import CryptoJS from"crypto-js";import{pcmtoWav}from"./lib/pcm2wav";import{convertFrameBufferToBase64,resampleBuffer}from"./lib/resample";import{WebSpeech}from"./class-asr";import{Platform}from"@ionic/angular";import{Diagnostic}from"@awesome-cordova-plugins/diagnostic/ngx";import*as i0 from"@angular/core";import*as i1 from"@ionic/angular";import*as i2 from"@awesome-cordova-plugins/diagnostic/ngx";export class FmodeVoiceService{constructor(t,e){this.platform=t,this.diagnostic=e,this.webSpeech=WebSpeech,this.isUserFinish=!1,this.recordWavBlob=null,this.recordPcmBlob=null,this.recordDuration=0,this.recordType="pcm",this.encodingType="raw",this.connStatus="",this.btnStatus="UNDEFINED",this.resultText="",this.resultTextTemp="",this.APPID="50f4a46c",this.API_SECRET="NzFlNmFhZDJjMDNkZGM3NzI0Mzg2OGNm",this.API_KEY="106ddc40dfd4b9ca6d7b47c70fada749",this.durationStr="00:00",this.duration=0,this.requestPermission()}toggleRecord(){console.log(this.btnStatus),"UNDEFINED"===this.btnStatus||"CLOSED"===this.btnStatus?this.startTalk():"CONNECTING"!==this.btnStatus&&"OPEN"!==this.btnStatus||this.finishTalk()}finishTalk(){this.isUserFinish=!0,this.onBeforeFinishTalk&&this.onBeforeFinishTalk(),this.recordStop()}async startTalk(t){this.resultText="",this.resultTextTemp="",this.onBeforeStartTalk&&this.onBeforeStartTalk(),event?.preventDefault(),await this.openWithPriviledge(),setTimeout((()=>{this.connectWebSocket()}),100),this.startCountdown(),this.onAfterStartTalk&&this.onAfterStartTalk()}cancelTalk(){this.onBeforeCancelTalk&&this.onBeforeCancelTalk(),this.recordStop(),this.iatWS?.close(),this.resultText=null,this.onAfterCancelTalk&&this.onAfterCancelTalk()}async recordStart(){this.createRecorder(),await this.openWithPriviledge(),this.recorder.start(),this.changeBtnStatus("OPEN"),this.onAfterRecordStart&&this.onAfterRecordStart()}recordStop(){return new Promise((t=>{clearInterval(this.countdownInterval),this.changeBtnStatus("CLOSED"),this.recorder?.stop((async(e,i)=>{try{this.iatWS.send(JSON.stringify({data:{status:2,format:"audio/L16;rate=16000",encoding:this.encodingType,audio:""}}))}catch(t){}let s=(window.URL||webkitURL).createObjectURL(e);console.log(e,s,"时长:"+i+"ms"),this.recordPcmBlob=e,this.recordWavBlob=await this.pcmBlobToWavBlob(e,44100),this.recordDuration=i,console.log("this.recordWavBlob",this.recordWavBlob),setTimeout((()=>{this.isUserFinish&&(this.onAfterFinishTalk&&this.onAfterFinishTalk(),this.isUserFinish=!1)}),2e3),this.recorder?.close(),this.recorder=null,console.log("localUrl",s),t(!0)}),(e=>{console.log("录音失败:"+e),this.recorder.close(),this.recorder=null,t(null)}))}))}playRecord(){this.playPCM(this.recordPcmBlob,44100)}async pcmBlobToWavBlob(t,e){return new Promise((i=>{let s=new FileReader;s.onload=function(t){let s=t.target.result,o=pcmtoWav(s,e,1,16);i(o)},s.readAsArrayBuffer(t)}))}async playPCM(t,e){let i=await this.pcmBlobToWavBlob(t,e),s=window.URL.createObjectURL(i),o=new Audio;o.src=s,o.play()}async playBuffers(){let t=await this.BuffersToBlob(this.buffers);this.playPCM(t,44100)}BuffersToBlob(t){let e=[];return t.forEach((t=>{t.forEach((t=>{e.push(t)}))})),new Blob([e],{type:"audio/pcm"})}splitAudioData(t){const e=1280,i=Math.ceil(t.length/e),s=[];for(let o=0;o<i;o++){const i=o*e,r=i+e,a=t.slice(i,r);s.push(a)}return s}BufferToBlob(t){return new Blob([t],{type:"audio/pcm"})}createRecorder(){this.recorder||(this.recorder=Recorder({type:this.recordType,sampleRate:44100,bitRate:16,onProcess:(t,e,i,s,o,r)=>{let a=t.length&&t[t.length-1];if(this.buffers=t,a=resampleBuffer(a,44100,16e3),this.iatWS.readyState===this.iatWS.OPEN){if(this.disableASR)return;this.iatWS.send(JSON.stringify({data:{status:1,format:"audio/L16;rate=16000",encoding:this.encodingType,audio:convertFrameBufferToBase64(a)}}))}this.waveClient?.input(t[t.length-1],e,s)}}))}async openWithPriviledge(){return console.log(this.btnStatus),await this.requestPermission(),this.createRecorder(),!!Recorder.IsOpen()||new Promise((t=>{this.recorder.open((()=>{let e=document.querySelector(".record-wave");e&&(console.log(e),Recorder.WaveView&&(this.waveClient=Recorder.WaveView({elem:".record-wave"}))),t(!0)}),((t,e)=>{console.log((e?"UserNotAllow,":"")+"无法录音:"+t)}))}))}getWebSocketUrl(){let t="wss://iat-api.xfyun.cn/v2/iat",e="iat-api.xfyun.cn",i=this.API_KEY,s=this.API_SECRET,o=(new Date).toUTCString(),r=`host: ${e}\ndate: ${o}\nGET /v2/iat HTTP/1.1`,a=CryptoJS.HmacSHA256(r,s),n=CryptoJS.enc.Base64.stringify(a);return t=`${t}?authorization=${btoa(`api_key="${i}", algorithm="hmac-sha256", headers="host date request-line", signature="${n}"`)}&date=${o}&host=${e}`,t}toBase64(t){for(var e="",i=new Uint8Array(t),s=i.byteLength,o=0;o<s;o++)e+=String.fromCharCode(i[o]);return window.btoa(e)}countTimer(){this.duration++;let t=String(parseInt(String(this.duration/60))).padStart(2,"0"),e=String((this.duration%60).toFixed(0)).padStart(2,"0"),i=t+":"+e;this.durationStr=i,this.connStatus=`录音中(${this.durationStr})`,console.log(this.duration,t,e),console.log(this.duration),console.log(i),this.onDurationStrChange&&this.onDurationStrChange(i)}startCountdown(){this.now=new Date,this.duration=0,this.countdownInterval&&clearInterval(this.countdownInterval),this.countdownInterval=setInterval((()=>{this.countTimer()}),1e3)}changeBtnStatus(t){this.btnStatus=t,"CONNECTING"===t?this.connStatus="建立连接中":"OPEN"===t||("CLOSING"===t?this.connStatus="关闭连接中":"CLOSED"===t&&(this.connStatus="开始录音"))}renderResult(t){let e=JSON.parse(t);if(e.data&&e.data.result){let t=e.data.result,i="",s=t.ws;for(let t=0;t<s.length;t++)i+=s[t].cw[0].w,console.log(i);t.pgs?("apd"===t.pgs&&(this.resultText=this.resultTextTemp),this.resultTextTemp=this.resultText+i):this.resultText=this.resultText+i,this.resultTextTemp||this.resultText,console.log("diff temp",this.resultTextTemp),console.log("diff result",this.resultText),this.onInputChange&&this.onInputChange(this.getUserInput())}0===e.code&&2===e.data.status&&this.iatWS.close(),0!==e.code&&(this.iatWS.close(),console.error(e))}getUserInput(){return""+(this.resultTextTemp||this.resultText)}connectWebSocket(){console.log("connectWebSocket");const t=this.getWebSocketUrl();if("WebSocket"in window)this.iatWS=new WebSocket(t);else if(!("MozWebSocket"in window))return void alert("浏览器不支持WebSocket");console.log("connectWebSocket",this.btnStatus),this.changeBtnStatus("CONNECTING"),this.iatWS.onopen=t=>{this.recordStart();var e={common:{app_id:this.APPID},business:{language:"zh_cn",domain:"iat",accent:"mandarin",vad_eos:5e3,dwa:"wpgs"},data:{status:0,format:"audio/L16;rate=16000",encoding:this.encodingType}};this.iatWS.send(JSON.stringify(e))},this.iatWS.onmessage=t=>{console.log("onmessage"+this.resultText),this.renderResult(t.data)},this.iatWS.onerror=t=>{console.error("error",t),this.recordStop(),this.changeBtnStatus("CLOSED")},this.iatWS.onclose=async t=>{console.log("onclose"+this.resultText),this.reconnectWebsocket()}}async reconnectWebsocket(){this.isUserFinish||this.connectWebSocket()}isCapacitor(){return this.platform.is("capacitor")||this.platform.is("cordova")}async requestPermission(){if(this.isCapacitor())try{await this.requestStoagePermission(),await this.requestCameraPermission(),await this.requestMicPermission(),await this.requestRecordAudioPermission()}catch(t){console.error(t)}}async requestRecordAudioPermission(){let t=await this.diagnostic.requestRuntimePermissions([this.diagnostic.permission.RECORD_AUDIO]);console.log("record permission request:",t)}async requestMicPermission(){let t=await this.diagnostic.isMicrophoneAuthorized();if(console.log("permisson_MIC:",t),!t){await this.diagnostic.requestMicrophoneAuthorization()}}async requestStoagePermission(){let t=await this.diagnostic.isExternalStorageAuthorized();if(console.log("permisson_STORAGE:",t),!t){await this.diagnostic.requestExternalStorageAuthorization()}}async requestCameraPermission(){let t=await this.diagnostic.isCameraAuthorized();if(console.log("permisson_Camera:",t),!t){await this.diagnostic.requestCameraAuthorization()}}static{this.ɵfac=i0.ɵɵngDeclareFactory({minVersion:"12.0.0",version:"17.3.12",ngImport:i0,type:FmodeVoiceService,deps:[{token:i1.Platform},{token:i2.Diagnostic}],target:i0.ɵɵFactoryTarget.Injectable})}static{this.ɵprov=i0.ɵɵngDeclareInjectable({minVersion:"12.0.0",version:"17.3.12",ngImport:i0,type:FmodeVoiceService,providedIn:"root"})}}i0.ɵɵngDeclareClassMetadata({minVersion:"12.0.0",version:"17.3.12",ngImport:i0,type:FmodeVoiceService,decorators:[{type:Injectable,args:[{providedIn:"root"}]}],ctorParameters:()=>[{type:i1.Platform},{type:i2.Diagnostic}]});
9
9
  var MODULE_PATH_NEED = `6K+l5paH5Lu25piv5pys6aG555uu55qE5LiA6YOo5YiGIFRoaXMgZmlsZSBpcyBwYXJ0IG9mIHRoZSBDb21wb25lbnRzIGluIEZtb2RlIEluYy4KICAgIOeJiOadg+aJgOaciSDCqSDmnKrmnaXpo57pqawgwqkg5rGf6KW/6ISR5o6n56eR5oqA5pyJ6ZmQ5YWs5Y+4IENvcHlyaWdodCDCqSBGbW9kZSBUZWNobm9sb2d5IENvLiwgTHRkLgogICAg5L+d55WZ5omA5pyJ5p2D5YipIEFsbCBSaWdodHMgUmVzZXJ2ZWQuCiAgICDkuKXnpoHlnKjmnKrnu4/mjojmnYPnmoTmg4XlhrXkuIvvvIzpgJrov4fku7vkvZXlqpLku4vlpI3liLbmraTmlofku7YgVW5hdXRob3JpemVkIGNvcHlpbmcgb2YgdGhpcyBmaWxlLCB2aWEgYW55IG1lZGl1bSBpcyBzdHJpY3RseSBwcm9oaWJpdGVkCiAgICDor6Xmlofku7bmmK/kuJPmnInnmoTmnLrlr4bmlofku7YgUHJvcHJpZXRhcnkgYW5kIGNvbmZpZGVudGlhbAogICAKICAgIENvcHlyaWdodCAyMDIxLW5vdyBGbW9kZSBJbmMuIHN1cHBvcnRAZm1vZGUuY24uIDE4NjA3MDA3MDczLgogICAg5L+d55WZ5omA5pyJ5p2D5YipIEFsbCByaWdodHMgcmVzZXJ2ZWQuCgogICAgUEFUSDovaG9tZS9yeWFuL3dvcmtzcGFjZS9ub3ZhL25vdmEtYWRtaW4vZGlzdC9mbW9kZS1uZy9lc20yMDIyL2xpYi9haWdjL3ZvaWNlL2Ztb2RlLXZvaWNlLnNlcnZpY2UubWpz`
10
10
 
@@ -5,6 +5,6 @@
5
5
  * 保留所有权利 All Rights Reserved.
6
6
  * /home/ryan/workspace/nova/nova-admin/dist/fmode-ng/esm2022/lib/aigc/voice/stream.player.mjs
7
7
  */
8
- import{PushAudioOutputStreamCallback}from"microsoft-cognitiveservices-speech-sdk/distrib/lib/src/sdk/Audio/PushAudioOutputStreamCallback";export class FmPushAudioOutputStreamCallback extends PushAudioOutputStreamCallback{constructor(){super(),this.audioContext=new window.AudioContext,this.source=null,this.buffer=[]}write(t){new Float32Array(t);this.buffer.push(t),this.playAudio()}playAudio(){this.source&&this.source.stop();const t=this.buffer.reduce(((t,e)=>t+e.length),0),e=new Float32Array(t);let s=0;for(const t of this.buffer)e.set(t,s),s+=t.length;const o=this.audioContext.createBuffer(1,e.length,this.audioContext.sampleRate);o.copyToChannel(e,0),this.source=this.audioContext.createBufferSource(),this.source.buffer=o,this.source.connect(this.audioContext.destination),this.source.start(),this.buffer=[]}close(){this.source&&this.source.stop(),this.audioContext.close()}}
8
+ import{PushAudioOutputStreamCallback}from"microsoft-cognitiveservices-speech-sdk/distrib/lib/src/sdk/Audio/PushAudioOutputStreamCallback";export class FmPushAudioOutputStreamCallback extends PushAudioOutputStreamCallback{constructor(){super(),this.buffer=[],this.isPlaying=!1,this.realBuffers=[],this.audioContext=new(window.AudioContext||window.webkitAudioContext),this.source=null,this.buffer=[]}async write(t){this.realBuffers.push(t);let i=new Float32Array(t);this.buffer.push(i),console.log(this.buffer),console.log(this.realBuffers),await this.playAudio()}async playAudio(){if(!this.isPlaying){this.isPlaying=!0;for(const t of this.realBuffers){const i=await this.audioContext.decodeAudioData(t),s=this.audioContext.createBufferSource();s.buffer=i,s.connect(this.audioContext.destination),s.start(),await new Promise((t=>{s.onended=t}))}this.isPlaying=!1}}close(){this.source&&this.source.stop(),this.audioContext.close()}}
9
9
  var MODULE_PATH_NEED = `6K+l5paH5Lu25piv5pys6aG555uu55qE5LiA6YOo5YiGIFRoaXMgZmlsZSBpcyBwYXJ0IG9mIHRoZSBDb21wb25lbnRzIGluIEZtb2RlIEluYy4KICAgIOeJiOadg+aJgOaciSDCqSDmnKrmnaXpo57pqawgwqkg5rGf6KW/6ISR5o6n56eR5oqA5pyJ6ZmQ5YWs5Y+4IENvcHlyaWdodCDCqSBGbW9kZSBUZWNobm9sb2d5IENvLiwgTHRkLgogICAg5L+d55WZ5omA5pyJ5p2D5YipIEFsbCBSaWdodHMgUmVzZXJ2ZWQuCiAgICDkuKXnpoHlnKjmnKrnu4/mjojmnYPnmoTmg4XlhrXkuIvvvIzpgJrov4fku7vkvZXlqpLku4vlpI3liLbmraTmlofku7YgVW5hdXRob3JpemVkIGNvcHlpbmcgb2YgdGhpcyBmaWxlLCB2aWEgYW55IG1lZGl1bSBpcyBzdHJpY3RseSBwcm9oaWJpdGVkCiAgICDor6Xmlofku7bmmK/kuJPmnInnmoTmnLrlr4bmlofku7YgUHJvcHJpZXRhcnkgYW5kIGNvbmZpZGVudGlhbAogICAKICAgIENvcHlyaWdodCAyMDIxLW5vdyBGbW9kZSBJbmMuIHN1cHBvcnRAZm1vZGUuY24uIDE4NjA3MDA3MDczLgogICAg5L+d55WZ5omA5pyJ5p2D5YipIEFsbCByaWdodHMgcmVzZXJ2ZWQuCgogICAgUEFUSDovaG9tZS9yeWFuL3dvcmtzcGFjZS9ub3ZhL25vdmEtYWRtaW4vZGlzdC9mbW9kZS1uZy9lc20yMDIyL2xpYi9haWdjL3ZvaWNlL3N0cmVhbS5wbGF5ZXIubWpz`
10
10
 
@@ -5,6 +5,6 @@
5
5
  * 保留所有权利 All Rights Reserved.
6
6
  * /home/ryan/workspace/nova/nova-admin/dist/fmode-ng/esm2022/lib/aigc/voice/tts/fmode-tts-class.mjs
7
7
  */
8
- import{SpeechSynthesizer}from"microsoft-cognitiveservices-speech-sdk/distrib/lib/src/sdk/SpeechSynthesizer";import{SpeechConfig}from"microsoft-cognitiveservices-speech-sdk/distrib/lib/src/sdk/SpeechConfig";import{ResultReason}from"microsoft-cognitiveservices-speech-sdk/distrib/lib/src/sdk/ResultReason";import Parse from"parse";import{AudioConfig}from"microsoft-cognitiveservices-speech-sdk/distrib/lib/src/sdk/Audio/AudioConfig";import{AudioPlayer}from"../audio.player";import{FmPushAudioOutputStreamCallback}from"../stream.player";export class FmodeTTS{constructor(e,t){this.uploadServ=t,this.subscriptionKey=e?.subscriptionKey,this.authorizationToken=e?.token,this.region=e?.region,this.subscriptionKey&&(this.speechConfig=SpeechConfig.fromSubscription(this.subscriptionKey,this.region)),this.authorizationToken&&(this.speechConfig=SpeechConfig.fromAuthorizationToken(this.authorizationToken,this.region));let o=new FmPushAudioOutputStreamCallback;this.audioConfig=AudioConfig.fromStreamOutput(o),this.synthesizer=new SpeechSynthesizer(this.speechConfig,this.audioConfig)}extractTextFromXML(e){const t=/>([^<]+)</g;let o,i=[];for(;null!==(o=t.exec(e));){const e=o[1].trim();e&&i.push(e)}return i.join(" ")}extractSSMLContent(e){var t=e.match(/<speak.*?<\/speak>/s);return t?t[0]:e}async speakAsync(e,t,o){if(!t){let o=Parse.Object.extend("ChatVoice");(t=new o).set("ssml",e),t.set("content",this.extractTextFromXML(e));let i=localStorage.getItem("company");i&&t.set("company",{__type:"Pointer",className:"Company",objectId:i}),Parse.User.current()?.id&&t.set("user",Parse.User.current().toPointer())}if(!t?.get("voiceFile")){let o=[];t?.get("content")&&o.push({content:t?.get("content")}),e&&o.push({ssml:e});let i=Parse.Query.fromJSON("ChatVoice",{include:"voiceFile",where:{$or:o}}),s=await i.first();t.set("voiceFile",s?.get("voiceFile"))}return t?.get("voiceFile")?(this.playAudioData(t?.get("voiceFile")?.get("url"),t,o),t):(e=this.extractSSMLContent(e),new Promise(((i,s)=>{const n=Date.now();let r="speakTextAsync";e?.indexOf("<")>-1&&(r="speakSsmlAsync"),this.eventMap?.onSpeakBefore&&this.eventMap?.onSpeakBefore(),this.synthesizer[r](e,(e=>{if(console.log(e),e.reason===ResultReason.SynthesizingAudioCompleted){this.eventMap?.onAudioCompleted&&this.eventMap?.onAudioCompleted();const s=Date.now();let r=e?.audioData;t.set("duration",Number(e?.audioDuration)/1e4),this.playAudioData(r,t,o),console.log(`Audio synthesis finished. Duration: ${s-n} ms`),setTimeout((()=>{o?.onStop()&&o?.onStop()}),2e3),i(t)}else s(`Speech synthesis failed. Reason: ${e.errorDetails}`)}),(e=>{s(`Error occurred during synthesis: ${e}`)}))})))}onBreak(e){}async playAudioData(e,t,o){let i;if(console.log("audioData",e),e?.indexOf&&e?.indexOf("http")>=-1)i=e;else{let o=new Blob([e],{type:"audio/wav"});i=URL.createObjectURL(o),this.uploadAndSaveVoice(o,t)}const s=AudioPlayer.getInstance();o?.onStart&&o?.onStart(t),s.setAudioEvent("onloadeddata",(()=>{let e=1e3*s.duration;console.log("duration",e),t?.get("duration")||(t?.set("duration",e),t?.save()),o?.onLoaded&&o?.onLoaded(s)})),console.log("event setting"),s.setAudioEvent("onabort",(()=>{o?.onStop&&o?.onStop()})),s.setAudioEvent("onerror",(()=>{o?.onStop&&o?.onStop()})),s.setAudioEvent("onpause",(()=>{o?.onStop&&o?.onStop()})),s.setAudioEvent("onended",(()=>{o?.onStop&&o?.onStop()})),s.setAudioEvent("onclose",(()=>{o?.onStop&&o?.onStop()}));let playAudio=()=>{s.play(i).then((()=>{})).catch((()=>{setTimeout((()=>{console.log("playAudio"),playAudio()}),200)}))};playAudio()}async uploadAndSaveVoice(e,t){if(this.uploadServ){let o=t?.id||this.uploadServ?.genMd5(t?.get("content")||t?.get("ssml")),i=new Date,s=o+i.getFullYear()+(i.getMonth()+1)+i.getDate()+i.getHours()+i.getMinutes()+i.getSeconds()+".wav",n=new File([e],s,{type:"audio/wav"}),r=await this.uploadServ.upload(n,(e=>{console.log(e)})),a={__type:"Pointer",className:"Attachment",objectId:r?.id};return a?.objectId&&(t.set("voiceFile",a),t=await t.save()),r?.url}return null}}
8
+ import{SpeechSynthesizer}from"microsoft-cognitiveservices-speech-sdk/distrib/lib/src/sdk/SpeechSynthesizer";import{SpeechConfig}from"microsoft-cognitiveservices-speech-sdk/distrib/lib/src/sdk/SpeechConfig";import{ResultReason}from"microsoft-cognitiveservices-speech-sdk/distrib/lib/src/sdk/ResultReason";import Parse from"parse";import{AudioConfig}from"microsoft-cognitiveservices-speech-sdk/distrib/lib/src/sdk/Audio/AudioConfig";import{AudioPlayer}from"../audio.player";import{FmPushAudioOutputStreamCallback}from"../stream.player";export class FmodeTTS{constructor(e,t){this.uploadServ=t,this.subscriptionKey=e?.subscriptionKey,this.authorizationToken=e?.token,this.region=e?.region,this.subscriptionKey&&(this.speechConfig=SpeechConfig.fromSubscription(this.subscriptionKey,this.region)),this.authorizationToken&&(this.speechConfig=SpeechConfig.fromAuthorizationToken(this.authorizationToken,this.region));let o=new FmPushAudioOutputStreamCallback;this.audioConfig=AudioConfig.fromStreamOutput(o),this.synthesizer=new SpeechSynthesizer(this.speechConfig,this.audioConfig)}extractTextFromXML(e){const t=/>([^<]+)</g;let o,i=[];for(;null!==(o=t.exec(e));){const e=o[1].trim();e&&i.push(e)}return i.join(" ")}extractSSMLContent(e){var t=e.match(/<speak.*?<\/speak>/s);return t?t[0]:e}async speakAsync(e,t,o){if(!t){let o=Parse.Object.extend("ChatVoice");(t=new o).set("ssml",e),t.set("content",this.extractTextFromXML(e));let i=localStorage.getItem("company");i&&t.set("company",{__type:"Pointer",className:"Company",objectId:i}),Parse.User.current()?.id&&t.set("user",Parse.User.current().toPointer())}if(!t?.get("voiceFile")){let o=[];t?.get("content")&&o.push({content:t?.get("content")}),e&&o.push({ssml:e});let i=Parse.Query.fromJSON("ChatVoice",{include:"voiceFile",where:{$or:o}}),s=await i.first();t.set("voiceFile",s?.get("voiceFile"))}return t?.get("voiceFile")?(this.playAudioData(t?.get("voiceFile")?.get("url"),t,o),t):(e=this.extractSSMLContent(e),new Promise(((i,s)=>{const n=Date.now();let r="speakTextAsync";e?.indexOf("<")>-1&&(r="speakSsmlAsync"),this.eventMap?.onSpeakBefore&&this.eventMap?.onSpeakBefore(),this.synthesizer[r](e,(e=>{if(console.log(e),e.reason===ResultReason.SynthesizingAudioCompleted){this.eventMap?.onAudioCompleted&&this.eventMap?.onAudioCompleted();const s=Date.now();let r=e?.audioData;t.set("duration",Number(e?.audioDuration)/1e4),this.playAudioData(r,t,o),console.log(`Audio synthesis finished. Duration: ${s-n} ms`),setTimeout((()=>{o?.onStop()&&o?.onStop()}),2e3),i(t)}else s(`Speech synthesis failed. Reason: ${e.errorDetails}`)}),(e=>{s(`Error occurred during synthesis: ${e}`)}))})))}onBreak(e){}async playAudioData(e,t,o){let i;if(console.log("audioData",e),!(e?.indexOf&&e?.indexOf("http")>=-1)){let o=new Blob([e],{type:"audio/wav"});return i=URL.createObjectURL(o),void this.uploadAndSaveVoice(o,t)}i=e;const s=AudioPlayer.getInstance();o?.onStart&&o?.onStart(t),s.setAudioEvent("onloadeddata",(()=>{let e=1e3*s.duration;console.log("duration",e),t?.get("duration")||(t?.set("duration",e),t?.save()),o?.onLoaded&&o?.onLoaded(s)})),console.log("event setting"),s.setAudioEvent("onabort",(()=>{o?.onStop&&o?.onStop()})),s.setAudioEvent("onerror",(()=>{o?.onStop&&o?.onStop()})),s.setAudioEvent("onpause",(()=>{o?.onStop&&o?.onStop()})),s.setAudioEvent("onended",(()=>{o?.onStop&&o?.onStop()})),s.setAudioEvent("onclose",(()=>{o?.onStop&&o?.onStop()}));let playAudio=()=>{s.play(i).then((()=>{})).catch((()=>{setTimeout((()=>{console.log("playAudio"),playAudio()}),200)}))};playAudio()}async uploadAndSaveVoice(e,t){if(this.uploadServ){let o=t?.id||this.uploadServ?.genMd5(t?.get("content")||t?.get("ssml")),i=new Date,s=o+i.getFullYear()+(i.getMonth()+1)+i.getDate()+i.getHours()+i.getMinutes()+i.getSeconds()+".wav",n=new File([e],s,{type:"audio/wav"}),r=await this.uploadServ.upload(n,(e=>{console.log(e)})),a={__type:"Pointer",className:"Attachment",objectId:r?.id};return a?.objectId&&(t.set("voiceFile",a),t=await t.save()),r?.url}return null}}
9
9
  var MODULE_PATH_NEED = `6K+l5paH5Lu25piv5pys6aG555uu55qE5LiA6YOo5YiGIFRoaXMgZmlsZSBpcyBwYXJ0IG9mIHRoZSBDb21wb25lbnRzIGluIEZtb2RlIEluYy4KICAgIOeJiOadg+aJgOaciSDCqSDmnKrmnaXpo57pqawgwqkg5rGf6KW/6ISR5o6n56eR5oqA5pyJ6ZmQ5YWs5Y+4IENvcHlyaWdodCDCqSBGbW9kZSBUZWNobm9sb2d5IENvLiwgTHRkLgogICAg5L+d55WZ5omA5pyJ5p2D5YipIEFsbCBSaWdodHMgUmVzZXJ2ZWQuCiAgICDkuKXnpoHlnKjmnKrnu4/mjojmnYPnmoTmg4XlhrXkuIvvvIzpgJrov4fku7vkvZXlqpLku4vlpI3liLbmraTmlofku7YgVW5hdXRob3JpemVkIGNvcHlpbmcgb2YgdGhpcyBmaWxlLCB2aWEgYW55IG1lZGl1bSBpcyBzdHJpY3RseSBwcm9oaWJpdGVkCiAgICDor6Xmlofku7bmmK/kuJPmnInnmoTmnLrlr4bmlofku7YgUHJvcHJpZXRhcnkgYW5kIGNvbmZpZGVudGlhbAogICAKICAgIENvcHlyaWdodCAyMDIxLW5vdyBGbW9kZSBJbmMuIHN1cHBvcnRAZm1vZGUuY24uIDE4NjA3MDA3MDczLgogICAg5L+d55WZ5omA5pyJ5p2D5YipIEFsbCByaWdodHMgcmVzZXJ2ZWQuCgogICAgUEFUSDovaG9tZS9yeWFuL3dvcmtzcGFjZS9ub3ZhL25vdmEtYWRtaW4vZGlzdC9mbW9kZS1uZy9lc20yMDIyL2xpYi9haWdjL3ZvaWNlL3R0cy9mbW9kZS10dHMtY2xhc3MubWpz`
10
10