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