Merge tag 'v1.9.22' into LADI/main
[jack2.git] / common / JackMidiAsyncQueue.cpp
blob00ca2839a1f0add652b0c98d64f82629ac7c24ff
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 #include <new>
22 #include "JackMidiAsyncQueue.h"
24 using Jack::JackMidiAsyncQueue;
26 JackMidiAsyncQueue::JackMidiAsyncQueue(size_t max_bytes, size_t max_messages)
28 data_buffer = new jack_midi_data_t[max_bytes];
29 byte_ring = jack_ringbuffer_create((max_bytes * sizeof(jack_midi_data_t)) +
30 1);
31 if (byte_ring) {
32 info_ring = jack_ringbuffer_create((max_messages * INFO_SIZE) + 1);
33 if (info_ring) {
34 jack_ringbuffer_mlock(byte_ring);
35 jack_ringbuffer_mlock(info_ring);
36 this->max_bytes = max_bytes;
37 return;
39 jack_ringbuffer_free(byte_ring);
41 delete[] data_buffer;
42 throw std::bad_alloc();
45 JackMidiAsyncQueue::~JackMidiAsyncQueue()
47 jack_ringbuffer_free(byte_ring);
48 jack_ringbuffer_free(info_ring);
49 delete[] data_buffer;
52 jack_midi_event_t *
53 JackMidiAsyncQueue::DequeueEvent()
55 jack_midi_event_t *event = 0;
56 if (jack_ringbuffer_read_space(info_ring) >= INFO_SIZE) {
57 size_t size;
58 event = &dequeue_event;
59 jack_ringbuffer_read(info_ring, (char *) &(event->time),
60 sizeof(jack_nframes_t));
61 jack_ringbuffer_read(info_ring, (char *) &size,
62 sizeof(size_t));
63 jack_ringbuffer_read(byte_ring, (char *) data_buffer,
64 size * sizeof(jack_midi_data_t));
65 event->buffer = data_buffer;
66 event->size = size;
68 return event;
71 Jack::JackMidiWriteQueue::EnqueueResult
72 JackMidiAsyncQueue::EnqueueEvent(jack_nframes_t time, size_t size,
73 jack_midi_data_t *buffer)
75 if (size > max_bytes) {
76 return BUFFER_TOO_SMALL;
78 if (! ((jack_ringbuffer_write_space(info_ring) >= INFO_SIZE) &&
79 (jack_ringbuffer_write_space(byte_ring) >=
80 (size * sizeof(jack_midi_data_t))))) {
81 return BUFFER_FULL;
83 jack_ringbuffer_write(byte_ring, (const char *) buffer,
84 size * sizeof(jack_midi_data_t));
85 jack_ringbuffer_write(info_ring, (const char *) (&time),
86 sizeof(jack_nframes_t));
87 jack_ringbuffer_write(info_ring, (const char *) (&size), sizeof(size_t));
88 return OK;
91 size_t
92 JackMidiAsyncQueue::GetAvailableSpace()
94 return jack_ringbuffer_write_space(info_ring) < INFO_SIZE ? 0 :
95 max_bytes - jack_ringbuffer_read_space(byte_ring);