Jack2  1.9.8
JackFFADOMidiInputPort.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 <memory>
21 
22 #include "JackFFADOMidiInputPort.h"
23 #include "JackMidiUtil.h"
24 
26 
27 JackFFADOMidiInputPort::JackFFADOMidiInputPort(size_t max_bytes)
28 {
29  event = 0;
30  receive_queue = new JackFFADOMidiReceiveQueue();
31  std::auto_ptr<JackFFADOMidiReceiveQueue> receive_queue_ptr(receive_queue);
32  write_queue = new JackMidiBufferWriteQueue();
33  std::auto_ptr<JackMidiBufferWriteQueue> write_queue_ptr(write_queue);
34  raw_queue = new JackMidiRawInputWriteQueue(write_queue, max_bytes,
35  max_bytes);
36  write_queue_ptr.release();
37  receive_queue_ptr.release();
38 }
39 
40 JackFFADOMidiInputPort::~JackFFADOMidiInputPort()
41 {
42  delete raw_queue;
43  delete receive_queue;
44  delete write_queue;
45 }
46 
47 void
48 JackFFADOMidiInputPort::Process(JackMidiBuffer *port_buffer,
49  uint32_t *input_buffer, jack_nframes_t frames)
50 {
51  receive_queue->ResetInputBuffer(input_buffer, frames);
52  write_queue->ResetMidiBuffer(port_buffer, frames);
53  jack_nframes_t boundary_frame = GetLastFrame() + frames;
54  if (! event) {
55  event = receive_queue->DequeueEvent();
56  }
57  for (; event; event = receive_queue->DequeueEvent()) {
58  switch (raw_queue->EnqueueEvent(event)) {
59  case JackMidiWriteQueue::BUFFER_FULL:
60 
61  // Processing events early might free up some space in the raw
62  // input queue.
63 
64  raw_queue->Process(boundary_frame);
65  switch (raw_queue->EnqueueEvent(event)) {
66  case JackMidiWriteQueue::BUFFER_TOO_SMALL:
67  // This shouldn't really happen. It indicates a bug if it
68  // does.
69  jack_error("JackFFADOMidiInputPort::Process - **BUG** "
70  "JackMidiRawInputWriteQueue::EnqueueEvent returned "
71  "`BUFFER_FULL`, and then returned "
72  "`BUFFER_TOO_SMALL` after a `Process()` call.");
73  // Fallthrough on purpose
74  case JackMidiWriteQueue::OK:
75  continue;
76  default:
77  return;
78  }
79  case JackMidiWriteQueue::BUFFER_TOO_SMALL:
80  jack_error("JackFFADOMidiInputPort::Process - The write queue "
81  "couldn't enqueue a %d-byte event. Dropping event.",
82  event->size);
83  // Fallthrough on purpose
84  case JackMidiWriteQueue::OK:
85  continue;
86  default:
87  // This is here to stop compliers from warning us about not
88  // handling enumeration values.
89  ;
90  }
91  break;
92  }
93  raw_queue->Process(boundary_frame);
94 }