Fix wine build
[carla.git] / source / native-plugins / midi-queue.hpp
blob93259a00e1b43aaaadee0defd810a77ee3d1f6b3
1 /*
2 * MIDI Event Queue
3 * Copyright (C) 2012-2020 Filipe Coelho <falktx@falktx.com>
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.
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.
15 * For a full copy of the GNU General Public License see the COPYING file
18 #ifndef MIDI_QUEUE_HPP_INCLUDED
19 #define MIDI_QUEUE_HPP_INCLUDED
21 #include "CarlaMutex.hpp"
23 template<uint16_t MAX_SIZE>
24 class MIDIEventQueue
26 public:
27 MIDIEventQueue() noexcept
28 : index(0),
29 empty(true),
30 full(false),
31 mutex() {}
33 bool isEmpty() const noexcept
35 return empty;
38 bool isNotEmpty() const noexcept
40 return !empty;
43 bool isFull() const noexcept
45 return full;
48 CarlaMutex& getMutex() noexcept
50 return mutex;
53 bool put(unsigned char d1, unsigned char d2, unsigned char d3)
55 CARLA_SAFE_ASSERT_RETURN(d1 != 0, false);
57 if (full)
58 return false;
60 for (unsigned short i=0; i < MAX_SIZE; i++)
62 if (data[i].d1 == 0)
64 data[i].d1 = d1;
65 data[i].d2 = d2;
66 data[i].d3 = d3;
67 empty = false;
68 full = (i == MAX_SIZE-1);
69 break;
73 return true;
76 bool get(uint8_t& d1, uint8_t& d2, uint8_t& d3)
78 if (empty)
79 return false;
81 full = false;
83 if (data[index].d1 == 0)
85 index = 0;
86 empty = true;
88 return false;
91 d1 = data[index].d1;
92 d2 = data[index].d2;
93 d3 = data[index].d3;
95 data[index].d1 = data[index].d2 = data[index].d3 = 0;
96 empty = false;
97 ++index;
99 return true;
102 bool tryToCopyDataFrom(MIDIEventQueue& queue) noexcept
104 const CarlaMutexTryLocker cml(queue.mutex);
106 if (cml.wasNotLocked())
107 return false;
109 // copy data from queue
110 carla_copyStruct(data, queue.data);
111 index = queue.index;
112 empty = queue.empty;
113 full = queue.full;
115 // reset queque
116 carla_zeroStruct(queue.data);
117 queue.index = 0;
118 queue.empty = true;
119 queue.full = false;
121 return true;
124 private:
125 struct MIDIEvent {
126 uint8_t d1, d2, d3;
128 MIDIEvent() noexcept
129 : d1(0), d2(0), d3(0) {}
132 MIDIEvent data[MAX_SIZE];
133 uint16_t index;
134 volatile bool empty, full;
136 CarlaMutex mutex;
139 #endif // MIDI_QUEUE_HPP_INCLUDED