Skip to content

Commit 20691f6

Browse files
committed
[Imp] OpenMPT: Enforce Windows 11's MIDI service to start up when OpenMPT launches, to avoid UI hangs when the user tries to use some MIDI functionality for the first time. See microsoft/MIDI#1076
git-svn-id: https://source.openmpt.org/svn/openmpt/trunk/OpenMPT@25495 56274372-70c3-4bfc-bfc3-4c3a0b034d27
1 parent 587eab4 commit 20691f6

1 file changed

Lines changed: 17 additions & 0 deletions

File tree

mptrack/Mptrack.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050
#include "mpt/string/utility.hpp"
5151
#include "openmpt/sounddevice/SoundDeviceManager.hpp"
5252

53+
#include <thread>
54+
5355
#if MPT_MSVC_AT_LEAST(2022, 2) && MPT_MSVC_BEFORE(2022, 3)
5456
// Work-around <https://developercommunity.visualstudio.com/t/warning-C4311-in-MFC-header-afxrecovery/10041328>,
5557
// see <https://developercommunity.visualstudio.com/t/Compiler-warnings-after-upgrading-to-17/10036311#T-N10061908>.
@@ -1541,6 +1543,21 @@ BOOL CTrackApp::InitInstanceImpl(CMPTCommandLineInfo &cmdInfo)
15411543
m_pSoundDevicesManager = std::make_unique<SoundDevice::Manager>(m_GlobalLogger, sysInfo, appInfo, std::move(deviceEnumerators));
15421544
m_pTrackerSettings->MigrateOldSoundDeviceSettings(*m_pSoundDevicesManager);
15431545

1546+
// Windows 11 lazily initializes its MIDI service on first use, which can
1547+
// take several seconds as it scans for Bluetooth and Network MIDI devices.
1548+
// Once the service is running, the delay / hang when trying to use any
1549+
// MIDI APIs effectively disappears.
1550+
// To work around that, force some MIDI API usage on a separate thread to
1551+
// force-initialize MidiSrv. Adding an instance of the MIDI I/O plugin,
1552+
// reviewing MIDI settings or turning on MIDI recording should no longer
1553+
// hang on first use then.
1554+
// Note that this can still block the OpenMPT UI if the main thread
1555+
// enumerates MME audio devices at the same time, e.g. because the user
1556+
// opened the settings dialog, as there appears to be shared internal lock
1557+
// between these two APIs.
1558+
// See https://github.com/microsoft/MIDI/issues/1076
1559+
std::thread{midiInGetNumDevs}.detach();
1560+
15441561
// Set default note names
15451562
CSoundFile::SetDefaultNoteNames();
15461563

0 commit comments

Comments
 (0)