SpectMorph
smaudio.hh
1 // Licensed GNU LGPL v3 or later: http://www.gnu.org/licenses/lgpl.html
2 
3 #ifndef SPECTMORPH_AUDIO_HH
4 #define SPECTMORPH_AUDIO_HH
5 
6 #include <vector>
7 
8 #include "smgenericin.hh"
9 #include "smgenericout.hh"
10 #include "smmath.hh"
11 #include "smutils.hh"
12 
13 #define SPECTMORPH_BINARY_FILE_VERSION 14
14 #define SPECTMORPH_SUPPORT_MULTI_CHANNEL 0
15 
16 namespace SpectMorph
17 {
18 
28 {
29 public:
30  std::vector<uint16_t> noise;
31  std::vector<uint16_t> freqs;
32  std::vector<uint16_t> mags;
33  std::vector<uint16_t> phases;
34  std::vector<float> original_fft;
35  std::vector<float> debug_samples;
36 
37  void sort_freqs();
38 
39  double
40  freqs_f (size_t i) const
41  {
42  return sm_ifreq2freq (freqs[i]);
43  }
44 
45  double
46  mags_f (size_t i) const
47  {
48  return sm_idb2factor (mags[i]);
49  }
50 
51  double
52  phases_f (size_t i) const
53  {
54  const double factor = 2.0 * M_PI / 65536.0;
55  return phases[i] * factor;
56  }
57 
58  double
59  noise_f (size_t i) const
60  {
61  return sm_idb2factor (noise[i]);
62  }
63 };
64 
65 enum AudioLoadOptions
66 {
67  AUDIO_LOAD_DEBUG,
68  AUDIO_SKIP_DEBUG
69 };
70 
78 class Audio
79 {
80  SPECTMORPH_CLASS_NON_COPYABLE (Audio);
81 public:
82  Audio();
83  ~Audio();
84 
85  enum LoopType {
86  LOOP_NONE = 0,
87  LOOP_FRAME_FORWARD,
88  LOOP_FRAME_PING_PONG,
89  LOOP_TIME_FORWARD,
90  LOOP_TIME_PING_PONG,
91  };
92 
93  float fundamental_freq = 0;
94  float mix_freq = 0;
95  float frame_size_ms = 0;
96  float frame_step_ms = 0;
97  float attack_start_ms = 0;
98  float attack_end_ms = 0;
99  int zeropad = 0;
100  LoopType loop_type = LOOP_NONE;
101  int loop_start = 0;
102  int loop_end = 0;
103  int zero_values_at_start = 0;
104  int sample_count = 0;
105  std::vector<float> original_samples;
106  float original_samples_norm_db = 0;
107  std::vector<AudioBlock> contents;
108 
109  Error load (const std::string& filename, AudioLoadOptions load_options = AUDIO_LOAD_DEBUG);
110  Error load (SpectMorph::GenericIn *file, AudioLoadOptions load_options = AUDIO_LOAD_DEBUG);
111  Error save (const std::string& filename) const;
112  Error save (SpectMorph::GenericOut *file) const;
113 
114  Audio *clone() const; // create a deep copy
115 
116  static bool loop_type_to_string (LoopType loop_type, std::string& s);
117  static bool string_to_loop_type (const std::string& s, LoopType& loop_type);
118 };
119 
120 }
121 
122 #endif
std::vector< float > original_samples
original time domain signal as samples (debugging only)
Definition: smaudio.hh:105
Generic Input Stream.
Definition: smgenericin.hh:17
Audio sample containing many blocks.
Definition: smaudio.hh:78
std::vector< float > original_fft
original zeropadded FFT data - for debugging only
Definition: smaudio.hh:34
Block of audio data, encoded in SpectMorph parametric format.
Definition: smaudio.hh:27
std::vector< uint16_t > noise
noise envelope, representing the original signal minus sine components
Definition: smaudio.hh:30
std::vector< uint16_t > freqs
frequencies of the sine components of this frame
Definition: smaudio.hh:31
std::vector< AudioBlock > contents
the actual frame data
Definition: smaudio.hh:107
std::vector< uint16_t > phases
phases of the sine components
Definition: smaudio.hh:33
Definition: smadsrenvelope.hh:8
std::vector< uint16_t > mags
magnitudes of the sine components
Definition: smaudio.hh:32
Generic Output Stream.
Definition: smgenericout.hh:17
std::vector< float > debug_samples
original audio samples for this frame - for debugging only
Definition: smaudio.hh:35