remove non rt-safe debug messages
[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
21 #include "pbd/malign.h"
22 #include "pbd/stacktrace.h"
23
24 #include "ardour/audio_buffer.h"
25 #include "ardour/audioengine.h"
26 #include "ardour/audio_port.h"
27 #include "ardour/data_type.h"
28 #include "ardour/port_engine.h"
29
30 using namespace ARDOUR;
31 using namespace std;
32
33 #define port_engine AudioEngine::instance()->port_engine()
34
35 AudioPort::AudioPort (const std::string& name, PortFlags flags)
36         : Port (name, DataType::AUDIO, flags)
37         , _buffer (new AudioBuffer (0))
38 {
39         assert (name.find_first_of (':') == string::npos);
40         cache_aligned_malloc ((void**) &_data, sizeof (Sample) * 8192);
41         _src.setup (_resampler_quality);
42         _src.set_rrfilt (10);
43 }
44
45 AudioPort::~AudioPort ()
46 {
47         cache_aligned_free (_data);
48         delete _buffer;
49 }
50
51 void
52 AudioPort::cycle_start (pframes_t nframes)
53 {
54         /* caller must hold process lock */
55         Port::cycle_start (nframes);
56
57         if (sends_output()) {
58                 _buffer->prepare ();
59         } else if (!externally_connected ()) {
60                 /* ardour internal port, just silence input, don't resample */
61                 // TODO reset resampler only once
62                 _src.reset ();
63                 memset (_data, 0, _cycle_nframes * sizeof (float));
64         } else {
65                 _src.inp_data  = (float*)port_engine.get_buffer (_port_handle, nframes);
66                 _src.inp_count = nframes;
67                 _src.out_count = _cycle_nframes;
68                 _src.set_rratio (_cycle_nframes / (double)nframes);
69                 _src.out_data  = _data;
70                 _src.process ();
71                 while (_src.out_count > 0) {
72                         *_src.out_data =  _src.out_data[-1];
73                         ++_src.out_data;
74                         --_src.out_count;
75                 }
76         }
77 }
78
79 void
80 AudioPort::cycle_end (pframes_t nframes)
81 {
82         if (sends_output() && !_buffer->written() && _port_handle) {
83                 if (!_buffer->data (0)) {
84                         get_audio_buffer (nframes);
85                 }
86                 if (_buffer->capacity() >= nframes) {
87                         _buffer->silence (nframes);
88                 }
89         }
90
91         if (sends_output() && _port_handle) {
92
93                 if (!externally_connected ()) {
94                         /* ardour internal port, data goes nowhere, skip resampling */
95                         // TODO reset resampler only once
96                         _src.reset ();
97                         return;
98                 }
99
100                 _src.inp_count = _cycle_nframes;
101                 _src.out_count = nframes;
102                 _src.set_rratio (nframes / (double)_cycle_nframes);
103                 _src.inp_data  = _data;
104                 _src.out_data  = (float*)port_engine.get_buffer (_port_handle, nframes);
105                 _src.process ();
106                 while (_src.out_count > 0) {
107                         *_src.out_data =  _src.out_data[-1];
108                         ++_src.out_data;
109                         --_src.out_count;
110                 }
111         }
112 }
113
114 void
115 AudioPort::cycle_split ()
116 {
117 }
118
119 AudioBuffer&
120 AudioPort::get_audio_buffer (pframes_t nframes)
121 {
122         /* caller must hold process lock */
123         assert (_port_handle);
124         if (!externally_connected ()) {
125                 _buffer->set_data ((Sample *) port_engine.get_buffer (_port_handle, _cycle_nframes) +
126                                 _global_port_buffer_offset, nframes);
127         } else {
128                 _buffer->set_data (&_data[_global_port_buffer_offset], nframes);
129         }
130         return *_buffer;
131 }
132
133 Sample*
134 AudioPort::engine_get_whole_audio_buffer ()
135 {
136         /* caller must hold process lock */
137         assert (_port_handle);
138         return (Sample *) port_engine.get_buffer (_port_handle, _cycle_nframes);
139 }