blob: 1b9ac3ce4cb8b40c3bf852c9579386618c058005 [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
34void WriteIntData(const int16_t* data,
35 size_t length,
36 WavWriter* wav_file,
37 RawFile* raw_file) {
38 if (wav_file) {
39 wav_file->WriteSamples(data, length);
40 }
41 if (raw_file) {
42 raw_file->WriteSamples(data, length);
43 }
44}
45
46void WriteFloatData(const float* const* data,
47 int samples_per_channel,
48 int num_channels,
49 WavWriter* wav_file,
50 RawFile* raw_file) {
51 size_t length = num_channels * samples_per_channel;
52 rtc::scoped_ptr<float[]> buffer(new float[length]);
53 Interleave(data, samples_per_channel, num_channels, buffer.get());
54 if (raw_file) {
55 raw_file->WriteSamples(buffer.get(), length);
56 }
57 // TODO(aluebs): Use ScaleToInt16Range() from audio_util
58 for (size_t i = 0; i < length; ++i) {
59 buffer[i] = buffer[i] > 0 ?
60 buffer[i] * std::numeric_limits<int16_t>::max() :
61 -buffer[i] * std::numeric_limits<int16_t>::min();
62 }
63 if (wav_file) {
64 wav_file->WriteSamples(buffer.get(), length);
65 }
66}
67
68FILE* OpenFile(const std::string& filename, const char* mode) {
69 FILE* file = fopen(filename.c_str(), mode);
70 if (!file) {
71 printf("Unable to open file %s\n", filename.c_str());
72 exit(1);
73 }
74 return file;
75}
76
77int SamplesFromRate(int rate) {
78 return AudioProcessing::kChunkSizeMs * rate / 1000;
79}
80
81void SetFrameSampleRate(AudioFrame* frame,
82 int sample_rate_hz) {
83 frame->sample_rate_hz_ = sample_rate_hz;
84 frame->samples_per_channel_ = AudioProcessing::kChunkSizeMs *
85 sample_rate_hz / 1000;
86}
87
88AudioProcessing::ChannelLayout LayoutFromChannels(int num_channels) {
89 switch (num_channels) {
90 case 1:
91 return AudioProcessing::kMono;
92 case 2:
93 return AudioProcessing::kStereo;
94 default:
kjellanderb7a5c162015-11-05 12:33:18 -080095 assert(false);
Andrew MacDonaldcb05b722015-05-07 22:17:51 -070096 return AudioProcessing::kMono;
97 }
98}
99
kjellanderb7a5c162015-11-05 12:33:18 -0800100std::vector<Point> ParseArrayGeometry(const std::string& mic_positions,
101 size_t num_mics) {
Andrew MacDonaldcb05b722015-05-07 22:17:51 -0700102 const std::vector<float> values = ParseList<float>(mic_positions);
kjellanderb7a5c162015-11-05 12:33:18 -0800103 RTC_CHECK_EQ(values.size(), 3 * num_mics)
104 << "Could not parse mic_positions or incorrect number of points.";
Andrew MacDonaldcb05b722015-05-07 22:17:51 -0700105
106 std::vector<Point> result;
107 result.reserve(num_mics);
108 for (size_t i = 0; i < values.size(); i += 3) {
kjellanderb7a5c162015-11-05 12:33:18 -0800109 double x = values[i + 0];
110 double y = values[i + 1];
111 double z = values[i + 2];
112 result.push_back(Point(x, y, z));
Andrew MacDonaldcb05b722015-05-07 22:17:51 -0700113 }
114
115 return result;
116}
117
Andrew MacDonaldcb05b722015-05-07 22:17:51 -0700118
119} // namespace webrtc