synth.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #include "synth.hpp"
  2. #include <fstream>
  3. #include <iostream>
  4. #include <assert.h>
  5. #include <math.h>
  6. #include <sekai/common.h>
  7. #include <sekai/mfcc.h>
  8. #include <sndfile.h>
  9. #include <stdlib.h> /* srand, rand */
  10. #define TWOPI (2 * M_PI)
  11. #define IMP_LENGTH_MAX (1024 * 8)
  12. using namespace std;
  13. extern std::string basedir;
  14. double midi_freq(float m) {
  15. /* converts a MIDI note number to a frequency
  16. <http://en.wikipedia.org/wiki/MIDI_Tuning_Standard> */
  17. return 440.0 * pow(2, (double)(m - 69.0) / 12.0);
  18. }
  19. Synth::Synth() {
  20. VoiceDefESPEAK* def = new VoiceDefESPEAK("autobahn.pho");
  21. for (int i = 0; i < MAX_POLYPHONY; i++) {
  22. key[i] = 0;
  23. freq[i] = 0;
  24. voice[i] = new SynthRT();
  25. voice[i]->voicedef = def;
  26. voice[i]->impulse_response = new float[IMP_LENGTH_MAX];
  27. voice[i]->samplerate = 0;
  28. }
  29. }
  30. void Synth::init(int samplerate, int buffer_size) {
  31. this->samplerate = samplerate;
  32. this->buffer_size = buffer_size;
  33. for (int i = 0; i < MAX_POLYPHONY; i++) {
  34. voice[i]->samplerate = samplerate;
  35. }
  36. }
  37. Synth::~Synth() {}
  38. void Synth::noteOn(int notenum, int velocity) {
  39. for (int i = 0; i < MAX_POLYPHONY; i++) {
  40. if (freq[i] == 0) {
  41. freq[i] = midi_freq(notenum + bend);
  42. key[i] = notenum;
  43. if (i > 0)
  44. voice[i]->setInputPositionSamples(voice[0]->inputPositionSamples());
  45. active_keys++;
  46. break;
  47. }
  48. }
  49. }
  50. void Synth::noteOff(int notenum) {
  51. for (int i = 0; i < MAX_POLYPHONY; i++) {
  52. if (key[i] == notenum) {
  53. freq[i] = 0;
  54. key[i] = 0;
  55. voice[i]->reset();
  56. voice[i]->setInputPositionSamples(0);
  57. active_keys--;
  58. break;
  59. }
  60. }
  61. }
  62. void Synth::controllerEvent(int a1, int a2) {
  63. if (a1 == 1) speed = 128 - a2;
  64. }
  65. void Synth::pitchBend(int a1, int a2) {
  66. float fract = 1.0 * (a2 * 128 + a1 - 8 * 1024) / (8 * 1024);
  67. bend = fract;
  68. for (int i = 0; i < MAX_POLYPHONY; i++) {
  69. if (freq[i]) freq[i] = midi_freq(key[i] + bend);
  70. }
  71. }
  72. void Synth::fill(float *buffer, int size) {
  73. for (int i = 0; i < size; i++) buffer[i] = 0;
  74. static int size2=size;
  75. for (int i = 0; i < MAX_POLYPHONY; i++) {
  76. if (key[i]) {
  77. float T0 = (samplerate / freq[i]);
  78. voice[i]->period = T0;
  79. voice[i]->enabled = true;
  80. }
  81. else
  82. {
  83. voice[i]->period = 10;
  84. voice[i]->enabled = false;
  85. }
  86. }
  87. float buffer_tmp[size];
  88. for (int i = 0; i < MAX_POLYPHONY; i++) {
  89. voice[i]->readData(buffer_tmp,size,size2);
  90. for (int j = 0; j < size; j++) buffer[j] += buffer_tmp[j];
  91. }
  92. if(active_keys) {
  93. for (int i = 0; i < size; i++) buffer[i] /= active_keys;
  94. }
  95. }
  96. static const int olabuffer_length=1024*8;
  97. SynthRT::SynthRT() : VoiceSampler(olabuffer_length)
  98. {
  99. }
  100. bool SynthRT::addOnePulse()
  101. {
  102. if(enabled)
  103. {
  104. float factor = 0.25;
  105. float pos = inputPositionSamples() * factor / samplerate;
  106. voicedef->getImpulseResponse(pos,impulse_response,&impulse_response_length,0);
  107. printf("pos %f impulse_response_length %i\n",pos,impulse_response_length);
  108. hanningWindow(impulse_response,impulse_response_length);
  109. ola(impulse_response,impulse_response_length,period);
  110. }
  111. else
  112. {
  113. float dummy=0;
  114. ola(&dummy,1,10);
  115. }
  116. return true;
  117. }