DISTRHO Plugin Framework
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

158 lines
4.7KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2019 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. * or without fee is hereby granted, provided that the above copyright notice and this
  7. * permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  10. * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  11. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  12. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  13. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include "DistrhoUI.hpp"
  17. #include "Window.hpp"
  18. #include "KeyboardWidget.hpp"
  19. START_NAMESPACE_DISTRHO
  20. // -----------------------------------------------------------------------------------------------------------
  21. class MidiKeyboardExampleUI : public UI,
  22. public KeyboardWidget::Callback
  23. {
  24. public:
  25. /* constructor */
  26. MidiKeyboardExampleUI()
  27. : UI(kUIWidth, kUIHeight),
  28. fKeyboardWidget(getParentWindow())
  29. {
  30. const uint keyboardDeltaWidth = kUIWidth - fKeyboardWidget.getWidth();
  31. fKeyboardWidget.setAbsoluteX(keyboardDeltaWidth / 2);
  32. fKeyboardWidget.setAbsoluteY(kUIHeight - fKeyboardWidget.getHeight() - 4);
  33. fKeyboardWidget.setCallback(this);
  34. // Add a min-size constraint to the window, to make sure that it can't become too small
  35. setGeometryConstraints(kUIWidth, kUIHeight, true, true);
  36. // Avoid key repeat when playing notes using the computer keyboard
  37. getParentWindow().setIgnoringKeyRepeat(true);
  38. }
  39. protected:
  40. /* --------------------------------------------------------------------------------------------------------
  41. * DSP/Plugin Callbacks */
  42. /**
  43. A parameter has changed on the plugin side.
  44. This is called by the host to inform the UI about parameter changes.
  45. This plugin does not have any parameters, so we can leave this blank.
  46. */
  47. void parameterChanged(uint32_t, float) override
  48. {
  49. }
  50. /* --------------------------------------------------------------------------------------------------------
  51. * Widget Callbacks */
  52. /**
  53. The OpenGL drawing function.
  54. Here, we set a custom background color.
  55. */
  56. void onDisplay() override
  57. {
  58. glClearColor(17.f / 255.f,
  59. 17.f / 255.f,
  60. 17.f / 255.f,
  61. 17.f / 255.f);
  62. glClear(GL_COLOR_BUFFER_BIT);
  63. }
  64. /**
  65. Allow playing notes using the bottom and top rows of the computer keyboard.
  66. */
  67. bool onKeyboard(const KeyboardEvent& ev) override
  68. {
  69. // Offset from C4
  70. int offset = -1;
  71. const int keyjazzKeyCount = 26;
  72. const uint keyjazzKeysQwerty[keyjazzKeyCount] = {'z', 's', 'x', 'd', 'c', 'v', 'g', 'b', 'h', 'n', 'j', 'm', ',', 'q', '2', 'w', '3', 'e', 'r', '5', 't', '6', 'y', '7', 'u', 'i'};
  73. for (int i = 0; i < keyjazzKeyCount; ++i)
  74. {
  75. if (ev.key == keyjazzKeysQwerty[i])
  76. {
  77. offset = i;
  78. // acknowledge duplicate C5
  79. if (i > 12)
  80. {
  81. offset -= 1;
  82. }
  83. break;
  84. }
  85. }
  86. if (offset == -1)
  87. {
  88. return false;
  89. }
  90. fKeyboardWidget.setKeyPressed(offset, ev.press, true);
  91. return true;
  92. }
  93. /**
  94. Called when a note is pressed on the piano.
  95. */
  96. void keyboardKeyPressed(const uint keyIndex) override
  97. {
  98. const uint C4 = 60;
  99. sendNote(0, C4 + keyIndex, 127);
  100. }
  101. /**
  102. Called when a note is released on the piano.
  103. */
  104. void keyboardKeyReleased(const uint keyIndex) override
  105. {
  106. const uint C4 = 60;
  107. sendNote(0, C4 + keyIndex, 0);
  108. }
  109. // -------------------------------------------------------------------------------------------------------
  110. private:
  111. static const int kUIWidth = 750;
  112. static const int kUIHeight = 124;
  113. KeyboardWidget fKeyboardWidget;
  114. /**
  115. Set our UI class as non-copyable and add a leak detector just in case.
  116. */
  117. DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(MidiKeyboardExampleUI)
  118. };
  119. /* ------------------------------------------------------------------------------------------------------------
  120. * UI entry point, called by DPF to create a new UI instance. */
  121. UI *createUI()
  122. {
  123. return new MidiKeyboardExampleUI();
  124. }
  125. // -----------------------------------------------------------------------------------------------------------
  126. END_NAMESPACE_DISTRHO