Playing a Midi file with time offset in C#
I'm currently trying to walk through a midi file as a song plays, with the midi file "playing" a few milliseconds ahead of the song. In greater detail, I'm visualizing the notes of the song by sliding said notes from the right to the left of the screen. I create the visualizations of the notes, just off of the right of the screen, upon a signal from the midi file, and then start translating them to the left. I would like them to pass the center of the screen at the same time that the note actually plays in the song.
I can play the midi file just fine using this code:
if (counter < _file.Events[track].Count)
{
double s_ms = Microsoft.Xna.Framework.Media.MediaPlayer.PlayPosition.TotalMilliseconds;
double m_ms = _file.Events[track][counter].AbsoluteTime / _pulses_per_millisecond;
if (s_ms + offset > m_ms)
{
counter++;
if (_file.Events[track][counter].CommandCod开发者_开发知识库e == NAudio.Midi.MidiCommandCode.NoteOn)
{
Console.Beep(777, 25);
Console.Write("Beep ");
}
}
}
The offset effectively makes the midi file play a burst of notes and then return to the track position it would be at if the offset wasn't used at all.
I'm stumped and would really appreciate any help. I'm using XNA 3.1 and NAudio. The midi plays in time with the song just fine. Of course, I need the play speed of the midi to remain the same as the song.
Why don't you play 2 midi files at the same time, while only using one to actually output the audio (if the audio API supports it...)
Or even better, parse the entire midi file once. Get the note and it's timing, and store it in one big array. When playing the actual midi file, use the array you just filled to visualize the notes.
精彩评论