您好我在浏览器上有一个工作语音录音机。它实现了navigator.getUserMedia和AudioContext 它以.wav格式记录声音 - 然后转换为.mp3并上传文件。
文件采样率设置为44100Hz,mp3输出重新采样为8000Hz。问题在于以此采样率记录大型音频文件所需的本地存储量。我通过转换为MP3缩小文件大小,采样率为8000。 我读到sampleRate是只读的。所以我想可以在实例化时设置采样率。我的问题是如何使用javascript更改录音机上的采样率?
NB。下面的代码已被修剪以显示感兴趣的部分。
try{
window.AudioContext = window.AudioContext || window.webkitAudioContext;
navigator.getMedia = ( navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia);
window.URL = window.URL || window.webkitURL;
audioCtx = new AudioContext;
var buffer = audioCtx.createBuffer(1, 8000, 8000);// this does not work!
}
catch (e)
{
console.log('unable to create a audiocontext');
exit;
}
navigator.getMedia({audio: true}, startUserMedia, function(e) {
console.log('No live audio input: ' + e);}
);
function startUserMedia(stream) {
var input = audioCtx.createMediaStreamSource(stream);
console.log('Media stream created.' );
console.log("input sample rate " +input.context.sampleRate);
input.connect(audioCtx.destination);
console.log('Input connected to audio context destination.');
recorder = new Recorder(input);
console.log('Recorder initialised.');
var mediaStreamSource = audioCtx.createMediaStreamSource(stream);
var analyser = audioCtx.createAnalyser();
analyser.smoothingTimeConstant = 0.3;
analyser.fftSize = 2048;
var javascriptNode = audioCtx.createScriptProcessor(2048, 1, 1);
mediaStreamSource.connect(analyser);
analyser.connect(javascriptNode);
javascriptNode.connect(audioCtx.destination);
javascriptNode.onaudioprocess = visualiser(analyser);
}