10 lines
6.1 KiB
JavaScript
10 lines
6.1 KiB
JavaScript
/**
|
|
* @Author: zhangtq 2452618307@qq.com
|
|
* @Date: 2024-11-18 13:33:30
|
|
* @LastEditors: zhangtq 2452618307@qq.com
|
|
* @LastEditTime: 2024-11-18 13:36:19
|
|
* @FilePath: static/js/qx/utility/talk.min.js
|
|
* @Description: 这是默认设置,可以在设置》工具》File Description中进行配置
|
|
*/
|
|
(function(window){window.URL=window.URL||window.webkitURL;navigator.getUserMedia=navigator.getUserMedia||navigator.webkitGetUserMedia||navigator.mozGetUserMedia||navigator.msGetUserMedia;var recorder={};var dtcWs={};var cb={};var Recorder=function(stream,config,name){config=config||{};config.sampleBits=config.sampleBits||16;config.sampleRate=config.sampleRate||(8000);var context=new AudioContext();var audioInput=context.createMediaStreamSource(stream);var recorder=context.createScriptProcessor(4096,1,1);var audioData={size:0,buffer:[],inputSampleRate:context.sampleRate,inputSampleBits:16,outputSampleRate:config.sampleRate,oututSampleBits:config.sampleBits,clear:function(){this.buffer=[];this.size=0},input:function(data){this.buffer.push(new Float32Array(data));this.size+=data.length},compress:function(){var data=new Float32Array(this.size);var offset=0;for(var i=0;i<this.buffer.length;i++){data.set(this.buffer[i],offset);offset+=this.buffer[i].length}var compression=parseInt(this.inputSampleRate/this.outputSampleRate);var length=data.length/compression;var result=new Float32Array(length);var index=0,j=0;while(index<length){result[index]=data[j];j+=compression;index++}return result},floatTo16BitPCM:function(input,output){for(var i=0;i<input.length;i++){var s=Math.max(-1,Math.min(1,input[i]));output[i]=(s<0?s*0x8000:s*0x7FFF)}},convertBuffer:function(){var bytes=this.compress();var data=new Float32Array(bytes);var out=new Int16Array(bytes.length);this.floatTo16BitPCM(data,out);return out},encodePCM:function(){var bytes=this.convertBuffer();var data=new DataView(bytes.buffer);return data},encodeG711A:function(){var smaples=this.convertBuffer();var g7111aBuf=alawmulaw.alaw.encode(smaples);return g7111aBuf},encodeG711aBuffer:function(){var smaples=this.convertBuffer();var g7111aBuf=alawmulaw.alaw.encode(smaples);var data=new DataView(g7111aBuf.buffer);return data}};this.start=function(){audioInput.connect(recorder);recorder.connect(context.destination)};this.stop=function(){audioInput.disconnect();recorder.disconnect()};this.getData=function(){return audioData.encodeG711A()};this.play=function(audio){audio.src=window.URL.createObjectURL(this.getBlob())};this.getBuffer=function(){return audioData.encodeG711aBuffer()};this.clear=function(){audioData.clear()};var that=this;recorder.onaudioprocess=function(e){audioData.input(e.inputBuffer.getChannelData(0));var data=that.getData();if(dtcWs[name]){dtcWs[name].send(data)};that.clear()}};Recorder.throwError=function(message){throw new function(){this.toString=function(){return message}}};Recorder.canRecording=(navigator.getUserMedia!=null);Recorder.get=function(callback,config,name){if(callback){if(navigator.getUserMedia){navigator.getUserMedia({audio:true},function(stream){var rec=new Recorder(stream,config,name);callback(rec)},function(error){switch(error.code||error.name){case'PERMISSION_DENIED':case'PermissionDeniedError':STRecorder.throwErr('用户拒绝提供信息。');break;case'NOT_SUPPORTED_ERROR':case'NotSupportedError':STRecorder.throwErr('浏览器不支持硬件设备。');break;case'MANDATORY_UNSATISFIED_ERROR':case'MandatoryUnsatisfiedError':STRecorder.throwErr('无法发现指定的硬件设备。');break;default:console.log('无法打开麦克风。异常信息:',error);STRecorder.throwErr('无法打开麦克风。异常信息:'+(error.code||error.name));break}});return}else if(navigator.mediaDevices&&navigator.mediaDevices.getUserMedia){navigator.mediaDevices.getUserMedia({audio:true}).then(function(stream){var rec=new Recorder(stream,config,name);callback(rec)}).catch(function(error){switch(error.code||error.name){case'PERMISSION_DENIED':case'PermissionDeniedError':Recorder.throwError('用户拒绝提供信息。');break;case'NOT_SUPPORTED_ERROR':case'NotSupportedError':Recorder.throwError('浏览器不支持硬件设备。');break;case'MANDATORY_UNSATISFIED_ERROR':case'MandatoryUnsatisfiedError':Recorder.throwError('无法发现指定的硬件设备。');break;default:Recorder.throwError('无法打开麦克风。异常信息:'+(error.code||error.name));break}});return}else{Recorder.throwError('当前浏览器不支持录音功能。');return}}};function dtcWebsocket(IP,Port,Token,queryToken,name){var http=location.protocol.indexOf("https")==-1?'ws://':'wss://';var ws=new WebSocket(http+location.host+'?token='+queryToken+'&IP='+IP+'&Port='+Port+'&Token='+Token+'&dtc=true');ws.onopen=function(evt){var message={type:"websocket_success",msg:"dtc websocket连接成功"};if(cb[name]){cb[name](message)};Recorder.get(function(rec){recorder[name]=rec;recorder[name].start()},{},name)};ws.onclose=function(evt){var message={type:"websocket_close",msg:"dtc websocket连接断开"};if(cb[name]){cb[name](message)};recorder[name].stop();recorder[name].clear();recorder[name]=null;dtcWs[name]=null;cb[name]=null};ws.onerror=function(evt){var message={type:"websocket_error",msg:evt};if(cb[name]){cb[name](message)};recorder[name].stop();recorder[name].clear();recorder[name]=null;dtcWs[name]=null;cb[name]=null};return ws}window.Recorder=Recorder;window.startTalk=function(token,puid,idx,name,callback){if(callback){cb[name]=callback}axios.post("./icvs2/audio/startTalk?token="+token,{puid:puid,idx:idx}).then(function(res){if(res.data.ErrorCode){var message={type:"startTalk_error",msg:res.data.message};if(cb[name]){cb[name](message)};return}else{dtcWs[name]=dtcWebsocket(res.data.IP,res.data.Port,res.data.Token,token,name);var message={type:"startTalk_success",msg:"打开对讲成功"};if(cb[name]){cb[name](message)};return}}).catch(function(err){console.log(err)})};window.stopTalk=function(name){if(recorder[name]){dtcWs[name].close();var message={type:"stopTalk_success",msg:"关闭对讲成功"};if(cb[name]){cb[name](message)};return}else{var message={type:"stopTalk_error",msg:"未找到对讲无法停止对讲"};if(cb[name]){cb[name](message)};return}}})(window);
|