audio_mixed_processor.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*******************************************************************************************
  2. *
  3. * raylib [audio] example - Mixed audio processing
  4. *
  5. * Example complexity rating: [★★★★] 4/4
  6. *
  7. * Example originally created with raylib 4.2, last time updated with raylib 4.2
  8. *
  9. * Example contributed by hkc (@hatkidchan) and reviewed by Ramon Santamaria (@raysan5)
  10. *
  11. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  12. * BSD-like license that allows static linking with closed source software
  13. *
  14. * Copyright (c) 2023 hkc (@hatkidchan)
  15. *
  16. ********************************************************************************************/
  17. #include "raylib.h"
  18. #include <math.h>
  19. static float exponent = 1.0f; // Audio exponentiation value
  20. static float averageVolume[400] = { 0.0f }; // Average volume history
  21. //------------------------------------------------------------------------------------
  22. // Audio processing function
  23. //------------------------------------------------------------------------------------
  24. void ProcessAudio(void *buffer, unsigned int frames)
  25. {
  26. float *samples = (float *)buffer; // Samples internally stored as <float>s
  27. float average = 0.0f; // Temporary average volume
  28. for (unsigned int frame = 0; frame < frames; frame++)
  29. {
  30. float *left = &samples[frame * 2 + 0], *right = &samples[frame * 2 + 1];
  31. *left = powf(fabsf(*left), exponent) * ( (*left < 0.0f)? -1.0f : 1.0f );
  32. *right = powf(fabsf(*right), exponent) * ( (*right < 0.0f)? -1.0f : 1.0f );
  33. average += fabsf(*left) / frames; // accumulating average volume
  34. average += fabsf(*right) / frames;
  35. }
  36. // Moving history to the left
  37. for (int i = 0; i < 399; i++) averageVolume[i] = averageVolume[i + 1];
  38. averageVolume[399] = average; // Adding last average value
  39. }
  40. //------------------------------------------------------------------------------------
  41. // Program main entry point
  42. //------------------------------------------------------------------------------------
  43. int main(void)
  44. {
  45. // Initialization
  46. //--------------------------------------------------------------------------------------
  47. const int screenWidth = 800;
  48. const int screenHeight = 450;
  49. InitWindow(screenWidth, screenHeight, "raylib [audio] example - processing mixed output");
  50. InitAudioDevice(); // Initialize audio device
  51. AttachAudioMixedProcessor(ProcessAudio);
  52. Music music = LoadMusicStream("resources/country.mp3");
  53. Sound sound = LoadSound("resources/coin.wav");
  54. PlayMusicStream(music);
  55. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  56. //--------------------------------------------------------------------------------------
  57. // Main game loop
  58. while (!WindowShouldClose()) // Detect window close button or ESC key
  59. {
  60. // Update
  61. //----------------------------------------------------------------------------------
  62. UpdateMusicStream(music); // Update music buffer with new stream data
  63. // Modify processing variables
  64. //----------------------------------------------------------------------------------
  65. if (IsKeyPressed(KEY_LEFT)) exponent -= 0.05f;
  66. if (IsKeyPressed(KEY_RIGHT)) exponent += 0.05f;
  67. if (exponent <= 0.5f) exponent = 0.5f;
  68. if (exponent >= 3.0f) exponent = 3.0f;
  69. if (IsKeyPressed(KEY_SPACE)) PlaySound(sound);
  70. // Draw
  71. //----------------------------------------------------------------------------------
  72. BeginDrawing();
  73. ClearBackground(RAYWHITE);
  74. DrawText("MUSIC SHOULD BE PLAYING!", 255, 150, 20, LIGHTGRAY);
  75. DrawText(TextFormat("EXPONENT = %.2f", exponent), 215, 180, 20, LIGHTGRAY);
  76. DrawRectangle(199, 199, 402, 34, LIGHTGRAY);
  77. for (int i = 0; i < 400; i++)
  78. {
  79. DrawLine(201 + i, 232 - (int)(averageVolume[i] * 32), 201 + i, 232, MAROON);
  80. }
  81. DrawRectangleLines(199, 199, 402, 34, GRAY);
  82. DrawText("PRESS SPACE TO PLAY OTHER SOUND", 200, 250, 20, LIGHTGRAY);
  83. DrawText("USE LEFT AND RIGHT ARROWS TO ALTER DISTORTION", 140, 280, 20, LIGHTGRAY);
  84. EndDrawing();
  85. //----------------------------------------------------------------------------------
  86. }
  87. // De-Initialization
  88. //--------------------------------------------------------------------------------------
  89. UnloadMusicStream(music); // Unload music stream buffers from RAM
  90. DetachAudioMixedProcessor(ProcessAudio); // Disconnect audio processor
  91. CloseAudioDevice(); // Close audio device (music streaming is automatically stopped)
  92. CloseWindow(); // Close window and OpenGL context
  93. //--------------------------------------------------------------------------------------
  94. return 0;
  95. }