1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 1998-2000, Matthes Bender
5  * Copyright (c) 2001-2009, RedWolf Design GmbH, http://www.clonk.de/
6  * Copyright (c) 2010-2016, The OpenClonk Team and contributors
7  *
8  * Distributed under the terms of the ISC license; see accompanying file
9  * "COPYING" for details.
10  *
11  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
12  * See accompanying file "TRADEMARK" for details.
13  *
14  * To redistribute this file separately, substitute the full license texts
15  * for the above references.
16  */
17 
18 /* Handles Music.ocg and randomly plays songs */
19 
20 #ifndef INC_C4MusicSystem
21 #define INC_C4MusicSystem
22 
23 #include "c4group/C4Group.h"
24 #include "platform/C4SoundIncludes.h"
25 
26 class C4MusicFileInfoNode;
27 class C4MusicFile;
28 
29 class C4MusicSystem
30 {
31 	friend class C4SoundEffect;
32 	friend class C4SoundInstance;
33 	friend class C4SoundSystem;
34 public:
35 	C4MusicSystem();
36 	~C4MusicSystem();
37 	void Clear();
38 	void ClearGame();
39 	void UpdateVolume(); // compute volume from game + config data
40 	void Execute(bool force_buffer_checks = false);
41 	void NotifySuccess();
42 	bool Init(const char * PlayList = nullptr);
43 	bool InitForScenario(C4Group & hGroup);
44 	bool Play(const char *szSongname = nullptr, bool fLoop = false, int fadetime_ms = 0, double max_resume_time = 0.0, bool allow_break = false);
45 	bool Play(C4MusicFile *NewFile, bool fLoop, double max_resume_time);
46 	bool Stop();
47 	void FadeOut(int fadeout_ms);
48 
49 	int SetPlayList(const char *szPlayList, bool fForceSwitch = false, int fadetime_ms = 0, double max_resume_time = 0.0);
50 
51 	bool ToggleOnOff(); // keyboard callback
52 
53 protected:
54 	// song list
55 	C4MusicFile* Songs{nullptr};
56 	int SongCount{0}, ASongCount, SCounter;
57 
58 	// play
59 	C4MusicFile *PlayMusicFile{nullptr};
60 	int Volume{100}; bool Loop;
61 
62 	// fading between two songs
63 	C4MusicFile *FadeMusicFile{nullptr}, *upcoming_music_file{nullptr};
64 	C4TimeMilliseconds FadeTimeStart, FadeTimeEnd;
65 
66 	// Wait time until next song
67 	bool is_waiting{false};
68 	C4TimeMilliseconds wait_time_end;
69 
70 	void LoadDir(const char *szPath); // load some music files (by wildcard / directory)
71 	void Load(const char *szFile); // load a music file
72 	void LoadMoreMusic(); // load music file names from MoreMusic.txt
73 	void ClearSongs();
74 
75 	bool GrpContainsMusic(C4Group &rGrp); // return whether this group contains music files
76 
77 	bool ScheduleWaitTime();
78 
79 	// SDL_mixer / OpenAL
80 	bool MODInitialized;
81 	bool InitializeMOD();
82 	void DeinitializeMOD();
83 #if AUDIO_TK == AUDIO_TK_OPENAL
84 private:
85 	ALCdevice* alcDevice{nullptr};
86 	ALCcontext* alcContext{nullptr};
87 public:
88 	void SelectContext();
GetContext()89 	ALCcontext *GetContext() const { return alcContext; }
GetDevice()90 	ALCdevice *GetDevice() const { return alcDevice; }
91 #endif
92 public:
IsMODInitialized()93 	inline bool IsMODInitialized() {return MODInitialized;}
94 
95 private:
96 	// scenario-defined music level
97 	int32_t game_music_level{100};
98 	// current play list
99 	StdCopyStrBuf playlist;
100 	bool playlist_valid{false};
101 	// Set to nonzero to allow pauses between songs
102 	int32_t music_break_min, music_break_max, music_break_chance;
103 	// Maximum time (in seconds) last position in a song is remembered until it would just be restarted from the beginning
104 	int32_t music_max_position_memory;
105 
106 	static const int32_t DefaultMusicBreak;
107 	static const int32_t DefaultMusicBreakChance;
108 	static const int32_t DefaultMusicMaxPositionMemory;
109 
110 public:
111 	void CompileFunc(class StdCompiler *comp);
112 
SetMusicBreakMin(int32_t val)113 	void SetMusicBreakMin(int32_t val) { music_break_min = std::max<int32_t>(val, 0); }
SetMusicBreakMax(int32_t val)114 	void SetMusicBreakMax(int32_t val) { music_break_max = std::max<int32_t>(val, 0); }
SetMusicBreakChance(int32_t val)115 	void SetMusicBreakChance(int32_t val) { music_break_chance = Clamp<int32_t>(val, 0, 100); }
SetMusicMaxPositionMemory(int32_t val)116 	void SetMusicMaxPositionMemory(int32_t val) { music_max_position_memory = val; }
117 	int32_t SetGameMusicLevel(int32_t val);
118 };
119 
120 
121 // --- helper stuff --- //
122 
123 enum MusicType { MUSICTYPE_MID, MUSICTYPE_MOD, MUSICTYPE_MP3, MUSICTYPE_OGG, MUSICTYPE_UNKNOWN };
124 
125 class C4MusicFileInfoNode // We need this for the MoreMusic.txt stuff
126 {
127 public:
C4MusicFileInfoNode()128 	C4MusicFileInfoNode() { next=nullptr; str=nullptr; };
~C4MusicFileInfoNode()129 	~C4MusicFileInfoNode() { if (str) delete [] str; }
130 	char* str;
131 	MusicType type;
132 	C4MusicFileInfoNode *next;
133 };
134 
135 MusicType GetMusicFileTypeByExtension(const char* ext);
136 
137 #endif
138