henrik.lundin@webrtc.org | d1fc5d4 | 2013-09-17 08:38:02 +0000 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/audio_coding/neteq/tools/audio_loop.h" |
henrik.lundin@webrtc.org | d1fc5d4 | 2013-09-17 08:38:02 +0000 | [diff] [blame] | 12 | |
| 13 | #include <assert.h> |
| 14 | #include <stdio.h> |
| 15 | #include <string.h> |
| 16 | |
| 17 | namespace webrtc { |
| 18 | namespace test { |
| 19 | |
| 20 | bool AudioLoop::Init(const std::string file_name, |
| 21 | size_t max_loop_length_samples, |
| 22 | size_t block_length_samples) { |
| 23 | FILE* fp = fopen(file_name.c_str(), "rb"); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 24 | if (!fp) |
| 25 | return false; |
henrik.lundin@webrtc.org | d1fc5d4 | 2013-09-17 08:38:02 +0000 | [diff] [blame] | 26 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 27 | audio_array_.reset( |
| 28 | new int16_t[max_loop_length_samples + block_length_samples]); |
| 29 | size_t samples_read = |
| 30 | fread(audio_array_.get(), sizeof(int16_t), max_loop_length_samples, fp); |
henrik.lundin@webrtc.org | d1fc5d4 | 2013-09-17 08:38:02 +0000 | [diff] [blame] | 31 | fclose(fp); |
| 32 | |
| 33 | // Block length must be shorter than the loop length. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 34 | if (block_length_samples > samples_read) |
| 35 | return false; |
henrik.lundin@webrtc.org | d1fc5d4 | 2013-09-17 08:38:02 +0000 | [diff] [blame] | 36 | |
| 37 | // Add an extra block length of samples to the end of the array, starting |
| 38 | // over again from the beginning of the array. This is done to simplify |
| 39 | // the reading process when reading over the end of the loop. |
| 40 | memcpy(&audio_array_[samples_read], audio_array_.get(), |
| 41 | block_length_samples * sizeof(int16_t)); |
| 42 | |
| 43 | loop_length_samples_ = samples_read; |
| 44 | block_length_samples_ = block_length_samples; |
minyue | 58e08cb | 2016-02-24 03:49:19 -0800 | [diff] [blame] | 45 | next_index_ = 0; |
henrik.lundin@webrtc.org | d1fc5d4 | 2013-09-17 08:38:02 +0000 | [diff] [blame] | 46 | return true; |
| 47 | } |
| 48 | |
kwiberg | 288886b | 2015-11-06 01:21:35 -0800 | [diff] [blame] | 49 | rtc::ArrayView<const int16_t> AudioLoop::GetNextBlock() { |
henrik.lundin@webrtc.org | d1fc5d4 | 2013-09-17 08:38:02 +0000 | [diff] [blame] | 50 | // Check that the AudioLoop is initialized. |
kwiberg | 288886b | 2015-11-06 01:21:35 -0800 | [diff] [blame] | 51 | if (block_length_samples_ == 0) |
| 52 | return rtc::ArrayView<const int16_t>(); |
henrik.lundin@webrtc.org | d1fc5d4 | 2013-09-17 08:38:02 +0000 | [diff] [blame] | 53 | |
| 54 | const int16_t* output_ptr = &audio_array_[next_index_]; |
| 55 | next_index_ = (next_index_ + block_length_samples_) % loop_length_samples_; |
kwiberg | 288886b | 2015-11-06 01:21:35 -0800 | [diff] [blame] | 56 | return rtc::ArrayView<const int16_t>(output_ptr, block_length_samples_); |
henrik.lundin@webrtc.org | d1fc5d4 | 2013-09-17 08:38:02 +0000 | [diff] [blame] | 57 | } |
| 58 | |
henrik.lundin@webrtc.org | d1fc5d4 | 2013-09-17 08:38:02 +0000 | [diff] [blame] | 59 | } // namespace test |
| 60 | } // namespace webrtc |