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 | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 11 | #include "webrtc/audio_processing/debug.pb.h" |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 12 | #include "webrtc/modules/audio_processing/common.h" |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 13 | #include "webrtc/modules/audio_processing/include/audio_processing.h" |
| 14 | #include "webrtc/modules/interface/module_common_types.h" |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 15 | #include "webrtc/system_wrappers/interface/scoped_ptr.h" |
| 16 | |
| 17 | namespace webrtc { |
| 18 | |
| 19 | static const AudioProcessing::Error kNoErr = AudioProcessing::kNoError; |
| 20 | #define EXPECT_NOERR(expr) EXPECT_EQ(kNoErr, (expr)) |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 21 | |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 22 | // Exits on failure; do not use in unit tests. |
| 23 | static inline FILE* OpenFile(const std::string& filename, const char* mode) { |
| 24 | FILE* file = fopen(filename.c_str(), mode); |
| 25 | if (!file) { |
| 26 | printf("Unable to open file %s\n", filename.c_str()); |
| 27 | exit(1); |
| 28 | } |
| 29 | return file; |
| 30 | } |
| 31 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 32 | static inline int SamplesFromRate(int rate) { |
| 33 | return AudioProcessing::kChunkSizeMs * rate / 1000; |
| 34 | } |
| 35 | |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 36 | static inline void SetFrameSampleRate(AudioFrame* frame, |
| 37 | int sample_rate_hz) { |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 38 | frame->sample_rate_hz_ = sample_rate_hz; |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 39 | frame->samples_per_channel_ = AudioProcessing::kChunkSizeMs * |
| 40 | sample_rate_hz / 1000; |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 41 | } |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 42 | |
| 43 | template <typename T> |
| 44 | void SetContainerFormat(int sample_rate_hz, |
| 45 | int num_channels, |
| 46 | AudioFrame* frame, |
| 47 | scoped_ptr<ChannelBuffer<T> >* cb) { |
| 48 | SetFrameSampleRate(frame, sample_rate_hz); |
| 49 | frame->num_channels_ = num_channels; |
| 50 | cb->reset(new ChannelBuffer<T>(frame->samples_per_channel_, num_channels)); |
| 51 | } |
| 52 | |
| 53 | static inline AudioProcessing::ChannelLayout LayoutFromChannels( |
| 54 | int num_channels) { |
| 55 | switch (num_channels) { |
| 56 | case 1: |
| 57 | return AudioProcessing::kMono; |
| 58 | case 2: |
| 59 | return AudioProcessing::kStereo; |
| 60 | default: |
| 61 | assert(false); |
| 62 | return AudioProcessing::kMono; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | // Allocates new memory in the scoped_ptr to fit the raw message and returns the |
| 67 | // number of bytes read. |
| 68 | static inline size_t ReadMessageBytesFromFile(FILE* file, |
| 69 | scoped_ptr<uint8_t[]>* bytes) { |
| 70 | // The "wire format" for the size is little-endian. Assume we're running on |
| 71 | // a little-endian machine. |
| 72 | int32_t size = 0; |
| 73 | if (fread(&size, sizeof(size), 1, file) != 1) |
| 74 | return 0; |
| 75 | if (size <= 0) |
| 76 | return 0; |
| 77 | |
| 78 | bytes->reset(new uint8_t[size]); |
| 79 | return fread(bytes->get(), sizeof((*bytes)[0]), size, file); |
| 80 | } |
| 81 | |
| 82 | // Returns true on success, false on error or end-of-file. |
| 83 | static inline bool ReadMessageFromFile(FILE* file, |
| 84 | ::google::protobuf::MessageLite* msg) { |
| 85 | scoped_ptr<uint8_t[]> bytes; |
| 86 | size_t size = ReadMessageBytesFromFile(file, &bytes); |
| 87 | if (!size) |
| 88 | return false; |
| 89 | |
| 90 | msg->Clear(); |
| 91 | return msg->ParseFromArray(bytes.get(), size); |
| 92 | } |
| 93 | |
| 94 | } // namespace webrtc |