HTML5錄音控件


最近的項目又需要用到錄音,年前有過調研,再次翻出來使用,這里做一個記錄。

HTML5提供了錄音支持,因此可以方便使用HTML5來錄音,來實現錄音、語音識別等功能,語音開發必備。但是ES標准提供的API並不人性化,不方便使用,並且不提供保存為wav的功能,開發起來費勁啊!!

github尋找輪子,發現Recorder.js,基本上可以滿足需求了,良好的封裝,支持導出wav,但是存在:

  • wav采樣率不可調整
  • recorder創建麻煩,需要自己初始化getUserMedia
  • 無實時數據回調,不方便繪制波形
  • 。。。

改造輪子

創建recorder工具方法

提供創建recorder工具函數,封裝audio接口:

static createRecorder(callback,config){
        window.AudioContext = window.AudioContext || window.webkitAudioContext;
        window.URL = window.URL || window.webkitURL;
        navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;
        
        if (navigator.getUserMedia) {
            navigator.getUserMedia(
                { audio: true } //只啟用音頻
                , function (stream) {
                    var audio_context = new AudioContext;
                    var input = audio_context.createMediaStreamSource(stream);
                    var rec = new Recorder(input, config);
                    callback(rec);
                }
                , function (error) {
                    switch (error.code || error.name) {
                        case 'PERMISSION_DENIED':
                        case 'PermissionDeniedError':
                            throwError('用戶拒絕提供信息。');
                            break;
                        case 'NOT_SUPPORTED_ERROR':
                        case 'NotSupportedError':
                            throwError('瀏覽器不支持硬件設備。');
                            break;
                        case 'MANDATORY_UNSATISFIED_ERROR':
                        case 'MandatoryUnsatisfiedError':
                            throwError('無法發現指定的硬件設備。');
                            break;
                        default:
                            throwError('無法打開麥克風。異常信息:' + (error.code || error.name));
                            break;
                    }
                });
        } else {
            throwError('當前瀏覽器不支持錄音功能。'); return;
        }
    }

采樣率

H5錄制的默認是44k的,文件大,不方便傳輸,因此需要進行重新采樣,一般采用插值取點方法:

以下代碼主要來自stackoverflow:

             /**
             * 轉換采樣率
             * @param data
             * @param newSampleRate 目標采樣率
             * @param oldSampleRate 原始數據采樣率
             * @returns {any[]|Array}
             */
            function interpolateArray(data, newSampleRate, oldSampleRate) {
                var fitCount = Math.round(data.length * (newSampleRate / oldSampleRate));
                var newData = new Array();
                var springFactor = new Number((data.length - 1) / (fitCount - 1));
                newData[0] = data[0]; // for new allocation
                for (var i = 1; i < fitCount - 1; i++) {
                    var tmp = i * springFactor;
                    var before = new Number(Math.floor(tmp)).toFixed();
                    var after = new Number(Math.ceil(tmp)).toFixed();
                    var atPoint = tmp - before;
                    newData[i] = this.linearInterpolate(data[before], data[after], atPoint);
                }
                newData[fitCount - 1] = data[data.length - 1]; // for new allocation
                return newData;
            }

            function linearInterpolate(before, after, atPoint) {
                return before + (after - before) * atPoint;
            }

修改導出wav函數exportWAV,增加采樣率選項:

            /**
             * 導出wav
             * @param type
             * @param desiredSamplingRate 期望的采樣率
             */
            function exportWAV(type,desiredSamplingRate) {
                // 默認為16k
                desiredSamplingRate = desiredSamplingRate || 16000;
                var buffers = [];
                for (var channel = 0; channel < numChannels; channel++) {
                    var buffer = mergeBuffers(recBuffers[channel], recLength);
                    // 需要轉換采樣率
                    if (desiredSamplingRate!=sampleRate) {
                        // 插值去點
                        buffer = interpolateArray(buffer, desiredSamplingRate, sampleRate);
                    }
                    buffers.push(buffer);
                }
                var interleaved = numChannels === 2 ? interleave(buffers[0], buffers[1]) : buffers[0];
                var dataview = encodeWAV(interleaved,desiredSamplingRate);
                var audioBlob = new Blob([dataview], { type: type });
                self.postMessage({ command: 'exportWAV', data: audioBlob });
            }

實時錄音數據回調

為了方便繪制音量、波形圖,需要獲取到實時數據:

config新增一個回調函數onaudioprocess:

  config = {
        bufferLen: 4096,
        numChannels: 1, // 默認單聲道
        mimeType: 'audio/wav',
        onaudioprocess:null
    };

