Skip to content

Conversation

polyrhythmatic
Copy link
Contributor

@tambien and I spent some time working on this, and we came up with this solution. Hoping this works as a resolution to #815

Yotam also had the idea of using a function/token request system, and this is basically what he's implemented here. At the end of "p5.Tone.js", the buffer load function is rewritten and a registered function is instantiated and then called at the end of the async call. p5 can use this function to add it's increment/decrement function.

    Tone.registeredPreload = function(callback){
        return function(){
            callback();
        }
    };

    //overwrite load function
    Tone.Buffer.load = function (url, callback) {
        var handle = Tone.registeredPreload();
        var request = new XMLHttpRequest();
        request.open('GET', url, true);
        request.responseType = 'arraybuffer';
        // decode asynchronously
        request.onload = function () {
            Tone.context.decodeAudioData(request.response, function (buff) {
                if (!buff) {
                    throw new Error('could not decode audio data:' + url);
                }
                callback(buff);
                handle();
            });
        };
        //send the request
        request.send();
        return request;
    };

    p5.prototype.registerPreloadMethod("registeredPreload", Tone);

@polyrhythmatic
Copy link
Contributor Author

@lmccart just wondering if you have any comments!

@lmccart
Copy link
Member

lmccart commented Aug 19, 2015

this is great @polyrhythmatic! I will review as soon as I have a little time, hopefully later today.

@lmccart
Copy link
Member

lmccart commented Aug 19, 2015

this looks good @polyrhythmatic thank you both for looking into this and cleaning up the preload function in p5.js.

lmccart pushed a commit that referenced this pull request Aug 19, 2015
updated version allows better object and function handling
@lmccart lmccart merged commit effc454 into processing:master Aug 19, 2015
@polyrhythmatic
Copy link
Contributor Author

Thanks @lmccart ! Willing to continue working on this, perhaps there are other libraries that we want to integrate and could pose other issues.

@lmccart
Copy link
Member

lmccart commented Aug 19, 2015

yeah, i think if you could add the info and example from tone.js to the wiki, that'd be very helpful.
https://github.com/processing/p5.js/wiki/Libraries#use-registerpreloadmethod-to-register-names-of-methods-with-p5-that-may-be-called-in-preload

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants