Line data Source code
1 : #pragma once
2 :
3 : #include <fstream>
4 : #include <map>
5 : #include <memory>
6 : #include <nlohmann/json_fwd.hpp>
7 : #include <sstream>
8 :
9 : #include "audio/engine/i_audio_engine.h"
10 : #include "common.h"
11 : #include "midi/i_midi_manager.h"
12 : #include "presets/i_preset_manager.h"
13 :
14 : namespace Amplitron {
15 :
16 : class IPresetSerializer;
17 : class IPresetStorage;
18 : class IPresetMigrator;
19 :
20 : constexpr int CURRENT_PRESET_VERSION = 2;
21 :
22 247 : struct PresetData {
23 : std::string name;
24 : std::string description;
25 247 : float input_gain = 0.7f;
26 247 : float output_gain = 0.8f;
27 :
28 490 : struct EffectData {
29 : std::string type;
30 236 : bool enabled = false;
31 236 : float mix = 1.0f;
32 : std::vector<std::pair<std::string, float>> params;
33 : std::map<std::string, std::string> metadata;
34 : };
35 : std::vector<EffectData> effects;
36 : std::vector<MidiMapping> midi_mappings;
37 :
38 247 : std::string routing = "linear";
39 :
40 134 : struct NodeData {
41 : std::string id;
42 : std::string type;
43 98 : float x = 0.0f;
44 98 : float y = 0.0f;
45 98 : bool enabled = true;
46 98 : float mix = 1.0f;
47 98 : int num_inputs = 0;
48 : std::vector<std::pair<std::string, float>> params;
49 : std::map<std::string, std::string> metadata;
50 : };
51 : std::vector<NodeData> nodes;
52 :
53 139 : struct LinkData {
54 : std::string src_pin;
55 : std::string dst_pin;
56 : };
57 : std::vector<LinkData> links;
58 : };
59 :
60 : // Serialization helpers
61 : std::string to_json_ext(const PresetData& preset);
62 : bool from_json_ext(const std::string& json_str, PresetData& preset);
63 :
64 : // nlohmann ADL hooks for PresetData and nested EffectData.
65 : void to_json(nlohmann::json& j, const PresetData::EffectData& fx);
66 : void from_json(const nlohmann::json& j, PresetData::EffectData& fx);
67 : void to_json(nlohmann::json& j, const PresetData& preset);
68 : void from_json(const nlohmann::json& j, PresetData& preset);
69 :
70 : // Directory helper exposed for tests and diagnostics.
71 : std::string get_user_presets_dir();
72 :
73 : class PresetManager : public IPresetManager {
74 : public:
75 : friend class PresetSerializer;
76 : friend class PresetStorage;
77 : friend class PresetMigrator;
78 :
79 : PresetManager();
80 : PresetManager(std::unique_ptr<IPresetSerializer> serializer,
81 : std::unique_ptr<IPresetStorage> storage,
82 : std::unique_ptr<IPresetMigrator> migrator);
83 : ~PresetManager() override;
84 :
85 : // Save provided preset data to JSON file
86 : bool save_preset_data(const std::string& filepath, const PresetData& preset) override;
87 :
88 : // Save current engine state to JSON file
89 : bool save_preset(const std::string& filepath, const std::string& preset_name,
90 : const std::string& description, IAudioEngine& engine,
91 : const std::vector<MidiMapping>& midi_mappings = {}) override;
92 :
93 : // Load preset from JSON file and apply to engine
94 : bool load_preset(const std::string& filepath, IAudioEngine& engine,
95 : IMidiManager* midi_manager = nullptr) override;
96 :
97 : // Serialize current graph to JSON
98 : static std::string graph_to_json(const AudioGraph& graph);
99 :
100 : // Load graph from JSON
101 : static bool graph_from_json(const std::string& json, AudioGraph& graph);
102 :
103 : std::vector<std::string> list_presets() override;
104 : bool delete_preset(const std::string& filepath) override;
105 : std::string get_last_error() const override;
106 : std::string get_presets_directory() const override;
107 :
108 : // Directory helpers / global config
109 : static std::string get_presets_dir();
110 : static void set_presets_dir(const std::string& dir);
111 24 : static const std::string& custom_presets_dir() { return custom_presets_dir_; }
112 :
113 : static void save_config();
114 : static void load_config();
115 :
116 : // Public migration hooks so test targets can validate behavior
117 : static std::string apply_migrations(const std::string& raw_json_string);
118 :
119 : private:
120 : std::unique_ptr<IPresetSerializer> serializer_;
121 : std::unique_ptr<IPresetStorage> storage_;
122 : std::unique_ptr<IPresetMigrator> migrator_;
123 :
124 : std::string last_error_;
125 : static std::string custom_presets_dir_;
126 :
127 : static void save_factory_presets(const std::string& dir);
128 : static std::string get_config_path();
129 : static std::string get_system_presets_dir();
130 : };
131 :
132 : // Compatibility alias to avoid breaking code referencing PresetManagerService
133 : using PresetManagerService = PresetManager;
134 :
135 : } // namespace Amplitron
|