Jack2 1.9.10

JackMidiAPI.cpp

00001 /*
00002 Copyright (C) 2007 Dmitry Baikov
00003 Original JACK MIDI implementation Copyright (C) 2004 Ian Esten
00004 
00005 This program is free software; you can redistribute it and/or modify
00006 it under the terms of the GNU Lesser General Public License as published by
00007 the Free Software Foundation; either version 2.1 of the License, or
00008 (at your option) any later version.
00009 
00010 This program is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 GNU Lesser General Public License for more details.
00014 
00015 You should have received a copy of the GNU Lesser General Public License
00016 along with this program; if not, write to the Free Software
00017 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00018 
00019 */
00020 
00021 #include "JackError.h"
00022 #include "JackMidiPort.h"
00023 #include <errno.h>
00024 #include <string.h>
00025 
00026 #ifdef __cplusplus
00027 extern "C"
00028 {
00029 #endif
00030 
00031     LIB_EXPORT uint32_t jack_midi_get_event_count(void* port_buffer);
00032 
00033     LIB_EXPORT int jack_midi_event_get(jack_midi_event_t* event,
00034                                    void* port_buffer, uint32_t event_index);
00035 
00036     LIB_EXPORT void jack_midi_clear_buffer(void* port_buffer);
00037     
00038     LIB_EXPORT void jack_midi_reset_buffer(void* port_buffer);
00039 
00040     LIB_EXPORT size_t jack_midi_max_event_size(void* port_buffer);
00041 
00042     LIB_EXPORT jack_midi_data_t* jack_midi_event_reserve(void* port_buffer,
00043             jack_nframes_t time, size_t data_size);
00044 
00045     LIB_EXPORT int jack_midi_event_write(void* port_buffer,
00046                                      jack_nframes_t time, const jack_midi_data_t* data, size_t data_size);
00047 
00048     LIB_EXPORT jack_nframes_t jack_midi_get_lost_event_count(void* port_buffer);
00049 
00050 #ifdef __cplusplus
00051 }
00052 #endif
00053 
00054 using namespace Jack;
00055 
00056 LIB_EXPORT
00057 uint32_t jack_midi_get_event_count(void* port_buffer)
00058 {
00059     JackMidiBuffer *buf = (JackMidiBuffer*)port_buffer;
00060     if (!buf || !buf->IsValid()) {
00061         return 0;
00062     }
00063     return buf->event_count;
00064 }
00065 
00066 LIB_EXPORT
00067 int jack_midi_event_get(jack_midi_event_t *event, void* port_buffer, uint32_t event_index)
00068 {
00069     JackMidiBuffer *buf = (JackMidiBuffer*)port_buffer;
00070     if (!buf || !buf->IsValid()) {
00071         return -EINVAL;
00072     }
00073     if (event_index >= buf->event_count) {
00074         return -ENOBUFS;
00075     }
00076     JackMidiEvent* ev = &buf->events[event_index];
00077     event->time = ev->time;
00078     event->size = ev->size;
00079     event->buffer = ev->GetData(buf);
00080     return 0;
00081 }
00082 
00083 LIB_EXPORT
00084 void jack_midi_clear_buffer(void* port_buffer)
00085 {
00086     JackMidiBuffer *buf = (JackMidiBuffer*)port_buffer;
00087     if (buf && buf->IsValid()) {
00088         buf->Reset(buf->nframes);
00089     }
00090 }
00091 
00092 LIB_EXPORT
00093 void jack_midi_reset_buffer(void* port_buffer)
00094 {
00095     MidiBufferInit(port_buffer, BUFFER_SIZE_MAX, BUFFER_SIZE_MAX);
00096 }
00097 
00098 LIB_EXPORT
00099 size_t jack_midi_max_event_size(void* port_buffer)
00100 {
00101     JackMidiBuffer *buf = (JackMidiBuffer*)port_buffer;
00102     if (buf && buf->IsValid())
00103         return buf->MaxEventSize();
00104     return 0;
00105 }
00106 
00107 LIB_EXPORT
00108 jack_midi_data_t* jack_midi_event_reserve(void* port_buffer, jack_nframes_t time, size_t data_size)
00109 {
00110     JackMidiBuffer *buf = (JackMidiBuffer*)port_buffer;
00111     if (! buf) {
00112         jack_error("jack_midi_event_reserve: port buffer is set to NULL");
00113         return 0;
00114     }
00115     if (! buf->IsValid()) {
00116         jack_error("jack_midi_event_reserve: port buffer is invalid");
00117         return 0;
00118     }
00119     if (time >= buf->nframes) {
00120         jack_error("jack_midi_event_reserve: time parameter is out of range "
00121                    "(%lu >= %lu)", time, buf->nframes);
00122         return 0;
00123     }
00124     if (buf->event_count && (buf->events[buf->event_count - 1].time > time)) {
00125         jack_error("jack_midi_event_reserve: time parameter is earlier than "
00126                    "last reserved event");
00127         return 0;
00128     }
00129     return buf->ReserveEvent(time, data_size);
00130 }
00131 
00132 LIB_EXPORT
00133 int jack_midi_event_write(void* port_buffer,
00134                           jack_nframes_t time, const jack_midi_data_t* data, size_t data_size)
00135 {
00136     JackMidiBuffer *buf = (JackMidiBuffer*)port_buffer;
00137     if (!buf && !buf->IsValid()) {
00138         return -EINVAL;
00139     }
00140     if (time >= buf->nframes || (buf->event_count && buf->events[buf->event_count - 1].time > time)) {
00141         return -EINVAL;
00142     }
00143     jack_midi_data_t* dest = buf->ReserveEvent(time, data_size);
00144     if (!dest) {
00145         return -ENOBUFS;
00146     }
00147     memcpy(dest, data, data_size);
00148     return 0;
00149 }
00150 
00151 LIB_EXPORT
00152 uint32_t jack_midi_get_lost_event_count(void* port_buffer)
00153 {
00154     JackMidiBuffer *buf = (JackMidiBuffer*)port_buffer;
00155     if (buf && buf->IsValid())
00156         return buf->lost_events;
00157     return 0;
00158 }