8fb66255d81350b54fdcf614edfb287cd142f725
[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 publish, nframes_t bufsize)
30         : Port (name, flags)
31         , BaseMidiPort (name, flags)
32         , PortFacade (name, flags)
33 {
34         set_name (name);
35
36         _buffer = new MidiBuffer (bufsize);
37
38         if (!publish) {
39                 _ext_port = 0;
40         } else {
41                 _ext_port = new JackMidiPort (name, flags, _buffer);
42         }
43
44         reset ();
45 }
46
47 MidiPort::~MidiPort()
48 {
49         if (_ext_port) {
50                 delete _ext_port;
51                 _ext_port = 0;
52         }
53 }
54
55 void
56 MidiPort::reset()
57 {
58         BaseMidiPort::reset();
59
60         if (_ext_port) {
61                 _ext_port->reset ();
62         }
63 }
64
65 void
66 MidiPort::cycle_start (nframes_t nframes, nframes_t offset)
67 {
68         /* caller must hold process lock */
69         
70         if (_ext_port) {
71                 _ext_port->cycle_start (nframes, offset);
72         }
73
74         if (_flags & IsInput) {
75
76                 if (_ext_port) {
77                         _buffer->read_from (dynamic_cast<BaseMidiPort*>(_ext_port)->get_midi_buffer(), nframes, offset);
78                         if (!_connections.empty()) {
79                                 (*_mixdown) (_connections, _buffer, nframes, offset, false);
80                         }
81
82                 } else {
83                 
84                         if (_connections.empty()) {
85                                 _buffer->silence (nframes, offset);
86                         } else {
87                                 (*_mixdown) (_connections, _buffer, nframes, offset, true);
88                         }
89                 }
90
91         } else {
92                 
93                 _buffer->silence (nframes, offset);
94         }
95 }