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.

80 lines
2.2 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright (c) 2014 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 "common_audio/channel_buffer.h"
  11. #include <cstdint>
  12. #include "common_audio/include/audio_util.h"
  13. #include "rtc_base/checks.h"
  14. namespace webrtc {
  15. IFChannelBuffer::IFChannelBuffer(size_t num_frames,
  16. size_t num_channels,
  17. size_t num_bands)
  18. : ivalid_(true),
  19. ibuf_(num_frames, num_channels, num_bands),
  20. fvalid_(true),
  21. fbuf_(num_frames, num_channels, num_bands) {}
  22. IFChannelBuffer::~IFChannelBuffer() = default;
  23. ChannelBuffer<int16_t>* IFChannelBuffer::ibuf() {
  24. RefreshI();
  25. fvalid_ = false;
  26. return &ibuf_;
  27. }
  28. ChannelBuffer<float>* IFChannelBuffer::fbuf() {
  29. RefreshF();
  30. ivalid_ = false;
  31. return &fbuf_;
  32. }
  33. const ChannelBuffer<int16_t>* IFChannelBuffer::ibuf_const() const {
  34. RefreshI();
  35. return &ibuf_;
  36. }
  37. const ChannelBuffer<float>* IFChannelBuffer::fbuf_const() const {
  38. RefreshF();
  39. return &fbuf_;
  40. }
  41. void IFChannelBuffer::RefreshF() const {
  42. if (!fvalid_) {
  43. RTC_DCHECK(ivalid_);
  44. fbuf_.set_num_channels(ibuf_.num_channels());
  45. const int16_t* const* int_channels = ibuf_.channels();
  46. float* const* float_channels = fbuf_.channels();
  47. for (size_t i = 0; i < ibuf_.num_channels(); ++i) {
  48. for (size_t j = 0; j < ibuf_.num_frames(); ++j) {
  49. float_channels[i][j] = int_channels[i][j];
  50. }
  51. }
  52. fvalid_ = true;
  53. }
  54. }
  55. void IFChannelBuffer::RefreshI() const {
  56. if (!ivalid_) {
  57. RTC_DCHECK(fvalid_);
  58. int16_t* const* int_channels = ibuf_.channels();
  59. ibuf_.set_num_channels(fbuf_.num_channels());
  60. const float* const* float_channels = fbuf_.channels();
  61. for (size_t i = 0; i < fbuf_.num_channels(); ++i) {
  62. FloatS16ToS16(float_channels[i], ibuf_.num_frames(), int_channels[i]);
  63. }
  64. ivalid_ = true;
  65. }
  66. }
  67. } // namespace webrtc