51d364a3d9d706851e5406954a6ea92ef0680777
[ardour.git] / libs / ardour / midi_port.cc
1 /*
2     Copyright (C) 2006 Paul Davis
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 #include <cassert>
20 #include <iostream>
21
22 #include "ardour/midi_port.h"
23 #include "ardour/data_type.h"
24 #include "ardour/audioengine.h"
25
26 using namespace ARDOUR;
27 using namespace std;
28
29 MidiPort::MidiPort (const std::string& name, Flags flags)
30         : Port (name, DataType::MIDI, flags)
31         , _has_been_mixed_down (false)
32         , _resolve_required (false)
33         , _input_active (true)
34 {
35         _buffer = new MidiBuffer (AudioEngine::instance()->raw_buffer_size (DataType::MIDI));
36 }
37
38 MidiPort::~MidiPort()
39 {
40         delete _buffer;
41 }
42
43 void
44 MidiPort::cycle_start (pframes_t nframes)
45 {
46         Port::cycle_start (nframes);
47
48         _buffer->clear ();
49
50         if (sends_output ()) {
51                 jack_midi_clear_buffer (jack_port_get_buffer (_jack_port, nframes));
52         }
53 }
54
55 MidiBuffer &
56 MidiPort::get_midi_buffer (pframes_t nframes)
57 {
58         if (_has_been_mixed_down) {
59                 return *_buffer;
60         }
61
62         if (receives_input ()) {
63
64                 if (_input_active) {
65
66                         void* jack_buffer = jack_port_get_buffer (_jack_port, nframes);
67                         const pframes_t event_count = jack_midi_get_event_count (jack_buffer);
68                         
69                         /* suck all relevant MIDI events from the JACK MIDI port buffer
70                            into our MidiBuffer
71                         */
72                         
73                         for (pframes_t i = 0; i < event_count; ++i) {
74                                 
75                                 jack_midi_event_t ev;
76                                 
77                                 jack_midi_event_get (&ev, jack_buffer, i);
78                                 
79                                 if (ev.buffer[0] == 0xfe) {
80                                         /* throw away active sensing */
81                                         continue;
82                                 }
83                                 
84                                 /* check that the event is in the acceptable time range */
85                                 
86                                 if ((ev.time >= (_global_port_buffer_offset + _port_buffer_offset)) &&
87                                     (ev.time < (_global_port_buffer_offset + _port_buffer_offset + nframes))) {
88                                         _buffer->push_back (ev);
89                                 } else {
90                                         cerr << "Dropping incoming MIDI at time " << ev.time << "; offset="
91                                              << _global_port_buffer_offset << " limit="
92                                              << (_global_port_buffer_offset + _port_buffer_offset + nframes) << "\n";
93                                 }
94                         } 
95
96                 } else {
97                         _buffer->silence (nframes);
98                 }
99
100         } else {
101                 _buffer->silence (nframes);
102         }
103
104         if (nframes) {
105                 _has_been_mixed_down = true;
106         }
107
108         return *_buffer;
109 }
110
111
112 void
113 MidiPort::cycle_end (pframes_t /*nframes*/)
114 {
115         _has_been_mixed_down = false;
116 }
117
118 void
119 MidiPort::cycle_split ()
120 {
121         _has_been_mixed_down = false;
122 }
123
124 void
125 MidiPort::resolve_notes (void* jack_buffer, MidiBuffer::TimeType when)
126 {
127         for (uint8_t channel = 0; channel <= 0xF; channel++) {
128
129                 uint8_t ev[3] = { ((uint8_t) (MIDI_CMD_CONTROL | channel)), MIDI_CTL_SUSTAIN, 0 };
130
131                 /* we need to send all notes off AND turn the
132                  * sustain/damper pedal off to handle synths
133                  * that prioritize sustain over AllNotesOff
134                  */
135
136                 if (jack_midi_event_write (jack_buffer, when, ev, 3) != 0) {
137                         cerr << "failed to deliver sustain-zero on channel " << channel << " on port " << name() << endl;
138                 }
139
140                 ev[1] = MIDI_CTL_ALL_NOTES_OFF;
141
142                 if (jack_midi_event_write (jack_buffer, 0, ev, 3) != 0) {
143                         cerr << "failed to deliver ALL NOTES OFF on channel " << channel << " on port " << name() << endl;
144                 }
145         }
146 }
147
148 void
149 MidiPort::flush_buffers (pframes_t nframes)
150 {
151         if (sends_output ()) {
152
153                 void* jack_buffer = jack_port_get_buffer (_jack_port, nframes);
154
155                 if (_resolve_required) {
156                         /* resolve all notes at the start of the buffer */
157                         resolve_notes (jack_buffer, 0);
158                         _resolve_required = false;
159                 }
160
161                 for (MidiBuffer::iterator i = _buffer->begin(); i != _buffer->end(); ++i) {
162
163                         const Evoral::MIDIEvent<MidiBuffer::TimeType> ev (*i, false);
164
165                         // event times are in frames, relative to cycle start
166
167                         assert (ev.time() < (nframes + _global_port_buffer_offset + _port_buffer_offset));
168
169                         if (ev.time() >= _global_port_buffer_offset + _port_buffer_offset) {
170                                 if (jack_midi_event_write (jack_buffer, (jack_nframes_t) ev.time(), ev.buffer(), ev.size()) != 0) {
171                                         cerr << "write failed, drop flushed note off on the floor, time "
172                                              << ev.time() << " > " << _global_port_buffer_offset + _port_buffer_offset << endl;
173                                 }
174                         } else {
175                                 cerr << "drop flushed event on the floor, time " << ev
176                                      << " to early for " << _global_port_buffer_offset 
177                                      << " + " << _port_buffer_offset << endl;
178                         }
179                 }
180         }
181 }
182
183 void
184 MidiPort::require_resolve ()
185 {
186         _resolve_required = true;
187 }
188
189 void
190 MidiPort::transport_stopped ()
191 {
192         _resolve_required = true;
193 }
194
195 void
196 MidiPort::realtime_locate ()
197 {
198         _resolve_required = true;
199 }
200
201 void
202 MidiPort::reset ()
203 {
204         Port::reset ();
205         delete _buffer;
206         _buffer = new MidiBuffer (AudioEngine::instance()->raw_buffer_size (DataType::MIDI));
207 }
208
209 void
210 MidiPort::set_input_active (bool yn)
211 {
212         _input_active = yn;
213 }