1d1fc5d4e179d162cf9453f23451793c71cab1609henrik.lundin@webrtc.org/* 2d1fc5d4e179d162cf9453f23451793c71cab1609henrik.lundin@webrtc.org * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. 3d1fc5d4e179d162cf9453f23451793c71cab1609henrik.lundin@webrtc.org * 4d1fc5d4e179d162cf9453f23451793c71cab1609henrik.lundin@webrtc.org * Use of this source code is governed by a BSD-style license 5d1fc5d4e179d162cf9453f23451793c71cab1609henrik.lundin@webrtc.org * that can be found in the LICENSE file in the root of the source 6d1fc5d4e179d162cf9453f23451793c71cab1609henrik.lundin@webrtc.org * tree. An additional intellectual property rights grant can be found 7d1fc5d4e179d162cf9453f23451793c71cab1609henrik.lundin@webrtc.org * in the file PATENTS. All contributing project authors may 8d1fc5d4e179d162cf9453f23451793c71cab1609henrik.lundin@webrtc.org * be found in the AUTHORS file in the root of the source tree. 9d1fc5d4e179d162cf9453f23451793c71cab1609henrik.lundin@webrtc.org */ 10d1fc5d4e179d162cf9453f23451793c71cab1609henrik.lundin@webrtc.org 119c55f0f957534144d2b8a64154f0a479249b34behenrik.lundin@webrtc.org#include "webrtc/modules/audio_coding/neteq/tools/audio_loop.h" 12d1fc5d4e179d162cf9453f23451793c71cab1609henrik.lundin@webrtc.org 13d1fc5d4e179d162cf9453f23451793c71cab1609henrik.lundin@webrtc.org#include <assert.h> 14d1fc5d4e179d162cf9453f23451793c71cab1609henrik.lundin@webrtc.org#include <stdio.h> 15d1fc5d4e179d162cf9453f23451793c71cab1609henrik.lundin@webrtc.org#include <string.h> 16d1fc5d4e179d162cf9453f23451793c71cab1609henrik.lundin@webrtc.org 17d1fc5d4e179d162cf9453f23451793c71cab1609henrik.lundin@webrtc.orgnamespace webrtc { 18d1fc5d4e179d162cf9453f23451793c71cab1609henrik.lundin@webrtc.orgnamespace test { 19d1fc5d4e179d162cf9453f23451793c71cab1609henrik.lundin@webrtc.org 20d1fc5d4e179d162cf9453f23451793c71cab1609henrik.lundin@webrtc.orgbool AudioLoop::Init(const std::string file_name, 21d1fc5d4e179d162cf9453f23451793c71cab1609henrik.lundin@webrtc.org size_t max_loop_length_samples, 22d1fc5d4e179d162cf9453f23451793c71cab1609henrik.lundin@webrtc.org size_t block_length_samples) { 23d1fc5d4e179d162cf9453f23451793c71cab1609henrik.lundin@webrtc.org FILE* fp = fopen(file_name.c_str(), "rb"); 24d1fc5d4e179d162cf9453f23451793c71cab1609henrik.lundin@webrtc.org if (!fp) return false; 25d1fc5d4e179d162cf9453f23451793c71cab1609henrik.lundin@webrtc.org 26d1fc5d4e179d162cf9453f23451793c71cab1609henrik.lundin@webrtc.org audio_array_.reset(new int16_t[max_loop_length_samples + 27d1fc5d4e179d162cf9453f23451793c71cab1609henrik.lundin@webrtc.org block_length_samples]); 28d1fc5d4e179d162cf9453f23451793c71cab1609henrik.lundin@webrtc.org size_t samples_read = fread(audio_array_.get(), sizeof(int16_t), 29d1fc5d4e179d162cf9453f23451793c71cab1609henrik.lundin@webrtc.org max_loop_length_samples, fp); 30d1fc5d4e179d162cf9453f23451793c71cab1609henrik.lundin@webrtc.org fclose(fp); 31d1fc5d4e179d162cf9453f23451793c71cab1609henrik.lundin@webrtc.org 32d1fc5d4e179d162cf9453f23451793c71cab1609henrik.lundin@webrtc.org // Block length must be shorter than the loop length. 33d1fc5d4e179d162cf9453f23451793c71cab1609henrik.lundin@webrtc.org if (block_length_samples > samples_read) return false; 34d1fc5d4e179d162cf9453f23451793c71cab1609henrik.lundin@webrtc.org 35d1fc5d4e179d162cf9453f23451793c71cab1609henrik.lundin@webrtc.org // Add an extra block length of samples to the end of the array, starting 36d1fc5d4e179d162cf9453f23451793c71cab1609henrik.lundin@webrtc.org // over again from the beginning of the array. This is done to simplify 37d1fc5d4e179d162cf9453f23451793c71cab1609henrik.lundin@webrtc.org // the reading process when reading over the end of the loop. 38d1fc5d4e179d162cf9453f23451793c71cab1609henrik.lundin@webrtc.org memcpy(&audio_array_[samples_read], audio_array_.get(), 39d1fc5d4e179d162cf9453f23451793c71cab1609henrik.lundin@webrtc.org block_length_samples * sizeof(int16_t)); 40d1fc5d4e179d162cf9453f23451793c71cab1609henrik.lundin@webrtc.org 41d1fc5d4e179d162cf9453f23451793c71cab1609henrik.lundin@webrtc.org loop_length_samples_ = samples_read; 42d1fc5d4e179d162cf9453f23451793c71cab1609henrik.lundin@webrtc.org block_length_samples_ = block_length_samples; 43d1fc5d4e179d162cf9453f23451793c71cab1609henrik.lundin@webrtc.org return true; 44d1fc5d4e179d162cf9453f23451793c71cab1609henrik.lundin@webrtc.org} 45d1fc5d4e179d162cf9453f23451793c71cab1609henrik.lundin@webrtc.org 46288886b2ec9a2dac730f115e9c3079d8439efe60kwibergrtc::ArrayView<const int16_t> AudioLoop::GetNextBlock() { 47d1fc5d4e179d162cf9453f23451793c71cab1609henrik.lundin@webrtc.org // Check that the AudioLoop is initialized. 48288886b2ec9a2dac730f115e9c3079d8439efe60kwiberg if (block_length_samples_ == 0) 49288886b2ec9a2dac730f115e9c3079d8439efe60kwiberg return rtc::ArrayView<const int16_t>(); 50d1fc5d4e179d162cf9453f23451793c71cab1609henrik.lundin@webrtc.org 51d1fc5d4e179d162cf9453f23451793c71cab1609henrik.lundin@webrtc.org const int16_t* output_ptr = &audio_array_[next_index_]; 52d1fc5d4e179d162cf9453f23451793c71cab1609henrik.lundin@webrtc.org next_index_ = (next_index_ + block_length_samples_) % loop_length_samples_; 53288886b2ec9a2dac730f115e9c3079d8439efe60kwiberg return rtc::ArrayView<const int16_t>(output_ptr, block_length_samples_); 54d1fc5d4e179d162cf9453f23451793c71cab1609henrik.lundin@webrtc.org} 55d1fc5d4e179d162cf9453f23451793c71cab1609henrik.lundin@webrtc.org 56d1fc5d4e179d162cf9453f23451793c71cab1609henrik.lundin@webrtc.org 57d1fc5d4e179d162cf9453f23451793c71cab1609henrik.lundin@webrtc.org} // namespace test 58d1fc5d4e179d162cf9453f23451793c71cab1609henrik.lundin@webrtc.org} // namespace webrtc 59