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>
20struct sample_read_info
22 int64_t samples_to_read{};
23 int64_t samples_to_write{};
27sample_info(int64_t bufferSize,
double durationRatio,
const ossia::token_request& t)
36 _.samples_to_read = t.physical_read_duration(durationRatio);
38 const auto room = t.safe_physical_write_duration(durationRatio, bufferSize);
39 const auto tick_dur = t.physical_write_duration(durationRatio);
40 _.samples_to_write = std::min(tick_dur, std::max<int64_t>(0, room));
46perform_upmix(
const std::size_t upmix,
const std::size_t chan, ossia::audio_port& ap)
79 for(std::size_t chan = 1; chan < upmix; ++chan)
80 ap.channel(chan).assign(ap.channel(0).begin(), ap.channel(0).end());
91inline void perform_start_offset(
const std::size_t start, ossia::audio_port& ap)
95 ap.get().insert(ap.get().begin(), start, ossia::audio_channel{});
108 ~at_end() { func(); }
116 RubberbandStretcher = 1,
119 [[nodiscard]] int64_t next_sample_to_read() const noexcept
122 [](
auto& stretcher)
noexcept {
return stretcher.next_sample_to_read; },
127 [[nodiscard]] int64_t start_delay() const noexcept
130 [](
auto& stretcher)
noexcept -> int64_t {
return stretcher.start_delay(); },
134 void transport(int64_t date)
137 [=](
auto& stretcher)
noexcept {
return stretcher.transport(date); }, m_stretch);
141 int64_t date, ossia::audio_stretch_mode mode, std::size_t channels,
142 std::size_t fileSampleRate)
148 case ossia::audio_stretch_mode::None: {
149 if(
auto s = ossia::get_if<RawStretcher>(&m_stretch))
155 m_stretch.emplace<RawStretcher>(date);
160#if defined(OSSIA_ENABLE_RUBBERBAND)
161 case ossia::audio_stretch_mode::RubberBandStandard:
162 case ossia::audio_stretch_mode::RubberBandPercussive:
163 case ossia::audio_stretch_mode::RubberBandStandardHQ:
164 case ossia::audio_stretch_mode::RubberBandPercussiveHQ: {
165 const auto preset = get_rubberband_preset(mode);
166 if(
auto s = ossia::get_if<RubberbandStretcher>(&m_stretch);
167 s && s->options == preset)
173 m_stretch.emplace<rubberband_stretcher>(
174 preset, channels, fileSampleRate, date);
180#if defined(OSSIA_ENABLE_LIBSAMPLERATE)
181 case ossia::audio_stretch_mode::Repitch:
182 case ossia::audio_stretch_mode::RepitchMediumQ:
183 case ossia::audio_stretch_mode::RepitchFastestQ: {
184 const auto preset = get_samplerate_preset(mode);
185 if(
auto s = ossia::get_if<RepitchStretcher>(&m_stretch);
186 s && s->repitchers.size() == channels && s->preset == preset)
193 m_stretch.emplace<repitch_stretcher>(preset, channels, 1024, date);
201 template <
typename T>
203 run(T& audio_fetcher,
const ossia::token_request& t, ossia::exec_state_facade e,
204 double tempo_ratio, std::size_t chan, std::size_t len, int64_t samples_to_read,
205 int64_t samples_to_write, int64_t samples_offset,
206 const ossia::mutable_audio_span<double>& ap)
209 [&](
auto& stretcher) {
211 audio_fetcher, t, e, tempo_ratio, chan, len, samples_to_read, samples_to_write,
217 [[nodiscard]]
bool stretch() const noexcept {
return m_stretch.index() != 0; }
222#if defined(OSSIA_ENABLE_RUBBERBAND)
226#if defined(OSSIA_ENABLE_LIBSAMPLERATE)
234struct sound_processing_info
236 time_value m_prev_date{time_value::infinite_min};
238 time_value m_loop_duration{};
239 time_value m_start_offset{};
243 int64_t m_loop_duration_samples{};
244 int64_t m_start_offset_samples{};
246 double m_last_stretch{1.0};
247 ossia::resampler m_resampler{};
254 m_loop_duration = loop_duration;
255 m_start_offset = start_offset;
259 void set_resampler(ossia::resampler&& r)
261 auto date = m_resampler.next_sample_to_read();
262 m_resampler = std::move(r);
263 m_resampler.transport(date);
266 void set_native_tempo(
double v) { tempo = v; }
285 [[nodiscard]] int64_t file_sample_for_model_time(
286 time_value date,
double timeline_tempo,
287 int file_sample_rate)
const noexcept
289 const int64_t base = to_sample(date, file_sample_rate);
290 if(m_resampler.stretch() && tempo > 0.0)
291 return int64_t(std::llround(
double(base) * ossia::root_tempo / tempo));
293 const double abs_tempo = std::abs(timeline_tempo);
295 return int64_t(std::llround(
double(base) * ossia::root_tempo / abs_tempo));
299 double update_stretch(
300 const ossia::token_request& t,
const ossia::exec_state_facade& e)
noexcept
302 double stretch_ratio = 1.;
303 double model_ratio = 1.;
304 if(tempo != 0. && m_resampler.stretch())
306 model_ratio = ossia::root_tempo / this->tempo;
307 stretch_ratio = this->tempo / t.tempo;
310 m_loop_duration_samples = m_loop_duration.impl * e.modelToSamples() * model_ratio;
311 m_start_offset_samples = m_start_offset.impl * e.modelToSamples() * model_ratio;
312 return stretch_ratio;
317 :
public ossia::nonowning_graph_node
318 ,
public sound_processing_info
321 virtual void transport(time_value date) = 0;
326 virtual void transport(
327 time_value date,
const ossia::tick_transport_info& transport_info)
329 (void)transport_info;
334class dummy_sound_node final :
public sound_node
337 ossia::audio_outlet audio_out;
341 m_outlets.push_back(&audio_out);
344 std::string label() const noexcept
override {
return "dummy_sound_node"; }
346 void transport(time_value date)
override { }
348 void run(
const ossia::token_request& t, ossia::exec_state_facade e)
noexcept override
353#if defined(OSSIA_SCENARIO_DATAFLOW)
354class sound_process final :
public ossia::node_process
357 using ossia::node_process::node_process;
360 void state(
const ossia::token_request& req)
override
365 static_cast<sound_node&
>(*this->node)
366 .set_loop_info(m_loop_duration, m_start_offset, m_loops);
374 void offset_impl(time_value date)
override
376 static_cast<sound_node&
>(*this->node).transport(date);
378 void transport_impl(time_value date)
override
380 static_cast<sound_node&
>(*this->node).transport(date);
383 time_value date,
const ossia::tick_transport_info& info)
override
385 static_cast<sound_node&
>(*this->node).transport(date, info);
The time_value class.
Definition ossia/editor/scenario/time_value.hpp:30