summaryrefslogtreecommitdiff
path: root/common/JackMidiRawOutputWriteQueue.cpp
blob: b81ea0c0b682220fd5549d4643bee1a4066ab37e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
Copyright (C) 2010 Devin Anderson

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

*/

#include <memory>
#include <new>

#include "JackError.h"
#include "JackMidiRawOutputWriteQueue.h"
#include "JackMidiUtil.h"

using Jack::JackMidiRawOutputWriteQueue;

#define STILL_TIME(c, b) ((! (b)) || ((c) < (b)))

JackMidiRawOutputWriteQueue::
JackMidiRawOutputWriteQueue(JackMidiSendQueue *send_queue, size_t non_rt_size,
                            size_t max_non_rt_messages, size_t max_rt_messages)
{
    non_rt_queue = new JackMidiAsyncQueue(non_rt_size, max_non_rt_messages);
    std::unique_ptr<JackMidiAsyncQueue> non_rt_ptr(non_rt_queue);
    rt_queue = new JackMidiAsyncQueue(max_rt_messages, max_rt_messages);
    std::unique_ptr<JackMidiAsyncQueue> rt_ptr(rt_queue);
    non_rt_event = 0;
    rt_event = 0;
    running_status = 0;
    this->send_queue = send_queue;
    rt_ptr.release();
    non_rt_ptr.release();
}

JackMidiRawOutputWriteQueue::~JackMidiRawOutputWriteQueue()
{
    delete non_rt_queue;
    delete rt_queue;
}

void
JackMidiRawOutputWriteQueue::DequeueNonRealtimeEvent()
{
    non_rt_event = non_rt_queue->DequeueEvent();
    if (non_rt_event) {
        non_rt_event_time = non_rt_event->time;
        running_status = ApplyRunningStatus(non_rt_event, running_status);
    }
}

void
JackMidiRawOutputWriteQueue::DequeueRealtimeEvent()
{
    rt_event = rt_queue->DequeueEvent();
    if (rt_event) {
        rt_event_time = rt_event->time;
    }
}

Jack::JackMidiWriteQueue::EnqueueResult
JackMidiRawOutputWriteQueue::EnqueueEvent(jack_nframes_t time, size_t size,
                                          jack_midi_data_t *buffer)
{
    JackMidiAsyncQueue *queue = (size == 1) && (*buffer >= 0xf8) ? rt_queue :
        non_rt_queue;
    return queue->EnqueueEvent(time, size, buffer);
}

void
JackMidiRawOutputWriteQueue::HandleWriteQueueBug(jack_nframes_t time,
                                                 jack_midi_data_t byte)
{
    jack_error("JackMidiRawOutputWriteQueue::HandleWriteQueueBug - **BUG** "
               "The write queue told us that it couldn't enqueue a 1-byte "
               "MIDI event scheduled for frame '%d'.  This is probably a bug "
               "in the write queue implementation.", time);
}

jack_nframes_t
JackMidiRawOutputWriteQueue::Process(jack_nframes_t boundary_frame)
{
    if (! non_rt_event) {
        DequeueNonRealtimeEvent();
    }
    if (! rt_event) {
        DequeueRealtimeEvent();
    }
    while (rt_event) {
        jack_nframes_t current_frame = send_queue->GetNextScheduleFrame();
        if ((rt_event_time > current_frame) && non_rt_event &&
            (non_rt_event_time < rt_event_time)) {
            if (! SendNonRTBytes(rt_event_time < boundary_frame ?
                                 rt_event_time : boundary_frame)) {
                return non_rt_event_time;
            }
            current_frame = send_queue->GetNextScheduleFrame();
        }
        if (! STILL_TIME(current_frame, boundary_frame)) {
            return (! non_rt_event) ? rt_event_time :
                non_rt_event_time < rt_event_time ? non_rt_event_time :
                rt_event_time;
        }
        if (! SendByte(rt_event_time, *(rt_event->buffer))) {
            return rt_event_time;
        }
        DequeueRealtimeEvent();
    }
    SendNonRTBytes(boundary_frame);
    return non_rt_event ? non_rt_event_time : 0;
}

bool
JackMidiRawOutputWriteQueue::SendByte(jack_nframes_t time,
                                      jack_midi_data_t byte)
{
    switch (send_queue->EnqueueEvent(time, 1, &byte)) {
    case BUFFER_TOO_SMALL:
        HandleWriteQueueBug(time, byte);
    case OK:
        return true;
    default:
        // This is here to stop compilers from warning us about not handling
        // enumeration values.
        ;
    }
    return false;
}

bool
JackMidiRawOutputWriteQueue::SendNonRTBytes(jack_nframes_t boundary_frame)
{
    while (non_rt_event) {
        for (; non_rt_event->size;
             (non_rt_event->size)--, (non_rt_event->buffer)++) {
            jack_nframes_t current_frame = send_queue->GetNextScheduleFrame();
            if (! STILL_TIME(current_frame, boundary_frame)) {
                return true;
            }
            if (! SendByte(non_rt_event_time, *(non_rt_event->buffer))) {
                return false;
            }
        }
        DequeueNonRealtimeEvent();
    }
    return true;
}