remove offset from process callback tree. some breakage may have occured. yes, really.
[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 {
32         // FIXME: size kludge (see BufferSet::ensure_buffers)
33         // Jack needs to tell us this
34         _buffer = new MidiBuffer (1024 * 32);
35 }
36
37 MidiPort::~MidiPort()
38 {
39         delete _buffer;
40 }
41
42
43 void
44 MidiPort::cycle_start (nframes_t nframes)
45 {
46         _buffer->clear ();
47         assert (_buffer->size () == 0);
48         
49         if (sends_output ()) {
50                 jack_midi_clear_buffer (jack_port_get_buffer (_jack_port, nframes));
51         }
52 }
53
54 MidiBuffer &
55 MidiPort::get_midi_buffer (nframes_t nframes, nframes_t offset)
56 {
57         if (_has_been_mixed_down) {
58                 return *_buffer;
59         }
60
61         if (receives_input ()) {
62
63                 void* jack_buffer = jack_port_get_buffer (_jack_port, nframes);
64                 const nframes_t event_count = jack_midi_get_event_count(jack_buffer);
65                 
66                 assert (event_count < _buffer->capacity());
67
68                 /* suck all relevant MIDI events from the JACK MIDI port buffer
69                    into our MidiBuffer
70                 */
71                 
72                 nframes_t off = offset + _port_offset;
73
74                 for (nframes_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.time > off && ev.time < off+nframes) {
81                                 _buffer->push_back (ev);
82                         }
83                 }
84                 
85                 if (nframes) {
86                         _has_been_mixed_down = true;
87                 }
88                 
89         } else {
90                 _buffer->silence (nframes);
91         }
92         
93         if (nframes) {
94                 _has_been_mixed_down = true;
95         }
96
97         return *_buffer;
98 }
99
100         
101 void
102 MidiPort::cycle_end (nframes_t nframes)
103 {
104         _has_been_mixed_down = false;
105 }
106
107 void
108 MidiPort::cycle_split ()
109 {
110         _has_been_mixed_down = false;
111 }
112
113 void
114 MidiPort::flush_buffers (nframes_t nframes, nframes_t offset)
115 {
116         if (sends_output ()) {
117                 
118                 void* jack_buffer = jack_port_get_buffer (_jack_port, nframes);
119
120                 for (MidiBuffer::iterator i = _buffer->begin(); i != _buffer->end(); ++i) {
121                         const Evoral::Event<nframes_t>& ev = *i;
122
123                         // event times are in frames, relative to cycle start
124
125                         // XXX split cycle start or cycle start?
126
127                         assert(ev.time() < (nframes+offset+_port_offset));
128
129                         if (ev.time() >= offset + _port_offset) {
130                                 jack_midi_event_write (jack_buffer, (jack_nframes_t) ev.time(), ev.buffer(), ev.size());
131                         }
132                 }
133         }
134 }
135