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