Question

I'm playing with a really toy project with NAudio, a simple DSL for music. I want to set the tempo in a MIDI stream I'm creating from scratch - actually starting from the DSL - but I can't figure out how to send the tempo ( ie the Beat Per Minute, ie the number of quarter in a minute), or better I can, but I have to use an hack. The default MidiEvent constructor/methods does not allow me to send additional data into the stream ( tempo is bitcoded as 3 bytes, representing the ms duration of a quarter note ) and I did not figure out a way to do this, except this bad hack ( that BTW works like a charm ):

output = new MidiEventCollection(0, Tpq);

var evt = new MetaEvent(MetaEventType.SetTempo, 3, 0);
/* Oh no really please don't dont do that!!!!!
 * what does it mean? You are touching a privare variable
 * and what the neck is that BitConverter bloody mess?
 */
 var fi = evt.GetType().GetField("data", BindingFlags.Instance | BindingFlags.NonPublic);
 var midiTempo = 60000000 / Bpm;
 var split = BitConverter.GetBytes(midiTempo);
 fi.SetValue(evt, new byte[] {split[0],split[1],split[2]  });
output.AddEvent(evt , 0);

I want ask, is there some polite way of doing this at present with NAudio?

Was it helpful?

Solution

Use TempoEvent for this:

var evt = new TempoEvent(midiTempo, 0)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top