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.

494 lines
14KB

  1. /*
  2. Copyright (C) 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. #pragma once
  16. #include "FreeEdit.h"
  17. #include "Stretch.h"
  18. struct ProcessParameters
  19. {
  20. ProcessParameters()
  21. {
  22. pitch_shift.enabled=false;
  23. pitch_shift.cents=0;
  24. octave.enabled=false;
  25. octave.om2=octave.om1=octave.o1=octave.o15=octave.o2=0.0f;
  26. octave.o0=1.0f;
  27. freq_shift.enabled=false;
  28. freq_shift.Hz=0;
  29. compressor.enabled=false;
  30. compressor.power=0.0f;
  31. filter.enabled=false;
  32. filter.stop=false;
  33. filter.low=0.0f;
  34. filter.high=22000.0f;
  35. filter.hdamp=0.0f;
  36. harmonics.enabled=false;
  37. harmonics.freq=440.0f;
  38. harmonics.bandwidth=25.0f;
  39. harmonics.nharmonics=10;
  40. harmonics.gauss=false;
  41. spread.enabled=false;
  42. spread.bandwidth=0.3f;
  43. tonal_vs_noise.enabled=false;
  44. tonal_vs_noise.preserve=0.5f;
  45. tonal_vs_noise.bandwidth=0.9f;
  46. };
  47. ~ProcessParameters(){
  48. };
  49. struct{
  50. bool enabled;
  51. int cents;
  52. }pitch_shift;
  53. struct{
  54. bool enabled;
  55. REALTYPE om2,om1,o0,o1,o15,o2;
  56. }octave;
  57. struct{
  58. bool enabled;
  59. int Hz;
  60. }freq_shift;
  61. struct{
  62. bool enabled;
  63. REALTYPE power;
  64. }compressor;
  65. struct{
  66. bool enabled;
  67. REALTYPE low,high;
  68. REALTYPE hdamp;
  69. bool stop;
  70. }filter;
  71. struct{
  72. bool enabled;
  73. REALTYPE freq;
  74. REALTYPE bandwidth;
  75. int nharmonics;
  76. bool gauss;
  77. }harmonics;
  78. struct{
  79. bool enabled;
  80. REALTYPE bandwidth;
  81. }spread;
  82. struct{
  83. bool enabled;
  84. REALTYPE preserve;
  85. REALTYPE bandwidth;
  86. }tonal_vs_noise;
  87. //FreeEdit free_filter;
  88. //FreeEdit stretch_multiplier;
  89. /*
  90. auto getMembers() const
  91. {
  92. return std::make_tuple(pitch_shift.enabled,
  93. pitch_shift.cents,
  94. octave.enabled,
  95. octave.o0,
  96. octave.o1,
  97. octave.o15,
  98. octave.o2,
  99. octave.om1,
  100. octave.om2,
  101. spread.enabled,
  102. spread.bandwidth,
  103. tonal_vs_noise.enabled,
  104. tonal_vs_noise.bandwidth,
  105. tonal_vs_noise.preserve,
  106. freq_shift.enabled,
  107. freq_shift.Hz,
  108. compressor.enabled,
  109. compressor.power,
  110. harmonics.bandwidth,
  111. harmonics.enabled,
  112. harmonics.freq,
  113. harmonics.gauss,
  114. harmonics.nharmonics,
  115. filter.enabled,
  116. filter.hdamp,
  117. filter.high,
  118. filter.low,
  119. filter.stop);
  120. }
  121. bool operator == (const ProcessParameters& other) const
  122. {
  123. return getMembers() == other.getMembers();
  124. }
  125. */
  126. bool operator == (const ProcessParameters& other) const noexcept
  127. {
  128. return pitch_shift.enabled == other.pitch_shift.enabled &&
  129. pitch_shift.cents == other.pitch_shift.cents &&
  130. octave.enabled == other.octave.enabled &&
  131. octave.o0 == other.octave.o0 &&
  132. octave.o1 == other.octave.o1 &&
  133. octave.o15 == other.octave.o15 &&
  134. octave.o2 == other.octave.o2 &&
  135. octave.om1 == other.octave.om1 &&
  136. octave.om2 == other.octave.om2 &&
  137. spread.enabled == other.spread.enabled &&
  138. spread.bandwidth == other.spread.bandwidth &&
  139. tonal_vs_noise.enabled == other.tonal_vs_noise.enabled &&
  140. tonal_vs_noise.bandwidth == other.tonal_vs_noise.bandwidth &&
  141. tonal_vs_noise.preserve == other.tonal_vs_noise.preserve &&
  142. freq_shift.enabled == other.freq_shift.enabled &&
  143. freq_shift.Hz == other.freq_shift.Hz &&
  144. compressor.enabled == other.compressor.enabled &&
  145. compressor.power == other.compressor.power &&
  146. harmonics.bandwidth == other.harmonics.bandwidth &&
  147. harmonics.enabled == other.harmonics.enabled &&
  148. harmonics.freq == other.harmonics.freq &&
  149. harmonics.gauss == other.harmonics.gauss &&
  150. harmonics.nharmonics == other.harmonics.nharmonics &&
  151. filter.enabled == other.filter.enabled &&
  152. filter.hdamp == other.filter.hdamp &&
  153. filter.high == other.filter.high &&
  154. filter.low == other.filter.low &&
  155. filter.stop == other.filter.stop;
  156. }
  157. };
  158. inline REALTYPE profile(REALTYPE fi, REALTYPE bwi) {
  159. REALTYPE x = fi / bwi;
  160. x *= x;
  161. if (x>14.71280603) return 0.0;
  162. return exp(-x);///bwi;
  163. };
  164. inline void spectrum_copy(int nfreq, REALTYPE* freq1, REALTYPE* freq2)
  165. {
  166. for (int i = 0; i<nfreq; i++) freq2[i] = freq1[i];
  167. };
  168. inline void spectrum_spread(int nfreq, double samplerate,
  169. std::vector<REALTYPE>& tmpfreq1,
  170. REALTYPE *freq1, REALTYPE *freq2, REALTYPE spread_bandwidth) {
  171. //convert to log spectrum
  172. REALTYPE minfreq = 20.0f;
  173. REALTYPE maxfreq = 0.5f*samplerate;
  174. REALTYPE log_minfreq = log(minfreq);
  175. REALTYPE log_maxfreq = log(maxfreq);
  176. for (int i = 0; i<nfreq; i++) {
  177. REALTYPE freqx = i / (REALTYPE)nfreq;
  178. REALTYPE x = exp(log_minfreq + freqx * (log_maxfreq - log_minfreq)) / maxfreq * nfreq;
  179. REALTYPE y = 0.0f;
  180. int x0 = (int)floor(x); if (x0 >= nfreq) x0 = nfreq - 1;
  181. int x1 = x0 + 1; if (x1 >= nfreq) x1 = nfreq - 1;
  182. REALTYPE xp = x - x0;
  183. if (x<nfreq) {
  184. y = freq1[x0] * (1.0f - xp) + freq1[x1] * xp;
  185. };
  186. tmpfreq1[i] = y;
  187. };
  188. //increase the bandwidth of each harmonic (by smoothing the log spectrum)
  189. int n = 2;
  190. REALTYPE bandwidth = spread_bandwidth;
  191. REALTYPE a = 1.0f - pow(2.0f, -bandwidth * bandwidth*10.0f);
  192. a = pow(a, 8192.0f / nfreq * n);
  193. for (int k = 0; k<n; k++) {
  194. tmpfreq1[0] = 0.0f;
  195. for (int i = 1; i<nfreq; i++) {
  196. tmpfreq1[i] = tmpfreq1[i - 1] * a + tmpfreq1[i] * (1.0f - a);
  197. };
  198. tmpfreq1[nfreq - 1] = 0.0f;
  199. for (int i = nfreq - 2; i>0; i--) {
  200. tmpfreq1[i] = tmpfreq1[i + 1] * a + tmpfreq1[i] * (1.0f - a);
  201. };
  202. };
  203. freq2[0] = 0;
  204. REALTYPE log_maxfreq_d_minfreq = log(maxfreq / minfreq);
  205. for (int i = 1; i<nfreq; i++) {
  206. REALTYPE freqx = i / (REALTYPE)nfreq;
  207. REALTYPE x = log((freqx*maxfreq) / minfreq) / log_maxfreq_d_minfreq * nfreq;
  208. REALTYPE y = 0.0;
  209. if ((x>0.0) && (x<nfreq)) {
  210. int x0 = (int)floor(x); if (x0 >= nfreq) x0 = nfreq - 1;
  211. int x1 = x0 + 1; if (x1 >= nfreq) x1 = nfreq - 1;
  212. REALTYPE xp = x - x0;
  213. y = tmpfreq1[x0] * (1.0f - xp) + tmpfreq1[x1] * xp;
  214. };
  215. freq2[i] = y;
  216. };
  217. };
  218. inline void spectrum_do_compressor(const ProcessParameters& pars, int nfreq, REALTYPE *freq1, REALTYPE *freq2) {
  219. REALTYPE rms = 0.0;
  220. for (int i = 0; i<nfreq; i++) rms += freq1[i] * freq1[i];
  221. rms = sqrt(rms / nfreq)*0.1f;
  222. if (rms<1e-3f) rms = 1e-3f;
  223. REALTYPE _rap = pow(rms, -pars.compressor.power);
  224. for (int i = 0; i<nfreq; i++) freq2[i] = freq1[i] * _rap;
  225. };
  226. inline void spectrum_do_tonal_vs_noise(const ProcessParameters& pars, int nfreq, double samplerate,
  227. std::vector<REALTYPE>& tmpfreq1,
  228. REALTYPE *freq1, REALTYPE *freq2) {
  229. spectrum_spread(nfreq, samplerate, tmpfreq1, freq1, tmpfreq1.data(), pars.tonal_vs_noise.bandwidth);
  230. if (pars.tonal_vs_noise.preserve >= 0.0) {
  231. REALTYPE mul = (pow(10.0f, pars.tonal_vs_noise.preserve) - 1.0f);
  232. for (int i = 0; i<nfreq; i++) {
  233. REALTYPE x = freq1[i];
  234. REALTYPE smooth_x = tmpfreq1[i] + 1e-6f;
  235. REALTYPE result = 0.0f;
  236. result = x - smooth_x * mul;
  237. if (result<0.0f) result = 0.0f;
  238. freq2[i] = result;
  239. };
  240. }
  241. else {
  242. REALTYPE mul = (pow(5.0f, 1.0f + pars.tonal_vs_noise.preserve) - 1.0f);
  243. for (int i = 0; i<nfreq; i++) {
  244. REALTYPE x = freq1[i];
  245. REALTYPE smooth_x = tmpfreq1[i] + 1e-6f;
  246. REALTYPE result = 0.0f;
  247. result = x - smooth_x * mul + 0.1f*mul;
  248. if (result<0.0f) result = x;
  249. else result = 0.0f;
  250. freq2[i] = result;
  251. };
  252. };
  253. };
  254. inline void spectrum_do_harmonics(const ProcessParameters& pars, std::vector<REALTYPE>& tmpfreq1, int nfreq, double samplerate, REALTYPE *freq1, REALTYPE *freq2) {
  255. REALTYPE freq = pars.harmonics.freq;
  256. REALTYPE bandwidth = pars.harmonics.bandwidth;
  257. int nharmonics = pars.harmonics.nharmonics;
  258. if (freq<10.0) freq = 10.0;
  259. REALTYPE *amp = tmpfreq1.data();
  260. for (int i = 0; i<nfreq; i++) amp[i] = 0.0;
  261. for (int nh = 1; nh <= nharmonics; nh++) {//for each harmonic
  262. REALTYPE bw_Hz;//bandwidth of the current harmonic measured in Hz
  263. REALTYPE bwi;
  264. REALTYPE fi;
  265. REALTYPE f = nh * freq;
  266. if (f >= samplerate / 2) break;
  267. bw_Hz = (pow(2.0f, bandwidth / 1200.0f) - 1.0f)*f;
  268. bwi = bw_Hz / (2.0f*samplerate);
  269. fi = f / samplerate;
  270. REALTYPE sum = 0.0f;
  271. REALTYPE max = 0.0f;
  272. for (int i = 1; i<nfreq; i++) {//todo: optimize here
  273. REALTYPE hprofile;
  274. hprofile = profile((i / (REALTYPE)nfreq*0.5f) - fi, bwi);
  275. amp[i] += hprofile;
  276. if (max<hprofile) max = hprofile;
  277. sum += hprofile;
  278. };
  279. };
  280. REALTYPE max = 0.0;
  281. for (int i = 1; i<nfreq; i++) {
  282. if (amp[i]>max) max = amp[i];
  283. };
  284. if (max<1e-8f) max = 1e-8f;
  285. for (int i = 1; i<nfreq; i++) {
  286. //REALTYPE c,s;
  287. REALTYPE a = amp[i] / max;
  288. if (!pars.harmonics.gauss) a = (a<0.368f ? 0.0f : 1.0f);
  289. freq2[i] = freq1[i] * a;
  290. };
  291. };
  292. inline void spectrum_add(int nfreq, REALTYPE *freq2, REALTYPE *freq1, REALTYPE a) {
  293. for (int i = 0; i<nfreq; i++) freq2[i] += freq1[i] * a;
  294. };
  295. inline void spectrum_zero(int nfreq,REALTYPE *freq1) {
  296. for (int i = 0; i<nfreq; i++) freq1[i] = 0.0;
  297. };
  298. inline void spectrum_do_freq_shift(const ProcessParameters& pars, int nfreq, double samplerate, REALTYPE *freq1, REALTYPE *freq2) {
  299. spectrum_zero(nfreq, freq2);
  300. int ifreq = (int)(pars.freq_shift.Hz / (samplerate*0.5)*nfreq);
  301. for (int i = 0; i<nfreq; i++) {
  302. int i2 = ifreq + i;
  303. if ((i2>0) && (i2<nfreq)) freq2[i2] = freq1[i];
  304. };
  305. };
  306. inline void spectrum_do_pitch_shift(const ProcessParameters& pars, int nfreq, REALTYPE *freq1, REALTYPE *freq2, REALTYPE _rap) {
  307. spectrum_zero(nfreq,freq2);
  308. if (_rap<1.0) {//down
  309. for (int i = 0; i<nfreq; i++) {
  310. int i2 = (int)(i*_rap);
  311. if (i2 >= nfreq) break;
  312. freq2[i2] += freq1[i];
  313. };
  314. };
  315. if (_rap >= 1.0) {//up
  316. _rap = 1.0f / _rap;
  317. for (int i = 0; i<nfreq; i++) {
  318. freq2[i] = freq1[(int)(i*_rap)];
  319. };
  320. };
  321. };
  322. inline void spectrum_do_octave(const ProcessParameters& pars, int nfreq, double samplerate,
  323. std::vector<REALTYPE>& sumfreq,
  324. std::vector<REALTYPE>& tmpfreq1,
  325. REALTYPE *freq1, REALTYPE *freq2) {
  326. spectrum_zero(nfreq,sumfreq.data());
  327. if (pars.octave.om2>1e-3) {
  328. spectrum_do_pitch_shift(pars,nfreq, freq1, tmpfreq1.data(), 0.25);
  329. spectrum_add(nfreq, sumfreq.data(), tmpfreq1.data(), pars.octave.om2);
  330. };
  331. if (pars.octave.om1>1e-3) {
  332. spectrum_do_pitch_shift(pars,nfreq, freq1, tmpfreq1.data(), 0.5);
  333. spectrum_add(nfreq,sumfreq.data(), tmpfreq1.data(), pars.octave.om1);
  334. };
  335. if (pars.octave.o0>1e-3) {
  336. spectrum_add(nfreq,sumfreq.data(), freq1, pars.octave.o0);
  337. };
  338. if (pars.octave.o1>1e-3) {
  339. spectrum_do_pitch_shift(pars,nfreq, freq1, tmpfreq1.data(), 2.0);
  340. spectrum_add(nfreq,sumfreq.data(), tmpfreq1.data(), pars.octave.o1);
  341. };
  342. if (pars.octave.o15>1e-3) {
  343. spectrum_do_pitch_shift(pars,nfreq, freq1, tmpfreq1.data(), 3.0);
  344. spectrum_add(nfreq,sumfreq.data(), tmpfreq1.data(), pars.octave.o15);
  345. };
  346. if (pars.octave.o2>1e-3) {
  347. spectrum_do_pitch_shift(pars, nfreq, freq1, tmpfreq1.data(), 4.0);
  348. spectrum_add(nfreq,sumfreq.data(), tmpfreq1.data(), pars.octave.o2);
  349. };
  350. REALTYPE sum = 0.01f + pars.octave.om2 + pars.octave.om1 + pars.octave.o0 + pars.octave.o1 + pars.octave.o15 + pars.octave.o2;
  351. if (sum<0.5f) sum = 0.5f;
  352. for (int i = 0; i<nfreq; i++) freq2[i] = sumfreq[i] / sum;
  353. };
  354. inline void spectrum_do_filter(const ProcessParameters& pars, int nfreq, double samplerate, REALTYPE *freq1, REALTYPE *freq2) {
  355. REALTYPE low = 0, high = 0;
  356. if (pars.filter.low<pars.filter.high) {//sort the low/high freqs
  357. low = pars.filter.low;
  358. high = pars.filter.high;
  359. }
  360. else {
  361. high = pars.filter.low;
  362. low = pars.filter.high;
  363. };
  364. int ilow = (int)(low / samplerate * nfreq*2.0f);
  365. int ihigh = (int)(high / samplerate * nfreq*2.0f);
  366. REALTYPE dmp = 1.0;
  367. REALTYPE dmprap = 1.0f - pow(pars.filter.hdamp*0.5f, 4.0f);
  368. for (int i = 0; i<nfreq; i++) {
  369. REALTYPE a = 0.0f;
  370. if ((i >= ilow) && (i<ihigh)) a = 1.0f;
  371. if (pars.filter.stop) a = 1.0f - a;
  372. freq2[i] = freq1[i] * a*dmp;
  373. dmp *= dmprap + 1e-8f;
  374. };
  375. };
  376. class SpectrumProcess
  377. {
  378. public:
  379. SpectrumProcess() {}
  380. SpectrumProcess(String name, int index) :
  381. m_name(name), m_index(index) {}
  382. String m_name;
  383. int m_index = -1;
  384. private:
  385. };
  386. std::vector<SpectrumProcess> make_spectrum_processes();
  387. class ProcessedStretch final : public Stretch
  388. {
  389. public:
  390. //stereo_mode: 0=mono,1=left,2=right
  391. ProcessedStretch(REALTYPE rap_,int in_bufsize_,FFTWindow w=W_HAMMING,bool bypass_=false,REALTYPE samplerate_=44100.0f,int stereo_mode=0);
  392. ~ProcessedStretch();
  393. void set_parameters(ProcessParameters *ppar);
  394. std::vector<int> m_spectrum_processes;
  395. void setBufferSize(int sz) override;
  396. private:
  397. REALTYPE get_stretch_multiplier(REALTYPE pos_percents) override;
  398. // void process_output(REALTYPE *smps,int nsmps);
  399. void process_spectrum(REALTYPE *freq) override;
  400. void do_harmonics(REALTYPE *freq1,REALTYPE *freq2);
  401. void do_pitch_shift(REALTYPE *freq1,REALTYPE *freq2,REALTYPE rap);
  402. void do_freq_shift(REALTYPE *freq1,REALTYPE *freq2);
  403. void do_octave(REALTYPE *freq1,REALTYPE *freq2);
  404. void do_filter(REALTYPE *freq1,REALTYPE *freq2);
  405. void do_free_filter(REALTYPE *freq1,REALTYPE *freq2);
  406. void do_compressor(REALTYPE *freq1,REALTYPE *freq2);
  407. void do_spread(REALTYPE *freq1,REALTYPE *freq2);
  408. void do_tonal_vs_noise(REALTYPE *freq1,REALTYPE *freq2);
  409. //void copy(const realvector& freq1,realvector& freq2);
  410. void copy(REALTYPE* freq1, REALTYPE* freq2);
  411. void add(REALTYPE *freq2,REALTYPE *freq1,REALTYPE a=1.0);
  412. void mul(REALTYPE *freq1,REALTYPE a);
  413. void zero(REALTYPE *freq1);
  414. void spread(REALTYPE *freq1,REALTYPE *freq2,REALTYPE spread_bandwidth);
  415. void update_free_filter();
  416. int nfreq=0;
  417. std::vector<REALTYPE> free_filter_freqs;
  418. ProcessParameters pars;
  419. std::vector<REALTYPE> infreq,sumfreq,tmpfreq1,tmpfreq2;
  420. //REALTYPE *fbfreq;
  421. };