与 javascript 一起玩 PCM
Play PCM with javascript
我在浏览器上播放 PCM 音频时遇到一些问题。 PCM 音频来自具有 udp 协议的 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 位,并且只有一个通道或单通道。
这里有人遇到同样的问题,或者有人对解决我的问题有建议吗?
几天前我一直在寻找解决方案,感谢任何帮助。
首先:
audioCtx.sampleRate = 16000;
不起作用。您无法修改 audioCtx.sampleRate。相反,您需要执行以下操作:
var frameCount = req.responseText.length / 2;
var myAudioBuffer = audioCtx.createBuffer(channels, frameCount, 16000);
因为您的文件是 16 位的,所以它的字节长度是您需要的帧数的两倍。
(req.responseText.charCodeAt(i) & 0xff)
将产生 0 到 255 之间的值,表示单个 8 位字节。你需要 16 位。
您需要知道样本的字节顺序,每次处理两个字节
对于小端(LSB 在前):
var word = (req.responseText.charCodeAt(i * 2) & 0xff) + ((req.responseText.charCodeAt(i * 2 + 1) & 0xff) << 8);
对于大端(MSB 在前):
var unsignedWord = ((req.responseText.charCodeAt(i * 2) & 0xff) << 8) + (req.responseText.charCodeAt(i * 2 + 1) & 0xff);
这将产生一个介于 0 和 65535 之间的数字,表示一个无符号的 16 位整数。为了转换为有符号整数,您需要执行以下操作(将 X 替换为上面的代码)
var signedWord = (unsignedWord + 32768) % 65536 - 32768;
这将产生一个介于 -32768 和 32767 之间的数字,然后您可以将其除以 32768.0 以获得您想要的结果。
nowBuffering[i] = signedWord / 32768.0;
编辑:工作示例 https://o.lgm.cl/example.html(16 位 LSB)
@Locolois
我试过你的 suggestion/solution 并得到了一些清晰的声音,遗憾的是听起来仍然不像原来的那样。它每秒也有白噪声,这没有我的解决方案那么令人毛骨悚然 :D 但我仍然没有听到我录制的声音。我不确定 android.audiorecord 导出的 pcm 是大端还是小端,所以我尝试了两种方法。但是我通过使用你为 big endian 提出的建议听到的声音对我来说听起来比 little endian 版本更正确。 little endian 版本也充满了白噪声。
您的解释是否正确实施?:
for (var i = 0; i < frameCount; i+=2) {
var msbFirst = (req.responseText.charCodeAt(i) & 0xff) + (req.responseText.charCodeAt(i + 1) & 0xff) << 8;
var msbSigned = (msbFirst + 32768) % 65536 - 32768;
nowBuffering[i] = msbSigned / 65536.0;
}
我在浏览器上播放 PCM 音频时遇到一些问题。 PCM 音频来自具有 udp 协议的 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 位,并且只有一个通道或单通道。
这里有人遇到同样的问题,或者有人对解决我的问题有建议吗?
几天前我一直在寻找解决方案,感谢任何帮助。
首先:
audioCtx.sampleRate = 16000;
不起作用。您无法修改 audioCtx.sampleRate。相反,您需要执行以下操作:
var frameCount = req.responseText.length / 2;
var myAudioBuffer = audioCtx.createBuffer(channels, frameCount, 16000);
因为您的文件是 16 位的,所以它的字节长度是您需要的帧数的两倍。
(req.responseText.charCodeAt(i) & 0xff)
将产生 0 到 255 之间的值,表示单个 8 位字节。你需要 16 位。
您需要知道样本的字节顺序,每次处理两个字节
对于小端(LSB 在前):
var word = (req.responseText.charCodeAt(i * 2) & 0xff) + ((req.responseText.charCodeAt(i * 2 + 1) & 0xff) << 8);
对于大端(MSB 在前):
var unsignedWord = ((req.responseText.charCodeAt(i * 2) & 0xff) << 8) + (req.responseText.charCodeAt(i * 2 + 1) & 0xff);
这将产生一个介于 0 和 65535 之间的数字,表示一个无符号的 16 位整数。为了转换为有符号整数,您需要执行以下操作(将 X 替换为上面的代码)
var signedWord = (unsignedWord + 32768) % 65536 - 32768;
这将产生一个介于 -32768 和 32767 之间的数字,然后您可以将其除以 32768.0 以获得您想要的结果。
nowBuffering[i] = signedWord / 32768.0;
编辑:工作示例 https://o.lgm.cl/example.html(16 位 LSB)
@Locolois
我试过你的 suggestion/solution 并得到了一些清晰的声音,遗憾的是听起来仍然不像原来的那样。它每秒也有白噪声,这没有我的解决方案那么令人毛骨悚然 :D 但我仍然没有听到我录制的声音。我不确定 android.audiorecord 导出的 pcm 是大端还是小端,所以我尝试了两种方法。但是我通过使用你为 big endian 提出的建议听到的声音对我来说听起来比 little endian 版本更正确。 little endian 版本也充满了白噪声。
您的解释是否正确实施?:
for (var i = 0; i < frameCount; i+=2) {
var msbFirst = (req.responseText.charCodeAt(i) & 0xff) + (req.responseText.charCodeAt(i + 1) & 0xff) << 8;
var msbSigned = (msbFirst + 32768) % 65536 - 32768;
nowBuffering[i] = msbSigned / 65536.0;
}