MayaFlux 0.1.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Depot.cpp
Go to the documentation of this file.
1#include "Depot.hpp"
3
11
13
14namespace fs = std::filesystem;
15
16namespace MayaFlux {
17
18std::shared_ptr<MayaFlux::Kakshya::SoundFileContainer> load_audio_file(const std::string& filepath)
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
28 reader->set_target_sample_rate(MayaFlux::Config::get_sample_rate());
29 reader->set_target_bit_depth(64);
30 reader->set_audio_options(IO::AudioReadOptions::DEINTERLEAVE);
31
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) {
56 std::vector<uint64_t> output_shape = { MayaFlux::Config::get_buffer_size(), sound_container->get_num_channels() };
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>();
65 std::vector<uint64_t> output_shape = { MayaFlux::Config::get_buffer_size(), sound_container->get_num_channels() };
66 processor->set_output_size(output_shape);
67 processor->set_auto_advance(true);
68
69 sound_container->set_default_processor(processor);
70 }
71
72 MF_INFO(
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}
83
84std::vector<std::shared_ptr<Buffers::ContainerBuffer>> hook_sound_container_to_buffers(const std::shared_ptr<MayaFlux::Kakshya::SoundFileContainer>& container)
85{
86 auto buffer_manager = MayaFlux::get_buffer_manager();
87 uint32_t num_channels = container->get_num_channels();
88 std::vector<std::shared_ptr<Buffers::ContainerBuffer>> created_buffers;
89
93 "Setting up audio playback for {} channels...",
94 num_channels);
95
96 for (uint32_t channel = 0; channel < num_channels; ++channel) {
97 auto container_buffer = buffer_manager->create_audio_buffer<MayaFlux::Buffers::ContainerBuffer>(
99 channel,
100 container,
101 channel);
102
103 container_buffer->initialize();
104
105 created_buffers.push_back(std::move(container_buffer));
106
107 MF_INFO(
110 "✓ Created buffer for channel {}",
111 channel);
112 }
113
114 return created_buffers;
115}
116
117std::shared_ptr<Buffers::TextureBuffer> load_image_file(const std::string& filepath)
118{
119 IO::ImageReader reader;
120
121 if (!reader.open(filepath)) {
123 "Failed to open image: {}", filepath);
124 return nullptr;
125 }
126
127 auto texture_buffer = reader.create_texture_buffer();
128
129 if (!texture_buffer) {
131 "Failed to create texture buffer from: {}", filepath);
132 return nullptr;
133 }
134
136 "Loaded image: {} ({}x{})",
137 fs::path(filepath).filename().string(),
138 texture_buffer->get_width(),
139 texture_buffer->get_height());
140
141 return texture_buffer;
142}
143
144bool is_image(const fs::path& filepath)
145{
146 if (!fs::exists(filepath) || !fs::is_regular_file(filepath)) {
147 return false;
148 }
149
150 auto ext = filepath.extension().string();
151 std::ranges::transform(ext, ext.begin(),
152 [](unsigned char c) { return std::tolower(c); });
153
154 static const std::unordered_set<std::string> image_extensions = {
155 ".png", ".jpg", ".jpeg", ".bmp", ".tga",
156 ".psd", ".gif", ".hdr", ".pic", ".pnm"
157 };
158
159 return image_extensions.contains(ext);
160}
161
162bool is_audio(const fs::path& filepath)
163{
164 if (!fs::exists(filepath) || !fs::is_regular_file(filepath)) {
165 return false;
166 }
167
168 auto ext = filepath.extension().string();
169 std::ranges::transform(ext, ext.begin(),
170 [](unsigned char c) { return std::tolower(c); });
171
172 static const std::unordered_set<std::string> audio_extensions = {
173 ".wav", ".aiff", ".aif", ".flac", ".ogg",
174 ".mp3", ".m4a", ".wma"
175 };
176
177 return audio_extensions.contains(ext);
178}
179
180}
#define MF_INFO(comp, ctx,...)
#define MF_ERROR(comp, ctx,...)
#define MF_TRACE(comp, ctx,...)
#define MF_DEBUG(comp, ctx,...)
Audio file loading and container management API.
void initialize()
Initialize the buffer after construction.
AudioBuffer implementation backed by a StreamContainer.
std::shared_ptr< Buffers::TextureBuffer > create_texture_buffer()
Create a VKBuffer containing the loaded image pixel data.
bool open(const std::string &filepath, FileReadOptions options=FileReadOptions::ALL) override
Open a file for reading.
File reader for image formats (PNG, JPG, BMP, TGA, etc.)
static void initialize_ffmpeg()
Initialize FFmpeg libraries (thread-safe, called automatically).
@ AUDIO_BACKEND
Standard audio processing backend configuration.
uint32_t get_buffer_size()
Gets the buffer size from the default engine.
Definition Config.cpp:51
uint32_t get_sample_rate()
Gets the sample rate from the default engine.
Definition Config.cpp:46
FileReadOptions
Generic options for file reading behavior.
@ EXTRACT_METADATA
Extract file metadata.
@ ContainerProcessing
Container operations (Kakshya - file/stream/region processing)
@ BufferManagement
Buffer Management (Buffers::BufferManager, creating buffers)
@ FileIO
Filesystem I/O operations.
@ Runtime
General runtime operations (default fallback)
@ API
MayaFlux/API Wrapper and convenience functions.
@ ROW_MAJOR
C/C++ style (last dimension varies fastest)
std::shared_ptr< MayaFlux::Kakshya::SoundFileContainer > load_audio_file(const std::string &filepath)
Loads an audio file into a SoundFileContainer with automatic format detection.
Definition Depot.cpp:18
bool is_image(const fs::path &filepath)
Definition Depot.cpp:144
bool is_audio(const fs::path &filepath)
Definition Depot.cpp:162
std::shared_ptr< Buffers::TextureBuffer > load_image_file(const std::string &filepath)
Loads an image file into a TextureBuffer.
Definition Depot.cpp:117
std::shared_ptr< Buffers::BufferManager > get_buffer_manager()
Gets the buffer manager from the default engine.
Definition Graph.cpp:81
std::vector< std::shared_ptr< Buffers::ContainerBuffer > > hook_sound_container_to_buffers(const std::shared_ptr< MayaFlux::Kakshya::SoundFileContainer > &container)
Connects a SoundFileContainer to the buffer system for immediate playback.
Definition Depot.cpp:84
Main namespace for the Maya Flux audio engine.
Definition LiveAid.hpp:6