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.

146 lines
3.8KB

  1. // Copyright 2013 Olivier Gillet.
  2. //
  3. // Author: Olivier Gillet (ol.gillet@gmail.com)
  4. //
  5. // This program is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. #include <cmath>
  16. #include <cstdio>
  17. #include <cstring>
  18. #include <cstdlib>
  19. #include "tides/generator.h"
  20. using namespace tides;
  21. using namespace stmlib;
  22. const uint32_t kSampleRate = 48000;
  23. struct StereoSample {
  24. uint16_t l, r;
  25. StereoSample(const GeneratorSample& s) {
  26. l = (s.unipolar >> 1);
  27. r = s.bipolar;
  28. }
  29. };
  30. struct TriggerPair {
  31. uint16_t l, r;
  32. TriggerPair(const GeneratorSample& s) {
  33. l = (s.flags & FLAG_END_OF_ATTACK) ? 32767 : 0;
  34. r = (s.flags & FLAG_END_OF_RELEASE) ? 32767 : 0;
  35. }
  36. };
  37. void write_wav_header(FILE* fp, int num_samples, int num_channels) {
  38. uint32_t l;
  39. uint16_t s;
  40. fwrite("RIFF", 4, 1, fp);
  41. l = 36 + num_samples * 2 * num_channels;
  42. fwrite(&l, 4, 1, fp);
  43. fwrite("WAVE", 4, 1, fp);
  44. fwrite("fmt ", 4, 1, fp);
  45. l = 16;
  46. fwrite(&l, 4, 1, fp);
  47. s = 1;
  48. fwrite(&s, 2, 1, fp);
  49. s = num_channels;
  50. fwrite(&s, 2, 1, fp);
  51. l = kSampleRate;
  52. fwrite(&l, 4, 1, fp);
  53. l = static_cast<uint32_t>(kSampleRate) * 2 * num_channels;
  54. fwrite(&l, 4, 1, fp);
  55. s = 2 * num_channels;
  56. fwrite(&s, 2, 1, fp);
  57. s = 16;
  58. fwrite(&s, 2, 1, fp);
  59. fwrite("data", 4, 1, fp);
  60. l = num_samples * 2 * num_channels;
  61. fwrite(&l, 4, 1, fp);
  62. }
  63. int main(void) {
  64. FILE* fp = fopen("lfo.wav", "wb");
  65. write_wav_header(fp, kSampleRate * 10, 2);
  66. /*for (uint16_t i = 0; i < 128; ++i) {
  67. for (uint16_t j = 0; j < 128; ++j) {
  68. Generator g;
  69. g.Init();
  70. g.set_range(GENERATOR_RANGE_HIGH);
  71. g.set_mode(GENERATOR_MODE_LOOPING);
  72. g.set_pitch(60 << 7);
  73. g.set_slope((j - 64) * 512);
  74. int32_t min = 0;
  75. int32_t max = 0;
  76. for (uint32_t k = 0; k < kSampleRate; ++k) {
  77. GeneratorSample s = g.Process(0);
  78. g.FillBufferSafe();
  79. if (s.bipolar < min) {
  80. min = s.bipolar;
  81. } else if (s.bipolar > max) {
  82. max = s.bipolar;
  83. }
  84. }
  85. printf("%d\t%d\t%d\t%d\n", (i << 7), (j - 64) * 512, min, max);
  86. }
  87. }*/
  88. Generator g;
  89. g.Init();
  90. g.set_range(GENERATOR_RANGE_HIGH);
  91. g.set_mode(GENERATOR_MODE_AR);
  92. g.set_slope(-32768);
  93. g.set_shape(0);
  94. g.set_smoothness(0);
  95. g.set_sync(false);
  96. // uint16_t period_pattern[] = { 400, 400, 200, 800 };
  97. // uint16_t step = 0;
  98. uint32_t counter = 0;
  99. // uint16_t period = period_pattern[step];
  100. uint32_t period = 512;
  101. for (uint32_t i = 0; i < kSampleRate * 10; ++i) {
  102. ++counter;
  103. uint8_t control = 0;
  104. if (counter >= period) {
  105. // step = (step + 1) % (sizeof(period_pattern) / sizeof(uint16_t));
  106. // period = period_pattern[step];
  107. // control |= CONTROL_CLOCK_RISING;
  108. control |= CONTROL_GATE_RISING;
  109. counter = 0;
  110. //period = (rand() % 1024) + 2;
  111. }
  112. if (counter <= period / 4) {
  113. control |= CONTROL_GATE;
  114. }
  115. /*uint32_t pitch = (24 << 7) + (i >> 5);
  116. if (pitch > (115 << 7)) {
  117. pitch = 115 << 7;
  118. }*/
  119. uint16_t tri = (i * 100);
  120. tri = tri > 32767 ? 65535 - tri : tri;
  121. //g.set_slope(tri);
  122. g.set_pitch((48 << 7));
  123. // StereoSample s = StereoSample(g.Process(control * 0));
  124. TriggerPair s = TriggerPair(g.Process(control));
  125. fwrite(&s, sizeof(s), 1, fp);
  126. g.FillBufferSafe();
  127. }
  128. }