0

I Use Wpf. I parse a midi file and use midioutshortmsg to send message in a For Loop, and sleep for delay Delta Time; My playback is lagging ,But, when i open other midi software at the same time (include WindowMediaPlayer) ,All the Problem is sloved,My Program work fine. When I close the other opened software.Problem is come back.

My program stuck in here. I have already to try send in midistreamout,MidiOutProc,or midioutlongmsg,etc.but my problem still can't solve. Thank You Very Much.

[DllImport("winmm.dll")]
private extern static int midiOutOpen(out int lphMidiOut, int uDeviceID, int dwCallback, int dwInstance, int dwFlags);
[DllImport("winmm.dll")]
public extern static int midiOutShortMsg(int lphMidiOut, int dwMsg);

public int midiOut;

private void Play_Click(object sender, RoutedEventArgs e)
{   
midiOutOpen(out midiOut, 0, 0, 0, 0);

InstanceCaller = new Thread(new ThreadStart(SendMessage));
InstanceCaller.Start();
}

private void SendMessage()
{
for (int i = 0; i < melodyList.Count; i++)
{
var messgae = Convert.ToInt32(melodyList[i], 16);
midiOutShortMsg(midiOut, messgae);
Thread.Sleep(Convert.ToInt32(durationList[i]));
}
}
cvp
  • 3
  • 1
  • whay are you sleeping durationlist[i] can you debug and see what those values are? – Shane_Yo Apr 28 '20 at 18:03
  • Possible duplicate of [Thread.Sleep(1) takes longer than 1ms](https://stackoverflow.com/questions/19066900/thread-sleep1-takes-longer-than-1ms) – CL. Apr 29 '20 at 07:21

1 Answers1

1

If you're happy with Windows, you can use winmm timer functions to run high-resolution timer which will give 1ms precision, which impossible with Thread.Sleep.

Also you can use third-party libraries, which do all this job for you. I'm the author of DryWetMIDI so you can use it for playback. Please read Playback article os the library docs to learn more.

Maxim
  • 1,995
  • 1
  • 19
  • 24
  • Thanks, Maxim .I try winmm timer,it can solve my problem.But use DryWetMIDI is more easy and convenient.Thanks. – cvp May 07 '20 at 22:33