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.

196 lines
4.8KB

  1. /*
  2. * Carla Tests
  3. * Copyright (C) 2013-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. #include "RtLinkedList.hpp"
  18. #include "CarlaString.hpp"
  19. #include "CarlaMutex.hpp"
  20. const unsigned short MIN_RT_EVENTS = 5;
  21. const unsigned short MAX_RT_EVENTS = 10;
  22. struct MyData {
  23. CarlaString str;
  24. int id;
  25. MyData() noexcept
  26. : id(-1) {}
  27. MyData(int i) noexcept
  28. : str(i),
  29. id(i) {}
  30. };
  31. struct PostRtEvents {
  32. CarlaMutex mutex;
  33. RtLinkedList<MyData>::Pool dataPool;
  34. RtLinkedList<MyData> data;
  35. RtLinkedList<MyData> dataPendingRT;
  36. PostRtEvents() noexcept
  37. : dataPool(MIN_RT_EVENTS, MAX_RT_EVENTS),
  38. data(dataPool, true),
  39. dataPendingRT(dataPool, true) {}
  40. ~PostRtEvents() noexcept
  41. {
  42. clear();
  43. }
  44. void appendRT(const MyData& event) noexcept
  45. {
  46. dataPendingRT.append(event);
  47. }
  48. void clear() noexcept
  49. {
  50. mutex.lock();
  51. data.clear();
  52. dataPendingRT.clear();
  53. mutex.unlock();
  54. }
  55. void trySplice() noexcept
  56. {
  57. if (mutex.tryLock())
  58. {
  59. dataPendingRT.spliceAppendTo(data);
  60. mutex.unlock();
  61. }
  62. }
  63. } postRtEvents;
  64. void run5Tests()
  65. {
  66. unsigned short k = 0;
  67. MyData allMyData[MAX_RT_EVENTS];
  68. // Make a safe copy of events while clearing them
  69. postRtEvents.mutex.lock();
  70. while (! postRtEvents.data.isEmpty())
  71. {
  72. MyData& my(postRtEvents.data.getFirst(true));
  73. allMyData[k++] = my;
  74. }
  75. postRtEvents.mutex.unlock();
  76. carla_stdout("Post-Rt Event Count: %i", k);
  77. assert(k == 5);
  78. // data should be empty now
  79. assert(postRtEvents.data.count() == 0);
  80. assert(postRtEvents.data.isEmpty());
  81. assert(postRtEvents.dataPendingRT.count() == 0);
  82. assert(postRtEvents.dataPendingRT.isEmpty());
  83. // Handle events now
  84. for (unsigned short i=0; i < k; ++i)
  85. {
  86. const MyData& my(allMyData[i]);
  87. carla_stdout("Got data: %i %s", my.id, my.str.buffer());
  88. }
  89. }
  90. int main()
  91. {
  92. MyData m1(1);
  93. MyData m2(2);
  94. MyData m3(3);
  95. MyData m4(4);
  96. MyData m5(5);
  97. // start
  98. assert(postRtEvents.data.count() == 0);
  99. assert(postRtEvents.data.isEmpty());
  100. assert(postRtEvents.dataPendingRT.count() == 0);
  101. assert(postRtEvents.dataPendingRT.isEmpty());
  102. // single append
  103. postRtEvents.appendRT(m1);
  104. postRtEvents.trySplice();
  105. assert(postRtEvents.data.count() == 1);
  106. assert(postRtEvents.dataPendingRT.count() == 0);
  107. // +3 appends
  108. postRtEvents.appendRT(m2);
  109. postRtEvents.appendRT(m4);
  110. postRtEvents.appendRT(m3);
  111. assert(postRtEvents.data.count() == 1);
  112. assert(postRtEvents.dataPendingRT.count() == 3);
  113. postRtEvents.trySplice();
  114. assert(postRtEvents.data.count() == 4);
  115. assert(postRtEvents.dataPendingRT.count() == 0);
  116. for (RtLinkedList<MyData>::Itenerator it = postRtEvents.data.begin(); it.valid(); it.next())
  117. {
  118. const MyData& my(it.getValue());
  119. carla_stdout("FOR DATA!!!: %i %s", my.id, my.str.buffer());
  120. if (my.id == 1)
  121. {
  122. // +1 append at
  123. postRtEvents.dataPendingRT.insertAt(m5, it);
  124. assert(postRtEvents.data.count() == 4);
  125. assert(postRtEvents.dataPendingRT.count() == 1);
  126. postRtEvents.trySplice();
  127. assert(postRtEvents.data.count() == 5);
  128. assert(postRtEvents.dataPendingRT.count() == 0);
  129. }
  130. }
  131. run5Tests();
  132. // reset
  133. postRtEvents.clear();
  134. assert(postRtEvents.data.count() == 0);
  135. assert(postRtEvents.data.isEmpty());
  136. assert(postRtEvents.dataPendingRT.count() == 0);
  137. assert(postRtEvents.dataPendingRT.isEmpty());
  138. // test non-rt
  139. const unsigned int CARLA_EVENT_DATA_ATOM = 0x01;
  140. const unsigned int CARLA_EVENT_DATA_MIDI_LL = 0x04;
  141. LinkedList<uint32_t> evIns, evOuts;
  142. evIns.append(CARLA_EVENT_DATA_ATOM);
  143. evOuts.append(CARLA_EVENT_DATA_ATOM);
  144. evOuts.append(CARLA_EVENT_DATA_MIDI_LL);
  145. if (evIns.count() > 0)
  146. {
  147. for (size_t j=0, count=evIns.count(); j < count; ++j)
  148. {
  149. const uint32_t& type(evIns.getAt(j));
  150. if (type == CARLA_EVENT_DATA_ATOM)
  151. pass();
  152. else if (type == CARLA_EVENT_DATA_MIDI_LL)
  153. pass();
  154. }
  155. }
  156. evIns.clear();
  157. evOuts.clear();
  158. return 0;
  159. }