Jack2  1.9.8
JackALSARawMidiSendQueue.cpp
1 /*
2 Copyright (C) 2011 Devin Anderson
3 
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 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 General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 
18 */
19 
20 #include <cassert>
21 
22 #include "JackALSARawMidiSendQueue.h"
23 #include "JackMidiUtil.h"
24 
26 
27 JackALSARawMidiSendQueue::JackALSARawMidiSendQueue(snd_rawmidi_t *rawmidi,
28  size_t bytes_per_poll)
29 {
30  assert(bytes_per_poll > 0);
31  this->bytes_per_poll = bytes_per_poll;
32  this->rawmidi = rawmidi;
33  blocked = false;
34  bytes_available = bytes_per_poll;
35 }
36 
37 Jack::JackMidiWriteQueue::EnqueueResult
38 JackALSARawMidiSendQueue::EnqueueEvent(jack_nframes_t time, size_t size,
39  jack_midi_data_t *buffer)
40 {
41  assert(size == 1);
42  if (time > GetCurrentFrame()) {
43  return EVENT_EARLY;
44  }
45  if (! bytes_available) {
46  return BUFFER_FULL;
47  }
48  ssize_t result = snd_rawmidi_write(rawmidi, buffer, 1);
49  switch (result) {
50  case 1:
51  blocked = false;
52  bytes_available--;
53  return OK;
54  case -EWOULDBLOCK:
55  blocked = true;
56  return BUFFER_FULL;
57  }
58  jack_error("JackALSARawMidiSendQueue::EnqueueEvent - snd_rawmidi_write: "
59  "%s", snd_strerror(result));
60  return EN_ERROR;
61 }
62 
63 bool
64 JackALSARawMidiSendQueue::IsBlocked()
65 {
66  return blocked;
67 }
68 
69 void
70 JackALSARawMidiSendQueue::ResetPollByteCount()
71 {
72  bytes_available = bytes_per_poll;
73 }