Remove unnecessary 0 checks before delete; see http://www.parashift.com/c++-faq-lite...
[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/jack_midi_port.h>
24 #include <ardour/data_type.h>
25
26 using namespace ARDOUR;
27 using namespace std;
28
29 MidiPort::MidiPort (const std::string& name, Flags flags, bool external, nframes_t capacity)
30         : Port (name, flags)
31         , BaseMidiPort (name, flags)
32         , PortFacade (name, flags)
33         , _has_been_mixed_down (false)
34 {
35         // FIXME: size kludge (see BufferSet::ensure_buffers)
36         // Jack needs to tell us this
37         _buffer = new MidiBuffer (capacity * 8);
38
39         if (external) {
40                 /* external ports use the same buffer for the jack port (_ext_port)
41                  * and internal ports (this) */
42                 _ext_port = new JackMidiPort (name, flags, _buffer);
43                 Port::set_name (_ext_port->name());
44         } else {
45                 /* internal ports just have a single buffer, no jack port */
46                 _ext_port = 0;
47                 set_name (name);
48         }
49
50         reset ();
51 }
52
53 MidiPort::~MidiPort()
54 {
55         delete _ext_port;
56         _ext_port = 0;
57 }
58
59 void
60 MidiPort::reset()
61 {
62         BaseMidiPort::reset();
63
64         if (_ext_port) {
65                 _ext_port->reset ();
66         }
67 }
68
69 void
70 MidiPort::cycle_start (nframes_t nframes, nframes_t offset)
71 {
72         if (_ext_port) {
73                 _ext_port->cycle_start (nframes, offset);
74         }
75 }
76
77 MidiBuffer &
78 MidiPort::get_midi_buffer( nframes_t nframes, nframes_t offset ) {
79         
80         if (_has_been_mixed_down)
81             return *_buffer;
82
83         if (_flags & IsInput) {
84                         
85                 if (_ext_port) {
86                 
87                         BaseMidiPort* mprt = dynamic_cast<BaseMidiPort*>(_ext_port);
88                         assert(mprt);
89                         assert(&mprt->get_midi_buffer(nframes,offset) == _buffer);
90
91                         if (!_connections.empty()) {
92                                 (*_mixdown) (_connections, _buffer, nframes, offset, false);
93                         }
94
95                 } else {
96                 
97                         if (_connections.empty()) {
98                                 _buffer->silence (nframes, offset);
99                         } else {
100                                 (*_mixdown) (_connections, _buffer, nframes, offset, true);
101                         }
102                 }
103
104         } else {
105                 _buffer->silence (nframes, offset);
106         }
107         if (nframes)
108                 _has_been_mixed_down = true;
109
110         return *_buffer;
111 }
112
113         
114 void
115 MidiPort::cycle_end (nframes_t nframes, nframes_t offset)
116 {
117         if (_ext_port) {
118                 _ext_port->cycle_end (nframes, offset);
119         }
120         _has_been_mixed_down = false;
121 }
122
123 void
124 MidiPort::flush_buffers (nframes_t nframes, nframes_t offset)
125 {
126         if (_ext_port) {
127                 _ext_port->flush_buffers (nframes, offset);
128         }
129 }