Strip trailing whitespace and fix other whitespace errors (e.g. space/tab mixing...
[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         _buffer = new MidiBuffer (raw_buffer_size(0));
33 }
34
35 MidiPort::~MidiPort()
36 {
37         delete _buffer;
38 }
39
40 void
41 MidiPort::cycle_start (nframes_t nframes)
42 {
43         _buffer->clear ();
44         assert (_buffer->size () == 0);
45
46         if (sends_output ()) {
47                 jack_midi_clear_buffer (jack_port_get_buffer (_jack_port, nframes));
48         }
49 }
50
51 MidiBuffer &
52 MidiPort::get_midi_buffer (nframes_t nframes, nframes_t offset)
53 {
54         if (_has_been_mixed_down) {
55                 return *_buffer;
56         }
57
58         if (receives_input ()) {
59
60                 void* jack_buffer = jack_port_get_buffer (_jack_port, nframes);
61                 const nframes_t event_count = jack_midi_get_event_count(jack_buffer);
62
63                 assert (event_count < _buffer->capacity());
64
65                 /* suck all relevant MIDI events from the JACK MIDI port buffer
66                    into our MidiBuffer
67                 */
68
69                 nframes_t off = offset + _port_offset;
70
71                 for (nframes_t i = 0; i < event_count; ++i) {
72
73                         jack_midi_event_t ev;
74
75                         jack_midi_event_get (&ev, jack_buffer, i);
76
77                         if (ev.time > off && ev.time < off+nframes) {
78                                 _buffer->push_back (ev);
79                         }
80                 }
81
82                 if (nframes) {
83                         _has_been_mixed_down = true;
84                 }
85
86         } else {
87                 _buffer->silence (nframes);
88         }
89
90         if (nframes) {
91                 _has_been_mixed_down = true;
92         }
93
94         return *_buffer;
95 }
96
97
98 void
99 MidiPort::cycle_end (nframes_t /*nframes*/)
100 {
101         _has_been_mixed_down = false;
102 }
103
104 void
105 MidiPort::cycle_split ()
106 {
107         _has_been_mixed_down = false;
108 }
109
110 void
111 MidiPort::flush_buffers (nframes_t nframes, nframes_t offset)
112 {
113         if (sends_output ()) {
114
115                 void* jack_buffer = jack_port_get_buffer (_jack_port, nframes);
116
117                 for (MidiBuffer::iterator i = _buffer->begin(); i != _buffer->end(); ++i) {
118                         const Evoral::Event<nframes_t>& ev = *i;
119
120                         // event times are in frames, relative to cycle start
121
122                         // XXX split cycle start or cycle start?
123
124                         assert(ev.time() < (nframes+offset+_port_offset));
125
126                         if (ev.time() >= offset + _port_offset) {
127                                 jack_midi_event_write (jack_buffer, (jack_nframes_t) ev.time(), ev.buffer(), ev.size());
128                         }
129                 }
130         }
131 }
132
133 size_t
134 MidiPort::raw_buffer_size (nframes_t nframes) const
135 {
136         return jack_midi_max_event_size(jack_port_get_buffer(_jack_port, nframes));
137 }
138