You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
3.6 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright (c) 2012 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. #include "audio/remix_resample.h"
  11. #include "api/audio/audio_frame.h"
  12. #include "audio/utility/audio_frame_operations.h"
  13. #include "common_audio/resampler/include/push_resampler.h"
  14. #include "rtc_base/checks.h"
  15. namespace webrtc {
  16. namespace voe {
  17. void RemixAndResample(const AudioFrame& src_frame,
  18. PushResampler<int16_t>* resampler,
  19. AudioFrame* dst_frame) {
  20. RemixAndResample(src_frame.data(), src_frame.samples_per_channel_,
  21. src_frame.num_channels_, src_frame.sample_rate_hz_,
  22. resampler, dst_frame);
  23. dst_frame->timestamp_ = src_frame.timestamp_;
  24. dst_frame->elapsed_time_ms_ = src_frame.elapsed_time_ms_;
  25. dst_frame->ntp_time_ms_ = src_frame.ntp_time_ms_;
  26. dst_frame->packet_infos_ = src_frame.packet_infos_;
  27. }
  28. void RemixAndResample(const int16_t* src_data,
  29. size_t samples_per_channel,
  30. size_t num_channels,
  31. int sample_rate_hz,
  32. PushResampler<int16_t>* resampler,
  33. AudioFrame* dst_frame) {
  34. const int16_t* audio_ptr = src_data;
  35. size_t audio_ptr_num_channels = num_channels;
  36. int16_t downmixed_audio[AudioFrame::kMaxDataSizeSamples];
  37. // Downmix before resampling.
  38. if (num_channels > dst_frame->num_channels_) {
  39. RTC_DCHECK(num_channels == 2 || num_channels == 4)
  40. << "num_channels: " << num_channels;
  41. RTC_DCHECK(dst_frame->num_channels_ == 1 || dst_frame->num_channels_ == 2)
  42. << "dst_frame->num_channels_: " << dst_frame->num_channels_;
  43. AudioFrameOperations::DownmixChannels(
  44. src_data, num_channels, samples_per_channel, dst_frame->num_channels_,
  45. downmixed_audio);
  46. audio_ptr = downmixed_audio;
  47. audio_ptr_num_channels = dst_frame->num_channels_;
  48. }
  49. if (resampler->InitializeIfNeeded(sample_rate_hz, dst_frame->sample_rate_hz_,
  50. audio_ptr_num_channels) == -1) {
  51. FATAL() << "InitializeIfNeeded failed: sample_rate_hz = " << sample_rate_hz
  52. << ", dst_frame->sample_rate_hz_ = " << dst_frame->sample_rate_hz_
  53. << ", audio_ptr_num_channels = " << audio_ptr_num_channels;
  54. }
  55. // TODO(yujo): for muted input frames, don't resample. Either 1) allow
  56. // resampler to return output length without doing the resample, so we know
  57. // how much to zero here; or 2) make resampler accept a hint that the input is
  58. // zeroed.
  59. const size_t src_length = samples_per_channel * audio_ptr_num_channels;
  60. int out_length =
  61. resampler->Resample(audio_ptr, src_length, dst_frame->mutable_data(),
  62. AudioFrame::kMaxDataSizeSamples);
  63. if (out_length == -1) {
  64. FATAL() << "Resample failed: audio_ptr = " << audio_ptr
  65. << ", src_length = " << src_length
  66. << ", dst_frame->mutable_data() = " << dst_frame->mutable_data();
  67. }
  68. dst_frame->samples_per_channel_ = out_length / audio_ptr_num_channels;
  69. // Upmix after resampling.
  70. if (num_channels == 1 && dst_frame->num_channels_ == 2) {
  71. // The audio in dst_frame really is mono at this point; MonoToStereo will
  72. // set this back to stereo.
  73. dst_frame->num_channels_ = 1;
  74. AudioFrameOperations::UpmixChannels(2, dst_frame);
  75. }
  76. }
  77. } // namespace voe
  78. } // namespace webrtc