Merge libs/ardour and gtk2_ardour with 2.0-ongoing R2837.
[ardour.git] / libs / ardour / audio_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/audio_port.h>
21 #include <ardour/jack_audio_port.h>
22 #include <ardour/audioengine.h>
23 #include <ardour/data_type.h>
24
25 using namespace ARDOUR;
26 using namespace std;
27
28 AudioPort::AudioPort (const std::string& name, Flags flags, bool external, nframes_t capacity)
29         : Port (name, flags)
30         , BaseAudioPort (name, flags)
31         , PortFacade (name, flags)
32 {
33         if (!external || receives_input()) {
34
35                 /* internal-only and input ports need their own buffers.
36                    external output ports use the external port buffer.
37                 */
38
39                 _buffer = new AudioBuffer (capacity);
40                 _own_buffer = true;
41         }
42
43         if (!external) {
44
45                 _ext_port = 0;
46                 set_name (name);
47
48         } else {
49                 
50                 /* make the JackAudioPort create its own buffer. For input,
51                    we will copy from it during cycle_start(). For output,
52                    we will set up our buffer to point to its buffer, which
53                    will in turn be using the JACK port buffer for data.
54                 */
55
56                 _ext_port = new JackAudioPort (name, flags, 0);
57
58                 if (sends_output()) {
59                         _buffer = &dynamic_cast<JackAudioPort*>(_ext_port)->get_audio_buffer();
60                 } 
61
62                 Port::set_name (_ext_port->name());
63         }
64
65         reset ();
66 }
67
68 AudioPort::~AudioPort()
69 {
70         if (_ext_port) {
71                 delete _ext_port;
72                 _ext_port = 0;
73         }
74 }
75
76 void
77 AudioPort::reset()
78 {
79         BaseAudioPort::reset();
80
81         if (_ext_port) {
82                 _ext_port->reset ();
83         }
84 }
85
86
87 void
88 AudioPort::cycle_start (nframes_t nframes, nframes_t offset)
89 {
90         /* caller must hold process lock */
91
92         if (_ext_port) {
93                 _ext_port->cycle_start (nframes, offset);
94         }
95
96         if (_flags & IsInput) {
97
98                 if (_ext_port) {
99                         _buffer->read_from (dynamic_cast<BaseAudioPort*>(_ext_port)->get_audio_buffer(), nframes, offset);
100
101                         if (!_connections.empty()) {
102                                 (*_mixdown) (_connections, _buffer, nframes, offset, false);
103                         }
104
105                 } else {
106                 
107                         if (_connections.empty()) {
108                                 _buffer->silence (nframes, offset);
109                         } else {
110                                 (*_mixdown) (_connections, _buffer, nframes, offset, true);
111                         }
112                 }
113
114         } else {
115                 
116                 // XXX if we could get the output stage to not purely mix into, but also
117                 // to initially overwrite the buffer, we could avoid this silence step.
118                 
119                 _buffer->silence (nframes, offset);
120         }
121 }
122
123 void
124 AudioPort::cycle_end (nframes_t nframes, nframes_t offset)
125 {
126 }