audio_module_playing.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*******************************************************************************************
  2. *
  3. * raylib [audio] example - Module playing (streaming)
  4. *
  5. * Example complexity rating: [★☆☆☆] 1/4
  6. *
  7. * Example originally created with raylib 1.5, last time updated with raylib 3.5
  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) 2016-2024 Ramon Santamaria (@raysan5)
  13. *
  14. ********************************************************************************************/
  15. #include "raylib.h"
  16. #define MAX_CIRCLES 64
  17. typedef struct {
  18. Vector2 position;
  19. float radius;
  20. float alpha;
  21. float speed;
  22. Color color;
  23. } CircleWave;
  24. //------------------------------------------------------------------------------------
  25. // Program main entry point
  26. //------------------------------------------------------------------------------------
  27. int main(void)
  28. {
  29. // Initialization
  30. //--------------------------------------------------------------------------------------
  31. const int screenWidth = 800;
  32. const int screenHeight = 450;
  33. SetConfigFlags(FLAG_MSAA_4X_HINT); // NOTE: Try to enable MSAA 4X
  34. InitWindow(screenWidth, screenHeight, "raylib [audio] example - module playing (streaming)");
  35. InitAudioDevice(); // Initialize audio device
  36. Color colors[14] = { ORANGE, RED, GOLD, LIME, BLUE, VIOLET, BROWN, LIGHTGRAY, PINK,
  37. YELLOW, GREEN, SKYBLUE, PURPLE, BEIGE };
  38. // Creates some circles for visual effect
  39. CircleWave circles[MAX_CIRCLES] = { 0 };
  40. for (int i = MAX_CIRCLES - 1; i >= 0; i--)
  41. {
  42. circles[i].alpha = 0.0f;
  43. circles[i].radius = (float)GetRandomValue(10, 40);
  44. circles[i].position.x = (float)GetRandomValue((int)circles[i].radius, (int)(screenWidth - circles[i].radius));
  45. circles[i].position.y = (float)GetRandomValue((int)circles[i].radius, (int)(screenHeight - circles[i].radius));
  46. circles[i].speed = (float)GetRandomValue(1, 100)/2000.0f;
  47. circles[i].color = colors[GetRandomValue(0, 13)];
  48. }
  49. Music music = LoadMusicStream("resources/mini1111.xm");
  50. music.looping = false;
  51. float pitch = 1.0f;
  52. PlayMusicStream(music);
  53. float timePlayed = 0.0f;
  54. bool pause = false;
  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. // Restart music playing (stop and play)
  64. if (IsKeyPressed(KEY_SPACE))
  65. {
  66. StopMusicStream(music);
  67. PlayMusicStream(music);
  68. pause = false;
  69. }
  70. // Pause/Resume music playing
  71. if (IsKeyPressed(KEY_P))
  72. {
  73. pause = !pause;
  74. if (pause) PauseMusicStream(music);
  75. else ResumeMusicStream(music);
  76. }
  77. if (IsKeyDown(KEY_DOWN)) pitch -= 0.01f;
  78. else if (IsKeyDown(KEY_UP)) pitch += 0.01f;
  79. SetMusicPitch(music, pitch);
  80. // Get timePlayed scaled to bar dimensions
  81. timePlayed = GetMusicTimePlayed(music)/GetMusicTimeLength(music)*(screenWidth - 40);
  82. // Color circles animation
  83. for (int i = MAX_CIRCLES - 1; (i >= 0) && !pause; i--)
  84. {
  85. circles[i].alpha += circles[i].speed;
  86. circles[i].radius += circles[i].speed*10.0f;
  87. if (circles[i].alpha > 1.0f) circles[i].speed *= -1;
  88. if (circles[i].alpha <= 0.0f)
  89. {
  90. circles[i].alpha = 0.0f;
  91. circles[i].radius = (float)GetRandomValue(10, 40);
  92. circles[i].position.x = (float)GetRandomValue((int)circles[i].radius, (int)(screenWidth - circles[i].radius));
  93. circles[i].position.y = (float)GetRandomValue((int)circles[i].radius, (int)(screenHeight - circles[i].radius));
  94. circles[i].color = colors[GetRandomValue(0, 13)];
  95. circles[i].speed = (float)GetRandomValue(1, 100)/2000.0f;
  96. }
  97. }
  98. //----------------------------------------------------------------------------------
  99. // Draw
  100. //----------------------------------------------------------------------------------
  101. BeginDrawing();
  102. ClearBackground(RAYWHITE);
  103. for (int i = MAX_CIRCLES - 1; i >= 0; i--)
  104. {
  105. DrawCircleV(circles[i].position, circles[i].radius, Fade(circles[i].color, circles[i].alpha));
  106. }
  107. // Draw time bar
  108. DrawRectangle(20, screenHeight - 20 - 12, screenWidth - 40, 12, LIGHTGRAY);
  109. DrawRectangle(20, screenHeight - 20 - 12, (int)timePlayed, 12, MAROON);
  110. DrawRectangleLines(20, screenHeight - 20 - 12, screenWidth - 40, 12, GRAY);
  111. // Draw help instructions
  112. DrawRectangle(20, 20, 425, 145, WHITE);
  113. DrawRectangleLines(20, 20, 425, 145, GRAY);
  114. DrawText("PRESS SPACE TO RESTART MUSIC", 40, 40, 20, BLACK);
  115. DrawText("PRESS P TO PAUSE/RESUME", 40, 70, 20, BLACK);
  116. DrawText("PRESS UP/DOWN TO CHANGE SPEED", 40, 100, 20, BLACK);
  117. DrawText(TextFormat("SPEED: %f", pitch), 40, 130, 20, MAROON);
  118. EndDrawing();
  119. //----------------------------------------------------------------------------------
  120. }
  121. // De-Initialization
  122. //--------------------------------------------------------------------------------------
  123. UnloadMusicStream(music); // Unload music stream buffers from RAM
  124. CloseAudioDevice(); // Close audio device (music streaming is automatically stopped)
  125. CloseWindow(); // Close window and OpenGL context
  126. //--------------------------------------------------------------------------------------
  127. return 0;
  128. }