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

Add examples on how to write LiveEvents to mid files #24

Open
Qualphey opened this issue Jan 3, 2024 · 3 comments
Open

Add examples on how to write LiveEvents to mid files #24

Qualphey opened this issue Jan 3, 2024 · 3 comments

Comments

@Qualphey
Copy link

Qualphey commented Jan 3, 2024

I'm trying to write events, coming from jack midi input port, to a mid file. I guess I could say I'm trying to record a midi sequence. The documentation doesn't really have much info nor any examples on how to write live event sequences to mid files.

I still tried and couldn't figure out how to convert LiveEvent to TrackEvent, so I could write it.

I've set up an SMF:

let header = midly::Header {
  format: midly::Format::SingleTrack, // Replace with the desired format
  timing: midly::Timing::Metrical(480.into()), // Replace with the desired timing information
};
let smf = midly::Smf::new(header);

and then tried to push an event to the first track:

let arena = midly::Arena::new();    
...
let live_event = LiveEvent::parse(data).unwrap();
smf.tracks[0].push(live_event.as_track_event(&arena));

But as_track_event returns TrackEventKind and I can't find no way to convert that.

@Qualphey
Copy link
Author

Qualphey commented Jan 4, 2024

Also I wonder if it's possible to write mid files streaming chunks of event data instead of writing the whole file at once. This way you could save up some memory space especially when you've got many tracks or making long recordings.

@kovaxis
Copy link
Owner

kovaxis commented Jan 25, 2024

The reason why a LiveEvent converts into a TrackEventKind instead of a TrackEvent is that:

TrackEvent = TrackEventKind + (timing info)

Timing info is not so easy to work with in MIDI, but it involves adding a timing message at the beggining of track 0 which defines the unit of time used in the file (a "tick"), and then converting the times at which the events happen to delta ticks.

I agree it would be nice to have a set of examples. If I have some spare time I will add some. If you (or any reader) has figured it out and has some spare time, feel free to add examples.

@Qualphey
Copy link
Author

Qualphey commented Jan 25, 2024

I've figured this one out. Managed to make a working POC where midi events can be recorded and saved to a .mid file and then played back. Now I'm reworking the code to use multi-threading to queue/fire events on one thread and write/read on another. I might as well post some examples here after I'm done.

@kovaxis kovaxis changed the title Add documentation on how to write LiveEvents to mid files. Add examples on how to write LiveEvents to mid files Jun 8, 2024
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