美文网首页
audio相关

audio相关

作者: hijiang | 来源:发表于2020-02-15 11:04 被阅读0次

    AudioContext.decodeAudioData:
    得到:
    AudioBuffer {length: 9853020, duration: 205.27125, sampleRate: 48000, numberOfChannels: 2}
    length: 9853020
    duration: 205.27125
    sampleRate: 48000
    numberOfChannels: 2

    document.getElementById("profile_mp3").onchange = function() {
                                var o = new FileReader;
                                o.onload = function(e) {
                                    //这个e.target.result是AudioBuffer
                                }
                                o.readAsArrayBuffer($("#profile_mp3").get(0).files[0]);
                            };
    

    输入给audiocontext解码:

    audioContext = new AudioContext;
                                    audioContext.decodeAudioData(e.target.result, (t) => {
                                       console.log("ttttt=", t);
                                    });
    

    得到:
    AudioBuffer {length: 9853020, duration: 205.27125, sampleRate: 48000, numberOfChannels: 2}
    此时,audioContext也会对象会存储这些信息:

    AudioContext {baseLatency: 0.01, destination: AudioDestinationNode, currentTime: 1.2693333333333334, sampleRate: 48000, listener: AudioListener, …}
    baseLatency: 0.01
    destination: AudioDestinationNode {maxChannelCount: 2, context: AudioContext, numberOfInputs: 1, numberOfOutputs: 0, channelCount: 2, …}
    currentTime: 5.770666666666667
    sampleRate: 48000
    listener: AudioListener {positionX: AudioParam, positionY: AudioParam, positionZ: AudioParam, forwardX: AudioParam, forwardY: AudioParam, …}
    state: "running"
    onstatechange: null
    audioWorklet: AudioWorklet {}
    __proto__: AudioContext
    

    当playAudioSource时,audioSource对象是一个AudioBufferSourceNode,这个是通过audioContext.createBufferSource来创建的

    实现混音的代码:

    selectMp32:function() {
                            var that = this;
                            document.getElementById("profile_mp32").onchange = function() {
                                var o = new FileReader;
                                o.onload = function(e) {
                                    const audioBuffer = e.target.result;
                                    that.audioContext.decodeAudioData(e.target.result, (t) => {
                                        audioSource = that.audioContext.createBufferSource();
                                        audioSource.buffer = t;
                                        audioSource.loop = true;
    
                                        audioSource.connect(that.audioContext.destination);
                                        audioSource.start(0, 0);
                                    });
                                }
                                o.readAsArrayBuffer($("#profile_mp32").get(0).files[0]);
                            };
                            $("#profile_mp32").click();
                        },
                        selectMp3:function() {
                            var that = this;
                            document.getElementById("profile_mp3").onchange = function() {
                                var o = new FileReader;
                                o.onload = function(e) {
                                    const audioBuffer = e.target.result;
    
                                    that.audioContext.decodeAudioData(e.target.result, (t) => {
                                        audioSource = that.audioContext.createBufferSource();
                                        audioSource.buffer = t;
                                        audioSource.loop = true;
                                        var analyserNode = that.audioContext.createAnalyser();
                                        analyserNode.fftSize = 2048;
                                        var gainNode = that.audioContext.createGain();
                                        // var audioStream = audioContext.create();
    
                                        audioSource.connect(that.audioContext.destination);
                                        audioSource.start(0, 0);
                                        console.log("audioStream=", audioStream);
                                        var o = audioStream.stream.getTracks()[0];
                                        console.log("tracks=", o);
                                    });
                                }
                                o.readAsArrayBuffer($("#profile_mp3").get(0).files[0]);
                            };
                            $("#profile_mp3").click();
                        },
    

    相关文章

      网友评论

          本文标题:audio相关

          本文链接:https://www.haomeiwen.com/subject/drodfhtx.html