sorta-kinda working latency compensation, latency reporting and capture alignment...
[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
25 using namespace ARDOUR;
26 using namespace std;
27
28 MidiPort::MidiPort (const std::string& name, Flags flags)
29         : Port (name, DataType::MIDI, flags)
30         , _has_been_mixed_down (false)
31         , _resolve_in_process (false)
32 {
33         _buffer = new MidiBuffer (raw_buffer_size(0));
34 }
35
36 MidiPort::~MidiPort()
37 {
38         delete _buffer;
39 }
40
41 void
42 MidiPort::cycle_start (pframes_t nframes)
43 {
44         Port::cycle_start (nframes);
45
46         _buffer->clear ();
47
48         assert (_buffer->size () == 0);
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                 void* jack_buffer = jack_port_get_buffer (_jack_port, nframes);
65                 const pframes_t event_count = jack_midi_get_event_count (jack_buffer);
66
67                 assert (event_count < _buffer->capacity());
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         if (nframes) {
101                 _has_been_mixed_down = true;
102         }
103
104         return *_buffer;
105 }
106
107
108 void
109 MidiPort::cycle_end (pframes_t /*nframes*/)
110 {
111         _has_been_mixed_down = false;
112 }
113
114 void
115 MidiPort::cycle_split ()
116 {
117         _has_been_mixed_down = false;
118 }
119
120 void
121 MidiPort::flush_buffers (pframes_t nframes, framepos_t time)
122 {
123         if (sends_output ()) {
124
125                 void* jack_buffer = jack_port_get_buffer (_jack_port, nframes);
126
127                 // Feed the data through the MidiStateTracker
128                 bool did_loop;
129
130                 _midi_state_tracker.track (_buffer->begin(), _buffer->end(), did_loop);
131
132                 if (did_loop || _resolve_in_process) {
133                         /* add necessary note offs */
134                         _midi_state_tracker.resolve_notes (*_buffer, time);
135                 }
136
137                 _resolve_in_process = false;
138
139                 for (MidiBuffer::iterator i = _buffer->begin(); i != _buffer->end(); ++i) {
140                         const Evoral::Event<framepos_t>& ev = *i;
141
142                         // event times are in frames, relative to cycle start
143
144                         assert (ev.time() < (nframes + _global_port_buffer_offset + _port_buffer_offset));
145
146                         if (ev.time() >= _global_port_buffer_offset + _port_buffer_offset) {
147                                 if (jack_midi_event_write (jack_buffer, (jack_nframes_t) ev.time(), ev.buffer(), ev.size()) != 0) {
148                                         cerr << "write failed, drop flushed note off on the floor, time " 
149                                              << ev.time() << " > " << _global_port_buffer_offset + _port_buffer_offset << endl;
150                                 }
151                         } else {
152                                 cerr << "drop flushed event on the floor, time " << ev.time() 
153                                      << " < " << _global_port_buffer_offset + _port_buffer_offset << endl;
154                         }
155                 }
156         }
157 }
158
159 void
160 MidiPort::transport_stopped ()
161 {
162         _resolve_in_process = true;
163 }
164
165 size_t
166 MidiPort::raw_buffer_size (pframes_t nframes) const
167 {
168         return jack_midi_max_event_size (jack_port_get_buffer (_jack_port, nframes));
169 }
170