merge new MTDM code from Fons' latest release.
[jack2.git] / common / JackMidiAsyncQueue.h
blob5f23eff482fa59532f6c1e237c29e5f0b575b641
1 /*
2 Copyright (C) 2010 Devin Anderson
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation; either version 2.1 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 #ifndef __JackMidiAsyncQueue__
21 #define __JackMidiAsyncQueue__
23 #include "JackMidiPort.h"
24 #include "JackMidiReadQueue.h"
25 #include "JackMidiWriteQueue.h"
26 #include "ringbuffer.h"
28 namespace Jack {
30 /**
31 * This is a MIDI message queue designed to allow one thread to pass MIDI
32 * messages to another thread (though it can also be used to buffer events
33 * internally). This is especially useful if the MIDI API you're
34 * attempting to interface with doesn't provide the ability to schedule
35 * MIDI events ahead of time and/or has blocking send/receive calls, as it
36 * allows a separate thread to handle input/output while the JACK process
37 * thread copies events from a MIDI buffer to this queue, or vice versa.
40 class SERVER_EXPORT JackMidiAsyncQueue:
41 public JackMidiReadQueue, public JackMidiWriteQueue {
43 private:
45 static const size_t INFO_SIZE =
46 sizeof(jack_nframes_t) + sizeof(size_t);
48 jack_ringbuffer_t *byte_ring;
49 jack_midi_data_t *data_buffer;
50 jack_midi_event_t dequeue_event;
51 jack_ringbuffer_t *info_ring;
52 size_t max_bytes;
54 public:
56 using JackMidiWriteQueue::EnqueueEvent;
58 /**
59 * Creates a new asynchronous MIDI message queue. The queue can store
60 * up to `max_messages` MIDI messages and up to `max_bytes` of MIDI
61 * data before it starts rejecting messages.
64 JackMidiAsyncQueue(size_t max_bytes=4096, size_t max_messages=1024);
66 virtual
67 ~JackMidiAsyncQueue();
69 /**
70 * Dequeues and returns a MIDI event. Returns '0' if there are no MIDI
71 * events available. This method may be overridden.
74 virtual jack_midi_event_t *
75 DequeueEvent();
77 /**
78 * Enqueues the MIDI event specified by the arguments. The return
79 * value indiciates whether or not the event was successfully enqueued.
80 * This method may be overridden.
83 virtual EnqueueResult
84 EnqueueEvent(jack_nframes_t time, size_t size,
85 jack_midi_data_t *buffer);
87 /**
88 * Returns the maximum size event that can be enqueued right *now*.
91 size_t
92 GetAvailableSpace();
98 #endif