summaryrefslogtreecommitdiffstats
path: root/variablespeed/jni/sola_time_scaler.cc
blob: 1ff1236bcb96d95b7fd6ad697b3f54150f7061b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "sola_time_scaler.h"

#include <math.h>
#include <hlogging.h>
#include <algorithm>

#include "ring_buffer.h"

#define FLAGS_sola_ring_buffer 2.0
#define FLAGS_sola_enable_correlation true


namespace video_editing {

// Returns a cross-correlation score for the specified buffers.
int SolaAnalyzer::Correlate(const float* buffer1, const float* buffer2,
                            int num_frames) {
  CHECK(initialized_);

  int score = 0;
  num_frames *= num_channels_;
  while (num_frames-- > 0) {
    // Increment the score if the sign bits match.
    score += ((bit_cast<int32>(*buffer1++) ^ bit_cast<int32>(*buffer2++)) >= 0)
              ? 1 : 0;
  }
  return score;
}

// Trivial SolaAnalyzer class to bypass correlation.
class SolaBypassAnalyzer : public SolaAnalyzer {
 public:
  SolaBypassAnalyzer() { }
  virtual int Correlate(const float*, const float*, int num_frames) {
    return num_frames * num_channels_;
  }
};


// Default constructor.
SolaTimeScaler::SolaTimeScaler()
    : input_buffer_(NULL), output_buffer_(NULL), analyzer_(NULL) {
  sample_rate_ = 0;
  num_channels_ = 0;

  draining_ = false;
  initialized_ = false;
}

SolaTimeScaler::~SolaTimeScaler() {
  delete input_buffer_;
  delete output_buffer_;
  delete analyzer_;
}

// Injects a SolaAnalyzer instance for analyzing signal frames.
void SolaTimeScaler::set_analyzer(SolaAnalyzer* analyzer) {
  MutexLock lock(&mutex_);  // lock out processing while updating
  delete analyzer_;
  analyzer_ = analyzer;
}

// Initializes a SOLA timescaler.
void SolaTimeScaler::Init(double sample_rate,
                          int num_channels,
                          double initial_speed,
                          double window_duration,
                          double overlap_duration) {
  MutexLock lock(&mutex_);  // lock out processing while updating

  sample_rate_ = sample_rate;
  num_channels_ = num_channels;
  speed_ = initial_speed;
  window_duration_ = window_duration;
  overlap_duration_ = overlap_duration;

  initialized_ = true;
  GenerateParameters();
  Reset();
}

// Adjusts the rate scaling factor.
void SolaTimeScaler::set_speed(double speed) {
  MutexLock lock(&mutex_);  // lock out processing while updating

  speed_ = speed;
  GenerateParameters();
}

// Generates processing parameters from the current settings.
void SolaTimeScaler::GenerateParameters() {
  if (speed_ < 0.1) {
    LOGE("Requested speed %fx limited to 0.1x", speed_);
    speed_ = 0.1;
  } else if (speed_ > 8.0) {
    LOGE("Requested speed %fx limited to 8.0x", speed_);
    speed_ = 8.0;
  }

  ratio_ = 1.0 / speed_;

  num_window_frames_ = nearbyint(sample_rate_ * window_duration_);

  // Limit the overlap to half the window size, and round up to an odd number.
  // Half of overlap window (rounded down) is also a useful number.
  overlap_duration_ = min(overlap_duration_, window_duration_ / 2.0);
  num_overlap_frames_ = nearbyint(sample_rate_ * overlap_duration_);
  num_overlap_frames_ |= 1;
  half_overlap_frames_ = num_overlap_frames_ >> 1;

  if (speed_ >= 1.) {
    // For compression (speed up), adjacent input windows overlap in the output.
    input_window_offset_ = num_window_frames_;
    target_merge_offset_ = nearbyint(num_window_frames_ * ratio_);
  } else {
    // For expansion (slow down), each input window start point overlaps the
    // previous, and they are placed adjacently in the output
    // (+/- half the overlap size).
    input_window_offset_ = nearbyint(num_window_frames_ * speed_);
    target_merge_offset_ = num_window_frames_;
  }

  // Make sure we copy enough extra data to be able to perform a
  // frame correlation over the range of target merge point +/- half overlap,
  // even when the previous merge point was adjusted backwards a half overlap.
  max_frames_to_merge_ = max(num_window_frames_,
      target_merge_offset_ + (2 * num_overlap_frames_));
  min_output_to_hold_=
      max_frames_to_merge_ + num_overlap_frames_ - target_merge_offset_;
}

// The input buffer has one writer and reader.
// The output buffer has one reader/updater, and one reader/consumer.
static const int kInputReader = 0;
static const int kOutputAnalysis = 0;
static const int kOutputConsumer = 1;

void SolaTimeScaler::Reset() {
  CHECK(initialized_);
  double duration = max(FLAGS_sola_ring_buffer, 20. * window_duration_);
  draining_ = false;

  delete input_buffer_;
  input_buffer_ = new RingBuffer();
  input_buffer_->Init(static_cast<int>
      (sample_rate_ * duration), num_channels_, 1);

  delete output_buffer_;
  output_buffer_ = new RingBuffer();
  output_buffer_->Init(static_cast<int>
      (sample_rate_ * ratio_ * duration), num_channels_, 2);

  if (analyzer_ == NULL) {
    if (FLAGS_sola_enable_correlation) {
      analyzer_ = new SolaAnalyzer();
    } else {
      analyzer_ = new SolaBypassAnalyzer();
    }
  }
  analyzer_->Init(sample_rate_, num_channels_);
}

// Returns the number of frames that the input buffer can accept.
int SolaTimeScaler::input_limit() const {
  CHECK(initialized_);
  return input_buffer_->overhead();
}

// Returns the number of available output frames.
int SolaTimeScaler::available() {
  CHECK(initialized_);

  int available = output_buffer_->available(kOutputConsumer);
  if (available > min_output_to_hold_) {
    available -= min_output_to_hold_;
  } else if (draining_) {
    Process();
    available = output_buffer_->available(kOutputConsumer);
    if (available > min_output_to_hold_) {
      available -= min_output_to_hold_;
    }
  } else {
    available = 0;
  }
  return available;
}

void SolaTimeScaler::Drain() {
  CHECK(initialized_);

  draining_ = true;
}


// Feeds audio to the timescaler, and processes as much data as possible.
int SolaTimeScaler::InjectSamples(float* buffer, int num_frames) {
  CHECK(initialized_);

  // Do not write more frames than the buffer can accept.
  num_frames = min(input_limit(), num_frames);
  if (!num_frames) {
    return 0;
  }

  // Copy samples to the input buffer and then process whatever can be consumed.
  input_buffer_->Write(buffer, num_frames);
  Process();
  return num_frames;
}

// Retrieves audio data from the timescaler.
int SolaTimeScaler::RetrieveSamples(float* buffer, int num_frames) {
  CHECK(initialized_);

  // Do not read more frames than available.
  num_frames = min(available(), num_frames);
  if (!num_frames) {
    return 0;
  }

  output_buffer_->Copy(kOutputConsumer, buffer, num_frames);
  output_buffer_->Seek(kOutputConsumer,
                       output_buffer_->Tell(kOutputConsumer) + num_frames);

  return num_frames;
}

// Munges input samples to produce output.
bool SolaTimeScaler::Process() {
  CHECK(initialized_);
  bool generated_data = false;

  // We can only process data if there is sufficient input available
  // (or we are draining the latency), and there is sufficient room
  // for output to be merged.
  while (((input_buffer_->available(kInputReader) > max_frames_to_merge_) ||
         draining_) && (output_buffer_->overhead() >= max_frames_to_merge_)) {
    MutexLock lock(&mutex_);  // lock out updates while processing each window

    // Determine the number of samples to merge into the output.
    int input_count =
        min(input_buffer_->available(kInputReader), max_frames_to_merge_);
    if (input_count == 0) {
      break;
    }
    // The input reader always points to the next window to process.
    float* input_pointer = input_buffer_->GetPointer(kInputReader, input_count);

    // The analysis reader always points to the ideal target merge point,
    // minus half an overlap window (ie, the starting point for correlation).
    // That means the available data from that point equals the number
    // of samples that must be cross-faded.
    int output_merge_cnt = output_buffer_->available(kOutputAnalysis);
    float* output_pointer =
        output_buffer_->GetPointer(kOutputAnalysis, output_merge_cnt);

    // If there is not enough data to do a proper correlation,
    // just merge at the ideal target point. Otherwise,
    // find the best correlation score, working from the center out.
    int merge_offset = min(output_merge_cnt, half_overlap_frames_);

    if ((output_merge_cnt >= (2 * num_overlap_frames_)) &&
        (input_count >= num_overlap_frames_)) {
      int best_offset = merge_offset;
      int best_score = 0;
      int score;
      for (int i = 0; i <= half_overlap_frames_; ++i) {
        score = analyzer_->Correlate(input_pointer,
            output_pointer + ((merge_offset + i) * num_channels_),
            num_overlap_frames_);
        if (score > best_score) {
          best_score = score;
          best_offset = merge_offset + i;
          if (score == (num_overlap_frames_ * num_channels_)) {
            break;  // It doesn't get better than perfect.
          }
        }
        if (i > 0) {
          score = analyzer_->Correlate(input_pointer,
              output_pointer + ((merge_offset - i) * num_channels_),
              num_overlap_frames_);
          if (score > best_score) {
            best_score = score;
            best_offset = merge_offset - i;
            if (score == (num_overlap_frames_ * num_channels_)) {
              break;  // It doesn't get better than perfect.
            }
          }
        }
      }
      merge_offset = best_offset;
    } else if ((output_merge_cnt > 0) && !draining_) {
      LOGE("no correlation performed");
    }

    // Crossfade the overlap between input and output, and then
    // copy in the remaining input.
    int crossfade_count = max(0, (output_merge_cnt - merge_offset));
    crossfade_count = min(crossfade_count, input_count);
    int remaining_count = input_count - crossfade_count;

    float* merge_pointer = output_pointer + (merge_offset * num_channels_);
    float flt_count = static_cast<float>(crossfade_count);
    for (int i = 0; i < crossfade_count; ++i) {
      // Linear cross-fade, for now.
      float input_scale = static_cast<float>(i) / flt_count;
      float output_scale = 1. - input_scale;
      for (int j = 0; j < num_channels_; ++j) {
        *merge_pointer = (*merge_pointer * output_scale) +
                         (*input_pointer++ * input_scale);
        ++merge_pointer;
      }
    }
    // Copy the merged buffer back into the output, if necessary, and
    // append the rest of the window.
    output_buffer_->MergeBack(kOutputAnalysis,
                              output_pointer, output_merge_cnt);
    output_buffer_->Write(input_pointer, remaining_count);

    // Advance the output analysis pointer to the next target merge point,
    // minus half an overlap window.  The target merge point is always
    // calculated as a delta from the previous ideal target, not the actual
    // target, to avoid drift.
    int output_advance = target_merge_offset_;
    if (output_merge_cnt < half_overlap_frames_) {
      // On the first window, back up the pointer for the next correlation.
      // Thereafter, that compensation is preserved.
      output_advance -= half_overlap_frames_;
    }

    // Don't advance beyond the available data, when finishing up.
    if (draining_) {
      output_advance =
          min(output_advance, output_buffer_->available(kOutputAnalysis));
    }
    output_buffer_->Seek(kOutputAnalysis,
        output_buffer_->Tell(kOutputAnalysis) + output_advance);

    // Advance the input pointer beyond the frames that are no longer needed.
    input_buffer_->Seek(kInputReader, input_buffer_->Tell(kInputReader) +
                        min(input_count, input_window_offset_));

    if ((crossfade_count + remaining_count) > 0) {
      generated_data = true;
    }
  }  // while (more to process)
  return generated_data;
}

}  // namespace video_editing