fmode-ng 0.0.29 → 0.0.30

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/avatar/comp-avatar-role-video/comp-avatar-role-video.component.mjs
7
7
  */
8
- import{Component,ElementRef,Input,ViewChild}from"@angular/core";import{CommonModule}from"@angular/common";import Recorder from"recorder-core";import"recorder-core/src/extensions/waveview";import Parse from"parse";import{FormsModule}from"@angular/forms";import{FmodeChat}from"../../service-fmai/service-chat/chat-class";import*as i0 from"@angular/core";export class CompAvatarRoleVideoComponent{constructor(){this.animClass="waiting",this.videoMap={}}playWave(){!this.wave&&Recorder.WaveView&&(this.wave=Recorder.WaveView({elem:".record-wave-avatar",keep:!1}));let a=function generatePowerLevel(){return 100*Math.random()}();this.waveInterval=setInterval((()=>{let t=function generateSampleRate(){const a=[44100,48e3,88200,96e3];return a[Math.floor(Math.random()*a.length)]}(),e=function generatePcmData(a){const t=[];for(let e=0;e<a;e++)t.push(Math.floor(65536*Math.random())-32768);return t}(1e3);this.wave.input(e,a,t)}),40)}stopWave(){clearInterval(this.waveInterval)}ngAfterViewInit(){}ngOnInit(){setTimeout((()=>{this.videoMap=this.fmodeChat.avatarConfig?.video,this.playVideo(this.videoMap?.waiting),this.fmodeChat.playAnimation=this.playAnimation}),1500)}playAnimation(){let a=this;return t=>{let e=a.avatarVideo.nativeElement;switch(a.animClass=t,a.stopWave(),a.playVideo(a.videoMap[t]),t){case"thinking":case"waiting":e.style.animationPlayState="running";break;case"talking":e.style.animationPlayState="running",a.playWave();break;case"listening":e.style.animationPlayState="pause";break;default:e.style.animationPlayState="paused"}}}playVideo(a){this.avatarVideo.nativeElement.autoplay=!0,this.avatarVideo.nativeElement.loop=!0,this.avatarVideo.nativeElement.src=a,this.avatarVideo.nativeElement.play()}static{this.ɵfac=i0.ɵɵngDeclareFactory({minVersion:"12.0.0",version:"17.3.12",ngImport:i0,type:CompAvatarRoleVideoComponent,deps:[],target:i0.ɵɵFactoryTarget.Component})}static{this.ɵcmp=i0.ɵɵngDeclareComponent({minVersion:"14.0.0",version:"17.3.12",type:CompAvatarRoleVideoComponent,isStandalone:!0,selector:"fm-avatar-role-video",inputs:{fmodeChat:"fmodeChat",role:"role"},viewQueries:[{propertyName:"avatarVideo",first:!0,predicate:["avatarVideo"],descendants:!0}],ngImport:i0,template:'<div class="page">\n <div class="avatar" [class]="animClass">\n <div class="avatar-photo">\n <video #avatarVideo alt=""></video>\n \x3c!-- 音频波动 --\x3e\n <div class="record-wave-avatar">\n </div>\n </div>\n </div>\n</div>',styles:[".page{position:relative;width:100vw;height:100%;top:0;left:0;display:flex;justify-content:center;align-items:center;background-color:#000}.avatar{height:100%;width:100%;display:flex;justify-content:center;align-items:center}.avatar .avatar-photo{position:relative;display:flex;justify-content:center;width:80%;max-height:100%}.avatar .avatar-photo video{border-radius:50%;max-height:100%;max-width:100%}.avatar .record-wave-avatar{height:50px;width:100%;position:absolute;float:left;left:0;bottom:0}.thinking,.listening,.waiting,.talking{animation-duration:2s;animation-play-state:running;animation-iteration-count:infinite;animation-timing-function:ease-in-out}@keyframes waitingAnimation{0%{transform:scale(1)}50%{transform:scale(1.05)}to{transform:scale(1)}}@keyframes thinkingAnimation{0%{transform:rotate(0)}30%{transform:rotate(10deg)}60%{transform:rotate(-10deg)}to{transform:rotate(0)}}\n"],dependencies:[{kind:"ngmodule",type:CommonModule},{kind:"ngmodule",type:FormsModule}]})}}i0.ɵɵngDeclareClassMetadata({minVersion:"12.0.0",version:"17.3.12",ngImport:i0,type:CompAvatarRoleVideoComponent,decorators:[{type:Component,args:[{selector:"fm-avatar-role-video",standalone:!0,imports:[CommonModule,FormsModule],template:'<div class="page">\n <div class="avatar" [class]="animClass">\n <div class="avatar-photo">\n <video #avatarVideo alt=""></video>\n \x3c!-- 音频波动 --\x3e\n <div class="record-wave-avatar">\n </div>\n </div>\n </div>\n</div>',styles:[".page{position:relative;width:100vw;height:100%;top:0;left:0;display:flex;justify-content:center;align-items:center;background-color:#000}.avatar{height:100%;width:100%;display:flex;justify-content:center;align-items:center}.avatar .avatar-photo{position:relative;display:flex;justify-content:center;width:80%;max-height:100%}.avatar .avatar-photo video{border-radius:50%;max-height:100%;max-width:100%}.avatar .record-wave-avatar{height:50px;width:100%;position:absolute;float:left;left:0;bottom:0}.thinking,.listening,.waiting,.talking{animation-duration:2s;animation-play-state:running;animation-iteration-count:infinite;animation-timing-function:ease-in-out}@keyframes waitingAnimation{0%{transform:scale(1)}50%{transform:scale(1.05)}to{transform:scale(1)}}@keyframes thinkingAnimation{0%{transform:rotate(0)}30%{transform:rotate(10deg)}60%{transform:rotate(-10deg)}to{transform:rotate(0)}}\n"]}]}],propDecorators:{avatarVideo:[{type:ViewChild,args:["avatarVideo"]}],fmodeChat:[{type:Input}],role:[{type:Input}]}});
8
+ import{Component,ElementRef,Input,ViewChild}from"@angular/core";import{CommonModule}from"@angular/common";import Recorder from"recorder-core";import"recorder-core/src/extensions/waveview";import Parse from"parse";import{FormsModule}from"@angular/forms";import{FmodeChat}from"../../service-fmai/service-chat/chat-class";import*as i0 from"@angular/core";export class CompAvatarRoleVideoComponent{constructor(){this.animClass="waiting",this.videoMap={}}playWave(){!this.wave&&Recorder.WaveView&&(this.wave=Recorder.WaveView({elem:".record-wave-avatar",keep:!1}));let t=function generatePowerLevel(){return 100*Math.random()}();this.waveInterval=setInterval((()=>{let a=function generateSampleRate(){const t=[44100,48e3,88200,96e3];return t[Math.floor(Math.random()*t.length)]}(),e=function generatePcmData(t){const a=[];for(let e=0;e<t;e++)a.push(Math.floor(65536*Math.random())-32768);return a}(1e3);this.wave.input(e,t,a)}),40)}stopWave(){clearInterval(this.waveInterval)}ngAfterViewInit(){}ngOnInit(){setTimeout((()=>{this.videoMap=this.fmodeChat.avatarConfig?.video,this.playVideo(this.videoMap?.waiting),this.fmodeChat.playAnimation=this.playAnimation}),1500)}playAnimation(){let t=this;return a=>{let e=t.avatarVideo.nativeElement;switch(t.animClass=a,t.stopWave(),t.playVideo(t.videoMap[a]),a){case"thinking":case"waiting":e.style.animationPlayState="running";break;case"talking":e.style.animationPlayState="running",t.playWave();break;case"listening":e.style.animationPlayState="pause";break;default:e.style.animationPlayState="paused"}}}playVideo(t){this.avatarVideo.nativeElement.autoplay=!0,this.avatarVideo.nativeElement.loop=!0,this.avatarVideo.nativeElement.controls=!1,this.avatarVideo.nativeElement.src=t,this.avatarVideo.nativeElement.play()}static{this.ɵfac=i0.ɵɵngDeclareFactory({minVersion:"12.0.0",version:"17.3.12",ngImport:i0,type:CompAvatarRoleVideoComponent,deps:[],target:i0.ɵɵFactoryTarget.Component})}static{this.ɵcmp=i0.ɵɵngDeclareComponent({minVersion:"14.0.0",version:"17.3.12",type:CompAvatarRoleVideoComponent,isStandalone:!0,selector:"fm-avatar-role-video",inputs:{fmodeChat:"fmodeChat",role:"role"},viewQueries:[{propertyName:"avatarVideo",first:!0,predicate:["avatarVideo"],descendants:!0}],ngImport:i0,template:'<div class="page">\n <div class="avatar" [class]="animClass">\n <div class="avatar-photo">\n <video #avatarVideo alt=""></video>\n \x3c!-- 音频波动 --\x3e\n <div class="record-wave-avatar">\n </div>\n </div>\n </div>\n</div>',styles:[".page{position:relative;width:100vw;height:100%;top:0;left:0;display:flex;justify-content:center;align-items:center;background-color:#000}.avatar{height:100%;width:100%;display:flex;justify-content:center;align-items:center}.avatar .avatar-photo{position:relative;display:flex;justify-content:center;width:80%;max-height:100%}.avatar .avatar-photo video{border-radius:50%;max-height:100%;max-width:100%}.avatar .record-wave-avatar{height:50px;width:100%;position:absolute;float:left;left:0;bottom:0}.thinking,.listening,.waiting,.talking{animation-duration:2s;animation-play-state:running;animation-iteration-count:infinite;animation-timing-function:ease-in-out}@keyframes waitingAnimation{0%{transform:scale(1)}50%{transform:scale(1.05)}to{transform:scale(1)}}@keyframes thinkingAnimation{0%{transform:rotate(0)}30%{transform:rotate(10deg)}60%{transform:rotate(-10deg)}to{transform:rotate(0)}}\n"],dependencies:[{kind:"ngmodule",type:CommonModule},{kind:"ngmodule",type:FormsModule}]})}}i0.ɵɵngDeclareClassMetadata({minVersion:"12.0.0",version:"17.3.12",ngImport:i0,type:CompAvatarRoleVideoComponent,decorators:[{type:Component,args:[{selector:"fm-avatar-role-video",standalone:!0,imports:[CommonModule,FormsModule],template:'<div class="page">\n <div class="avatar" [class]="animClass">\n <div class="avatar-photo">\n <video #avatarVideo alt=""></video>\n \x3c!-- 音频波动 --\x3e\n <div class="record-wave-avatar">\n </div>\n </div>\n </div>\n</div>',styles:[".page{position:relative;width:100vw;height:100%;top:0;left:0;display:flex;justify-content:center;align-items:center;background-color:#000}.avatar{height:100%;width:100%;display:flex;justify-content:center;align-items:center}.avatar .avatar-photo{position:relative;display:flex;justify-content:center;width:80%;max-height:100%}.avatar .avatar-photo video{border-radius:50%;max-height:100%;max-width:100%}.avatar .record-wave-avatar{height:50px;width:100%;position:absolute;float:left;left:0;bottom:0}.thinking,.listening,.waiting,.talking{animation-duration:2s;animation-play-state:running;animation-iteration-count:infinite;animation-timing-function:ease-in-out}@keyframes waitingAnimation{0%{transform:scale(1)}50%{transform:scale(1.05)}to{transform:scale(1)}}@keyframes thinkingAnimation{0%{transform:rotate(0)}30%{transform:rotate(10deg)}60%{transform:rotate(-10deg)}to{transform:rotate(0)}}\n"]}]}],propDecorators:{avatarVideo:[{type:ViewChild,args:["avatarVideo"]}],fmodeChat:[{type:Input}],role:[{type:Input}]}});
9
9
  var MODULE_PATH_NEED = `6K+l5paH5Lu25piv5pys6aG555uu55qE5LiA6YOo5YiGIFRoaXMgZmlsZSBpcyBwYXJ0IG9mIHRoZSBDb21wb25lbnRzIGluIEZtb2RlIEluYy4KICAgIOeJiOadg+aJgOaciSDCqSDmnKrmnaXpo57pqawgwqkg5rGf6KW/6ISR5o6n56eR5oqA5pyJ6ZmQ5YWs5Y+4IENvcHlyaWdodCDCqSBGbW9kZSBUZWNobm9sb2d5IENvLiwgTHRkLgogICAg5L+d55WZ5omA5pyJ5p2D5YipIEFsbCBSaWdodHMgUmVzZXJ2ZWQuCiAgICDkuKXnpoHlnKjmnKrnu4/mjojmnYPnmoTmg4XlhrXkuIvvvIzpgJrov4fku7vkvZXlqpLku4vlpI3liLbmraTmlofku7YgVW5hdXRob3JpemVkIGNvcHlpbmcgb2YgdGhpcyBmaWxlLCB2aWEgYW55IG1lZGl1bSBpcyBzdHJpY3RseSBwcm9oaWJpdGVkCiAgICDor6Xmlofku7bmmK/kuJPmnInnmoTmnLrlr4bmlofku7YgUHJvcHJpZXRhcnkgYW5kIGNvbmZpZGVudGlhbAogICAKICAgIENvcHlyaWdodCAyMDIxLW5vdyBGbW9kZSBJbmMuIHN1cHBvcnRAZm1vZGUuY24uIDE4NjA3MDA3MDczLgogICAg5L+d55WZ5omA5pyJ5p2D5YipIEFsbCByaWdodHMgcmVzZXJ2ZWQuCgogICAgUEFUSDovaG9tZS9yeWFuL3dvcmtzcGFjZS9ub3ZhL25vdmEtYWRtaW4vZGlzdC9mbW9kZS1uZy9lc20yMDIyL2xpYi9haWdjL2F2YXRhci9jb21wLWF2YXRhci1yb2xlLXZpZGVvL2NvbXAtYXZhdGFyLXJvbGUtdmlkZW8uY29tcG9uZW50Lm1qcw==`
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.recordPcmBlob=null,this.recordType="pcm",this.encodingType="raw",this.connStatus="",this.btnStatus="UNDEFINED",this.resultText="",this.resultTextTemp="",this.APPID="23265215",this.API_SECRET="YzMzMWUzMmFkYjU2ZjE5OTc4M2Q1NjE1",this.API_KEY="8c58aa034946e1de30f2b145cf0cc3c3",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.onBeforeFinishTalk&&this.onBeforeFinishTalk(),this.recordStop(),setTimeout((()=>{this.onAfterFinishTalk&&this.onAfterFinishTalk()}),1e3)}async startTalk(t){this.onBeforeStartTalk&&this.onBeforeStartTalk(),event?.preventDefault(),await this.openWithPriviledge(),setTimeout((()=>{this.connectWebSocket()}),100),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(){this.recorder?.stop(((t,e)=>{this.iatWS.send(JSON.stringify({data:{status:2,format:"audio/L16;rate=16000",encoding:this.encodingType,audio:""}}));let i=(window.URL||webkitURL).createObjectURL(t);console.log(t,i,"时长:"+e+"ms"),this.recordPcmBlob=t,this.recorder.close(),this.recorder=null,console.log("localUrl",i)}),(t=>{console.log("录音失败:"+t),this.recorder.close(),this.recorder=null})),clearInterval(this.countdownInterval),this.changeBtnStatus("CLOSED")}playRecord(){this.playPCM(this.recordPcmBlob,44100)}playPCM(t,e){let i=new FileReader;i.onload=function(t){let i=t.target.result,o=pcmtoWav(i,e,1,16),r=new Audio;r.src=o,r.play()},i.readAsArrayBuffer(t)}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 r=0;r<i;r++){const i=r*e,s=i+e,a=t.slice(i,s);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,r,s)=>{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,r=(new Date).toUTCString(),s=`host: ${e}\ndate: ${r}\nGET /v2/iat HTTP/1.1`,a=CryptoJS.HmacSHA256(s,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=${r}&host=${e}`,t}toBase64(t){for(var e="",i=new Uint8Array(t),o=i.byteLength,r=0;r<o;r++)e+=String.fromCharCode(i[r]);return window.btoa(e)}countdown(){let t=60;this.connStatus=`录音中(${t}s)`,this.countdownInterval=setInterval((()=>{t-=1,console.log(t),t<=0?(clearInterval(this.countdownInterval),this.recordStop()):this.connStatus=`录音中(${t}s)`}),1e3)}changeBtnStatus(t){this.btnStatus=t,"CONNECTING"===t?(this.connStatus="建立连接中",this.resultText="",this.resultTextTemp=""):"OPEN"===t?this.countdown():"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",this.resultTextTemp,this.resultText)}0===e.code&&2===e.data.status&&this.iatWS.close(),0!==e.code&&(this.iatWS.close(),console.error(e))}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=>{this.renderResult(t.data)},this.iatWS.onerror=t=>{console.error("error",t),this.recordStop(),this.changeBtnStatus("CLOSED")},this.iatWS.onclose=t=>{console.error("close",t),this.recordStop(),this.changeBtnStatus("CLOSED")}}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.recordPcmBlob=null,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.requestPermission()}toggleRecord(){console.log(this.btnStatus),"UNDEFINED"===this.btnStatus||"CLOSED"===this.btnStatus?this.startTalk():"CONNECTING"!==this.btnStatus&&"OPEN"!==this.btnStatus||this.finishTalk()}finishTalk(){this.onBeforeFinishTalk&&this.onBeforeFinishTalk(),this.recordStop(),setTimeout((()=>{this.onAfterFinishTalk&&this.onAfterFinishTalk()}),1e3)}async startTalk(t){this.onBeforeStartTalk&&this.onBeforeStartTalk(),event?.preventDefault(),await this.openWithPriviledge(),setTimeout((()=>{this.connectWebSocket()}),100),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(){this.recorder?.stop(((t,e)=>{this.iatWS.send(JSON.stringify({data:{status:2,format:"audio/L16;rate=16000",encoding:this.encodingType,audio:""}}));let i=(window.URL||webkitURL).createObjectURL(t);console.log(t,i,"时长:"+e+"ms"),this.recordPcmBlob=t,this.recorder.close(),this.recorder=null,console.log("localUrl",i)}),(t=>{console.log("录音失败:"+t),this.recorder.close(),this.recorder=null})),clearInterval(this.countdownInterval),this.changeBtnStatus("CLOSED")}playRecord(){this.playPCM(this.recordPcmBlob,44100)}playPCM(t,e){let i=new FileReader;i.onload=function(t){let i=t.target.result,o=pcmtoWav(i,e,1,16),r=new Audio;r.src=o,r.play()},i.readAsArrayBuffer(t)}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 r=0;r<i;r++){const i=r*e,s=i+e,a=t.slice(i,s);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,r,s)=>{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,r=(new Date).toUTCString(),s=`host: ${e}\ndate: ${r}\nGET /v2/iat HTTP/1.1`,a=CryptoJS.HmacSHA256(s,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=${r}&host=${e}`,t}toBase64(t){for(var e="",i=new Uint8Array(t),o=i.byteLength,r=0;r<o;r++)e+=String.fromCharCode(i[r]);return window.btoa(e)}countdown(){let t=60;this.connStatus=`录音中(${t}s)`,this.countdownInterval=setInterval((()=>{t-=1,console.log(t),t<=0?(clearInterval(this.countdownInterval),this.recordStop()):this.connStatus=`录音中(${t}s)`}),1e3)}changeBtnStatus(t){this.btnStatus=t,"CONNECTING"===t?(this.connStatus="建立连接中",this.resultText="",this.resultTextTemp=""):"OPEN"===t?this.countdown():"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",this.resultTextTemp,this.resultText)}0===e.code&&2===e.data.status&&this.iatWS.close(),0!==e.code&&(this.iatWS.close(),console.error(e))}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=>{this.renderResult(t.data)},this.iatWS.onerror=t=>{console.error("error",t),this.recordStop(),this.changeBtnStatus("CLOSED")},this.iatWS.onclose=t=>{console.error("close",t),this.recordStop(),this.changeBtnStatus("CLOSED")}}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