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.

238 lines
7.0KB

  1. /*
  2. * Carla Engine utils
  3. * Copyright (C) 2011-2022 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 kEngineTypeJuce:
  38. return "kEngineTypeJuce";
  39. case kEngineTypeRtAudio:
  40. return "kEngineTypeRtAudio";
  41. case kEngineTypeSDL:
  42. return "kEngineTypeSDL";
  43. case kEngineTypePlugin:
  44. return "kEngineTypePlugin";
  45. case kEngineTypeBridge:
  46. return "kEngineTypeBridge";
  47. case kEngineTypeDummy:
  48. return "kEngineTypeDummy";
  49. }
  50. carla_stderr("CarlaBackend::EngineType2Str(%i) - invalid type", type);
  51. return nullptr;
  52. }
  53. static inline
  54. const char* EnginePortType2Str(const EnginePortType type) noexcept
  55. {
  56. switch (type)
  57. {
  58. case kEnginePortTypeNull:
  59. return "kEnginePortTypeNull";
  60. case kEnginePortTypeAudio:
  61. return "kEnginePortTypeAudio";
  62. case kEnginePortTypeCV:
  63. return "kEnginePortTypeCV";
  64. case kEnginePortTypeEvent:
  65. return "kEnginePortTypeEvent";
  66. }
  67. carla_stderr("CarlaBackend::EnginePortType2Str(%i) - invalid type", type);
  68. return nullptr;
  69. }
  70. static inline
  71. const char* EngineEventType2Str(const EngineEventType type) noexcept
  72. {
  73. switch (type)
  74. {
  75. case kEngineEventTypeNull:
  76. return "kEngineEventTypeNull";
  77. case kEngineEventTypeControl:
  78. return "kEngineEventTypeControl";
  79. case kEngineEventTypeMidi:
  80. return "kEngineEventTypeMidi";
  81. }
  82. carla_stderr("CarlaBackend::EngineEventType2Str(%i) - invalid type", type);
  83. return nullptr;
  84. }
  85. static inline
  86. const char* EngineControlEventType2Str(const EngineControlEventType type) noexcept
  87. {
  88. switch (type)
  89. {
  90. case kEngineControlEventTypeNull:
  91. return "kEngineNullEvent";
  92. case kEngineControlEventTypeParameter:
  93. return "kEngineControlEventTypeParameter";
  94. case kEngineControlEventTypeMidiBank:
  95. return "kEngineControlEventTypeMidiBank";
  96. case kEngineControlEventTypeMidiProgram:
  97. return "kEngineControlEventTypeMidiProgram";
  98. case kEngineControlEventTypeAllSoundOff:
  99. return "kEngineControlEventTypeAllSoundOff";
  100. case kEngineControlEventTypeAllNotesOff:
  101. return "kEngineControlEventTypeAllNotesOff";
  102. }
  103. carla_stderr("CarlaBackend::EngineControlEventType2Str(%i) - invalid type", type);
  104. return nullptr;
  105. }
  106. // -----------------------------------------------------------------------
  107. static inline
  108. void fillEngineEventsFromWaterMidiBuffer(EngineEvent engineEvents[kMaxEngineEventInternalCount], const water::MidiBuffer& midiBuffer)
  109. {
  110. const uint8_t* midiData;
  111. int numBytes, sampleNumber;
  112. ushort engineEventIndex = 0;
  113. for (ushort i=0; i < kMaxEngineEventInternalCount; ++i)
  114. {
  115. const EngineEvent& engineEvent(engineEvents[i]);
  116. if (engineEvent.type != kEngineEventTypeNull)
  117. continue;
  118. engineEventIndex = i;
  119. break;
  120. }
  121. for (water::MidiBuffer::Iterator midiBufferIterator(midiBuffer); midiBufferIterator.getNextEvent(midiData, numBytes, sampleNumber) && engineEventIndex < kMaxEngineEventInternalCount;)
  122. {
  123. CARLA_SAFE_ASSERT_CONTINUE(numBytes > 0);
  124. CARLA_SAFE_ASSERT_CONTINUE(sampleNumber >= 0);
  125. CARLA_SAFE_ASSERT_CONTINUE(numBytes < 0xFF /* uint8_t max */);
  126. EngineEvent& engineEvent(engineEvents[engineEventIndex++]);
  127. engineEvent.time = static_cast<uint32_t>(sampleNumber);
  128. engineEvent.fillFromMidiData(static_cast<uint8_t>(numBytes), midiData, 0);
  129. }
  130. }
  131. // -----------------------------------------------------------------------
  132. static inline
  133. void fillWaterMidiBufferFromEngineEvents(water::MidiBuffer& midiBuffer, const EngineEvent engineEvents[kMaxEngineEventInternalCount])
  134. {
  135. uint8_t size = 0;
  136. uint8_t mdata[3] = { 0, 0, 0 };
  137. uint8_t mdataTmp[EngineMidiEvent::kDataSize];
  138. const uint8_t* mdataPtr;
  139. for (ushort i=0; i < kMaxEngineEventInternalCount; ++i)
  140. {
  141. const EngineEvent& engineEvent(engineEvents[i]);
  142. /**/ if (engineEvent.type == kEngineEventTypeNull)
  143. {
  144. break;
  145. }
  146. else if (engineEvent.type == kEngineEventTypeControl)
  147. {
  148. const EngineControlEvent& ctrlEvent(engineEvent.ctrl);
  149. size = ctrlEvent.convertToMidiData(engineEvent.channel, mdata);
  150. mdataPtr = mdata;
  151. }
  152. else if (engineEvent.type == kEngineEventTypeMidi)
  153. {
  154. const EngineMidiEvent& midiEvent(engineEvent.midi);
  155. size = midiEvent.size;
  156. CARLA_SAFE_ASSERT_CONTINUE(size > 0);
  157. if (size > EngineMidiEvent::kDataSize)
  158. {
  159. CARLA_SAFE_ASSERT_CONTINUE(midiEvent.dataExt != nullptr);
  160. mdataPtr = midiEvent.dataExt;
  161. }
  162. else
  163. {
  164. // set first byte
  165. mdataTmp[0] = static_cast<uint8_t>(midiEvent.data[0] | (engineEvent.channel & MIDI_CHANNEL_BIT));
  166. if (size > 1)
  167. {
  168. // copy rest
  169. carla_copy<uint8_t>(mdataTmp+1, midiEvent.data+1, size-1U);
  170. }
  171. // done
  172. mdataPtr = mdataTmp;
  173. }
  174. }
  175. else
  176. {
  177. continue;
  178. }
  179. if (size > 0)
  180. midiBuffer.addEvent(mdataPtr, static_cast<int>(size), static_cast<int>(engineEvent.time));
  181. }
  182. }
  183. // -------------------------------------------------------------------
  184. // Helper classes
  185. class ScopedEngineEnvironmentLocker
  186. {
  187. public:
  188. ScopedEngineEnvironmentLocker(CarlaEngine* const engine) noexcept;
  189. ~ScopedEngineEnvironmentLocker() noexcept;
  190. private:
  191. CarlaEngine::ProtectedData* const pData;
  192. CARLA_PREVENT_HEAP_ALLOCATION
  193. CARLA_DECLARE_NON_COPYABLE(ScopedEngineEnvironmentLocker)
  194. };
  195. // -----------------------------------------------------------------------
  196. CARLA_BACKEND_END_NAMESPACE
  197. #endif // CARLA_ENGINE_UTILS_HPP_INCLUDED