2015-05-07 22:17:51 -07:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
|
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
*/
|
|
|
|
|
|
2019-07-05 19:08:33 +02:00
|
|
|
#include "modules/audio_processing/test/test_utils.h"
|
|
|
|
|
|
2015-12-17 03:04:15 -08:00
|
|
|
#include <utility>
|
|
|
|
|
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "rtc_base/checks.h"
|
2018-07-25 16:05:48 +02:00
|
|
|
#include "rtc_base/system/arch.h"
|
2015-05-07 22:17:51 -07:00
|
|
|
|
|
|
|
|
namespace webrtc {
|
|
|
|
|
|
|
|
|
|
RawFile::RawFile(const std::string& filename)
|
|
|
|
|
: file_handle_(fopen(filename.c_str(), "wb")) {}
|
|
|
|
|
|
|
|
|
|
RawFile::~RawFile() {
|
|
|
|
|
fclose(file_handle_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RawFile::WriteSamples(const int16_t* samples, size_t num_samples) {
|
|
|
|
|
#ifndef WEBRTC_ARCH_LITTLE_ENDIAN
|
|
|
|
|
#error "Need to convert samples to little-endian when writing to PCM file"
|
|
|
|
|
#endif
|
|
|
|
|
fwrite(samples, sizeof(*samples), num_samples, file_handle_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RawFile::WriteSamples(const float* samples, size_t num_samples) {
|
|
|
|
|
fwrite(samples, sizeof(*samples), num_samples, file_handle_);
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-17 06:39:05 -08:00
|
|
|
ChannelBufferWavReader::ChannelBufferWavReader(std::unique_ptr<WavReader> file)
|
2015-12-17 03:04:15 -08:00
|
|
|
: file_(std::move(file)) {}
|
2015-11-20 00:11:53 -08:00
|
|
|
|
2016-08-29 13:10:29 -07:00
|
|
|
ChannelBufferWavReader::~ChannelBufferWavReader() = default;
|
|
|
|
|
|
2015-11-20 00:11:53 -08:00
|
|
|
bool ChannelBufferWavReader::Read(ChannelBuffer<float>* buffer) {
|
|
|
|
|
RTC_CHECK_EQ(file_->num_channels(), buffer->num_channels());
|
|
|
|
|
interleaved_.resize(buffer->size());
|
|
|
|
|
if (file_->ReadSamples(interleaved_.size(), &interleaved_[0]) !=
|
|
|
|
|
interleaved_.size()) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FloatS16ToFloat(&interleaved_[0], interleaved_.size(), &interleaved_[0]);
|
|
|
|
|
Deinterleave(&interleaved_[0], buffer->num_frames(), buffer->num_channels(),
|
|
|
|
|
buffer->channels());
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-17 06:39:05 -08:00
|
|
|
ChannelBufferWavWriter::ChannelBufferWavWriter(std::unique_ptr<WavWriter> file)
|
2015-12-17 03:04:15 -08:00
|
|
|
: file_(std::move(file)) {}
|
2015-11-20 00:11:53 -08:00
|
|
|
|
2016-08-29 13:10:29 -07:00
|
|
|
ChannelBufferWavWriter::~ChannelBufferWavWriter() = default;
|
|
|
|
|
|
2015-11-20 00:11:53 -08:00
|
|
|
void ChannelBufferWavWriter::Write(const ChannelBuffer<float>& buffer) {
|
|
|
|
|
RTC_CHECK_EQ(file_->num_channels(), buffer.num_channels());
|
|
|
|
|
interleaved_.resize(buffer.size());
|
|
|
|
|
Interleave(buffer.channels(), buffer.num_frames(), buffer.num_channels(),
|
|
|
|
|
&interleaved_[0]);
|
|
|
|
|
FloatToFloatS16(&interleaved_[0], interleaved_.size(), &interleaved_[0]);
|
|
|
|
|
file_->WriteSamples(&interleaved_[0], interleaved_.size());
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-12 09:57:01 +02:00
|
|
|
ChannelBufferVectorWriter::ChannelBufferVectorWriter(std::vector<float>* output)
|
|
|
|
|
: output_(output) {
|
|
|
|
|
RTC_DCHECK(output_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ChannelBufferVectorWriter::~ChannelBufferVectorWriter() = default;
|
|
|
|
|
|
|
|
|
|
void ChannelBufferVectorWriter::Write(const ChannelBuffer<float>& buffer) {
|
|
|
|
|
// Account for sample rate changes throughout a simulation.
|
|
|
|
|
interleaved_buffer_.resize(buffer.size());
|
|
|
|
|
Interleave(buffer.channels(), buffer.num_frames(), buffer.num_channels(),
|
|
|
|
|
interleaved_buffer_.data());
|
|
|
|
|
size_t old_size = output_->size();
|
|
|
|
|
output_->resize(old_size + interleaved_buffer_.size());
|
|
|
|
|
FloatToFloatS16(interleaved_buffer_.data(), interleaved_buffer_.size(),
|
|
|
|
|
output_->data() + old_size);
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-07 22:17:51 -07:00
|
|
|
void WriteIntData(const int16_t* data,
|
|
|
|
|
size_t length,
|
|
|
|
|
WavWriter* wav_file,
|
|
|
|
|
RawFile* raw_file) {
|
|
|
|
|
if (wav_file) {
|
|
|
|
|
wav_file->WriteSamples(data, length);
|
|
|
|
|
}
|
|
|
|
|
if (raw_file) {
|
|
|
|
|
raw_file->WriteSamples(data, length);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WriteFloatData(const float* const* data,
|
Misc. small cleanups.
* Better param names
* Avoid using negative values for (bogus) placeholder channel counts (mostly in tests). Since channels will be changing to size_t, negative values will be illegal; it's sufficient to use 0 in these cases.
* Use arraysize()
* Use size_t for counting frames, samples, blocks, buffers, and bytes -- most of these are already size_t in most places, this just fixes some stragglers
* reinterpret_cast<int64_t>(void*) is not necessarily safe; use uintptr_t instead
* Remove unnecessary code, e.g. dead code, needlessly long/repetitive code, or function overrides that exactly match the base definition
* Fix indenting
* Use uint32_t for timestamps (matching how it's already a uint32_t in most places)
* Spelling
* RTC_CHECK_EQ(expected, actual)
* Rewrap
* Use .empty()
* Be more pedantic about matching int/int32_t/
* Remove pointless consts on input parameters to functions
* Add missing sanity checks
All this was found in the course of constructing https://codereview.webrtc.org/1316523002/ , and is being landed separately first.
BUG=none
TEST=none
Review URL: https://codereview.webrtc.org/1534193008
Cr-Commit-Position: refs/heads/master@{#11191}
2016-01-08 13:50:27 -08:00
|
|
|
size_t samples_per_channel,
|
Convert channel counts to size_t.
IIRC, this was originally requested by ajm during review of the other size_t conversions I did over the past year, and I agreed it made sense, but wanted to do it separately since those changes were already gargantuan.
BUG=chromium:81439
TEST=none
R=henrik.lundin@webrtc.org, henrika@webrtc.org, kjellander@webrtc.org, minyue@webrtc.org, perkj@webrtc.org, solenberg@webrtc.org, stefan@webrtc.org, tina.legrand@webrtc.org
Review URL: https://codereview.webrtc.org/1316523002 .
Cr-Commit-Position: refs/heads/master@{#11229}
2016-01-12 16:26:35 -08:00
|
|
|
size_t num_channels,
|
2015-05-07 22:17:51 -07:00
|
|
|
WavWriter* wav_file,
|
|
|
|
|
RawFile* raw_file) {
|
|
|
|
|
size_t length = num_channels * samples_per_channel;
|
2016-02-17 06:39:05 -08:00
|
|
|
std::unique_ptr<float[]> buffer(new float[length]);
|
2015-05-07 22:17:51 -07:00
|
|
|
Interleave(data, samples_per_channel, num_channels, buffer.get());
|
|
|
|
|
if (raw_file) {
|
|
|
|
|
raw_file->WriteSamples(buffer.get(), length);
|
|
|
|
|
}
|
|
|
|
|
// TODO(aluebs): Use ScaleToInt16Range() from audio_util
|
|
|
|
|
for (size_t i = 0; i < length; ++i) {
|
2018-06-19 15:03:05 +02:00
|
|
|
buffer[i] = buffer[i] > 0
|
|
|
|
|
? buffer[i] * std::numeric_limits<int16_t>::max()
|
|
|
|
|
: -buffer[i] * std::numeric_limits<int16_t>::min();
|
2015-05-07 22:17:51 -07:00
|
|
|
}
|
|
|
|
|
if (wav_file) {
|
|
|
|
|
wav_file->WriteSamples(buffer.get(), length);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FILE* OpenFile(const std::string& filename, const char* mode) {
|
|
|
|
|
FILE* file = fopen(filename.c_str(), mode);
|
|
|
|
|
if (!file) {
|
|
|
|
|
printf("Unable to open file %s\n", filename.c_str());
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
return file;
|
|
|
|
|
}
|
|
|
|
|
|
Misc. small cleanups.
* Better param names
* Avoid using negative values for (bogus) placeholder channel counts (mostly in tests). Since channels will be changing to size_t, negative values will be illegal; it's sufficient to use 0 in these cases.
* Use arraysize()
* Use size_t for counting frames, samples, blocks, buffers, and bytes -- most of these are already size_t in most places, this just fixes some stragglers
* reinterpret_cast<int64_t>(void*) is not necessarily safe; use uintptr_t instead
* Remove unnecessary code, e.g. dead code, needlessly long/repetitive code, or function overrides that exactly match the base definition
* Fix indenting
* Use uint32_t for timestamps (matching how it's already a uint32_t in most places)
* Spelling
* RTC_CHECK_EQ(expected, actual)
* Rewrap
* Use .empty()
* Be more pedantic about matching int/int32_t/
* Remove pointless consts on input parameters to functions
* Add missing sanity checks
All this was found in the course of constructing https://codereview.webrtc.org/1316523002/ , and is being landed separately first.
BUG=none
TEST=none
Review URL: https://codereview.webrtc.org/1534193008
Cr-Commit-Position: refs/heads/master@{#11191}
2016-01-08 13:50:27 -08:00
|
|
|
size_t SamplesFromRate(int rate) {
|
|
|
|
|
return static_cast<size_t>(AudioProcessing::kChunkSizeMs * rate / 1000);
|
2015-05-07 22:17:51 -07:00
|
|
|
}
|
|
|
|
|
|
2018-06-19 15:03:05 +02:00
|
|
|
void SetFrameSampleRate(AudioFrame* frame, int sample_rate_hz) {
|
2015-05-07 22:17:51 -07:00
|
|
|
frame->sample_rate_hz_ = sample_rate_hz;
|
2018-06-19 15:03:05 +02:00
|
|
|
frame->samples_per_channel_ =
|
|
|
|
|
AudioProcessing::kChunkSizeMs * sample_rate_hz / 1000;
|
2015-05-07 22:17:51 -07:00
|
|
|
}
|
|
|
|
|
|
Convert channel counts to size_t.
IIRC, this was originally requested by ajm during review of the other size_t conversions I did over the past year, and I agreed it made sense, but wanted to do it separately since those changes were already gargantuan.
BUG=chromium:81439
TEST=none
R=henrik.lundin@webrtc.org, henrika@webrtc.org, kjellander@webrtc.org, minyue@webrtc.org, perkj@webrtc.org, solenberg@webrtc.org, stefan@webrtc.org, tina.legrand@webrtc.org
Review URL: https://codereview.webrtc.org/1316523002 .
Cr-Commit-Position: refs/heads/master@{#11229}
2016-01-12 16:26:35 -08:00
|
|
|
AudioProcessing::ChannelLayout LayoutFromChannels(size_t num_channels) {
|
2015-05-07 22:17:51 -07:00
|
|
|
switch (num_channels) {
|
|
|
|
|
case 1:
|
|
|
|
|
return AudioProcessing::kMono;
|
|
|
|
|
case 2:
|
|
|
|
|
return AudioProcessing::kStereo;
|
|
|
|
|
default:
|
2015-11-20 00:11:53 -08:00
|
|
|
RTC_CHECK(false);
|
2015-05-07 22:17:51 -07:00
|
|
|
return AudioProcessing::kMono;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace webrtc
|