Home | History | Annotate | Download | only in test
      1 /*
      2  *  Copyright (c) 2015 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 <utility>
     12 
     13 #include "webrtc/base/checks.h"
     14 #include "webrtc/modules/audio_processing/test/test_utils.h"
     15 
     16 namespace webrtc {
     17 
     18 RawFile::RawFile(const std::string& filename)
     19     : file_handle_(fopen(filename.c_str(), "wb")) {}
     20 
     21 RawFile::~RawFile() {
     22   fclose(file_handle_);
     23 }
     24 
     25 void RawFile::WriteSamples(const int16_t* samples, size_t num_samples) {
     26 #ifndef WEBRTC_ARCH_LITTLE_ENDIAN
     27 #error "Need to convert samples to little-endian when writing to PCM file"
     28 #endif
     29   fwrite(samples, sizeof(*samples), num_samples, file_handle_);
     30 }
     31 
     32 void RawFile::WriteSamples(const float* samples, size_t num_samples) {
     33   fwrite(samples, sizeof(*samples), num_samples, file_handle_);
     34 }
     35 
     36 ChannelBufferWavReader::ChannelBufferWavReader(rtc::scoped_ptr<WavReader> file)
     37     : file_(std::move(file)) {}
     38 
     39 bool ChannelBufferWavReader::Read(ChannelBuffer<float>* buffer) {
     40   RTC_CHECK_EQ(file_->num_channels(), buffer->num_channels());
     41   interleaved_.resize(buffer->size());
     42   if (file_->ReadSamples(interleaved_.size(), &interleaved_[0]) !=
     43       interleaved_.size()) {
     44     return false;
     45   }
     46 
     47   FloatS16ToFloat(&interleaved_[0], interleaved_.size(), &interleaved_[0]);
     48   Deinterleave(&interleaved_[0], buffer->num_frames(), buffer->num_channels(),
     49                buffer->channels());
     50   return true;
     51 }
     52 
     53 ChannelBufferWavWriter::ChannelBufferWavWriter(rtc::scoped_ptr<WavWriter> file)
     54     : file_(std::move(file)) {}
     55 
     56 void ChannelBufferWavWriter::Write(const ChannelBuffer<float>& buffer) {
     57   RTC_CHECK_EQ(file_->num_channels(), buffer.num_channels());
     58   interleaved_.resize(buffer.size());
     59   Interleave(buffer.channels(), buffer.num_frames(), buffer.num_channels(),
     60              &interleaved_[0]);
     61   FloatToFloatS16(&interleaved_[0], interleaved_.size(), &interleaved_[0]);
     62   file_->WriteSamples(&interleaved_[0], interleaved_.size());
     63 }
     64 
     65 void WriteIntData(const int16_t* data,
     66                   size_t length,
     67                   WavWriter* wav_file,
     68                   RawFile* raw_file) {
     69   if (wav_file) {
     70     wav_file->WriteSamples(data, length);
     71   }
     72   if (raw_file) {
     73     raw_file->WriteSamples(data, length);
     74   }
     75 }
     76 
     77 void WriteFloatData(const float* const* data,
     78                     size_t samples_per_channel,
     79                     size_t num_channels,
     80                     WavWriter* wav_file,
     81                     RawFile* raw_file) {
     82   size_t length = num_channels * samples_per_channel;
     83   rtc::scoped_ptr<float[]> buffer(new float[length]);
     84   Interleave(data, samples_per_channel, num_channels, buffer.get());
     85   if (raw_file) {
     86     raw_file->WriteSamples(buffer.get(), length);
     87   }
     88   // TODO(aluebs): Use ScaleToInt16Range() from audio_util
     89   for (size_t i = 0; i < length; ++i) {
     90     buffer[i] = buffer[i] > 0 ?
     91                 buffer[i] * std::numeric_limits<int16_t>::max() :
     92                 -buffer[i] * std::numeric_limits<int16_t>::min();
     93   }
     94   if (wav_file) {
     95     wav_file->WriteSamples(buffer.get(), length);
     96   }
     97 }
     98 
     99 FILE* OpenFile(const std::string& filename, const char* mode) {
    100   FILE* file = fopen(filename.c_str(), mode);
    101   if (!file) {
    102     printf("Unable to open file %s\n", filename.c_str());
    103     exit(1);
    104   }
    105   return file;
    106 }
    107 
    108 size_t SamplesFromRate(int rate) {
    109   return static_cast<size_t>(AudioProcessing::kChunkSizeMs * rate / 1000);
    110 }
    111 
    112 void SetFrameSampleRate(AudioFrame* frame,
    113                         int sample_rate_hz) {
    114   frame->sample_rate_hz_ = sample_rate_hz;
    115   frame->samples_per_channel_ = AudioProcessing::kChunkSizeMs *
    116       sample_rate_hz / 1000;
    117 }
    118 
    119 AudioProcessing::ChannelLayout LayoutFromChannels(size_t num_channels) {
    120   switch (num_channels) {
    121     case 1:
    122       return AudioProcessing::kMono;
    123     case 2:
    124       return AudioProcessing::kStereo;
    125     default:
    126       RTC_CHECK(false);
    127       return AudioProcessing::kMono;
    128   }
    129 }
    130 
    131 std::vector<Point> ParseArrayGeometry(const std::string& mic_positions) {
    132   const std::vector<float> values = ParseList<float>(mic_positions);
    133   const size_t num_mics =
    134       rtc::CheckedDivExact(values.size(), static_cast<size_t>(3));
    135   RTC_CHECK_GT(num_mics, 0u) << "mic_positions is not large enough.";
    136 
    137   std::vector<Point> result;
    138   result.reserve(num_mics);
    139   for (size_t i = 0; i < values.size(); i += 3) {
    140     result.push_back(Point(values[i + 0], values[i + 1], values[i + 2]));
    141   }
    142 
    143   return result;
    144 }
    145 
    146 std::vector<Point> ParseArrayGeometry(const std::string& mic_positions,
    147                                       size_t num_mics) {
    148   std::vector<Point> result = ParseArrayGeometry(mic_positions);
    149   RTC_CHECK_EQ(result.size(), num_mics)
    150       << "Could not parse mic_positions or incorrect number of points.";
    151   return result;
    152 }
    153 
    154 }  // namespace webrtc
    155