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.

162 lines
4.8KB

  1. // Copyright 2014 Olivier Gillet.
  2. //
  3. // Author: Olivier Gillet (ol.gillet@gmail.com)
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. // See http://creativecommons.org/licenses/MIT/ for more information.
  24. //
  25. // -----------------------------------------------------------------------------
  26. #include <cassert>
  27. #include <cmath>
  28. #include <cstdlib>
  29. #include <cstring>
  30. #include <vector>
  31. #include <xmmintrin.h>
  32. #include "clouds/dsp/granular_processor.h"
  33. #include "clouds/resources.h"
  34. using namespace clouds;
  35. using namespace std;
  36. using namespace stmlib;
  37. const size_t kSampleRate = 32000;
  38. const size_t kBlockSize = 32;
  39. void write_wav_header(FILE* fp, int num_samples, int num_channels) {
  40. uint32_t l;
  41. uint16_t s;
  42. fwrite("RIFF", 4, 1, fp);
  43. l = 36 + num_samples * 2 * num_channels;
  44. fwrite(&l, 4, 1, fp);
  45. fwrite("WAVE", 4, 1, fp);
  46. fwrite("fmt ", 4, 1, fp);
  47. l = 16;
  48. fwrite(&l, 4, 1, fp);
  49. s = 1;
  50. fwrite(&s, 2, 1, fp);
  51. s = num_channels;
  52. fwrite(&s, 2, 1, fp);
  53. l = kSampleRate;
  54. fwrite(&l, 4, 1, fp);
  55. l = static_cast<uint32_t>(kSampleRate) * 2 * num_channels;
  56. fwrite(&l, 4, 1, fp);
  57. s = 2 * num_channels;
  58. fwrite(&s, 2, 1, fp);
  59. s = 16;
  60. fwrite(&s, 2, 1, fp);
  61. fwrite("data", 4, 1, fp);
  62. l = num_samples * 2 * num_channels;
  63. fwrite(&l, 4, 1, fp);
  64. }
  65. void TestDSP() {
  66. size_t duration = 15;
  67. FILE* fp_in = fopen("audio_samples/sine.wav", "rb");
  68. FILE* fp_out = fopen("clouds.wav", "wb");
  69. size_t remaining_samples = kSampleRate * duration;
  70. write_wav_header(fp_out, remaining_samples, 2);
  71. fseek(fp_in, 48, SEEK_SET);
  72. uint8_t large_buffer[118784];
  73. uint8_t small_buffer[65536 - 128];
  74. GranularProcessor processor;
  75. processor.Init(
  76. &large_buffer[0], sizeof(large_buffer),
  77. &small_buffer[0],sizeof(small_buffer));
  78. processor.set_num_channels(2);
  79. processor.set_low_fidelity(false);
  80. processor.set_playback_mode(PLAYBACK_MODE_GRANULAR);
  81. Parameters* p = processor.mutable_parameters();
  82. size_t block_counter = 0;
  83. float phase_ = 0.0f;
  84. bool synthetic = false;
  85. processor.Prepare();
  86. float pot_noise = 0.0f;
  87. while (remaining_samples) {
  88. // uint16_t tri = (remaining_samples * 0.4);
  89. // tri = tri > 32767 ? 65535 - tri : tri;
  90. // float triangle = tri / 32768.0f;
  91. p->gate = false;
  92. p->trigger = false;// || (block_counter & 2047) > 1024;
  93. p->freeze = false; // || (block_counter & 2047) > 1024;
  94. p->granular.reverse = true;
  95. pot_noise += 0.05f * ((Random::GetSample() / 32768.0f) * 0.05f - pot_noise);
  96. p->position = Random::GetFloat();//triangle * 0.0f + 0.5f;
  97. p->size = Random::GetFloat();//0.99f;
  98. p->pitch = Random::GetFloat() * 24.0f; //0.0f + (triangle > 0.5f ? 1.0f : 0.0f) * 0.0f;
  99. p->density = 0.99f;
  100. p->texture = 0.7f;
  101. p->dry_wet = 1.0f;
  102. p->stereo_spread = 0.0f;
  103. p->feedback = 0.0f;
  104. p->reverb = 0.0f;
  105. ++block_counter;
  106. ShortFrame input[kBlockSize];
  107. ShortFrame output[kBlockSize];
  108. if (synthetic) {
  109. for (size_t i = 0; i < kBlockSize; ++i) {
  110. phase_ += 400.0f / kSampleRate; // (block_counter & 512 ? 110.0f : 220.0f) / kSampleRate;
  111. while (phase_ >= 1.0) {
  112. phase_ -= 1.0;
  113. }
  114. input[i].l = 16384.0f * sinf(phase_ * M_PI * 2);
  115. input[i].r = 32768.0f * (phase_ - 0.5);
  116. // input[i].r = input[i].l = 0;
  117. }
  118. remaining_samples -= kBlockSize;
  119. } else {
  120. if (fread(
  121. input,
  122. sizeof(ShortFrame),
  123. kBlockSize,
  124. fp_in) != kBlockSize) {
  125. break;
  126. }
  127. remaining_samples -= kBlockSize;
  128. }
  129. processor.Process(input, output, kBlockSize);
  130. processor.Prepare();
  131. fwrite(output, sizeof(ShortFrame), kBlockSize, fp_out);
  132. }
  133. fclose(fp_out);
  134. fclose(fp_in);
  135. }
  136. int main(void) {
  137. _MM_SET_FLUSH_ZERO_MODE(_MM_FLUSH_ZERO_ON);
  138. TestDSP();
  139. // TestGrainSize();
  140. }