Jack2  1.9.9
JackMidiPort.cpp
1 /*
2 Copyright (C) 2007 Dmitry Baikov
3 Original JACK MIDI implementation Copyright (C) 2004 Ian Esten
4 
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9 
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
14 
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 
19 */
20 
21 #include "JackError.h"
22 #include "JackPortType.h"
23 #include "JackMidiPort.h"
24 #include <assert.h>
25 #include <string.h>
26 
27 namespace Jack
28 {
29 
30 SERVER_EXPORT void JackMidiBuffer::Reset(jack_nframes_t nframes)
31 {
32  /* This line ate 1 hour of my life... dsbaikov */
33  this->nframes = nframes;
34  write_pos = 0;
35  event_count = 0;
36  lost_events = 0;
37  mix_index = 0;
38 }
39 
40 SERVER_EXPORT jack_shmsize_t JackMidiBuffer::MaxEventSize() const
41 {
42  assert (((jack_shmsize_t) - 1) < 0); // jack_shmsize_t should be signed
43  jack_shmsize_t left = buffer_size - (sizeof(JackMidiBuffer) + sizeof(JackMidiEvent) * (event_count + 1) + write_pos);
44  if (left < 0)
45  return 0;
46  if (left <= JackMidiEvent::INLINE_SIZE_MAX)
47  return JackMidiEvent::INLINE_SIZE_MAX;
48  return left;
49 }
50 
51 SERVER_EXPORT jack_midi_data_t* JackMidiBuffer::ReserveEvent(jack_nframes_t time, jack_shmsize_t size)
52 {
53  jack_shmsize_t space = MaxEventSize();
54  if (space == 0 || size > space) {
55  jack_error("JackMidiBuffer::ReserveEvent - the buffer does not have "
56  "enough room to enqueue a %lu byte event", size);
57  lost_events++;
58  return 0;
59  }
60  JackMidiEvent* event = &events[event_count++];
61  event->time = time;
62  event->size = size;
63  if (size <= JackMidiEvent::INLINE_SIZE_MAX)
64  return event->data;
65 
66  write_pos += size;
67  event->offset = buffer_size - write_pos;
68  return (jack_midi_data_t*)this + event->offset;
69 }
70 
71 static void MidiBufferInit(void* buffer, size_t buffer_size, jack_nframes_t nframes)
72 {
73  JackMidiBuffer* midi = (JackMidiBuffer*)buffer;
74  midi->magic = JackMidiBuffer::MAGIC;
75  /* Since port buffer has actually always BUFFER_SIZE_MAX frames, we can safely use all the size */
76  midi->buffer_size = BUFFER_SIZE_MAX * sizeof(jack_default_audio_sample_t);
77  midi->Reset(nframes);
78 }
79 
80 /*
81  * The mixdown function below, is a simplest (read slowest) implementation possible.
82  * But, since it is unlikely that it will mix many buffers with many events,
83  * it should perform quite good.
84  * More efficient (and possibly, fastest possible) implementation (it exists),
85  * using calendar queue algorithm is about 3 times bigger, and uses alloca().
86  * So, let's listen to D.Knuth about premature optimisation, a leave the current
87  * implementation as is, until it is proved to be a bottleneck.
88  * Dmitry Baikov.
89  */
90 static void MidiBufferMixdown(void* mixbuffer, void** src_buffers, int src_count, jack_nframes_t nframes)
91 {
92  JackMidiBuffer* mix = static_cast<JackMidiBuffer*>(mixbuffer);
93  if (!mix->IsValid()) {
94  jack_error("Jack::MidiBufferMixdown - invalid mix buffer");
95  return;
96  }
97  mix->Reset(nframes);
98 
99  int event_count = 0;
100  for (int i = 0; i < src_count; ++i) {
101  JackMidiBuffer* buf = static_cast<JackMidiBuffer*>(src_buffers[i]);
102  if (!buf->IsValid()) {
103  jack_error("Jack::MidiBufferMixdown - invalid source buffer");
104  return;
105  }
106  buf->mix_index = 0;
107  event_count += buf->event_count;
108  mix->lost_events += buf->lost_events;
109  }
110 
111  int events_done;
112  for (events_done = 0; events_done < event_count; ++events_done) {
113  JackMidiBuffer* next_buf = 0;
114  JackMidiEvent* next_event = 0;
115 
116  // find the earliest event
117  for (int i = 0; i < src_count; ++i) {
118  JackMidiBuffer* buf = static_cast<JackMidiBuffer*>(src_buffers[i]);
119  if (buf->mix_index >= buf->event_count)
120  continue;
121  JackMidiEvent* e = &buf->events[buf->mix_index];
122  if (!next_event || e->time < next_event->time) {
123  next_event = e;
124  next_buf = buf;
125  }
126  }
127  assert(next_event != 0);
128 
129  // write the event
130  jack_midi_data_t* dest = mix->ReserveEvent(next_event->time, next_event->size);
131  if (!dest)
132  break;
133  memcpy(dest, next_event->GetData(next_buf), next_event->size);
134  next_buf->mix_index++;
135  }
136  mix->lost_events += event_count - events_done;
137 }
138 
139 static size_t MidiBufferSize()
140 {
141  return BUFFER_SIZE_MAX * sizeof(jack_default_audio_sample_t);
142 }
143 
144 const JackPortType gMidiPortType =
145 {
146  JACK_DEFAULT_MIDI_TYPE,
147  MidiBufferSize,
148  MidiBufferInit,
149  MidiBufferMixdown
150 };
151 
152 } // namespace Jack
SERVER_EXPORT void jack_error(const char *fmt,...)
Definition: JackError.cpp:91
jack_shmsize_t write_pos
data write position from the end of the buffer.
Definition: JackMidiPort.h:81