具有自定义采样率的 RecordRTC 记录静音

RecordRTC with custom sample rate records silence

我正在尝试使用 RecordRTC.js 从麦克风录制音频并将其上传到 nancyfx 服务器。

出于测试目的,我只是尝试上传音频流并将其保存为 wav 文件。但是,我的要求是流以 22050Hz 的频率以 16 位保存。

问题是,当我使用标准配置(没有recordRtcOptions)录制文件时,我可以上传文件并保存它。当我在 recordrtc 中指定采样率时,输出文件只是无声。

代码的相关部分在 angularJS 服务中,如下所示:

app.service('AudioService', ['$window', '$http', function($window, $http) {

    var recordRtcOptions = {
        'sample-rate' : 22050
    };

    navigator.getUserMedia = (
        $window.navigator.getUserMedia ||
        $window.navigator.webkitGetUserMedia ||
        $window.navigator.mozGetUserMedia ||
        $window.navigator.msGetUserMedia)

    var _recordRTC = {};
    navigator.getUserMedia({ audio: true, video: false }, function (stream) {
        console.log('starting to initialize getUserMedia');
        console.log(recordRtcOptions);

        _recordRTC = RecordRTC(stream, recordRtcOptions);    

        console.log('Finished initializing getUserMedia');
    }, function (error) {
        console.log('Error initializing media stream: ' + error);  
    });

    var instance = {};

    instance.startRecording = function() {
        console.log('starting to record...');
        console.log('sample rate: ' + _recordRTC.sampleRate);
        _recordRTC.startRecording();
    };

    instance.stopRecording = function(uploadPath) {

        console.log('sample rate: ' + _recordRTC.sampleRate);


        _recordRTC.stopRecording(function(audioVideoMURL) {
            console.log('stopped recording...');
            console.log('recordrtc stop sample rate: ' + _recordRTC.sampleRate);

            $http({
                method : 'POST',
                url : uploadPath,
                data : _recordRTC.getBlob()
            }).success(function(data) {
                console.log('POST /audio Success');

            }).error(function() {
                console.log('POST /audio error'); 
            });
        });

    };

    return instance;

}]);

知道问题出在哪里吗?

要了解您需要研究的问题 RecordRTC.js

  • 首先函数 mergeProps 复制您提供的配置。
  • 函数reformatProps将"sample-rate"转换为属性"sampleRate".
  • StereoRecorder用于记录,内部使用StereoAudioRecorder,本质上与mattdiamond/Recorderjs.
  • 类似

当你查看它时,它不会将 sampleRate 作为输入,而是根据这些行

确定它
var Recorder = function(source, cfg){
    ...
    this.context = source.context;
    ...
        sampleRate: this.context.sampleRate, // --> this is the right way to provide sampleRate

在 RecordRTC 中,

var sampleRate = typeof config.sampleRate !== 'undefined' ? config.sampleRate : context.sampleRate || 44100;

长话短说,如果你注意到,这里也没有提供采样率,它需要 context.sampleRate 这是麦克风提供的采样率,所以仅仅改变配置中的采样率是不够的,因为所做的只是改变写入 .wav 文件的采样率值(可以检查函数 mergeLeftRightBuffers 进行确认)但数据仍会以原始采样率记录。

如果真的要修改sampleRate,可以看看