Mpt_midi.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * MPT_MIDI.cpp
  3. * ------------
  4. * Purpose: MIDI Input handling code.
  5. * Notes : (currently none)
  6. * Authors: OpenMPT Devs
  7. * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
  8. */
  9. #include "stdafx.h"
  10. #include <mmsystem.h>
  11. #include "Mainfrm.h"
  12. #include "InputHandler.h"
  13. #include "Dlsbank.h"
  14. #include "../soundlib/MIDIEvents.h"
  15. OPENMPT_NAMESPACE_BEGIN
  16. #ifdef MPT_ALL_LOGGING
  17. #define MPTMIDI_RECORDLOG
  18. #endif
  19. // Midi Input globals
  20. HMIDIIN CMainFrame::shMidiIn = nullptr;
  21. //Get Midi message(dwParam1), apply MIDI settings having effect on volume, and return
  22. //the volume value [0, 256]. In addition value -1 is used as 'use default value'-indicator.
  23. int CMainFrame::ApplyVolumeRelatedSettings(const DWORD &dwParam1, const BYTE midivolume)
  24. {
  25. int nVol = MIDIEvents::GetDataByte2FromEvent(dwParam1);
  26. if(TrackerSettings::Instance().m_dwMidiSetup & MIDISETUP_RECORDVELOCITY)
  27. {
  28. nVol = (CDLSBank::DLSMidiVolumeToLinear(nVol)+255) >> 8;
  29. nVol *= TrackerSettings::Instance().midiVelocityAmp / 100;
  30. Limit(nVol, 1, 256);
  31. if(TrackerSettings::Instance().m_dwMidiSetup & MIDISETUP_MIDIVOL_TO_NOTEVOL)
  32. nVol = static_cast<int>((midivolume / 127.0) * nVol);
  33. } else
  34. {
  35. // Case: No velocity record.
  36. if(TrackerSettings::Instance().m_dwMidiSetup & MIDISETUP_MIDIVOL_TO_NOTEVOL)
  37. nVol = 4*((midivolume+1)/2);
  38. else //Use default volume
  39. nVol = -1;
  40. }
  41. return nVol;
  42. }
  43. void ApplyTransposeKeyboardSetting(CMainFrame &rMainFrm, uint32 &dwParam1)
  44. {
  45. if ( (TrackerSettings::Instance().m_dwMidiSetup & MIDISETUP_TRANSPOSEKEYBOARD)
  46. && (MIDIEvents::GetChannelFromEvent(dwParam1) != 9) )
  47. {
  48. int nTranspose = rMainFrm.GetBaseOctave() - 4;
  49. if (nTranspose)
  50. {
  51. int note = MIDIEvents::GetDataByte1FromEvent(dwParam1);
  52. if (note < 0x80)
  53. {
  54. note += nTranspose * 12;
  55. Limit(note, 0, NOTE_MAX - NOTE_MIN);
  56. dwParam1 &= 0xffff00ff;
  57. dwParam1 |= (note << 8);
  58. }
  59. }
  60. }
  61. }
  62. /////////////////////////////////////////////////////////////////////////////
  63. // MMSYSTEM Midi Record
  64. void CALLBACK MidiInCallBack(HMIDIIN, UINT wMsg, DWORD_PTR, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
  65. {
  66. CMainFrame *pMainFrm = CMainFrame::GetMainFrame();
  67. HWND hWndMidi;
  68. if (!pMainFrm) return;
  69. #ifdef MPTMIDI_RECORDLOG
  70. DWORD dwMidiStatus = dwParam1 & 0xFF;
  71. DWORD dwMidiByte1 = (dwParam1 >> 8) & 0xFF;
  72. DWORD dwMidiByte2 = (dwParam1 >> 16) & 0xFF;
  73. DWORD dwTimeStamp = (DWORD)dwParam2;
  74. MPT_LOG_GLOBAL(LogDebug, "MIDI", MPT_UFORMAT("time={}ms status={} data={}.{}")(mpt::ufmt::dec(dwTimeStamp), mpt::ufmt::HEX0<2>(dwMidiStatus), mpt::ufmt::HEX0<2>(dwMidiByte1), mpt::ufmt::HEX0<2>(dwMidiByte2)));
  75. #endif
  76. hWndMidi = pMainFrm->GetMidiRecordWnd();
  77. if(wMsg == MIM_DATA || wMsg == MIM_MOREDATA)
  78. {
  79. uint32 data = static_cast<uint32>(dwParam1);
  80. if(::IsWindow(hWndMidi))
  81. {
  82. switch(MIDIEvents::GetTypeFromEvent(data))
  83. {
  84. case MIDIEvents::evNoteOff: // Note Off
  85. case MIDIEvents::evNoteOn: // Note On
  86. ApplyTransposeKeyboardSetting(*pMainFrm, data);
  87. [[fallthrough]];
  88. default:
  89. if(::PostMessage(hWndMidi, WM_MOD_MIDIMSG, data, dwParam2))
  90. return; // Message has been handled
  91. break;
  92. }
  93. }
  94. // Pass MIDI to keyboard handler
  95. CMainFrame::GetInputHandler()->HandleMIDIMessage(kCtxAllContexts, data);
  96. } else if(wMsg == MIM_LONGDATA)
  97. {
  98. // SysEx...
  99. } else if (wMsg == MIM_CLOSE)
  100. {
  101. // midiInClose will trigger this, but also disconnecting a USB MIDI device (although delayed, seems to be coupled to calling something like midiInGetNumDevs).
  102. // In the latter case, we need to inform the UI.
  103. if(CMainFrame::shMidiIn != nullptr)
  104. {
  105. pMainFrm->SendMessage(WM_COMMAND, ID_MIDI_RECORD);
  106. }
  107. }
  108. }
  109. bool CMainFrame::midiOpenDevice(bool showSettings)
  110. {
  111. if (shMidiIn) return true;
  112. if (midiInOpen(&shMidiIn, TrackerSettings::Instance().GetCurrentMIDIDevice(), (DWORD_PTR)MidiInCallBack, 0, CALLBACK_FUNCTION) != MMSYSERR_NOERROR)
  113. {
  114. shMidiIn = nullptr;
  115. // Show MIDI configuration on fail.
  116. if(showSettings)
  117. {
  118. CMainFrame::m_nLastOptionsPage = OPTIONS_PAGE_MIDI;
  119. CMainFrame::GetMainFrame()->OnViewOptions();
  120. }
  121. // Let's see if the user updated the settings.
  122. if(midiInOpen(&shMidiIn, TrackerSettings::Instance().GetCurrentMIDIDevice(), (DWORD_PTR)MidiInCallBack, 0, CALLBACK_FUNCTION) != MMSYSERR_NOERROR)
  123. {
  124. shMidiIn = nullptr;
  125. return false;
  126. }
  127. }
  128. midiInStart(shMidiIn);
  129. return true;
  130. }
  131. void CMainFrame::midiCloseDevice()
  132. {
  133. if (shMidiIn)
  134. {
  135. // Prevent infinite loop in MIM_CLOSE
  136. auto handle = shMidiIn;
  137. shMidiIn = nullptr;
  138. midiInClose(handle);
  139. }
  140. }
  141. void CMainFrame::OnMidiRecord()
  142. {
  143. if (shMidiIn)
  144. {
  145. midiCloseDevice();
  146. } else
  147. {
  148. midiOpenDevice();
  149. }
  150. }
  151. void CMainFrame::OnUpdateMidiRecord(CCmdUI *pCmdUI)
  152. {
  153. if (pCmdUI) pCmdUI->SetCheck((shMidiIn) ? TRUE : FALSE);
  154. }
  155. OPENMPT_NAMESPACE_END