madde_synth.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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 <unistd.h>
  40. #include <QAction>
  41. #include <QTimer>
  42. #include <QtConcurrent/QtConcurrent>
  43. #include "madde_synth.h"
  44. #include "maddefile.h"
  45. QString MaddeLOIDSynth::name() { return "MaddeLOID"; }
  46. QString MaddeLOIDSynth::description() {
  47. return "An additive singing synthesizer";
  48. }
  49. QString MaddeLOIDSynth::version() { return "18.04"; }
  50. #include <math.h>
  51. #include "maddewindow.h"
  52. void MaddeLOIDSynth::start(float keyNum) {
  53. if (_threadRunning) return;
  54. _f0 = frequencyFromNote(keyNum);
  55. _phase = 0;
  56. // _ctrl->startThread(this);
  57. QtConcurrent::run(this, &MaddeLOIDSynth::run);
  58. }
  59. void MaddeLOIDSynth::stop() { _f0 = 0; }
  60. int MaddeLOIDSynth::run() {
  61. _threadRunning = true;
  62. while (_f0) {
  63. while (jack_ringbuffer_write_space(_ringbuffer) <
  64. FFT_SIZE / 2 * sizeof(float)) {
  65. usleep(1000);
  66. if (_f0 == 0) break;
  67. }
  68. if (_f0) {
  69. int nharmonics = 50;
  70. for (int i = 0; i < 6; i++) {
  71. if (_resNeedsUpdate[i]) {
  72. EprResonanceUpdate(&_res[i], _osc.fs);
  73. _resNeedsUpdate[i] = false;
  74. }
  75. }
  76. for (int i = 0; i < 50; i++) {
  77. if (_f0 > 0) {
  78. float factor = 0.1;
  79. float f = _f0 * (i + 1);
  80. double gain = EprAtFrequency(&_src, f, _osc.fs, _res, RES_COUNT);
  81. _osc.amp[i] =
  82. pow(M_E, (gain / TWENTY_OVER_LOG10)) / nharmonics * factor;
  83. _osc.frq[i] = f;
  84. } else {
  85. _osc.amp[i] = 0;
  86. _osc.frq[i] = 0;
  87. }
  88. }
  89. _osc.processOneFrame();
  90. float output_buffer[FFT_SIZE / 2];
  91. for (int i = 0; i < FFT_SIZE / 2; i++) {
  92. output_buffer[i] = _osc.output_buffer[i] * 0.9;
  93. }
  94. jack_ringbuffer_write(_ringbuffer, (char*)output_buffer,
  95. FFT_SIZE / 2 * sizeof(float));
  96. };
  97. }
  98. _threadRunning = false;
  99. return 0;
  100. }
  101. void MaddeLOIDSynth::readData(float* data, int dataLength) {
  102. unsigned int dataLength2 = dataLength * sizeof(float);
  103. unsigned int readSpace = jack_ringbuffer_read_space(_ringbuffer);
  104. readSpace = jack_ringbuffer_read_space(_ringbuffer);
  105. if (readSpace > dataLength2)
  106. jack_ringbuffer_read(_ringbuffer, (char*)data, dataLength2);
  107. }
  108. void MaddeLOIDSynth::setup2() // rename to setupUI
  109. {
  110. QAction* madde = new QAction("MaddeLOID", this);
  111. _ctrl->addPluginAction(madde);
  112. connect(madde, SIGNAL(triggered()), this, SLOT(madde_triggered()));
  113. }
  114. void MaddeLOIDSynth::madde_triggered() {
  115. if (_madde) return;
  116. _madde = new MaddeWindow();
  117. _madde->setVisible(true);
  118. connect(_madde, SIGNAL(loadDefault()), this, SLOT(loadDefault()));
  119. connect(_madde, SIGNAL(loadFile(QString)), this, SLOT(loadFile(QString)));
  120. connect(_madde, SIGNAL(saveToFile(QString)), this, SLOT(saveToFile(QString)));
  121. connect(_madde, SIGNAL(mouseEvent(int)), this, SLOT(mouseEvent(int)));
  122. connect(_madde, SIGNAL(valueChanged(QString, QVariant)), this,
  123. SLOT(madde_valueChanged(QString, QVariant)));
  124. connect(_madde, SIGNAL(closed()), this, SLOT(madde_closed()));
  125. for (int i = 0; i < 6; i++) {
  126. _madde->setValue("F" + QVariant(i + 1).toString(), init_f[i]);
  127. _madde->setValue("BW" + QVariant(i + 1).toString(), init_bw[i]);
  128. _madde->setValue("A" + QVariant(i + 1).toString(), init_gain_db[i]);
  129. if (i != 5) {
  130. _madde->setValue("UseF" + QVariant(i + 1).toString(), true);
  131. _resEnabled[i] = true;
  132. } else
  133. _resEnabled[i] = false;
  134. _resNeedsUpdate[i] = false;
  135. }
  136. _madde->setValue("gaindb", init_controls[0]);
  137. _madde->setValue("slope", init_controls[1]);
  138. _madde->setValue("slopedepthdb", init_controls[2]);
  139. }
  140. void MaddeLOIDSynth::loadDefault() {
  141. for (int i = 0; i < 6; i++) {
  142. _madde->setValue("F" + QVariant(i + 1).toString(), init_f[i]);
  143. _madde->setValue("BW" + QVariant(i + 1).toString(), init_bw[i]);
  144. _madde->setValue("A" + QVariant(i + 1).toString(), init_gain_db[i]);
  145. if (i != 5) {
  146. _madde->setValue("UseF" + QVariant(i + 1).toString(), true);
  147. _resEnabled[i] = true;
  148. } else
  149. _resEnabled[i] = false;
  150. _resNeedsUpdate[i] = false;
  151. }
  152. // synth
  153. for (int i = 0; i < RES_COUNT; i++) {
  154. _res[i].f = init_f[i];
  155. _res[i].bw = init_bw[i];
  156. _res[i].gain_db = init_gain_db[i];
  157. _res[i].enabled = 1;
  158. EprResonanceUpdate(&_res[i], _osc.fs);
  159. }
  160. _res[5].enabled = 0;
  161. _src.gaindb = init_controls[0];
  162. _src.slope = init_controls[1];
  163. _src.slopedepthdb = init_controls[2];
  164. _madde->setValue("gaindb", init_controls[0]);
  165. _madde->setValue("slope", init_controls[1]);
  166. _madde->setValue("slopedepthdb", init_controls[2]);
  167. }
  168. void MaddeLOIDSynth::loadFile(QString fileName) {
  169. MaddeFile file;
  170. file.readFromFile(fileName);
  171. _src.gaindb = file.getEpRSource("gaindb").toDouble();
  172. _src.slopedepthdb = file.getEpRSource("slopedepthdb").toDouble();
  173. _src.slope = file.getEpRSource("slope").toDouble();
  174. _madde->setValue("gaindb", _src.gaindb);
  175. _madde->setValue("slopedepthdb", _src.slopedepthdb);
  176. _madde->setValue("slope", _src.slope);
  177. for (int i = 0; i < 6; i++) {
  178. float frq = file.getFormant("F" + QVariant(i + 1).toString()).toFloat();
  179. float q = file.getFormant("Q" + QVariant(i + 1).toString()).toFloat();
  180. QVariant a = file.getFormant("A" + QVariant(i + 1).toString());
  181. bool useformant =
  182. file.getFormant("UseF" + QVariant(i + 1).toString()).toBool();
  183. float a2 = init_gain_db[i];
  184. if (a.toString().length() > 0) a2 = a.toFloat();
  185. _madde->setValue("F" + QVariant(i + 1).toString(), frq);
  186. _madde->setValue("BW" + QVariant(i + 1).toString(), frq / q);
  187. _madde->setValue("A" + QVariant(i + 1).toString(), a2);
  188. if (useformant) {
  189. _madde->setValue("UseF" + QVariant(i + 1).toString(), true);
  190. _resEnabled[i] = true;
  191. } else {
  192. _resEnabled[i] = false;
  193. _madde->setValue("UseF" + QVariant(i + 1).toString(), false);
  194. }
  195. _res[i].f = frq;
  196. _res[i].bw = frq / q;
  197. _res[i].gain_db = a2;
  198. _res[i].enabled = useformant;
  199. EprResonanceUpdate(&_res[i], _osc.fs);
  200. }
  201. }
  202. void MaddeLOIDSynth::madde_valueChanged(QString key, QVariant value) {
  203. if (key[0] == 'F') {
  204. key.replace("F", "");
  205. int index = QVariant(key).toInt() - 1;
  206. _res[index].f = value.toFloat();
  207. _resNeedsUpdate[index] = true;
  208. }
  209. if (key[0] == 'A') {
  210. key.replace("A", "");
  211. int index = QVariant(key).toInt() - 1;
  212. _res[index].gain_db = value.toFloat();
  213. _resNeedsUpdate[index] = true;
  214. }
  215. if (key[0] == 'Q') {
  216. key.replace("Q", "");
  217. int index = QVariant(key).toInt() - 1;
  218. _res[index].bw = _res[index].f / value.toFloat();
  219. _resNeedsUpdate[index] = true;
  220. }
  221. if (key.startsWith("UseF")) {
  222. key.replace("UseF", "");
  223. int index = QVariant(key).toInt() - 1;
  224. _res[index].enabled = (int)value.toBool();
  225. }
  226. if (key == "gaindb") _src.gaindb = value.toFloat();
  227. if (key == "slope") _src.slope = value.toFloat();
  228. if (key == "slopedepthdb") _src.slopedepthdb = value.toFloat();
  229. }
  230. void MaddeLOIDSynth::madde_closed() {
  231. _madde->deleteLater();
  232. _madde = nullptr;
  233. }
  234. void MaddeLOIDSynth::setup(IController* ctrl) {
  235. _ctrl = ctrl;
  236. _osc.fs = ctrl->sampleRate();
  237. QTimer::singleShot(100, this, SLOT(setup2()));
  238. // load default values
  239. for (int i = 0; i < RES_COUNT; i++) {
  240. _res[i].f = init_f[i];
  241. _res[i].bw = init_bw[i];
  242. _res[i].gain_db = init_gain_db[i];
  243. _res[i].enabled = 1;
  244. EprResonanceUpdate(&_res[i], _osc.fs);
  245. }
  246. _res[5].enabled = 0;
  247. _src.gaindb = init_controls[0];
  248. _src.slope = init_controls[1];
  249. _src.slopedepthdb = init_controls[2];
  250. _ringbuffer = jack_ringbuffer_create(4096 * sizeof(float));
  251. }
  252. void MaddeLOIDSynth::saveToFile(QString fileName) {
  253. MaddeFile file;
  254. for (int i = 0; i < 6; i++) {
  255. float f = _res[i].f;
  256. if (f == 0) f = init_f[i];
  257. float q = f / _res[i].bw;
  258. if (std::isnan(q)) q = 10;
  259. file.setFormant("F" + QVariant(i + 1).toString(), f);
  260. file.setFormant("Q" + QVariant(i + 1).toString(), q);
  261. file.setFormant("A" + QVariant(i + 1).toString(), _res[i].gain_db);
  262. file.setFormant("UseF" + QVariant(i + 1).toString(), _res[i].enabled);
  263. }
  264. file.setEpRSource("gaindb", _src.gaindb);
  265. file.setEpRSource("slope", _src.slope);
  266. file.setEpRSource("slopedepthdb", _src.slopedepthdb);
  267. file.writeToFile(fileName);
  268. }
  269. void MaddeLOIDSynth::mouseEvent(int status) {
  270. DEVLOG_INFO("mouse " + STR(status))
  271. if (status == 1)
  272. start(48);
  273. else
  274. stop();
  275. }