Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Usage in a service worker? #19

Open
paulrosen opened this issue Apr 19, 2024 · 3 comments
Open

Usage in a service worker? #19

paulrosen opened this issue Apr 19, 2024 · 3 comments

Comments

@paulrosen
Copy link

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!

@drubinstein
Copy link
Contributor

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

Regarding using it in a <script> tag. I believe you can if you use https://cdn.jsdelivr.net/npm/@spotify/[email protected] and https://cdn.jsdelivr.net/npm/@spotify/[email protected]/model/ for the model.

@paulrosen
Copy link
Author

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
Copy link
Author

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.

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

No branches or pull requests

2 participants