andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 1 | /* |
| 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.org | 8328e7c | 2014-10-31 04:58:14 +0000 | [diff] [blame] | 11 | #include <math.h> |
aluebs@webrtc.org | 021e76f | 2014-09-04 18:12:00 +0000 | [diff] [blame] | 12 | #include <limits> |
| 13 | |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 14 | #include "webrtc/audio_processing/debug.pb.h" |
aluebs@webrtc.org | 021e76f | 2014-09-04 18:12:00 +0000 | [diff] [blame] | 15 | #include "webrtc/common_audio/include/audio_util.h" |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 16 | #include "webrtc/common_audio/wav_file.h" |
aluebs@webrtc.org | 8789376 | 2014-11-27 23:40:25 +0000 | [diff] [blame] | 17 | #include "webrtc/modules/audio_processing/channel_buffer.h" |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 18 | #include "webrtc/modules/audio_processing/include/audio_processing.h" |
| 19 | #include "webrtc/modules/interface/module_common_types.h" |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 20 | #include "webrtc/system_wrappers/interface/scoped_ptr.h" |
| 21 | |
| 22 | namespace webrtc { |
| 23 | |
| 24 | static const AudioProcessing::Error kNoErr = AudioProcessing::kNoError; |
| 25 | #define EXPECT_NOERR(expr) EXPECT_EQ(kNoErr, (expr)) |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 26 | |
aluebs@webrtc.org | 021e76f | 2014-09-04 18:12:00 +0000 | [diff] [blame] | 27 | class 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 | |
| 51 | static inline void WriteIntData(const int16_t* data, |
| 52 | size_t length, |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 53 | WavWriter* wav_file, |
aluebs@webrtc.org | 021e76f | 2014-09-04 18:12:00 +0000 | [diff] [blame] | 54 | 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 | |
| 63 | static inline void WriteFloatData(const float* const* data, |
| 64 | size_t samples_per_channel, |
| 65 | int num_channels, |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 66 | WavWriter* wav_file, |
aluebs@webrtc.org | 021e76f | 2014-09-04 18:12:00 +0000 | [diff] [blame] | 67 | 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.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 85 | // Exits on failure; do not use in unit tests. |
| 86 | static 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.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 95 | static inline int SamplesFromRate(int rate) { |
| 96 | return AudioProcessing::kChunkSizeMs * rate / 1000; |
| 97 | } |
| 98 | |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 99 | static inline void SetFrameSampleRate(AudioFrame* frame, |
| 100 | int sample_rate_hz) { |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 101 | frame->sample_rate_hz_ = sample_rate_hz; |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 102 | frame->samples_per_channel_ = AudioProcessing::kChunkSizeMs * |
| 103 | sample_rate_hz / 1000; |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 104 | } |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 105 | |
| 106 | template <typename T> |
| 107 | void 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 | |
| 116 | static 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. |
| 131 | static 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. |
| 146 | static 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.org | 8328e7c | 2014-10-31 04:58:14 +0000 | [diff] [blame] | 157 | template <typename T> |
| 158 | float 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.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 179 | } // namespace webrtc |