Skip to content

Is there an example how to play/stream audio to Web Audio API ? #99

@animator013

Description

@animator013

I am having very hard times to get this working.

I tried to convert the pcmdata do Float32Array but I always get audio with cracks. It is possible to hear and recognize voice but it's not clear and cracks a lot, unusable.

Can you please help me with this ?

Here is my code:

        let audio = new Int16Array(pcmData.length / Int16Array.BYTES_PER_ELEMENT);
        let length = audio.length;
        for (let i = 0; i < length; ++i) {
            audio[i] = pcmData.readInt16LE(i * Int16Array.BYTES_PER_ELEMENT);
        }
        let right = new Float32Array(audio.length);
        let channelCounter = 0;
        for (let i=0; i < audio.length; i++) {
            let normalizedRight = audio[i] / 32768;
            right[channelCounter] = normalizedRight;

            channelCounter++;
        }

        let source = this.audioContext.createBufferSource();
        let audioBuffer = this.audioContext.createBuffer(1, right.length, 48000);
        audioBuffer.getChannelData(0).set(right);
        source.buffer = audioBuffer;
        source.connect(this.audioContext.destination);
        if(this.startTime == 0) this.startTime = this.audioContext.currentTime + (audioBuffer.length / audioBuffer.sampleRate)/2;
        source.start(this.startTime);
        this.startTime += audioBuffer.length / audioBuffer.sampleRate;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions