clarify some confusion about how "raw" port buffer sizes are defined
[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_in_process (false)
33 {
34         _buffer = new MidiBuffer (AudioEngine::instance()->raw_buffer_size (DataType::MIDI));
35 }
36
37 MidiPort::~MidiPort()
38 {
39         delete _buffer;
40 }
41
42 void
43 MidiPort::cycle_start (pframes_t nframes)
44 {
45         Port::cycle_start (nframes);
46
47         _buffer->clear ();
48
49         assert (_buffer->size () == 0);
50
51         if (sends_output ()) {
52                 jack_midi_clear_buffer (jack_port_get_buffer (_jack_port, nframes));
53         }
54 }
55
56 MidiBuffer &
57 MidiPort::get_midi_buffer (pframes_t nframes)
58 {
59         if (_has_been_mixed_down) {
60                 return *_buffer;
61         }
62
63         if (receives_input ()) {
64
65                 void* jack_buffer = jack_port_get_buffer (_jack_port, nframes);
66                 const pframes_t event_count = jack_midi_get_event_count (jack_buffer);
67
68                 assert (event_count < _buffer->capacity());
69
70                 /* suck all relevant MIDI events from the JACK MIDI port buffer
71                    into our MidiBuffer
72                 */
73
74                 for (pframes_t i = 0; i < event_count; ++i) {
75
76                         jack_midi_event_t ev;
77
78                         jack_midi_event_get (&ev, jack_buffer, i);
79
80                         if (ev.buffer[0] == 0xfe) {
81                                 /* throw away active sensing */
82                                 continue;
83                         }
84
85                         /* check that the event is in the acceptable time range */
86
87                         if ((ev.time >= (_global_port_buffer_offset + _port_buffer_offset)) && 
88                             (ev.time < (_global_port_buffer_offset + _port_buffer_offset + nframes))) {
89                                 _buffer->push_back (ev);
90                         } else {
91                                 cerr << "Dropping incoming MIDI at time " << ev.time << "; offset=" 
92                                      << _global_port_buffer_offset << " limit=" 
93                                      << (_global_port_buffer_offset + _port_buffer_offset + nframes) << "\n";
94                         }
95                 }
96
97         } else {
98                 _buffer->silence (nframes);
99         }
100
101         if (nframes) {
102                 _has_been_mixed_down = true;
103         }
104
105         return *_buffer;
106 }
107
108
109 void
110 MidiPort::cycle_end (pframes_t /*nframes*/)
111 {
112         _has_been_mixed_down = false;
113 }
114
115 void
116 MidiPort::cycle_split ()
117 {
118         _has_been_mixed_down = false;
119 }
120
121 void
122 MidiPort::flush_buffers (pframes_t nframes, framepos_t time)
123 {
124         if (sends_output ()) {
125
126                 void* jack_buffer = jack_port_get_buffer (_jack_port, nframes);
127
128                 // Feed the data through the MidiStateTracker
129                 bool did_loop;
130
131                 _midi_state_tracker.track (_buffer->begin(), _buffer->end(), did_loop);
132
133                 if (did_loop || _resolve_in_process) {
134                         /* add necessary note offs */
135                         _midi_state_tracker.resolve_notes (*_buffer, time);
136                 }
137
138                 _resolve_in_process = false;
139
140                 for (MidiBuffer::iterator i = _buffer->begin(); i != _buffer->end(); ++i) {
141                         const Evoral::Event<framepos_t>& ev = *i;
142
143                         // event times are in frames, relative to cycle start
144
145                         assert (ev.time() < (nframes + _global_port_buffer_offset + _port_buffer_offset));
146
147                         if (ev.time() >= _global_port_buffer_offset + _port_buffer_offset) {
148                                 if (jack_midi_event_write (jack_buffer, (jack_nframes_t) ev.time(), ev.buffer(), ev.size()) != 0) {
149                                         cerr << "write failed, drop flushed note off on the floor, time " 
150                                              << ev.time() << " > " << _global_port_buffer_offset + _port_buffer_offset << endl;
151                                 }
152                         } else {
153                                 cerr << "drop flushed event on the floor, time " << ev.time() 
154                                      << " < " << _global_port_buffer_offset + _port_buffer_offset << endl;
155                         }
156                 }
157         }
158 }
159
160 void
161 MidiPort::transport_stopped ()
162 {
163         _resolve_in_process = true;
164 }
165
166 void
167 MidiPort::reset ()
168 {
169         Port::reset ();
170         delete _buffer;
171         _buffer = new MidiBuffer (AudioEngine::instance()->raw_buffer_size (DataType::MIDI));
172 }