OSSIA
Open Scenario System for Interactive Application
Loading...
Searching...
No Matches
sound_mmap.hpp
1#pragma once
2#include <ossia/audio/audio_parameter.hpp>
3#include <ossia/audio/drwav_handle.hpp>
4#include <ossia/dataflow/audio_stretch_mode.hpp>
5#include <ossia/dataflow/graph_node.hpp>
6#include <ossia/dataflow/nodes/media.hpp>
7#include <ossia/dataflow/nodes/sound.hpp>
8#include <ossia/dataflow/port.hpp>
9#include <ossia/detail/pod_vector.hpp>
10
11#include <type_traits>
12
13namespace ossia::nodes
14{
15
16class sound_mmap final : public ossia::sound_node
17{
18public:
19 sound_mmap() { m_outlets.push_back(&audio_out); }
20
21 ~sound_mmap() = default;
22
23 std::string label() const noexcept override { return "sound_mmap"; }
24
25 void set_start(std::size_t v) { start = v; }
26
27 void set_upmix(std::size_t v) { upmix = v; }
28
29 void set_sound(drwav_handle hdl)
30 {
31 using namespace snd;
32 m_handle = std::move(hdl);
33 if(m_handle)
34 {
35 switch(m_handle.translatedFormatTag())
36 {
37 case DR_WAVE_FORMAT_PCM: {
38 switch(m_handle.bitsPerSample())
39 {
40 case 8:
41 m_converter = read_u8;
42 break;
43 case 16:
44 m_converter = read_s16;
45 break;
46 case 24:
47 m_converter = read_s24;
48 break;
49 case 32:
50 m_converter = read_s32;
51 break;
52 }
53 break;
54 }
55 case DR_WAVE_FORMAT_IEEE_FLOAT: {
56 switch(m_handle.bitsPerSample())
57 {
58 case 32:
59 m_converter = read_f32;
60 break;
61 case 64:
62 m_converter = read_f64;
63 break;
64 }
65 break;
66 }
67 default:
68 m_converter = nullptr;
69 break;
70 }
71 }
72 }
73
74 void transport(time_value date) override
75 {
76 if(m_handle)
77 m_resampler.transport(to_sample(date, m_handle.sampleRate()));
78 }
79
80 void fetch_audio(
81 int64_t start, int64_t samples_to_write, double** audio_array_base) noexcept
82 {
83 const int channels = this->channels();
84 const int file_duration = this->duration();
85
86 m_resampleBuffer.resize(channels);
87 for(auto& buf : m_resampleBuffer)
88 buf.resize(samples_to_write);
89
90 float** audio_array = (float**)alloca(sizeof(float*) * channels);
91 for(int i = 0; i < channels; i++)
92 {
93 m_resampleBuffer[i].resize(samples_to_write);
94 audio_array[i] = m_resampleBuffer[i].data();
95 }
96
97 ossia::mutable_audio_span<float> source(channels);
98
99 double* frame_data{};
100 if(samples_to_write * channels > 10000)
101 {
102 m_safetyBuffer.resize(samples_to_write * channels);
103 frame_data = m_safetyBuffer.data();
104 // TODO detect if we happen to be in this case often, and if so, garbage
105 // collect at some point
106 }
107 else
108 {
109 frame_data = (double*)alloca(sizeof(double) * samples_to_write * channels);
110 }
111
112 if(m_loops)
113 {
114 for(int k = 0; k < samples_to_write; k++)
115 {
116 // TODO add a special case if [0; samples_to_write] don't loop around
117 int pos = this->m_start_offset_samples
118 + ((start + k) % this->m_loop_duration_samples);
119 if(pos >= file_duration)
120 {
121 for(int i = 0; i < channels; i++)
122 audio_array[i][k] = 0;
123 continue;
124 }
125
126 const bool ok = this->m_handle.seek_to_pcm_frame(pos);
127 if(!ok)
128 {
129 for(int i = 0; i < channels; i++)
130 audio_array[i][k] = 0;
131 continue;
132 }
133
134 const int max = 1;
135 const auto count = this->m_handle.read_pcm_frames(max, frame_data);
136 if(count >= 0)
137 {
138 for(int i = 0; i < channels; i++)
139 source[i] = std::span(audio_array[i] + k, count);
140 m_converter(source, frame_data, count);
141 }
142 else
143 {
144 for(int i = 0; i < channels; i++)
145 audio_array[i][k] = 0;
146 }
147 }
148 }
149 else
150 {
151 for(int i = 0; i < channels; i++)
152 {
153 source[i] = std::span(audio_array[i], samples_to_write);
154 }
155
156 bool ok = start + m_start_offset_samples < file_duration;
157 if(ok)
158 ok = ok && this->m_handle.seek_to_pcm_frame(start + m_start_offset_samples);
159
160 if(ok)
161 {
162 const auto count = this->m_handle.read_pcm_frames(samples_to_write, frame_data);
163 m_converter(source, frame_data, count);
164 for(int i = 0; i < channels; i++)
165 for(int k = count; k < samples_to_write; k++)
166 audio_array[i][k] = 0;
167 }
168 else
169 {
170 for(int i = 0; i < channels; i++)
171 for(int k = 0; k < samples_to_write; k++)
172 audio_array[i][k] = 0;
173 return;
174 }
175 }
176
177 for(int i = 0; i < channels; i++)
178 std::copy_n(audio_array[i], samples_to_write, audio_array_base[i]);
179 }
180
181 void fetch_audio(int64_t start, int64_t samples_to_write, float** audio_array) noexcept
182 {
183 const int channels = this->channels();
184 const int file_duration = this->duration();
185
186 ossia::mutable_audio_span<float> source(channels);
187
188 double* frame_data{};
189 if(samples_to_write * channels > 10000)
190 {
191 m_safetyBuffer.resize(samples_to_write * channels);
192 frame_data = m_safetyBuffer.data();
193 // TODO detect if we happen to be in this case often, and if so, garbage
194 // collect at some point
195 }
196 else
197 {
198 frame_data = (double*)alloca(sizeof(double) * samples_to_write * channels);
199 }
200
201 if(m_loops)
202 {
203 for(int k = 0; k < samples_to_write; k++)
204 {
205 // TODO add a special case if [0; samples_to_write] don't loop around
206 int pos = this->m_start_offset_samples
207 + ((start + k) % this->m_loop_duration_samples);
208 if(pos >= file_duration)
209 {
210 for(int i = 0; i < channels; i++)
211 audio_array[i][k] = 0;
212 continue;
213 }
214
215 const bool ok = this->m_handle.seek_to_pcm_frame(pos);
216 if(!ok)
217 {
218 for(int i = 0; i < channels; i++)
219 audio_array[i][k] = 0;
220 continue;
221 }
222
223 const int max = 1;
224 const auto count = this->m_handle.read_pcm_frames(max, frame_data);
225 if(count >= 0)
226 {
227 for(int i = 0; i < channels; i++)
228 source[i] = std::span(audio_array[i] + k, count);
229 m_converter(source, frame_data, count);
230 }
231 else
232 {
233 for(int i = 0; i < channels; i++)
234 audio_array[i][k] = 0;
235 }
236 }
237 }
238 else
239 {
240 for(int i = 0; i < channels; i++)
241 {
242 source[i] = std::span(audio_array[i], samples_to_write);
243 }
244
245 const bool ok = this->m_handle.seek_to_pcm_frame(start + m_start_offset_samples);
246 if(!ok)
247 {
248 for(int i = 0; i < channels; i++)
249 for(int k = 0; k < samples_to_write; k++)
250 audio_array[i][k] = 0;
251 return;
252 }
253
254 const auto count = this->m_handle.read_pcm_frames(samples_to_write, frame_data);
255 m_converter(source, frame_data, count);
256 for(int i = 0; i < channels; i++)
257 for(int k = count; k < samples_to_write; k++)
258 audio_array[i][k] = 0;
259 }
260 }
261
262 void run(const ossia::token_request& t, ossia::exec_state_facade e) noexcept override
263 {
264 if(!m_handle || !m_converter)
265 return;
266
267 // TODO do the backwards play head
268 if(!t.forward())
269 return;
270
271 const auto channels = m_handle.channels();
272 const auto len = m_handle.totalPCMFrameCount();
273
274 ossia::audio_port& ap = *audio_out;
275 ap.set_channels(std::max((std::size_t)upmix, (std::size_t)channels));
276
277 const auto [samples_to_read, samples_to_write]
278 = snd::sample_info(e.bufferSize(), e.modelToSamples(), t);
279 if(samples_to_write <= 0)
280 return;
281
282 assert(samples_to_write > 0);
283
284 const auto samples_offset = t.physical_start(e.modelToSamples());
285 if(t.tempo > 0)
286 {
287 if(t.prev_date < m_prev_date)
288 {
289 // Sentinel: we never played.
290 if(m_prev_date == ossia::time_value{ossia::time_value::infinite_min})
291 {
292 if(t.prev_date != 0_tv)
293 {
294 transport(t.prev_date);
295 }
296 else
297 {
298 // Otherwise we don't need transport, everything is already at 0
299 m_prev_date = 0_tv;
300 }
301 }
302 else
303 {
304 transport(t.prev_date);
305 }
306 }
307
308 for(std::size_t chan = 0; chan < channels; chan++)
309 {
310 ap.channel(chan).resize(e.bufferSize());
311 }
312
313 double stretch_ratio = update_stretch(t, e);
314
315 // Resample
316 m_resampler.run(
317 *this, t, e, stretch_ratio, channels, len, samples_to_read, samples_to_write,
318 samples_offset, ap);
319
320 for(std::size_t chan = 0; chan < channels; chan++)
321 {
322 // fade
323 snd::do_fade(
324 t.start_discontinuous, t.end_discontinuous, ap.channel(chan), samples_offset,
325 samples_to_write);
326 }
327
328 ossia::snd::perform_upmix(this->upmix, channels, ap);
329 ossia::snd::perform_start_offset(this->start, ap);
330
331 m_prev_date = t.date;
332 }
333 else
334 {
335 /* TODO */
336 }
337 }
338
339 [[nodiscard]] std::size_t channels() const
340 {
341 return m_handle ? m_handle.channels() : 0;
342 }
343 [[nodiscard]] std::size_t duration() const
344 {
345 return m_handle ? m_handle.totalPCMFrameCount() : 0;
346 }
347
348private:
349 drwav_handle m_handle{};
350
351 ossia::audio_outlet audio_out;
352
353 std::size_t start{};
354 std::size_t upmix{};
355
356 using read_fn_t
357 = void (*)(ossia::mutable_audio_span<float>& ap, void* data, int64_t samples);
358 read_fn_t m_converter{};
359 std::vector<double> m_safetyBuffer;
360 std::vector<std::vector<float>> m_resampleBuffer;
361};
362
363}
OSSIA_INLINE constexpr auto max(const T a, const U b) noexcept -> typename std::conditional<(sizeof(T) > sizeof(U)), T, U >::type
max function tailored for values
Definition math.hpp:96
The time_value class.
Definition ossia/editor/scenario/time_value.hpp:30