MidiInOutEditor.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * MidiInOutEditor.cpp
  3. * -------------------
  4. * Purpose: Editor interface for the MidiInOut plugin.
  5. * Notes : (currently none)
  6. * Authors: Johannes Schultz (OpenMPT Devs)
  7. * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
  8. */
  9. #include "stdafx.h"
  10. #ifdef MODPLUG_TRACKER
  11. #include "MidiInOut.h"
  12. #include "MidiInOutEditor.h"
  13. #include "../Mptrack.h"
  14. #include "../resource.h"
  15. #include <rtmidi/RtMidi.h>
  16. OPENMPT_NAMESPACE_BEGIN
  17. BEGIN_MESSAGE_MAP(MidiInOutEditor, CAbstractVstEditor)
  18. //{{AFX_MSG_MAP(MidiInOutEditor)
  19. ON_CBN_SELCHANGE(IDC_COMBO1, &MidiInOutEditor::OnInputChanged)
  20. ON_CBN_SELCHANGE(IDC_COMBO2, &MidiInOutEditor::OnOutputChanged)
  21. ON_EN_CHANGE(IDC_EDIT1, &MidiInOutEditor::OnLatencyChanged)
  22. ON_COMMAND(IDC_CHECK1, &MidiInOutEditor::OnTimingMessagesChanged)
  23. //}}AFX_MSG_MAP
  24. END_MESSAGE_MAP()
  25. void MidiInOutEditor::DoDataExchange(CDataExchange* pDX)
  26. {
  27. CDialog::DoDataExchange(pDX);
  28. //{{AFX_DATA_MAP(MidiInOutEditor)
  29. DDX_Control(pDX, IDC_COMBO1, m_inputCombo);
  30. DDX_Control(pDX, IDC_COMBO2, m_outputCombo);
  31. DDX_Control(pDX, IDC_EDIT1, m_latencyEdit);
  32. DDX_Control(pDX, IDC_SPIN1, m_latencySpin);
  33. //}}AFX_DATA_MAP
  34. }
  35. MidiInOutEditor::MidiInOutEditor(MidiInOut &plugin)
  36. : CAbstractVstEditor(plugin)
  37. {
  38. }
  39. bool MidiInOutEditor::OpenEditor(CWnd *parent)
  40. {
  41. Create(IDD_MIDI_IO_PLUGIN, parent);
  42. MidiInOut &plugin = static_cast<MidiInOut &>(m_VstPlugin);
  43. m_latencyEdit.AllowFractions(true);
  44. m_latencyEdit.AllowNegative(true);
  45. m_latencyEdit.SetDecimalValue(plugin.m_latency * 1000.0, 4);
  46. m_latencySpin.SetRange32(mpt::saturate_round<int>(plugin.GetOutputLatency() * -1000.0), int32_max);
  47. PopulateList(m_inputCombo, plugin.m_midiIn, plugin.m_inputDevice, true);
  48. PopulateList(m_outputCombo, plugin.m_midiOut, plugin.m_outputDevice, false);
  49. CheckDlgButton(IDC_CHECK1, plugin.m_sendTimingInfo ? BST_CHECKED : BST_UNCHECKED);
  50. m_locked = false;
  51. return CAbstractVstEditor::OpenEditor(parent);
  52. }
  53. // Update lists of available input / output devices
  54. void MidiInOutEditor::PopulateList(CComboBox &combo, RtMidi &rtDevice, MidiDevice &midiDevice, bool isInput)
  55. {
  56. combo.SetRedraw(FALSE);
  57. combo.ResetContent();
  58. // Add dummy device
  59. combo.SetItemData(combo.AddString(_T("<none>")), static_cast<DWORD_PTR>(MidiInOut::kNoDevice));
  60. // Go through all RtMidi devices
  61. auto ports = rtDevice.getPortCount();
  62. int selectedItem = 0;
  63. CString portName;
  64. for(unsigned int i = 0; i < ports; i++)
  65. {
  66. try
  67. {
  68. portName = theApp.GetFriendlyMIDIPortName(mpt::ToCString(mpt::Charset::UTF8, midiDevice.GetPortName(i)), isInput);
  69. int result = combo.AddString(portName);
  70. combo.SetItemData(result, i);
  71. if(result != CB_ERR && i == midiDevice.index)
  72. selectedItem = result;
  73. } catch(RtMidiError &)
  74. {
  75. }
  76. }
  77. combo.SetCurSel(selectedItem);
  78. combo.SetRedraw(TRUE);
  79. }
  80. // Refresh current input / output device in GUI
  81. void MidiInOutEditor::SetCurrentDevice(CComboBox &combo, MidiDevice::ID device)
  82. {
  83. int items = combo.GetCount();
  84. for(int i = 0; i < items; i++)
  85. {
  86. if(static_cast<MidiDevice::ID>(combo.GetItemData(i)) == device)
  87. {
  88. combo.SetCurSel(i);
  89. break;
  90. }
  91. }
  92. }
  93. static void IOChanged(MidiInOut &plugin, CComboBox &combo, PlugParamIndex param)
  94. {
  95. // Update device ID and notify plugin.
  96. MidiDevice::ID newDevice = static_cast<MidiDevice::ID>(combo.GetItemData(combo.GetCurSel()));
  97. plugin.SetParameter(param, MidiInOut::DeviceIDToParameter(newDevice));
  98. plugin.AutomateParameter(param);
  99. }
  100. void MidiInOutEditor::OnInputChanged()
  101. {
  102. IOChanged(static_cast<MidiInOut &>(m_VstPlugin), m_inputCombo, MidiInOut::kInputParameter);
  103. }
  104. void MidiInOutEditor::OnOutputChanged()
  105. {
  106. IOChanged(static_cast<MidiInOut &>(m_VstPlugin), m_outputCombo, MidiInOut::kOutputParameter);
  107. }
  108. void MidiInOutEditor::OnLatencyChanged()
  109. {
  110. MidiInOut &plugin = static_cast<MidiInOut &>(m_VstPlugin);
  111. double latency = 0.0;
  112. if(!m_locked && m_latencyEdit.GetDecimalValue(latency))
  113. {
  114. plugin.m_latency = latency * (1.0 / 1000.0);
  115. plugin.SetModified();
  116. }
  117. }
  118. void MidiInOutEditor::OnTimingMessagesChanged()
  119. {
  120. if(!m_locked)
  121. {
  122. MidiInOut &plugin = static_cast<MidiInOut &>(m_VstPlugin);
  123. plugin.m_sendTimingInfo = IsDlgButtonChecked(IDC_CHECK1) != BST_UNCHECKED;
  124. plugin.SetModified();
  125. }
  126. }
  127. OPENMPT_NAMESPACE_END
  128. #endif // MODPLUG_TRACKER