2016-05-15 02:04:03 +00:00
|
|
|
// Copyright 2016 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2016-12-13 00:16:46 +00:00
|
|
|
#pragma once
|
|
|
|
|
2016-05-15 02:04:03 +00:00
|
|
|
#include <cstddef>
|
|
|
|
#include <memory>
|
|
|
|
#include <vector>
|
|
|
|
#include "common/common_types.h"
|
|
|
|
|
|
|
|
namespace AudioCore {
|
|
|
|
|
|
|
|
class TimeStretcher final {
|
|
|
|
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.
|
2017-02-27 01:58:51 +00:00
|
|
|
* @param num_samples Number of samples.
|
2016-05-15 02:04:03 +00:00
|
|
|
*/
|
2018-09-06 20:03:28 +00:00
|
|
|
void AddSamples(const s16* sample_buffer, std::size_t num_samples);
|
2016-05-15 02:04:03 +00:00
|
|
|
|
|
|
|
/// 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.
|
2016-09-18 00:38:01 +00:00
|
|
|
* @param sample_delay How many samples are buffered downstream of this module and haven't been
|
|
|
|
* played yet.
|
2016-05-15 02:04:03 +00:00
|
|
|
* @return Samples to play in interleaved stereo PCM16 format.
|
|
|
|
*/
|
2018-09-06 20:03:28 +00:00
|
|
|
std::vector<s16> Process(std::size_t sample_delay);
|
2016-05-15 02:04:03 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
struct Impl;
|
|
|
|
std::unique_ptr<Impl> impl;
|
|
|
|
|
|
|
|
/// INTERNAL: ratio = wallclock time / emulated time
|
|
|
|
double CalculateCurrentRatio();
|
2016-09-18 00:38:01 +00:00
|
|
|
/// INTERNAL: If we have too many or too few samples downstream, nudge ratio in the appropriate
|
|
|
|
/// direction.
|
2018-09-06 20:03:28 +00:00
|
|
|
double CorrectForUnderAndOverflow(double ratio, std::size_t sample_delay) const;
|
2016-05-15 02:04:03 +00:00
|
|
|
/// INTERNAL: Gets the time-stretched samples from SoundTouch.
|
|
|
|
std::vector<s16> GetSamples();
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace AudioCore
|