You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

380 lines
10KB

  1. /*
  2. Copyright (C) 2006-2011 Nasca Octavian Paul
  3. Author: Nasca Octavian Paul
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of version 2 of the GNU General Public License
  6. as published by the Free Software Foundation.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License (version 2) for more details.
  11. You should have received a copy of the GNU General Public License (version 2)
  12. along with this program; if not, write to the Free Software Foundation,
  13. Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  14. */
  15. #include "Stretch.h"
  16. #include <stdlib.h>
  17. #include <math.h>
  18. unsigned int FFT::start_rand_seed=1;
  19. FFT::FFT(int nsamples_){
  20. nsamples=nsamples_;
  21. if (nsamples%2!=0) {
  22. nsamples+=1;
  23. printf("WARNING: Odd sample size on FFT::FFT() (%d)",nsamples);
  24. };
  25. smp=new REALTYPE[nsamples];for (int i=0;i<nsamples;i++) smp[i]=0.0;
  26. freq=new REALTYPE[nsamples/2+1];for (int i=0;i<nsamples/2+1;i++) freq[i]=0.0;
  27. window.data=new REALTYPE[nsamples];for (int i=0;i<nsamples;i++) window.data[i]=0.707;
  28. window.type=W_RECTANGULAR;
  29. #ifdef KISSFFT
  30. datar=new kiss_fft_scalar[nsamples+2];
  31. for (int i=0;i<nsamples+2;i++) datar[i]=0.0;
  32. datac=new kiss_fft_cpx[nsamples/2+2];
  33. for (int i=0;i<nsamples/2+2;i++) datac[i].r=datac[i].i=0.0;
  34. plankfft = kiss_fftr_alloc(nsamples,0,0,0);
  35. plankifft = kiss_fftr_alloc(nsamples,1,0,0);
  36. #else
  37. data=new REALTYPE[nsamples];for (int i=0;i<nsamples;i++) data[i]=0.0;
  38. planfftw=fftwf_plan_r2r_1d(nsamples,data,data,FFTW_R2HC,FFTW_ESTIMATE);
  39. planifftw=fftwf_plan_r2r_1d(nsamples,data,data,FFTW_HC2R,FFTW_ESTIMATE);
  40. #endif
  41. rand_seed=start_rand_seed;
  42. start_rand_seed+=161103;
  43. };
  44. FFT::~FFT(){
  45. delete []smp;
  46. delete []freq;
  47. delete []window.data;
  48. #ifdef KISSFFT
  49. delete []datar;
  50. delete []datac;
  51. free(plankfft);
  52. free(plankifft);
  53. #else
  54. delete []data;
  55. fftwf_destroy_plan(planfftw);
  56. fftwf_destroy_plan(planifftw);
  57. #endif
  58. };
  59. void FFT::smp2freq(){
  60. #ifdef KISSFFT
  61. for (int i=0;i<nsamples;i++) datar[i]=smp[i];
  62. kiss_fftr(plankfft,datar,datac);
  63. #else
  64. for (int i=0;i<nsamples;i++) data[i]=smp[i];
  65. fftwf_execute(planfftw);
  66. #endif
  67. for (int i=1;i<nsamples/2;i++) {
  68. #ifdef KISSFFT
  69. REALTYPE c=datac[i].r;
  70. REALTYPE s=datac[i].i;
  71. #else
  72. REALTYPE c=data[i];
  73. REALTYPE s=data[nsamples-i];
  74. #endif
  75. freq[i]=sqrt(c*c+s*s);
  76. };
  77. freq[0]=0.0;
  78. };
  79. void FFT::freq2smp(){
  80. REALTYPE inv_2p15_2pi=1.0/16384.0*M_PI;
  81. for (int i=1;i<nsamples/2;i++) {
  82. rand_seed=(rand_seed*1103515245+12345);
  83. unsigned int rand=(rand_seed>>16)&0x7fff;
  84. REALTYPE phase=rand*inv_2p15_2pi;
  85. #ifdef KISSFFT
  86. datac[i].r=freq[i]*cos(phase);
  87. datac[i].i=freq[i]*sin(phase);
  88. #else
  89. data[i]=freq[i]*cos(phase);
  90. data[nsamples-i]=freq[i]*sin(phase);
  91. #endif
  92. };
  93. #ifdef KISSFFT
  94. datac[0].r=datac[0].i=0.0;
  95. kiss_fftri(plankifft,datac,datar);
  96. for (int i=0;i<nsamples;i++) smp[i]=datar[i]/nsamples;
  97. #else
  98. data[0]=data[nsamples/2+1]=data[nsamples/2]=0.0;
  99. fftwf_execute(planifftw);
  100. for (int i=0;i<nsamples;i++) smp[i]=data[i]/nsamples;
  101. #endif
  102. };
  103. void FFT::applywindow(FFTWindow type){
  104. if (window.type!=type){
  105. window.type=type;
  106. switch (type){
  107. case W_RECTANGULAR:
  108. for (int i=0;i<nsamples;i++) window.data[i]=0.707;
  109. break;
  110. case W_HAMMING:
  111. for (int i=0;i<nsamples;i++) window.data[i]=0.53836-0.46164*cos(2*M_PI*i/(nsamples+1.0));
  112. break;
  113. case W_HANN:
  114. for (int i=0;i<nsamples;i++) window.data[i]=0.5*(1.0-cos(2*M_PI*i/(nsamples-1.0)));
  115. break;
  116. case W_BLACKMAN:
  117. for (int i=0;i<nsamples;i++) window.data[i]=0.42-0.5*cos(2*M_PI*i/(nsamples-1.0))+0.08*cos(4*M_PI*i/(nsamples-1.0));
  118. break;
  119. case W_BLACKMAN_HARRIS:
  120. for (int i=0;i<nsamples;i++) window.data[i]=0.35875-0.48829*cos(2*M_PI*i/(nsamples-1.0))+0.14128*cos(4*M_PI*i/(nsamples-1.0))-0.01168*cos(6*M_PI*i/(nsamples-1.0));
  121. break;
  122. };
  123. };
  124. for (int i=0;i<nsamples;i++) smp[i]*=window.data[i];
  125. };
  126. /*******************************************/
  127. Stretch::Stretch(REALTYPE rap_,int bufsize_,FFTWindow w,bool bypass_,REALTYPE samplerate_,int stereo_mode_){
  128. freezing=false;
  129. onset_detection_sensitivity=0.0;
  130. samplerate=samplerate_;
  131. rap=rap_;
  132. bufsize=bufsize_;
  133. bypass=bypass_;
  134. stereo_mode=stereo_mode_;
  135. if (bufsize<8) bufsize=8;
  136. out_buf=new REALTYPE[bufsize];
  137. old_freq=new REALTYPE[bufsize];
  138. very_old_smps=new REALTYPE[bufsize];
  139. new_smps=new REALTYPE[bufsize];
  140. old_smps=new REALTYPE[bufsize];
  141. old_out_smps=new REALTYPE[bufsize*2];
  142. for (int i=0;i<bufsize*2;i++) {
  143. old_out_smps[i]=0.0;
  144. };
  145. for (int i=0;i<bufsize;i++) {
  146. old_freq[i]=0.0;
  147. new_smps[i]=0.0;
  148. old_smps[i]=0.0;
  149. very_old_smps[i]=0.0;
  150. };
  151. infft=new FFT(bufsize*2);
  152. fft=new FFT(bufsize*2);
  153. outfft=new FFT(bufsize*2);
  154. remained_samples=0.0;
  155. window_type=w;
  156. require_new_buffer=false;
  157. c_pos_percents=0.0;
  158. extra_onset_time_credit=0.0;
  159. skip_samples=0;
  160. };
  161. Stretch::~Stretch(){
  162. delete [] old_freq;
  163. delete [] out_buf;
  164. delete [] new_smps;
  165. delete [] old_smps;
  166. delete [] very_old_smps;
  167. delete [] old_out_smps;
  168. delete fft;
  169. delete infft;
  170. delete outfft;
  171. };
  172. void Stretch::set_rap(REALTYPE newrap){
  173. //if ((rap>=1.0)&&(newrap>=1.0))
  174. rap=newrap;
  175. };
  176. void Stretch::do_analyse_inbuf(REALTYPE *smps){
  177. //get the frequencies
  178. for (int i=0;i<bufsize;i++) {
  179. infft->smp[i]=old_smps[i];
  180. infft->smp[i+bufsize]=smps[i];
  181. old_freq[i]=infft->freq[i];
  182. };
  183. infft->applywindow(window_type);
  184. infft->smp2freq();
  185. };
  186. void Stretch::do_next_inbuf_smps(REALTYPE *smps){
  187. for (int i=0;i<bufsize;i++) {
  188. very_old_smps[i]=old_smps[i];
  189. old_smps[i]=new_smps[i];
  190. new_smps[i]=smps[i];
  191. };
  192. };
  193. REALTYPE Stretch::do_detect_onset(){
  194. REALTYPE result=0.0;
  195. if (onset_detection_sensitivity>1e-3){
  196. REALTYPE os=0.0,osinc=0.0;
  197. REALTYPE osincold=1e-5;
  198. int maxk=1+(int)(bufsize*500.0/(samplerate*0.5));
  199. int k=0;
  200. for (int i=0;i<bufsize;i++) {
  201. osinc+=infft->freq[i]-old_freq[i];
  202. osincold+=old_freq[i];
  203. if (k>=maxk) {
  204. k=0;
  205. os+=osinc/osincold;
  206. osinc=0;
  207. };
  208. k++;
  209. };
  210. os+=osinc;
  211. if (os<0.0) os=0.0;
  212. //if (os>1.0) os=1.0;
  213. REALTYPE os_strength=pow(20.0,1.0-onset_detection_sensitivity)-1.0;
  214. REALTYPE os_strength_h=os_strength*0.75;
  215. if (os>os_strength_h){
  216. result=(os-os_strength_h)/(os_strength-os_strength_h);
  217. if (result>1.0) result=1.0;
  218. };
  219. if (result>1.0) result=1.0;
  220. };
  221. return result;
  222. };
  223. REALTYPE Stretch::process(REALTYPE *smps,int nsmps){
  224. REALTYPE onset=0.0;
  225. if (bypass){
  226. for (int i=0;i<bufsize;i++) out_buf[i]=smps[i];
  227. return 0.0;
  228. };
  229. if (smps!=NULL){
  230. if ((nsmps!=0)&&(nsmps!=bufsize)&&(nsmps!=get_max_bufsize())){
  231. printf("Warning wrong nsmps on Stretch::process() %d,%d\n",nsmps,bufsize);
  232. return 0.0;
  233. };
  234. if (nsmps!=0){//new data arrived: update the frequency components
  235. do_analyse_inbuf(smps);
  236. if (nsmps==get_max_bufsize()) {
  237. for (int k=bufsize;k<get_max_bufsize();k+=bufsize) do_analyse_inbuf(smps+k);
  238. };
  239. if (onset_detection_sensitivity>1e-3) onset=do_detect_onset();
  240. };
  241. //move the buffers
  242. if (nsmps!=0){//new data arrived: update the frequency components
  243. do_next_inbuf_smps(smps);
  244. if (nsmps==get_max_bufsize()) {
  245. for (int k=bufsize;k<get_max_bufsize();k+=bufsize) do_next_inbuf_smps(smps+k);
  246. };
  247. };
  248. //construct the input fft
  249. int start_pos=(int)(floor(remained_samples*bufsize));
  250. if (start_pos>=bufsize) start_pos=bufsize-1;
  251. for (int i=0;i<bufsize-start_pos;i++) fft->smp[i]=very_old_smps[i+start_pos];
  252. for (int i=0;i<bufsize;i++) fft->smp[i+bufsize-start_pos]=old_smps[i];
  253. for (int i=0;i<start_pos;i++) fft->smp[i+2*bufsize-start_pos]=new_smps[i];
  254. //compute the output spectrum
  255. fft->applywindow(window_type);
  256. fft->smp2freq();
  257. for (int i=0;i<bufsize;i++) outfft->freq[i]=fft->freq[i];
  258. //for (int i=0;i<bufsize;i++) outfft->freq[i]=infft->freq[i]*remained_samples+old_freq[i]*(1.0-remained_samples);
  259. process_spectrum(outfft->freq);
  260. outfft->freq2smp();
  261. //make the output buffer
  262. REALTYPE tmp=1.0/(float) bufsize*M_PI;
  263. REALTYPE hinv_sqrt2=0.853553390593;//(1.0+1.0/sqrt(2))*0.5;
  264. REALTYPE ampfactor=2.0;
  265. //remove the resulted unwanted amplitude modulation (caused by the interference of N and N+1 windowed buffer and compute the output buffer
  266. for (int i=0;i<bufsize;i++) {
  267. REALTYPE a=(0.5+0.5*cos(i*tmp));
  268. REALTYPE out=outfft->smp[i+bufsize]*(1.0-a)+old_out_smps[i]*a;
  269. out_buf[i]=out*(hinv_sqrt2-(1.0-hinv_sqrt2)*cos(i*2.0*tmp))*ampfactor;
  270. };
  271. //copy the current output buffer to old buffer
  272. for (int i=0;i<bufsize*2;i++) old_out_smps[i]=outfft->smp[i];
  273. };
  274. if (!freezing){
  275. long double used_rap=rap*get_stretch_multiplier(c_pos_percents);
  276. long double r=1.0/used_rap;
  277. if (extra_onset_time_credit>0){
  278. REALTYPE credit_get=0.5*r;//must be smaller than r
  279. extra_onset_time_credit-=credit_get;
  280. if (extra_onset_time_credit<0.0) extra_onset_time_credit=0.0;
  281. r-=credit_get;
  282. };
  283. long double old_remained_samples_test=remained_samples;
  284. remained_samples+=r;
  285. int result=0;
  286. if (remained_samples>=1.0){
  287. skip_samples=(int)(floor(remained_samples-1.0)*bufsize);
  288. remained_samples=remained_samples-floor(remained_samples);
  289. require_new_buffer=true;
  290. }else{
  291. require_new_buffer=false;
  292. };
  293. };
  294. // long double rf_test=remained_samples-old_remained_samples_test;//this value should be almost like "rf" (for most of the time with the exception of changing the "ri" value) for extremely long stretches (otherwise the shown stretch value is not accurate)
  295. //for stretch up to 10^18x "long double" must have at least 64 bits in the fraction part (true for gcc compiler on x86 and macosx)
  296. return onset;
  297. };
  298. void Stretch::here_is_onset(REALTYPE onset){
  299. if (freezing) return;
  300. if (onset>0.5){
  301. require_new_buffer=true;
  302. extra_onset_time_credit+=1.0-remained_samples;
  303. remained_samples=0.0;
  304. skip_samples=0;
  305. };
  306. };
  307. int Stretch::get_nsamples(REALTYPE current_pos_percents){
  308. if (bypass) return bufsize;
  309. if (freezing) return 0;
  310. c_pos_percents=current_pos_percents;
  311. return require_new_buffer?bufsize:0;
  312. };
  313. int Stretch::get_nsamples_for_fill(){
  314. return get_max_bufsize();
  315. };
  316. int Stretch::get_skip_nsamples(){
  317. if (freezing||bypass) return 0;
  318. return skip_samples;
  319. };
  320. REALTYPE Stretch::get_stretch_multiplier(REALTYPE pos_percents){
  321. return 1.0;
  322. };