summaryrefslogtreecommitdiff
path: root/libs/ardour/audio_port.cc
blob: 55d65d850d6a69ebf564998bb644ff136ae78282 (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
/*
    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)
{
	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);
		
	}
	
}

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

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

	_has_been_mixed_down = false;

	if (external ()) {
		/* external ports use JACK's memory */
		_buffer->set_data ((Sample *) jack_port_get_buffer (_jack_port, nframes), nframes + offset);
	}
}

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

	if (_has_been_mixed_down) {
		return *_buffer;
	}

	if (receives_input ()) {

		/* INPUT */

		/* If we're external (), we have some data in our buffer set up by JACK;
		   otherwise, we have an undefined buffer.  In either case we mix down
		   our non-JACK inputs; either accumulating into the JACK data or
		   overwriting the undefined data */
		   
		mixdown (nframes, offset, !external ());
		
	} else {

		/* OUTPUT */

		if (!external ()) {
			/* start internal output buffers with silence */
			_buffer->silence (nframes, offset);
		}
		
	}
	
	if (nframes) {
		_has_been_mixed_down = true;
	}

	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)
{
	if (_connections.empty()) {
		if (first_overwrite) {
			_buffer->silence (cnt, offset);
		}
		return;
	}
	
	set<Port*>::const_iterator p = _connections.begin();

	if (first_overwrite) {
		_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);
	}
}

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