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.

298 lines
10KB

  1. #ifndef MAINCOMPONENT_H_INCLUDED
  2. #define MAINCOMPONENT_H_INCLUDED
  3. #include "../JuceLibraryCode/JuceHeader.h"
  4. #include "Audio.h"
  5. #include "WaveshapeProgram.h"
  6. //==============================================================================
  7. /**
  8. A struct that handles the setup and layout of the DrumPadGridProgram
  9. */
  10. struct SynthGrid
  11. {
  12. SynthGrid (int cols, int rows)
  13. : numColumns (cols),
  14. numRows (rows)
  15. {
  16. constructGridFillArray();
  17. }
  18. /** Creates a GridFill object for each pad in the grid and sets its colour
  19. and fill before adding it to an array of GridFill objects
  20. */
  21. void constructGridFillArray()
  22. {
  23. gridFillArray.clear();
  24. for (int i = 0; i < numRows; ++i)
  25. {
  26. for (int j = 0; j < numColumns; ++j)
  27. {
  28. DrumPadGridProgram::GridFill fill;
  29. int padNum = (i * 5) + j;
  30. fill.colour = notes.contains (padNum) ? baseGridColour
  31. : tonics.contains (padNum) ? Colours::white
  32. : Colours::black;
  33. fill.fillType = DrumPadGridProgram::GridFill::FillType::gradient;
  34. gridFillArray.add (fill);
  35. }
  36. }
  37. }
  38. int getNoteNumberForPad (int x, int y) const
  39. {
  40. int xIndex = x / 3;
  41. int yIndex = y / 3;
  42. return 60 + ((4 - yIndex) * 5) + xIndex;
  43. }
  44. //==============================================================================
  45. int numColumns, numRows;
  46. float width, height;
  47. Array<DrumPadGridProgram::GridFill> gridFillArray;
  48. Colour baseGridColour = Colours::green;
  49. Colour touchColour = Colours::red;
  50. Array<int> tonics = { 4, 12, 20 };
  51. Array<int> notes = { 1, 3, 6, 7, 9, 11, 14, 15, 17, 19, 22, 24 };
  52. //==============================================================================
  53. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SynthGrid)
  54. };
  55. //==============================================================================
  56. /**
  57. The main component
  58. */
  59. class MainComponent : public Component,
  60. public TopologySource::Listener,
  61. private TouchSurface::Listener,
  62. private ControlButton::Listener
  63. {
  64. public:
  65. MainComponent()
  66. {
  67. setSize (600, 400);
  68. // Register MainContentComponent as a listener to the PhysicalTopologySource object
  69. topologySource.addListener (this);
  70. };
  71. ~MainComponent()
  72. {
  73. if (activeBlock != nullptr)
  74. detachActiveBlock();
  75. }
  76. void paint (Graphics& g) override
  77. {
  78. g.fillAll (Colours::lightgrey);
  79. g.drawText ("Connect a Lightpad Block to play.",
  80. getLocalBounds(), Justification::centred, false);
  81. }
  82. void resized() override {}
  83. /** Overridden from TopologySource::Listener, called when the topology changes */
  84. void topologyChanged() override
  85. {
  86. // Reset the activeBlock object
  87. if (activeBlock != nullptr)
  88. detachActiveBlock();
  89. // Get the array of currently connected Block objects from the PhysicalTopologySource
  90. auto blocks = topologySource.getCurrentTopology().blocks;
  91. // Iterate over the array of Block objects
  92. for (auto b : blocks)
  93. {
  94. // Find the first Lightpad
  95. if (b->getType() == Block::Type::lightPadBlock)
  96. {
  97. activeBlock = b;
  98. // Register MainContentComponent as a listener to the touch surface
  99. if (auto surface = activeBlock->getTouchSurface())
  100. surface->addListener (this);
  101. // Register MainContentComponent as a listener to any buttons
  102. for (auto button : activeBlock->getButtons())
  103. button->addListener (this);
  104. // Get the LEDGrid object from the Lightpad and set its program to the program for the current mode
  105. if (auto grid = activeBlock->getLEDGrid())
  106. {
  107. // Work out scale factors to translate X and Y touches to LED indexes
  108. scaleX = static_cast<float> (grid->getNumColumns() - 1) / activeBlock->getWidth();
  109. scaleY = static_cast<float> (grid->getNumRows() - 1) / activeBlock->getHeight();
  110. setLEDProgram (grid);
  111. }
  112. break;
  113. }
  114. }
  115. }
  116. private:
  117. /** Overridden from TouchSurface::Listener. Called when a Touch is received on the Lightpad */
  118. void touchChanged (TouchSurface&, const TouchSurface::Touch& touch) override
  119. {
  120. if (currentMode == waveformSelectionMode && touch.isTouchStart)
  121. {
  122. // Change the displayed waveshape to the next one
  123. ++waveshapeMode;
  124. if (waveshapeMode > 3)
  125. waveshapeMode = 0;
  126. waveshapeProgram->setWaveshapeType (static_cast<uint8> (waveshapeMode));
  127. }
  128. else if (currentMode == playMode)
  129. {
  130. // Translate X and Y touch events to LED indexes
  131. int xLed = roundToInt (touch.startX * scaleX);
  132. int yLed = roundToInt (touch.startY * scaleY);
  133. // Limit the number of touches per second
  134. constexpr int maxNumTouchMessagesPerSecond = 100;
  135. auto now = Time::getCurrentTime();
  136. clearOldTouchTimes (now);
  137. int midiChannel = waveshapeMode + 1;
  138. // Send the touch event to the DrumPadGridProgram and Audio class
  139. if (touch.isTouchStart)
  140. {
  141. gridProgram->startTouch (touch.startX, touch.startY);
  142. audio.noteOn (midiChannel, layout.getNoteNumberForPad (xLed, yLed), touch.z);
  143. }
  144. else if (touch.isTouchEnd)
  145. {
  146. gridProgram->endTouch (touch.startX, touch.startY);
  147. audio.noteOff (midiChannel, layout.getNoteNumberForPad (xLed, yLed), 1.0);
  148. }
  149. else
  150. {
  151. if (touchMessageTimesInLastSecond.size() > maxNumTouchMessagesPerSecond / 3)
  152. return;
  153. gridProgram->sendTouch (touch.x, touch.y, touch.z,
  154. layout.touchColour);
  155. // Send pitch change and pressure values to the Audio class
  156. audio.pitchChange (midiChannel, (touch.x - touch.startX) / activeBlock->getWidth());
  157. audio.pressureChange (midiChannel, touch.z);
  158. }
  159. touchMessageTimesInLastSecond.add (now);
  160. }
  161. }
  162. /** Overridden from ControlButton::Listener. Called when a button on the Lightpad is pressed */
  163. void buttonPressed (ControlButton&, Block::Timestamp) override {}
  164. /** Overridden from ControlButton::Listener. Called when a button on the Lightpad is released */
  165. void buttonReleased (ControlButton&, Block::Timestamp) override
  166. {
  167. // Turn any active synthesiser notes off
  168. audio.allNotesOff();
  169. // Switch modes
  170. if (currentMode == waveformSelectionMode)
  171. currentMode = playMode;
  172. else if (currentMode == playMode)
  173. currentMode = waveformSelectionMode;
  174. // Set the LEDGrid program to the new mode
  175. setLEDProgram (activeBlock->getLEDGrid());
  176. }
  177. /** Clears the old touch times */
  178. void clearOldTouchTimes (const Time now)
  179. {
  180. for (int i = touchMessageTimesInLastSecond.size(); --i >= 0;)
  181. if (touchMessageTimesInLastSecond.getReference(i) < now - juce::RelativeTime::seconds (0.33))
  182. touchMessageTimesInLastSecond.remove (i);
  183. }
  184. /** Removes TouchSurface and ControlButton listeners and sets activeBlock to nullptr */
  185. void detachActiveBlock()
  186. {
  187. if (auto surface = activeBlock->getTouchSurface())
  188. surface->removeListener (this);
  189. for (auto button : activeBlock->getButtons())
  190. button->removeListener (this);
  191. activeBlock = nullptr;
  192. }
  193. /** Sets the LEDGrid Program for the selected mode */
  194. void setLEDProgram (LEDGrid* grid)
  195. {
  196. if (currentMode == waveformSelectionMode)
  197. {
  198. // Create a new WaveshapeProgram for the LEDGrid
  199. waveshapeProgram = new WaveshapeProgram (*grid);
  200. // Set the LEDGrid program
  201. grid->setProgram (waveshapeProgram);
  202. // Initialise the program
  203. waveshapeProgram->setWaveshapeType (static_cast<uint8> (waveshapeMode));
  204. waveshapeProgram->generateWaveshapes();
  205. }
  206. else if (currentMode == playMode)
  207. {
  208. // Create a new DrumPadGridProgram for the LEDGrid
  209. gridProgram = new DrumPadGridProgram (*grid);
  210. // Set the LEDGrid program
  211. grid->setProgram (gridProgram);
  212. // Setup the grid layout
  213. gridProgram->setGridFills (layout.numColumns,
  214. layout.numRows,
  215. layout.gridFillArray);
  216. }
  217. }
  218. enum BlocksSynthMode
  219. {
  220. waveformSelectionMode = 0,
  221. playMode
  222. };
  223. BlocksSynthMode currentMode = playMode;
  224. //==============================================================================
  225. Audio audio;
  226. DrumPadGridProgram* gridProgram = nullptr;
  227. WaveshapeProgram* waveshapeProgram = nullptr;
  228. SynthGrid layout { 5, 5 };
  229. PhysicalTopologySource topologySource;
  230. Block::Ptr activeBlock;
  231. Array<juce::Time> touchMessageTimesInLastSecond;
  232. int waveshapeMode = 0;
  233. float scaleX = 0.0;
  234. float scaleY = 0.0;
  235. //==============================================================================
  236. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainComponent)
  237. };
  238. #endif // MAINCOMPONENT_H_INCLUDED