Apply MIDI looping patch from torbenh, with minor changes.
[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         , _has_been_mixed_down( false )
33 {
34         if (!external || receives_input()) {
35
36                 /* internal-only and input ports need their own buffers.
37                    external output ports use the external port buffer.
38                 */
39
40                 _buffer = new AudioBuffer (capacity);
41                 _own_buffer = true;
42         }
43
44         if (!external) {
45
46                 _ext_port = 0;
47                 set_name (name);
48
49         } else {
50                 
51                 /* make the JackAudioPort create its own buffer. For input,
52                    we will copy from it during cycle_start(). For output,
53                    we will set up our buffer to point to its buffer, which
54                    will in turn be using the JACK port buffer for data.
55                 */
56
57                 _ext_port = new JackAudioPort (name, flags, 0);
58
59                 //if (sends_output()) {
60                 //      _buffer = &dynamic_cast<JackAudioPort*>(_ext_port)->get_audio_buffer( nframes, offset );
61                 //} 
62
63                 Port::set_name (_ext_port->name());
64         }
65
66         reset ();
67 }
68
69 AudioPort::~AudioPort()
70 {
71         if (_ext_port) {
72                 delete _ext_port;
73                 _ext_port = 0;
74         }
75 }
76
77 void
78 AudioPort::reset()
79 {
80         BaseAudioPort::reset();
81
82         if (_ext_port) {
83                 _ext_port->reset ();
84         }
85 }
86
87
88 void
89 AudioPort::cycle_start (nframes_t nframes, nframes_t offset)
90 {
91         /* caller must hold process lock */
92
93         if (_ext_port) {
94                 _ext_port->cycle_start (nframes, offset);
95         }
96         _has_been_mixed_down = false;
97 }
98
99 AudioBuffer &
100 AudioPort::get_audio_buffer( nframes_t nframes, nframes_t offset ) {
101
102         if (_has_been_mixed_down)       
103                 return *_buffer;
104
105         if (_flags & IsInput) {
106
107                 if (_ext_port) {
108                         _buffer->read_from (dynamic_cast<BaseAudioPort*>(_ext_port)->get_audio_buffer (nframes, offset), nframes, offset);
109
110                         if (!_connections.empty()) {
111                                 (*_mixdown) (_connections, _buffer, nframes, offset, false);
112                         }
113
114                 } else {
115                 
116                         if (_connections.empty()) {
117                                 _buffer->silence (nframes, offset);
118                         } else {
119                                 (*_mixdown) (_connections, _buffer, nframes, offset, true);
120                         }
121                 }
122
123         } else {
124                 
125                 // XXX if we could get the output stage to not purely mix into, but also
126                 // to initially overwrite the buffer, we could avoid this silence step.
127                 if (_ext_port) {
128                         _buffer = & (dynamic_cast<BaseAudioPort*>(_ext_port)->get_audio_buffer( nframes, offset ));
129                 }
130                 if (nframes)
131                         _buffer->silence (nframes, offset);
132         }
133         if (nframes)
134                 _has_been_mixed_down = true;
135
136         return *_buffer;
137 }
138
139 void
140 AudioPort::cycle_end (nframes_t nframes, nframes_t offset)
141 {
142         _has_been_mixed_down=false;
143 }