Zrythm v2.0.0-alpha.1
a highly automated and intuitive digital audio workstation
Loading...
Searching...
No Matches
timestretch_engine.h
1// SPDX-FileCopyrightText: © 2026 Alexandros Theodotou <alex@zrythm.org>
2// SPDX-License-Identifier: LicenseRef-ZrythmLicense
3
4#pragma once
5
6#include <cstdint>
7#include <memory>
8#include <span>
9#include <stdexcept>
10#include <string_view>
11
12#include "dsp/time_warp_map.h"
13#include "utils/audio.h"
14#include "utils/units.h"
15
16#include <QObject>
17
18namespace zrythm::dsp
19{
20
25{
26 Q_GADGET
27
28public:
44 enum class Algorithm : std::uint8_t
45 {
46 Beats,
47 Monophonic,
48 Polyphonic,
49 Repitch,
50 };
51 Q_ENUM (Algorithm)
52
53 Algorithm algorithm{ Algorithm::Polyphonic };
55 bool preserve_formants{ false };
56};
57
69{
70public:
71 virtual ~ITimeStretchEngine () = default;
72
76 [[nodiscard]] virtual std::string_view id () const = 0;
77
81 [[nodiscard]] virtual bool
82 supports (StretchOptions::Algorithm algorithm) const = 0;
83
97 const utils::audio::AudioBuffer &input,
98 const TimeWarpMap &warp,
99 const StretchOptions &options) [[clang::blocking]]
100 {
101 if (!warp.is_valid ())
102 throw std::invalid_argument ("TimeWarpMap is not valid");
103 if (warp.source_length != units::samples (input.getNumSamples ()))
104 throw std::invalid_argument (
105 "TimeWarpMap source_length does not match input frame count");
106 if (!supports (options.algorithm))
107 throw std::invalid_argument (
108 "engine does not support the requested algorithm");
109 return stretch_impl (input, warp, options);
110 }
111
112private:
120 [[nodiscard]] virtual utils::audio::AudioBuffer stretch_impl (
121 const utils::audio::AudioBuffer &input,
122 const TimeWarpMap &warp,
123 const StretchOptions &options) [[clang::blocking]] = 0;
124};
125
127enum class TimeStretchEngineId : std::uint8_t
128{
129 RubberBand,
130 Resampler,
131};
132
135{
136 TimeStretchEngineId id;
138 std::string_view key;
140 std::string_view display_name;
141 bool supports_pitch_preserve;
142};
143
149[[nodiscard]] std::span<const TimeStretchEngineInfo>
150available_timestretch_engines () noexcept;
151
160[[nodiscard]] std::unique_ptr<ITimeStretchEngine>
161create_timestretch_engine (
162 TimeStretchEngineId id,
163 units::sample_rate_t sample_rate);
164
170[[nodiscard]] std::unique_ptr<ITimeStretchEngine>
171create_default_timestretch_engine (
172 const StretchOptions &options,
173 units::sample_rate_t sample_rate);
174
175} // namespace zrythm::dsp
176
177Q_DECLARE_METATYPE (zrythm::dsp::StretchOptions::Algorithm)
Audio resampler.
Definition resampler.h:24
Abstract time-stretch engine.
virtual std::string_view id() const =0
A stable, non-translatable identifier for persistence (settings).
utils::audio::AudioBuffer stretch(const utils::audio::AudioBuffer &input, const TimeWarpMap &warp, const StretchOptions &options)
Stretch input according to warp (Non-Virtual Interface).
virtual bool supports(StretchOptions::Algorithm algorithm) const =0
Whether this engine supports the given algorithm.
Options governing a stretch operation.
Algorithm
Material-aware timestretch algorithm.
bool preserve_formants
Preserve formants (only meaningful when algorithm is not Repitch).
Metadata describing an engine, for the settings UI.
std::string_view key
Non-translatable identifier (matches ITimeStretchEngine::id).
std::string_view display_name
Translation context string for the display name.
A monotonic time-warp mapping from source samples to output samples.