test_espeak.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #include <sekai/VoiceDefESPEAK.h>
  2. #include <sekai/VoiceSampler.h>
  3. #include <sndfile.h>
  4. SNDFILE *sf_debug;
  5. float interp_linear(float *x, float *y, int nx, float ref) {
  6. int i;
  7. for (i = 0; i < nx - 1; i++) {
  8. if (ref >= x[i] && ref <= x[i + 1]) {
  9. float x1 = x[i];
  10. float x2 = x[i + 1];
  11. float tmp = (ref - x1) / (x2 - x1);
  12. return y[i] * (1 - tmp) + y[i + 1] * tmp;
  13. }
  14. }
  15. fprintf(stderr, "INTERP_LINEAR: out of range\n");
  16. return NAN;
  17. }
  18. class TestSynth : public VoiceSampler {
  19. public:
  20. TestSynth() : VoiceSampler(8192) {
  21. vox[0] = new VoiceDefESPEAK(
  22. "do.voicedef");
  23. vox[1] = new VoiceDefESPEAK(
  24. "re.voicedef");
  25. vox[2] = new VoiceDefESPEAK(
  26. "mi.voicedef");
  27. _impulseResponse = new float[1024];
  28. }
  29. protected:
  30. virtual bool addOnePulse() {
  31. int samplerate = 22050;
  32. float currentTime = inputPositionSamples() / samplerate;
  33. int index = (int)currentTime;
  34. if (index == 3) return false;
  35. // get impulse response
  36. float otoTime = currentTime;
  37. while (otoTime > 1.0) otoTime -= 1.0;
  38. if (index == 0) {
  39. float x[3] = {0, 0.10, 1.0};
  40. float y[3] = {0, 0.10, 0.20};
  41. otoTime = interp_linear(x, y, 3, otoTime);
  42. }
  43. if (index == 1) {
  44. float x[3] = {0, 0.10, 1.0};
  45. float y[3] = {0, 0.10, 0.20};
  46. otoTime = interp_linear(x, y, 3, otoTime);
  47. }
  48. if (index == 2) {
  49. float x[3] = {0, 0.20, 1.0};
  50. float y[3] = {0, 0.20, 0.40};
  51. otoTime = interp_linear(x, y, 3, otoTime);
  52. }
  53. int impulseResponseLength = 0;
  54. vox[index]->getImpulseResponse(otoTime, _impulseResponse,
  55. &impulseResponseLength,0);
  56. float f0[3] = {261.63, 293.67, 329.63};
  57. float output_f0 = f0[index];
  58. float period = samplerate * 1.0f / output_f0;
  59. VoiceSampler::hanningWindow(_impulseResponse, impulseResponseLength);
  60. // printf("index %i %i\n",index,impulseResponseLength);
  61. sf_write_float(sf_debug, _impulseResponse, impulseResponseLength);
  62. float mark = 1;
  63. sf_write_float(sf_debug, &mark, 1);
  64. ola(_impulseResponse, impulseResponseLength, period);
  65. return true;
  66. }
  67. private:
  68. VoiceDefESPEAK *vox[3];
  69. float *_impulseResponse;
  70. };
  71. int main() {
  72. TestSynth *synth = new TestSynth();
  73. SF_INFO info = {0};
  74. info.samplerate = 22050;
  75. info.channels = 1;
  76. info.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16;
  77. SNDFILE *sf = sf_open("/tmp/test_espeak.wav", SFM_WRITE, &info);
  78. info.samplerate = 22050;
  79. info.channels = 1;
  80. info.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16;
  81. sf_debug = sf_open("/tmp/test_espeak_debug.wav", SFM_WRITE, &info);
  82. while (1) {
  83. const int size = 1024;
  84. int fill = size * 4;
  85. float buffer_out[size];
  86. if (synth->readData(buffer_out, size, fill) == false) break;
  87. sf_write_float(sf, buffer_out, size);
  88. }
  89. sf_close(sf);
  90. sf_close(sf_debug);
  91. }