Jack2 1.9.6

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 "JackCompilerDeps.h"
00024 #include <errno.h>
00025 #include <string.h>
00026 #include "JackSystemDeps.h"
00027 
00028 #ifdef __cplusplus
00029 extern "C"
00030 {
00031 #endif
00032 
00033     EXPORT jack_nframes_t jack_midi_get_event_count(void* port_buffer);
00034 
00035     EXPORT int jack_midi_event_get(jack_midi_event_t* event,
00036                                    void* port_buffer, jack_nframes_t event_index);
00037 
00038     EXPORT void jack_midi_clear_buffer(void* port_buffer);
00039 
00040     EXPORT size_t jack_midi_max_event_size(void* port_buffer);
00041 
00042     EXPORT jack_midi_data_t* jack_midi_event_reserve(void* port_buffer,
00043             jack_nframes_t time, size_t data_size);
00044 
00045     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     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 EXPORT
00057 jack_nframes_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     return buf->event_count;
00063 }
00064 
00065 EXPORT
00066 int jack_midi_event_get(jack_midi_event_t *event, void* port_buffer, jack_nframes_t event_index)
00067 {
00068     JackMidiBuffer *buf = (JackMidiBuffer*)port_buffer;
00069     if (!buf || !buf->IsValid())
00070         return -EINVAL;
00071     if (event_index >= buf->event_count)
00072         return -ENOBUFS;
00073     JackMidiEvent* ev = &buf->events[event_index];
00074     event->time = ev->time;
00075     event->size = ev->size;
00076     event->buffer = ev->GetData(buf);
00077     return 0;
00078 }
00079 
00080 EXPORT
00081 void jack_midi_clear_buffer(void* port_buffer)
00082 {
00083     JackMidiBuffer *buf = (JackMidiBuffer*)port_buffer;
00084     if (buf && buf->IsValid())
00085         buf->Reset(buf->nframes);
00086 }
00087 
00088 EXPORT
00089 size_t jack_midi_max_event_size(void* port_buffer)
00090 {
00091     JackMidiBuffer *buf = (JackMidiBuffer*)port_buffer;
00092     if (buf && buf->IsValid())
00093         return buf->MaxEventSize();
00094     return 0;
00095 }
00096 
00097 EXPORT
00098 jack_midi_data_t* jack_midi_event_reserve(void* port_buffer, jack_nframes_t time, size_t data_size)
00099 {
00100     JackMidiBuffer *buf = (JackMidiBuffer*)port_buffer;
00101     if (!buf && !buf->IsValid())
00102         return 0;
00103     if (time >= buf->nframes || (buf->event_count && buf->events[buf->event_count - 1].time > time))
00104         return 0;
00105     return buf->ReserveEvent(time, data_size);
00106 }
00107 
00108 EXPORT
00109 int jack_midi_event_write(void* port_buffer,
00110                           jack_nframes_t time, const jack_midi_data_t* data, size_t data_size)
00111 {
00112     JackMidiBuffer *buf = (JackMidiBuffer*)port_buffer;
00113     if (!buf && !buf->IsValid())
00114         return -EINVAL;
00115     if (time >= buf->nframes || (buf->event_count && buf->events[buf->event_count - 1].time > time))
00116         return -EINVAL;
00117     jack_midi_data_t* dest = buf->ReserveEvent(time, data_size);
00118     if (!dest)
00119         return -ENOBUFS;
00120     memcpy(dest, data, data_size);
00121     return 0;
00122 }
00123 
00124 EXPORT
00125 jack_nframes_t jack_midi_get_lost_event_count(void* port_buffer)
00126 {
00127     JackMidiBuffer *buf = (JackMidiBuffer*)port_buffer;
00128     if (buf && buf->IsValid())
00129         return buf->lost_events;
00130     return 0;
00131 }