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.
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
)) +
32 info_ring
= jack_ringbuffer_create((max_messages
* INFO_SIZE
) + 1);
34 jack_ringbuffer_mlock(byte_ring
);
35 jack_ringbuffer_mlock(info_ring
);
36 this->max_bytes
= max_bytes
;
39 jack_ringbuffer_free(byte_ring
);
42 throw std::bad_alloc();
45 JackMidiAsyncQueue::~JackMidiAsyncQueue()
47 jack_ringbuffer_free(byte_ring
);
48 jack_ringbuffer_free(info_ring
);
53 JackMidiAsyncQueue::DequeueEvent()
55 jack_midi_event_t
*event
= 0;
56 if (jack_ringbuffer_read_space(info_ring
) >= INFO_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
,
63 jack_ringbuffer_read(byte_ring
, (char *) data_buffer
,
64 size
* sizeof(jack_midi_data_t
));
65 event
->buffer
= data_buffer
;
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
))))) {
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));
92 JackMidiAsyncQueue::GetAvailableSpace()
94 return jack_ringbuffer_write_space(info_ring
) < INFO_SIZE
? 0 :
95 max_bytes
- jack_ringbuffer_read_space(byte_ring
);