basic-pitch-ts icon indicating copy to clipboard operation
basic-pitch-ts copied to clipboard

Usage in a service worker?

Open paulrosen opened this issue 1 year ago • 3 comments

This library is amazing! However, when it's processing it locks up the UI so animations stop and buttons are not clickable.

I was wondering if I could put this in a service worker and am not sure if that has been done before.

The first problem is that a service worker can't import files and I can't figure out how to have my service worker use files in node_modules. I'm guessing I'll need a build step.

I was wondering if there were already a build option in this library for being able to load it the using a single <script> tag, because I think I'd be able to use that in my service worker. I didn't see that.

So I think I might need to write that, which I'll try to do but I'm not sure if I can.

Also I'm not sure if that will even work since there are two other files that get loaded in (the bin and the json files) and I'm not sure if that will work inside the service worker.

Any pointers you can give while I flail away at this would be appreciated.

Thanks!

paulrosen avatar Apr 19 '24 14:04 paulrosen

I think it should work. On basicpitch.io, we have basicpitch run in an async function on file load.

Regarding using it in a

drubinstein avatar Apr 19 '24 14:04 drubinstein

Thanks. It looks like I need to call importScripts instead of require and I need to serve the files from the same domain so I think I do need to create a build step for this. Still investigating...

paulrosen avatar Apr 19 '24 15:04 paulrosen

For my immediate purposes, I just needed noteFramesToTime, addPitchBendsToNoteEvents, and outputToNotesPoly so I didn't need the whole tensor flow to work in the service worker. I copied over toMidi.js from the cjs build and Midi.js from the @tonejs dependency and just had to tweak a couple lines. I'm sure there is a cleaner way to do this, but so far it seems to work ok.

I have a feeling there might be some more complications when I try to put the BasicPitch class in a service worker but I don't immediately need that.

paulrosen avatar Apr 19 '24 20:04 paulrosen