MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches

◆ load_bounded()

std::shared_ptr< Kakshya::DynamicSoundStream > MayaFlux::IO::SoundFileReader::load_bounded ( const std::string &  filepath,
uint64_t  max_frames = 0,
bool  truncate = false 
)

Load an audio file into a size-bounded DynamicSoundStream.

Opens the file, reads up to max_frames frames into a fully resident DynamicSoundStream with auto-resize disabled, then closes the file. No DataProcessor is configured on the result; that is the caller's responsibility (see IOManager::configure_audio_processor).

Parameters
filepathPath to the audio file.
max_framesUpper bound on frame count. Defaults to 5 seconds at the target sample rate.
truncateIf true, silently truncate files that exceed max_frames with an MF_WARN. If false, return nullptr on excess.
Returns
Populated DynamicSoundStream, or nullptr on failure.

Definition at line 289 of file SoundFileReader.cpp.

293{
294 if (!can_read(filepath)) {
295 set_error("load_bounded: unsupported file: " + filepath);
296 return nullptr;
297 }
298
299 if (!open(filepath, FileReadOptions::EXTRACT_METADATA)) {
300 set_error("load_bounded: open failed: " + get_last_error());
301 return nullptr;
302 }
303
304 std::shared_ptr<AudioStreamContext> audio;
305 {
306 std::shared_lock lock(m_context_mutex);
307 audio = m_audio;
308 }
309
310 if (!audio || !audio->is_valid()) {
311 set_error("load_bounded: no valid audio stream");
312 close();
313 return nullptr;
314 }
315
316 const uint64_t effective_rate = m_target_sample_rate > 0
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) {
326 set_error(std::format(
327 "load_bounded: file has {} frames, exceeds limit of {}",
328 audio->total_frames, max_frames));
329 close();
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
354 ? read_frames(frames_to_load, 0)
355 : read_all();
356
357 close();
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)

References can_read(), close(), MayaFlux::IO::DEINTERLEAVE, MayaFlux::IO::EXTRACT_METADATA, MayaFlux::Journal::FileIO, get_last_error(), MayaFlux::Kakshya::INTERLEAVED, MayaFlux::Journal::IO, m_audio, m_audio_options, m_context_mutex, m_target_sample_rate, MF_WARN, MayaFlux::IO::NONE, open(), MayaFlux::Kakshya::PLANAR, read_all(), read_frames(), and set_error().

+ Here is the call graph for this function: