comparison audio/TimeStretchWrapper.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
comparison
equal deleted inserted replaced
739:ddfac001b543 740:846970dbef17
83 83
84 if (!m_stretcher) { 84 if (!m_stretcher) {
85 return m_source->getSourceSamples(samples, nchannels, nframes); 85 return m_source->getSourceSamples(samples, nchannels, nframes);
86 } 86 }
87 87
88 sv_frame_t available;
89 sv_frame_t fedToStretcher = 0;
90
91 vector<float *> inputPtrs(m_channelCount, nullptr); 88 vector<float *> inputPtrs(m_channelCount, nullptr);
92 for (int i = 0; i < m_channelCount; ++i) { 89 for (int i = 0; i < m_channelCount; ++i) {
93 inputPtrs[i] = m_inputs[i].data(); 90 inputPtrs[i] = m_inputs[i].data();
94 } 91 }
95 92
96 // The input block for a given output is approx output / ratio, 93 // The input block for a given output is approx output / ratio,
97 // but we can't predict it exactly, for an adaptive timestretcher. 94 // but we can't predict it exactly, for an adaptive timestretcher.
98 95
96 sv_frame_t available;
97
99 while ((available = m_stretcher->available()) < nframes) { 98 while ((available = m_stretcher->available()) < nframes) {
100 99
101 sv_frame_t reqd = sv_frame_t 100 int reqd = int(ceil(double(nframes - available) / m_timeRatio));
102 (ceil(double(nframes - available) / m_timeRatio)); 101 reqd = std::max(reqd, int(m_stretcher->getSamplesRequired()));
103 reqd = std::max(reqd, sv_frame_t(m_stretcher->getSamplesRequired()));
104 reqd = std::min(reqd, m_stretcherInputSize); 102 reqd = std::min(reqd, m_stretcherInputSize);
105 if (reqd == 0) reqd = 1; 103 if (reqd == 0) reqd = 1;
106 104
107 int got = m_source->getSourceSamples 105 int got = m_source->getSourceSamples
108 (inputPtrs.data(), nchannels, reqd); 106 (inputPtrs.data(), nchannels, reqd);
124 TimeStretchWrapper::checkStretcher() 122 TimeStretchWrapper::checkStretcher()
125 { 123 {
126 lock_guard<mutex> guard(m_mutex); 124 lock_guard<mutex> guard(m_mutex);
127 125
128 if (m_timeRatio == 1.0 || !m_channelCount || !m_sampleRate) { 126 if (m_timeRatio == 1.0 || !m_channelCount || !m_sampleRate) {
129 SVDEBUG << "TimeStretchWrapper::checkStretcher: m_timeRatio = "
130 << m_timeRatio << ", m_channelCount = " << m_channelCount
131 << ", m_sampleRate = " << m_sampleRate
132 << ", no need for stretcher" << endl;
133 if (m_stretcher) { 127 if (m_stretcher) {
134 SVDEBUG << "(Deleting existing one)" << endl; 128 SVDEBUG << "TimeStretchWrapper::checkStretcher: m_timeRatio = "
129 << m_timeRatio << ", m_channelCount = " << m_channelCount
130 << ", m_sampleRate = " << m_sampleRate
131 << ", deleting existing stretcher" << endl;
135 delete m_stretcher; 132 delete m_stretcher;
136 m_stretcher = nullptr; 133 m_stretcher = nullptr;
137 } 134 }
138 return; 135 return;
139 } 136 }
145 } 142 }
146 143
147 SVDEBUG << "TimeStretchWrapper::checkStretcher: creating stretcher with ratio " << m_timeRatio << endl; 144 SVDEBUG << "TimeStretchWrapper::checkStretcher: creating stretcher with ratio " << m_timeRatio << endl;
148 145
149 m_stretcher = new RubberBandStretcher 146 m_stretcher = new RubberBandStretcher
150 (m_sampleRate, 147 (size_t(round(m_sampleRate)),
151 m_channelCount, 148 m_channelCount,
152 RubberBandStretcher::OptionProcessRealTime, 149 RubberBandStretcher::OptionProcessRealTime,
153 m_timeRatio); 150 m_timeRatio);
154 151
155 m_inputs.resize(m_channelCount); 152 m_inputs.resize(m_channelCount);