resampler.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright (c) 2011 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. /*
  11. * A wrapper for resampling a numerous amount of sampling combinations.
  12. */
  13. #ifndef COMMON_AUDIO_RESAMPLER_INCLUDE_RESAMPLER_H_
  14. #define COMMON_AUDIO_RESAMPLER_INCLUDE_RESAMPLER_H_
  15. #include <stddef.h>
  16. #include <stdint.h>
  17. namespace webrtc {
  18. // All methods return 0 on success and -1 on failure.
  19. class Resampler {
  20. public:
  21. Resampler();
  22. Resampler(int inFreq, int outFreq, size_t num_channels);
  23. ~Resampler();
  24. // Reset all states
  25. int Reset(int inFreq, int outFreq, size_t num_channels);
  26. // Reset all states if any parameter has changed
  27. int ResetIfNeeded(int inFreq, int outFreq, size_t num_channels);
  28. // Resample samplesIn to samplesOut.
  29. int Push(const int16_t* samplesIn,
  30. size_t lengthIn,
  31. int16_t* samplesOut,
  32. size_t maxLen,
  33. size_t& outLen); // NOLINT: to avoid changing APIs
  34. private:
  35. enum ResamplerMode {
  36. kResamplerMode1To1,
  37. kResamplerMode1To2,
  38. kResamplerMode1To3,
  39. kResamplerMode1To4,
  40. kResamplerMode1To6,
  41. kResamplerMode1To12,
  42. kResamplerMode2To3,
  43. kResamplerMode2To11,
  44. kResamplerMode4To11,
  45. kResamplerMode8To11,
  46. kResamplerMode11To16,
  47. kResamplerMode11To32,
  48. kResamplerMode2To1,
  49. kResamplerMode3To1,
  50. kResamplerMode4To1,
  51. kResamplerMode6To1,
  52. kResamplerMode12To1,
  53. kResamplerMode3To2,
  54. kResamplerMode11To2,
  55. kResamplerMode11To4,
  56. kResamplerMode11To8
  57. };
  58. // Computes the resampler mode for a given sampling frequency pair.
  59. // Returns -1 for unsupported frequency pairs.
  60. static int ComputeResamplerMode(int in_freq_hz,
  61. int out_freq_hz,
  62. ResamplerMode* mode);
  63. // Generic pointers since we don't know what states we'll need
  64. void* state1_;
  65. void* state2_;
  66. void* state3_;
  67. // Storage if needed
  68. int16_t* in_buffer_;
  69. int16_t* out_buffer_;
  70. size_t in_buffer_size_;
  71. size_t out_buffer_size_;
  72. size_t in_buffer_size_max_;
  73. size_t out_buffer_size_max_;
  74. int my_in_frequency_khz_;
  75. int my_out_frequency_khz_;
  76. ResamplerMode my_mode_;
  77. size_t num_channels_;
  78. // Extra instance for stereo
  79. Resampler* slave_left_;
  80. Resampler* slave_right_;
  81. };
  82. } // namespace webrtc
  83. #endif // COMMON_AUDIO_RESAMPLER_INCLUDE_RESAMPLER_H_