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.

458 lines
12KB

  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.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, int nfreq, double samplerate, REALTYPE *freq1, REALTYPE *freq2) {
  231. REALTYPE freq = pars.harmonics.freq;
  232. REALTYPE bandwidth = pars.harmonics.bandwidth;
  233. int nharmonics = pars.harmonics.nharmonics;
  234. if (freq<10.0) freq = 10.0;
  235. REALTYPE *amp = tmpfreq1.data();
  236. for (int i = 0; i<nfreq; i++) amp[i] = 0.0;
  237. for (int nh = 1; nh <= nharmonics; nh++) {//for each harmonic
  238. REALTYPE bw_Hz;//bandwidth of the current harmonic measured in Hz
  239. REALTYPE bwi;
  240. REALTYPE fi;
  241. REALTYPE f = nh * freq;
  242. if (f >= samplerate / 2) break;
  243. bw_Hz = (pow(2.0f, bandwidth / 1200.0f) - 1.0f)*f;
  244. bwi = bw_Hz / (2.0f*samplerate);
  245. fi = f / samplerate;
  246. REALTYPE sum = 0.0f;
  247. REALTYPE max = 0.0f;
  248. for (int i = 1; i<nfreq; i++) {//todo: optimize here
  249. REALTYPE hprofile;
  250. hprofile = profile((i / (REALTYPE)nfreq*0.5f) - fi, bwi);
  251. amp[i] += hprofile;
  252. if (max<hprofile) max = hprofile;
  253. sum += hprofile;
  254. };
  255. };
  256. REALTYPE max = 0.0;
  257. for (int i = 1; i<nfreq; i++) {
  258. if (amp[i]>max) max = amp[i];
  259. };
  260. if (max<1e-8f) max = 1e-8f;
  261. for (int i = 1; i<nfreq; i++) {
  262. //REALTYPE c,s;
  263. REALTYPE a = amp[i] / max;
  264. if (!pars.harmonics.gauss) a = (a<0.368f ? 0.0f : 1.0f);
  265. freq2[i] = freq1[i] * a;
  266. };
  267. };
  268. inline void spectrum_add(int nfreq, REALTYPE *freq2, REALTYPE *freq1, REALTYPE a) {
  269. for (int i = 0; i<nfreq; i++) freq2[i] += freq1[i] * a;
  270. };
  271. inline void spectrum_zero(int nfreq,REALTYPE *freq1) {
  272. for (int i = 0; i<nfreq; i++) freq1[i] = 0.0;
  273. };
  274. inline void spectrum_do_freq_shift(const ProcessParameters& pars, int nfreq, double samplerate, REALTYPE *freq1, REALTYPE *freq2) {
  275. spectrum_zero(nfreq, freq2);
  276. int ifreq = (int)(pars.freq_shift.Hz / (samplerate*0.5)*nfreq);
  277. for (int i = 0; i<nfreq; i++) {
  278. int i2 = ifreq + i;
  279. if ((i2>0) && (i2<nfreq)) freq2[i2] = freq1[i];
  280. };
  281. };
  282. inline void spectrum_do_pitch_shift(const ProcessParameters& pars, int nfreq, REALTYPE *freq1, REALTYPE *freq2, REALTYPE _rap) {
  283. spectrum_zero(nfreq,freq2);
  284. if (_rap<1.0) {//down
  285. for (int i = 0; i<nfreq; i++) {
  286. int i2 = (int)(i*_rap);
  287. if (i2 >= nfreq) break;
  288. freq2[i2] += freq1[i];
  289. };
  290. };
  291. if (_rap >= 1.0) {//up
  292. _rap = 1.0f / _rap;
  293. for (int i = 0; i<nfreq; i++) {
  294. freq2[i] = freq1[(int)(i*_rap)];
  295. };
  296. };
  297. };
  298. inline void spectrum_do_octave(const ProcessParameters& pars, int nfreq, double samplerate,
  299. std::vector<REALTYPE>& sumfreq,
  300. std::vector<REALTYPE>& tmpfreq1,
  301. REALTYPE *freq1, REALTYPE *freq2) {
  302. spectrum_zero(nfreq,sumfreq.data());
  303. if (pars.octave.om2>1e-3) {
  304. spectrum_do_pitch_shift(pars,nfreq, freq1, tmpfreq1.data(), 0.25);
  305. spectrum_add(nfreq, sumfreq.data(), tmpfreq1.data(), pars.octave.om2);
  306. };
  307. if (pars.octave.om1>1e-3) {
  308. spectrum_do_pitch_shift(pars,nfreq, freq1, tmpfreq1.data(), 0.5);
  309. spectrum_add(nfreq,sumfreq.data(), tmpfreq1.data(), pars.octave.om1);
  310. };
  311. if (pars.octave.o0>1e-3) {
  312. spectrum_add(nfreq,sumfreq.data(), freq1, pars.octave.o0);
  313. };
  314. if (pars.octave.o1>1e-3) {
  315. spectrum_do_pitch_shift(pars,nfreq, freq1, tmpfreq1.data(), 2.0);
  316. spectrum_add(nfreq,sumfreq.data(), tmpfreq1.data(), pars.octave.o1);
  317. };
  318. if (pars.octave.o15>1e-3) {
  319. spectrum_do_pitch_shift(pars,nfreq, freq1, tmpfreq1.data(), 3.0);
  320. spectrum_add(nfreq,sumfreq.data(), tmpfreq1.data(), pars.octave.o15);
  321. };
  322. if (pars.octave.o2>1e-3) {
  323. spectrum_do_pitch_shift(pars, nfreq, freq1, tmpfreq1.data(), 4.0);
  324. spectrum_add(nfreq,sumfreq.data(), tmpfreq1.data(), pars.octave.o2);
  325. };
  326. REALTYPE sum = 0.01f + pars.octave.om2 + pars.octave.om1 + pars.octave.o0 + pars.octave.o1 + pars.octave.o15 + pars.octave.o2;
  327. if (sum<0.5f) sum = 0.5f;
  328. for (int i = 0; i<nfreq; i++) freq2[i] = sumfreq[i] / sum;
  329. };
  330. inline void spectrum_do_filter(const ProcessParameters& pars, int nfreq, double samplerate, REALTYPE *freq1, REALTYPE *freq2) {
  331. REALTYPE low = 0, high = 0;
  332. if (pars.filter.low<pars.filter.high) {//sort the low/high freqs
  333. low = pars.filter.low;
  334. high = pars.filter.high;
  335. }
  336. else {
  337. high = pars.filter.low;
  338. low = pars.filter.high;
  339. };
  340. int ilow = (int)(low / samplerate * nfreq*2.0f);
  341. int ihigh = (int)(high / samplerate * nfreq*2.0f);
  342. REALTYPE dmp = 1.0;
  343. REALTYPE dmprap = 1.0f - pow(pars.filter.hdamp*0.5f, 4.0f);
  344. for (int i = 0; i<nfreq; i++) {
  345. REALTYPE a = 0.0f;
  346. if ((i >= ilow) && (i<ihigh)) a = 1.0f;
  347. if (pars.filter.stop) a = 1.0f - a;
  348. freq2[i] = freq1[i] * a*dmp;
  349. dmp *= dmprap + 1e-8f;
  350. };
  351. };
  352. class SpectrumProcess
  353. {
  354. public:
  355. SpectrumProcess() {}
  356. SpectrumProcess(int index, bool enabled) : m_index(index), m_enabled(enabled) {}
  357. int m_index = -1;
  358. bool m_enabled = true;
  359. };
  360. class ProcessedStretch final : public Stretch
  361. {
  362. public:
  363. //stereo_mode: 0=mono,1=left,2=right
  364. ProcessedStretch(REALTYPE rap_,int in_bufsize_,FFTWindow w=W_HAMMING,bool bypass_=false,REALTYPE samplerate_=44100.0f,int stereo_mode=0);
  365. ~ProcessedStretch();
  366. void set_parameters(ProcessParameters *ppar);
  367. std::vector<SpectrumProcess> m_spectrum_processes;
  368. void setBufferSize(int sz) override;
  369. private:
  370. REALTYPE get_stretch_multiplier(REALTYPE pos_percents) override;
  371. // void process_output(REALTYPE *smps,int nsmps);
  372. void process_spectrum(REALTYPE *freq) override;
  373. //void copy(const realvector& freq1,realvector& freq2);
  374. void copy(REALTYPE* freq1, REALTYPE* freq2);
  375. void add(REALTYPE *freq2,REALTYPE *freq1,REALTYPE a=1.0);
  376. void mul(REALTYPE *freq1,REALTYPE a);
  377. void zero(REALTYPE *freq1);
  378. void update_free_filter();
  379. int nfreq=0;
  380. std::vector<REALTYPE> free_filter_freqs;
  381. ProcessParameters pars;
  382. std::vector<REALTYPE> infreq,sumfreq,tmpfreq1,tmpfreq2;
  383. //REALTYPE *fbfreq;
  384. };