Help: how to write a midi file. ¿?

Nov 9, 2007 at 1:08 PM
Hi there!

I need an exmple code to write a midi file.
I try to save a midi like this:

Play in Channel 1 (piano) the note C for 3 seconds and stop.
Play in Channel 2 (bass) the note G for 4 seconds and stop.
Set the BPM of the midi file = 120
Save the midi.

Thanks!
Coordinator
Nov 9, 2007 at 9:48 PM
Hi Sanko

You create a MidiEventCollection and add MidiEvents to it. For example...

MidiEventCollection collection = new MidiEventCollection(0, 120);
collection.AddEvent(new TextEvent("Test", MetaEventType.TextEvent, 0), 0);
collection.AddEvent(new NoteOnEvent(0, 1, 30, 100, 15), 1);
collection.AddEvent(new NoteOnEvent(15, 1, 30, 100, 15), 1);
collection.AddEvent(new NoteOnEvent(30, 1, 30, 100, 15), 1);
collection.AddEvent(new NoteOnEvent(0, 10, 60, 100, 15), 10);
collection.AddEvent(new NoteOnEvent(15, 10, 60, 100, 15), 10);
collection.AddEvent(new NoteOnEvent(30, 10, 60, 100, 15), 10);
collection.PrepareForExport();

then you can export them to a Midi File as follows:

MidiFile.Export(destFileName, outputFileEvents);

hope this helps. feel free to ask further questions

Mark
Nov 10, 2007 at 2:00 PM
Thank you for your time and effort.