base64ToArrayBuffer错误:无法在“窗口"上执行"atob".(网络音频API) [英] base64ToArrayBuffer Error: Failed To Execute 'atob' on 'Window'. (Web Audio API)

查看:50
本文介绍了base64ToArrayBuffer错误:无法在“窗口"上执行"atob".(网络音频API)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我对Web Audio API还是很陌生,只是四天前才听说(尽管从那时起,我已经投入了大约50个小时的研究和实验时间).我也是javascript新手.

So I'm pretty new to the Web Audio API, having only heard about it 4 days ago (though since then I've put in probably about 50 hours of research and experimentation with it). I'm also more of a novice at javascript.

情况:我正在尝试开发一个脚本,该脚本将从API(编码为base64字符串)中获取Google的TTS返回值,并将其传输到arrayBuffer以在Web Audio API中使用,以便我可以发送它通过某些节点.

Situation: I'm trying to develop a script that will take Google's TTS return from the API (which is encoded as a base64 string) and transfer it to an arrayBuffer for use in the Web Audio API so that I can send it through some of the nodes.

我已经从集成到我的网站的Google TTS API中获得了回报.当我在计算机上的测试服务器上测试脚本时,我可以听到audioContext以轻微静态的形式被激活.但是,我似乎无法诊断或修复的base64ToArray字符串之一弹出错误.

I've already got the return from the Google TTS API integrated into my website. When I test the script on my test-server on my computer, I can hear the audioContext activate in the form of slight static. However, there is an error popping up with one of my base64ToArray strings that I can't seem to diagnose or fix.

这是错误:

未捕获的DOMException:无法在"Window"上执行"atob":要解码的字符串未正确编码.在base64ToArrayBuffer(16:31)在(10:23)

这是代码.为了这篇文章的缘故,我只保留了base64字符串的一部分.另外请注意:我尝试通过包括数据URI以及仅base64String来格式化base64Strings的方法-两种方法均无效.

And here's the code. For the sake of this post, I've only kept a portion of the base64 strings. Also a side note: I've tried formatting the base64Strings by including the data URI, as well just the base64String - neither method has worked.

任何帮助都会得到感谢!

Any help would be apprciated!

var isPlaying = false;
var audioContext;
var avaVoice;
var gainNode;
var reverbNode;
var pannerNode;
var updateTime = 200;
var global = global || window;

var avaVoiceDataUri = base64ToArrayBuffer('UklGRuC6DABXQVZFZm10IBAAAAABAAIAAHcBAADKCAAGABgAYmV4dFoCAABNdWx0aW1lZGlhIGludGVyZmFjZSBzb3VuZCBlZmZlY3QuIFVJIEh1ZCBtb2Rlcm4gYW5kIFNjaWVudGlmaWMuAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA');

var impulseResponse = base64ToArrayBuffer("data:audio/wav;base64,UklGRhCjBwBXQVZFZm10IBAAAAABAAIARKwAAJgJBAAGABgAZGF0YeyiBwAAAAAAAADRBwDKBgCwAQBA+/8y6v/yuP8JAQA2R/");


function base64ToArrayBuffer(base64) {
    var binaryString = window.atob(base64);
    var len = binaryString.length;
    var bytes = new Uint8Array(len);
    for (var i = 0; i < len; i++)        {
        bytes[i] = binaryString.charCodeAt(i);
    }
    return bytes.buffer;
}



function initSound() {
    audioContext = new AudioContext();
    

    avaVoice.crossOrigin = "anonymous";
    avaVoice.loop = false;
    var source = audioContext.createMediaElementSource(avaVoice);

    gainNode = audioContext.createGain();
    gainNode.gain.value = 0.5;

    reverbNode = audioContext.createConvolver();
    var reverbSoundArrayBuffer = base64ToArrayBuffer(impulseResponse);
    audioContext.decodeAudioData(reverbSoundArrayBuffer,
        function(buffer) {
            reverbNode.buffer = buffer;
        },
        function(e) {
            alert("Error when decoding audio data" + e.err);
    });

    // Connect the audio chain together
    source.connect(gainNode);
    gainNode.connect(reverbNode);
    reverbNode.connect(audioContext.destination);

    avaVoice.play();
  var intervalId = setInterval(updateTime);
}




window.addEventListener("click", function(){
    isPlaying = true
});



initSound();

推荐答案

您的base64无效.

Your base64 is invalid.

在我看来,就像您最后缺少一个 A 一样.如果添加了它,它就可以正常解码.

Looks to me like you're missing an A on the end. If you add it, it decodes just fine.

还请注意,如果您使用的是 atob ,则不会包含数据URI部分.

Also note that if you're using atob, you won't include the data URI portion.

如果确实有一个数据URI,则应使用Fetch API来获取一个Blob,这样就包括了MIME类型.然后,您可以直接从中解码音频数据.另请参阅: https://stackoverflow.com/a/62677424/362536

If you do have a data URI to begin with, you should use the Fetch API to get a Blob, so that way the MIME type is included. You can then decode audio data directly from it. See also: https://stackoverflow.com/a/62677424/362536

这篇关于base64ToArrayBuffer错误:无法在“窗口"上执行"atob".(网络音频API)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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