Browse Source

Native MIDI: Fix MinGW playing speed & Fix Win32 playing bug

LouYihua 7 years ago
parent
commit
3685f08ddb
2 changed files with 16 additions and 9 deletions
  1. 2 1
      midi.c
  2. 14 8
      win32/native_midi.cpp

+ 2 - 1
midi.c

@@ -32,7 +32,7 @@ MIDI_Play(
 )
 {
 #if PAL_HAS_NATIVEMIDI
-	if (!native_midi_detect() ) 
+	if (!native_midi_detect())
 		return;
 
 	if (native_midi_active(g_pMidi) && iNumRIX == g_iMidiCurrent)
@@ -41,6 +41,7 @@ MIDI_Play(
 	}
 
 	AUDIO_PlayCDTrack(-1);
+	native_midi_stop(g_pMidi);
 	native_midi_freesong(g_pMidi);
 	g_pMidi = NULL;
 	g_iMidiCurrent = -1;

+ 14 - 8
win32/native_midi.cpp

@@ -39,6 +39,8 @@
 #include "native_midi/native_midi.h"
 #include "native_midi/native_midi_common.h"
 
+static int native_midi_available = -1;
+
 enum class MidiSystemMessage {
 	Exclusive = 0,
 	TimeCode = 1,
@@ -122,7 +124,7 @@ struct MidiEvent
 
 	std::chrono::system_clock::duration DeltaTimeAsTick(uint16_t ppq)
 	{
-		return std::chrono::system_clock::duration((int64_t)deltaTime * tempo * 10 / ppq);
+		return std::chrono::microseconds((int64_t)deltaTime * tempo / ppq);
 	}
 
 	MMRESULT Send(HMIDIOUT hmo) { return message->Send(hmo); }
@@ -225,15 +227,19 @@ static void MIDItoStream(NativeMidiSong *song, MIDIEvent *eventlist)
 
 int native_midi_detect()
 {
-	HMIDIOUT out;
-
-	if (MMSYSERR_NOERROR == midiOutOpen(&out, MIDI_MAPPER, 0, 0, CALLBACK_NULL))
+	if (-1 == native_midi_available)
 	{
-		midiOutClose(out);
-		return 1;
-	}
+		HMIDIOUT out;
 
-	return 0;
+		if (MMSYSERR_NOERROR == midiOutOpen(&out, MIDI_MAPPER, 0, 0, CALLBACK_NULL))
+		{
+			midiOutClose(out);
+			native_midi_available = 1;
+		}
+		else
+			native_midi_available = 0;
+	}
+	return native_midi_available;
 }
 
 NativeMidiSong *native_midi_loadsong(const char *midifile)