修改錄音數據處理函數:

        this.node.onaudioprocess = (e) => {
            if (!this.recording) return;
            var buffer = [];

            for (var channel = 0; channel < this.config.numChannels; channel++) {
                buffer.push(e.inputBuffer.getChannelData(channel));
            }

            // 發送給worker
            this.worker.postMessage({
                command: 'record',
                buffer: buffer
            });

            // 數據回調
            if(this.config.onaudioprocess){
                this.config.onaudioprocess(buffer[0]);
            }
        };

這樣,在創建recorder時,配置onaudioprocess就可以獲取到實時數據了

實時數據編碼

編碼計算耗時,需要放到worker執行:

接口函數新增encode,發送消息給worker,讓worker執行:

    encode(cb,buffer,sampleRate) {
        cb = cb || this.config.callback;
        if (!cb) throw new Error('Callback not set');
        this.callbacks.encode.push(cb);
        this.worker.postMessage({ command: 'encode',buffer:buffer,sampleRate:sampleRate});
    }

worker里新增encode函數,處理encode請求,完成后執行回調


 self.onmessage = function (e) {
                switch (e.data.command) {

                    case 'encode':
                        encode(e.data.buffer,e.data.sampleRate);
                        break;

                }
            };
			
    encode(cb,buffer,sampleRate) {
        cb = cb || this.config.callback;
        if (!cb) throw new Error('Callback not set');
        this.callbacks.encode.push(cb);
        this.worker.postMessage({ command: 'encode',buffer:buffer,sampleRate:sampleRate});
    }

wav上傳

增加一個上傳函數:

     exportWAVAndUpload(url, callback) {
        var _url = url;
        exportWAV(function(blob){
            var fd = new FormData();
            fd.append("audioData", blob);
            var xhr = new XMLHttpRequest();
            if (callback) {
                xhr.upload.addEventListener("progress", function (e) {
                    callback('uploading', e);
                }, false);
                xhr.addEventListener("load", function (e) {
                    callback('ok', e);
                }, false);
                xhr.addEventListener("error", function (e) {
                    callback('error', e);
                }, false);
                xhr.addEventListener("abort", function (e) {
                    callback('cancel', e);
                }, false);
            }
            xhr.open("POST", url);
            xhr.send(fd);
        })     
    }

完整代碼

=點擊下載

發現新輪子

今天再次看這個項目,發現這個項目已經不維護了,

Note: This repository is not being actively maintained due to lack of time and interest. If you maintain or know of a good fork, please let me know so I can direct future visitors to it. In the meantime, if this library isn't working, you can find a list of popular forks here: http://forked.yannick.io/mattdiamond/recorderjs.

作者推薦https://github.com/chris-rudmin/Recorderjs,提供更多的功能:

  • bitRate (optional) Specifies the target bitrate in bits/sec. The encoder selects an application-specific default when this is not specified.
  • bufferLength - (optional) The length of the buffer that the internal JavaScriptNode uses to capture the audio. Can be tweaked if experiencing performance issues. Defaults to 4096.
  • encoderApplication - (optional) Specifies the encoder application. Supported values are 2048 - Voice, 2049 - Full Band Audio, 2051 - Restricted Low Delay. Defaults to 2049.
  • encoderComplexity - (optional) Value between 0 and 10 which determines latency and processing for resampling. 0 is fastest with lowest complexity. 10 is slowest with highest complexity. The encoder selects a default when this is not specified.
  • encoderFrameSize (optional) Specifies the frame size in ms used for encoding. Defaults to 20.
  • encoderPath - (optional) Path to encoderWorker.min.js worker script. Defaults to encoderWorker.min.js
  • encoderSampleRate - (optional) Specifies the sample rate to encode at. Defaults to 48000. Supported values are 8000, 12000, 16000, 24000 or 48000.
  • leaveStreamOpen - (optional) Keep the stream around when trying to stop recording, so you can re-start without re-initStream. Defaults to false.
  • maxBuffersPerPage - (optional) Specifies the maximum number of buffers to use before generating an Ogg page. This can be used to lower the streaming latency. The lower the value the more overhead the ogg stream will incur. Defaults to 40.
  • monitorGain - (optional) Sets the gain of the monitoring output. Gain is an a-weighted value between 0 and 1. Defaults to 0
  • numberOfChannels - (optional) The number of channels to record. 1 = mono, 2 = stereo. Defaults to 1. Maximum 2 channels are supported.
  • originalSampleRateOverride - (optional) Override the ogg opus 'input sample rate' field. Google Speech API requires this field to be 16000.
  • resampleQuality - (optional) Value between 0 and 10 which determines latency and processing for resampling. 0 is fastest with lowest quality. 10 is slowest with highest quality. Defaults to 3.
  • streamPages - (optional) dataAvailable event will fire after each encoded page. Defaults to false.

推薦使用


作者:Jadepeng
出處:jqpeng的技術記事本--http://www.cnblogs.com/xiaoqi
您的支持是對博主最大的鼓勵,感謝您的認真閱讀。
本文版權歸作者所有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM