UNPKG

@jxstjh/jhvideo

Version:

HTML5 jhvideo base on MPEG2-TS Stream Player

175 lines 7.12 kB
/** * pcm-processor */ import { alaw, mulaw } from 'alawmulaw'; var PCMResampler = /** @class */ (function () { function PCMResampler(options, output, root) { if (root === void 0) { root = null; } this.tempSamples = []; this.options = options; this.output = output; this.root = root; // this.root.output({ // type: 'created', // ...this.options // }) } PCMResampler.prototype.input = function (inputSamples) { var _a = this.options, fromSampleRate = _a.fromSampleRate, audioSample = _a.audioSample, audioBitwidth = _a.audioBitwidth; var dataLen = audioSample * 10 * 2 / 1000; var compression = fromSampleRate / audioSample; var length = Math.floor(inputSamples.length / compression); var samples = new Float32Array(length); var index = 0; var inputIndex = 0; while (index < length) { samples[index] = inputSamples[inputIndex]; inputIndex += compression; // 每次都跳过4个数据 index++; } var sampleLen = samples.length; var dataView = null; if (audioBitwidth === 16) { // 采样精度为16 var buffer = new ArrayBuffer(sampleLen * 2); dataView = new DataView(buffer); var offset = 0; for (var i = 0; i < samples.length; i++, offset += 2) { var s = Math.max(-1, Math.min(1, samples[i])); dataView.setInt16(offset, s < 0 ? s * 0x8000 : s * 0x7fff, true); } } else if (audioBitwidth === 8) { // 采样精度为8 var buffer = new ArrayBuffer(sampleLen); dataView = new DataView(buffer); var offset = 0; for (var i = 0; i < samples.length; i++, offset++) { var s = Math.max(-1, Math.min(1, samples[i])); var val = s < 0 ? s * 0x8000 : s * 0x7fff; val = parseInt(255 / (65535 / (val + 32768)), 10); dataView.setInt8(offset, val, true); } } if (dataView && dataView.buffer) { var bufferAudioPCM = new Uint8Array(dataView.buffer); var iSampleIndex = this.tempSamples.length; for (var i = 0, iBufferLen = bufferAudioPCM.byteLength; i < iBufferLen; i++) { this.tempSamples[iSampleIndex++] = bufferAudioPCM[i]; if (this.tempSamples.length === dataLen * 2) { // console.log(this.tempSamples) this.output(new Uint8Array(this.tempSamples.slice(0))); this.tempSamples = []; iSampleIndex = 0; } } } }; return PCMResampler; }()); var PCMEncoder = /** @class */ (function () { function PCMEncoder(options, output, root) { if (root === void 0) { root = null; } var _this = this; this.options = options; this.output = output; this.root = root; var _a = this.options, audioType = _a.audioType, fromSampleRate = _a.fromSampleRate, audioSample = _a.audioSample, audioChannel = _a.audioChannel, audioBitrate = _a.audioBitrate; // this.output({ audioType, fromSampleRate, audioSample, audioChannel, audioBitrate }) switch (audioType) { case 'g711a': this.encoder = alaw; this.encode = function (pcmSamples) { if (_this.options.bFileWrite) { _this.output({ type: 'pcm', pcmSamples: pcmSamples }); } var alawSamples = alaw.encode(new Int16Array(pcmSamples.buffer)); _this.output(alawSamples.buffer); }; break; case 'g711u': this.encoder = mulaw; this.encode = function (pcmSamples) { var mulawSamples = mulaw.encode(new Int16Array(pcmSamples.buffer)); _this.output(mulawSamples.buffer); }; break; case 'aac': this.encoder = new AudioEncoder({ output: function (chunk) { var accSamples = new ArrayBuffer(chunk.byteLength); chunk.copyTo(accSamples); _this.output(accSamples); }, error: function (err) { console.error(err); _this.encoder = null; } }); this.encoder.configure({ codec: 'mp4a.40.2', // aac sampleRate: audioSample, numberOfChannels: audioChannel, bitrate: audioBitrate, }); this.encode = function (pcmSamples) { var audioData = new AudioData({ format: 'f32-planar', sampleRate: fromSampleRate, numberOfChannels: audioChannel, numberOfFrames: pcmSamples.length / audioChannel, timestamp: 0, data: pcmSamples }); _this.encoder.encode(audioData); }; break; default: // mp3 // this.encoder = new lamejs.Mp3Encoder(audioChannel, audioSampleRate, audioBitrate / 1000) // this.encode = (inputBuffer) => { // let pcmSamples = inputBuffer.getChannelData(0) // float32array // let mp3Samples = this.encoder.encodeBuffer(new Int16Array(pcmSamples.buffer)) // console.log('encode func', inputBuffer, pcmSamples, new Int16Array(pcmSamples.buffer), mp3Samples) // this.eventsCenter.emit('stream.output', mp3Samples.buffer) // } break; } } /** * * @param {float32array} pcmSamples */ PCMEncoder.prototype.input = function (pcmSamples) { this.encode(pcmSamples); }; return PCMEncoder; }()); var PCMProcessor = /** @class */ (function () { function PCMProcessor(options, output) { var _this = this; this.options = options; this.output = output; this.encoder = new PCMEncoder(options, function (samples) { // console.log(samples) _this.output(samples); }, this); this.resampler = new PCMResampler(options, function (samples) { // this.output(samples) _this.encoder.input(samples); }, this); } /** * * @param {Float32Array} inputSamples */ PCMProcessor.prototype.input = function (inputSamples) { this.resampler.input(inputSamples); }; return PCMProcessor; }()); export default PCMProcessor; //# sourceMappingURL=pcm-processor.js.map