2#include <ossia/audio/fade.hpp>
3#include <ossia/dataflow/audio_stretch_mode.hpp>
4#include <ossia/dataflow/node_process.hpp>
5#include <ossia/dataflow/nodes/media.hpp>
6#include <ossia/dataflow/nodes/timestretch/raw_stretcher.hpp>
7#include <ossia/dataflow/nodes/timestretch/repitch_stretcher.hpp>
8#include <ossia/dataflow/nodes/timestretch/rubberband_stretcher.hpp>
9#include <ossia/dataflow/port.hpp>
10#include <ossia/dataflow/sample_to_float.hpp>
11#include <ossia/detail/pod_vector.hpp>
12#include <ossia/detail/variant.hpp>
18struct sample_read_info
20 int64_t samples_to_read{};
21 int64_t samples_to_write{};
25sample_info(int64_t bufferSize,
double durationRatio,
const ossia::token_request& t)
34 _.samples_to_read = t.physical_read_duration(durationRatio);
35 _.samples_to_write = t.safe_physical_write_duration(durationRatio, bufferSize);
41perform_upmix(
const std::size_t upmix,
const std::size_t chan, ossia::audio_port& ap)
74 for(std::size_t chan = 1; chan < upmix; ++chan)
75 ap.channel(chan).assign(ap.channel(0).begin(), ap.channel(0).end());
86inline void perform_start_offset(
const std::size_t start, ossia::audio_port& ap)
90 ap.get().insert(ap.get().begin(), start, ossia::audio_channel{});
103 ~at_end() { func(); }
111 RubberbandStretcher = 1,
114 [[nodiscard]] int64_t next_sample_to_read() const noexcept
117 [](
auto& stretcher)
noexcept {
return stretcher.next_sample_to_read; },
121 void transport(int64_t date)
124 [=](
auto& stretcher)
noexcept {
return stretcher.transport(date); }, m_stretch);
128 int64_t date, ossia::audio_stretch_mode mode, std::size_t channels,
129 std::size_t fileSampleRate)
135 case ossia::audio_stretch_mode::None: {
136 if(
auto s = ossia::get_if<RawStretcher>(&m_stretch))
142 m_stretch.emplace<RawStretcher>(date);
147#if defined(OSSIA_ENABLE_RUBBERBAND)
148 case ossia::audio_stretch_mode::RubberBandStandard:
149 case ossia::audio_stretch_mode::RubberBandPercussive:
150 case ossia::audio_stretch_mode::RubberBandStandardHQ:
151 case ossia::audio_stretch_mode::RubberBandPercussiveHQ: {
152 const auto preset = get_rubberband_preset(mode);
153 if(
auto s = ossia::get_if<RubberbandStretcher>(&m_stretch);
154 s && s->options == preset)
160 m_stretch.emplace<rubberband_stretcher>(
161 preset, channels, fileSampleRate, date);
167#if defined(OSSIA_ENABLE_LIBSAMPLERATE)
168 case ossia::audio_stretch_mode::Repitch:
169 case ossia::audio_stretch_mode::RepitchMediumQ:
170 case ossia::audio_stretch_mode::RepitchFastestQ: {
171 const auto preset = get_samplerate_preset(mode);
172 if(
auto s = ossia::get_if<RepitchStretcher>(&m_stretch);
173 s && s->repitchers.size() == channels && s->preset == preset)
180 m_stretch.emplace<repitch_stretcher>(preset, channels, 1024, date);
188 template <
typename T>
190 run(T& audio_fetcher,
const ossia::token_request& t, ossia::exec_state_facade e,
191 double tempo_ratio, std::size_t chan, std::size_t len, int64_t samples_to_read,
192 int64_t samples_to_write, int64_t samples_offset,
193 const ossia::mutable_audio_span<double>& ap)
196 [&](
auto& stretcher) {
198 audio_fetcher, t, e, tempo_ratio, chan, len, samples_to_read, samples_to_write,
204 [[nodiscard]]
bool stretch() const noexcept {
return m_stretch.index() != 0; }
209#if defined(OSSIA_ENABLE_RUBBERBAND)
213#if defined(OSSIA_ENABLE_LIBSAMPLERATE)
221struct sound_processing_info
223 time_value m_prev_date{time_value::infinite_min};
225 time_value m_loop_duration{};
226 time_value m_start_offset{};
230 int64_t m_loop_duration_samples{};
231 int64_t m_start_offset_samples{};
233 double m_last_stretch{1.0};
234 ossia::resampler m_resampler{};
241 m_loop_duration = loop_duration;
242 m_start_offset = start_offset;
246 void set_resampler(ossia::resampler&& r)
248 auto date = m_resampler.next_sample_to_read();
249 m_resampler = std::move(r);
250 m_resampler.transport(date);
253 void set_native_tempo(
double v) { tempo = v; }
255 double update_stretch(
256 const ossia::token_request& t,
const ossia::exec_state_facade& e)
noexcept
258 double stretch_ratio = 1.;
259 double model_ratio = 1.;
262 if(m_resampler.stretch())
264 model_ratio = ossia::root_tempo / this->tempo;
265 stretch_ratio = this->tempo / t.tempo;
269 model_ratio = ossia::root_tempo / t.tempo;
273 m_loop_duration_samples = m_loop_duration.impl * e.modelToSamples() * model_ratio;
274 m_start_offset_samples = m_start_offset.impl * e.modelToSamples() * model_ratio;
275 return stretch_ratio;
280 :
public ossia::nonowning_graph_node
281 ,
public sound_processing_info
284 virtual void transport(time_value date) = 0;
287class dummy_sound_node final :
public sound_node
290 ossia::audio_outlet audio_out;
294 m_outlets.push_back(&audio_out);
297 std::string label() const noexcept
override {
return "dummy_sound_node"; }
299 void transport(time_value date)
override { }
301 void run(
const ossia::token_request& t, ossia::exec_state_facade e)
noexcept override
306#if defined(OSSIA_SCENARIO_DATAFLOW)
307class sound_process final :
public ossia::node_process
310 using ossia::node_process::node_process;
313 void state(
const ossia::token_request& req)
override
318 static_cast<sound_node&
>(*this->node)
319 .set_loop_info(m_loop_duration, m_start_offset, m_loops);
327 void offset_impl(time_value date)
override
329 static_cast<sound_node&
>(*this->node).transport(date);
331 void transport_impl(time_value date)
override
333 static_cast<sound_node&
>(*this->node).transport(date);
The time_value class.
Definition ossia/editor/scenario/time_value.hpp:30