Sample rates not divisible by 100, in particular 11025 Hz and 22050 Hz, have long been used with APM in Chrome, but the support has never been stated explicitly. This CL makes minor modifications to the APM API to clarify how rates are handled when 10 ms is not an integer number of samples. Unit tests are also extended to cover this case better. This does not update all references to 10 ms and implicit floor(sample_rate/100) computations, but it does at least take us closer to a correct API. Note that not all code needs to support these sample rates. For example, audio processing submodules only need to operate on the native APM rates 16000, 32000, 48000. Bug: chromium:1332484 Change-Id: I1dad15468f6ccb9c0d4d09c5819fe87f8388d5b8 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/268769 Reviewed-by: Henrik Andreassson <henrika@webrtc.org> Commit-Queue: Sam Zackrisson <saza@webrtc.org> Reviewed-by: Ivo Creusen <ivoc@webrtc.org> Cr-Commit-Position: refs/heads/main@{#37682}
87 lines
2.9 KiB
C++
87 lines
2.9 KiB
C++
/*
|
|
* 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.
|
|
*/
|
|
|
|
#include "modules/audio_processing/test/test_utils.h"
|
|
|
|
#include <utility>
|
|
|
|
#include "rtc_base/checks.h"
|
|
#include "rtc_base/system/arch.h"
|
|
|
|
namespace webrtc {
|
|
|
|
ChannelBufferWavReader::ChannelBufferWavReader(std::unique_ptr<WavReader> file)
|
|
: file_(std::move(file)) {}
|
|
|
|
ChannelBufferWavReader::~ChannelBufferWavReader() = default;
|
|
|
|
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;
|
|
}
|
|
|
|
ChannelBufferWavWriter::ChannelBufferWavWriter(std::unique_ptr<WavWriter> file)
|
|
: file_(std::move(file)) {}
|
|
|
|
ChannelBufferWavWriter::~ChannelBufferWavWriter() = default;
|
|
|
|
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());
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
void SetFrameSampleRate(Int16FrameData* frame, int sample_rate_hz) {
|
|
frame->sample_rate_hz = sample_rate_hz;
|
|
frame->samples_per_channel =
|
|
AudioProcessing::kChunkSizeMs * sample_rate_hz / 1000;
|
|
}
|
|
|
|
} // namespace webrtc
|