I'm posting my question here as it was suggested to do so on the NAudio page(http://naudio.codeplex.com/documentation).
Given that I've loaded a .mid file with NAudio, I'd like to step through the events of a track in proper time. Upon reaching a "NoteOn" event, I'd like to write to my console something like "Beep". This way I could display the simple activity of a .mid file. In detail, how can I properly step through th开发者_如何学Ce file's events in time?
I'm using vsC# 2008 and the .dll of NAudio is working fine. I've loaded in a midi file with the library and can extract the track information and everything, but I'm just not sure how to use it in respect to time. I'm thinking I'll need to do something with either the "absolute time" or "delta time" values supplied. Thank you for the help.
Edit: After a very helpful response I was able to figure out how to do what I intended.
double convertAbsoluteTimeToMilliseconds = 1.0 / 30.0;
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
foreach (MidiEvent note in _f1.Events[1])
{
while (note.AbsoluteTime * convertAbsoluteTimeToMilliseconds > sw.ElapsedMilliseconds) { }
if(note.CommandCode==MidiCommandCode.NoteOn)
Console.Write ("beep ");
}
sw.Stop();
If anyone knows of an alternative way to do this please do let me know. Using sleep for a difference of time resulted in a response that got slower and slower for some reason. This seems to work fine however. Also, I'm sure that "convertAbsoluteTimeToMilliseconds" is machine dependent.
Pseudocode:
DateTime offset = DateTime.Now; /Start playing here.
foreach (MidiEvent note in events)
{
DateTime beepAt = offset.AddMilliseconds (note.AbsoluteTime * convertAbsoluteTimeToMilliseconds);
System.Threading.Thread.Sleep (beepAt - offset);
Console.WriteLine ("beep");
}
精彩评论