rserota / wad

Web Audio DAW. Use the Web Audio API for dynamic sound synthesis. It's like jQuery for your ears.
MIT License
1.9k stars 160 forks source link

Can't switch panning type (3d/stereo) on play #40

Closed alexmooch closed 9 years ago

alexmooch commented 9 years ago

Hi @rserota, great work on this script! It works like a charm, just one thing is not clear to me.

With code just like

var saw = new Wad({source: 'sine'});

stereo panning seems to be used by default. So following line:

saw.play({panning: [10, 0, 10]});

throws an exception Uncaught TypeError: Failed to set the 'value' property on 'AudioParam': The provided float value is non-finite. at line 473 in wad.js. Setting up 3d panning manually:

var saw = new Wad({source: 'sine', panning: [0, 0, 0]}); // or 'panning: []'

fixes the thing. And as I just figured out, this code:

var saw = new Wad({source: 'sine', panning: [0, 0, 0]});
saw.play({panning: 0.5});

gives again an error: Uncaught TypeError: Failed to execute 'setPosition' on 'PannerNode': The provided float value is non-finite. (line 468).

So, the question is if it's possible to change panning type after Wad is created? And can it be made automatically?

P.S. Should be in setUpPanningOnPlay not current Wad's but arg's panning type checked? Like

var setUpPanningOnPlay = function(that, arg) {
    var panning = arg && arg.panning; // can be zero provided as argument
    if (typeof(panning) === 'undefined') panning = that.panning.location;

    if (typeof(panning) === 'number') {
        that.panning.node = context.createStereoPanner();
        that.panning.node.pan.value = panning;
    }
    else {
        that.panning.node = context.createPanner();
        that.panning.node.setPosition(panning[0], panning[1], panning[2]);
    }

    that.nodes.push(that.panning.node);
};

or similar, I guess?

rserota commented 9 years ago

My original intention was that once you initialize a Wad with one panner, you couldn't switch it to the other type on the fly. I just didn't think it really made sense, though I guess if you know what you're doing, there isn't really any harm.

Would you like to test your suggested code and submit it as a pull request? You'd also need to modify the setPanning method to be more dynamic, similarly to what you did with setUpPanningOnPlay.

rserota commented 9 years ago

I implemented the change you suggested (and updated other relevant code). If you pull down fresh code, it should work the way you want. You can now switch between 3d and stereo panning on the fly.