time_stretch.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright 2016 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <cstddef>
  6. #include <memory>
  7. #include <vector>
  8. #include "common/common_types.h"
  9. namespace AudioCore {
  10. class TimeStretcher final {
  11. public:
  12. TimeStretcher();
  13. ~TimeStretcher();
  14. /**
  15. * Set sample rate for the samples that Process returns.
  16. * @param sample_rate The sample rate.
  17. */
  18. void SetOutputSampleRate(unsigned int sample_rate);
  19. /**
  20. * Add samples to be processed.
  21. * @param sample_buffer Buffer of samples in interleaved stereo PCM16 format.
  22. * @param num_sample Number of samples.
  23. */
  24. void AddSamples(const s16* sample_buffer, size_t num_samples);
  25. /// Flush audio remaining in internal buffers.
  26. void Flush();
  27. /// Resets internal state and clears buffers.
  28. void Reset();
  29. /**
  30. * Does audio stretching and produces the time-stretched samples.
  31. * Timer calculations use sample_delay to determine how much of a margin we have.
  32. * @param sample_delay How many samples are buffered downstream of this module and haven't been
  33. * played yet.
  34. * @return Samples to play in interleaved stereo PCM16 format.
  35. */
  36. std::vector<s16> Process(size_t sample_delay);
  37. private:
  38. struct Impl;
  39. std::unique_ptr<Impl> impl;
  40. /// INTERNAL: ratio = wallclock time / emulated time
  41. double CalculateCurrentRatio();
  42. /// INTERNAL: If we have too many or too few samples downstream, nudge ratio in the appropriate
  43. /// direction.
  44. double CorrectForUnderAndOverflow(double ratio, size_t sample_delay) const;
  45. /// INTERNAL: Gets the time-stretched samples from SoundTouch.
  46. std::vector<s16> GetSamples();
  47. };
  48. } // namespace AudioCore