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.

264 lines
7.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCETICE project - Copyright 2009 by Lucio Asnaghi.
  4. JUCETICE is based around the JUCE library - "Jules' Utility Class Extensions"
  5. Copyright 2007 by Julian Storer.
  6. ------------------------------------------------------------------------------
  7. JUCE and JUCETICE can be redistributed and/or modified under the terms of
  8. the GNU General Public License, as published by the Free Software Foundation;
  9. either version 2 of the License, or (at your option) any later version.
  10. JUCE and JUCETICE are distributed in the hope that they will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with JUCE and JUCETICE; if not, visit www.gnu.org/licenses or write to
  16. Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  17. Boston, MA 02111-1307 USA
  18. ==============================================================================
  19. */
  20. BEGIN_JUCE_NAMESPACE
  21. //==============================================================================
  22. MidiAutomatable::MidiAutomatable()
  23. : controllerNumber (-1),
  24. #if 0
  25. transfer (MidiAutomatable::Linear),
  26. #endif
  27. midiAutomatorManager (0)
  28. {
  29. #if 0
  30. function = MakeDelegate (this, &MidiAutomatable::transferFunctionLinear);
  31. #endif
  32. }
  33. MidiAutomatable::~MidiAutomatable()
  34. {
  35. if (midiAutomatorManager)
  36. midiAutomatorManager->removeMidiAutomatable (this);
  37. }
  38. void MidiAutomatable::activateLearning ()
  39. {
  40. jassert (midiAutomatorManager != 0); // if you fallback here then you don't have registered your
  41. // midi automatable object to the manager before actually starting
  42. // the midi learn feature
  43. if (midiAutomatorManager)
  44. midiAutomatorManager->setActiveLearner (this);
  45. }
  46. void MidiAutomatable::setControllerNumber (const int control)
  47. {
  48. if (controllerNumber != control)
  49. {
  50. controllerNumber = control;
  51. if (midiAutomatorManager)
  52. midiAutomatorManager->registerMidiAutomatable (this);
  53. }
  54. }
  55. #if 0
  56. void MidiAutomatable::setTransferFunction (MidiTransferFunction newFunction)
  57. {
  58. switch (newFunction)
  59. {
  60. default:
  61. case MidiAutomatable::Linear:
  62. function = MakeDelegate (this, &MidiAutomatable::transferFunctionLinear);
  63. break;
  64. case MidiAutomatable::InvertedLinear:
  65. function = MakeDelegate (this, &MidiAutomatable::transferFunctionLinear);
  66. break;
  67. }
  68. transfer = newFunction;
  69. }
  70. #endif
  71. void MidiAutomatable::setMidiAutomatorManager (MidiAutomatorManager* newManager)
  72. {
  73. midiAutomatorManager = newManager;
  74. }
  75. #if 0
  76. float MidiAutomatable::transferFunctionLinear (int ccValue)
  77. {
  78. return ccValue * float_MidiScaler;
  79. }
  80. float MidiAutomatable::transferFunctionInvertedLinear (int ccValue)
  81. {
  82. return 1.0f - ccValue * float_MidiScaler;
  83. }
  84. #endif
  85. void MidiAutomatable::handleMidiPopupMenu (const MouseEvent& e)
  86. {
  87. PopupMenu menu, ccSubMenu;
  88. const int controllerNumber = getControllerNumber ();
  89. for (int i = 0; i < 128; i++)
  90. {
  91. ccSubMenu.addItem (i + 1000,
  92. "CC# " + String(i) + " " + MidiMessage::getControllerName (i),
  93. true,
  94. controllerNumber == i);
  95. }
  96. if (controllerNumber != -1)
  97. menu.addItem (-1, "Assigned to CC# " + String (controllerNumber), false);
  98. else
  99. menu.addItem (-1, "Not assigned", false);
  100. menu.addSeparator ();
  101. menu.addItem (1, "Midi Learn");
  102. menu.addSubMenu ("Set CC", ccSubMenu);
  103. menu.addItem (2, "Reset CC", controllerNumber != -1);
  104. int result = menu.show ();
  105. switch (result)
  106. {
  107. case 1:
  108. activateLearning ();
  109. break;
  110. case 2:
  111. setControllerNumber (-1);
  112. break;
  113. default:
  114. if (result >= 1000 && result < 1128)
  115. setControllerNumber (result - 1000);
  116. break;
  117. }
  118. }
  119. //==============================================================================
  120. MidiAutomatorManager::MidiAutomatorManager ()
  121. : activeLearner (0)
  122. {
  123. for (int i = 0; i < 128; i++)
  124. controllers.add (new Array<void*>);
  125. }
  126. MidiAutomatorManager::~MidiAutomatorManager ()
  127. {
  128. for (int i = 0; i < 128; i++)
  129. delete controllers.getUnchecked (i);
  130. }
  131. //==============================================================================
  132. void MidiAutomatorManager::registerMidiAutomatable (MidiAutomatable* object)
  133. {
  134. object->setMidiAutomatorManager (this);
  135. for (int i = 0; i < 128; i++)
  136. {
  137. Array<void*>* array = controllers.getUnchecked (i);
  138. if (array->contains (object))
  139. {
  140. array->remove (array->indexOf (object));
  141. break;
  142. }
  143. }
  144. if (object->getControllerNumber () != -1)
  145. {
  146. Array<void*>* array = controllers.getUnchecked (object->getControllerNumber ());
  147. array->add (object);
  148. }
  149. }
  150. //==============================================================================
  151. void MidiAutomatorManager::removeMidiAutomatable (MidiAutomatable* object)
  152. {
  153. if (activeLearner == object)
  154. activeLearner = 0;
  155. for (int i = 0; i < 128; i++)
  156. {
  157. Array<void*>* array = controllers.getUnchecked (i);
  158. if (array->contains (object))
  159. {
  160. array->remove (array->indexOf (object));
  161. break;
  162. }
  163. }
  164. }
  165. //==============================================================================
  166. void MidiAutomatorManager::clearMidiAutomatableFromCC (const int ccNumber)
  167. {
  168. jassert (ccNumber >= 0 && ccNumber < 128);
  169. Array<void*>* array = controllers.getUnchecked (ccNumber);
  170. array->clear();
  171. }
  172. //==============================================================================
  173. void MidiAutomatorManager::setActiveLearner (MidiAutomatable* object)
  174. {
  175. activeLearner = object;
  176. }
  177. //==============================================================================
  178. bool MidiAutomatorManager::handleMidiMessage (const MidiMessage& message)
  179. {
  180. bool messageWasHandled = false;
  181. if (message.isController ())
  182. {
  183. if (activeLearner != 0)
  184. {
  185. activeLearner->setControllerNumber (message.getControllerNumber ());
  186. activeLearner = 0;
  187. }
  188. else
  189. {
  190. Array<void*>* array = controllers.getUnchecked (message.getControllerNumber ());
  191. for (int i = 0; i < array->size (); i++)
  192. {
  193. MidiAutomatable* learnObject = (MidiAutomatable*) array->getUnchecked (i);
  194. messageWasHandled |= learnObject->handleMidiMessage (message);
  195. }
  196. }
  197. }
  198. return messageWasHandled;
  199. }
  200. //==============================================================================
  201. bool MidiAutomatorManager::handleMidiMessageBuffer (MidiBuffer& buffer)
  202. {
  203. int samplePosition;
  204. MidiMessage message (0xf4);
  205. MidiBuffer::Iterator it (buffer);
  206. bool messageWasHandled = false;
  207. while (it.getNextEvent (message, samplePosition))
  208. {
  209. messageWasHandled |= handleMidiMessage (message);
  210. }
  211. return messageWasHandled;
  212. }
  213. END_JUCE_NAMESPACE