Home | History | Annotate | Download | only in tools
      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 "webrtc/modules/audio_coding/neteq/tools/audio_loop.h"
     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");
     24   if (!fp) return false;
     25 
     26   audio_array_.reset(new int16_t[max_loop_length_samples +
     27                                  block_length_samples]);
     28   size_t samples_read = fread(audio_array_.get(), sizeof(int16_t),
     29                               max_loop_length_samples, fp);
     30   fclose(fp);
     31 
     32   // Block length must be shorter than the loop length.
     33   if (block_length_samples > samples_read) return false;
     34 
     35   // Add an extra block length of samples to the end of the array, starting
     36   // over again from the beginning of the array. This is done to simplify
     37   // the reading process when reading over the end of the loop.
     38   memcpy(&audio_array_[samples_read], audio_array_.get(),
     39          block_length_samples * sizeof(int16_t));
     40 
     41   loop_length_samples_ = samples_read;
     42   block_length_samples_ = block_length_samples;
     43   return true;
     44 }
     45 
     46 const int16_t* AudioLoop::GetNextBlock() {
     47   // Check that the AudioLoop is initialized.
     48   if (block_length_samples_ == 0) return NULL;
     49 
     50   const int16_t* output_ptr = &audio_array_[next_index_];
     51   next_index_ = (next_index_ + block_length_samples_) % loop_length_samples_;
     52   return output_ptr;
     53 }
     54 
     55 
     56 }  // namespace test
     57 }  // namespace webrtc
     58