epr_synth.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*
  2. This file is part of QTau
  3. Copyright (C) 2013-2019 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 simple demo synth using the EpR voice model
  19. // using freqs from https://en.wikipedia.org/wiki/Formant
  20. #include "epr_synth.h"
  21. #include <sekai/midi.h>
  22. #define __devloglevel__ 4
  23. QString EpRSynth::name() { return "EpR"; }
  24. QString EpRSynth::description() { return "Based on the Klatt synthesizer"; }
  25. QString EpRSynth::version() { return "19.04"; }
  26. bool EpRSynth::synthIsRealtime() { return true; }
  27. void __devlog__(QString logtype,QString filename,int line,QString msg)
  28. {
  29. QString logmsg = logtype+": "+filename.replace("../../","").replace("../","")+":"+QVariant(line).toString()+": "+msg;
  30. fprintf(stderr,"[synth]%s\n",logmsg.toUtf8().data());
  31. }
  32. void EpRSynth::setup(IController* ctrl) {
  33. this->_ctrl = ctrl;
  34. this->_jack_samplerate = ctrl->sampleRate();
  35. connect(this,&EpRSynth::logDebug,this,&EpRSynth::on_logDebug);
  36. connect(this,&EpRSynth::logError,this,&EpRSynth::on_logError);
  37. connect(this,&EpRSynth::logSuccess,this,&EpRSynth::on_logSuccess);
  38. connect(this,&EpRSynth::endOfThread,this,&EpRSynth::on_endOfThread);
  39. fprintf(stderr,"ringbuffer allocate %i\n",_jack_samplerate);
  40. _src.gaindb = 40;
  41. _src.slope = -0.00001;
  42. _src.slopedepthdb = 77;
  43. _osc.fs=_jack_samplerate;
  44. formant fo;
  45. fo.f1 = 860;
  46. fo.f2 = 1610;
  47. _formantMap["a"]=fo;
  48. fo.f1 = 360;
  49. fo.f2 = 640;
  50. _formantMap["o"]=fo;
  51. fo.f1 = 390;
  52. fo.f2 = 2300;
  53. _formantMap["e"]=fo;
  54. fo.f1 = 240;
  55. fo.f2 = 2400;
  56. _formantMap["i"]=fo;
  57. fo.f1 = 250;
  58. fo.f2 = 595;
  59. _formantMap["u"]=fo;
  60. }
  61. bool EpRSynth::synthesize(IScore* score)
  62. {
  63. _pos = 0;
  64. _currentNote = 0;
  65. _stop = false;
  66. _notes.clear();
  67. for(int i=0;i<score->getNoteCount();i++)
  68. {
  69. auto n = score->getNote(i);
  70. DEVLOG_DEBUG("note "+STR(i));
  71. DEVLOG_DEBUG("rest "+STR(n.rest)+" length "+STR(n.lenght));
  72. noteEvent evt;
  73. evt.startPos = n.start;
  74. evt.endPos = n.start+n.lenght;
  75. evt.f0 = frequencyFromNote(n.pitch);
  76. evt.fo = _formantMap[n.lyric];
  77. _notes.push_back(evt);
  78. }
  79. return true;
  80. }
  81. void EpRSynth::genOneFrame()
  82. {
  83. float f0 = 0;
  84. formant fo;
  85. if(_stop) return;
  86. //update EpR on specific timestamps
  87. float current_time = _pos*1.0/_jack_samplerate;
  88. if(current_time>_notes[_currentNote].startPos && current_time<_notes[_currentNote].endPos)
  89. {
  90. f0 = _notes[_currentNote].f0;
  91. fo = _notes[_currentNote].fo;
  92. }
  93. int nharmonics = 50;
  94. if(f0){
  95. _res[0].gain_db = 20;
  96. _res[0].bw = fo.f1/10.0;
  97. _res[0].f = fo.f1;
  98. _res[0].enabled = 1;
  99. _res[1].gain_db = 20;
  100. _res[1].bw = fo.f2/10.0;
  101. _res[1].f = fo.f2;
  102. _res[1].enabled = 1;
  103. for(int i=2;i<RES_COUNT;i++)
  104. {
  105. _res[i].gain_db = 5;
  106. _res[i].bw = 10;
  107. _res[i].f = i*1500;
  108. _res[i].enabled = 1;
  109. }
  110. for(int i=0;i<6;i++)
  111. {
  112. EprResonanceUpdate(&_res[i],_osc.fs);
  113. }
  114. for(int i=0;i<nharmonics;i++)
  115. {
  116. float factor = 0.1;
  117. float f = f0*(i+1);
  118. double gain = EprAtFrequency(&_src,f,_osc.fs,_res,RES_COUNT);
  119. _osc.amp[i] = pow(M_E,(gain/TWENTY_OVER_LOG10))/nharmonics*factor;
  120. _osc.frq[i] = f;
  121. }
  122. }
  123. else {
  124. for(int i=0;i<nharmonics;i++)
  125. {
  126. _osc.amp[i]=0;
  127. _osc.frq[i]=0;
  128. }
  129. }
  130. if(current_time>=_notes[_currentNote].endPos)
  131. {
  132. _currentNote++;
  133. if(_currentNote==_notes.count()) _stop=true;
  134. }
  135. }
  136. int EpRSynth::readData(float *data, int size)
  137. {
  138. static jack_ringbuffer_t* my_ringbuffer=nullptr;
  139. int padding = 256;
  140. if(my_ringbuffer==nullptr)
  141. {
  142. my_ringbuffer = jack_ringbuffer_create(4096*16*sizeof(float));
  143. }
  144. while(jack_ringbuffer_read_space(my_ringbuffer)<(size+padding)*sizeof(float) && _stop==false )
  145. {
  146. genOneFrame();
  147. _pos += FFT_SIZE/2;
  148. _osc.processOneFrame();
  149. float output_buffer[FFT_SIZE/2];
  150. for(int i=0;i<FFT_SIZE/2;i++)
  151. {
  152. output_buffer[i] = _osc.output_buffer[i]*0.9;
  153. }
  154. if(jack_ringbuffer_write_space(my_ringbuffer)>FFT_SIZE/2*sizeof(float))
  155. jack_ringbuffer_write(my_ringbuffer,(char*)output_buffer,FFT_SIZE/2*sizeof(float));
  156. else
  157. fprintf(stderr,"buffer full\n");
  158. }
  159. if(jack_ringbuffer_read_space(my_ringbuffer)>(size+padding)*sizeof(float))
  160. {
  161. jack_ringbuffer_read(my_ringbuffer,(char*)data,size*sizeof(float));
  162. }
  163. else
  164. {
  165. if(_stop) return 1;
  166. }
  167. return 0;
  168. }
  169. QString EpRSynth::getTranscription(QString txt)
  170. {
  171. return txt;
  172. }
  173. bool EpRSynth::doPhonemeTransformation(QStringList& list)
  174. {
  175. (void) list;
  176. return false;
  177. }
  178. bool EpRSynth::setVoice(QString voiceName)
  179. {
  180. if(voiceName=="Klatt") return true;
  181. return false;
  182. }
  183. QStringList EpRSynth::listVoices()
  184. {
  185. QStringList voices;
  186. voices << "Klatt";
  187. return voices;
  188. }
  189. //??
  190. void EpRSynth::on_logError(QString error)
  191. {
  192. _ctrl->logError(error);
  193. }
  194. void EpRSynth::on_logSuccess(QString success)
  195. {
  196. _ctrl->logSuccess(success);
  197. }
  198. void EpRSynth::on_logDebug(QString debug)
  199. {
  200. _ctrl->logDebug(debug);
  201. }
  202. void EpRSynth::on_endOfThread()
  203. {
  204. }
  205. bool EpRSynth::setCacheDir(QString cacheDir)
  206. {
  207. (void) cacheDir;
  208. return true;
  209. }