HTML5 Web Audio API,从javax.sound移植并获得失真 [英] HTML5 Web Audio API, porting from javax.sound and getting distortion

查看:110
本文介绍了HTML5 Web Audio API,从javax.sound移植并获得失真的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要动态生成音频(从wav或mp3文件生成不是一个选项)。幸运的是,新的WebAudio API(FF4和Chrome 13)提供了这项功能。

I have a requirement to generate audio on the fly (generating from wav or mp3 files is not an option). Luckily the new WebAudio API (FF4 and Chrome 13) provides this functionality.

我有一些Java代码,我正在移植到Javascript,如下所示:

I have some Java code i'm porting to Javascript that looks like this:

byte[] buffer = new byte[]{ 56, -27, 88, -29, 88, -29, 88, -29 ............ };
AudioFormat af = new AudioFormat(44100, 16, 1, true, false);
SourceDataLine sdl = AudioSystem.getSourceDataLine(af);
sdl.open(af, 1470 * 4); //create 4 frame audio buffer
sdl.start();
sdl.write(buffer, 0, buffer.length);

我正在尝试使用Web Audio API,但它极度失真。这是我在JS中使用的代码:

I'm trying to get this working with the Web Audio API, but it is extremely distorted. Here is the code i'm using in JS:

var buffer = [ 56, -27, 88, -29, 88, -29, 88, -29 ............ ];
var ctx = new webkitAudioContext();
var src = ctx.createBufferSource();
src.buffer = ctx.createBuffer(1, buffer.length, 44100);
src.buffer.getChannelData(0).set(buffer);
src.connect(ctx.destination);
src.looping = false;
src.noteOn(0);

这是我正在测试的.java文件:
http://gwt-nes-port.googlecode.com/svn/wiki/webAudioTests /Main.java

Here is a .java file that i'm testing with: http://gwt-nes-port.googlecode.com/svn/wiki/webAudioTests/Main.java

这是我正在测试的.js文件:
http://gwt-nes-port.googlecode.com/svn/wiki/webAudioTests/Main.js

And here is the .js file that i'm testing with: http://gwt-nes-port.googlecode.com/svn/wiki/webAudioTests/Main.js

有关javax.sound和Web Audio如何工作之间差异的任何提示,以及导致我的JS代码失真的原因是什么?

Any tips on the differences between how javax.sound and Web Audio work, and what is causing the distortion in my JS code?

推荐答案

感谢非常乐于助人的Google Chrome团队,我想出了这一点。以下是他们所说的:

Thanks to the very helpful Google Chrome team, I figured this one out. Here is what they said:


嗨Brad,看起来你的
样本数据超出了有效的
范围。对于此API,满量程
浮点PCM音频数据应
在-1.0范围内 - > +1.0

Hi Brad, it looks like your sample-data is outside of the valid range. For this API, the full-scale floating-point PCM audio data should be within the range -1.0 -> +1.0

也许你的数据值是16位
缩放(-32768 - > +32767)。

Perhaps your data values are 16bit scaled (-32768 -> +32767).

所以当我创建我的字节数组时,我需要确保所有内容都表示为小数。所以不是这样:

So when I create my byte array, I need to make sure everything is represented as a decimal. So instead of this:

byte[] buffer = new byte[]{ 56, -27, 88, -29, 88, ............ };

我真的需要这样的东西:

I really needed something that looked like this:

byte[] buffer = new byte[]{ 0.023, -0.1, 0.125, -0.045, ............ };

所以在我的代码中我添加了一些逻辑来将16位缩放值转换为适当的范围,如下所示:

So in my code I just added some logic to convert the 16bit scaled values to the appropriate range like this:

for(var i=0;i<buffer.length;i++) {
   var b = buffer[i];
   b = (b>0)?b/32767:b/-32768;
   buffer[i] = b;
}

现在音频表示为小数,不再听起来像是扭曲的重音金属歌曲。

Now the audio is represented as a decimal and no longer sounds like a distorted heavy metal song.

这篇关于HTML5 Web Audio API,从javax.sound移植并获得失真的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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