Files
raylib/examples/audio/audio_music_stream.c

145 lines
5.1 KiB
C
Raw Normal View History

/*******************************************************************************************
*
* raylib [audio] example - music stream
*
* Example complexity rating: [] 1/4
*
* Example originally created with raylib 1.3, last time updated with raylib 4.2
*
2022-07-20 01:28:37 +02:00
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
* BSD-like license that allows static linking with closed source software
*
* Copyright (c) 2015-2025 Ramon Santamaria (@raysan5)
*
********************************************************************************************/
#include "raylib.h"
2022-06-21 19:53:18 +02:00
//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
2019-05-20 16:36:42 +02:00
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
2019-05-20 16:36:42 +02:00
const int screenWidth = 800;
const int screenHeight = 450;
2014-09-29 23:41:05 +02:00
InitWindow(screenWidth, screenHeight, "raylib [audio] example - music stream");
2014-09-29 23:41:05 +02:00
InitAudioDevice(); // Initialize audio device
2014-09-29 23:41:05 +02:00
Music music = LoadMusicStream("resources/country.mp3");
2019-05-20 16:36:42 +02:00
PlayMusicStream(music);
2014-09-29 23:41:05 +02:00
2022-07-29 12:08:04 +02:00
float timePlayed = 0.0f; // Time played normalized [0.0f..1.0f]
bool pause = false; // Music playing paused
2025-11-20 00:03:08 +01:00
float pan = 0.0f; // Default audio pan center [-1.0f..1.0f]
SetMusicPan(music, pan);
2025-11-20 00:03:08 +01:00
float volume = 0.8f; // Default audio volume [0.0f..1.0f]
SetMusicVolume(music, volume);
2022-11-08 22:47:05 +02:00
SetTargetFPS(30); // Set our game to run at 30 frames-per-second
//--------------------------------------------------------------------------------------
2014-09-29 23:41:05 +02:00
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
2019-05-20 16:36:42 +02:00
UpdateMusicStream(music); // Update music buffer with new stream data
2025-08-07 17:08:22 +02:00
2022-08-02 10:23:54 +02:00
// Restart music playing (stop and play)
if (IsKeyPressed(KEY_SPACE))
{
StopMusicStream(music);
PlayMusicStream(music);
}
// Pause/Resume music playing
if (IsKeyPressed(KEY_P))
{
pause = !pause;
if (pause) PauseMusicStream(music);
else ResumeMusicStream(music);
}
2025-11-20 00:03:08 +01:00
// Set audio pan
if (IsKeyDown(KEY_LEFT))
2025-11-20 00:03:08 +01:00
{
pan -= 0.05f;
if (pan < -1.0f) pan = -1.0f;
SetMusicPan(music, pan);
}
else if (IsKeyDown(KEY_RIGHT))
2025-11-20 00:03:08 +01:00
{
pan += 0.05f;
if (pan > 1.0f) pan = 1.0f;
SetMusicPan(music, pan);
}
2025-11-20 00:03:08 +01:00
// Set audio volume
if (IsKeyDown(KEY_DOWN))
2025-11-20 00:03:08 +01:00
{
volume -= 0.05f;
if (volume < 0.0f) volume = 0.0f;
SetMusicVolume(music, volume);
}
else if (IsKeyDown(KEY_UP))
2025-11-20 00:03:08 +01:00
{
volume += 0.05f;
if (volume > 1.0f) volume = 1.0f;
SetMusicVolume(music, volume);
}
2019-05-20 16:36:42 +02:00
2022-07-29 12:08:04 +02:00
// Get normalized time played for current music stream
timePlayed = GetMusicTimePlayed(music)/GetMusicTimeLength(music);
2022-07-29 12:08:04 +02:00
if (timePlayed > 1.0f) timePlayed = 1.0f; // Make sure time played is no longer than music
//----------------------------------------------------------------------------------
2014-09-29 23:41:05 +02:00
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
2014-09-29 23:41:05 +02:00
ClearBackground(RAYWHITE);
2014-09-29 23:41:05 +02:00
DrawText("MUSIC SHOULD BE PLAYING!", 255, 150, 20, LIGHTGRAY);
2025-11-20 00:03:08 +01:00
DrawText("LEFT-RIGHT for PAN CONTROL", 320, 74, 10, DARKBLUE);
DrawRectangle(300, 100, 200, 12, LIGHTGRAY);
DrawRectangleLines(300, 100, 200, 12, GRAY);
DrawRectangle(300 + (pan + 1.0)/2.0f*200 - 5, 92, 10, 28, DARKGRAY);
2014-09-29 23:41:05 +02:00
DrawRectangle(200, 200, 400, 12, LIGHTGRAY);
2022-07-29 12:08:04 +02:00
DrawRectangle(200, 200, (int)(timePlayed*400.0f), 12, MAROON);
DrawRectangleLines(200, 200, 400, 12, GRAY);
2019-05-20 16:36:42 +02:00
DrawText("PRESS SPACE TO RESTART MUSIC", 215, 250, 20, LIGHTGRAY);
DrawText("PRESS P TO PAUSE/RESUME MUSIC", 208, 280, 20, LIGHTGRAY);
2025-11-20 00:03:08 +01:00
DrawText("UP-DOWN for VOLUME CONTROL", 320, 334, 10, DARKGREEN);
DrawRectangle(300, 360, 200, 12, LIGHTGRAY);
DrawRectangleLines(300, 360, 200, 12, GRAY);
DrawRectangle(300 + volume*200 - 5, 352, 10, 28, DARKGRAY);
2014-09-29 23:41:05 +02:00
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadMusicStream(music); // Unload music stream buffers from RAM
CloseAudioDevice(); // Close audio device (music streaming is automatically stopped)
2014-09-29 23:41:05 +02:00
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
2014-09-29 23:41:05 +02:00
return 0;
2022-11-08 22:47:05 +02:00
}