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.

229 lines
6.8KB

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