grimmdude / MidiWriterJS

♬ A JavaScript library which provides an API for programmatically generating and creating expressive multi-track MIDI files and JSON.
MIT License
557 stars 60 forks source link

Midi Reader Implementation #130

Open wileymc opened 8 months ago

wileymc commented 8 months ago

I've found a case where I need to take a URI generated by MidiWriterJS and modify it and output an updated URI. Is there a good way to do this with the existing library functionality? If not, I propose a new Reader class that accepts a URI or File as an input and generates the Track object from it. I can take a shot at the implementation.

grimmdude commented 8 months ago

Hi @wileymc, there isn't currently a way to read a MIDI file into this library and modify its contents. Given the serial nature of adding events with MidiWriterJS I'm not sure how modifying an existing MIDI file would work exactly. Do you have any thoughts about how that would look?

wileymc commented 7 months ago

The way I imagine it being used would look something like this:

const write = new MidiWriter.Writer(track);
const uri = write.dataUri();

const track = new MidiWriter.Track().fromUri();
  track.addEvent(
    new MidiWriter.ProgramChangeEvent({ instrument: 5 })
  );

For the implementation, I think something similar to what tonejs has done here would be possible but may be difficult given MidiWriters current architecture.