blob: e5204da5a09dc135bab17a3f1621e06eabd6d3db [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.orga8b97372014-03-10 22:26:12 +000011#include "webrtc/audio_processing/debug.pb.h"
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000012#include "webrtc/modules/audio_processing/common.h"
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000013#include "webrtc/modules/audio_processing/include/audio_processing.h"
14#include "webrtc/modules/interface/module_common_types.h"
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000015#include "webrtc/system_wrappers/interface/scoped_ptr.h"
16
17namespace webrtc {
18
19static const AudioProcessing::Error kNoErr = AudioProcessing::kNoError;
20#define EXPECT_NOERR(expr) EXPECT_EQ(kNoErr, (expr))
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000021
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000022// Exits on failure; do not use in unit tests.
23static 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.orgddbb8a22014-04-22 21:00:04 +000032static inline int SamplesFromRate(int rate) {
33 return AudioProcessing::kChunkSizeMs * rate / 1000;
34}
35
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000036static inline void SetFrameSampleRate(AudioFrame* frame,
37 int sample_rate_hz) {
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000038 frame->sample_rate_hz_ = sample_rate_hz;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000039 frame->samples_per_channel_ = AudioProcessing::kChunkSizeMs *
40 sample_rate_hz / 1000;
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000041}
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000042
43template <typename T>
44void 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
53static 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.
68static 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.
83static 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