Use normal Processor run_in_place interface on Meter.
[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, MidiBuffer* buf)
24         : Port (name, flgs)
25         , JackPort (name, DataType::MIDI, flgs)
26         , BaseMidiPort (name, flgs) 
27 {
28         if (buf) {
29
30                 cout << name << " BUFFER" << endl;
31
32                 _buffer = buf;
33                 _own_buffer = false;
34
35         } else {
36
37                 cout << name << " NO BUFFER" << endl;
38
39                 /* data space will be provided by JACK */
40                 _buffer = new MidiBuffer (0);
41                 _own_buffer = true;
42         }
43 }
44
45 void
46 JackMidiPort::cycle_start (nframes_t nframes, nframes_t offset_ignored_but_probably_should_not_be)
47 {
48         _buffer->clear();
49         assert(_buffer->size() == 0);
50
51         if (_flags & IsOutput) {
52                 // no buffer, nothing to do
53                 return;
54         }
55
56         // We're an input - copy Jack events to internal buffer
57         
58         void* jack_buffer = jack_port_get_buffer(_port, nframes);
59         const nframes_t event_count = jack_midi_get_event_count(jack_buffer);
60
61         assert(event_count < _buffer->capacity());
62
63         jack_midi_event_t ev;
64
65         for (nframes_t i=0; i < event_count; ++i) {
66
67                 jack_midi_event_get (&ev, jack_buffer, i);
68
69                 _buffer->push_back (ev);
70         }
71
72         assert(_buffer->size() == event_count);
73
74         if (_buffer->size() > 0)
75                 cerr << "MIDIPort got " << event_count << " events." << endl;
76 }
77
78 void
79 JackMidiPort::cycle_end (nframes_t nframes, nframes_t offset_ignored_but_probably_should_not_be)
80 {
81         if (_flags & IsInput) {
82                 return;
83         }
84
85         // We're an output - copy events from source buffer to Jack buffer
86         
87         void* jack_buffer = jack_port_get_buffer (_port, nframes);
88
89         jack_midi_clear_buffer (jack_buffer);
90
91         for (MidiBuffer::iterator i = _buffer->begin(); i != _buffer->end(); ++i) {
92                 const MidiEvent& ev = *i;
93                 // event times should be frames, relative to cycle start
94                 assert(ev.time() >= 0);
95                 assert(ev.time() < nframes);
96                 jack_midi_event_write (jack_buffer, (jack_nframes_t) ev.time(), ev.buffer(), ev.size());
97         }
98 }