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.

491 lines
13KB

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