Python synthesizer stuff
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.

138 lines
3.6KB

  1. #include <math.h>
  2. #include <stdlib.h>
  3. #include <SDL/SDL.h>
  4. #include <SDL/SDL_mixer.h>
  5. #define CHUNK_SIZE 4096
  6. #if 1
  7. #define FLANGER_BUFFER_SIZE 88200
  8. #else
  9. #define FLANGER_BUFFER_SIZE 2000
  10. #endif
  11. void handle_exit(void)
  12. {
  13. Mix_Quit();
  14. SDL_Quit();
  15. puts("Shutdown complete.");
  16. }
  17. static struct flanger {
  18. int samples;
  19. int frequency;
  20. int
  21. start_ms,
  22. stop_ms;
  23. int cur_delay;
  24. int sweep_dir;
  25. int sweep_speed;
  26. Sint16 buf[FLANGER_BUFFER_SIZE];
  27. } flange_chan;
  28. /* Perform flanging effect */
  29. void flanger_func(int chan, void *stream, int len, void *udata)
  30. {
  31. int i;
  32. Sint16 *sbuf = stream;
  33. struct flanger *flanger = udata;
  34. int local_len = len / sizeof(Sint16);
  35. /*printf("Samples: %d, Local Len: %d, Len: %d\n", flanger->samples, local_len, len);*/
  36. for (i = 0; i < local_len; i++) {
  37. flanger->buf[(flanger->samples + i) %
  38. FLANGER_BUFFER_SIZE] = sbuf[i];
  39. sbuf[i] = sbuf[i] / 2 + flanger->buf[(flanger->samples +
  40. (FLANGER_BUFFER_SIZE - flanger->cur_delay / 1000 * 2) + i) %
  41. FLANGER_BUFFER_SIZE] / 2;
  42. if (!(i%2)) {
  43. if (flanger->cur_delay >= flanger->frequency /
  44. (1000 / flanger->stop_ms) * 1000) {
  45. flanger->sweep_dir = -flanger->sweep_speed;
  46. } else if (flanger->cur_delay <= flanger->frequency /
  47. (1000 / flanger->start_ms) * 1000) {
  48. flanger->sweep_dir = flanger->sweep_speed;
  49. }
  50. flanger->cur_delay += flanger->sweep_dir;
  51. }
  52. /*sbuf[i] = 0;*/
  53. }
  54. flanger->samples = (flanger->samples + local_len) % FLANGER_BUFFER_SIZE;
  55. return;
  56. }
  57. int main(int argc, char *argv[])
  58. {
  59. char *mp3_file;
  60. Mix_Music *mp3_stream;
  61. /* Audio settings */
  62. int frequency = 44100;
  63. Uint16 format = AUDIO_S16;
  64. int channels = 2;
  65. if (argc != 2) {
  66. printf("Usage %s: <mp3-file>\n", argv[0]);
  67. return EXIT_SUCCESS;
  68. }
  69. mp3_file = argv[1];
  70. /* Setup shutdown sequence */
  71. atexit(handle_exit);
  72. /* Setup SDL & friends */
  73. SDL_Init(SDL_INIT_AUDIO);
  74. Mix_Init(MIX_INIT_MP3);
  75. /* Open audio */
  76. if (Mix_OpenAudio(frequency, format, channels, CHUNK_SIZE)) {
  77. printf("Mix_OpenAudio failed: %s\n", Mix_GetError());
  78. /* Mix_FreeMusic(mp3_stream); */
  79. return EXIT_FAILURE;
  80. }
  81. /* Verify audio compatibility */
  82. Mix_QuerySpec(&frequency, &format, &channels);
  83. printf("Device settings are: %d, %hd, %d\n", frequency, format, channels);
  84. if (format != AUDIO_S16) {
  85. puts("Selected device format incompatible :-(\n");
  86. return EXIT_FAILURE;
  87. }
  88. /* Load MP3 file */
  89. mp3_stream = Mix_LoadMUS(mp3_file);
  90. if (!mp3_stream) {
  91. printf("Mix_LoadMUS failed: %s\n", Mix_GetError());
  92. return EXIT_FAILURE;
  93. }
  94. /* Setup flangers */
  95. memset(flange_chan.buf, 0x0, sizeof(short) * FLANGER_BUFFER_SIZE);
  96. flange_chan.frequency = frequency;
  97. flange_chan.samples = 0;
  98. flange_chan.start_ms = 1;
  99. flange_chan.stop_ms = 20;
  100. flange_chan.sweep_speed = 5;
  101. flange_chan.cur_delay = 0;
  102. flange_chan.sweep_dir = 1;
  103. if (!Mix_RegisterEffect(MIX_CHANNEL_POST, flanger_func, NULL, &flange_chan)) {
  104. printf("Mix_RegisterEffect failed: %s\n", Mix_GetError());
  105. return EXIT_FAILURE;
  106. }
  107. /* Start playback */
  108. if (Mix_PlayMusic(mp3_stream, 0) < 0) {
  109. printf("Mix_PlayMusic failed: %s\n", Mix_GetError());
  110. Mix_FreeMusic(mp3_stream);
  111. return EXIT_FAILURE;
  112. }
  113. while (Mix_PlayingMusic())
  114. SDL_Delay(100);
  115. return EXIT_SUCCESS;
  116. }