1 /**
2 Allows the player to stream music from disk without loading it entirely into memory
3 */
4 module dgt.music;
5 import derelict.sdl2.mixer;
6 import dgt.util : nullTerminate;
7 
8 /**
9 Represents a piece of music that can be played by streaming
10 
11 Only one piece of music can be played at once
12 */
13 struct Music
14 {
15 	private Mix_Music* source;
16 
17 	@disable this();
18 	@disable this(this);
19 
20     @nogc nothrow:
21     ///Load the music clip from a path
22     this(string path)
23     {
24 		auto pathNullTerminated = nullTerminate(path);
25         source = Mix_LoadMUS(pathNullTerminated.ptr);
26 		pathNullTerminated.destroy();
27     }
28 
29 	~this()
30 	{
31 		Mix_FreeMusic(source);
32 	}
33 
34     ///Get the volume of the music channel
35     @property int volume() const { return Mix_VolumeMusic(-1); }
36     ///Set the volume of the music channel
37     @property int volume(in int value) { return Mix_VolumeMusic(value); }
38     ///Play a song a given number of times
39     void play(in int times = 1)
40     {
41         Mix_PlayMusic(source, times - 1);
42     }
43     ///Pause the music
44     void pause()
45     {
46         Mix_PauseMusic();
47     }
48     ///Resume the music from pause
49     void resume()
50     {
51         Mix_ResumeMusic();
52     }
53     ///Stop the music
54     void stop()
55     {
56         Mix_HaltMusic();
57     }
58     ///Fade the music out over a given number of milliseconds
59     void fadeOut(int ms)
60     {
61         Mix_FadeOutMusic(ms);
62     }
63 }