【问题标题】:webkitAudioContext Playback of base64 WAVwebkitAudioContext 播放base64 WAV
【发布时间】:2012-10-25 21:36:17
【问题描述】:

我有一个 wav 文件,其中包含:ffmpeg -i my.wav

给出输出:

output:   Duration: 00:00:12.63, bitrate: 352 kb/s
    Stream #0.0: Audio: pcm_s16le, 22050 Hz, 1 channels, s16, 352 kb/s

这在 JavaScript 中存储为 base64 字符串,作为下面列出的 base64 变量加载到内存中。

代码如下:

byte_str = Base64.decode(base64)
buffer = new ArrayBuffer(byte_str.length*2)
buffer_view = new Uint16Array(buffer)
/* this is coffeescript */
for i in [0..byte_str.length]
   buffer_view[i] = byte_str.charCodeAt(i)
console.log(byte_str.length)
console.log(buffer_view)
console.log(buffer_view.buffer)
context = new webkitAudioContext()
context.decodeAudioData(buffer,function (buffer) {
    /* never gets hit */
    source = @context.createBufferSource()
    source.buffer = buffer
    source.connect(@context.destination)
    source.noteOn(0)
});

输出:

108185
[82, 73, 70, 70, 36, 128, 87, 65, 86, 69, 102, 109, 116, 32, 16, 0, 0, 0, 1, 0, 1, 0, 34, 86, 0, 0, 68, 49152, 2, 0, 16, 0, 100, 97, 116, 97, 0, 128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,....]
ArrayBuffer {byteLength: 216370} 

如果您查看此演示文稿,它有一个 WAV 标头: http://html5-demos.appspot.com/static/html5-whats-new/template/index.html#29

var header = new Uint8Array([
    0x52,0x49,0x46,0x46, // "RIFF"
    0, 0, 0, 0,          // put total size here
    0x57,0x41,0x56,0x45,
    ...
]);

如果您将第一个值转换为十六进制,这与我的相同,但在我的情况下,回调永远不会为 decodeAudioData 触发。有什么想法吗?

【问题讨论】:

    标签: javascript google-chrome webkit webkitaudiocontext


    【解决方案1】:

    事实证明这是使用此库的编码问题:http://www.webtoolkit.info/javascript-base64.html 我删除了 _utf8_decode 步骤,效果很好!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-19
      • 2013-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多