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.

midi-queue.hpp 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * MIDI Event Queue
  3. * Copyright (C) 2012-2020 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * 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 COPYING file
  16. */
  17. #ifndef MIDI_QUEUE_HPP_INCLUDED
  18. #define MIDI_QUEUE_HPP_INCLUDED
  19. #include "CarlaMutex.hpp"
  20. template<uint16_t MAX_SIZE>
  21. class MIDIEventQueue
  22. {
  23. public:
  24. MIDIEventQueue() noexcept
  25. : index(0),
  26. empty(true),
  27. full(false),
  28. mutex() {}
  29. bool isEmpty() const noexcept
  30. {
  31. return empty;
  32. }
  33. bool isNotEmpty() const noexcept
  34. {
  35. return !empty;
  36. }
  37. bool isFull() const noexcept
  38. {
  39. return full;
  40. }
  41. CarlaMutex& getMutex() noexcept
  42. {
  43. return mutex;
  44. }
  45. bool put(unsigned char d1, unsigned char d2, unsigned char d3)
  46. {
  47. CARLA_SAFE_ASSERT_RETURN(d1 != 0, false);
  48. if (full)
  49. return false;
  50. for (unsigned short i=0; i < MAX_SIZE; i++)
  51. {
  52. if (data[i].d1 == 0)
  53. {
  54. data[i].d1 = d1;
  55. data[i].d2 = d2;
  56. data[i].d3 = d3;
  57. empty = false;
  58. full = (i == MAX_SIZE-1);
  59. break;
  60. }
  61. }
  62. return true;
  63. }
  64. bool get(uint8_t& d1, uint8_t& d2, uint8_t& d3)
  65. {
  66. if (empty)
  67. return false;
  68. full = false;
  69. if (data[index].d1 == 0)
  70. {
  71. index = 0;
  72. empty = true;
  73. return false;
  74. }
  75. d1 = data[index].d1;
  76. d2 = data[index].d2;
  77. d3 = data[index].d3;
  78. data[index].d1 = data[index].d2 = data[index].d3 = 0;
  79. empty = false;
  80. ++index;
  81. return true;
  82. }
  83. bool tryToCopyDataFrom(MIDIEventQueue& queue) noexcept
  84. {
  85. const CarlaMutexTryLocker cml(queue.mutex);
  86. if (cml.wasNotLocked())
  87. return false;
  88. // copy data from queue
  89. carla_copyStruct(data, queue.data);
  90. index = queue.index;
  91. empty = queue.empty;
  92. full = queue.full;
  93. // reset queque
  94. carla_zeroStruct(queue.data);
  95. queue.index = 0;
  96. queue.empty = true;
  97. queue.full = false;
  98. return true;
  99. }
  100. private:
  101. struct MIDIEvent {
  102. uint8_t d1, d2, d3;
  103. MIDIEvent() noexcept
  104. : d1(0), d2(0), d3(0) {}
  105. };
  106. MIDIEvent data[MAX_SIZE];
  107. uint16_t index;
  108. volatile bool empty, full;
  109. CarlaMutex mutex;
  110. };
  111. #endif // MIDI_QUEUE_HPP_INCLUDED