c62d31a6d02d6da7860e554ffce669bd2be841c4
[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/audioengine.h"
22 #include "ardour/data_type.h"
23 #include "ardour/audio_buffer.h"
24
25 using namespace ARDOUR;
26 using namespace std;
27
28 AudioPort::AudioPort (const std::string& name, Flags flags)
29         : Port (name, DataType::AUDIO, flags)
30         , _buffer (new AudioBuffer (0))
31 {
32         assert (name.find_first_of (':') == string::npos);
33 }
34
35 AudioPort::~AudioPort ()
36 {
37         delete _buffer;
38 }
39
40 void
41 AudioPort::cycle_start (nframes_t nframes)
42 {
43         /* caller must hold process lock */
44
45         /* get_buffer() must only be run on outputs here in cycle_start().
46
47            Inputs must be done in the correct processing order, which 
48            requires interleaving with route processing. that will 
49            happen when Port::get_buffer() is called.
50         */
51
52         if (sends_output()) {
53
54                 /* Notice that cycle_start() is always run with the *entire* process cycle frame count,
55                    so we do not bother to apply _port_offset here - we always want the address of the
56                    entire JACK port buffer. We are not collecting data here - just noting the
57                    address where we will write data later in the process cycle.
58                 */
59
60                 _buffer->set_data ((Sample *) jack_port_get_buffer (_jack_port, nframes), nframes);
61                 _buffer->prepare ();
62         }
63 }
64
65 void
66 AudioPort::cycle_end (nframes_t nframes)
67 {
68         if (sends_output() && !_buffer->written()) {
69                 _buffer->silence (nframes);
70         }
71 }
72
73 void
74 AudioPort::cycle_split ()
75 {
76 }
77
78 AudioBuffer&
79 AudioPort::get_audio_buffer (nframes_t nframes, nframes_t offset)
80 {
81         /* caller must hold process lock */
82
83         if (receives_input ()) {
84
85                 /* Get a pointer to the audio data @ offset + _port_offset within the JACK port buffer and store
86                    it in our _buffer member.
87
88                    Note that offset is expected to be zero in almost all cases.
89                 */
90
91                 _buffer->set_data ((Sample *) jack_port_get_buffer (_jack_port, nframes) + offset + _port_offset, nframes);
92         } 
93         
94         /* output ports set their _buffer data information during ::cycle_start()
95          */
96
97         return *_buffer;
98 }
99