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}})
|