Commit f371f0c8 authored by Stefan Westerfeld's avatar Stefan Westerfeld

Optimize performance for skipping in resampled input.

Signed-off-by: Stefan Westerfeld's avatarStefan Westerfeld <stefan@space.twc.de>
parent c16d9bdf
...@@ -383,7 +383,7 @@ public: ...@@ -383,7 +383,7 @@ public:
{ {
} }
virtual void write_frames (const vector<float>& frames) = 0; virtual void write_frames (const vector<float>& frames, bool skip = false) = 0;
virtual vector<float> read_frames (size_t frames) = 0; virtual vector<float> read_frames (size_t frames) = 0;
virtual size_t can_read_frames() const = 0; virtual size_t can_read_frames() const = 0;
}; };
...@@ -392,13 +392,17 @@ template<class Resampler> ...@@ -392,13 +392,17 @@ template<class Resampler>
class BufferedResamplerImpl : public ResamplerImpl class BufferedResamplerImpl : public ResamplerImpl
{ {
const int n_channels = 0; const int n_channels = 0;
const int old_rate = 0;
const int new_rate = 0;
bool first_write = true; bool first_write = true;
Resampler m_resampler; Resampler m_resampler;
vector<float> buffer; vector<float> buffer;
public: public:
BufferedResamplerImpl (int n_channels) : BufferedResamplerImpl (int n_channels, int old_rate, int new_rate) :
n_channels (n_channels) n_channels (n_channels),
old_rate (old_rate),
new_rate (new_rate)
{ {
} }
Resampler& Resampler&
...@@ -407,7 +411,7 @@ public: ...@@ -407,7 +411,7 @@ public:
return m_resampler; return m_resampler;
} }
void void
write_frames (const vector<float>& frames) write_frames (const vector<float>& frames, bool skip)
{ {
if (first_write) if (first_write)
{ {
...@@ -424,6 +428,13 @@ public: ...@@ -424,6 +428,13 @@ public:
uint start = 0; uint start = 0;
do do
{
if (frames.size() / n_channels - start >= old_rate && skip)
{
buffer.resize (buffer.size() + new_rate * n_channels);
start += old_rate;
}
else
{ {
const int out_count = Params::frame_size; const int out_count = Params::frame_size;
float out[out_count * n_channels]; float out[out_count * n_channels];
...@@ -440,6 +451,7 @@ public: ...@@ -440,6 +451,7 @@ public:
start += frames.size() / n_channels - start - m_resampler.inp_count; start += frames.size() / n_channels - start - m_resampler.inp_count;
} }
}
while (start != frames.size() / n_channels); while (start != frames.size() / n_channels);
} }
vector<float> vector<float>
...@@ -481,7 +493,7 @@ create_resampler (int n_channels, int old_rate, int new_rate) ...@@ -481,7 +493,7 @@ create_resampler (int n_channels, int old_rate, int new_rate)
*/ */
const int hlen = 16; const int hlen = 16;
auto resampler = new BufferedResamplerImpl<Resampler> (n_channels); auto resampler = new BufferedResamplerImpl<Resampler> (n_channels, old_rate, new_rate);
if (resampler->resampler().setup (old_rate, new_rate, n_channels, hlen) == 0) if (resampler->resampler().setup (old_rate, new_rate, n_channels, hlen) == 0)
{ {
return resampler; return resampler;
...@@ -489,7 +501,7 @@ create_resampler (int n_channels, int old_rate, int new_rate) ...@@ -489,7 +501,7 @@ create_resampler (int n_channels, int old_rate, int new_rate)
else else
delete resampler; delete resampler;
auto vresampler = new BufferedResamplerImpl<VResampler> (n_channels); auto vresampler = new BufferedResamplerImpl<VResampler> (n_channels, old_rate, new_rate);
const double ratio = double (new_rate) / old_rate; const double ratio = double (new_rate) / old_rate;
if (vresampler->resampler().setup (ratio, n_channels, hlen) == 0) if (vresampler->resampler().setup (ratio, n_channels, hlen) == 0)
{ {
...@@ -575,7 +587,7 @@ public: ...@@ -575,7 +587,7 @@ public:
vector<float> samples (zeros * n_channels); vector<float> samples (zeros * n_channels);
/* resample to the watermark sample rate */ /* resample to the watermark sample rate */
in_resampler->write_frames (samples); in_resampler->write_frames (samples, true);
size_t skip_blocks = in_resampler->can_read_frames() / Params::frame_size; size_t skip_blocks = in_resampler->can_read_frames() / Params::frame_size;
...@@ -586,7 +598,7 @@ public: ...@@ -586,7 +598,7 @@ public:
wm_samples.resize (wm_gen.skip (r_samples.size() / n_channels) * n_channels); wm_samples.resize (wm_gen.skip (r_samples.size() / n_channels) * n_channels);
/* resample back to the original sample rate of the audio file */ /* resample back to the original sample rate of the audio file */
out_resampler->write_frames (wm_samples); out_resampler->write_frames (wm_samples, true);
size_t to_read = out_resampler->can_read_frames(); size_t to_read = out_resampler->can_read_frames();
return out_resampler->read_frames (to_read).size() / n_channels; return out_resampler->read_frames (to_read).size() / n_channels;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment