Remove unnecessary 0 checks before delete; see http://www.parashift.com/c++-faq-lite...
[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         , _has_been_mixed_down (false)
28 {
29         // MIDI ports always need a buffer since jack buffer format is different
30         assert(buf);
31
32         _buffer = buf;
33         _own_buffer = false;
34 }
35
36 void
37 JackMidiPort::cycle_start (nframes_t nframes, nframes_t offset)
38 {
39         /* FIXME: offset */
40
41         _buffer->clear();
42         assert(_buffer->size() == 0);
43
44         if (_flags & IsInput) {
45                 return;
46         }
47
48         // We're an output - delete the midi_events.
49         
50         void* jack_buffer = jack_port_get_buffer (_port, nframes);
51
52         jack_midi_clear_buffer (jack_buffer);
53 }
54
55 MidiBuffer &
56 JackMidiPort::get_midi_buffer( nframes_t nframes, nframes_t offset ) {
57
58         if (_has_been_mixed_down)
59             return *_buffer;
60
61         if (_flags & IsOutput) {
62                 return *_buffer;
63         }
64
65         // We're an input - copy Jack events to internal buffer
66         
67         void* jack_buffer = jack_port_get_buffer(_port, nframes);
68         const nframes_t event_count = jack_midi_get_event_count(jack_buffer);
69
70         assert(event_count < _buffer->capacity());
71
72         jack_midi_event_t ev;
73
74         for (nframes_t i=0; i < event_count; ++i) {
75
76                 jack_midi_event_get (&ev, jack_buffer, i);
77
78                 // i guess this should do but i leave it off to test the rest first.
79                 //if (ev.time > offset && ev.time < offset+nframes)
80                         _buffer->push_back (ev);
81         }
82
83         assert(_buffer->size() == event_count);
84
85         /*if (_buffer->size() > 0)
86                 cerr << "JackMIDIPort got " << event_count << " events (buf " << _buffer << ")" << endl;*/
87         if (nframes)
88                 _has_been_mixed_down = true;
89
90         return *_buffer;
91 }
92
93 void
94 JackMidiPort::cycle_end (nframes_t nframes, nframes_t offset)
95 {
96         /* FIXME: offset */
97
98         _has_been_mixed_down = false;
99
100 #if 0
101         if (_flags & IsInput) {
102                 return;
103         }
104
105         // We're an output - copy events from source buffer to Jack buffer
106         
107         void* jack_buffer = jack_port_get_buffer (_port, nframes);
108
109         jack_midi_clear_buffer (jack_buffer);
110
111         for (MidiBuffer::iterator i = _buffer->begin(); i != _buffer->end(); ++i) {
112                 const Evoral::Event& ev = *i;
113
114                 // event times should be frames, relative to cycle start
115                 assert(ev.time() >= 0);
116                 assert(ev.time() < nframes);
117                 jack_midi_event_write (jack_buffer, (jack_nframes_t) ev.time(), ev.buffer(), ev.size());
118         }
119 #endif
120 }
121
122 void
123 JackMidiPort::flush_buffers (nframes_t nframes, nframes_t offset)
124 {
125         /* FIXME: offset */
126
127         if (_flags & IsInput) {
128                 return;
129         }
130
131         void* jack_buffer = jack_port_get_buffer (_port, nframes);
132
133         for (MidiBuffer::iterator i = _buffer->begin(); i != _buffer->end(); ++i) {
134                 const Evoral::Event& ev = *i;
135                 // event times should be frames, relative to cycle start
136                 assert(ev.time() >= 0);
137                 assert(ev.time() < (nframes+offset));
138                 if (ev.time() >= offset)
139                         jack_midi_event_write (jack_buffer, (jack_nframes_t) ev.time(), ev.buffer(), ev.size());
140         }
141 }