Loads an audio file into a SoundFileContainer with automatic format detection.
Provides comprehensive audio file loading with format detection, sample rate conversion, and bit depth optimization. Supports all FFmpeg formats including WAV, MP3, FLAC, OGG, etc. The container is immediately ready for use with buffer system integration. Returns nullptr on failure with error details logged to stderr.
19{
20 auto reader = std::make_unique<IO::SoundFileReader>();
22
23 if (!reader->can_read(filepath)) {
24 std::cerr << "Cannot read file: " << filepath << '\n';
25 return nullptr;
26 }
27
29 reader->set_target_bit_depth(64);
30 reader->set_audio_options(IO::AudioReadOptions::DEINTERLEAVE);
31
32 IO::FileReadOptions options = IO::FileReadOptions::EXTRACT_METADATA;
33 if (!reader->open(filepath, options)) {
34 MF_ERROR(Journal::Component::API, Journal::Context::FileIO,
"Failed to open file: {}", reader->get_last_error());
35 return nullptr;
36 }
37
38 auto container = reader->create_container();
39 auto sound_container = std::dynamic_pointer_cast<Kakshya::SoundFileContainer>(container);
40 if (!sound_container) {
41 MF_ERROR(Journal::Component::API, Journal::Context::Runtime,
"Failed to create sound container");
42 return nullptr;
43 }
44
45 sound_container->set_memory_layout(Kakshya::MemoryLayout::ROW_MAJOR);
46
47 if (!reader->load_into_container(sound_container)) {
48 MF_ERROR(Journal::Component::API, Journal::Context::Runtime,
"Failed to load audio data: {}", reader->get_last_error());
49 return nullptr;
50 }
51
52 auto existing_processor = std::dynamic_pointer_cast<Kakshya::ContiguousAccessProcessor>(
53 sound_container->get_default_processor());
54
55 if (existing_processor) {
57 existing_processor->set_output_size(output_shape);
58 existing_processor->set_auto_advance(true);
59
60 MF_DEBUG(Journal::Component::API, Journal::Context::ContainerProcessing,
"Configured existing ContiguousAccessProcessor");
61 } else {
62 MF_TRACE(Journal::Component::API, Journal::Context::ContainerProcessing,
"No default processor found, creating a new ContiguousAccessProcessor");
63
64 auto processor = std::make_shared<Kakshya::ContiguousAccessProcessor>();
66 processor->set_output_size(output_shape);
67 processor->set_auto_advance(true);
68
69 sound_container->set_default_processor(processor);
70 }
71
73 Journal::Component::API,
74 Journal::Context::FileIO,
75 "Loaded audio file: {} | Channels: {} | Frames: {} | Sample Rate: {} Hz",
76 filepath,
77 sound_container->get_num_channels(),
78 sound_container->get_num_frames(),
79 sound_container->get_sample_rate());
80
81 return sound_container;
82}
#define MF_INFO(comp, ctx,...)
#define MF_ERROR(comp, ctx,...)
#define MF_TRACE(comp, ctx,...)
#define MF_DEBUG(comp, ctx,...)
static void initialize_ffmpeg()
Initialize FFmpeg libraries (thread-safe, called automatically).
uint32_t get_buffer_size()
Gets the buffer size from the default engine.
uint32_t get_sample_rate()
Gets the sample rate from the default engine.