summaryrefslogtreecommitdiff
path: root/linux/firewire/JackFFADOMidiInputPort.cpp
blob: 40a1ee14e2956dafb27211048bbfed5d45008f28 (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
/*
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 "JackFFADOMidiInputPort.h"
#include "JackMidiUtil.h"
#include "JackError.h"

using Jack::JackFFADOMidiInputPort;

JackFFADOMidiInputPort::JackFFADOMidiInputPort(size_t max_bytes)
{
    event = 0;
    receive_queue = new JackFFADOMidiReceiveQueue();
    std::unique_ptr<JackFFADOMidiReceiveQueue> receive_queue_ptr(receive_queue);
    write_queue = new JackMidiBufferWriteQueue();
    std::unique_ptr<JackMidiBufferWriteQueue> write_queue_ptr(write_queue);
    raw_queue = new JackMidiRawInputWriteQueue(write_queue, max_bytes,
                                               max_bytes);
    write_queue_ptr.release();
    receive_queue_ptr.release();
}

JackFFADOMidiInputPort::~JackFFADOMidiInputPort()
{
    delete raw_queue;
    delete receive_queue;
    delete write_queue;
}

void
JackFFADOMidiInputPort::Process(JackMidiBuffer *port_buffer,
                                uint32_t *input_buffer, jack_nframes_t frames)
{
    receive_queue->ResetInputBuffer(input_buffer, frames);
    write_queue->ResetMidiBuffer(port_buffer, frames);
    jack_nframes_t boundary_frame = GetLastFrame() + frames;
    if (! event) {
        event = receive_queue->DequeueEvent();
    }
    for (; event; event = receive_queue->DequeueEvent()) {
        switch (raw_queue->EnqueueEvent(event)) {
        case JackMidiWriteQueue::BUFFER_FULL:

            // Processing events early might free up some space in the raw
            // input queue.

            raw_queue->Process(boundary_frame);
            switch (raw_queue->EnqueueEvent(event)) {
            case JackMidiWriteQueue::BUFFER_TOO_SMALL:
                // This shouldn't really happen.  It indicates a bug if it
                // does.
                jack_error("JackFFADOMidiInputPort::Process - **BUG** "
                           "JackMidiRawInputWriteQueue::EnqueueEvent returned "
                           "`BUFFER_FULL`, and then returned "
                           "`BUFFER_TOO_SMALL` after a `Process()` call.");
                // Fallthrough on purpose
            case JackMidiWriteQueue::OK:
                continue;
            default:
                return;
            }
        case JackMidiWriteQueue::BUFFER_TOO_SMALL:
            jack_error("JackFFADOMidiInputPort::Process - The write queue "
                       "couldn't enqueue a %d-byte event.  Dropping event.",
                       event->size);
            // Fallthrough on purpose
        case JackMidiWriteQueue::OK:
            continue;
        default:
            // This is here to stop compliers from warning us about not
            // handling enumeration values.
            ;
        }
        break;
    }
    raw_queue->Process(boundary_frame);
}