synth.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. #include "synth.hpp"
  2. #include <math.h>
  3. #include <iostream>
  4. #include <stdlib.h> /* srand, rand */
  5. #include <sekai/mfcc.h>
  6. #include <json/json.h>
  7. #include <iostream>
  8. #include <fstream>
  9. #include <sekai/common.h>
  10. #define TWOPI (2*M_PI)
  11. using namespace std;
  12. extern std::string basedir;
  13. double midi_freq(float m) {
  14. /* converts a MIDI note number to a frequency
  15. <http://en.wikipedia.org/wiki/MIDI_Tuning_Standard> */
  16. return 440 * pow(2, (double)(m-69.0)/12.0);
  17. }
  18. Synth::Synth()
  19. {
  20. //FIXME
  21. }
  22. void Synth::init(int samplerate,int buffer_size)
  23. {
  24. this->samplerate = samplerate;
  25. this->buffer_size = buffer_size;
  26. enabled = false;
  27. reader = new VVDReader();
  28. int fft_size = 2048;
  29. fprintf(stderr,"sample_rate=%i\n",samplerate);
  30. synth = new WorldSynth2(1024*1024*1024,fft_size,samplerate);
  31. //jsoncpp is only used by this test program
  32. ifstream ifs(basedir+"/voice.json");
  33. Json::Reader jreader;
  34. Json::Value arr;
  35. jreader.parse(ifs, arr);
  36. for (int i = 0; i < arr.size(); i++){
  37. std::string vvd = arr[i]["vvd"].asString();
  38. auto x = arr[i]["x"];
  39. auto y = arr[i]["y"];
  40. bool valid = reader->addVVD(basedir+"/"+vvd);
  41. if(!valid) {
  42. fprintf(stderr,"invalid vvd\n");
  43. abort();
  44. }
  45. if(x.size()!=y.size())
  46. {
  47. fprintf(stderr,"invalid entry in voices.json\n");
  48. abort();
  49. }
  50. segment* seg = new segment;
  51. seg->count=x.size();
  52. seg->x=new float[seg->count];
  53. seg->y=new float[seg->count];
  54. for (int j = 0; j < x.size(); j++){
  55. seg->x[j]=x[j].asInt()*0.001;
  56. seg->y[j]=y[j].asInt()*0.001;
  57. }
  58. segments.push_back(seg);
  59. }
  60. vvddata = (float*) new char[reader->getFrameSize()];//FIXME add allocator for vvddata
  61. }
  62. Synth::~Synth()
  63. {
  64. }
  65. void Synth::noteOn(int notenum, int velocity)
  66. {
  67. this->notenum = notenum;
  68. int pitch = (int)(midi_freq(notenum)+0.5);
  69. printf("pitch %i notenum %i\n",pitch,notenum);
  70. int current_samples = synth->currentTime();//time in samples
  71. float current_time = current_samples*1.0/samplerate;
  72. switch(notenum % 12)
  73. {
  74. case 0:
  75. current_oto = 0;
  76. break;
  77. case 2:
  78. current_oto = 1;
  79. break;
  80. case 4:
  81. current_oto = 2;
  82. break;
  83. case 5:
  84. current_oto = 3;
  85. break;
  86. case 7:
  87. current_oto = 4;
  88. break;
  89. case 9:
  90. current_oto = 5;
  91. break;
  92. case 11:
  93. current_oto = 6;
  94. break;
  95. default:
  96. current_oto = 7;
  97. }
  98. //fprintf(stderr,"select %i\n",reader->selectVVD(current_oto));
  99. if(current_oto!=7)
  100. {
  101. segment* seg = segments[current_oto];
  102. float delta = current_time - seg->x[0];
  103. for(int i=0;i<seg->count;i++)
  104. {
  105. seg->x[i]+=delta;
  106. }
  107. reader->selectVVD(current_oto);
  108. enabled = true;
  109. }
  110. else
  111. enabled = false;
  112. }
  113. void Synth::noteOff(int notenum)
  114. {
  115. this->notenum = 0;
  116. enabled=false;
  117. #if 0
  118. if(notenum==this->notenum)
  119. {
  120. lastnote=this->notenum;
  121. this->notenum=0;
  122. current_frame=0;
  123. current_oto = 7;
  124. }
  125. fprintf(stderr,"select %i\n",vvdreader->selectVVD(current_oto));
  126. #endif
  127. }
  128. float Synth::getCurrentF0(int pos)
  129. {
  130. (void) pos;
  131. if(notenum) return (float)midi_freq(notenum);
  132. else return 0.0;
  133. }
  134. segment* Synth::getCurrentSegment(int)
  135. {
  136. if(enabled)
  137. {
  138. return segments[current_oto];
  139. }
  140. else return 0;
  141. }
  142. void Synth::fill(float* buffer, int size)
  143. {
  144. int cepstrum_length = 32;//FIXME
  145. while(1)
  146. {
  147. int current_samples = synth->currentTime();//time in samples
  148. float current_time = current_samples*1.0/samplerate;
  149. segment* seg = getCurrentSegment(current_samples);
  150. #if 1
  151. if(seg && current_time > seg->x[seg->count-1])
  152. {
  153. enabled=false;
  154. seg=0;
  155. }
  156. #endif
  157. if(seg) {
  158. float f0 = getCurrentF0(current_samples);
  159. float fractionalIndex=interp_linear(seg->x,seg->y,seg->count,current_time)*1000.0/reader->getFramePeriod();
  160. reader->getSegment(fractionalIndex,vvddata);
  161. float* mel_cepstrum1 = &vvddata[1];
  162. float* mel_cepstrum2 = &vvddata[1+cepstrum_length];
  163. //TODO
  164. // getCurrentConcatenation(current_time)
  165. // concatenation
  166. synth->setFrame(mel_cepstrum1,mel_cepstrum2,cepstrum_length);
  167. synth->setF0(f0);
  168. } else {
  169. synth->setSilence();
  170. }
  171. synth->doSynth(); //synth one frame
  172. if(synth->isFilled(size+2048)) break;
  173. }
  174. synth->pop(buffer,size);
  175. }