javascript - 使用 javascript 播放 PCM

标签 javascript audio wav web-audio-api

我在浏览器上播放 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 位,并且只有一个 channel 或单 channel 。

这里有没有人遇到同样的问题或者有没有人有解决我的问题的建议?

几天以来我一直在寻找解决方案并感谢任何帮助。

最佳答案

首先:

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 位最低有效位)

关于javascript - 使用 javascript 播放 PCM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43551473/

相关文章:

wav - 使用音频文件服务写入 32 位浮点 Wav 文件

javascript - jQuery一个div,两个url,用css做一个选择框

javascript - 排列/格式化 html 和 js 代码

javascript - 使用 JS 检查此场景中是否存在 html 标签

linux - 如何在 Linux 中播放 MIDI 输入

java - 通过 http Retrofit2 请求发送 WAV 文件 - Android Studio

javascript - 避免 * 来自 css 样式

javascript - Node.js 停止播放音频

android - 一个文件的声音?

java - 从音频文件中获取字节流?