madde_synth.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*
  2. This file is part of QTau
  3. Copyright (C) 2013-2018 Tobias "Tomoko" Platen <tplaten@posteo.de>
  4. Copyright (C) 2013 digited <https://github.com/digited>
  5. Copyright (C) 2010-2013 HAL@ShurabaP <https://github.com/haruneko>
  6. QTau is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. SPDX-License-Identifier: GPL-3.0+
  17. */
  18. // [a] from Hatsune Miku, estimated using praat
  19. // used to configure EpR voice model, see http://www.mtg.upf.edu/node/1231
  20. float init_f[6] = {915.597,1408.48,1892.801,4025.529,4509.241,5500};
  21. float init_bw[6] = {174.803,167.995,125.245 ,411.338 ,429.417, 0};
  22. float init_gain_db[6] = {15,15,10,5,5,2};
  23. float init_controls[3] = { 22.5, -7E-5, 77 };
  24. #include <sekai/midi.h>
  25. /* --------------------------------------------------------------------------- *
  26. *
  27. This program is free software: you can redistribute it and/or modify
  28. it under the terms of the GNU General Public License as published by
  29. the Free Software Foundation, either version 3 of the License, or
  30. (at your option) any later version.
  31. This program is distributed in the hope that it will be useful,
  32. but WITHOUT ANY WARRANTY; without even the implied warranty of
  33. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  34. GNU General Public License for more details.
  35. You should have received a copy of the GNU General Public License
  36. along with this program. If not, see <http://www.gnu.org/licenses/>.
  37. * --------------------------------------------------------------------------- */
  38. #define __devloglevel__ 20
  39. #include "madde_synth.h"
  40. #include <unistd.h>
  41. #include <QAction>
  42. #include <QTimer>
  43. #include "maddefile.h"
  44. #include <QtConcurrent/QtConcurrent>
  45. QString MaddeLOIDSynth::name() { return "MaddeLOID"; }
  46. QString MaddeLOIDSynth::description() { return "An additive singing synthesizer"; }
  47. QString MaddeLOIDSynth::version() { return "18.04"; }
  48. #include "maddewindow.h"
  49. #include <math.h>
  50. void MaddeLOIDSynth::start(float keyNum)
  51. {
  52. if(_threadRunning) return;
  53. _f0 = frequencyFromNote(keyNum);
  54. _phase = 0;
  55. // _ctrl->startThread(this);
  56. QtConcurrent::run(this,&MaddeLOIDSynth::run);
  57. }
  58. void MaddeLOIDSynth::stop()
  59. {
  60. _f0 = 0;
  61. }
  62. int MaddeLOIDSynth::run()
  63. {
  64. _threadRunning=true;
  65. while(_f0) {
  66. while(jack_ringbuffer_write_space(_ringbuffer)<FFT_SIZE/2*sizeof(float)) {
  67. usleep(1000);
  68. if(_f0==0) break;
  69. }
  70. if(_f0){
  71. int nharmonics = 50;
  72. for(int i=0;i<6;i++)
  73. {
  74. if(_resNeedsUpdate[i])
  75. {
  76. EprResonanceUpdate(&_res[i],_osc.fs);
  77. _resNeedsUpdate[i]=false;
  78. }
  79. }
  80. for(int i=0;i<50;i++)
  81. {
  82. if(_f0>0) {
  83. float factor = 0.1;
  84. float f = _f0*(i+1);
  85. double gain = EprAtFrequency(&_src,f,_osc.fs,_res,RES_COUNT);
  86. _osc.amp[i] = pow(M_E,(gain/TWENTY_OVER_LOG10))/nharmonics*factor;
  87. _osc.frq[i] = f;
  88. }
  89. else {
  90. _osc.amp[i]=0;
  91. _osc.frq[i]=0;
  92. }
  93. }
  94. _osc.processOneFrame();
  95. float output_buffer[FFT_SIZE/2];
  96. for(int i=0;i<FFT_SIZE/2;i++)
  97. {
  98. output_buffer[i] = _osc.output_buffer[i]*0.9;
  99. }
  100. jack_ringbuffer_write(_ringbuffer,(char*)output_buffer,FFT_SIZE/2*sizeof(float));
  101. };
  102. }
  103. _threadRunning=false;
  104. return 0;
  105. }
  106. void MaddeLOIDSynth::readData(float* data,int dataLength)
  107. {
  108. unsigned int dataLength2 = dataLength*sizeof(float);
  109. unsigned int readSpace = jack_ringbuffer_read_space(_ringbuffer);
  110. readSpace = jack_ringbuffer_read_space(_ringbuffer);
  111. if(readSpace>dataLength2)
  112. jack_ringbuffer_read(_ringbuffer,(char*)data,dataLength2);
  113. }
  114. void MaddeLOIDSynth::setup2() //rename to setupUI
  115. {
  116. QAction* madde = new QAction("MaddeLOID",this);
  117. _ctrl->addPluginAction(madde);
  118. connect(madde,SIGNAL(triggered()),this,SLOT(madde_triggered()));
  119. }
  120. void MaddeLOIDSynth::madde_triggered()
  121. {
  122. if(_madde) return;
  123. _madde = new MaddeWindow();
  124. _madde->setVisible(true);
  125. connect(_madde,SIGNAL(loadDefault()),this,SLOT(loadDefault()));
  126. connect(_madde,SIGNAL(loadFile(QString)),this,SLOT(loadFile(QString)));
  127. connect(_madde,SIGNAL(saveToFile(QString)),this,SLOT(saveToFile(QString)));
  128. connect(_madde,SIGNAL(mouseEvent(int)),this,SLOT(mouseEvent(int)));
  129. connect(_madde,SIGNAL(valueChanged(QString,QVariant)),this,SLOT(madde_valueChanged(QString,QVariant)));
  130. connect(_madde,SIGNAL(closed()),this,SLOT(madde_closed()));
  131. for(int i=0;i<6;i++)
  132. {
  133. _madde->setValue("F"+QVariant(i+1).toString(),init_f[i]);
  134. _madde->setValue("BW"+QVariant(i+1).toString(),init_bw[i]);
  135. _madde->setValue("A"+QVariant(i+1).toString(),init_gain_db[i]);
  136. if(i!=5) {
  137. _madde->setValue("UseF"+QVariant(i+1).toString(),true);
  138. _resEnabled[i]=true;
  139. } else
  140. _resEnabled[i]=false;
  141. _resNeedsUpdate[i]=false;
  142. }
  143. _madde->setValue("gaindb",init_controls[0]);
  144. _madde->setValue("slope",init_controls[1]);
  145. _madde->setValue("slopedepthdb",init_controls[2]);
  146. }
  147. void MaddeLOIDSynth::loadDefault()
  148. {
  149. for(int i=0;i<6;i++)
  150. {
  151. _madde->setValue("F"+QVariant(i+1).toString(),init_f[i]);
  152. _madde->setValue("BW"+QVariant(i+1).toString(),init_bw[i]);
  153. _madde->setValue("A"+QVariant(i+1).toString(),init_gain_db[i]);
  154. if(i!=5) {
  155. _madde->setValue("UseF"+QVariant(i+1).toString(),true);
  156. _resEnabled[i]=true;
  157. } else
  158. _resEnabled[i]=false;
  159. _resNeedsUpdate[i]=false;
  160. }
  161. //synth
  162. for(int i=0;i<RES_COUNT;i++)
  163. {
  164. _res[i].f = init_f[i];
  165. _res[i].bw = init_bw[i];
  166. _res[i].gain_db = init_gain_db[i];
  167. _res[i].enabled=1;
  168. EprResonanceUpdate(&_res[i],_osc.fs);
  169. }
  170. _res[5].enabled=0;
  171. _src.gaindb = init_controls[0];
  172. _src.slope = init_controls[1];
  173. _src.slopedepthdb = init_controls[2];
  174. _madde->setValue("gaindb",init_controls[0]);
  175. _madde->setValue("slope",init_controls[1]);
  176. _madde->setValue("slopedepthdb",init_controls[2]);
  177. }
  178. void MaddeLOIDSynth::loadFile(QString fileName)
  179. {
  180. MaddeFile file;
  181. file.readFromFile(fileName);
  182. _src.gaindb = file.getEpRSource("gaindb").toDouble();
  183. _src.slopedepthdb = file.getEpRSource("slopedepthdb").toDouble();
  184. _src.slope = file.getEpRSource("slope").toDouble();
  185. _madde->setValue("gaindb",_src.gaindb);
  186. _madde->setValue("slopedepthdb",_src.slopedepthdb);
  187. _madde->setValue("slope",_src.slope);
  188. for(int i=0;i<6;i++)
  189. {
  190. float frq = file.getFormant("F"+QVariant(i+1).toString()).toFloat();
  191. float q = file.getFormant("Q"+QVariant(i+1).toString()).toFloat();
  192. QVariant a = file.getFormant("A"+QVariant(i+1).toString());
  193. bool useformant = file.getFormant("UseF"+QVariant(i+1).toString()).toBool();
  194. float a2=init_gain_db[i];
  195. if(a.toString().length()>0) a2=a.toFloat();
  196. _madde->setValue("F"+QVariant(i+1).toString(),frq);
  197. _madde->setValue("BW"+QVariant(i+1).toString(),frq/q);
  198. _madde->setValue("A"+QVariant(i+1).toString(),a2);
  199. if(useformant) {
  200. _madde->setValue("UseF"+QVariant(i+1).toString(),true);
  201. _resEnabled[i]=true;
  202. } else
  203. {
  204. _resEnabled[i]=false;
  205. _madde->setValue("UseF"+QVariant(i+1).toString(),false);
  206. }
  207. _res[i].f = frq;
  208. _res[i].bw = frq/q;
  209. _res[i].gain_db = a2;
  210. _res[i].enabled=useformant;
  211. EprResonanceUpdate(&_res[i],_osc.fs);
  212. }
  213. }
  214. void MaddeLOIDSynth::madde_valueChanged(QString key,QVariant value)
  215. {
  216. if(key[0]=='F')
  217. {
  218. key.replace("F","");
  219. int index = QVariant(key).toInt()-1;
  220. _res[index].f=value.toFloat();
  221. _resNeedsUpdate[index]=true;
  222. }
  223. if(key[0]=='A')
  224. {
  225. key.replace("A","");
  226. int index = QVariant(key).toInt()-1;
  227. _res[index].gain_db=value.toFloat();
  228. _resNeedsUpdate[index]=true;
  229. }
  230. if(key[0]=='Q')
  231. {
  232. key.replace("Q","");
  233. int index = QVariant(key).toInt()-1;
  234. _res[index].bw=_res[index].f/value.toFloat();
  235. _resNeedsUpdate[index]=true;
  236. }
  237. if(key.startsWith("UseF"))
  238. {
  239. key.replace("UseF","");
  240. int index = QVariant(key).toInt()-1;
  241. _res[index].enabled=(int)value.toBool();
  242. }
  243. if(key=="gaindb") _src.gaindb=value.toFloat();
  244. if(key=="slope") _src.slope=value.toFloat();
  245. if(key=="slopedepthdb") _src.slopedepthdb=value.toFloat();
  246. }
  247. void MaddeLOIDSynth::madde_closed()
  248. {
  249. _madde->deleteLater();
  250. _madde = nullptr;
  251. }
  252. void MaddeLOIDSynth::setup(IController *ctrl) {
  253. _ctrl = ctrl;
  254. _osc.fs = ctrl->sampleRate();
  255. QTimer::singleShot(100, this,SLOT(setup2()));
  256. //load default values
  257. for(int i=0;i<RES_COUNT;i++)
  258. {
  259. _res[i].f = init_f[i];
  260. _res[i].bw = init_bw[i];
  261. _res[i].gain_db = init_gain_db[i];
  262. _res[i].enabled=1;
  263. EprResonanceUpdate(&_res[i],_osc.fs);
  264. }
  265. _res[5].enabled=0;
  266. _src.gaindb = init_controls[0];
  267. _src.slope = init_controls[1];
  268. _src.slopedepthdb = init_controls[2];
  269. _ringbuffer = jack_ringbuffer_create(4096*sizeof(float));
  270. }
  271. void MaddeLOIDSynth::saveToFile(QString fileName)
  272. {
  273. MaddeFile file;
  274. for(int i=0;i<6;i++)
  275. {
  276. float f = _res[i].f;
  277. if(f==0) f=init_f[i];
  278. float q = f/_res[i].bw;
  279. if(std::isnan(q)) q = 10;
  280. file.setFormant("F"+QVariant(i+1).toString(),f);
  281. file.setFormant("Q"+QVariant(i+1).toString(),q);
  282. file.setFormant("A"+QVariant(i+1).toString(),_res[i].gain_db);
  283. file.setFormant("UseF"+QVariant(i+1).toString(),_res[i].enabled);
  284. }
  285. file.setEpRSource("gaindb",_src.gaindb);
  286. file.setEpRSource("slope",_src.slope);
  287. file.setEpRSource("slopedepthdb",_src.slopedepthdb);
  288. file.writeToFile(fileName);
  289. }
  290. void MaddeLOIDSynth::mouseEvent(int status)
  291. {
  292. DEVLOG_INFO("mouse "+STR(status))
  293. if(status==1)
  294. start(48);
  295. else
  296. stop();
  297. }