Audio plugin host https://kx.studio/carla
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.

CarlaEngineUtils.hpp 6.7KB

10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * Carla Engine utils
  3. * Copyright (C) 2011-2014 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it 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. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #ifndef CARLA_ENGINE_UTILS_HPP_INCLUDED
  18. #define CARLA_ENGINE_UTILS_HPP_INCLUDED
  19. #include "CarlaEngine.hpp"
  20. #include "CarlaUtils.hpp"
  21. #include "CarlaMIDI.h"
  22. #include "juce_audio_basics.h"
  23. CARLA_BACKEND_START_NAMESPACE
  24. // -----------------------------------------------------------------------
  25. // Maximum internal pre-allocated events
  26. const ushort kMaxEngineEventInternalCount = 512;
  27. // -----------------------------------------------------------------------
  28. static inline
  29. const char* EngineType2Str(const EngineType type) noexcept
  30. {
  31. switch (type)
  32. {
  33. case kEngineTypeNull:
  34. return "kEngineTypeNull";
  35. case kEngineTypeJack:
  36. return "kEngineTypeJack";
  37. case kEngineTypeJuce:
  38. return "kEngineTypeJuce";
  39. case kEngineTypeRtAudio:
  40. return "kEngineTypeRtAudio";
  41. case kEngineTypePlugin:
  42. return "kEngineTypePlugin";
  43. case kEngineTypeBridge:
  44. return "kEngineTypeBridge";
  45. }
  46. carla_stderr("CarlaBackend::EngineType2Str(%i) - invalid type", type);
  47. return nullptr;
  48. }
  49. static inline
  50. const char* EnginePortType2Str(const EnginePortType type) noexcept
  51. {
  52. switch (type)
  53. {
  54. case kEnginePortTypeNull:
  55. return "kEnginePortTypeNull";
  56. case kEnginePortTypeAudio:
  57. return "kEnginePortTypeAudio";
  58. case kEnginePortTypeCV:
  59. return "kEnginePortTypeCV";
  60. case kEnginePortTypeEvent:
  61. return "kEnginePortTypeEvent";
  62. }
  63. carla_stderr("CarlaBackend::EnginePortType2Str(%i) - invalid type", type);
  64. return nullptr;
  65. }
  66. static inline
  67. const char* EngineEventType2Str(const EngineEventType type) noexcept
  68. {
  69. switch (type)
  70. {
  71. case kEngineEventTypeNull:
  72. return "kEngineEventTypeNull";
  73. case kEngineEventTypeControl:
  74. return "kEngineEventTypeControl";
  75. case kEngineEventTypeMidi:
  76. return "kEngineEventTypeMidi";
  77. }
  78. carla_stderr("CarlaBackend::EngineEventType2Str(%i) - invalid type", type);
  79. return nullptr;
  80. }
  81. static inline
  82. const char* EngineControlEventType2Str(const EngineControlEventType type) noexcept
  83. {
  84. switch (type)
  85. {
  86. case kEngineControlEventTypeNull:
  87. return "kEngineNullEvent";
  88. case kEngineControlEventTypeParameter:
  89. return "kEngineControlEventTypeParameter";
  90. case kEngineControlEventTypeMidiBank:
  91. return "kEngineControlEventTypeMidiBank";
  92. case kEngineControlEventTypeMidiProgram:
  93. return "kEngineControlEventTypeMidiProgram";
  94. case kEngineControlEventTypeAllSoundOff:
  95. return "kEngineControlEventTypeAllSoundOff";
  96. case kEngineControlEventTypeAllNotesOff:
  97. return "kEngineControlEventTypeAllNotesOff";
  98. }
  99. carla_stderr("CarlaBackend::EngineControlEventType2Str(%i) - invalid type", type);
  100. return nullptr;
  101. }
  102. // -----------------------------------------------------------------------
  103. static inline
  104. void fillEngineEventsFromJuceMidiBuffer(EngineEvent engineEvents[kMaxEngineEventInternalCount], const juce::MidiBuffer& midiBuffer)
  105. {
  106. const uint8_t* midiData;
  107. int numBytes, sampleNumber;
  108. ushort engineEventIndex = 0;
  109. for (ushort i=0; i < kMaxEngineEventInternalCount; ++i)
  110. {
  111. const EngineEvent& engineEvent(engineEvents[i]);
  112. if (engineEvent.type != kEngineEventTypeNull)
  113. continue;
  114. engineEventIndex = i;
  115. break;
  116. }
  117. for (juce::MidiBuffer::Iterator midiBufferIterator(midiBuffer); midiBufferIterator.getNextEvent(midiData, numBytes, sampleNumber) && engineEventIndex < kMaxEngineEventInternalCount;)
  118. {
  119. CARLA_SAFE_ASSERT_CONTINUE(numBytes > 0);
  120. CARLA_SAFE_ASSERT_CONTINUE(sampleNumber >= 0);
  121. CARLA_SAFE_ASSERT_CONTINUE(numBytes < 0xFF /* uint8_t max */);
  122. EngineEvent& engineEvent(engineEvents[engineEventIndex++]);
  123. engineEvent.time = static_cast<uint32_t>(sampleNumber);
  124. engineEvent.fillFromMidiData(static_cast<uint8_t>(numBytes), midiData, 0);
  125. }
  126. }
  127. // -----------------------------------------------------------------------
  128. static inline
  129. void fillJuceMidiBufferFromEngineEvents(juce::MidiBuffer& midiBuffer, const EngineEvent engineEvents[kMaxEngineEventInternalCount])
  130. {
  131. uint8_t size = 0;
  132. uint8_t mdata[3] = { 0, 0, 0 };
  133. const uint8_t* mdataPtr = mdata;
  134. uint8_t mdataTmp[EngineMidiEvent::kDataSize];
  135. for (ushort i=0; i < kMaxEngineEventInternalCount; ++i)
  136. {
  137. const EngineEvent& engineEvent(engineEvents[i]);
  138. if (engineEvent.type == kEngineEventTypeNull)
  139. {
  140. break;
  141. }
  142. else if (engineEvent.type == kEngineEventTypeControl)
  143. {
  144. const EngineControlEvent& ctrlEvent(engineEvent.ctrl);
  145. ctrlEvent.convertToMidiData(engineEvent.channel, size, mdata);
  146. mdataPtr = mdata;
  147. }
  148. else if (engineEvent.type == kEngineEventTypeMidi)
  149. {
  150. const EngineMidiEvent& midiEvent(engineEvent.midi);
  151. size = midiEvent.size;
  152. if (size > EngineMidiEvent::kDataSize && midiEvent.dataExt != nullptr)
  153. {
  154. mdataPtr = midiEvent.dataExt;
  155. }
  156. else
  157. {
  158. // copy
  159. carla_copy<uint8_t>(mdataTmp, midiEvent.data, size);
  160. // add channel
  161. mdataTmp[0] |= (engineEvent.channel & MIDI_CHANNEL_BIT);
  162. // done
  163. mdataPtr = mdataTmp;
  164. }
  165. }
  166. else
  167. {
  168. continue;
  169. }
  170. if (size > 0)
  171. midiBuffer.addEvent(mdataPtr, static_cast<int>(size), static_cast<int>(engineEvent.time));
  172. }
  173. }
  174. // -------------------------------------------------------------------
  175. // Helper classes
  176. class ScopedEngineEnvironmentLocker
  177. {
  178. public:
  179. ScopedEngineEnvironmentLocker(CarlaEngine* const engine) noexcept;
  180. ~ScopedEngineEnvironmentLocker() noexcept;
  181. private:
  182. CarlaEngine::ProtectedData* const pData;
  183. CARLA_PREVENT_HEAP_ALLOCATION
  184. CARLA_DECLARE_NON_COPY_CLASS(ScopedEngineEnvironmentLocker)
  185. };
  186. // -----------------------------------------------------------------------
  187. CARLA_BACKEND_END_NAMESPACE
  188. #endif // CARLA_ENGINE_UTILS_HPP_INCLUDED