blob: 0bf6f27d61b9824714cc0d696e8cbe04a8c600b9 [file] [log] [blame]
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00001/*
2 * Copyright (c) 2014 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
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +000011#include <math.h>
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +000012#include <limits>
13
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000014#include "webrtc/audio_processing/debug.pb.h"
kjellander@webrtc.org035e9122015-01-28 19:57:00 +000015#include "webrtc/common_audio/channel_buffer.h"
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +000016#include "webrtc/common_audio/include/audio_util.h"
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000017#include "webrtc/common_audio/wav_file.h"
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000018#include "webrtc/modules/audio_processing/include/audio_processing.h"
19#include "webrtc/modules/interface/module_common_types.h"
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000020#include "webrtc/system_wrappers/interface/scoped_ptr.h"
21
22namespace webrtc {
23
24static const AudioProcessing::Error kNoErr = AudioProcessing::kNoError;
25#define EXPECT_NOERR(expr) EXPECT_EQ(kNoErr, (expr))
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000026
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +000027class RawFile {
28 public:
29 RawFile(const std::string& filename)
30 : file_handle_(fopen(filename.c_str(), "wb")) {}
31
32 ~RawFile() {
33 fclose(file_handle_);
34 }
35
36 void WriteSamples(const int16_t* samples, size_t num_samples) {
37#ifndef WEBRTC_ARCH_LITTLE_ENDIAN
38#error "Need to convert samples to little-endian when writing to PCM file"
39#endif
40 fwrite(samples, sizeof(*samples), num_samples, file_handle_);
41 }
42
43 void WriteSamples(const float* samples, size_t num_samples) {
44 fwrite(samples, sizeof(*samples), num_samples, file_handle_);
45 }
46
47 private:
48 FILE* file_handle_;
49};
50
51static inline void WriteIntData(const int16_t* data,
52 size_t length,
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000053 WavWriter* wav_file,
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +000054 RawFile* raw_file) {
55 if (wav_file) {
56 wav_file->WriteSamples(data, length);
57 }
58 if (raw_file) {
59 raw_file->WriteSamples(data, length);
60 }
61}
62
63static inline void WriteFloatData(const float* const* data,
64 size_t samples_per_channel,
65 int num_channels,
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000066 WavWriter* wav_file,
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +000067 RawFile* raw_file) {
68 size_t length = num_channels * samples_per_channel;
69 scoped_ptr<float[]> buffer(new float[length]);
70 Interleave(data, samples_per_channel, num_channels, buffer.get());
71 if (raw_file) {
72 raw_file->WriteSamples(buffer.get(), length);
73 }
74 // TODO(aluebs): Use ScaleToInt16Range() from audio_util
75 for (size_t i = 0; i < length; ++i) {
76 buffer[i] = buffer[i] > 0 ?
77 buffer[i] * std::numeric_limits<int16_t>::max() :
78 -buffer[i] * std::numeric_limits<int16_t>::min();
79 }
80 if (wav_file) {
81 wav_file->WriteSamples(buffer.get(), length);
82 }
83}
84
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000085// Exits on failure; do not use in unit tests.
86static inline FILE* OpenFile(const std::string& filename, const char* mode) {
87 FILE* file = fopen(filename.c_str(), mode);
88 if (!file) {
89 printf("Unable to open file %s\n", filename.c_str());
90 exit(1);
91 }
92 return file;
93}
94
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000095static inline int SamplesFromRate(int rate) {
96 return AudioProcessing::kChunkSizeMs * rate / 1000;
97}
98
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000099static inline void SetFrameSampleRate(AudioFrame* frame,
100 int sample_rate_hz) {
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000101 frame->sample_rate_hz_ = sample_rate_hz;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000102 frame->samples_per_channel_ = AudioProcessing::kChunkSizeMs *
103 sample_rate_hz / 1000;
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000104}
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000105
106template <typename T>
107void SetContainerFormat(int sample_rate_hz,
108 int num_channels,
109 AudioFrame* frame,
110 scoped_ptr<ChannelBuffer<T> >* cb) {
111 SetFrameSampleRate(frame, sample_rate_hz);
112 frame->num_channels_ = num_channels;
113 cb->reset(new ChannelBuffer<T>(frame->samples_per_channel_, num_channels));
114}
115
116static inline AudioProcessing::ChannelLayout LayoutFromChannels(
117 int num_channels) {
118 switch (num_channels) {
119 case 1:
120 return AudioProcessing::kMono;
121 case 2:
122 return AudioProcessing::kStereo;
123 default:
124 assert(false);
125 return AudioProcessing::kMono;
126 }
127}
128
129// Allocates new memory in the scoped_ptr to fit the raw message and returns the
130// number of bytes read.
131static inline size_t ReadMessageBytesFromFile(FILE* file,
132 scoped_ptr<uint8_t[]>* bytes) {
133 // The "wire format" for the size is little-endian. Assume we're running on
134 // a little-endian machine.
135 int32_t size = 0;
136 if (fread(&size, sizeof(size), 1, file) != 1)
137 return 0;
138 if (size <= 0)
139 return 0;
140
141 bytes->reset(new uint8_t[size]);
142 return fread(bytes->get(), sizeof((*bytes)[0]), size, file);
143}
144
145// Returns true on success, false on error or end-of-file.
146static inline bool ReadMessageFromFile(FILE* file,
147 ::google::protobuf::MessageLite* msg) {
148 scoped_ptr<uint8_t[]> bytes;
149 size_t size = ReadMessageBytesFromFile(file, &bytes);
150 if (!size)
151 return false;
152
153 msg->Clear();
154 return msg->ParseFromArray(bytes.get(), size);
155}
156
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +0000157template <typename T>
158float ComputeSNR(const T* ref, const T* test, int length, float* variance) {
159 float mse = 0;
160 float mean = 0;
161 *variance = 0;
162 for (int i = 0; i < length; ++i) {
163 T error = ref[i] - test[i];
164 mse += error * error;
165 *variance += ref[i] * ref[i];
166 mean += ref[i];
167 }
168 mse /= length;
169 *variance /= length;
170 mean /= length;
171 *variance -= mean * mean;
172
173 float snr = 100; // We assign 100 dB to the zero-error case.
174 if (mse > 0)
175 snr = 10 * log10(*variance / mse);
176 return snr;
177}
178
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000179} // namespace webrtc