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

@ -15,6 +15,7 @@ DspInterface::DspInterface() = default;
DspInterface::~DspInterface() = default;
void DspInterface::SetSink(const std::string& sink_id, const std::string& audio_device) {
sink.reset();
const SinkDetails& sink_details = GetSinkDetails(sink_id);
sink = sink_details.factory(audio_device);
sink->SetCallback(
@ -32,7 +33,7 @@ void DspInterface::EnableStretching(bool enable) {
return;
if (!enable) {
FlushResidualStretcherAudio();
flushing_time_stretcher = true;
}
perform_time_stretching = enable;
}
@ -51,17 +52,27 @@ void DspInterface::OutputFrame(StereoFrame16& frame) {
fifo.Push(frame.data(), frame.size());
}
void DspInterface::FlushResidualStretcherAudio() {}
void DspInterface::OutputCallback(s16* buffer, size_t num_frames) {
const size_t frames_written = fifo.Pop(buffer, num_frames);
void DspInterface::OutputCallback(s16* buffer, std::size_t num_frames) {
std::size_t frames_written;
if (perform_time_stretching) {
const std::vector<s16> in{fifo.Pop()};
const std::size_t num_in{in.size() / 2};
frames_written = time_stretcher.Process(in.data(), num_in, buffer, num_frames);
} else if (flushing_time_stretcher) {
time_stretcher.Flush();
frames_written = time_stretcher.Process(nullptr, 0, buffer, num_frames);
frames_written += fifo.Pop(buffer, num_frames - frames_written);
flushing_time_stretcher = false;
} else {
frames_written = fifo.Pop(buffer, num_frames);
}
if (frames_written > 0) {
std::memcpy(&last_frame[0], buffer + 2 * (frames_written - 1), 2 * sizeof(s16));
}
// Hold last emitted frame; this prevents popping.
for (size_t i = frames_written; i < num_frames; i++) {
for (std::size_t i = frames_written; i < num_frames; i++) {
std::memcpy(buffer + 2 * i, &last_frame[0], 2 * sizeof(s16));
}
}