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.

83 lines
2.3KB

  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 "peaks/processors.h"
  20. using namespace peaks;
  21. using namespace stmlib;
  22. const uint32_t kSampleRate = 48000;
  23. void write_wav_header(FILE* fp, int num_samples, int num_channels) {
  24. uint32_t l;
  25. uint16_t s;
  26. fwrite("RIFF", 4, 1, fp);
  27. l = 36 + num_samples * 2 * num_channels;
  28. fwrite(&l, 4, 1, fp);
  29. fwrite("WAVE", 4, 1, fp);
  30. fwrite("fmt ", 4, 1, fp);
  31. l = 16;
  32. fwrite(&l, 4, 1, fp);
  33. s = 1;
  34. fwrite(&s, 2, 1, fp);
  35. s = num_channels;
  36. fwrite(&s, 2, 1, fp);
  37. l = kSampleRate;
  38. fwrite(&l, 4, 1, fp);
  39. l = static_cast<uint32_t>(kSampleRate) * 2 * num_channels;
  40. fwrite(&l, 4, 1, fp);
  41. s = 2 * num_channels;
  42. fwrite(&s, 2, 1, fp);
  43. s = 16;
  44. fwrite(&s, 2, 1, fp);
  45. fwrite("data", 4, 1, fp);
  46. l = num_samples * 2 * num_channels;
  47. fwrite(&l, 4, 1, fp);
  48. }
  49. int main(void) {
  50. FILE* fp = fopen("peaks.wav", "wb");
  51. write_wav_header(fp, kSampleRate * 10, 1);
  52. processors[0].Init(1);
  53. processors[0].set_control_mode(CONTROL_MODE_HALF);
  54. processors[0].set_function(PROCESSOR_FUNCTION_BASS_DRUM);
  55. processors[0].set_parameter(0, 60000);
  56. processors[0].set_parameter(1, 40000);
  57. uint32_t period = kSampleRate / 2;
  58. for (uint32_t i = 0; i < kSampleRate * 10 ; ++i) {
  59. uint16_t tri = i;
  60. tri = tri > 32767 ? 65535 - tri : tri;
  61. uint8_t control = 0;
  62. if (i % period < (period / 4)) {
  63. control |= CONTROL_GATE;
  64. }
  65. if (i % period == 0) {
  66. control |= CONTROL_GATE_RISING;
  67. }
  68. int16_t s = processors[0].Process(control);
  69. fwrite(&s, sizeof(s), 1, fp);
  70. processors[0].Buffer();
  71. }
  72. }