在 C# 中播放带有时间偏移的 Midi 文件
我目前正在尝试在播放歌曲时浏览 midi 文件,midi 文件在歌曲之前“播放”几毫秒。更详细地说,我通过从屏幕右侧向左侧滑动所述音符来可视化歌曲的音符。我根据来自 midi 文件的信号在屏幕右侧创建音符的可视化,然后开始将它们翻译到左侧。我希望他们在歌曲中实际播放音符的同时经过屏幕中心。
我可以使用以下代码很好地播放 midi 文件:
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].CommandCode == NAudio.Midi.MidiCommandCode.NoteOn)
{
Console.Beep(777, 25);
Console.Write("Beep ");
}
}
}
偏移量有效地使 midi 文件播放一系列音符,然后返回到根本不使用偏移量时所处的轨道位置。
我很困惑,非常感谢任何帮助。我正在使用 XNA 3.1 和 NAudio。 MIDI 与歌曲的节奏配合得很好。当然,我需要midi的播放速度与歌曲保持相同。
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].CommandCode == 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么不同时播放 2 个 midi 文件,而只使用一个来实际输出音频(如果音频 API 支持它......)
或者更好的是,解析整个 midi 文件一次。
获取笔记及其时间,并将其存储在一个大数组中。
播放实际的 MIDI 文件时,使用刚刚填充的数组来可视化音符。
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.