Load an audio file into a size-bounded DynamicSoundStream.
293{
295 set_error(
"load_bounded: unsupported file: " + filepath);
296 return nullptr;
297 }
298
301 return nullptr;
302 }
303
304 std::shared_ptr<AudioStreamContext> audio;
305 {
308 }
309
310 if (!audio || !audio->is_valid()) {
311 set_error(
"load_bounded: no valid audio stream");
313 return nullptr;
314 }
315
318 : audio->sample_rate;
319
320 if (max_frames == 0)
321 max_frames = effective_rate * 5;
322
323 const bool over_limit = audio->total_frames > max_frames;
324
325 if (over_limit && !truncate) {
327 "load_bounded: file has {} frames, exceeds limit of {}",
328 audio->total_frames, max_frames));
330 return nullptr;
331 }
332
333 if (over_limit) {
335 "load_bounded: {} has {} frames, truncating to {}",
336 filepath, audio->total_frames, max_frames);
337 }
338
339 const uint64_t frames_to_load = over_limit ? max_frames : audio->total_frames;
341
342 auto stream = std::make_shared<Kakshya::DynamicSoundStream>(
343 effective_rate,
344 static_cast<uint32_t>(audio->channels));
345
346 stream->get_structure().organization = planar
349
350 stream->set_auto_resize(false);
351 stream->ensure_capacity(frames_to_load);
352
353 auto data = over_limit
356
358
359 if (data.empty()) {
360 set_error(
"load_bounded: read returned no data");
361 return nullptr;
362 }
363
364 stream->set_all_data(data);
365
366 return stream;
367}
#define MF_WARN(comp, ctx,...)
std::vector< Kakshya::DataVariant > read_all() override
Read the entire audio file into memory.
void close() override
Close the currently open file and release resources.
std::string get_last_error() const override
Get the last error message encountered by the reader.
uint32_t m_target_sample_rate
Target sample rate for resampling (0 = use source rate).
bool open(const std::string &filepath, FileReadOptions options=FileReadOptions::ALL) override
Open an audio file for reading.
std::shared_ptr< AudioStreamContext > m_audio
Codec + resampler state.
std::shared_mutex m_context_mutex
Guards both context pointers.
bool can_read(const std::string &filepath) const override
Check if this reader can open the given file.
void set_error(const std::string &error) const
Set the last error message.
std::vector< Kakshya::DataVariant > read_frames(uint64_t num_frames, uint64_t offset=0)
Read a specific number of frames from the file.
AudioReadOptions m_audio_options
Audio-specific read options.
@ DEINTERLEAVE
Output planar (per-channel) doubles instead of interleaved.
@ EXTRACT_METADATA
Extract file metadata.
@ FileIO
Filesystem I/O operations.
@ IO
Networking, file handling, streaming.
@ PLANAR
Separate DataVariant per logical unit (LLL...RRR for stereo)
@ INTERLEAVED
Single DataVariant with interleaved data (LRLRLR for stereo)