• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
11 #include "webrtc/audio_processing/debug.pb.h"
12 #include "webrtc/modules/audio_processing/common.h"
13 #include "webrtc/modules/audio_processing/include/audio_processing.h"
14 #include "webrtc/modules/interface/module_common_types.h"
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))
21 
22 // Exits on failure; do not use in unit tests.
OpenFile(const std::string & filename,const char * mode)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 
SamplesFromRate(int rate)32 static inline int SamplesFromRate(int rate) {
33   return AudioProcessing::kChunkSizeMs * rate / 1000;
34 }
35 
SetFrameSampleRate(AudioFrame * frame,int sample_rate_hz)36 static inline void SetFrameSampleRate(AudioFrame* frame,
37                                       int sample_rate_hz) {
38   frame->sample_rate_hz_ = sample_rate_hz;
39   frame->samples_per_channel_ = AudioProcessing::kChunkSizeMs *
40       sample_rate_hz / 1000;
41 }
42 
43 template <typename T>
SetContainerFormat(int sample_rate_hz,int num_channels,AudioFrame * frame,scoped_ptr<ChannelBuffer<T>> * cb)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 
LayoutFromChannels(int num_channels)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.
ReadMessageBytesFromFile(FILE * file,scoped_ptr<uint8_t[]> * bytes)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.
ReadMessageFromFile(FILE * file,::google::protobuf::MessageLite * msg)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
95