Remove internal ports.
[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, nframes_t offset)
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                 jack_midi_event_t ev;
69                 
70                 for (nframes_t i = 0; i < event_count; ++i) {
71                         
72                         jack_midi_event_get (&ev, jack_buffer, i);
73                         
74                         // i guess this should do but i leave it off to test the rest first.
75                         //if (ev.time > offset && ev.time < offset+nframes)
76                         _buffer->push_back (ev);
77                 }
78                 
79                 if (nframes) {
80                         _has_been_mixed_down = true;
81                 }
82                 
83         } else {
84                 _buffer->silence (nframes, offset);
85         }
86         
87         if (nframes) {
88                 _has_been_mixed_down = true;
89         }
90
91         return *_buffer;
92 }
93
94         
95 void
96 MidiPort::cycle_end (nframes_t nframes, nframes_t offset)
97 {
98 #if 0
99
100         if (sends_output ()) {
101                 /* FIXME: offset */
102
103                 // We're an output - copy events from source buffer to Jack buffer
104                 
105                 void* jack_buffer = jack_port_get_buffer (_jack_port, nframes);
106                 
107                 jack_midi_clear_buffer (jack_buffer);
108                 
109                 for (MidiBuffer::iterator i = _buffer->begin(); i != _buffer->end(); ++i) {
110                         const Evoral::Event& ev = *i;
111
112                         // event times should be frames, relative to cycle start
113                         assert(ev.time() >= 0);
114                         assert(ev.time() < nframes);
115                         jack_midi_event_write (jack_buffer, (jack_nframes_t) ev.time(), ev.buffer(), ev.size());
116                 }
117         }
118 #endif
119
120         _has_been_mixed_down = false;
121 }
122
123 void
124 MidiPort::flush_buffers (nframes_t nframes, nframes_t offset)
125 {
126         /* FIXME: offset */
127         
128         if (sends_output ()) {
129                 
130                 void* jack_buffer = jack_port_get_buffer (_jack_port, nframes);
131
132                 for (MidiBuffer::iterator i = _buffer->begin(); i != _buffer->end(); ++i) {
133                         const Evoral::Event<double>& ev = *i;
134                         // event times should be frames, relative to cycle start
135                         assert(ev.time() >= 0);
136                         assert(ev.time() < (nframes+offset));
137                         if (ev.time() >= offset) {
138                                 jack_midi_event_write (jack_buffer, (jack_nframes_t) ev.time(), ev.buffer(), ev.size());
139                         }
140                 }
141         }
142 }
143