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.

545 lines
15KB

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