time_stretch: Simplify audio stretcher

This commit is contained in:
MerryMage 2018-09-08 21:28:19 +01:00
parent f34711219a
commit eed55a813e
4 changed files with 88 additions and 162 deletions

View file

@ -4,57 +4,39 @@
#pragma once
#include <array>
#include <cstddef>
#include <memory>
#include <vector>
#include "common/common_types.h"
namespace soundtouch {
class SoundTouch;
}
namespace AudioCore {
class TimeStretcher final {
class TimeStretcher {
public:
TimeStretcher();
~TimeStretcher();
/**
* Set sample rate for the samples that Process returns.
* @param sample_rate The sample rate.
*/
void SetOutputSampleRate(unsigned int sample_rate);
/**
* Add samples to be processed.
* @param sample_buffer Buffer of samples in interleaved stereo PCM16 format.
* @param num_samples Number of samples.
*/
void AddSamples(const s16* sample_buffer, std::size_t num_samples);
/// @param in Input sample buffer
/// @param num_in Number of input frames in `in`
/// @param out Output sample buffer
/// @param num_out Desired number of output frames in `out`
/// @returns Actual number of frames written to `out`
std::size_t Process(const s16* in, std::size_t num_in, s16* out, std::size_t num_out);
void Clear();
/// Flush audio remaining in internal buffers.
void Flush();
/// Resets internal state and clears buffers.
void Reset();
/**
* Does audio stretching and produces the time-stretched samples.
* Timer calculations use sample_delay to determine how much of a margin we have.
* @param sample_delay How many samples are buffered downstream of this module and haven't been
* played yet.
* @return Samples to play in interleaved stereo PCM16 format.
*/
std::vector<s16> Process(std::size_t sample_delay);
private:
struct Impl;
std::unique_ptr<Impl> impl;
/// INTERNAL: ratio = wallclock time / emulated time
double CalculateCurrentRatio();
/// INTERNAL: If we have too many or too few samples downstream, nudge ratio in the appropriate
/// direction.
double CorrectForUnderAndOverflow(double ratio, std::size_t sample_delay) const;
/// INTERNAL: Gets the time-stretched samples from SoundTouch.
std::vector<s16> GetSamples();
unsigned int sample_rate;
std::unique_ptr<soundtouch::SoundTouch> sound_touch;
double stretch_ratio = 1.0;
};
} // namespace AudioCore