summaryrefslogtreecommitdiff
path: root/libs/ardour/audio_port.cc
blob: 0e48c2a69960200fb16f3b228f3dc63d17e7b892 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
    Copyright (C) 2006 Paul Davis 

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include <cassert>
#include <ardour/audio_port.h>
#include <ardour/audioengine.h>
#include <ardour/data_type.h>
#include <ardour/audio_buffer.h>

using namespace ARDOUR;
using namespace std;

AudioPort::AudioPort (const std::string& name, Flags flags, bool ext, nframes_t capacity)
	: Port (name, DataType::AUDIO, flags, ext)
	, _has_been_mixed_down (false)
	, _buffer (0)
	, _internal_buffer (false)
{
	assert (name.find_first_of (':') == string::npos);

	if (external ()) {
		
		/* external ports use the external port buffer */
		_buffer = new AudioBuffer (0);

	} else {

		/* internal ports need their own buffers */
		_buffer = new AudioBuffer (capacity);
	}

	check_buffer_status ();
	
}

AudioPort::~AudioPort()
{
	delete _buffer;
}

void
AudioPort::cycle_start (nframes_t nframes, nframes_t offset)
{
	/* caller must hold process lock */

	/* For external (JACK) ports, get_buffer() must only be run 
	   on outputs here in cycle_start().

	   Inputs must be done in the correct processing order, which 
	   requires interleaving with route processing. that will 
	   happen when Port::get_buffer() is called.
	*/
	
	if (!receives_input() && external ()) {
		_buffer->set_data ((Sample *) jack_port_get_buffer (_jack_port, nframes) + offset, nframes);
	}

	if (receives_input()) {
		_has_been_mixed_down = false;
	} else {
		_buffer->silence (nframes, offset);
	}
}

AudioBuffer &
AudioPort::get_audio_buffer (nframes_t nframes, nframes_t offset)
{
	/* caller must hold process lock */

	if (receives_input () && !_has_been_mixed_down) {

		/* external ports use JACK's memory unless otherwise noted */
		
		if (external()) {
			if (!using_internal_data()) {
				_buffer->set_data ((Sample *) jack_port_get_buffer (_jack_port, nframes) + offset, nframes);
			} else {
				_buffer->silence (nframes, offset);
			}
		}

		mixdown (nframes, offset, !external ());
	} 
	
	return *_buffer;
}

void
AudioPort::cycle_end (nframes_t nframes, nframes_t offset)
{
	_has_been_mixed_down = false;
}

void
AudioPort::mixdown (nframes_t cnt, nframes_t offset, bool first_overwrite)
{
	/* note: this is only called for input ports */

	if (_connections.empty()) {

		/* no internal mixing to do, so for internal ports
		   just make sure the buffer is silent.
		*/
		
		if (!external()) {
			_buffer->silence (cnt, offset);
		} 

	} else {

		set<Port*>::const_iterator p = _connections.begin();

		/* mix in internally-connected ports. if this is an external port
		   then it may already have data present from JACK. in that case, we 
		   do not want to overwrite that data, so we skip the initial ::read_from()
		   call and do everything with accumulate_from()
		 */

		if (!external()) {
			_buffer->read_from (dynamic_cast<AudioPort*>(*p)->get_audio_buffer (cnt, offset), cnt, offset);
			++p;
			
		}

		for (; p != _connections.end (); ++p) {
			_buffer->accumulate_from (dynamic_cast<AudioPort*>(*p)->get_audio_buffer (cnt, offset), cnt, offset);

		}
	}

	/* XXX horrible heuristic designed to check that we worked the whole buffer.
	   Needs fixing but its a hard problem.
	*/

	if (cnt && offset == 0) {
		_has_been_mixed_down = true;
	}
}

void
AudioPort::reset ()
{
	Port::reset ();
	
	if (_buffer->capacity () != 0) {
		_buffer->resize (_engine->frames_per_cycle ());
		_buffer->clear ();
	}
}

bool
AudioPort::using_internal_data () const
{
	return _internal_buffer;
}

void
AudioPort::use_internal_data ()
{
	_buffer->replace_data (_buffer->capacity());
	_internal_buffer = true;
}

void
AudioPort::use_external_data ()
{
	_internal_buffer = false;
	_buffer->drop_data ();
}