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.

100 lines
3.5 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [audio] example - Multichannel sound playing
  4. *
  5. * This example has been created using raylib 2.6 (www.raylib.com)
  6. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  7. *
  8. * Example contributed by Chris Camacho (@codifies) and reviewed by Ramon Santamaria (@raysan5)
  9. *
  10. * Copyright (c) 2019 Chris Camacho (@codifies) and Ramon Santamaria (@raysan5)
  11. *
  12. ********************************************************************************************/
  13. #include "raylib.h"
  14. int main(void)
  15. {
  16. // Initialization
  17. //--------------------------------------------------------------------------------------
  18. const int screenWidth = 800;
  19. const int screenHeight = 450;
  20. InitWindow(screenWidth, screenHeight, "raylib [audio] example - Multichannel sound playing");
  21. InitAudioDevice(); // Initialize audio device
  22. Sound fxWav = LoadSound("resources/sound.wav"); // Load WAV audio file
  23. Sound fxOgg = LoadSound("resources/tanatana.ogg"); // Load OGG audio file
  24. int frame = 0;
  25. SetSoundVolume(fxWav, 0.2);
  26. PlaySound(fxOgg);
  27. bool inhibitWav = false;
  28. bool inhibitOgg = false;
  29. int maxFrame = 60;
  30. int soundsCounter = 0;
  31. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  32. //--------------------------------------------------------------------------------------
  33. // Main game loop
  34. while (!WindowShouldClose()) // Detect window close button or ESC key
  35. {
  36. // Update
  37. //----------------------------------------------------------------------------------
  38. frame++;
  39. if (IsKeyDown(KEY_ENTER)) inhibitWav = !inhibitWav;
  40. if (IsKeyDown(KEY_SPACE)) inhibitOgg = !inhibitOgg;
  41. // Deliberatly hammer the play pool to see what dropping old pool entries sounds like....
  42. if ((frame%5) == 0)
  43. {
  44. if (!inhibitWav) PlaySoundMulti(fxWav);
  45. }
  46. if (frame == maxFrame)
  47. {
  48. if (!inhibitOgg) PlaySoundMulti(fxOgg);
  49. frame = 0;
  50. maxFrame = GetRandomValue(6,12);
  51. }
  52. soundsCounter = GetSoundsPlaying();
  53. //----------------------------------------------------------------------------------
  54. // Draw
  55. //----------------------------------------------------------------------------------
  56. BeginDrawing();
  57. ClearBackground(RAYWHITE);
  58. DrawText("Multichannel sound abuse!", 200, 180, 20, LIGHTGRAY);
  59. DrawText("Space to inhibit new ogg triggering", 200, 200, 20, LIGHTGRAY);
  60. DrawText("Enter to inhibit new wav triggering", 200, 220, 20, LIGHTGRAY);
  61. DrawText(FormatText("Number of concurrentsounds: %i", soundsCounter), 200, 280, 20, LIGHTGRAY);
  62. EndDrawing();
  63. //----------------------------------------------------------------------------------
  64. }
  65. // De-Initialization
  66. //--------------------------------------------------------------------------------------
  67. StopSoundMulti(); // We must stop the buffer pool before unloading
  68. UnloadSound(fxWav); // Unload sound data
  69. UnloadSound(fxOgg); // Unload sound data
  70. CloseAudioDevice(); // Close audio device
  71. CloseWindow(); // Close window and OpenGL context
  72. //--------------------------------------------------------------------------------------
  73. return 0;
  74. }