the Properties & 64bit region commit
[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 (nframes_t nframes)
43 {
44         _buffer->clear ();
45         assert (_buffer->size () == 0);
46
47         if (sends_output ()) {
48                 jack_midi_clear_buffer (jack_port_get_buffer (_jack_port, nframes));
49         }
50 }
51
52 MidiBuffer &
53 MidiPort::get_midi_buffer (nframes_t nframes, nframes_t offset)
54 {
55         if (_has_been_mixed_down) {
56                 return *_buffer;
57         }
58
59         if (receives_input ()) {
60
61                 void* jack_buffer = jack_port_get_buffer (_jack_port, nframes);
62                 const nframes_t event_count = jack_midi_get_event_count(jack_buffer);
63
64                 assert (event_count < _buffer->capacity());
65
66                 /* suck all relevant MIDI events from the JACK MIDI port buffer
67                    into our MidiBuffer
68                 */
69
70                 nframes_t off = offset + _port_offset;
71
72                 for (nframes_t i = 0; i < event_count; ++i) {
73
74                         jack_midi_event_t ev;
75
76                         jack_midi_event_get (&ev, jack_buffer, i);
77
78                         if (ev.time > off && ev.time < off+nframes) {
79                                 _buffer->push_back (ev);
80                         }
81                 }
82
83                 if (nframes) {
84                         _has_been_mixed_down = true;
85                 }
86
87         } else {
88                 _buffer->silence (nframes);
89         }
90
91         if (nframes) {
92                 _has_been_mixed_down = true;
93         }
94
95         return *_buffer;
96 }
97
98
99 void
100 MidiPort::cycle_end (nframes_t /*nframes*/)
101 {
102         _has_been_mixed_down = false;
103 }
104
105 void
106 MidiPort::cycle_split ()
107 {
108         _has_been_mixed_down = false;
109 }
110
111 void
112 MidiPort::flush_buffers (nframes_t nframes, nframes64_t time, nframes_t offset)
113 {
114         if (sends_output ()) {
115
116                 void* jack_buffer = jack_port_get_buffer (_jack_port, nframes);
117
118                 // Feed the data through the MidiStateTracker
119                 bool did_loop;
120
121                 _midi_state_tracker.track (_buffer->begin(), _buffer->end(), did_loop);
122
123                 if (did_loop || _resolve_in_process) {
124                         /* add necessary note offs */
125                         _midi_state_tracker.resolve_notes (*_buffer, time);
126                 }
127
128                 _resolve_in_process = false;
129
130                 for (MidiBuffer::iterator i = _buffer->begin(); i != _buffer->end(); ++i) {
131                         const Evoral::Event<nframes_t>& ev = *i;
132
133                         // event times are in frames, relative to cycle start
134
135                         // XXX split cycle start or cycle start?
136
137                         assert(ev.time() < (nframes+offset+_port_offset));
138
139                         if (ev.time() >= offset + _port_offset) {
140                                 jack_midi_event_write (jack_buffer, (jack_nframes_t) ev.time(), ev.buffer(), ev.size());
141                         }
142                 }
143         }
144 }
145
146 void
147 MidiPort::transport_stopped ()
148 {
149         _resolve_in_process = true;
150 }
151
152 size_t
153 MidiPort::raw_buffer_size (nframes_t nframes) const
154 {
155         return jack_midi_max_event_size(jack_port_get_buffer(_jack_port, nframes));
156 }
157