1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
| function HZRecorder (stream, config) { config = config || {} config.sampleBits = config.sampleBits || 16 config.sampleRate = config.sampleRate || 16000
let context = new (window.webkitAudioContext || window.AudioContext)() let audioInput = context.createMediaStreamSource(stream) let createScript = context.createScriptProcessor || context.createJavaScriptNode let recorder = createScript.apply(context, [4096, 1, 1])
let audioData = { size: 0 , buffer: [] , inputSampleRate: context.sampleRate , inputSampleBits: 16 , outputSampleRate: config.sampleRate , oututSampleBits: config.sampleBits , input: function (data) { this.buffer.push(new Float32Array(data)) this.size += data.length } , compress: function () { let data = new Float32Array(this.size) let offset = 0 for (let i = 0; i < this.buffer.length; i++) { data.set(this.buffer[i], offset) offset += this.buffer[i].length } let compression = parseInt(this.inputSampleRate / this.outputSampleRate) let length = data.length / compression let result = new Float32Array(length) let index = 0, j = 0 while (index < length) { result[index] = data[j] j += compression index++ } return result } , encodeWAV: function () { let sampleRate = Math.min(this.inputSampleRate, this.outputSampleRate) let sampleBits = Math.min(this.inputSampleBits, this.oututSampleBits) let bytes = this.compress() let dataLength = bytes.length * (sampleBits / 8) let buffer = new ArrayBuffer(44 + dataLength) let data = new DataView(buffer)
let channelCount = 1 let offset = 0
let writeString = function (str) { for (let i = 0; i < str.length; i++) { data.setUint8(offset + i, str.charCodeAt(i)) } }
writeString('RIFF') offset += 4 data.setUint32(offset, 36 + dataLength, true) offset += 4 writeString('WAVE') offset += 4 writeString('fmt ') offset += 4 data.setUint32(offset, 16, true) offset += 4 data.setUint16(offset, 1, true) offset += 2 data.setUint16(offset, channelCount, true) offset += 2 data.setUint32(offset, sampleRate, true) offset += 4 data.setUint32(offset, channelCount * sampleRate * (sampleBits / 8), true) offset += 4 data.setUint16(offset, channelCount * (sampleBits / 8), true) offset += 2 data.setUint16(offset, sampleBits, true) offset += 2 writeString('data') offset += 4 data.setUint32(offset, dataLength, true) offset += 4 if (sampleBits === 8) { for (let i = 0; i < bytes.length; i++, offset++) { let s = Math.max(-1, Math.min(1, bytes[i])) let val = s < 0 ? s * 0x8000 : s * 0x7FFF val = parseInt(255 / (65535 / (val + 32768))) data.setInt8(offset, val, true) } } else { for (let i = 0; i < bytes.length; i++, offset += 2) { let s = Math.max(-1, Math.min(1, bytes[i])) data.setInt16(offset, s < 0 ? s * 0x8000 : s * 0x7FFF, true) } }
return new Blob([data], {type: 'audio/wav'}) } } this.start = function () { audioInput.connect(recorder) recorder.connect(context.destination) }
this.stop = function () { recorder.disconnect() }
this.getBlob = function () { this.stop() console.log(audioData.encodeWAV()) return audioData.encodeWAV() }
this.play = function (audio) { let blob = this.getBlob() audio.src = window.URL.createObjectURL(this.getBlob()) }
this.upload = function () { return this.getBlob() }
recorder.onaudioprocess = function (e) { audioData.input(e.inputBuffer.getChannelData(0)) }
return this }
export { HZRecorder }
|