audio_stream_effects.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*******************************************************************************************
  2. *
  3. * raylib [audio] example - Music stream processing effects
  4. *
  5. * Example complexity rating: [★★★★] 4/4
  6. *
  7. * Example originally created with raylib 4.2, last time updated with raylib 5.0
  8. *
  9. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  10. * BSD-like license that allows static linking with closed source software
  11. *
  12. * Copyright (c) 2022-2025 Ramon Santamaria (@raysan5)
  13. *
  14. ********************************************************************************************/
  15. #include "raylib.h"
  16. #include <stdlib.h> // Required for: NULL
  17. // Required delay effect variables
  18. static float *delayBuffer = NULL;
  19. static unsigned int delayBufferSize = 0;
  20. static unsigned int delayReadIndex = 2;
  21. static unsigned int delayWriteIndex = 0;
  22. //------------------------------------------------------------------------------------
  23. // Module Functions Declaration
  24. //------------------------------------------------------------------------------------
  25. static void AudioProcessEffectLPF(void *buffer, unsigned int frames); // Audio effect: lowpass filter
  26. static void AudioProcessEffectDelay(void *buffer, unsigned int frames); // Audio effect: delay
  27. //------------------------------------------------------------------------------------
  28. // Program main entry point
  29. //------------------------------------------------------------------------------------
  30. int main(void)
  31. {
  32. // Initialization
  33. //--------------------------------------------------------------------------------------
  34. const int screenWidth = 800;
  35. const int screenHeight = 450;
  36. InitWindow(screenWidth, screenHeight, "raylib [audio] example - stream effects");
  37. InitAudioDevice(); // Initialize audio device
  38. Music music = LoadMusicStream("resources/country.mp3");
  39. // Allocate buffer for the delay effect
  40. delayBufferSize = 48000*2; // 1 second delay (device sampleRate*channels)
  41. delayBuffer = (float *)RL_CALLOC(delayBufferSize, sizeof(float));
  42. PlayMusicStream(music);
  43. float timePlayed = 0.0f; // Time played normalized [0.0f..1.0f]
  44. bool pause = false; // Music playing paused
  45. bool enableEffectLPF = false; // Enable effect low-pass-filter
  46. bool enableEffectDelay = false; // Enable effect delay (1 second)
  47. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  48. //--------------------------------------------------------------------------------------
  49. // Main game loop
  50. while (!WindowShouldClose()) // Detect window close button or ESC key
  51. {
  52. // Update
  53. //----------------------------------------------------------------------------------
  54. UpdateMusicStream(music); // Update music buffer with new stream data
  55. // Restart music playing (stop and play)
  56. if (IsKeyPressed(KEY_SPACE))
  57. {
  58. StopMusicStream(music);
  59. PlayMusicStream(music);
  60. }
  61. // Pause/Resume music playing
  62. if (IsKeyPressed(KEY_P))
  63. {
  64. pause = !pause;
  65. if (pause) PauseMusicStream(music);
  66. else ResumeMusicStream(music);
  67. }
  68. // Add/Remove effect: lowpass filter
  69. if (IsKeyPressed(KEY_F))
  70. {
  71. enableEffectLPF = !enableEffectLPF;
  72. if (enableEffectLPF) AttachAudioStreamProcessor(music.stream, AudioProcessEffectLPF);
  73. else DetachAudioStreamProcessor(music.stream, AudioProcessEffectLPF);
  74. }
  75. // Add/Remove effect: delay
  76. if (IsKeyPressed(KEY_D))
  77. {
  78. enableEffectDelay = !enableEffectDelay;
  79. if (enableEffectDelay) AttachAudioStreamProcessor(music.stream, AudioProcessEffectDelay);
  80. else DetachAudioStreamProcessor(music.stream, AudioProcessEffectDelay);
  81. }
  82. // Get normalized time played for current music stream
  83. timePlayed = GetMusicTimePlayed(music)/GetMusicTimeLength(music);
  84. if (timePlayed > 1.0f) timePlayed = 1.0f; // Make sure time played is no longer than music
  85. //----------------------------------------------------------------------------------
  86. // Draw
  87. //----------------------------------------------------------------------------------
  88. BeginDrawing();
  89. ClearBackground(RAYWHITE);
  90. DrawText("MUSIC SHOULD BE PLAYING!", 245, 150, 20, LIGHTGRAY);
  91. DrawRectangle(200, 180, 400, 12, LIGHTGRAY);
  92. DrawRectangle(200, 180, (int)(timePlayed*400.0f), 12, MAROON);
  93. DrawRectangleLines(200, 180, 400, 12, GRAY);
  94. DrawText("PRESS SPACE TO RESTART MUSIC", 215, 230, 20, LIGHTGRAY);
  95. DrawText("PRESS P TO PAUSE/RESUME MUSIC", 208, 260, 20, LIGHTGRAY);
  96. DrawText(TextFormat("PRESS F TO TOGGLE LPF EFFECT: %s", enableEffectLPF? "ON" : "OFF"), 200, 320, 20, GRAY);
  97. DrawText(TextFormat("PRESS D TO TOGGLE DELAY EFFECT: %s", enableEffectDelay? "ON" : "OFF"), 180, 350, 20, GRAY);
  98. EndDrawing();
  99. //----------------------------------------------------------------------------------
  100. }
  101. // De-Initialization
  102. //--------------------------------------------------------------------------------------
  103. UnloadMusicStream(music); // Unload music stream buffers from RAM
  104. CloseAudioDevice(); // Close audio device (music streaming is automatically stopped)
  105. RL_FREE(delayBuffer); // Free delay buffer
  106. CloseWindow(); // Close window and OpenGL context
  107. //--------------------------------------------------------------------------------------
  108. return 0;
  109. }
  110. //------------------------------------------------------------------------------------
  111. // Module Functions Definition
  112. //------------------------------------------------------------------------------------
  113. // Audio effect: lowpass filter
  114. static void AudioProcessEffectLPF(void *buffer, unsigned int frames)
  115. {
  116. static float low[2] = { 0.0f, 0.0f };
  117. static const float cutoff = 70.0f / 44100.0f; // 70 Hz lowpass filter
  118. const float k = cutoff / (cutoff + 0.1591549431f); // RC filter formula
  119. // Converts the buffer data before using it
  120. float *bufferData = (float *)buffer;
  121. for (unsigned int i = 0; i < frames*2; i += 2)
  122. {
  123. const float l = bufferData[i];
  124. const float r = bufferData[i + 1];
  125. low[0] += k * (l - low[0]);
  126. low[1] += k * (r - low[1]);
  127. bufferData[i] = low[0];
  128. bufferData[i + 1] = low[1];
  129. }
  130. }
  131. // Audio effect: delay
  132. static void AudioProcessEffectDelay(void *buffer, unsigned int frames)
  133. {
  134. for (unsigned int i = 0; i < frames*2; i += 2)
  135. {
  136. float leftDelay = delayBuffer[delayReadIndex++]; // ERROR: Reading buffer -> WHY??? Maybe thread related???
  137. float rightDelay = delayBuffer[delayReadIndex++];
  138. if (delayReadIndex == delayBufferSize) delayReadIndex = 0;
  139. ((float *)buffer)[i] = 0.5f*((float *)buffer)[i] + 0.5f*leftDelay;
  140. ((float *)buffer)[i + 1] = 0.5f*((float *)buffer)[i + 1] + 0.5f*rightDelay;
  141. delayBuffer[delayWriteIndex++] = ((float *)buffer)[i];
  142. delayBuffer[delayWriteIndex++] = ((float *)buffer)[i + 1];
  143. if (delayWriteIndex == delayBufferSize) delayWriteIndex = 0;
  144. }
  145. }