This project has moved. For the latest updates, please go here.

MIDI file creation

Apr 7, 2011 at 2:45 PM

Hey,

I'm trying to create a file programatically, and I'm using this code:

     MidiEventCollection collection = new MidiEventCollection(0, 120) ;

            collection.AddEvent(new TextEvent("Test", MetaEventType.TextEvent, 0), 0);
            NoteOnEvent evento = new NoteOnEvent(2,1,40,100,15);
            collection.AddEvent(evento, 1);
            collection.PrepareForExport();
            MidiFile.Export("test.mid", collection);

However, the test.mid file doesn't play any sound. I tried opening the file using your library and it says it's missing a noteOff event. What am I doing wrong?

Coordinator
Apr 9, 2011 at 7:26 AM

Yes, for every NoteOn MIDI message there must be a NoteOff event.

Try something like this:
MidiEventCollection collection = new MidiEventCollection(0, 120);
collection.AddEvent(new NoteEvent(1, 1, MidiCommandCode.NoteOn, 60, 100), 0);
collection.AddEvent(new NoteEvent(300, 1, MidiCommandCode.NoteOff, 60, 0), 0);
collection.PrepareForExport();

MidiFile.Export(txtFile.Text, collection);

Apr 9, 2011 at 10:57 AM
Ok, ok, then. What is the duartion (100, in this example) useful for? Shouldn't it create the NoteOff event automatically?

Thanks again


From: [email removed]
To: [email removed]
Date: Sat, 9 Apr 2011 00:26:18 -0700
Subject: Re: MIDI file creation [naudio:252963]

From: markheath
Yes, for every NoteOn MIDI message there must be a NoteOff event.
Try something like this:
MidiEventCollection collection = new MidiEventCollection(0, 120);
collection.AddEvent(new NoteEvent(1, 1, MidiCommandCode.NoteOn, 60, 100), 0);
collection.AddEvent(new NoteEvent(300, 1, MidiCommandCode.NoteOff, 60, 0), 0);
collection.PrepareForExport();

MidiFile.Export(txtFile.Text, collection);
Read the full discussion online.
To add a post to this discussion, reply to this email (naudio@discussions.codeplex.com)
To start a new discussion for this project, email naudio@discussions.codeplex.com
You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe on CodePlex.com.
Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at CodePlex.com
Coordinator
Apr 10, 2011 at 8:03 AM

This code was developed for MIDI File Mapper and MIDI File Splitter, for which the API works well (loading in existing MIDI files and modifying them). I agree though that it is not intuitive for people wanting to create MIDI from scratch. Perhaps this could be addressed in a future version.

Mark

May 2, 2011 at 12:09 PM

It all worked.

I have one more question: given a BPM, how do I calculate the deltaTicksperquarternote and the absolute duration ( the one I should use in NoteOn and NoteOff events)?

Thanks

Coordinator
May 2, 2011 at 4:14 PM

delta ticks per quarter note is your choice. people usually go with numbers that are multiples of 2 and 3, allowing for subdivisions into triplets etc. Common values are 96, 240, 480. So if you have 120 BPM and 240 ticks per beat there are 28800 ticks per minute, or 480 ticks per second (so you have an effective time resolution of 2ms.

May 2, 2011 at 5:24 PM

But if, for example, I’m given 120 BPM and someone asks me for a quarter note C4, how long should the duration of that note be?

Thank you

From: markheath [email removed]
Sent: Monday, May 02, 2011 5:15 PM
To: bernardocaldas@hotmail.com
Subject: Re: MIDI file creation [naudio:252963]

From: markheath

delta ticks per quarter note is your choice. people usually go with numbers that are multiples of 2 and 3, allowing for subdivisions into triplets etc. Common values are 96, 240, 480. So if you have 120 BPM and 240 ticks per beat there are 28800 ticks per minute, or 480 ticks per second (so you have an effective time resolution of 2ms.

Coordinator
May 2, 2011 at 8:11 PM

the BPM doesn't matter. If you selected 240 delta ticks per quarter note, then a quarter note on C4 has a duration of 240.

Mark