1 /*
2 * Copyright (c) 2015 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 "modules/audio_processing/test/test_utils.h"
12
13 #include <utility>
14
15 #include "rtc_base/checks.h"
16 #include "rtc_base/system/arch.h"
17
18 namespace webrtc {
19
RawFile(const std::string & filename)20 RawFile::RawFile(const std::string& filename)
21 : file_handle_(fopen(filename.c_str(), "wb")) {}
22
~RawFile()23 RawFile::~RawFile() {
24 fclose(file_handle_);
25 }
26
WriteSamples(const int16_t * samples,size_t num_samples)27 void RawFile::WriteSamples(const int16_t* samples, size_t num_samples) {
28 #ifndef WEBRTC_ARCH_LITTLE_ENDIAN
29 #error "Need to convert samples to little-endian when writing to PCM file"
30 #endif
31 fwrite(samples, sizeof(*samples), num_samples, file_handle_);
32 }
33
WriteSamples(const float * samples,size_t num_samples)34 void RawFile::WriteSamples(const float* samples, size_t num_samples) {
35 fwrite(samples, sizeof(*samples), num_samples, file_handle_);
36 }
37
ChannelBufferWavReader(std::unique_ptr<WavReader> file)38 ChannelBufferWavReader::ChannelBufferWavReader(std::unique_ptr<WavReader> file)
39 : file_(std::move(file)) {}
40
41 ChannelBufferWavReader::~ChannelBufferWavReader() = default;
42
Read(ChannelBuffer<float> * buffer)43 bool ChannelBufferWavReader::Read(ChannelBuffer<float>* buffer) {
44 RTC_CHECK_EQ(file_->num_channels(), buffer->num_channels());
45 interleaved_.resize(buffer->size());
46 if (file_->ReadSamples(interleaved_.size(), &interleaved_[0]) !=
47 interleaved_.size()) {
48 return false;
49 }
50
51 FloatS16ToFloat(&interleaved_[0], interleaved_.size(), &interleaved_[0]);
52 Deinterleave(&interleaved_[0], buffer->num_frames(), buffer->num_channels(),
53 buffer->channels());
54 return true;
55 }
56
ChannelBufferWavWriter(std::unique_ptr<WavWriter> file)57 ChannelBufferWavWriter::ChannelBufferWavWriter(std::unique_ptr<WavWriter> file)
58 : file_(std::move(file)) {}
59
60 ChannelBufferWavWriter::~ChannelBufferWavWriter() = default;
61
Write(const ChannelBuffer<float> & buffer)62 void ChannelBufferWavWriter::Write(const ChannelBuffer<float>& buffer) {
63 RTC_CHECK_EQ(file_->num_channels(), buffer.num_channels());
64 interleaved_.resize(buffer.size());
65 Interleave(buffer.channels(), buffer.num_frames(), buffer.num_channels(),
66 &interleaved_[0]);
67 FloatToFloatS16(&interleaved_[0], interleaved_.size(), &interleaved_[0]);
68 file_->WriteSamples(&interleaved_[0], interleaved_.size());
69 }
70
ChannelBufferVectorWriter(std::vector<float> * output)71 ChannelBufferVectorWriter::ChannelBufferVectorWriter(std::vector<float>* output)
72 : output_(output) {
73 RTC_DCHECK(output_);
74 }
75
76 ChannelBufferVectorWriter::~ChannelBufferVectorWriter() = default;
77
Write(const ChannelBuffer<float> & buffer)78 void ChannelBufferVectorWriter::Write(const ChannelBuffer<float>& buffer) {
79 // Account for sample rate changes throughout a simulation.
80 interleaved_buffer_.resize(buffer.size());
81 Interleave(buffer.channels(), buffer.num_frames(), buffer.num_channels(),
82 interleaved_buffer_.data());
83 size_t old_size = output_->size();
84 output_->resize(old_size + interleaved_buffer_.size());
85 FloatToFloatS16(interleaved_buffer_.data(), interleaved_buffer_.size(),
86 output_->data() + old_size);
87 }
88
WriteIntData(const int16_t * data,size_t length,WavWriter * wav_file,RawFile * raw_file)89 void WriteIntData(const int16_t* data,
90 size_t length,
91 WavWriter* wav_file,
92 RawFile* raw_file) {
93 if (wav_file) {
94 wav_file->WriteSamples(data, length);
95 }
96 if (raw_file) {
97 raw_file->WriteSamples(data, length);
98 }
99 }
100
WriteFloatData(const float * const * data,size_t samples_per_channel,size_t num_channels,WavWriter * wav_file,RawFile * raw_file)101 void WriteFloatData(const float* const* data,
102 size_t samples_per_channel,
103 size_t num_channels,
104 WavWriter* wav_file,
105 RawFile* raw_file) {
106 size_t length = num_channels * samples_per_channel;
107 std::unique_ptr<float[]> buffer(new float[length]);
108 Interleave(data, samples_per_channel, num_channels, buffer.get());
109 if (raw_file) {
110 raw_file->WriteSamples(buffer.get(), length);
111 }
112 // TODO(aluebs): Use ScaleToInt16Range() from audio_util
113 for (size_t i = 0; i < length; ++i) {
114 buffer[i] = buffer[i] > 0
115 ? buffer[i] * std::numeric_limits<int16_t>::max()
116 : -buffer[i] * std::numeric_limits<int16_t>::min();
117 }
118 if (wav_file) {
119 wav_file->WriteSamples(buffer.get(), length);
120 }
121 }
122
OpenFile(const std::string & filename,const char * mode)123 FILE* OpenFile(const std::string& filename, const char* mode) {
124 FILE* file = fopen(filename.c_str(), mode);
125 if (!file) {
126 printf("Unable to open file %s\n", filename.c_str());
127 exit(1);
128 }
129 return file;
130 }
131
SamplesFromRate(int rate)132 size_t SamplesFromRate(int rate) {
133 return static_cast<size_t>(AudioProcessing::kChunkSizeMs * rate / 1000);
134 }
135
SetFrameSampleRate(Int16FrameData * frame,int sample_rate_hz)136 void SetFrameSampleRate(Int16FrameData* frame, int sample_rate_hz) {
137 frame->sample_rate_hz = sample_rate_hz;
138 frame->samples_per_channel =
139 AudioProcessing::kChunkSizeMs * sample_rate_hz / 1000;
140 }
141
LayoutFromChannels(size_t num_channels)142 AudioProcessing::ChannelLayout LayoutFromChannels(size_t num_channels) {
143 switch (num_channels) {
144 case 1:
145 return AudioProcessing::kMono;
146 case 2:
147 return AudioProcessing::kStereo;
148 default:
149 RTC_CHECK(false);
150 return AudioProcessing::kMono;
151 }
152 }
153
154 } // namespace webrtc
155