Giter VIP home page Giter VIP logo

Comments (7)

samirkumardas avatar samirkumardas commented on June 9, 2024

It expects a single byte array. In fact, this player divides the provided buffer into multiple channels eventually https://github.com/samirkumardas/pcm-player/blob/master/pcm-player.js#L88.

You can modify the flash and feed methods as you have already separate data for the left and right channels.

from pcm-player.

kboniadi avatar kboniadi commented on June 9, 2024

Thanks for getting back to me. I modified the code a little to accept two arrays instead of one, and the audio plays. However, there is a strange buzzing sound that occurs when audio is playing. The buzzing sounds doesn't occur when nothing is being played though. Do you have any idea what this could be?? This is the code I have right now in pcm-player.js:

export function PCMPlayer(option) {
    this.init(option);
}

PCMPlayer.prototype.init = function(option) {
    var defaults = {
        encoding: '16bitInt',
        channels: 1,
        sampleRate: 8000,
        flushingTime: 1000
    };
    this.option = Object.assign({}, defaults, option);
    // this.samples = new Float32Array();
    // this.flush = this.flush.bind(this);
    // this.interval = setInterval(this.flush, this.option.flushingTime);
    // this.maxValue = this.getMaxValue();
    // this.typedArray = this.getTypedArray();
    this.createContext();
};

// PCMPlayer.prototype.getMaxValue = function () {
//     var encodings = {
//         '8bitInt': 128,
//         '16bitInt': 32768,
//         '32bitInt': 2147483648,
//         '32bitFloat': 1
//     }

//     return encodings[this.option.encoding] ? encodings[this.option.encoding] : encodings['16bitInt'];
// };

// PCMPlayer.prototype.getTypedArray = function () {
//     var typedArrays = {
//         '8bitInt': Int8Array,
//         '16bitInt': Int16Array,
//         '32bitInt': Int32Array,
//         '32bitFloat': Float32Array
//     }

//     return typedArrays[this.option.encoding] ? typedArrays[this.option.encoding] : typedArrays['16bitInt'];
// };

PCMPlayer.prototype.createContext = function() {
    this.audioCtx = new (window.AudioContext || window.webkitAudioContext)();
    this.gainNode = this.audioCtx.createGain();
    this.gainNode.gain.value = 1;
    this.gainNode.connect(this.audioCtx.destination);
    this.startTime = this.audioCtx.currentTime;
};

// PCMPlayer.prototype.isTypedArray = function(data) {
//     return (data.byteLength && data.buffer && data.buffer.constructor == ArrayBuffer);
// };

PCMPlayer.prototype.feed = function({channelData, length}) {
    var audioSrc = this.audioCtx.createBufferSource(),
    audioBuffer = this.audioCtx.createBuffer(this.option.channels, length, this.option.sampleRate);

    for (let c = 0; c < this.option.channels; c++) {
        if (audioBuffer.copyToChannel) {
            audioBuffer.copyToChannel(channelData[c], c);
        } else {
            console.log("copyToChannel not supported")
            let audioData = audioBuffer.getChannelData(c);
            for (let i = 0; i < channelData[c].byteLength; i++) {
              audioData[i] = channelData[c][i];
            }
        }
    }

    if (this.startTime < this.audioCtx.currentTime) {
        this.startTime = this.audioCtx.currentTime;
    }

    audioSrc.buffer = audioBuffer;
    audioSrc.connect(this.gainNode);
    audioSrc.start(this.startTime);
    this.startTime += audioBuffer.duration;

    // if (!this.isTypedArray(data)) return;
    // data = this.getFormatedValue(data);
    // var tmp = new Float32Array(this.samples.length + data.length);
    // tmp.set(this.samples, 0);
    // tmp.set(data, this.samples.length);
    // this.samples = tmp;
};

from pcm-player.

samirkumardas avatar samirkumardas commented on June 9, 2024

I usually get click noise most of the time. Very difficult to tell the reasons. You can try writing buffer into a file. Then play the file using a player to see if it plays well. Also, you can feed whole stored data at a time to see if the noise emanates from streaming data because sometimes streaming data cause some noise due to incorrect cut-off of PCM data.

from pcm-player.

kboniadi avatar kboniadi commented on June 9, 2024

Hey thank you so much for the feedback. I ended up fixing the issue. It actually had nothing to do with your pcm-player code. That being said, I do have another question for you.

Could you explain the purpose of having to manually check and reset the startTime back to currentTime?

if (this.startTime < this.audioCtx.currentTime) {
    this.startTime = this.audioCtx.currentTime;
}

In my specific code base, I actually had to also make a condition where if the startTime > currentTime then startTime = currentTime. I'm just confused on why the startTime is getting out of sync (< and > currentTime). Thanks

from pcm-player.

kboniadi avatar kboniadi commented on June 9, 2024

Also just an observation, but when startTime gets ahead of currentTime, I set it to currentTime which causes startTime to do a little "flutter" where it goes less then currentTime. So it ends up triggering your if statement a few times before getting in sync.

from pcm-player.

samirkumardas avatar samirkumardas commented on June 9, 2024

In my specific code base, I actually had to also make a condition where if the startTime > currentTime then startTime = currentTime. I'm just confused on why the startTime is getting out of sync (< and > currentTime). Thanks

Well, I don't know the exact reason. It could be the buggy implementation of AudioContext. I had also encountered the syncing issue and that IF conditional was a workaround in my case.

from pcm-player.

jakeershareef avatar jakeershareef commented on June 9, 2024

hai.
i am not able to play audio, please help me to fix the problem

from pcm-player.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.