2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
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.
13 #include "webrtc/audio_processing/debug.pb.h"
14 #include "webrtc/common_audio/include/audio_util.h"
15 #include "webrtc/common_audio/wav_writer.h"
16 #include "webrtc/modules/audio_processing/common.h"
17 #include "webrtc/modules/audio_processing/include/audio_processing.h"
18 #include "webrtc/modules/interface/module_common_types.h"
19 #include "webrtc/system_wrappers/interface/scoped_ptr.h"
23 static const AudioProcessing::Error kNoErr = AudioProcessing::kNoError;
24 #define EXPECT_NOERR(expr) EXPECT_EQ(kNoErr, (expr))
28 RawFile(const std::string& filename)
29 : file_handle_(fopen(filename.c_str(), "wb")) {}
35 void WriteSamples(const int16_t* samples, size_t num_samples) {
36 #ifndef WEBRTC_ARCH_LITTLE_ENDIAN
37 #error "Need to convert samples to little-endian when writing to PCM file"
39 fwrite(samples, sizeof(*samples), num_samples, file_handle_);
42 void WriteSamples(const float* samples, size_t num_samples) {
43 fwrite(samples, sizeof(*samples), num_samples, file_handle_);
50 static inline void WriteIntData(const int16_t* data,
55 wav_file->WriteSamples(data, length);
58 raw_file->WriteSamples(data, length);
62 static inline void WriteFloatData(const float* const* data,
63 size_t samples_per_channel,
67 size_t length = num_channels * samples_per_channel;
68 scoped_ptr<float[]> buffer(new float[length]);
69 Interleave(data, samples_per_channel, num_channels, buffer.get());
71 raw_file->WriteSamples(buffer.get(), length);
73 // TODO(aluebs): Use ScaleToInt16Range() from audio_util
74 for (size_t i = 0; i < length; ++i) {
75 buffer[i] = buffer[i] > 0 ?
76 buffer[i] * std::numeric_limits<int16_t>::max() :
77 -buffer[i] * std::numeric_limits<int16_t>::min();
80 wav_file->WriteSamples(buffer.get(), length);
84 // Exits on failure; do not use in unit tests.
85 static inline FILE* OpenFile(const std::string& filename, const char* mode) {
86 FILE* file = fopen(filename.c_str(), mode);
88 printf("Unable to open file %s\n", filename.c_str());
94 static inline int SamplesFromRate(int rate) {
95 return AudioProcessing::kChunkSizeMs * rate / 1000;
98 static inline void SetFrameSampleRate(AudioFrame* frame,
100 frame->sample_rate_hz_ = sample_rate_hz;
101 frame->samples_per_channel_ = AudioProcessing::kChunkSizeMs *
102 sample_rate_hz / 1000;
105 template <typename T>
106 void SetContainerFormat(int sample_rate_hz,
109 scoped_ptr<ChannelBuffer<T> >* cb) {
110 SetFrameSampleRate(frame, sample_rate_hz);
111 frame->num_channels_ = num_channels;
112 cb->reset(new ChannelBuffer<T>(frame->samples_per_channel_, num_channels));
115 static inline AudioProcessing::ChannelLayout LayoutFromChannels(
117 switch (num_channels) {
119 return AudioProcessing::kMono;
121 return AudioProcessing::kStereo;
124 return AudioProcessing::kMono;
128 // Allocates new memory in the scoped_ptr to fit the raw message and returns the
129 // number of bytes read.
130 static inline size_t ReadMessageBytesFromFile(FILE* file,
131 scoped_ptr<uint8_t[]>* bytes) {
132 // The "wire format" for the size is little-endian. Assume we're running on
133 // a little-endian machine.
135 if (fread(&size, sizeof(size), 1, file) != 1)
140 bytes->reset(new uint8_t[size]);
141 return fread(bytes->get(), sizeof((*bytes)[0]), size, file);
144 // Returns true on success, false on error or end-of-file.
145 static inline bool ReadMessageFromFile(FILE* file,
146 ::google::protobuf::MessageLite* msg) {
147 scoped_ptr<uint8_t[]> bytes;
148 size_t size = ReadMessageBytesFromFile(file, &bytes);
153 return msg->ParseFromArray(bytes.get(), size);
156 } // namespace webrtc