SpectMorph
smencoder.hh
1 // Licensed GNU LGPL v3 or later: http://www.gnu.org/licenses/lgpl.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 {
25  float mix_freq;
26 
29 
32 
34  int zeropad;
35 
37  size_t frame_step;
38 
40  size_t frame_size;
41 
43  size_t block_size;
44 
47 
48  /* config file parameters */
49  std::vector<std::string> param_name_d; // names of all supported double parameters
50  std::map<std::string, double> param_value_d; // values of double parameters from config file
51  std::vector<std::string> param_name_s; // names of all supported string parameters
52  std::map<std::string, std::string> param_value_s; // values of string parameters from config file
53 
54  bool load_config (const std::string& filename);
55  bool get_param (const std::string& param, double& value) const;
56  bool get_param (const std::string& param, std::string& value) const;
57 
58  EncoderParams();
59 };
60 
61 struct Tracksel {
62  size_t frame;
63  size_t d; /* FFT position */
64  double freq;
65  double mag;
66  double mag2; /* magnitude in dB */
67  double phase; /* phase */
68  bool is_harmonic;
69  Tracksel *prev, *next;
70 };
71 
73 {
74 public:
75  std::vector<float> noise;
76  std::vector<float> freqs;
77  std::vector<float> mags;
78  std::vector<float> phases;
79  std::vector<float> original_fft;
80  std::vector<float> debug_samples;
81 };
82 
91 class Encoder
92 {
93  EncoderParams enc_params;
94  int loop_start;
95  int loop_end;
96  Audio::LoopType loop_type;
97 
98  bool check_harmonic (double freq, double& new_freq, double mix_freq);
99 
100  std::vector< std::vector<Tracksel> > frame_tracksels;
101 
102  struct Attack
103  {
104  double attack_start_ms;
105  double attack_end_ms;
106  };
107  double attack_error (const std::vector< std::vector<double> >& unscaled_signal, const std::vector<float>& window, const Attack& attack, std::vector<double>& out_scale);
108 
109 public:
110  std::vector<EncoderBlock> audio_blocks;
111  Attack optimal_attack;
112  size_t zero_values_at_start;
113  size_t sample_count;
114  std::vector<float> original_samples;
115 
116  Encoder (const EncoderParams& enc_params);
117 
118  // single encoder steps:
119  void compute_stft (const WavData& wav_data, int channel, const std::vector<float>& window);
120  void search_local_maxima (const std::vector<float>& window);
121  void link_partials();
122  void validate_partials();
123  void optimize_partials (const std::vector<float>& window, int optimization_level);
124  void spectral_subtract (const std::vector<float>& window);
125  void approx_noise (const std::vector<float>& window);
126  void compute_attack_params (const std::vector<float>& window);
127  void sort_freqs();
128  void debug_decode (const std::string& filename, const std::vector<float>& window);
129 
130  // all-in-one encoding function:
131  void encode (const WavData& wav_data, int channel, const std::vector<float>& window, int optimization_level,
132  bool attack, bool track_sines);
133 
134  void set_loop (Audio::LoopType loop_type, int loop_start, int loop_end);
135  void set_loop_seconds (Audio::LoopType loop_type, double loop_start, double loop_end);
136  void save (const std::string& filename);
137 };
138 
139 }
140 
141 #endif
std::vector< float > original_fft
original zeropadded FFT data - for debugging only
Definition: smencoder.hh:79
float frame_step_ms
Definition: smencoder.hh:28
float frame_size_ms
Definition: smencoder.hh:31
size_t frame_step
Definition: smencoder.hh:37
Definition: smencoder.hh:72
Definition: smencoder.hh:61
std::vector< EncoderBlock > audio_blocks
current state, and end result of the encoding algorithm
Definition: smencoder.hh:110
std::vector< float > phases
phases of the sine components
Definition: smencoder.hh:78
double fundamental_freq
Definition: smencoder.hh:46
Encoder parameters.
Definition: smencoder.hh:22
Definition: smadsrenvelope.hh:8
float mix_freq
Definition: smencoder.hh:25
std::vector< float > freqs
frequencies of the sine components of this frame
Definition: smencoder.hh:76
std::vector< float > mags
magnitudes of the sine components
Definition: smencoder.hh:77
std::vector< float > debug_samples
original audio samples for this frame - for debugging only
Definition: smencoder.hh:80
Encoder producing SpectMorph parametric data from sample data.
Definition: smencoder.hh:91
int zeropad
Definition: smencoder.hh:34
size_t frame_size
Definition: smencoder.hh:40
std::vector< float > noise
noise envelope, representing the original signal minus sine components
Definition: smencoder.hh:75
size_t block_size
Definition: smencoder.hh:43
Definition: smwavdata.hh:13