使用JavaScript播放原始音频 [英] Play raw audio with JavaScript

查看:65
本文介绍了使用JavaScript播放原始音频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的数字流

<代码> -0.00015259254737998596,-0.00009155552842799158,0.00009155552842799158,0.00021362956633198035,0.0003662221137119663,0.0003967406231879635,0.00024414807580797754,0.00012207403790398877,0.00012207403790398877,0.00012207403790398877,0.0003357036042359691,0.0003357036042359691,0.00018311105685598315,0.00003051850947599719,0,-0.00012207403790398877,0.00006103701895199438,0.00027466658528397473,0.0003967406231879635,0.0003967406231879635,0.0003967406231879635,0.0003967406231879635,0.0003967406231879635,0.0003662221137119663,0.0004882961516159551,0.0004577776421399579,0.00027466658528397473,0.00003051850947599719,-0.00027466658528397473 ....

应该表示音频流.我是从此处获取的,并且我已经通过网络传输了它们,现在我我尝试播放实际的声音,我从此处中摘录了一段代码,但我却得到了 Uncaught(在承诺中)DOMException:无法解码音频数据

Which supposedly represent an audio stream. I got them from here and I've transmitted them over the web, now I'm trying to play the actual sound and I got a snippet from here but I'm getting Uncaught (in promise) DOMException: Unable to decode audio data

我觉得我很想念我,只是希望它能像魔术一样工作,而事实并非如此.

I feel like I'm missing quite a lot I just expect this to work like magic and it just could not be the case..

我的代码

var ws = new WebSocket("ws://....");
ws.onmessage = function (event) {
  playByteArray(event.data);
}

var context = new AudioContext();
function playByteArray(byteArray) {

  var arrayBuffer = new ArrayBuffer(byteArray.length);
  var bufferView = new Uint8Array(arrayBuffer);
  for (var i = 0; i < byteArray.length; i++) {
    bufferView[i] = byteArray[i];
  }

  context.decodeAudioData(arrayBuffer, function (buffer) {
    buf = buffer;
    play();
  });
}

// Play the loaded file
function play() {
  // Create a source node from the buffer
  var source = context.createBufferSource();
  source.buffer = buf;
  // Connect to the final output node (the speakers)
  source.connect(context.destination);
  // Play immediately
  source.start(0);
}

广播部分

var ws = new WebSocket("ws://.....");
window.addEventListener("audioinput", function onAudioInput(evt) {
  if (ws) {
    ws.send(evt.data);
  }
}, false);

audioinput.start({
    bufferSize: 8192
});

推荐答案

您似乎在处理兼容的音频数据格式.链接到的代码用于播放字节数组,在这种情况下,您的音频数据应该是(更长)整数字符串,范围是0到255.

It doesn't look like you're dealing with compatible audio data formats. The code you linked to is for playing byte arrays, in which case your audio data should be a (much longer) string of integer numbers from 0 to 255.

您所拥有的是相当短的(随音频数据流逝)一串浮点数.我无法确定应该使用哪种音频格式,但是需要使用其他播放器.

What you've got is a fairly short (as audio data goes) string of floating point numbers. I can't tell what audio format that's supposed to be, but it would require a different player.

这篇关于使用JavaScript播放原始音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