MIDI.js icon indicating copy to clipboard operation
MIDI.js copied to clipboard

How to implement web worker in MIDI.js app ?

Open shivrajsa opened this issue 7 years ago • 4 comments

I have below code in my app.js to play sequence of notes.

var notes = [67, 78, 67 ,67,90, 56,..,...,....];
var delay = [.........],tmpdelay=0;
var ctxtime = MIDI.getContext().currentTime;
for(var i=0; l < notes.length; i++)
{
MIDI.noteOn(channel, notes[i], velocity, ctxtime+tmpdelay);
tmpdelay = tmpdelay + delay[i]
}

How can I implement web worker for such case ?

shivrajsa avatar Sep 04 '17 10:09 shivrajsa

What problem do you want to solve with the web worker? You don't play many notes so you won't have the other issue where web workers are discussed.

Sent from my android device.

-----Original Message----- From: shivrajsa [email protected] To: "mudcube/MIDI.js" [email protected] Cc: Subscribed [email protected] Sent: Mon, 04 Sep 2017 10:25 Subject: [mudcube/MIDI.js] How to implement web worker in MIDI.js app ? (#217)

I have below code in my app.js to play sequence of notes.

var notes = [67, 78, 67 ,67,90, 56,..,...,....];
var delay = [.........],tmpdelay=0;
var ctxtime = MIDI.getContext().currentTime;
for(var i=0; l < notes.length; i++)
{
MIDI.noteOn(channel, notes[i], velocity, ctxtime+tmpdelay);
tmpdelay = tmpdelay + delay[i]
}

How can I implement web worker for such case ?

-- You are receiving this because you are subscribed to this thread. Reply to this email directly or view it on GitHub: https://github.com/mudcube/MIDI.js/issues/217

hmoffatt avatar Sep 04 '17 19:09 hmoffatt

My app has no restriction on playing number of notes at a time. Code I have shared is to give idea. And when there are many notes performance of web gets affected.

Currently I am exploring vkthread to implement web worker.

shivrajsa avatar Sep 05 '17 04:09 shivrajsa

Rather than using a loop with increasingly longer delays for noteOn, you might want to play the first note then use a setTimeout that will play the next note, and so on until all the notes are played.

That way you will trigger only one note at a time, and have one timer, rather than creating a very large loop and relying on the delays to calculate the timing.

gleitz avatar Sep 15 '17 01:09 gleitz

@gleitz Thank you very much, I will try to implement it, I may have to do good enough changes to implement this in my App.

I got good example which demonstrates how setTimeout works synchronously and asynchronously.

https://stackoverflow.com/questions/26380086/how-browser-execute-javascript-render-asynchronous/26381899#26381899

shivrajsa avatar Sep 15 '17 04:09 shivrajsa