paulstretch_cpp

PaulStretch
Log | Files | Refs | LICENSE

Stretch.h (3692B)


      1 /*
      2   Copyright (C) 2006-2011 Nasca Octavian Paul
      3   Author: Nasca Octavian Paul
      4 
      5   This program is free software; you can redistribute it and/or modify
      6   it under the terms of version 2 of the GNU General Public License 
      7   as published by the Free Software Foundation.
      8 
      9   This program is distributed in the hope that it will be useful,
     10   but WITHOUT ANY WARRANTY; without even the implied warranty of
     11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     12   GNU General Public License (version 2) for more details.
     13 
     14   You should have received a copy of the GNU General Public License (version 2)
     15   along with this program; if not, write to the Free Software Foundation,
     16   Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
     17 */
     18 #ifndef STRETCH_H
     19 #define STRETCH_H
     20 #include "globals.h"
     21 
     22 #ifdef KISSFFT
     23 #include <kiss_fftr.h>
     24 #else
     25 #include <fftw3.h>
     26 #endif
     27 
     28 
     29 enum FFTWindow{W_RECTANGULAR,W_HAMMING,W_HANN,W_BLACKMAN,W_BLACKMAN_HARRIS};
     30 
     31 class FFT{//FFT class that considers phases as random
     32 	public:
     33 		FFT(int nsamples_);//samples must be even
     34 		~FFT();
     35 		void smp2freq();//input is smp, output is freq (phases are discarded)
     36 		void freq2smp();//input is freq,output is smp (phases are random)
     37 		void applywindow(FFTWindow type);
     38 		REALTYPE *smp;//size of samples/2
     39 		REALTYPE *freq;//size of samples
     40 		int nsamples;
     41 	private:
     42 #ifdef KISSFFT
     43 		kiss_fftr_cfg  plankfft,plankifft;	
     44 		kiss_fft_scalar *datar;
     45 		kiss_fft_cpx *datac;
     46 #else
     47 		fftwf_plan planfftw,planifftw;
     48 		REALTYPE *data;
     49 #endif
     50 		struct{
     51 			REALTYPE *data;
     52 			FFTWindow type;
     53 		}window;
     54 
     55 		unsigned int rand_seed;
     56 		static unsigned int start_rand_seed;
     57 };
     58 
     59 class Stretch{
     60 	public:
     61 		Stretch(REALTYPE rap_,int in_bufsize_,FFTWindow w=W_HAMMING,bool bypass_=false,REALTYPE samplerate_=44100,int stereo_mode_=0);
     62 		//in_bufsize is also a half of a FFT buffer (in samples)
     63 		virtual ~Stretch();
     64 
     65 		int get_max_bufsize(){
     66 			return bufsize*3;
     67 		};
     68 		int get_bufsize(){
     69 			return bufsize;
     70 		};
     71 
     72 		REALTYPE get_onset_detection_sensitivity(){
     73 			return onset_detection_sensitivity;
     74 		};
     75 
     76 		REALTYPE process(REALTYPE *smps,int nsmps);//returns the onset value
     77 		void set_freezing(bool new_freezing){
     78 			freezing=new_freezing;
     79 		};
     80 
     81 
     82 		REALTYPE *out_buf;//pot sa pun o variabila "max_out_bufsize" si asta sa fie marimea lui out_buf si pe out_bufsize sa il folosesc ca marime adaptiva
     83 
     84 		int get_nsamples(REALTYPE current_pos_percents);//how many samples are required 
     85 		int get_nsamples_for_fill();//how many samples are required to be added for a complete buffer refill (at start of the song or after seek)
     86 		int get_skip_nsamples();//used for shorten
     87 
     88 		void set_rap(REALTYPE newrap);//set the current stretch value
     89 
     90 		void set_onset_detection_sensitivity(REALTYPE detection_sensitivity){
     91 			onset_detection_sensitivity=detection_sensitivity;
     92 			if (detection_sensitivity<1e-3) extra_onset_time_credit=0.0;
     93 		};
     94 		void here_is_onset(REALTYPE onset);
     95 
     96 		FFTWindow window_type;
     97 	protected:
     98 		int bufsize;
     99 
    100 		virtual void process_spectrum(REALTYPE *freq){};
    101 		virtual REALTYPE get_stretch_multiplier(REALTYPE pos_percents);
    102 		REALTYPE samplerate;
    103 		int stereo_mode;//0=mono,1=left,2=right
    104 	private:
    105 
    106 		void do_analyse_inbuf(REALTYPE *smps);
    107 		void do_next_inbuf_smps(REALTYPE *smps);
    108 		REALTYPE do_detect_onset();
    109 
    110 //		REALTYPE *in_pool;//de marimea in_bufsize
    111 		REALTYPE rap,onset_detection_sensitivity;
    112 		REALTYPE *old_out_smps;
    113 		REALTYPE *old_freq;
    114 		REALTYPE *new_smps,*old_smps,*very_old_smps;
    115 
    116 		FFT *infft,*outfft;
    117 		FFT *fft;
    118 		long double remained_samples;//0..1
    119 		long double extra_onset_time_credit;
    120 		REALTYPE c_pos_percents;
    121 		int skip_samples;
    122 		bool require_new_buffer;
    123 		bool bypass,freezing;
    124 };
    125 
    126 
    127 #endif
    128