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.

123 line
4.8 KiB

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