audio_loop.cc (2093B)
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 11 #include "modules/audio_coding/neteq/tools/audio_loop.h" 12 13 #include <cstdint> 14 #include <cstdio> 15 #include <cstring> 16 #include <string> 17 18 #include "absl/strings/string_view.h" 19 #include "api/array_view.h" 20 21 namespace webrtc { 22 namespace test { 23 24 bool AudioLoop::Init(absl::string_view file_name, 25 size_t max_loop_length_samples, 26 size_t block_length_samples) { 27 FILE* fp = fopen(std::string(file_name).c_str(), "rb"); 28 if (!fp) 29 return false; 30 31 audio_array_.reset( 32 new int16_t[max_loop_length_samples + block_length_samples]); 33 size_t samples_read = 34 fread(audio_array_.get(), sizeof(int16_t), max_loop_length_samples, fp); 35 fclose(fp); 36 37 // Block length must be shorter than the loop length. 38 if (block_length_samples > samples_read) 39 return false; 40 41 // Add an extra block length of samples to the end of the array, starting 42 // over again from the beginning of the array. This is done to simplify 43 // the reading process when reading over the end of the loop. 44 memcpy(&audio_array_[samples_read], audio_array_.get(), 45 block_length_samples * sizeof(int16_t)); 46 47 loop_length_samples_ = samples_read; 48 block_length_samples_ = block_length_samples; 49 next_index_ = 0; 50 return true; 51 } 52 53 ArrayView<const int16_t> AudioLoop::GetNextBlock() { 54 // Check that the AudioLoop is initialized. 55 if (block_length_samples_ == 0) 56 return ArrayView<const int16_t>(); 57 58 const int16_t* output_ptr = &audio_array_[next_index_]; 59 next_index_ = (next_index_ + block_length_samples_) % loop_length_samples_; 60 return ArrayView<const int16_t>(output_ptr, block_length_samples_); 61 } 62 63 } // namespace test 64 } // namespace webrtc