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