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.

236 lines
7.0KB

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