SpectMorph

lib/smencoder.hh

00001 /* 
00002  * Copyright (C) 2010 Stefan Westerfeld
00003  *
00004  * This library is free software; you can redistribute it and/or modify it
00005  * under the terms of the GNU Lesser General Public License as published by the
00006  * Free Software Foundation; either version 3 of the License, or (at your
00007  * option) any later version.
00008  *
00009  * This library is distributed in the hope that it will be useful, but WITHOUT
00010  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00011  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
00012  * for more details.
00013  *
00014  * You should have received a copy of the GNU Lesser General Public License
00015  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 
00018 #ifndef SPECTMORPH_ENCODER_HH
00019 #define SPECTMORPH_ENCODER_HH
00020 
00021 #include <sys/types.h>
00022 #include <vector>
00023 #include <string>
00024 #include <bse/gsldatautils.h>
00025 
00026 #include "smaudio.hh"
00027 
00028 namespace SpectMorph
00029 {
00030 
00036 struct EncoderParams
00037 {
00039   float   mix_freq;         
00040 
00042   float   frame_step_ms;
00043 
00045   float   frame_size_ms;
00046 
00048   int     zeropad;
00049 
00051   size_t  frame_step;
00052 
00054   size_t  frame_size;
00055 
00057   size_t  block_size;
00058 
00060   double  fundamental_freq;
00061 };
00062 
00063 struct Tracksel {
00064   size_t   frame;
00065   size_t   d;         /* FFT position */
00066   double   freq;
00067   double   mag;
00068   double   mag2;      /* magnitude in dB */
00069   double   phase;     /* phase */
00070   bool     is_harmonic;
00071   Tracksel *prev, *next;
00072 };
00073 
00082 class Encoder
00083 {
00084   EncoderParams                        enc_params;
00085   int                                  loop_start;
00086   int                                  loop_end;
00087 
00088   bool check_harmonic (double freq, double& new_freq, double mix_freq);
00089 
00090   std::vector< std::vector<Tracksel> > frame_tracksels; 
00091 
00092   struct Attack
00093   {
00094     double attack_start_ms;
00095     double attack_end_ms;
00096   };
00097   double attack_error (const std::vector< std::vector<double> >& unscaled_signal, const std::vector<float>& window, const Attack& attack, std::vector<double>& out_scale);
00098 
00099 public:
00100   std::vector<AudioBlock>              audio_blocks;    
00101   Attack                               optimal_attack;
00102   size_t                               zero_values_at_start;
00103   size_t                               sample_count;
00104   std::vector<float>                   original_samples;
00105 
00106   Encoder (const EncoderParams& enc_params);
00107 
00108   // single encoder steps:
00109   void compute_stft (GslDataHandle *dhandle, int channel, const std::vector<float>& window);
00110   void search_local_maxima();
00111   void link_partials();
00112   void validate_partials();
00113   void optimize_partials (const std::vector<float>& window, int optimization_level);
00114   void spectral_subtract (const std::vector<float>& window);
00115   void approx_noise (const std::vector<float>& window);
00116   void compute_attack_params (const std::vector<float>& window);
00117   void compute_lpc_lsf();
00118   void sort_freqs();
00119 
00120   // all-in-one encoding function:
00121   void encode (GslDataHandle *dhandle, int channel, const std::vector<float>& window, int optimization_level,
00122                bool attack, bool track_sines);
00123 
00124   void set_time_loop (int loop_start, int loop_end);
00125   void save (const std::string& filename, double fundamental_freq);
00126 };
00127 
00128 }
00129 
00130 #endif
 All Classes Functions Variables Enumerations Enumerator