rollback to 3428, before the mysterious removal of libs/* at 3431/3432
[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 {
34         // FIXME: size kludge (see BufferSet::ensure_buffers)
35         // Jack needs to tell us this
36         _buffer = new MidiBuffer (capacity * 8);
37
38         if (external) {
39                 /* external ports use the same buffer for the jack port (_ext_port)
40                  * and internal ports (this) */
41                 _ext_port = new JackMidiPort (name, flags, _buffer);
42                 Port::set_name (_ext_port->name());
43         } else {
44                 /* internal ports just have a single buffer, no jack port */
45                 _ext_port = 0;
46                 set_name (name);
47         }
48
49         reset ();
50 }
51
52 MidiPort::~MidiPort()
53 {
54         if (_ext_port) {
55                 delete _ext_port;
56                 _ext_port = 0;
57         }
58 }
59
60 void
61 MidiPort::reset()
62 {
63         BaseMidiPort::reset();
64
65         if (_ext_port) {
66                 _ext_port->reset ();
67         }
68 }
69
70 void
71 MidiPort::cycle_start (nframes_t nframes, nframes_t offset)
72 {
73         if (_ext_port) {
74                 _ext_port->cycle_start (nframes, offset);
75         }
76         
77         if (_flags & IsInput) {
78                         
79                 if (_ext_port) {
80                 
81                         BaseMidiPort* mprt = dynamic_cast<BaseMidiPort*>(_ext_port);
82                         assert(mprt);
83                         assert(&mprt->get_midi_buffer() == _buffer);
84
85                         if (!_connections.empty()) {
86                                 (*_mixdown) (_connections, _buffer, nframes, offset, false);
87                         }
88
89                 } else {
90                 
91                         if (_connections.empty()) {
92                                 _buffer->silence (nframes, offset);
93                         } else {
94                                 (*_mixdown) (_connections, _buffer, nframes, offset, true);
95                         }
96                 }
97
98         } else {
99                         
100                 _buffer->silence (nframes, offset);
101         }
102 }
103
104         
105 void
106 MidiPort::cycle_end (nframes_t nframes, nframes_t offset)
107 {
108         if (_ext_port) {
109                 _ext_port->cycle_end (nframes, offset);
110         }
111 }
112