You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

synth_sine.cpp 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #include "Audio.h"
  2. #include "arm_math.h"
  3. // data_waveforms.c
  4. extern "C" {
  5. extern const int16_t AudioWaveformSine[257];
  6. }
  7. void AudioSynthWaveformSine::frequency(float f)
  8. {
  9. if (f > AUDIO_SAMPLE_RATE_EXACT / 2 || f < 0.0) return;
  10. phase_increment = (f / AUDIO_SAMPLE_RATE_EXACT) * 4294967296.0f;
  11. }
  12. void AudioSynthWaveformSine::update(void)
  13. {
  14. audio_block_t *block;
  15. uint32_t i, ph, inc, index, scale;
  16. int32_t val1, val2;
  17. //Serial.println("AudioSynthWaveformSine::update");
  18. block = allocate();
  19. if (block) {
  20. ph = phase;
  21. inc = phase_increment;
  22. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  23. index = ph >> 24;
  24. val1 = AudioWaveformSine[index];
  25. val2 = AudioWaveformSine[index+1];
  26. scale = (ph >> 8) & 0xFFFF;
  27. val2 *= scale;
  28. val1 *= 0xFFFF - scale;
  29. block->data[i] = (val1 + val2) >> 16;
  30. //Serial.print(block->data[i]);
  31. //Serial.print(", ");
  32. //if ((i % 12) == 11) Serial.println();
  33. ph += inc;
  34. }
  35. //Serial.println();
  36. phase = ph;
  37. transmit(block);
  38. release(block);
  39. return;
  40. }
  41. phase += phase_increment * AUDIO_BLOCK_SAMPLES;
  42. }
  43. void AudioSynthWaveformSineModulated::frequency(float f)
  44. {
  45. if (f > AUDIO_SAMPLE_RATE_EXACT / 2 || f < 0.0) return;
  46. phase_increment = (f / AUDIO_SAMPLE_RATE_EXACT) * 4294967296.0f;
  47. }
  48. void AudioSynthWaveformSineModulated::update(void)
  49. {
  50. audio_block_t *block, *modinput;
  51. uint32_t i, ph, inc, index, scale;
  52. int32_t val1, val2;
  53. //Serial.println("AudioSynthWaveformSineModulated::update");
  54. modinput = receiveReadOnly();
  55. ph = phase;
  56. inc = phase_increment;
  57. block = allocate();
  58. if (!block) {
  59. // unable to allocate memory, so we'll send nothing
  60. if (modinput) {
  61. // but if we got modulation data, update the phase
  62. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  63. ph += inc + modinput->data[i] * modulation_factor;
  64. }
  65. release(modinput);
  66. } else {
  67. ph += phase_increment * AUDIO_BLOCK_SAMPLES;
  68. }
  69. phase = ph;
  70. return;
  71. }
  72. if (modinput) {
  73. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  74. index = ph >> 24;
  75. val1 = AudioWaveformSine[index];
  76. val2 = AudioWaveformSine[index+1];
  77. scale = (ph >> 8) & 0xFFFF;
  78. val2 *= scale;
  79. val1 *= 0xFFFF - scale;
  80. block->data[i] = (val1 + val2) >> 16;
  81. //Serial.print(block->data[i]);
  82. //Serial.print(", ");
  83. //if ((i % 12) == 11) Serial.println();
  84. ph += inc + modinput->data[i] * modulation_factor;
  85. }
  86. release(modinput);
  87. } else {
  88. ph = phase;
  89. inc = phase_increment;
  90. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  91. index = ph >> 24;
  92. val1 = AudioWaveformSine[index];
  93. val2 = AudioWaveformSine[index+1];
  94. scale = (ph >> 8) & 0xFFFF;
  95. val2 *= scale;
  96. val1 *= 0xFFFF - scale;
  97. block->data[i] = (val1 + val2) >> 16;
  98. //Serial.print(block->data[i]);
  99. //Serial.print(", ");
  100. //if ((i % 12) == 11) Serial.println();
  101. ph += inc;
  102. }
  103. }
  104. phase = ph;
  105. transmit(block);
  106. release(block);
  107. }