SpectMorph
sminstrument.hh
1 // Licensed GNU LGPL v2.1 or later: http://www.gnu.org/licenses/lgpl-2.1.html
2 
3 #ifndef SPECTMORPH_INSTRUMENT_HH
4 #define SPECTMORPH_INSTRUMENT_HH
5 
6 #include "smutils.hh"
7 #include "smwavdata.hh"
8 #include "smsignal.hh"
9 #include "smmath.hh"
10 #include "smaudio.hh"
11 
12 #include <map>
13 #include <memory>
14 
15 namespace SpectMorph
16 {
17 
18 enum MarkerType {
19  MARKER_NONE = 0,
20  MARKER_LOOP_START,
21  MARKER_LOOP_END,
22  MARKER_CLIP_START,
23  MARKER_CLIP_END
24 };
25 
26 class Instrument;
27 class ZipWriter;
28 class ZipReader;
29 class Sample
30 {
31 public:
32  enum class Loop { NONE, FORWARD, PING_PONG, SINGLE_FRAME };
33 
34  struct Shared
35  {
36  WavData m_wav_data;
37  std::string m_wav_data_hash;
38  public:
39  Shared (const WavData& wav_data);
40 
41  const WavData& wav_data() const;
42  std::string wav_data_hash() const;
43  };
44  typedef std::shared_ptr<Shared> SharedP;
45 private:
46 
47  SPECTMORPH_CLASS_NON_COPYABLE (Sample);
48 
49  std::map<MarkerType, double> marker_map;
50  int m_midi_note = 69;
51  Instrument *instrument = nullptr;
52  Loop m_loop = Loop::NONE;
53  double m_volume = 0;
54 
55  SharedP m_shared;
56 
57 public:
58  Sample (Instrument *inst, const WavData& wav_data);
59  void set_marker (MarkerType marker_type, double value);
60  double get_marker (MarkerType marker_type) const;
61 
62  int midi_note() const;
63  void set_midi_note (int note);
64 
65  Loop loop() const;
66  void set_loop (Loop loop);
67 
68  double volume() const;
69  void set_volume (double volume);
70 
71  SharedP shared() const;
72 
73  const WavData& wav_data() const;
74  std::string wav_data_hash() const;
75 
76  std::string filename;
77  std::string short_name;
78 
79  std::unique_ptr<Audio> audio;
80 };
81 
83 {
84 public:
85  struct AutoVolume {
86  enum { FROM_LOOP, GLOBAL } method = FROM_LOOP;
87 
88  bool enabled = false;
89  double gain = 0; // used by: global
90  };
91 
92  struct AutoTune {
93  enum { SIMPLE, ALL_FRAMES, SMOOTH } method = SIMPLE;
94 
95  bool enabled = false;
96  int partials = 1; // used by: all_frames, smooth
97  double time = 100; // used_by: smooth
98  double amount = 25; // used by: smooth
99  };
101  {
102  std::string param;
103  std::string value;
104  };
106  {
107  bool enabled = false;
108 
109  std::vector<EncoderEntry> entries;
110  };
111 
112  enum class LoadOptions
113  {
114  ALL,
115  NAME_ONLY
116  };
117 
118 private:
119  SPECTMORPH_CLASS_NON_COPYABLE (Instrument);
120 
121  std::vector<std::unique_ptr<Sample>> samples;
122  int m_selected = -1;
123  std::string m_name = "untitled";
124  std::string m_short_name;
125 
126  double m_global_volume = 0;
127  AutoVolume m_auto_volume;
128  AutoTune m_auto_tune;
129  EncoderConfig m_encoder_config;
130 
131  Error load (const std::string& filename, ZipReader *zip_reader, LoadOptions load_options = LoadOptions::ALL);
132  Error save (const std::string& filename, ZipWriter *zip_writer) const;
133 public:
134  Instrument();
135 
136  Sample *add_sample (const WavData& wav_data, const std::string& filename);
137  Sample *sample (size_t n) const;
138  void remove_sample();
139  std::string gen_short_name (std::vector<std::unique_ptr<Sample>>& samples, const std::string& filename);
140 
141  size_t size() const;
142  void clear();
143  std::string name() const;
144  void set_name (const std::string& name);
145  std::string short_name() const;
146  void set_short_name (const std::string& short_name);
147 
148  std::string version();
149 
150  int selected() const;
151  void set_selected (int sel);
152 
153  Error load (const std::string& filename, LoadOptions load_options = LoadOptions::ALL);
154  Error load (ZipReader& zip_reader, LoadOptions load_options = LoadOptions::ALL);
155 
156  Error save (const std::string& filename) const;
157  Error save (ZipWriter& zip_writer) const;
158 
159  Instrument *clone() const;
160  void update_order();
161  void marker_changed();
162  void volume_changed();
163 
164  std::map<int, int> used_count() const;
165 
166  double global_volume() const;
167  void set_global_volume (double new_volume);
168 
169  AutoVolume auto_volume() const;
170  void set_auto_volume (const AutoVolume& new_value);
171 
172  AutoTune auto_tune() const;
173  void set_auto_tune (const AutoTune& new_value);
174 
175  EncoderConfig encoder_config() const;
176  void set_encoder_config (const EncoderConfig& new_value);
177 
178  Signal<> signal_volume_changed;
179  Signal<> signal_samples_changed;
180  Signal<> signal_selected_sample_changed;
181  Signal<> signal_marker_changed;
182  Signal<> signal_global_changed; // global auto volume, auto tune or advanced params changed
183 };
184 
185 }
186 
187 #endif
Definition: smutils.hh:102
Definition: sminstrument.hh:83
Definition: sminstrument.hh:30
Definition: smsignal.hh:150
Definition: smwavdata.hh:17
Definition: smzip.hh:15
Definition: smzip.hh:34
Definition: sminstrument.hh:92
Definition: sminstrument.hh:85
Definition: sminstrument.hh:106
Definition: sminstrument.hh:101
Definition: sminstrument.hh:35