Merge windows+cc branch into cairocanvas branch. Not finished, need to now merge...
[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 #define port_engine AudioEngine::instance()->port_engine()
30
31 MidiPort::MidiPort (const std::string& name, PortFlags flags)
32         : Port (name, DataType::MIDI, flags)
33         , _has_been_mixed_down (false)
34         , _resolve_required (false)
35         , _input_active (true)
36         , _always_parse (false)
37 {
38         _buffer = new MidiBuffer (AudioEngine::instance()->raw_buffer_size (DataType::MIDI));
39 }
40
41 MidiPort::~MidiPort()
42 {
43         delete _buffer;
44 }
45
46 void
47 MidiPort::cycle_start (pframes_t nframes)
48 {
49         framepos_t now = AudioEngine::instance()->sample_time_at_cycle_start();
50
51         Port::cycle_start (nframes);
52
53         _buffer->clear ();
54
55         if (sends_output ()) {
56                 port_engine.midi_clear (port_engine.get_buffer (_port_handle, nframes));
57         }
58
59         if (_always_parse) {
60                 MidiBuffer& mb (get_midi_buffer (nframes));
61
62                 /* dump incoming MIDI to parser */
63                 
64                 for (MidiBuffer::iterator b = mb.begin(); b != mb.end(); ++b) {
65                         uint8_t* buf = (*b).buffer();
66                         
67                         _self_parser.set_timestamp (now + (*b).time());
68                         
69                         uint32_t limit = (*b).size();
70                         
71                         for (size_t n = 0; n < limit; ++n) {
72                                 _self_parser.scanner (buf[n]);
73                         }
74                 }
75         }
76 }
77
78 MidiBuffer &
79 MidiPort::get_midi_buffer (pframes_t nframes)
80 {
81         if (_has_been_mixed_down) {
82                 return *_buffer;
83         }
84
85         if (receives_input ()) {
86
87                 if (_input_active) {
88
89                         void* buffer = port_engine.get_buffer (_port_handle, nframes);
90                         const pframes_t event_count = port_engine.get_midi_event_count (buffer);
91
92                         /* suck all relevant MIDI events from the MIDI port buffer
93                            into our MidiBuffer
94                         */
95
96                         for (pframes_t i = 0; i < event_count; ++i) {
97                                 
98                                 pframes_t timestamp;
99                                 size_t size;
100                                 uint8_t* buf;
101                                 
102                                 port_engine.midi_event_get (timestamp, size, &buf, buffer, i);
103                                 
104                                 if (buf[0] == 0xfe) {
105                                         /* throw away active sensing */
106                                         continue;
107                                 }
108                                 
109                                 /* check that the event is in the acceptable time range */
110                                 
111                                 if ((timestamp >= (_global_port_buffer_offset + _port_buffer_offset)) &&
112                                     (timestamp < (_global_port_buffer_offset + _port_buffer_offset + nframes))) {
113                                         _buffer->push_back (timestamp, size, buf);
114                                 } else {
115                                         cerr << "Dropping incoming MIDI at time " << timestamp << "; offset="
116                                              << _global_port_buffer_offset << " limit="
117                                              << (_global_port_buffer_offset + _port_buffer_offset + nframes) << "\n";
118                                 }
119                         } 
120
121                 } else {
122                         _buffer->silence (nframes);
123                 }
124
125         } else {
126                 _buffer->silence (nframes);
127         }
128
129         if (nframes) {
130                 _has_been_mixed_down = true;
131         }
132
133         return *_buffer;
134 }
135
136 void
137 MidiPort::cycle_end (pframes_t /*nframes*/)
138 {
139         _has_been_mixed_down = false;
140 }
141
142 void
143 MidiPort::cycle_split ()
144 {
145         _has_been_mixed_down = false;
146 }
147
148 void
149 MidiPort::resolve_notes (void* port_buffer, MidiBuffer::TimeType when)
150 {
151         for (uint8_t channel = 0; channel <= 0xF; channel++) {
152
153                 uint8_t ev[3] = { ((uint8_t) (MIDI_CMD_CONTROL | channel)), MIDI_CTL_SUSTAIN, 0 };
154
155                 /* we need to send all notes off AND turn the
156                  * sustain/damper pedal off to handle synths
157                  * that prioritize sustain over AllNotesOff
158                  */
159
160                 if (port_engine.midi_event_put (port_buffer, when, ev, 3) != 0) {
161                         cerr << "failed to deliver sustain-zero on channel " << (int)channel << " on port " << name() << endl;
162                 }
163
164                 ev[1] = MIDI_CTL_ALL_NOTES_OFF;
165
166                 if (port_engine.midi_event_put (port_buffer, 0, ev, 3) != 0) {
167                         cerr << "failed to deliver ALL NOTES OFF on channel " << (int)channel << " on port " << name() << endl;
168                 }
169         }
170 }
171
172 void
173 MidiPort::flush_buffers (pframes_t nframes)
174 {
175         if (sends_output ()) {
176
177                 void* port_buffer = 0;
178                 
179                 if (_resolve_required) {
180                         port_buffer = port_engine.get_buffer (_port_handle, nframes);
181                         /* resolve all notes at the start of the buffer */
182                         resolve_notes (port_buffer, 0);
183                         _resolve_required = false;
184                 } 
185                 
186                 if (_buffer->empty()) {
187                         return;
188                 }
189
190                 if (!port_buffer) {
191                         port_buffer = port_engine.get_buffer (_port_handle, nframes);
192                 }
193
194                 for (MidiBuffer::iterator i = _buffer->begin(); i != _buffer->end(); ++i) {
195
196                         const Evoral::MIDIEvent<MidiBuffer::TimeType> ev (*i, false);
197
198                         // event times are in frames, relative to cycle start
199
200                         assert (ev.time() < (nframes + _global_port_buffer_offset + _port_buffer_offset));
201
202                         if (ev.time() >= _global_port_buffer_offset + _port_buffer_offset) {
203                                 if (port_engine.midi_event_put (port_buffer, (pframes_t) ev.time(), ev.buffer(), ev.size()) != 0) {
204                                         cerr << "write failed, drop flushed note off on the floor, time "
205                                              << ev.time() << " > " << _global_port_buffer_offset + _port_buffer_offset << endl;
206                                 }
207                         } else {
208                                 cerr << "drop flushed event on the floor, time " << ev.time()
209                                      << " to early for " << _global_port_buffer_offset 
210                                      << " + " << _port_buffer_offset << endl;
211                         }
212                 }
213
214                 /* done.. the data has moved to the port buffer, mark it so 
215                  */
216
217                 _buffer->clear ();
218         }
219 }
220
221 void
222 MidiPort::require_resolve ()
223 {
224         _resolve_required = true;
225 }
226
227 void
228 MidiPort::transport_stopped ()
229 {
230         _resolve_required = true;
231 }
232
233 void
234 MidiPort::realtime_locate ()
235 {
236         _resolve_required = true;
237 }
238
239 void
240 MidiPort::reset ()
241 {
242         Port::reset ();
243         delete _buffer;
244         cerr << name() << " new MIDI buffer of size " << AudioEngine::instance()->raw_buffer_size (DataType::MIDI) << endl;
245         _buffer = new MidiBuffer (AudioEngine::instance()->raw_buffer_size (DataType::MIDI));
246 }
247
248 void
249 MidiPort::set_input_active (bool yn)
250 {
251         _input_active = yn;
252 }
253
254 void
255 MidiPort::set_always_parse (bool yn)
256 {
257         _always_parse = yn;
258 }