The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

361 lines
12KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. #pragma once
  20. #include "../JuceLibraryCode/JuceHeader.h"
  21. #include "Audio.h"
  22. #include "WaveshapeProgram.h"
  23. //==============================================================================
  24. /**
  25. A struct that handles the setup and layout of the DrumPadGridProgram
  26. */
  27. struct SynthGrid
  28. {
  29. SynthGrid (int cols, int rows)
  30. : numColumns (cols),
  31. numRows (rows)
  32. {
  33. constructGridFillArray();
  34. }
  35. /** Creates a GridFill object for each pad in the grid and sets its colour
  36. and fill before adding it to an array of GridFill objects
  37. */
  38. void constructGridFillArray()
  39. {
  40. gridFillArray.clear();
  41. for (int i = 0; i < numRows; ++i)
  42. {
  43. for (int j = 0; j < numColumns; ++j)
  44. {
  45. DrumPadGridProgram::GridFill fill;
  46. int padNum = (i * 5) + j;
  47. fill.colour = notes.contains (padNum) ? baseGridColour
  48. : tonics.contains (padNum) ? Colours::white
  49. : Colours::black;
  50. fill.fillType = DrumPadGridProgram::GridFill::FillType::gradient;
  51. gridFillArray.add (fill);
  52. }
  53. }
  54. }
  55. int getNoteNumberForPad (int x, int y) const
  56. {
  57. int xIndex = x / 3;
  58. int yIndex = y / 3;
  59. return 60 + ((4 - yIndex) * 5) + xIndex;
  60. }
  61. //==============================================================================
  62. int numColumns, numRows;
  63. float width, height;
  64. Array<DrumPadGridProgram::GridFill> gridFillArray;
  65. Colour baseGridColour = Colours::green;
  66. Colour touchColour = Colours::red;
  67. Array<int> tonics = { 4, 12, 20 };
  68. Array<int> notes = { 1, 3, 6, 7, 9, 11, 14, 15, 17, 19, 22, 24 };
  69. //==============================================================================
  70. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SynthGrid)
  71. };
  72. //==============================================================================
  73. /**
  74. The main component
  75. */
  76. class MainComponent : public Component,
  77. public TopologySource::Listener,
  78. private TouchSurface::Listener,
  79. private ControlButton::Listener,
  80. #if JUCE_IOS
  81. private Button::Listener,
  82. #endif
  83. private Timer
  84. {
  85. public:
  86. MainComponent()
  87. {
  88. setSize (600, 400);
  89. // Register MainContentComponent as a listener to the PhysicalTopologySource object
  90. topologySource.addListener (this);
  91. #if JUCE_IOS
  92. connectButton.setButtonText ("Connect");
  93. connectButton.addListener (this);
  94. addAndMakeVisible (connectButton);
  95. #endif
  96. };
  97. ~MainComponent()
  98. {
  99. if (activeBlock != nullptr)
  100. detachActiveBlock();
  101. }
  102. void paint (Graphics& g) override
  103. {
  104. g.setColour (getLookAndFeel().findColour (Label::textColourId));
  105. g.drawText ("Connect a Lightpad Block to play.",
  106. getLocalBounds(), Justification::centred, false);
  107. }
  108. void resized() override
  109. {
  110. #if JUCE_IOS
  111. connectButton.setBounds (getRight() - 100, 20, 80, 30);
  112. #endif
  113. }
  114. /** Overridden from TopologySource::Listener, called when the topology changes */
  115. void topologyChanged() override
  116. {
  117. // Reset the activeBlock object
  118. if (activeBlock != nullptr)
  119. detachActiveBlock();
  120. // Get the array of currently connected Block objects from the PhysicalTopologySource
  121. auto blocks = topologySource.getCurrentTopology().blocks;
  122. // Iterate over the array of Block objects
  123. for (auto b : blocks)
  124. {
  125. // Find the first Lightpad
  126. if (b->getType() == Block::Type::lightPadBlock)
  127. {
  128. activeBlock = b;
  129. // Register MainContentComponent as a listener to the touch surface
  130. if (auto surface = activeBlock->getTouchSurface())
  131. surface->addListener (this);
  132. // Register MainContentComponent as a listener to any buttons
  133. for (auto button : activeBlock->getButtons())
  134. button->addListener (this);
  135. // Get the LEDGrid object from the Lightpad and set its program to the program for the current mode
  136. if (auto grid = activeBlock->getLEDGrid())
  137. {
  138. // Work out scale factors to translate X and Y touches to LED indexes
  139. scaleX = static_cast<float> (grid->getNumColumns() - 1) / activeBlock->getWidth();
  140. scaleY = static_cast<float> (grid->getNumRows() - 1) / activeBlock->getHeight();
  141. setLEDProgram (*activeBlock);
  142. }
  143. break;
  144. }
  145. }
  146. }
  147. private:
  148. /** Overridden from TouchSurface::Listener. Called when a Touch is received on the Lightpad */
  149. void touchChanged (TouchSurface&, const TouchSurface::Touch& touch) override
  150. {
  151. if (currentMode == waveformSelectionMode && touch.isTouchStart && allowTouch)
  152. {
  153. // Change the displayed waveshape to the next one
  154. ++waveshapeMode;
  155. if (waveshapeMode > 3)
  156. waveshapeMode = 0;
  157. waveshapeProgram->setWaveshapeType (static_cast<uint8> (waveshapeMode));
  158. allowTouch = false;
  159. startTimer (250);
  160. }
  161. else if (currentMode == playMode)
  162. {
  163. // Translate X and Y touch events to LED indexes
  164. int xLed = roundToInt (touch.startX * scaleX);
  165. int yLed = roundToInt (touch.startY * scaleY);
  166. // Limit the number of touches per second
  167. constexpr int maxNumTouchMessagesPerSecond = 100;
  168. auto now = Time::getCurrentTime();
  169. clearOldTouchTimes (now);
  170. int midiChannel = waveshapeMode + 1;
  171. // Send the touch event to the DrumPadGridProgram and Audio class
  172. if (touch.isTouchStart)
  173. {
  174. gridProgram->startTouch (touch.startX, touch.startY);
  175. audio.noteOn (midiChannel, layout.getNoteNumberForPad (xLed, yLed), touch.z);
  176. }
  177. else if (touch.isTouchEnd)
  178. {
  179. gridProgram->endTouch (touch.startX, touch.startY);
  180. audio.noteOff (midiChannel, layout.getNoteNumberForPad (xLed, yLed), 1.0);
  181. }
  182. else
  183. {
  184. if (touchMessageTimesInLastSecond.size() > maxNumTouchMessagesPerSecond / 3)
  185. return;
  186. gridProgram->sendTouch (touch.x, touch.y, touch.z,
  187. layout.touchColour);
  188. // Send pitch change and pressure values to the Audio class
  189. audio.pitchChange (midiChannel, (touch.x - touch.startX) / activeBlock->getWidth());
  190. audio.pressureChange (midiChannel, touch.z);
  191. }
  192. touchMessageTimesInLastSecond.add (now);
  193. }
  194. }
  195. /** Overridden from ControlButton::Listener. Called when a button on the Lightpad is pressed */
  196. void buttonPressed (ControlButton&, Block::Timestamp) override {}
  197. /** Overridden from ControlButton::Listener. Called when a button on the Lightpad is released */
  198. void buttonReleased (ControlButton&, Block::Timestamp) override
  199. {
  200. // Turn any active synthesiser notes off
  201. audio.allNotesOff();
  202. // Switch modes
  203. if (currentMode == waveformSelectionMode)
  204. currentMode = playMode;
  205. else if (currentMode == playMode)
  206. currentMode = waveformSelectionMode;
  207. // Set the LEDGrid program to the new mode
  208. setLEDProgram (*activeBlock);
  209. }
  210. #if JUCE_IOS
  211. void buttonClicked (Button* b) override
  212. {
  213. if (b == &connectButton)
  214. BluetoothMidiDevicePairingDialogue::open();
  215. }
  216. #endif
  217. /** Clears the old touch times */
  218. void clearOldTouchTimes (const Time now)
  219. {
  220. for (int i = touchMessageTimesInLastSecond.size(); --i >= 0;)
  221. if (touchMessageTimesInLastSecond.getReference(i) < now - juce::RelativeTime::seconds (0.33))
  222. touchMessageTimesInLastSecond.remove (i);
  223. }
  224. /** Removes TouchSurface and ControlButton listeners and sets activeBlock to nullptr */
  225. void detachActiveBlock()
  226. {
  227. if (auto surface = activeBlock->getTouchSurface())
  228. surface->removeListener (this);
  229. for (auto button : activeBlock->getButtons())
  230. button->removeListener (this);
  231. activeBlock = nullptr;
  232. }
  233. /** Sets the LEDGrid Program for the selected mode */
  234. void setLEDProgram (Block& block)
  235. {
  236. if (currentMode == waveformSelectionMode)
  237. {
  238. // Create a new WaveshapeProgram for the LEDGrid
  239. waveshapeProgram = new WaveshapeProgram (block);
  240. // Set the LEDGrid program
  241. block.setProgram (waveshapeProgram);
  242. // Initialise the program
  243. waveshapeProgram->setWaveshapeType (static_cast<uint8> (waveshapeMode));
  244. waveshapeProgram->generateWaveshapes();
  245. }
  246. else if (currentMode == playMode)
  247. {
  248. // Create a new DrumPadGridProgram for the LEDGrid
  249. gridProgram = new DrumPadGridProgram (block);
  250. // Set the LEDGrid program
  251. auto error = block.setProgram (gridProgram);
  252. if (error.failed())
  253. {
  254. DBG (error.getErrorMessage());
  255. jassertfalse;
  256. }
  257. // Setup the grid layout
  258. gridProgram->setGridFills (layout.numColumns,
  259. layout.numRows,
  260. layout.gridFillArray);
  261. }
  262. }
  263. /** Stops touch events from triggering multiple waveshape mode changes */
  264. void timerCallback() override { allowTouch = true; }
  265. enum BlocksSynthMode
  266. {
  267. waveformSelectionMode = 0,
  268. playMode
  269. };
  270. BlocksSynthMode currentMode = playMode;
  271. //==============================================================================
  272. Audio audio;
  273. DrumPadGridProgram* gridProgram = nullptr;
  274. WaveshapeProgram* waveshapeProgram = nullptr;
  275. SynthGrid layout { 5, 5 };
  276. PhysicalTopologySource topologySource;
  277. Block::Ptr activeBlock;
  278. Array<juce::Time> touchMessageTimesInLastSecond;
  279. int waveshapeMode = 0;
  280. float scaleX = 0.0;
  281. float scaleY = 0.0;
  282. bool allowTouch = true;
  283. //==============================================================================
  284. #if JUCE_IOS
  285. TextButton connectButton;
  286. #endif
  287. //==============================================================================
  288. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainComponent)
  289. };