Jack2: JackMidiAsyncQueue.cpp Source File

Jack2 1.9.12

JackMidiAsyncQueue.cpp
1 /*
2 Copyright (C) 2010 Devin Anderson
3
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.
8
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.
13
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.
17
18 */
19
20 #include <new>
21
22 #include "JackMidiAsyncQueue.h"
23
24 using Jack::JackMidiAsyncQueue;
25
26 JackMidiAsyncQueue::JackMidiAsyncQueue(size_t max_bytes, size_t max_messages)
27 {
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;
38 }
39 jack_ringbuffer_free(byte_ring);
40 }
41 delete data_buffer;
42 throw std::bad_alloc();
43 }
44
45 JackMidiAsyncQueue::~JackMidiAsyncQueue()
46 {
47 jack_ringbuffer_free(byte_ring);
48 jack_ringbuffer_free(info_ring);
49 delete[] data_buffer;
50 }
51
52 jack_midi_event_t *
53 JackMidiAsyncQueue::DequeueEvent()
54 {
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;
67 }
68 return event;
69 }
70
71 Jack::JackMidiWriteQueue::EnqueueResult
72 JackMidiAsyncQueue::EnqueueEvent(jack_nframes_t time, size_t size,
73 jack_midi_data_t *buffer)
74 {
75 if (size > max_bytes) {
76 return BUFFER_TOO_SMALL;
77 }
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;
82 }
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;
89 }
90
91 size_t
92 JackMidiAsyncQueue::GetAvailableSpace()
93 {
94 return jack_ringbuffer_write_space(info_ring) < INFO_SIZE ? 0 :
95 max_bytes - jack_ringbuffer_read_space(byte_ring);
96 }
jack_ringbuffer_read
size_t jack_ringbuffer_read(jack_ringbuffer_t *rb, char *dest, size_t cnt)
Jack::JackMidiAsyncQueue::EnqueueEvent
virtual EnqueueResult EnqueueEvent(jack_nframes_t time, size_t size, jack_midi_data_t *buffer)
Definition: JackMidiAsyncQueue.cpp:72
jack_midi_event_t
Definition: JackMidiPort.h:33
jack_ringbuffer_mlock
int jack_ringbuffer_mlock(jack_ringbuffer_t *rb)
Jack::JackMidiAsyncQueue::GetAvailableSpace
size_t GetAvailableSpace()
Definition: JackMidiAsyncQueue.cpp:92
Jack::JackMidiAsyncQueue
Definition: JackMidiAsyncQueue.h:40
jack_ringbuffer_write
size_t jack_ringbuffer_write(jack_ringbuffer_t *rb, const char *src, size_t cnt)
jack_ringbuffer_write_space
size_t jack_ringbuffer_write_space(const jack_ringbuffer_t *rb)
jack_ringbuffer_read_space
size_t jack_ringbuffer_read_space(const jack_ringbuffer_t *rb)
jack_ringbuffer_create
jack_ringbuffer_t * jack_ringbuffer_create(size_t sz)
jack_ringbuffer_free
void jack_ringbuffer_free(jack_ringbuffer_t *rb)
Jack::JackMidiAsyncQueue::DequeueEvent
virtual jack_midi_event_t * DequeueEvent()
Definition: JackMidiAsyncQueue.cpp:53