LCOV - code coverage report
Current view: top level - src/presets - preset_manager.h (source / functions) Coverage Total Hit
Test: merged.info Lines: 100.0 % 16 16
Test Date: 2026-06-03 09:13:19 Functions: 100.0 % 2 2

            Line data    Source code
       1              : #pragma once
       2              : 
       3              : #include "common.h"
       4              : #include "audio/engine/audio_engine.h"
       5              : #include "midi/midi_manager.h"
       6              : #include <nlohmann/json_fwd.hpp>
       7              : #include <fstream>
       8              : #include <map>
       9              : #include <sstream>
      10              : 
      11              : namespace Amplitron {
      12              : 
      13              : constexpr int CURRENT_PRESET_VERSION = 2;
      14              : 
      15          247 : struct PresetData {
      16              :     std::string name;
      17              :     std::string description;
      18          247 :     float input_gain = 0.7f;
      19          247 :     float output_gain = 0.8f;
      20              : 
      21          487 :     struct EffectData {
      22              :         std::string type;
      23          236 :         bool enabled = false;
      24          236 :         float mix = 1.0f;
      25              :         std::vector<std::pair<std::string, float>> params;
      26              :         std::map<std::string, std::string> metadata;
      27              :     };
      28              :     std::vector<EffectData> effects;
      29              :     std::vector<MidiMapping> midi_mappings;
      30              : 
      31          247 :     std::string routing = "linear";
      32              : 
      33          134 :     struct NodeData {
      34              :         std::string id;
      35              :         std::string type;
      36           98 :         float x = 0.0f;
      37           98 :         float y = 0.0f;
      38           98 :         bool enabled = true;
      39           98 :         float mix = 1.0f;
      40           98 :         int num_inputs = 0;
      41              :         std::vector<std::pair<std::string, float>> params;
      42              :         std::map<std::string, std::string> metadata;
      43              :     };
      44              :     std::vector<NodeData> nodes;
      45              : 
      46          139 :     struct LinkData {
      47              :         std::string src_pin;
      48              :         std::string dst_pin;
      49              :     };
      50              :     std::vector<LinkData> links;
      51              : };
      52              : 
      53              : // Serialization helpers are declared here so translation units that include
      54              : // preset_manager.h (including tests) can use preset JSON APIs without relying
      55              : // on direct inclusion order of preset_json.h.
      56              : std::string to_json_ext(const PresetData& preset);
      57              : bool from_json_ext(const std::string& json_str, PresetData& preset);
      58              : 
      59              : // nlohmann ADL hooks for PresetData and nested EffectData.
      60              : void to_json(nlohmann::json& j, const PresetData::EffectData& fx);
      61              : void from_json(const nlohmann::json& j, PresetData::EffectData& fx);
      62              : void to_json(nlohmann::json& j, const PresetData& preset);
      63              : void from_json(const nlohmann::json& j, PresetData& preset);
      64              : 
      65              : // Directory helper exposed for tests and diagnostics.
      66              : std::string get_user_presets_dir();
      67              : 
      68              : class PresetManager {
      69              : public:
      70              :     // Save provided preset data to JSON file
      71              :     static bool save_preset_data(const std::string& filepath,
      72              :                                  const PresetData& preset);
      73              : 
      74              :     // Save current engine state to JSON file
      75              :     static bool save_preset(const std::string& filepath,
      76              :                             const std::string& preset_name,
      77              :                             const std::string& description,
      78              :                             AudioEngine& engine,
      79              :                             const std::vector<MidiMapping>& midi_mappings = {});
      80              : 
      81              :     // Load preset from JSON file and apply to engine
      82              :     static bool load_preset(const std::string& filepath,
      83              :                             AudioEngine& engine,
      84              :                             MidiManager* midi_manager = nullptr);
      85              : 
      86              :     // Serialize current graph to JSON
      87              :     static std::string graph_to_json(const AudioGraph& graph);
      88              : 
      89              :     // Load graph from JSON
      90              :     static bool graph_from_json(const std::string& json, AudioGraph& graph);
      91              : 
      92              :     static std::string get_presets_dir();
      93              :     static void set_presets_dir(const std::string& dir);
      94           24 :     static const std::string& custom_presets_dir() { return custom_presets_dir_; }
      95              : 
      96              :     static void save_config();
      97              :     static void load_config();
      98              :     static std::vector<std::string> list_presets();
      99            3 :     static const std::string& last_error() { return last_error_; }
     100              : 
     101              :     // Public migration hooks so test targets can validate behavior
     102              :     static std::string apply_migrations(const std::string& raw_json_string);
     103              : 
     104              : private:
     105              :     static std::string last_error_;
     106              :     static std::string custom_presets_dir_;
     107              : 
     108              :     static void save_factory_presets(const std::string& dir);
     109              :     static std::string get_config_path();
     110              :     static std::string get_system_presets_dir();
     111              : };
     112              : 
     113              : } // namespace Amplitron
        

Generated by: LCOV version 2.0-1