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.

227 lines
6.7KB

  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 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. CARLA_SAFE_ASSERT_CONTINUE(numBytes < 0xFF /* uint8_t max */);
  121. EngineEvent& engineEvent(engineEvents[engineEventIndex++]);
  122. engineEvent.time = static_cast<uint32_t>(sampleNumber);
  123. engineEvent.fillFromMidiData(static_cast<uint8_t>(numBytes), midiData, 0);
  124. }
  125. }
  126. // -----------------------------------------------------------------------
  127. static inline
  128. void fillJuceMidiBufferFromEngineEvents(juce::MidiBuffer& midiBuffer, const EngineEvent engineEvents[kMaxEngineEventInternalCount])
  129. {
  130. uint8_t size = 0;
  131. uint8_t mdata[3] = { 0, 0, 0 };
  132. const uint8_t* mdataPtr = mdata;
  133. uint8_t mdataTmp[EngineMidiEvent::kDataSize];
  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. {
  153. mdataPtr = midiEvent.dataExt;
  154. }
  155. else
  156. {
  157. // copy
  158. carla_copy<uint8_t>(mdataTmp, midiEvent.data, size);
  159. // add channel
  160. mdataTmp[0] = static_cast<uint8_t>(mdataTmp[0] | (engineEvent.channel & MIDI_CHANNEL_BIT));
  161. // done
  162. mdataPtr = mdataTmp;
  163. }
  164. }
  165. else
  166. {
  167. continue;
  168. }
  169. if (size > 0)
  170. midiBuffer.addEvent(mdataPtr, static_cast<int>(size), static_cast<int>(engineEvent.time));
  171. }
  172. }
  173. // -------------------------------------------------------------------
  174. // Helper classes
  175. class ScopedEngineEnvironmentLocker
  176. {
  177. public:
  178. ScopedEngineEnvironmentLocker(CarlaEngine* const engine) noexcept;
  179. ~ScopedEngineEnvironmentLocker() noexcept;
  180. private:
  181. CarlaEngine::ProtectedData* const pData;
  182. CARLA_PREVENT_HEAP_ALLOCATION
  183. CARLA_DECLARE_NON_COPY_CLASS(ScopedEngineEnvironmentLocker)
  184. };
  185. // -----------------------------------------------------------------------
  186. CARLA_BACKEND_END_NAMESPACE
  187. #endif // CARLA_ENGINE_UTILS_HPP_INCLUDED