pvpp.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * Copyright (c) 1983-2020 Richard Dobson and Composers Desktop Project Ltd
  3. * http://www.rwdobson.com
  4. * http://www.composersdesktop.com
  5. * This file is part of the CDP System.
  6. * The CDP System is free software; you can redistribute it
  7. * and/or modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * The CDP System is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. * See the GNU Lesser General Public License for more details.
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with the CDP System; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. */
  20. /*pvpp.h*/
  21. /* class def for pvoc wrapper */
  22. #ifndef __PVPP_H_INCLUDED__
  23. #define __PVPP_H_INCLUDED__
  24. #ifndef MIN
  25. #define MIN(x,y) ( ((x)>(y)) ? (y) : (x) )
  26. #endif
  27. #ifndef MAX
  28. #define MAX(x,y) ( ((x)>(y)) ? (x) : (y) )
  29. #endif
  30. #ifdef __cplusplus
  31. extern "C" {
  32. //#include <pvfileio.h>
  33. #endif
  34. #include "pvdefs.h"
  35. #ifdef USE_FFTW
  36. #include <rfftw.h>
  37. #endif
  38. #ifdef __cplusplus
  39. }
  40. #endif
  41. #ifdef MAC
  42. #include <sys/param.h>
  43. #include <sys/sysctl.h>
  44. #include <CoreServices/CoreServices.h>
  45. //#include <Accelerate/Accelerate.h>
  46. #endif
  47. //}
  48. // must have -faltivec in C compiler flags
  49. #if defined(__VEC__)
  50. Boolean HasAltiVec(void);
  51. void TurnJavaModeOffOnG4(void);
  52. void RestoreJavaModeOnG4(void);
  53. #endif
  54. #ifdef NOTDEF
  55. typedef enum pvocmode { PVPP_NOT_SET,PVPP_OFFLINE,PVPP_STREAMING};
  56. typedef enum pvoc_overlapfac {PVOC_O_W0,PVOC_O_W1,PVOC_O_W2,PVOC_O_W3,PVOC_O_DEFAULT};
  57. typedef enum pvoc_scaletype {PVOC_S_TIME,PVOC_S_PITCH,PVOC_S_NONE};
  58. typedef enum pvoc_frametype { PVOC_AMP_FREQ,PVOC_AMP_PHASE,PVOC_COMPLEX };
  59. typedef enum pvoc_windowtype {PVOC_DEFAULT,
  60. PVOC_HAMMING,
  61. PVOC_HANN,
  62. PVOC_KAISER,
  63. PVOC_RECT,
  64. PVOC_CUSTOM};
  65. #endif
  66. class phasevocoder
  67. {
  68. /* some may be made protected in due course, for derived classes */
  69. private:
  70. double rratio;
  71. float *input, /* pointer to start of input buffer */
  72. *output, /* pointer to start of output buffer */
  73. *anal, /* pointer to start of analysis buffer */
  74. *syn, /* pointer to start of synthesis buffer */
  75. *banal, /* pointer to anal[1] (for FFT calls) */
  76. *bsyn, /* pointer to syn[1] (for FFT calls) */
  77. *nextIn, /* pointer to next empty word in input */
  78. *nextOut, /* pointer to next empty word in output */
  79. *analWindow, /* pointer to center of analysis window */
  80. *synWindow, /* pointer to center of synthesis window */
  81. *maxAmp, /* pointer to start of max amp buffer */
  82. *avgAmp, /* pointer to start of avg amp buffer */
  83. *avgFrq, /* pointer to start of avg frq buffer */
  84. *env, /* pointer to start of spectral envelope */
  85. *i0, /* pointer to amplitude channels */
  86. *i1, /* pointer to frequency channels */
  87. *oi, /* pointer to old phase channels */
  88. *oldInPhase, /* pointer to start of input phase buffer */
  89. *oldOutPhase; /* pointer to start of output phase buffer */
  90. int m, n;
  91. int N , /* number of phase vocoder channels (bands) */
  92. M, /* length of analWindow impulse response */
  93. L, /* length of synWindow impulse response */
  94. D, /* decimation factor (default will be M/8) */
  95. I, /* interpolation factor (default will be I=D)*/
  96. W , /* filter overlap factor (determines M, L) */
  97. /*RWD: want EXACT frequency! */
  98. // F, /* fundamental frequency (determines N) */
  99. // F2, /* F/2 */ /* RWD NOT USED */
  100. K, /* set kaiser window*/
  101. H, /* set vonHann window */
  102. analWinLen, /* half-length of analysis window */
  103. synWinLen; /* half-length of synthesis window */
  104. /* RWD see above */
  105. float Fexact;
  106. long // tmprate, /* temporary variable */
  107. sampsize, /* sample size for output file */
  108. //origrate, /* sample rate of file analysed */
  109. outCount, /* number of samples written to output */
  110. ibuflen, /* length of input buffer */
  111. obuflen, /* length of output buffer */
  112. nI, /* current input (analysis) sample */
  113. nO, /* current output (synthesis) sample */
  114. nMaxOut, /* last output (synthesis) sample */
  115. nMin, /* first input (analysis) sample */
  116. nMax; /* last input sample (unless EOF) */
  117. /***************************** 6:2:91 OLD CODE **************
  118. long origsize;
  119. *******************************NEW CODE **********************/
  120. long origsize; /* sample type of file analysed */
  121. char ch; /* needed for crack (commandline interpreter)*/
  122. int ifd, ofd; /* CDP sound file handles */
  123. float beta , /* parameter for Kaiser window */
  124. real, /* real part of analysis data */
  125. imag, /* imaginary part of analysis data */
  126. mag, /* magnitude of analysis data */
  127. phase, /* phase of analysis data */
  128. angleDif, /* angle difference */
  129. RoverTwoPi, /* R/D divided by 2*Pi */
  130. TwoPioverR, /* 2*Pi divided by R/I */
  131. sum, /* scale factor for renormalizing windows */
  132. ftot, /* scale factor for calculating statistics */
  133. rIn, /* decimated sampling rate */
  134. rOut, /* pre-interpolated sampling rate */
  135. invR, /* 1. / srate */
  136. time, /* nI / srate */
  137. tvx0, /* current x value of time-var function */
  138. tvx1, /* next x value of time-var function */
  139. tvdx, /* tvx1 - tvx0 */
  140. tvy0, /* current y value of time-var function */
  141. tvy1, /* next y value of time-var function */
  142. tvdy, /* tvy1 - tvy0 */
  143. frac, /* tvdy / tvdx */
  144. warp, /* spectral envelope warp factor */
  145. R , /* input sampling rate */
  146. P , /* pitch scale factor */
  147. Pinv, /* 1. / P */
  148. T; /* time scale factor ( >1 to expand)*/
  149. int i,j,k, /* index variables */
  150. Dd, /* number of new inputs to read (Dd <= D) */
  151. Ii, /* number of new outputs to write (Ii <= I) */
  152. N2, /* N/2 */
  153. NO, /* synthesis NO = N / P */
  154. NO2, /* NO/2 */
  155. IO, /* synthesis IO = I / P */
  156. IOi, /* synthesis IOi = Ii / P */
  157. //Mlen,
  158. Mf, /* flag for even M */
  159. Lf, /* flag for even L */
  160. //Dfac,
  161. flag, /* end-of-input flag */
  162. C, /* flag for resynthesizing even or odd chans */
  163. Ci, /* flag for resynthesizing chans i to j only */
  164. Cj, /* flag for resynthesizing chans i to j only */
  165. CC, /* flag for selected channel resynthesis */
  166. X, /* flag for magnitude output */
  167. E, /* flag for spectral envelope output */
  168. tvflg, /* flag for time-varying time-scaling */
  169. tvnxt, /* counter for stepping thru time-var func */
  170. tvlen; /* length of time-varying function */
  171. float srate; /* sample rate from header on stdin */
  172. float timecheckf;
  173. long isr, /* sampling rate */
  174. Nchans; /* no of chans */
  175. /* my vars */
  176. int vH; /* von Hann window */
  177. pvocmode m_mode;
  178. long bin_index; /* index into oldOutPhase to do fast norm_phase */
  179. float *synWindow_base;
  180. float *analWindow_base;
  181. #ifdef USE_FFTW
  182. rfftwnd_plan forward_plan, inverse_plan;
  183. int in_fftw_size,out_fftw_size;
  184. float Ninv;
  185. #endif
  186. protected:
  187. double besseli( double x); /* from Csound*/
  188. void hamming(float *win,int winLen,int even);
  189. void kaiser(float *win,int len,double Beta); /* from Csound */
  190. void vonhann(float *win,int winLen,int even);
  191. public:
  192. phasevocoder();
  193. virtual ~phasevocoder();
  194. /* neither init func inputs all possible pvoc parameters - these are two possible subsets */
  195. bool init(long outsrate,long fftlen,long winlen,long decfac,float scalefac,
  196. pvoc_scaletype stype,pvoc_windowtype wtype,pvocmode mode);
  197. /* when pvoc -W flag used (ofac), it sets anal window size, decimation, etc */
  198. bool init(long outsrate,long fftlen,pvoc_overlapfac ofac,float scalefac,
  199. pvoc_scaletype stype,pvoc_windowtype wtype,pvocmode mode);
  200. /* to come: full init using PVOCDATA etc, or directly from another phasevocoder */
  201. long anal_overlap(void) { return D;}
  202. long syn_interp(void) {
  203. if(P != 1.0f)
  204. return D;
  205. else
  206. return I;}
  207. long fftlen(void) {return N;}
  208. long winlen(void) { return M;}
  209. /*retval gives numsamps written to outbuf, -1 for error */
  210. long process_frame(float *frame,float *outbuf,pvoc_frametype frametype);
  211. long process_frame(float *frame,short *outbuf,pvoc_frametype frametype);
  212. /* returns (variable) numsamps written to outbuf*/
  213. long generate_frame(float *fbuf,float *outanal,long samps,pvoc_frametype frametype);
  214. void reset_phases(void);
  215. #ifdef USE_CDP
  216. void setprops(SFPROPS *props);
  217. #endif
  218. };
  219. #endif