cifkao / html-midi-player

🎹 Play and display MIDI files on the web
https://cifkao.github.io/html-midi-player/
BSD 2-Clause "Simplified" License
630 stars 56 forks source link

Inclusion in Jupyter notebooks #14

Closed cifkao closed 7 months ago

cifkao commented 3 years ago

So far I have not succeeded in including the player in Jupyter notebooks. I tried including the library using a RequireJS call like here, but I get an error about a "mismatched anonymous define()" like here. Maybe the bundle is in a format that RequireJS cannot work with?

drscotthawley commented 7 months ago

I did create a custom Jupyter 'display' object tonight that seems to work fine in local "trusted" Jupyter notebooks, for both web-hosted midi files and local midi files, without the need to spawn a separate web server.

I can share the code later after I clean it up a bit -- after I re-locate and attribute the person (Tony Hirst?) who posted the code on which I based mine.

On Colab, it will only work for web-hosted midi files, otherwise you get the usual CORS errors. Same with logging the player to WandB. ( I may see if any WandB engineers want to help with that.)

drscotthawley commented 7 months ago

Here goes! I'll attach an example Jupyter notebook .ipynb file if it'll let me. I'd be happy to send this as a PR to doc/ somewhere:

Screenshot 2023-11-27 at 10 53 56 AM

...ok then here's a link to a Colab! Note however that the second part with the local file viz won't work on Colab: https://colab.research.google.com/drive/1a5xTPgH31icTt1_kUkqGF_J0GTYoHFW7?usp=sharing

drscotthawley commented 7 months ago

Just updated the Colab to show an example with WandB logging, which already worked for remote midi file urls without the new object. Active project link: https://wandb.ai/drscotthawley/html-midi-player?workspace=user-drscotthawley

If WandB support is able to help with referencing logged midi files,...? 🔥

cifkao commented 7 months ago

@drscotthawley This is cool!

For Colab & WandB, could you encode the MIDI file as an object URL and somehow pass that into the iframe? Not sure if the player will accept an object URL directly or if you will have to decode it into a NoteSequence object.

drscotthawley commented 7 months ago

@cifkao Great minds think alike! Indeed, encoding it as a data object url was the thing that occurred to me earlier today and what enabled everything else to work.

I made a simple pip-installable package at https://github.com/drscotthawley/midi-player

Take a look in the examples/ directory where there's a notebook that can run from anywhere. I've immortalized your name in stylers.py for your "Advanced" example code.

Happy to keep making changes and improvements.

drscotthawley commented 7 months ago

...although what I really should do is now stop fiddling with this, and get back to the task for which I really wanted the player -- to use it to help me track & debug my training code! lol

cifkao commented 7 months ago

That's amazing! I think I can now close this issue thanks to you!