comparison audio/EffectWrapper.cpp @ 740:846970dbef17 audio-source-refactor

Use shared_ptr for plugin instances throughout
author Chris Cannam
date Fri, 20 Mar 2020 16:31:58 +0000
parents ddfac001b543
children 54393ed09d65
comparison
equal deleted inserted replaced
739:ddfac001b543 740:846970dbef17
16 16
17 #include <rubberband/RubberBandStretcher.h> 17 #include <rubberband/RubberBandStretcher.h>
18 18
19 #include "base/Debug.h" 19 #include "base/Debug.h"
20 20
21 //#define DEBUG_EFFECT_WRAPPER 1
22
21 using namespace std; 23 using namespace std;
22 24
23 static const int DEFAULT_RING_BUFFER_SIZE = 131071; 25 static const int DEFAULT_RING_BUFFER_SIZE = 131071;
24 26
25 EffectWrapper::EffectWrapper(ApplicationPlaybackSource *source) : 27 EffectWrapper::EffectWrapper(ApplicationPlaybackSource *source) :
37 void 39 void
38 EffectWrapper::setEffect(weak_ptr<RealTimePluginInstance> effect) 40 EffectWrapper::setEffect(weak_ptr<RealTimePluginInstance> effect)
39 { 41 {
40 lock_guard<mutex> guard(m_mutex); 42 lock_guard<mutex> guard(m_mutex);
41 43
44 #ifdef DEBUG_EFFECT_WRAPPER
45 SVCERR << "EffectWrapper[" << this
46 << "]::setEffect(" << effect.lock() << ")" << endl;
47 #endif
48
42 m_effect = effect; 49 m_effect = effect;
43 m_failed = false; 50 m_failed = false;
44 } 51 }
45 52
46 void 53 void
47 EffectWrapper::setBypassed(bool bypassed) 54 EffectWrapper::setBypassed(bool bypassed)
48 { 55 {
49 lock_guard<mutex> guard(m_mutex); 56 lock_guard<mutex> guard(m_mutex);
50 57
58 #ifdef DEBUG_EFFECT_WRAPPER
59 SVCERR << "EffectWrapper[" << this
60 << "]::setBypassed(" << bypassed << ")" << endl;
61 #endif
62
51 m_bypassed = bypassed; 63 m_bypassed = bypassed;
52 } 64 }
53 65
54 bool 66 bool
55 EffectWrapper::isBypassed() const 67 EffectWrapper::isBypassed() const
62 void 74 void
63 EffectWrapper::reset() 75 EffectWrapper::reset()
64 { 76 {
65 lock_guard<mutex> guard(m_mutex); 77 lock_guard<mutex> guard(m_mutex);
66 78
79 #ifdef DEBUG_EFFECT_WRAPPER
80 SVCERR << "EffectWrapper[" << this << "]::reset" << endl;
81 #endif
82
67 for (auto &rb: m_effectOutputBuffers) { 83 for (auto &rb: m_effectOutputBuffers) {
68 rb.reset(); 84 rb.reset();
69 } 85 }
86
87 m_failed = false;
70 } 88 }
71 89
72 int 90 int
73 EffectWrapper::getSourceSamples(float *const *samples, 91 EffectWrapper::getSourceSamples(float *const *samples,
74 int nchannels, int nframes) 92 int nchannels, int nframes)
75 { 93 {
76 lock_guard<mutex> guard(m_mutex); 94 lock_guard<mutex> guard(m_mutex);
77 95
96 #ifdef DEBUG_EFFECT_WRAPPER
97 SVCERR << "EffectWrapper[" << this << "]::getSourceSamples: " << nframes
98 << " frames across " << nchannels << " channels" << endl;
99 #endif
100
78 auto effect(m_effect.lock()); 101 auto effect(m_effect.lock());
79 102
80 if (!effect || m_bypassed || m_failed) { 103 if (!effect) {
104 #ifdef DEBUG_EFFECT_WRAPPER
105 SVCERR << "EffectWrapper::getSourceSamples: "
106 << "no effect is set" << endl;
107 #endif
81 return m_source->getSourceSamples(samples, nchannels, nframes); 108 return m_source->getSourceSamples(samples, nchannels, nframes);
82 } 109 }
83 110
111 if (m_bypassed || m_failed) {
112 #ifdef DEBUG_EFFECT_WRAPPER
113 SVCERR << "EffectWrapper::getSourceSamples: "
114 << "effect is bypassed or has failed" << endl;
115 #endif
116 return m_source->getSourceSamples(samples, nchannels, nframes);
117 }
118
84 static int warnings = 0; 119 static int warnings = 0;
85 if (nchannels != m_channelCount) { 120 if (nchannels != m_channelCount) {
86 if (warnings >= 0) { 121 if (warnings >= 0) {
87 SVCERR << "WARNING: getSourceSamples called for a number of channels different from that set with setSystemPlaybackChannelCount (" 122 SVCERR << "WARNING: EffectWrapper::getSourceSamples called for a number of channels different from that set with setSystemPlaybackChannelCount ("
88 << nchannels << " vs " << m_channelCount << ")" << endl; 123 << nchannels << " vs " << m_channelCount << ")" << endl;
89 if (++warnings == 6) { 124 if (++warnings == 6) {
90 SVCERR << "(further warnings will be suppressed)" << endl; 125 SVCERR << "(further warnings will be suppressed)" << endl;
91 warnings = -1; 126 warnings = -1;
92 } 127 }
124 float **ib = effect->getAudioInputBuffers(); 159 float **ib = effect->getAudioInputBuffers();
125 float **ob = effect->getAudioOutputBuffers(); 160 float **ob = effect->getAudioOutputBuffers();
126 int blockSize = effect->getBufferSize(); 161 int blockSize = effect->getBufferSize();
127 162
128 int got = 0; 163 int got = 0;
129 int offset = 0;
130 164
131 while (got < nframes) { 165 while (got < nframes) {
132 166
133 int read = 0; 167 int read = 0;
134 for (int c = 0; c < nchannels; ++c) { 168 for (int c = 0; c < nchannels; ++c) {
135 read = m_effectOutputBuffers[c].read(samples[c], nframes - got); 169 read = m_effectOutputBuffers[c].read(samples[c] + got,
170 nframes - got);
136 } 171 }
137 172
138 got += read; 173 got += read;
139 174
140 if (got < nframes) { 175 if (got < nframes) {
141 176
142 int toRun = m_source->getSourceSamples(ib, nchannels, blockSize); 177 int toRun = m_source->getSourceSamples(ib, nchannels, blockSize);
143 if (toRun <= 0) break; 178 if (toRun <= 0) break;
144 179
180 #ifdef DEBUG_EFFECT_WRAPPER
181 SVCERR << "EffectWrapper::getSourceSamples: Running effect "
182 << "for " << toRun << " frames" << endl;
183 #endif
145 effect->run(Vamp::RealTime::zeroTime, toRun); 184 effect->run(Vamp::RealTime::zeroTime, toRun);
146 185
147 for (int c = 0; c < nchannels; ++c) { 186 for (int c = 0; c < nchannels; ++c) {
148 m_effectOutputBuffers[c].write(ob[c], toRun); 187 m_effectOutputBuffers[c].write(ob[c], toRun);
149 } 188 }
156 void 195 void
157 EffectWrapper::setSystemPlaybackChannelCount(int count) 196 EffectWrapper::setSystemPlaybackChannelCount(int count)
158 { 197 {
159 { 198 {
160 lock_guard<mutex> guard(m_mutex); 199 lock_guard<mutex> guard(m_mutex);
200 #ifdef DEBUG_EFFECT_WRAPPER
201 SVCERR << "EffectWrapper[" << this
202 << "]::setSystemPlaybackChannelCount(" << count << ")" << endl;
203 #endif
161 m_effectOutputBuffers.resize 204 m_effectOutputBuffers.resize
162 (count, RingBuffer<float>(DEFAULT_RING_BUFFER_SIZE)); 205 (count, RingBuffer<float>(DEFAULT_RING_BUFFER_SIZE));
163 m_channelCount = count; 206 m_channelCount = count;
164 } 207 }
165 m_source->setSystemPlaybackChannelCount(count); 208 m_source->setSystemPlaybackChannelCount(count);