raylib/ports/audio/audio_module_playing

Licence This example has its own licence, rather than Odin's: raylib/ports/LICENSE
These are ported Raylib examples, and as such, they are subject to Raylib's license. Copyright (c) 2013-2024 Ramon Santamaria (@raysan5)
The rest of the licence
This software is provided "as-is", without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution.
A screenshot of audio_module_playing

Code

audio_module_playing.odin ¶
167 linesSource

1/*******************************************************************************************
2*
3*   raylib [audio] example - module playing
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-2025 Ramon Santamaria (@raysan5)
13*
14********************************************************************************************/
15
16package raylib_examples
17
18import rl "vendor:raylib"
19
20CircleWave :: struct {
21	position: rl.Vector2,
22	radius: f32,
23	alpha: f32,
24	speed: f32,
25	color: rl.Color,
26}
27
28//------------------------------------------------------------------------------------
29// Program main entry point
30//------------------------------------------------------------------------------------
31main :: proc() {
32	// Initialization
33	//--------------------------------------------------------------------------------------
34	SCREEN_WIDTH :: 800
35	SCREEN_HEIGHT :: 450
36
37	MAX_CIRCLES :: 64
38
39	rl.SetConfigFlags({.MSAA_4X_HINT})  // NOTE: Try to enable MSAA 4X
40
41	rl.InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "raylib [audio] example - module playing")
42
43	rl.InitAudioDevice()                  // Initialize audio device
44
45	colors := [14]rl.Color {rl.ORANGE, rl.RED, rl.GOLD, rl.LIME, rl.BLUE, rl.VIOLET, rl.BROWN, rl.LIGHTGRAY, rl.PINK,
46						 rl.YELLOW, rl.GREEN, rl.SKYBLUE, rl.PURPLE, rl.BEIGE}
47
48	// Creates some circles for visual effect
49	circles: [MAX_CIRCLES]CircleWave
50
51	for &c in circles {
52		radius := rl.GetRandomValue(10, 40)
53
54		c = {
55			radius = f32(radius),
56			position = {f32(rl.GetRandomValue(radius, SCREEN_WIDTH - radius)), f32(rl.GetRandomValue(radius, SCREEN_HEIGHT - radius))},
57			speed = f32(rl.GetRandomValue(1, 100))/2000,
58			color = colors[rl.GetRandomValue(0, 13)],
59		}
60	}
61
62	music := rl.LoadMusicStream("resources/mini1111.xm")
63	defer rl.UnloadMusicStream(music)          // Unload music stream buffers from RAM
64	music.looping = false
65	pitch: f32 = 1
66
67	rl.PlayMusicStream(music)
68
69	time_played: f32
70	pause: bool
71
72	rl.SetTargetFPS(60)               // Set our game to run at 60 frames-per-second
73	//--------------------------------------------------------------------------------------
74
75	// Main game loop
76	for !rl.WindowShouldClose() {    // Detect window close button or ESC key
77		// Update
78		//----------------------------------------------------------------------------------
79		rl.UpdateMusicStream(music)      // Update music buffer with new stream data
80
81		// Restart music playing (stop and play)
82		if rl.IsKeyPressed(.SPACE) {
83			rl.StopMusicStream(music)
84			rl.PlayMusicStream(music)
85			pause = false
86		}
87
88		// Pause/Resume music playing
89		if rl.IsKeyPressed(.P) {
90			pause = !pause
91
92			if pause {
93				rl.PauseMusicStream(music)
94			} else {
95				rl.ResumeMusicStream(music)
96			}
97		}
98
99		if rl.IsKeyDown(.DOWN) {
100			pitch -= 0.01
101		} else if rl.IsKeyDown(.UP) {
102			pitch += 0.01
103		}
104
105		rl.SetMusicPitch(music, pitch)
106
107		// Get time_played scaled to bar dimensions
108		time_played = rl.GetMusicTimePlayed(music)/rl.GetMusicTimeLength(music)*(SCREEN_WIDTH - 40)
109
110		// Color circles animation
111		if !pause {
112			for &c in circles {
113				c.alpha += c.speed
114				c.radius += c.speed*10
115
116				if c.alpha > 1 {
117					c.speed *= -1
118				}
119
120				if c.alpha <= 0 {
121					radius := rl.GetRandomValue(10, 40)
122
123					c = {
124						radius = f32(radius),
125						position = {f32(rl.GetRandomValue(radius, SCREEN_WIDTH - radius)), f32(rl.GetRandomValue(radius, SCREEN_HEIGHT - radius))},
126						speed = f32(rl.GetRandomValue(1, 100))/2000,
127						color = colors[rl.GetRandomValue(0, 13)],
128					}
129				}
130			}
131		}
132		
133		//----------------------------------------------------------------------------------
134
135		// Draw
136		//----------------------------------------------------------------------------------
137		rl.BeginDrawing()
138
139			rl.ClearBackground(rl.RAYWHITE)
140
141			for c in circles {
142				rl.DrawCircleV(c.position, c.radius, rl.Fade(c.color, c.alpha))
143			}
144
145			// Draw time bar
146			rl.DrawRectangle(20, SCREEN_HEIGHT - 20 - 12, SCREEN_WIDTH - 40, 12, rl.LIGHTGRAY)
147			rl.DrawRectangle(20, SCREEN_HEIGHT - 20 - 12, i32(time_played), 12, rl.MAROON)
148			rl.DrawRectangleLines(20, SCREEN_HEIGHT - 20 - 12, SCREEN_WIDTH - 40, 12, rl.GRAY)
149
150			// Draw help instructions
151			rl.DrawRectangle(20, 20, 425, 145, rl.WHITE)
152			rl.DrawRectangleLines(20, 20, 425, 145, rl.GRAY)
153			rl.DrawText("PRESS SPACE TO RESTART MUSIC", 40, 40, 20, rl.BLACK)
154			rl.DrawText("PRESS P TO PAUSE/RESUME", 40, 70, 20, rl.BLACK)
155			rl.DrawText("PRESS UP/DOWN TO CHANGE SPEED", 40, 100, 20, rl.BLACK)
156			rl.DrawText(rl.TextFormat("SPEED: %f", pitch), 40, 130, 20, rl.MAROON)
157
158		rl.EndDrawing()
159		//----------------------------------------------------------------------------------
160	}
161
162	// De-Initialization
163	//--------------------------------------------------------------------------------------
164	rl.CloseAudioDevice()     // Close audio device (music streaming is automatically stopped)
165	rl.CloseWindow()          // Close window and OpenGL context
166	//--------------------------------------------------------------------------------------
167}

Assets 1

Declarations Used 50