HTML5 Web Audio API,从 javax.sound 移植并出现失真
我需要动态生成音频(不能从 wav 或 mp3 文件生成)。幸运的是,新的 WebAudio API(FF4 和 Chrome 13)提供了此功能。
我有一些 Java 代码正在移植到 Javascript,如下所示:
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 中使用的代码:
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
这是我正在测试的 .js 文件: http://gwt-nes-port.googlecode.com/svn /wiki/webAudioTests/Main.js
关于 javax.sound 和 Web Audio 工作方式之间的差异以及导致我的 JS 代码失真的原因有什么提示吗?
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.
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);
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);
Here is a .java file that i'm testing with:
http://gwt-nes-port.googlecode.com/svn/wiki/webAudioTests/Main.java
And here is the .js file that i'm testing with:
http://gwt-nes-port.googlecode.com/svn/wiki/webAudioTests/Main.js
Any tips on the differences between how javax.sound and Web Audio work, and what is causing the distortion in my JS code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
感谢非常乐于助人的 Google Chrome 团队,我解决了这个问题。他们是这么说的:
因此,当我创建字节数组时,我需要确保所有内容都表示为十进制。所以,
我真的需要这样的东西:
所以在我的代码中,我只是添加了一些逻辑来将 16 位缩放值转换为适当的范围,如下所示:
现在音频表示为十进制,不再听起来像扭曲的重金属歌曲。
Thanks to the very helpful Google Chrome team, I figured this one out. Here is what they said:
So when I create my byte array, I need to make sure everything is represented as a decimal. So instead of this:
I really needed something that looked like this:
So in my code I just added some logic to convert the 16bit scaled values to the appropriate range like this:
Now the audio is represented as a decimal and no longer sounds like a distorted heavy metal song.
您可以在没有 Web Audio API 的情况下执行此操作,因为您拥有示例数据。
动态生成 .wav 文件并使用类型数组。
幻灯片 23:http://davidflanagan.com/Talks/jsconf11/BytesAndBlobs.html
You can do this without the Web Audio API since you have the sample data.
Generate a .wav file on the fly and use type arrays.
Slide 23: http://davidflanagan.com/Talks/jsconf11/BytesAndBlobs.html