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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * Carla Engine utils
  3. * Copyright (C) 2011-2018 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 "water/midi/MidiBuffer.h"
  23. CARLA_BACKEND_START_NAMESPACE
  24. // -----------------------------------------------------------------------
  25. // Maximum internal pre-allocated events
  26. const ushort kMaxEngineEventInternalCount = 2048;
  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 kEngineTypeRtAudio:
  38. return "kEngineTypeRtAudio";
  39. case kEngineTypePlugin:
  40. return "kEngineTypePlugin";
  41. case kEngineTypeBridge:
  42. return "kEngineTypeBridge";
  43. }
  44. carla_stderr("CarlaBackend::EngineType2Str(%i) - invalid type", type);
  45. return nullptr;
  46. }
  47. static inline
  48. const char* EnginePortType2Str(const EnginePortType type) noexcept
  49. {
  50. switch (type)
  51. {
  52. case kEnginePortTypeNull:
  53. return "kEnginePortTypeNull";
  54. case kEnginePortTypeAudio:
  55. return "kEnginePortTypeAudio";
  56. case kEnginePortTypeCV:
  57. return "kEnginePortTypeCV";
  58. case kEnginePortTypeEvent:
  59. return "kEnginePortTypeEvent";
  60. }
  61. carla_stderr("CarlaBackend::EnginePortType2Str(%i) - invalid type", type);
  62. return nullptr;
  63. }
  64. static inline
  65. const char* EngineEventType2Str(const EngineEventType type) noexcept
  66. {
  67. switch (type)
  68. {
  69. case kEngineEventTypeNull:
  70. return "kEngineEventTypeNull";
  71. case kEngineEventTypeControl:
  72. return "kEngineEventTypeControl";
  73. case kEngineEventTypeMidi:
  74. return "kEngineEventTypeMidi";
  75. }
  76. carla_stderr("CarlaBackend::EngineEventType2Str(%i) - invalid type", type);
  77. return nullptr;
  78. }
  79. static inline
  80. const char* EngineControlEventType2Str(const EngineControlEventType type) noexcept
  81. {
  82. switch (type)
  83. {
  84. case kEngineControlEventTypeNull:
  85. return "kEngineNullEvent";
  86. case kEngineControlEventTypeParameter:
  87. return "kEngineControlEventTypeParameter";
  88. case kEngineControlEventTypeMidiBank:
  89. return "kEngineControlEventTypeMidiBank";
  90. case kEngineControlEventTypeMidiProgram:
  91. return "kEngineControlEventTypeMidiProgram";
  92. case kEngineControlEventTypeAllSoundOff:
  93. return "kEngineControlEventTypeAllSoundOff";
  94. case kEngineControlEventTypeAllNotesOff:
  95. return "kEngineControlEventTypeAllNotesOff";
  96. }
  97. carla_stderr("CarlaBackend::EngineControlEventType2Str(%i) - invalid type", type);
  98. return nullptr;
  99. }
  100. // -----------------------------------------------------------------------
  101. static inline
  102. void fillEngineEventsFromWaterMidiBuffer(EngineEvent engineEvents[kMaxEngineEventInternalCount], const water::MidiBuffer& midiBuffer)
  103. {
  104. const uint8_t* midiData;
  105. int numBytes, sampleNumber;
  106. ushort engineEventIndex = 0;
  107. for (ushort i=0; i < kMaxEngineEventInternalCount; ++i)
  108. {
  109. const EngineEvent& engineEvent(engineEvents[i]);
  110. if (engineEvent.type != kEngineEventTypeNull)
  111. continue;
  112. engineEventIndex = i;
  113. break;
  114. }
  115. for (water::MidiBuffer::Iterator midiBufferIterator(midiBuffer); midiBufferIterator.getNextEvent(midiData, numBytes, sampleNumber) && engineEventIndex < kMaxEngineEventInternalCount;)
  116. {
  117. CARLA_SAFE_ASSERT_CONTINUE(numBytes > 0);
  118. CARLA_SAFE_ASSERT_CONTINUE(sampleNumber >= 0);
  119. CARLA_SAFE_ASSERT_CONTINUE(numBytes < 0xFF /* uint8_t max */);
  120. EngineEvent& engineEvent(engineEvents[engineEventIndex++]);
  121. engineEvent.time = static_cast<uint32_t>(sampleNumber);
  122. engineEvent.fillFromMidiData(static_cast<uint8_t>(numBytes), midiData, 0);
  123. }
  124. }
  125. // -----------------------------------------------------------------------
  126. static inline
  127. void fillWaterMidiBufferFromEngineEvents(water::MidiBuffer& midiBuffer, const EngineEvent engineEvents[kMaxEngineEventInternalCount])
  128. {
  129. uint8_t size = 0;
  130. uint8_t mdata[3] = { 0, 0, 0 };
  131. uint8_t mdataTmp[EngineMidiEvent::kDataSize];
  132. const uint8_t* mdataPtr;
  133. for (ushort i=0; i < kMaxEngineEventInternalCount; ++i)
  134. {
  135. const EngineEvent& engineEvent(engineEvents[i]);
  136. /**/ if (engineEvent.type == kEngineEventTypeNull)
  137. {
  138. break;
  139. }
  140. else if (engineEvent.type == kEngineEventTypeControl)
  141. {
  142. const EngineControlEvent& ctrlEvent(engineEvent.ctrl);
  143. size = ctrlEvent.convertToMidiData(engineEvent.channel, mdata);
  144. mdataPtr = mdata;
  145. }
  146. else if (engineEvent.type == kEngineEventTypeMidi)
  147. {
  148. const EngineMidiEvent& midiEvent(engineEvent.midi);
  149. size = midiEvent.size;
  150. CARLA_SAFE_ASSERT_CONTINUE(size > 0);
  151. if (size > EngineMidiEvent::kDataSize)
  152. {
  153. CARLA_SAFE_ASSERT_CONTINUE(midiEvent.dataExt != nullptr);
  154. mdataPtr = midiEvent.dataExt;
  155. }
  156. else
  157. {
  158. // set first byte
  159. mdataTmp[0] = static_cast<uint8_t>(midiEvent.data[0] | (engineEvent.channel & MIDI_CHANNEL_BIT));
  160. // copy rest
  161. carla_copy<uint8_t>(mdataTmp+1, midiEvent.data+1, size-1);
  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