Jack2  1.9.8
JackALSARawMidiSendQueue.cpp
00001 /*
00002 Copyright (C) 2011 Devin Anderson
00003 
00004 This program is free software; you can redistribute it and/or modify
00005 it under the terms of the GNU General Public License as published by
00006 the Free Software Foundation; either version 2 of the License, or
00007 (at your option) any later version.
00008 
00009 This program is distributed in the hope that it will be useful,
00010 but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 GNU General Public License for more details.
00013 
00014 You should have received a copy of the GNU General Public License
00015 along with this program; if not, write to the Free Software
00016 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00017 
00018 */
00019 
00020 #include <cassert>
00021 
00022 #include "JackALSARawMidiSendQueue.h"
00023 #include "JackMidiUtil.h"
00024 
00025 using Jack::JackALSARawMidiSendQueue;
00026 
00027 JackALSARawMidiSendQueue::JackALSARawMidiSendQueue(snd_rawmidi_t *rawmidi,
00028                                                    size_t bytes_per_poll)
00029 {
00030     assert(bytes_per_poll > 0);
00031     this->bytes_per_poll = bytes_per_poll;
00032     this->rawmidi = rawmidi;
00033     blocked = false;
00034     bytes_available = bytes_per_poll;
00035 }
00036 
00037 Jack::JackMidiWriteQueue::EnqueueResult
00038 JackALSARawMidiSendQueue::EnqueueEvent(jack_nframes_t time, size_t size,
00039                                        jack_midi_data_t *buffer)
00040 {
00041     assert(size == 1);
00042     if (time > GetCurrentFrame()) {
00043         return EVENT_EARLY;
00044     }
00045     if (! bytes_available) {
00046         return BUFFER_FULL;
00047     }
00048     ssize_t result = snd_rawmidi_write(rawmidi, buffer, 1);
00049     switch (result) {
00050     case 1:
00051         blocked = false;
00052         bytes_available--;
00053         return OK;
00054     case -EWOULDBLOCK:
00055         blocked = true;
00056         return BUFFER_FULL;
00057     }
00058     jack_error("JackALSARawMidiSendQueue::EnqueueEvent - snd_rawmidi_write: "
00059                "%s", snd_strerror(result));
00060     return EN_ERROR;
00061 }
00062 
00063 bool
00064 JackALSARawMidiSendQueue::IsBlocked()
00065 {
00066     return blocked;
00067 }
00068 
00069 void
00070 JackALSARawMidiSendQueue::ResetPollByteCount()
00071 {
00072     bytes_available = bytes_per_poll;
00073 }