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 <bse/gsldatautils.hh>
10 
11 #include "smaudio.hh"
12 
13 namespace SpectMorph
14 {
15 
22 {
24  float mix_freq;
25 
28 
31 
33  int zeropad;
34 
36  size_t frame_step;
37 
39  size_t frame_size;
40 
42  size_t block_size;
43 
46 
47  EncoderParams();
48 };
49 
50 struct Tracksel {
51  size_t frame;
52  size_t d; /* FFT position */
53  double freq;
54  double mag;
55  double mag2; /* magnitude in dB */
56  double phase; /* phase */
57  bool is_harmonic;
58  Tracksel *prev, *next;
59 };
60 
62 {
63 public:
64  std::vector<float> noise;
65  std::vector<float> freqs;
66  std::vector<float> mags;
67  std::vector<float> phases;
68  std::vector<float> lpc_lsf_p;
69  std::vector<float> lpc_lsf_q;
70  std::vector<float> original_fft;
71  std::vector<float> debug_samples;
72 };
73 
82 class Encoder
83 {
84  EncoderParams enc_params;
85  int loop_start;
86  int loop_end;
87  Audio::LoopType loop_type;
88 
89  bool check_harmonic (double freq, double& new_freq, double mix_freq);
90 
91  std::vector< std::vector<Tracksel> > frame_tracksels;
92 
93  struct Attack
94  {
95  double attack_start_ms;
96  double attack_end_ms;
97  };
98  double attack_error (const std::vector< std::vector<double> >& unscaled_signal, const std::vector<float>& window, const Attack& attack, std::vector<double>& out_scale);
99 
100 public:
101  std::vector<EncoderBlock> audio_blocks;
102  Attack optimal_attack;
103  size_t zero_values_at_start;
104  size_t sample_count;
105  std::vector<float> original_samples;
106 
107  Encoder (const EncoderParams& enc_params);
108 
109  // single encoder steps:
110  void compute_stft (GslDataHandle *dhandle, int channel, const std::vector<float>& window);
111  void search_local_maxima (const std::vector<float>& window);
112  void link_partials();
113  void validate_partials();
114  void optimize_partials (const std::vector<float>& window, int optimization_level);
115  void spectral_subtract (const std::vector<float>& window);
116  void approx_noise (const std::vector<float>& window);
117  void compute_attack_params (const std::vector<float>& window);
118  void compute_lpc_lsf();
119  void sort_freqs();
120  void debug_decode (const std::string& filename, const std::vector<float>& window);
121 
122  // all-in-one encoding function:
123  void encode (GslDataHandle *dhandle, int channel, const std::vector<float>& window, int optimization_level,
124  bool attack, bool track_sines, bool do_lpc);
125 
126  void set_loop (Audio::LoopType loop_type, int loop_start, int loop_end);
127  void set_loop_seconds (Audio::LoopType loop_type, double loop_start, double loop_end);
128  void save (const std::string& filename);
129 };
130 
131 }
132 
133 #endif
std::vector< float > original_fft
original zeropadded FFT data - for debugging only
Definition: smencoder.hh:70
float frame_step_ms
Definition: smencoder.hh:27
float frame_size_ms
Definition: smencoder.hh:30
size_t frame_step
Definition: smencoder.hh:36
Definition: smencoder.hh:61
Definition: smencoder.hh:50
std::vector< EncoderBlock > audio_blocks
current state, and end result of the encoding algorithm
Definition: smencoder.hh:101
std::vector< float > phases
phases of the sine components
Definition: smencoder.hh:67
double fundamental_freq
Definition: smencoder.hh:45
Encoder parameters.
Definition: smencoder.hh:21
std::vector< float > lpc_lsf_q
LPC line spectrum frequencies, Q(z) roots.
Definition: smencoder.hh:69
Definition: smaudio.hh:15
float mix_freq
Definition: smencoder.hh:24
std::vector< float > freqs
frequencies of the sine components of this frame
Definition: smencoder.hh:65
std::vector< float > mags
magnitudes of the sine components
Definition: smencoder.hh:66
std::vector< float > debug_samples
original audio samples for this frame - for debugging only
Definition: smencoder.hh:71
Encoder producing SpectMorph parametric data from sample data.
Definition: smencoder.hh:82
int zeropad
Definition: smencoder.hh:33
std::vector< float > lpc_lsf_p
LPC line spectrum frequencies, P(z) roots.
Definition: smencoder.hh:68
size_t frame_size
Definition: smencoder.hh:39
std::vector< float > noise
noise envelope, representing the original signal minus sine components
Definition: smencoder.hh:64
size_t block_size
Definition: smencoder.hh:42