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

Need an example to readme #1

Open
anton-okolelov opened this issue Nov 16, 2015 · 5 comments
Open

Need an example to readme #1

anton-okolelov opened this issue Nov 16, 2015 · 5 comments

Comments

@anton-okolelov
Copy link

Thank you for writing rimd lib!

Could you please add an example of creating midi-file that plays a note (for example to readme)?
It would be really helpful, because it's too complicated to puzzle out midi format, rust language and rimd library simultaneously.

@nicklan
Copy link
Contributor

nicklan commented Nov 16, 2015

Sure. So, rimd doesn't really deal with playing midi notes, just the midi files. There are a number of other libraries for playing notes like the portaudio ones, or rust-jack. I've tried to keep the format that rimd stores midi notes very simple so it's easy to inter-operate with those libraries.

That said, you're right, this will usually be used in the context of a bigger library and I should add an example of using it somehow. I'm swamped right now, but will get to that in the near future.

@mitchmindtree
Copy link
Member

This might be fun to use for the example?

@anton-okolelov
Copy link
Author

Thank you for the response.

I'd like to explain. I'm trying to write a program that is some kind of composer. It compose music and write it to midi file.
And I have trouble doing it.
Here is my try:

extern crate rimd;

use rimd::{MidiMessage,SMFBuilder,SMFWriter,TrackEvent,Event};
use std::path::Path;

fn main() {

    let note_on = MidiMessage::note_on(69,100,0);
    let note_off = MidiMessage::note_off(69,100,0);


    let mut builder = SMFBuilder::new();
    builder.add_track();

    builder.add_event(0, TrackEvent{vtime: 5, event: Event::Midi(note_on)});
    builder.add_event(0, TrackEvent{vtime: 0, event: Event::Midi(note_off)});

    let mut smf = builder.result();

    smf.division = 96;
   let writer = SMFWriter::from_smf(smf);
   let result = writer.write_to_file(Path::new("/home/user/file.smf"));

}

It creates file.smf, but when I run
timidity file.smf
I hear nothing.
So, I don't understand what I'm doing wrong. Maybe I don't understand midi format, maybe I know rust too bad, maybe there is something that I had to do in rimd api.

If there was a start point ( a simple example with one note wrote to midi file) I would easily extend it to my program

@nicklan
Copy link
Contributor

nicklan commented Feb 9, 2016

Sorry for the super long delay in getting back to you! Anyway, there were two issues here. rimd was indeed broken, but the way you were using it was as well. If you want to add those events you'd want to do:

    builder.add_event(0, TrackEvent{vtime: 0, event: Event::Midi(note_on)});
    builder.add_event(0, TrackEvent{vtime: 5, event: Event::Midi(note_off)});

Note the switch of the 0 and the 5 in the first parameter. add_event like that adds the event with the number of ticks after the current last event in the track. Doing first 5 then 0 would add, at time 5, two events, a note on and a note off, which is why you didn't hear anything. Doing 0 and then 5 will play the note for 5 ticks. (that order was causing a panic before which was a real bug in rimd which is fixed and pushed)

BTW, for simple smf creation you might find using the add_midi_rel and add_meta_rel functions a bit easier since they allow thinking in relative time.

If things are working for you now, please close this, otherwise, let me know what's broken now :)

@nicklan nicklan closed this as completed Feb 9, 2016
@nicklan nicklan reopened this Feb 9, 2016
@Benjamin-Davies
Copy link

Is there still interest in this issue?

If so, I have managed to create a working demo of the library at https://github.com/Benjamin-Davies/rimd-demo/blob/master/src/main.rs. It feels a bit bulky to put into a README though, so maybe we should reduce it to just a single note without any of the meta-events.

I would be happy to alter the example and create a pull request.

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

4 participants