Files
Arbitrate-FrontendH5/node_modules/.cache/babel-loader/2babf1fd8150659771913124ede5c7fbafcedfd1bae89e604a2eacb580deb522.json
T

1 line
33 KiB
JSON
Raw Normal View History

2024-01-18 10:46:33 +08:00
{"ast":null,"code":"import \"core-js/modules/es.typed-array.to-reversed.js\";\nimport \"core-js/modules/es.typed-array.to-sorted.js\";\nimport \"core-js/modules/es.typed-array.with.js\";\nimport \"core-js/modules/web.url-search-params.delete.js\";\nimport \"core-js/modules/web.url-search-params.has.js\";\nimport \"core-js/modules/web.url-search-params.size.js\";\nimport \"core-js/modules/es.array.push.js\";\nexport function to16BitPCM(input) {\n const dataLength = input.length * (16 / 8);\n const dataBuffer = new ArrayBuffer(dataLength);\n const dataView = new DataView(dataBuffer);\n let offset = 0;\n for (let i = 0; i < input.length; i++, offset += 2) {\n const s = Math.max(-1, Math.min(1, input[i]));\n dataView.setInt16(offset, s < 0 ? s * 0x8000 : s * 0x7fff, true);\n }\n return dataView;\n}\nexport function to16kHz(audioData, sampleRate = 44100) {\n const data = new Float32Array(audioData);\n const fitCount = Math.round(data.length * (16000 / sampleRate));\n const newData = new Float32Array(fitCount);\n const springFactor = (data.length - 1) / (fitCount - 1);\n newData[0] = data[0];\n for (let i = 1; i < fitCount - 1; i++) {\n const tmp = i * springFactor;\n const before = Math.floor(tmp).toFixed();\n const after = Math.ceil(tmp).toFixed();\n const atPoint = tmp - before;\n newData[i] = data[before] + (data[after] - data[before]) * atPoint;\n }\n newData[fitCount - 1] = data[data.length - 1];\n return newData;\n}\nconst audioWorkletCode = `\n class MyProcessor extends AudioWorkletProcessor {\n constructor(options) {\n super(options);\n this.audioData = [];\n this.nextUpdateFrame = 40;\n this.sampleCount = 0;\n this.bitCount = 0;\n }\n \n get intervalInFrames() {\n return 40 / 1000 * sampleRate;\n }\n \n process(inputs) {\n // 去处理音频数据\n // eslint-disable-next-line no-undef\n if (inputs[0][0]) {\n const output = ${to16kHz}(inputs[0][0], sampleRate);\n this.sampleCount += 1;\n const audioData = ${to16BitPCM}(output);\n this.bitCount += 1;\n const data = [...new Int8Array(audioData.buffer)];\n this.audioData = this.audioData.concat(data);\n this.nextUpdateFrame -= inputs[0][0].length;\n if (this.nextUpdateFrame < 0) {\n this.nextUpdateFrame += this.intervalInFrames;\n this.port.postMessage({\n audioData: new Int8Array(this.audioData),\n sampleCount: this.sampleCount,\n bitCount: this.bitCount\n });\n this.audioData = [];\n }\n return true;\n }\n }\n }\n \n registerProcessor('my-processor', MyProcessor);\n `;\nconst audioWorkletBlobURL = window.URL.createObjectURL(new Blob([audioWorkletCode], {\n type: 'text/javascript'\n}));\nconst TAG = 'WebRecorder';\nnavigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;\nexport default class WebRecorder {\n constructor(requestId, isLog) {\n this.audioData = [];\n this.allAudioData = [];\n this.stream = null;\n this.audioContext = null;\n this.requestId = requestId;\n this.frameTime = [];\n this.frameCount = 0;\n this.sampleCount = 0;\n this.bitCount = 0;\n this.mediaStreamSource = null;\n this.isLog = isLog;\n }\n static isSupportMediaDevicesMedia() {\n return !!(navigator.getUserMedia || navigator.mediaDevices && navigator.mediaDevices.getUserMedia);\n }\n static isSupportUserMediaMedia() {\n return !!navigator.getUserMedia;\n }\n static isSupportAudioContext() {\n return typeof AudioContext !== 'undefined' || typeof webkitAudioContext !== 'undefined';\n }\n static isSupportMediaStreamSource(requestId, audioContext) {\n return typeof audioContext.createMediaStreamSource === 'function';\n }\n static isSupportAudioWorklet(audioContext) {\n return audioContext.audioWorklet && typeof audioContext.audioWorklet.addModule === 'function' && typeof AudioWorkletNode !== 'undefined';\n }