webrtc/modules/audio_coding/acm2/acm_resampler.cc
Tommi 1f3679884c Start using ArrayView in AudioFrame, update PushResampler
Start introducing ArrayView to AudioFrame and code that flows down
from there.  In this first step:
* Add `data_view()` that returns a read-only ArrayView for the
  audio buffer. When AudioFrame is not initialized however, data_view()
  will return a nullptr whereas the current data() method never returns
  nullptr.
* Add `mutable_data()` that requires two arguments for properly setting
  the samples per channel and number of channels that's required for
  accurately reserving the returned mutable ArrayView.
  A notable behavior change is that if the requested number of channels
  is larger than supported or the calculated buffer size is too large,
  the function will trigger a check.
* Add TODOs for following work.

Bug: chromium:335805780
Change-Id: I2937de800422589ebe6a3840b3caadf3d9ff8b00
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/347982
Reviewed-by: Per Åhgren <peah@webrtc.org>
Commit-Queue: Tomas Gunnarsson <tommi@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#42202}
2024-04-30 15:33:08 +00:00

62 lines
2 KiB
C++

/*
* Copyright (c) 2012 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_coding/acm2/acm_resampler.h"
#include <string.h>
#include "rtc_base/logging.h"
namespace webrtc {
namespace acm2 {
ACMResampler::ACMResampler() {}
ACMResampler::~ACMResampler() {}
int ACMResampler::Resample10Msec(const int16_t* in_audio,
int in_freq_hz,
int out_freq_hz,
size_t num_audio_channels,
size_t out_capacity_samples,
int16_t* out_audio) {
size_t in_length = in_freq_hz * num_audio_channels / 100;
if (in_freq_hz == out_freq_hz) {
if (out_capacity_samples < in_length) {
RTC_DCHECK_NOTREACHED();
return -1;
}
memcpy(out_audio, in_audio, in_length * sizeof(int16_t));
return static_cast<int>(in_length / num_audio_channels);
}
if (resampler_.InitializeIfNeeded(in_freq_hz, out_freq_hz,
num_audio_channels) != 0) {
RTC_LOG(LS_ERROR) << "InitializeIfNeeded(" << in_freq_hz << ", "
<< out_freq_hz << ", " << num_audio_channels
<< ") failed.";
return -1;
}
int out_length = resampler_.Resample(
rtc::ArrayView<const int16_t>(in_audio, in_length),
rtc::ArrayView<int16_t>(out_audio, out_capacity_samples));
if (out_length == -1) {
RTC_LOG(LS_ERROR) << "Resample(" << in_audio << ", " << in_length << ", "
<< out_audio << ", " << out_capacity_samples
<< ") failed.";
return -1;
}
return static_cast<int>(out_length / num_audio_channels);
}
} // namespace acm2
} // namespace webrtc