【发布时间】:2018-12-05 04:35:10
【问题描述】:
我在浏览器上播放 PCM 音频时遇到了一些问题。 PCM 音频来自具有 udp-protocol 的 android 设备,并在服务器上保存为 *.raw
我在 webaudioapi 的帮助下尝试播放这个保存的文件失败了。使用以下代码,给我播放一些带有白噪声的令人毛骨悚然的声音:
var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
audioCtx.sampleRate = 16000;
// Stereo
var channels = 1;
// Create an empty two second stereo buffer at the
// sample rate of the AudioContext
var frameCount = audioCtx.sampleRate * 10.0;
var myAudioBuffer = audioCtx.createBuffer(channels, frameCount, audioCtx.sampleRate);
var req = new XMLHttpRequest();
req.open('GET', "example.raw", false);
req.overrideMimeType('text\/plain; charset=x-user-defined');
req.send(null);
function play(){
for (var channel = 0; channel < channels; channel++) {
var nowBuffering = myAudioBuffer.getChannelData(channel,16,16000);
for (var i = 0; i < frameCount; i++) {
// audio needs to be in [-1.0; 1.0]
// for this reason I also tried to divide it by 32767
// as my pcm sample is in 16-Bit. It plays still the
// same creepy sound less noisy.
nowBuffering[i] = (req.responseText.charCodeAt(i) & 0xff;
}
}
// Get an AudioBufferSourceNode.
// This is the AudioNode to use when we want to play an AudioBuffer
var source = audioCtx.createBufferSource();
// set the buffer in the AudioBufferSourceNode
source.buffer = myAudioBuffer;
// connect the AudioBufferSourceNode to the
// destination so we can hear the sound
source.connect(audioCtx.destination);
// start the source playing
source.start();
}
它正在播放一种无法识别的声音,我不确定它是否正在播放我认为它必须做的 pcm 文件。
我想它必须对 pcm 文件做一些事情。 PCM 文件的采样率为 16 kHz,每个采样 16 位,并且只有一个通道或单通道。
这里有人遇到同样的问题,或者有人有解决我问题的建议吗?
几天以来我一直在寻找解决方案,并感谢任何帮助。
【问题讨论】:
标签: javascript audio wav web-audio-api