mudcube / MIDI.js

:musical_keyboard: Making life easy to create a MIDI-app on the web. Includes a library to program synesthesia into your app for memory recognition or for creating trippy effects. Convert soundfonts for Guitar, Bass, Drums, ect. into code that can be read by the browser. Supports multiple simultaneous instruments and perfect timing.
http://mudcu.be/midi-js/
MIT License
3.81k stars 640 forks source link

How to implement web worker in MIDI.js app ? #217

Open shivrajsa opened 7 years ago

shivrajsa commented 7 years ago

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 ?

hmoffatt commented 7 years ago

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 notifications@github.com To: "mudcube/MIDI.js" MIDI.js@noreply.github.com Cc: Subscribed subscribed@noreply.github.com 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

shivrajsa commented 7 years ago

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.

gleitz commented 7 years ago

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.

shivrajsa commented 7 years ago

@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