push_sinc_resampler.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * Copyright (c) 2013 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. #ifndef COMMON_AUDIO_RESAMPLER_PUSH_SINC_RESAMPLER_H_
  11. #define COMMON_AUDIO_RESAMPLER_PUSH_SINC_RESAMPLER_H_
  12. #include <stddef.h>
  13. #include <stdint.h>
  14. #include <memory>
  15. #include "common_audio/resampler/sinc_resampler.h"
  16. #include "rtc_base/constructor_magic.h"
  17. namespace webrtc {
  18. // A thin wrapper over SincResampler to provide a push-based interface as
  19. // required by WebRTC. SincResampler uses a pull-based interface, and will
  20. // use SincResamplerCallback::Run() to request data upon a call to Resample().
  21. // These Run() calls will happen on the same thread Resample() is called on.
  22. class PushSincResampler : public SincResamplerCallback {
  23. public:
  24. // Provide the size of the source and destination blocks in samples. These
  25. // must correspond to the same time duration (typically 10 ms) as the sample
  26. // ratio is inferred from them.
  27. PushSincResampler(size_t source_frames, size_t destination_frames);
  28. ~PushSincResampler() override;
  29. // Perform the resampling. |source_frames| must always equal the
  30. // |source_frames| provided at construction. |destination_capacity| must be
  31. // at least as large as |destination_frames|. Returns the number of samples
  32. // provided in destination (for convenience, since this will always be equal
  33. // to |destination_frames|).
  34. size_t Resample(const int16_t* source,
  35. size_t source_frames,
  36. int16_t* destination,
  37. size_t destination_capacity);
  38. size_t Resample(const float* source,
  39. size_t source_frames,
  40. float* destination,
  41. size_t destination_capacity);
  42. // Delay due to the filter kernel. Essentially, the time after which an input
  43. // sample will appear in the resampled output.
  44. static float AlgorithmicDelaySeconds(int source_rate_hz) {
  45. return 1.f / source_rate_hz * SincResampler::kKernelSize / 2;
  46. }
  47. protected:
  48. // Implements SincResamplerCallback.
  49. void Run(size_t frames, float* destination) override;
  50. private:
  51. friend class PushSincResamplerTest;
  52. SincResampler* get_resampler_for_testing() { return resampler_.get(); }
  53. std::unique_ptr<SincResampler> resampler_;
  54. std::unique_ptr<float[]> float_buffer_;
  55. const float* source_ptr_;
  56. const int16_t* source_ptr_int_;
  57. const size_t destination_frames_;
  58. // True on the first call to Resample(), to prime the SincResampler buffer.
  59. bool first_pass_;
  60. // Used to assert we are only requested for as much data as is available.
  61. size_t source_available_;
  62. RTC_DISALLOW_COPY_AND_ASSIGN(PushSincResampler);
  63. };
  64. } // namespace webrtc
  65. #endif // COMMON_AUDIO_RESAMPLER_PUSH_SINC_RESAMPLER_H_