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; },
122 [[nodiscard]] int64_t start_delay() const noexcept
125 [](
auto& stretcher)
noexcept -> int64_t {
return stretcher.start_delay(); },
129 void transport(int64_t date)
132 [=](
auto& stretcher)
noexcept {
return stretcher.transport(date); }, m_stretch);
136 int64_t date, ossia::audio_stretch_mode mode, std::size_t channels,
137 std::size_t fileSampleRate)
143 case ossia::audio_stretch_mode::None: {
144 if(
auto s = ossia::get_if<RawStretcher>(&m_stretch))
150 m_stretch.emplace<RawStretcher>(date);
155#if defined(OSSIA_ENABLE_RUBBERBAND)
156 case ossia::audio_stretch_mode::RubberBandStandard:
157 case ossia::audio_stretch_mode::RubberBandPercussive:
158 case ossia::audio_stretch_mode::RubberBandStandardHQ:
159 case ossia::audio_stretch_mode::RubberBandPercussiveHQ: {
160 const auto preset = get_rubberband_preset(mode);
161 if(
auto s = ossia::get_if<RubberbandStretcher>(&m_stretch);
162 s && s->options == preset)
168 m_stretch.emplace<rubberband_stretcher>(
169 preset, channels, fileSampleRate, date);
175#if defined(OSSIA_ENABLE_LIBSAMPLERATE)
176 case ossia::audio_stretch_mode::Repitch:
177 case ossia::audio_stretch_mode::RepitchMediumQ:
178 case ossia::audio_stretch_mode::RepitchFastestQ: {
179 const auto preset = get_samplerate_preset(mode);
180 if(
auto s = ossia::get_if<RepitchStretcher>(&m_stretch);
181 s && s->repitchers.size() == channels && s->preset == preset)
188 m_stretch.emplace<repitch_stretcher>(preset, channels, 1024, date);
196 template <
typename T>
198 run(T& audio_fetcher,
const ossia::token_request& t, ossia::exec_state_facade e,
199 double tempo_ratio, std::size_t chan, std::size_t len, int64_t samples_to_read,
200 int64_t samples_to_write, int64_t samples_offset,
201 const ossia::mutable_audio_span<double>& ap)
204 [&](
auto& stretcher) {
206 audio_fetcher, t, e, tempo_ratio, chan, len, samples_to_read, samples_to_write,
212 [[nodiscard]]
bool stretch() const noexcept {
return m_stretch.index() != 0; }
217#if defined(OSSIA_ENABLE_RUBBERBAND)
221#if defined(OSSIA_ENABLE_LIBSAMPLERATE)
229struct sound_processing_info
231 time_value m_prev_date{time_value::infinite_min};
233 time_value m_loop_duration{};
234 time_value m_start_offset{};
238 int64_t m_loop_duration_samples{};
239 int64_t m_start_offset_samples{};
241 double m_last_stretch{1.0};
242 ossia::resampler m_resampler{};
249 m_loop_duration = loop_duration;
250 m_start_offset = start_offset;
254 void set_resampler(ossia::resampler&& r)
256 auto date = m_resampler.next_sample_to_read();
257 m_resampler = std::move(r);
258 m_resampler.transport(date);
261 void set_native_tempo(
double v) { tempo = v; }
266 [[nodiscard]] int64_t file_sample_for_model_time(
267 time_value date,
double timeline_tempo,
268 int file_sample_rate)
const noexcept
270 const int64_t base = to_sample(date, file_sample_rate);
271 const double abs_tempo = std::abs(timeline_tempo);
272 if(!m_resampler.stretch() || tempo <= 0.0 || abs_tempo <= 0.0)
275 return int64_t(
double(base) * abs_tempo / tempo);
278 double update_stretch(
279 const ossia::token_request& t,
const ossia::exec_state_facade& e)
noexcept
281 double stretch_ratio = 1.;
282 double model_ratio = 1.;
285 if(m_resampler.stretch())
287 model_ratio = ossia::root_tempo / this->tempo;
288 stretch_ratio = this->tempo / t.tempo;
292 model_ratio = ossia::root_tempo / t.tempo;
296 m_loop_duration_samples = m_loop_duration.impl * e.modelToSamples() * model_ratio;
297 m_start_offset_samples = m_start_offset.impl * e.modelToSamples() * model_ratio;
298 return stretch_ratio;
303 :
public ossia::nonowning_graph_node
304 ,
public sound_processing_info
307 virtual void transport(time_value date) = 0;
312 virtual void transport(
313 time_value date,
const ossia::tick_transport_info& transport_info)
315 (void)transport_info;
320class dummy_sound_node final :
public sound_node
323 ossia::audio_outlet audio_out;
327 m_outlets.push_back(&audio_out);
330 std::string label() const noexcept
override {
return "dummy_sound_node"; }
332 void transport(time_value date)
override { }
334 void run(
const ossia::token_request& t, ossia::exec_state_facade e)
noexcept override
339#if defined(OSSIA_SCENARIO_DATAFLOW)
340class sound_process final :
public ossia::node_process
343 using ossia::node_process::node_process;
346 void state(
const ossia::token_request& req)
override
351 static_cast<sound_node&
>(*this->node)
352 .set_loop_info(m_loop_duration, m_start_offset, m_loops);
360 void offset_impl(time_value date)
override
362 static_cast<sound_node&
>(*this->node).transport(date);
364 void transport_impl(time_value date)
override
366 static_cast<sound_node&
>(*this->node).transport(date);
369 time_value date,
const ossia::tick_transport_info& info)
override
371 static_cast<sound_node&
>(*this->node).transport(date, info);
The time_value class.
Definition ossia/editor/scenario/time_value.hpp:30