Clean/strengthen up constructor/type stuff in new port system.
[ardour.git] / libs / ardour / jack_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 <ardour/jack_midi_port.h>
21
22 using namespace ARDOUR;
23 JackMidiPort::JackMidiPort (const std::string& name, Flags flgs)
24         : Port (DataType::MIDI, flgs)
25         , JackPort (name, DataType::MIDI, flgs)
26         , MidiPort (flgs, 4096) // FIXME FIXME FIXME Jack needs to tell us this
27         , _nframes_this_cycle(0)
28 {
29 }
30
31 void
32 JackMidiPort::cycle_start (nframes_t nframes)
33 {
34         _buffer.clear();
35         assert(_buffer.size() == 0);
36
37         _nframes_this_cycle = nframes;
38
39         if (_flags & JackPortIsOutput) {
40                 _buffer.silence(nframes);
41                 assert(_buffer.size() == 0);
42                 return;
43         }
44
45         // We're an input - copy Jack events to internal buffer
46         
47         void* jack_buffer = jack_port_get_buffer(_port, nframes);
48         const nframes_t event_count
49                 = jack_midi_get_event_count(jack_buffer);
50
51         assert(event_count < _buffer.capacity());
52
53         jack_midi_event_t ev;
54
55         for (nframes_t i=0; i < event_count; ++i) {
56
57                 jack_midi_event_get(&ev, jack_buffer, i);
58
59                 _buffer.push_back(ev);
60         }
61
62         assert(_buffer.size() == event_count);
63
64         //if (_buffer.size() > 0)
65         //      cerr << "MIDIPort got " << event_count << " events." << endl;
66 }
67
68 void
69 JackMidiPort::cycle_end()
70 {
71         if (_flags & JackPortIsInput) {
72                 _nframes_this_cycle = 0;
73                 return;
74         }
75
76         // We're an output - copy events from internal buffer to Jack buffer
77         
78         void* jack_buffer = jack_port_get_buffer(_port, _nframes_this_cycle);
79
80         //const nframes_t event_count = _buffer.size();
81         //if (event_count > 0)
82         //      cerr << "MIDIPort writing " << event_count << " events." << endl;
83
84         jack_midi_clear_buffer(jack_buffer);
85
86         for (MidiBuffer::iterator i = _buffer.begin(); i != _buffer.end(); ++i) {
87                 const MidiEvent& ev = *i;
88                 // event times should be frames, relative to cycle start
89                 assert(ev.time() >= 0);
90                 assert(ev.time() < _nframes_this_cycle);
91                 jack_midi_event_write(jack_buffer, (jack_nframes_t)ev.time(), ev.buffer(), ev.size());
92         }
93         
94         _nframes_this_cycle = 0;
95 }