SpectMorph
smencoder.hh
1 // Licensed GNU LGPL v2.1 or later: http://www.gnu.org/licenses/lgpl-2.1.html
2 
3 #ifndef SPECTMORPH_ENCODER_HH
4 #define SPECTMORPH_ENCODER_HH
5 
6 #include <sys/types.h>
7 #include <vector>
8 #include <string>
9 #include <map>
10 
11 #include "smaudio.hh"
12 #include "smwavdata.hh"
13 
14 namespace SpectMorph
15 {
16 
23 {
24  /* config file parameters */
25  std::vector<std::string> param_name_d; // names of all supported double parameters
26  std::map<std::string, double> param_value_d; // values of double parameters from config file
27  std::vector<std::string> param_name_s; // names of all supported string parameters
28  std::map<std::string, std::string> param_value_s; // values of string parameters from config file
29 
30 public:
32  float mix_freq = 0;
33 
35  float frame_step_ms = 0;
36 
38  float frame_size_ms = 0;
39 
41  int zeropad = 0;
42 
44  size_t frame_step = 0;
45 
47  size_t frame_size = 0;
48 
50  size_t block_size = 0;
51 
53  double fundamental_freq = 0;
54 
56  bool enable_phases = true;
57 
59  std::vector<float> window;
60 
62  std::function<bool()> kill_function;
63 
64  bool add_config_entry (const std::string& param, const std::string& value);
65 
66  bool load_config (const std::string& filename);
67  bool get_param (const std::string& param, double& value) const;
68  bool get_param (const std::string& param, std::string& value) const;
69 
70  EncoderParams();
71 
73  void setup_params (const WavData& wav_data, double fundamental_freq);
74 
76  void set_kill_function (const std::function<bool()>& kill_function);
77 };
78 
79 struct Tracksel {
80  size_t frame;
81  size_t d; /* FFT position */
82  double freq;
83  double mag;
84  double mag2; /* magnitude in dB */
85  double phase; /* phase */
86 
87  Tracksel *prev, *next;
88 };
89 
91 {
92 public:
93  std::vector<float> noise;
94  std::vector<float> freqs;
95  std::vector<float> mags;
96  std::vector<float> phases;
97  std::vector<float> original_fft;
98  std::vector<float> debug_samples;
99 };
100 
109 class Encoder
110 {
111  EncoderParams enc_params;
112  int loop_start;
113  int loop_end;
114  Audio::LoopType loop_type;
115 
116  std::vector< std::vector<Tracksel> > frame_tracksels;
117 
118  struct Attack
119  {
120  double attack_start_ms;
121  double attack_end_ms;
122  };
123  double attack_error (const std::vector< std::vector<double> >& unscaled_signal, const Attack& attack, std::vector<double>& out_scale);
124 
125  // single encoder steps:
126  void compute_stft (const WavData& wav_data, int channel);
127  void search_local_maxima();
128  void link_partials();
129  void validate_partials();
130  void optimize_partials (int optimization_level);
131  void spectral_subtract();
132  void approx_noise();
133  void compute_attack_params();
134  void sort_freqs();
135 
136  inline bool
137  killed (const char *where, uint64_t z = 0)
138  {
139  if (z != 0) // inlined fast check
140  return false;
141 
142  // debugging: printf ("%-10s ", where);
143 
144  return enc_params.kill_function && enc_params.kill_function();
145  }
146 
147  Attack optimal_attack;
148  size_t zero_values_at_start;
149  size_t sample_count;
150 
151 public:
152  std::vector<EncoderBlock> audio_blocks;
153  std::vector<float> original_samples;
154 
155  Encoder (const EncoderParams& enc_params);
156 
157  void debug_decode (const std::string& filename);
158 
159  // all-in-one encoding function:
160  bool encode (const WavData& wav_data, int channel, int optimization_level,
161  bool attack, bool track_sines);
162 
163  static std::string version(); // changes if encoder algorithm changed (for cache invalidation)
164 
165  void set_loop (Audio::LoopType loop_type, int loop_start, int loop_end);
166  void set_loop_seconds (Audio::LoopType loop_type, double loop_start, double loop_end);
167 
168  Error save (const std::string& filename);
169  Audio *save_as_audio();
170 };
171 
172 }
173 
174 #endif
Audio sample containing many blocks.
Definition: smaudio.hh:80
Definition: smencoder.hh:91
std::vector< float > debug_samples
original audio samples for this frame - for debugging only
Definition: smencoder.hh:98
std::vector< float > freqs
frequencies of the sine components of this frame
Definition: smencoder.hh:94
std::vector< float > mags
magnitudes of the sine components
Definition: smencoder.hh:95
std::vector< float > original_fft
original zeropadded FFT data - for debugging only
Definition: smencoder.hh:97
std::vector< float > phases
phases of the sine components
Definition: smencoder.hh:96
std::vector< float > noise
noise envelope, representing the original signal minus sine components
Definition: smencoder.hh:93
Encoder parameters.
Definition: smencoder.hh:23
int zeropad
Definition: smencoder.hh:41
size_t frame_step
Definition: smencoder.hh:44
void set_kill_function(const std::function< bool()> &kill_function)
Definition: smencoder.cc:247
size_t block_size
Definition: smencoder.hh:50
double fundamental_freq
Definition: smencoder.hh:53
size_t frame_size
Definition: smencoder.hh:47
float mix_freq
Definition: smencoder.hh:32
bool enable_phases
Definition: smencoder.hh:56
float frame_step_ms
Definition: smencoder.hh:35
float frame_size_ms
Definition: smencoder.hh:38
std::function< bool()> kill_function
Definition: smencoder.hh:62
void setup_params(const WavData &wav_data, double fundamental_freq)
Definition: smencoder.cc:197
std::vector< float > window
Definition: smencoder.hh:59
Encoder producing SpectMorph parametric data from sample data.
Definition: smencoder.hh:110
Error save(const std::string &filename)
Definition: smencoder.cc:1506
bool encode(const WavData &wav_data, int channel, int optimization_level, bool attack, bool track_sines)
Definition: smencoder.cc:1376
Encoder(const EncoderParams &enc_params)
Definition: smencoder.cc:255
Audio * save_as_audio()
Definition: smencoder.cc:1518
std::vector< EncoderBlock > audio_blocks
current state, and end result of the encoding algorithm
Definition: smencoder.hh:152
Definition: smutils.hh:102
Definition: smwavdata.hh:17
Definition: smencoder.hh:79