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.

218 lines
6.3KB

  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 "juce_audio_basics.h"
  22. CARLA_BACKEND_START_NAMESPACE
  23. // -----------------------------------------------------------------------
  24. // Maximum internal pre-allocated events
  25. const ushort kMaxEngineEventInternalCount = 512;
  26. // -----------------------------------------------------------------------
  27. static inline
  28. const char* EngineType2Str(const EngineType type) noexcept
  29. {
  30. switch (type)
  31. {
  32. case kEngineTypeNull:
  33. return "kEngineTypeNull";
  34. case kEngineTypeJack:
  35. return "kEngineTypeJack";
  36. case kEngineTypeJuce:
  37. return "kEngineTypeJuce";
  38. case kEngineTypeRtAudio:
  39. return "kEngineTypeRtAudio";
  40. case kEngineTypePlugin:
  41. return "kEngineTypePlugin";
  42. case kEngineTypeBridge:
  43. return "kEngineTypeBridge";
  44. }
  45. carla_stderr("CarlaBackend::EngineType2Str(%i) - invalid type", type);
  46. return nullptr;
  47. }
  48. static inline
  49. const char* EnginePortType2Str(const EnginePortType type) noexcept
  50. {
  51. switch (type)
  52. {
  53. case kEnginePortTypeNull:
  54. return "kEnginePortTypeNull";
  55. case kEnginePortTypeAudio:
  56. return "kEnginePortTypeAudio";
  57. case kEnginePortTypeCV:
  58. return "kEnginePortTypeCV";
  59. case kEnginePortTypeEvent:
  60. return "kEnginePortTypeEvent";
  61. }
  62. carla_stderr("CarlaBackend::EnginePortType2Str(%i) - invalid type", type);
  63. return nullptr;
  64. }
  65. static inline
  66. const char* EngineEventType2Str(const EngineEventType type) noexcept
  67. {
  68. switch (type)
  69. {
  70. case kEngineEventTypeNull:
  71. return "kEngineEventTypeNull";
  72. case kEngineEventTypeControl:
  73. return "kEngineEventTypeControl";
  74. case kEngineEventTypeMidi:
  75. return "kEngineEventTypeMidi";
  76. }
  77. carla_stderr("CarlaBackend::EngineEventType2Str(%i) - invalid type", type);
  78. return nullptr;
  79. }
  80. static inline
  81. const char* EngineControlEventType2Str(const EngineControlEventType type) noexcept
  82. {
  83. switch (type)
  84. {
  85. case kEngineControlEventTypeNull:
  86. return "kEngineNullEvent";
  87. case kEngineControlEventTypeParameter:
  88. return "kEngineControlEventTypeParameter";
  89. case kEngineControlEventTypeMidiBank:
  90. return "kEngineControlEventTypeMidiBank";
  91. case kEngineControlEventTypeMidiProgram:
  92. return "kEngineControlEventTypeMidiProgram";
  93. case kEngineControlEventTypeAllSoundOff:
  94. return "kEngineControlEventTypeAllSoundOff";
  95. case kEngineControlEventTypeAllNotesOff:
  96. return "kEngineControlEventTypeAllNotesOff";
  97. }
  98. carla_stderr("CarlaBackend::EngineControlEventType2Str(%i) - invalid type", type);
  99. return nullptr;
  100. }
  101. // -----------------------------------------------------------------------
  102. static inline
  103. void fillEngineEventsFromJuceMidiBuffer(EngineEvent engineEvents[kMaxEngineEventInternalCount], const juce::MidiBuffer& midiBuffer)
  104. {
  105. const uint8_t* midiData;
  106. int numBytes, sampleNumber;
  107. ushort engineEventIndex = 0;
  108. for (ushort i=0; i < kMaxEngineEventInternalCount; ++i)
  109. {
  110. const EngineEvent& engineEvent(engineEvents[i]);
  111. if (engineEvent.type != kEngineEventTypeNull)
  112. continue;
  113. engineEventIndex = i;
  114. break;
  115. }
  116. for (juce::MidiBuffer::Iterator midiBufferIterator(midiBuffer); midiBufferIterator.getNextEvent(midiData, numBytes, sampleNumber) && engineEventIndex < kMaxEngineEventInternalCount;)
  117. {
  118. CARLA_SAFE_ASSERT_CONTINUE(numBytes > 0);
  119. CARLA_SAFE_ASSERT_CONTINUE(sampleNumber >= 0);
  120. if (numBytes > UINT8_MAX)
  121. continue;
  122. EngineEvent& engineEvent(engineEvents[engineEventIndex++]);
  123. engineEvent.time = static_cast<uint32_t>(sampleNumber);
  124. engineEvent.fillFromMidiData(static_cast<uint8_t>(numBytes), midiData);
  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. for (ushort i=0; i < kMaxEngineEventInternalCount; ++i)
  135. {
  136. const EngineEvent& engineEvent(engineEvents[i]);
  137. if (engineEvent.type == kEngineEventTypeNull)
  138. {
  139. break;
  140. }
  141. else if (engineEvent.type == kEngineEventTypeControl)
  142. {
  143. const EngineControlEvent& ctrlEvent(engineEvent.ctrl);
  144. ctrlEvent.convertToMidiData(engineEvent.channel, size, mdata);
  145. mdataPtr = mdata;
  146. }
  147. else if (engineEvent.type == kEngineEventTypeMidi)
  148. {
  149. const EngineMidiEvent& midiEvent(engineEvent.midi);
  150. size = midiEvent.size;
  151. if (size > EngineMidiEvent::kDataSize && midiEvent.dataExt != nullptr)
  152. mdataPtr = midiEvent.dataExt;
  153. else
  154. mdataPtr = midiEvent.data;
  155. }
  156. else
  157. {
  158. continue;
  159. }
  160. if (size > 0)
  161. midiBuffer.addEvent(mdataPtr, static_cast<int>(size), static_cast<int>(engineEvent.time));
  162. }
  163. }
  164. // -------------------------------------------------------------------
  165. // Helper classes
  166. class ScopedEngineEnvironmentLocker
  167. {
  168. public:
  169. ScopedEngineEnvironmentLocker(CarlaEngine* const engine) noexcept;
  170. ~ScopedEngineEnvironmentLocker() noexcept;
  171. private:
  172. CarlaEngine::ProtectedData* const pData;
  173. CARLA_PREVENT_HEAP_ALLOCATION
  174. CARLA_DECLARE_NON_COPY_CLASS(ScopedEngineEnvironmentLocker)
  175. };
  176. // -----------------------------------------------------------------------
  177. CARLA_BACKEND_END_NAMESPACE
  178. #endif // CARLA_ENGINE_UTILS_HPP_INCLUDED