MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Depot.cpp
Go to the documentation of this file.
1#include "Depot.hpp"
3
7
11
14
17
19
20namespace fs = std::filesystem;
21
22namespace MayaFlux {
23
24namespace {
25
26 bool require_portal(const char* caller)
27 {
30 "{}: Portal::System not initialized", caller);
31 return false;
32 }
33 return true;
34 }
35
36 const std::vector<Portal::System::Dialog::ChooserFilter> k_audio_open_filters {
37 { .name = "Audio", .extensions = { "wav", "aiff", "aif", "flac", "ogg", "mp3", "m4a", "wma" } },
38 { .name = "All Files", .extensions = { "*" } }
39 };
40
41 const std::vector<Portal::System::Dialog::ChooserFilter> k_audio_save_filters {
42 { .name = "Audio", .extensions = { "wav", "flac", "ogg", "mp3" } },
43 { .name = "All Files", .extensions = { "*" } }
44 };
45
46 const std::vector<Portal::System::Dialog::ChooserFilter> k_video_filters {
47 { .name = "Video", .extensions = { "mp4", "mkv", "mov", "webm", "avi" } },
48 { .name = "All Files", .extensions = { "*" } }
49 };
50
51 const std::vector<Portal::System::Dialog::ChooserFilter> k_image_filters {
52 { .name = "Image", .extensions = { "png", "jpg", "jpeg", "bmp", "tga", "psd", "gif", "hdr", "pic", "pnm", "exr" } },
53 { .name = "All Files", .extensions = { "*" } }
54 };
55
56 const std::vector<Portal::System::Dialog::ChooserFilter> k_mesh_filters {
57 { .name = "3D Model", .extensions = { "glb", "gltf", "fbx", "obj", "ply", "stl", "dae" } },
58 { .name = "All Files", .extensions = { "*" } }
59 };
60
61 const std::vector<Portal::System::Dialog::ChooserFilter> k_image_save_filters {
62 { .name = "Image", .extensions = { "png", "jpg", "jpeg", "bmp", "tga", "exr", "hdr" } },
63 { .name = "All Files", .extensions = { "*" } }
64 };
65
66 bool check_extension(const fs::path& filepath, const Portal::System::Dialog::ChooserFilter& filter)
67 {
68 if (!fs::exists(filepath) || !fs::is_regular_file(filepath))
69 return false;
70
71 auto ext = filepath.extension().string();
72 if (ext.empty())
73 return false;
74
75 std::ranges::transform(ext, ext.begin(), [](unsigned char c) { return std::tolower(c); });
76 const std::string_view bare { ext.data() + 1, ext.size() - 1 }; // strip leading dot
77
78 return std::ranges::any_of(filter.extensions,
79 [&](const std::string& e) { return e == bare; });
80 }
81
82} // namespace
83
84// ---------------------------------------------------------------------------
85
86std::vector<std::shared_ptr<Buffers::SoundContainerBuffer>> prepare_audio_buffers(
87 const std::shared_ptr<Kakshya::SoundFileContainer>& container)
88{
89 if (!container) {
91 "prepare_audio_buffers failed: null container");
92 return {};
93 }
94
95 uint32_t num_channels = container->get_num_channels();
96 std::vector<std::shared_ptr<Buffers::SoundContainerBuffer>> created_buffers;
97
98 for (uint32_t ch = 0; ch < num_channels; ++ch) {
99 auto buf = std::make_shared<Buffers::SoundContainerBuffer>(
100 ch, Config::get_buffer_size(), container, ch);
101 buf->initialize();
102 created_buffers.push_back(std::move(buf));
103 }
104
105 return created_buffers;
106}
107
108bool is_image(const fs::path& filepath)
109{
110 return check_extension(filepath, k_image_filters[0]);
111}
112
113bool is_audio(const fs::path& filepath)
114{
115 return check_extension(filepath, k_audio_open_filters[0]);
116}
117
118// ---------------------------------------------------------------------------
119// Dialog-backed load
120// ---------------------------------------------------------------------------
121
122std::shared_ptr<Kakshya::SoundFileContainer> choose_audio()
123{
124 if (!require_portal("choose_audio"))
125 return nullptr;
126
127 return Portal::System::Dialog::open_file<std::shared_ptr<Kakshya::SoundFileContainer>>(
128 [](const fs::path& p) { return get_io_manager()->load_audio(p.string()); },
130 k_audio_open_filters);
131}
132
133std::shared_ptr<Kakshya::VideoFileContainer> choose_video()
134{
135 if (!require_portal("choose_video"))
136 return nullptr;
137
138 return Portal::System::Dialog::open_file<std::shared_ptr<Kakshya::VideoFileContainer>>(
139 [](const fs::path& p) { return get_io_manager()->load_video(p.string()); },
141 k_video_filters);
142}
143
144std::shared_ptr<Buffers::TextureBuffer> choose_image()
145{
146 if (!require_portal("choose_image"))
147 return nullptr;
148
149 return Portal::System::Dialog::open_file<std::shared_ptr<Buffers::TextureBuffer>>(
150 [](const fs::path& p) { return get_io_manager()->load_image(p.string()); },
152 k_image_filters);
153}
154
155std::vector<std::shared_ptr<Buffers::MeshBuffer>> choose_mesh()
156{
157 if (!require_portal("choose_mesh"))
158 return {};
159
160 return Portal::System::Dialog::open_file<std::vector<std::shared_ptr<Buffers::MeshBuffer>>>(
161 [](const fs::path& p) { return get_io_manager()->load_mesh(p.string()); },
163 k_mesh_filters);
164}
165
166std::shared_ptr<Nodes::Network::MeshNetwork> choose_mesh_network(IO::TextureResolver resolver)
167{
168 if (!require_portal("choose_mesh_network"))
169 return nullptr;
170
171 return Portal::System::Dialog::open_file<std::shared_ptr<Nodes::Network::MeshNetwork>>(
172 [r = std::move(resolver)](const fs::path& p) mutable {
173 return get_io_manager()->load_mesh_network(p.string(), std::move(r));
174 },
176 k_mesh_filters);
177}
178
179// ---------------------------------------------------------------------------
180// Dialog-backed save
181// ---------------------------------------------------------------------------
182
184 const std::shared_ptr<Kakshya::SoundStreamContainer>& container,
185 const std::string& suggested_name)
186{
187 if (!require_portal("save_audio"))
188 return false;
189
190 auto iom = get_io_manager();
191 if (!iom) {
193 "save_audio: IOManager unavailable");
194 return false;
195 }
196
197 return Portal::System::Dialog::save_file<bool>(
198 [&iom, &container](const fs::path& p) {
199 iom->write(container, p.string());
200 return true;
201 },
203 suggested_name,
204 k_audio_save_filters);
205}
206
208 const std::shared_ptr<Buffers::TextureBuffer>& buffer,
209 const std::string& suggested_name)
210{
211 if (!require_portal("save_image"))
212 return false;
213
214 auto iom = get_io_manager();
215 if (!iom) {
217 "save_image: IOManager unavailable");
218 return false;
219 }
220
221 return Portal::System::Dialog::save_file<bool>(
222 [&iom, &buffer](const fs::path& p) {
223 return iom->save_image(buffer, p.string(), IO::ImageWriteOptions {});
224 },
226 suggested_name,
227 k_image_save_filters);
228}
229
231 const std::shared_ptr<Buffers::TextureBuffer>& buffer,
232 const std::string& suggested_name,
233 const IO::ImageWriteOptions& options)
234{
235 if (!require_portal("save_image"))
236 return false;
237
238 auto iom = get_io_manager();
239 if (!iom) {
241 "save_image: IOManager unavailable");
242 return false;
243 }
244
245 return Portal::System::Dialog::save_file<bool>(
246 [&iom, &buffer, &options](const fs::path& p) {
247 return iom->save_image(buffer, p.string(), options);
248 },
250 suggested_name,
251 k_image_save_filters);
252}
253
254// ---------------------------------------------------------------------------
255
256std::shared_ptr<IO::IOManager> get_io_manager()
257{
259 return get_context().get_io_manager();
260
262 "Attempted to access IOManager before engine initialization");
263 return nullptr;
264}
265
266} // namespace MayaFlux
#define MF_ERROR(comp, ctx,...)
Core engine lifecycle and configuration API.
Audio file loading and container management API.
std::shared_ptr< IO::IOManager > get_io_manager()
Gets the IO manager.
Definition Engine.hpp:322
uint32_t get_buffer_size()
Gets the buffer size from the default engine.
Definition Config.cpp:114
SystemDialogError
Failure modes for OS dialog operations.
std::function< std::shared_ptr< Core::VKImage >(const std::string &)> TextureResolver
Callable that maps a raw material texture path to a GPU image.
Definition Depot.hpp:25
@ Runtime
General runtime operations (default fallback)
@ API
MayaFlux/API Wrapper and convenience functions.
Core::SystemFileFilter ChooserFilter
A named group of accepted file extensions.
Definition Chooser.hpp:27
bool is_initialized()
Return true if Portal::System has been initialized.
Definition System.cpp:85
bool is_engine_configured()
Checks if the default engine has currently accepted all configurations and initialized all managers.
Definition Config.cpp:52
bool is_image(const fs::path &filepath)
Definition Depot.cpp:108
std::shared_ptr< Kakshya::VideoFileContainer > choose_video()
Present a native open-file dialog filtered to video formats and load the chosen file via IOManager::l...
Definition Depot.cpp:133
bool save_image(const std::shared_ptr< Buffers::TextureBuffer > &buffer, const std::string &suggested_name)
Present a native save-file dialog filtered to image formats and save buffer to the chosen path via IO...
Definition Depot.cpp:207
std::shared_ptr< Nodes::Network::MeshNetwork > choose_mesh_network(IO::TextureResolver resolver)
Present a native open-file dialog filtered to 3D model formats and load the chosen file as a MeshNetw...
Definition Depot.cpp:166
bool is_audio(const fs::path &filepath)
Definition Depot.cpp:113
std::vector< std::shared_ptr< Buffers::MeshBuffer > > choose_mesh()
Present a native open-file dialog filtered to 3D model formats and load the chosen file via IOManager...
Definition Depot.cpp:155
std::shared_ptr< Kakshya::SoundFileContainer > choose_audio()
Present a native open-file dialog filtered to audio formats and load the chosen file via IOManager::l...
Definition Depot.cpp:122
bool save_audio(const std::shared_ptr< Kakshya::SoundStreamContainer > &container, const std::string &suggested_name)
Present a native save-file dialog and write container to the chosen path via IOManager::write().
Definition Depot.cpp:183
Core::Engine & get_context()
Gets the default engine instance.
Definition Core.cpp:68
std::shared_ptr< IO::IOManager > get_io_manager()
Retrieves the global IOManager instance for file loading and buffer management.
Definition Depot.cpp:256
std::shared_ptr< Buffers::TextureBuffer > choose_image()
Present a native open-file dialog filtered to image formats and load the chosen file via IOManager::l...
Definition Depot.cpp:144
std::vector< std::shared_ptr< Buffers::SoundContainerBuffer > > prepare_audio_buffers(const std::shared_ptr< Kakshya::SoundFileContainer > &container)
Constructs and initializes per-channel SoundContainerBuffers without registering them.
Definition Depot.cpp:86
Main namespace for the Maya Flux audio engine.
Definition Runtime.cpp:12
Configuration for image writing.