我试图通过getUserMedia录制一个48000Hz录音.但没有运气.返回的音频MediaStream返回44100Hz.我如何设置为48000Hz?
以下是我的代码片段:
var startUsermedia = this.startUsermedia; navigator.getUserMedia({ audio: true,//sampleRate: 48000 },startUsermedia,function (e) { console.log('No live audio input: ' + e); });
startUsermedia功能:
startUsermedia: function (stream) { var input = audio_context.createMediaStreamSource(stream); console.log('Media stream created.'); // Uncomment if you want the audio to Feedback directly //input.connect(audio_context.destination); //__log('Input connected to audio context destination.'); recorder = new Recorder(input); console.log('Recorder initialised.'); },
我尝试改变AudioContext的propertyRate属性,但没有运气.
我如何将sampleRate更改为48000Hz?
解决方法
据我所知,在音频环境中没有办法改变采样率.采样率通常是记录设备的采样率,并保持这种方式.所以你不能写这样的东西:
var input = audio_context.createMediaStreamSource(stream); var resampler = new Resampler(44100,48000); input.connect(resampler); resampler.connect(audio_context.destination);
但是,如果要使用音频流,请对其进行重新采样,然后将其发送到后端(或者将其与Web Audio API之外的其他设备一起发送),则可以使用外部采样率转换器(例如https://github.com/taisel/XAudioJS/blob/master/resampler.js).
var resampler = new Resampler(44100,48000,1,2229); function startUsermedia(stream) { var input = audio_context.createMediaStreamSource(stream); console.log('Media stream created.'); recorder = audio_context.createScriptProcessor(2048); recorder.onaudioprocess = recorderProcess; recorder.connect(audio_context.destination); } function recorderProcess(e) { var buffer = e.inputBuffer.getChannelData(0); var resampled = resampler.resampler(buffer); //--> do sth with the resampled data for instance send to server }