OSSIA
Open Scenario System for Interactive Application
Loading...
Searching...
No Matches
sound.hpp
1#pragma once
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>
13
14namespace ossia
15{
16namespace snd
17{
18struct sample_read_info
19{
20 int64_t samples_to_read{};
21 int64_t samples_to_write{};
22};
23
24inline auto
25sample_info(int64_t bufferSize, double durationRatio, const ossia::token_request& t)
26{
27 sample_read_info _;
28 if(t.paused())
29 return _;
30
31 if(t.speed == 0.0)
32 return _;
33
34 _.samples_to_read = t.physical_read_duration(durationRatio);
35 _.samples_to_write = t.safe_physical_write_duration(durationRatio, bufferSize);
36
37 return _;
38}
39
40inline void
41perform_upmix(const std::size_t upmix, const std::size_t chan, ossia::audio_port& ap)
42{
43 // Upmix
44 if(upmix != 0)
45 {
46 if(upmix < chan)
47 {
48 /* TODO
49 // Downmix
50 switch(upmix)
51 {
52 case 1:
53 {
54 for(std::size_t i = 1; i < chan; i++)
55 {
56 if(ap.channel(0).size() < ap.channel(i).size())
57 ap.channel(0).resize(ap.channel(i).size());
58
59 for(std::size_t j = 0; j < ap.channel(i).size(); j++)
60 ap.channel(0)[j] += ap.channel(i)[j];
61 }
62 }
63 default:
64 // TODO
65 break;
66 }
67 */
68 }
69 else if(upmix > chan)
70 {
71 switch(chan)
72 {
73 case 1: {
74 for(std::size_t chan = 1; chan < upmix; ++chan)
75 ap.channel(chan).assign(ap.channel(0).begin(), ap.channel(0).end());
76 break;
77 }
78 default:
79 // TODO
80 break;
81 }
82 }
83 }
84}
85
86inline void perform_start_offset(const std::size_t start, ossia::audio_port& ap)
87{
88 if(start != 0)
89 {
90 ap.get().insert(ap.get().begin(), start, ossia::audio_channel{});
91 }
92}
93}
94
95template <typename T>
96struct at_end
97{
98 T func;
99 at_end(T t)
100 : func{t}
101 {
102 }
103 ~at_end() { func(); }
104};
105
106struct resampler
107{
108 enum
109 {
110 RawStretcher = 0,
111 RubberbandStretcher = 1,
112 RepitchStretcher = 2
113 };
114 [[nodiscard]] int64_t next_sample_to_read() const noexcept
115 {
116 return ossia::visit(
117 [](auto& stretcher) noexcept { return stretcher.next_sample_to_read; },
118 m_stretch);
119 }
120
121 void transport(int64_t date)
122 {
123 ossia::visit(
124 [=](auto& stretcher) noexcept { return stretcher.transport(date); }, m_stretch);
125 }
126
127 void reset(
128 int64_t date, ossia::audio_stretch_mode mode, std::size_t channels,
129 std::size_t fileSampleRate)
130 {
131 // TODO use the date parameter to buffer ! else transport won't work
132 switch(mode)
133 {
134 default:
135 case ossia::audio_stretch_mode::None: {
136 if(auto s = ossia::get_if<RawStretcher>(&m_stretch))
137 {
138 s->transport(date);
139 }
140 else
141 {
142 m_stretch.emplace<RawStretcher>(date);
143 }
144 break;
145 }
146
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)
155 {
156 s->transport(date);
157 }
158 else
159 {
160 m_stretch.emplace<rubberband_stretcher>(
161 preset, channels, fileSampleRate, date);
162 }
163 break;
164 }
165#endif
166
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)
174 {
175 s->transport(date);
176 }
177 else
178 {
179 // FIXME why 1024 here ?!
180 m_stretch.emplace<repitch_stretcher>(preset, channels, 1024, date);
181 }
182 break;
183 }
184#endif
185 }
186 }
187
188 template <typename T>
189 void
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)
194 {
195 ossia::visit(
196 [&](auto& stretcher) {
197 stretcher.run(
198 audio_fetcher, t, e, tempo_ratio, chan, len, samples_to_read, samples_to_write,
199 samples_offset, ap);
200 },
201 m_stretch);
202 }
203
204 [[nodiscard]] bool stretch() const noexcept { return m_stretch.index() != 0; }
205
206private:
207 ossia::variant<
208 raw_stretcher
209#if defined(OSSIA_ENABLE_RUBBERBAND)
210 ,
211 rubberband_stretcher
212#endif
213#if defined(OSSIA_ENABLE_LIBSAMPLERATE)
214 ,
215 repitch_stretcher
216#endif
217 >
218 m_stretch;
219};
220
221struct sound_processing_info
222{
223 time_value m_prev_date{time_value::infinite_min};
224
225 time_value m_loop_duration{};
226 time_value m_start_offset{};
227
228 double tempo{};
229
230 int64_t m_loop_duration_samples{};
231 int64_t m_start_offset_samples{};
232
233 double m_last_stretch{1.0};
234 ossia::resampler m_resampler{};
235
236 bool m_loops{};
237
238 void set_loop_info(
239 ossia::time_value loop_duration, ossia::time_value start_offset, bool loops)
240 {
241 m_loop_duration = loop_duration;
242 m_start_offset = start_offset;
243 m_loops = loops;
244 }
245
246 void set_resampler(ossia::resampler&& r)
247 {
248 auto date = m_resampler.next_sample_to_read();
249 m_resampler = std::move(r);
250 m_resampler.transport(date);
251 }
252
253 void set_native_tempo(double v) { tempo = v; }
254
255 double update_stretch(
256 const ossia::token_request& t, const ossia::exec_state_facade& e) noexcept
257 {
258 double stretch_ratio = 1.;
259 double model_ratio = 1.;
260 if(tempo != 0.)
261 {
262 if(m_resampler.stretch())
263 {
264 model_ratio = ossia::root_tempo / this->tempo;
265 stretch_ratio = this->tempo / t.tempo;
266 }
267 else
268 {
269 model_ratio = ossia::root_tempo / t.tempo;
270 }
271 }
272
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;
276 }
277};
278
279class sound_node
280 : public ossia::nonowning_graph_node
281 , public sound_processing_info
282{
283public:
284 virtual void transport(time_value date) = 0;
285};
286
287class dummy_sound_node final : public sound_node
288{
289public:
290 ossia::audio_outlet audio_out;
291 dummy_sound_node()
292 {
293 // Add a dummy outlet so that interval can connect propagation to it
294 m_outlets.push_back(&audio_out);
295 }
296
297 std::string label() const noexcept override { return "dummy_sound_node"; }
298
299 void transport(time_value date) override { }
300
301 void run(const ossia::token_request& t, ossia::exec_state_facade e) noexcept override
302 {
303 }
304};
305
306#if defined(OSSIA_SCENARIO_DATAFLOW)
307class sound_process final : public ossia::node_process
308{
309public:
310 using ossia::node_process::node_process;
311
312protected:
313 void state(const ossia::token_request& req) override
314 {
315 // TODO here we should also pass the execution state so that we can
316 // leverage the timing info & transform loop_duration / start_offset in
317 // samples right here...
318 static_cast<sound_node&>(*this->node)
319 .set_loop_info(m_loop_duration, m_start_offset, m_loops);
320
321 // Start offset and looping are done manually inside the sound nodes
322 // since it is much more efficient in this case
323 // (see fetch_audio)
324 node->request(req);
325 }
326
327 void offset_impl(time_value date) override
328 {
329 static_cast<sound_node&>(*this->node).transport(date);
330 }
331 void transport_impl(time_value date) override
332 {
333 static_cast<sound_node&>(*this->node).transport(date);
334 }
335};
336#endif
337
338}
Definition git_info.h:7
The time_value class.
Definition ossia/editor/scenario/time_value.hpp:30