2013-09-17 08:38:02 +00:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
|
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
*/
|
|
|
|
|
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "modules/audio_coding/neteq/tools/audio_loop.h"
|
2013-09-17 08:38:02 +00:00
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
2022-07-20 12:53:07 +02:00
|
|
|
#include "absl/strings/string_view.h"
|
|
|
|
|
|
2013-09-17 08:38:02 +00:00
|
|
|
namespace webrtc {
|
|
|
|
|
namespace test {
|
|
|
|
|
|
2022-07-20 12:53:07 +02:00
|
|
|
bool AudioLoop::Init(absl::string_view file_name,
|
2013-09-17 08:38:02 +00:00
|
|
|
size_t max_loop_length_samples,
|
|
|
|
|
size_t block_length_samples) {
|
2022-07-20 12:53:07 +02:00
|
|
|
FILE* fp = fopen(std::string(file_name).c_str(), "rb");
|
2013-09-17 08:38:02 +00:00
|
|
|
if (!fp)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
audio_array_.reset(
|
|
|
|
|
new int16_t[max_loop_length_samples + block_length_samples]);
|
|
|
|
|
size_t samples_read =
|
|
|
|
|
fread(audio_array_.get(), sizeof(int16_t), max_loop_length_samples, fp);
|
|
|
|
|
fclose(fp);
|
|
|
|
|
|
|
|
|
|
// Block length must be shorter than the loop length.
|
|
|
|
|
if (block_length_samples > samples_read)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
// Add an extra block length of samples to the end of the array, starting
|
|
|
|
|
// over again from the beginning of the array. This is done to simplify
|
|
|
|
|
// the reading process when reading over the end of the loop.
|
|
|
|
|
memcpy(&audio_array_[samples_read], audio_array_.get(),
|
|
|
|
|
block_length_samples * sizeof(int16_t));
|
|
|
|
|
|
|
|
|
|
loop_length_samples_ = samples_read;
|
|
|
|
|
block_length_samples_ = block_length_samples;
|
2016-02-24 03:49:19 -08:00
|
|
|
next_index_ = 0;
|
2013-09-17 08:38:02 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-06 01:21:35 -08:00
|
|
|
rtc::ArrayView<const int16_t> AudioLoop::GetNextBlock() {
|
2013-09-17 08:38:02 +00:00
|
|
|
// Check that the AudioLoop is initialized.
|
2015-11-06 01:21:35 -08:00
|
|
|
if (block_length_samples_ == 0)
|
|
|
|
|
return rtc::ArrayView<const int16_t>();
|
2013-09-17 08:38:02 +00:00
|
|
|
|
|
|
|
|
const int16_t* output_ptr = &audio_array_[next_index_];
|
|
|
|
|
next_index_ = (next_index_ + block_length_samples_) % loop_length_samples_;
|
2015-11-06 01:21:35 -08:00
|
|
|
return rtc::ArrayView<const int16_t>(output_ptr, block_length_samples_);
|
2013-09-17 08:38:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace test
|
|
|
|
|
} // namespace webrtc
|