blob: 0045f068a3c15c2329c8cc04ae013a9cf1a25e9b [file] [log] [blame]
henrik.lundin@webrtc.orgd1fc5d42013-09-17 08:38:02 +00001/*
2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/audio_coding/neteq/tools/audio_loop.h"
henrik.lundin@webrtc.orgd1fc5d42013-09-17 08:38:02 +000012
henrik.lundin@webrtc.orgd1fc5d42013-09-17 08:38:02 +000013#include <stdio.h>
14#include <string.h>
15
16namespace webrtc {
17namespace test {
18
19bool AudioLoop::Init(const std::string file_name,
20 size_t max_loop_length_samples,
21 size_t block_length_samples) {
22 FILE* fp = fopen(file_name.c_str(), "rb");
Yves Gerey665174f2018-06-19 15:03:05 +020023 if (!fp)
24 return false;
henrik.lundin@webrtc.orgd1fc5d42013-09-17 08:38:02 +000025
Yves Gerey665174f2018-06-19 15:03:05 +020026 audio_array_.reset(
27 new int16_t[max_loop_length_samples + block_length_samples]);
28 size_t samples_read =
29 fread(audio_array_.get(), sizeof(int16_t), max_loop_length_samples, fp);
henrik.lundin@webrtc.orgd1fc5d42013-09-17 08:38:02 +000030 fclose(fp);
31
32 // Block length must be shorter than the loop length.
Yves Gerey665174f2018-06-19 15:03:05 +020033 if (block_length_samples > samples_read)
34 return false;
henrik.lundin@webrtc.orgd1fc5d42013-09-17 08:38:02 +000035
36 // Add an extra block length of samples to the end of the array, starting
37 // over again from the beginning of the array. This is done to simplify
38 // the reading process when reading over the end of the loop.
39 memcpy(&audio_array_[samples_read], audio_array_.get(),
40 block_length_samples * sizeof(int16_t));
41
42 loop_length_samples_ = samples_read;
43 block_length_samples_ = block_length_samples;
minyue58e08cb2016-02-24 03:49:19 -080044 next_index_ = 0;
henrik.lundin@webrtc.orgd1fc5d42013-09-17 08:38:02 +000045 return true;
46}
47
kwiberg288886b2015-11-06 01:21:35 -080048rtc::ArrayView<const int16_t> AudioLoop::GetNextBlock() {
henrik.lundin@webrtc.orgd1fc5d42013-09-17 08:38:02 +000049 // Check that the AudioLoop is initialized.
kwiberg288886b2015-11-06 01:21:35 -080050 if (block_length_samples_ == 0)
51 return rtc::ArrayView<const int16_t>();
henrik.lundin@webrtc.orgd1fc5d42013-09-17 08:38:02 +000052
53 const int16_t* output_ptr = &audio_array_[next_index_];
54 next_index_ = (next_index_ + block_length_samples_) % loop_length_samples_;
kwiberg288886b2015-11-06 01:21:35 -080055 return rtc::ArrayView<const int16_t>(output_ptr, block_length_samples_);
henrik.lundin@webrtc.orgd1fc5d42013-09-17 08:38:02 +000056}
57
henrik.lundin@webrtc.orgd1fc5d42013-09-17 08:38:02 +000058} // namespace test
59} // namespace webrtc