1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #ifndef GROOVIE_MUSIC_H
24 #define GROOVIE_MUSIC_H
25 
26 #include "common/array.h"
27 #include "common/mutex.h"
28 #include "audio/mididrv.h"
29 #include "audio/mixer.h"
30 
31 class MidiParser;
32 
33 namespace Groovie {
34 
35 class GroovieEngine;
36 
37 class MusicPlayer {
38 public:
39 	MusicPlayer(GroovieEngine *vm);
40 	virtual ~MusicPlayer();
41 
42 	void playSong(uint32 fileref);
43 	void setBackgroundSong(uint32 fileref);
44 	void playCD(uint8 track);
45 	void startBackground();
46 
47 	void frameTick();
48 	void setBackgroundDelay(uint16 delay);
49 
50 	// Volume
51 	void setUserVolume(uint16 volume);
52 	void setGameVolume(uint16 volume, uint16 time);
53 
54 private:
55 	// Song playback
56 	bool play(uint32 fileref, bool loop);
57 	bool _isPlaying;
58 	uint32 _backgroundFileRef;
59 	uint8 _prevCDtrack;
60 
61 	uint16 _backgroundDelay;
62 
63 	// T7G iOS credits mp3 stream
64 	void playCreditsIOS();
65 	void stopCreditsIOS();
66 	Audio::SoundHandle _handleCreditsIOS;
67 
68 	// Volume fading
69 	uint32 _fadingStartTime;
70 	uint16 _fadingStartVolume;
71 	uint16 _fadingEndVolume;
72 	uint16 _fadingDuration;
73 	void applyFading();
74 
75 protected:
76 	GroovieEngine *_vm;
77 
78 	// Callback
79 	static void onTimer(void *data);
onTimerInternal()80 	virtual void onTimerInternal() {}
81 	Common::Mutex _mutex;
82 
83 	// User volume
84 	uint16 _userVolume;
85 	// Game volume
86 	uint16 _gameVolume;
87 
88 	// These are specific for each type of music
89 	virtual void updateVolume() = 0;
90 	virtual bool load(uint32 fileref, bool loop) = 0;
91 	virtual void unload();
92 };
93 
94 class MusicPlayerMidi : public MusicPlayer, public MidiDriver_BASE {
95 public:
96 	MusicPlayerMidi(GroovieEngine *vm);
97 	~MusicPlayerMidi();
98 
99 	// MidiDriver_BASE interface
100 	virtual void send(uint32 b);
101 	virtual void metaEvent(byte type, byte *data, uint16 length);
102 
103 private:
104 	// Channel volumes
105 	byte _chanVolumes[0x10];
106 	void updateChanVolume(byte channel);
107 
108 	void endTrack();
109 
110 protected:
111 	byte *_data;
112 	MidiParser *_midiParser;
113 	MidiDriver *_driver;
114 
115 	virtual void onTimerInternal();
116 	void updateVolume();
117 	void unload();
118 
119 	bool loadParser(Common::SeekableReadStream *stream, bool loop);
120 };
121 
122 class MusicPlayerXMI : public MusicPlayerMidi {
123 public:
124 	MusicPlayerXMI(GroovieEngine *vm, const Common::String &gtlName);
125 	~MusicPlayerXMI();
126 
127 	void send(uint32 b);
128 
129 protected:
130 	bool load(uint32 fileref, bool loop);
131 
132 private:
133 	// Channel banks
134 	byte _chanBanks[0x10];
135 
136 	// Output music type
137 	uint8 _musicType;
138 
139 	bool _milesAudioMode;
140 
141 	// Timbres
142 	class Timbre {
143 	public:
Timbre()144 		Timbre() : data(NULL), patch(0), bank(0), size(0) {}
145 		byte patch;
146 		byte bank;
147 		uint32 size;
148 		byte *data;
149 	};
150 	Common::Array<Timbre> _timbres;
151 	void loadTimbres(const Common::String &filename);
152 	void clearTimbres();
153 	void setTimbreAD(byte channel, const Timbre &timbre);
154 	void setTimbreMT(byte channel, const Timbre &timbre);
155 };
156 
157 class MusicPlayerMac_t7g : public MusicPlayerMidi {
158 public:
159 	MusicPlayerMac_t7g(GroovieEngine *vm);
160 
161 protected:
162 	bool load(uint32 fileref, bool loop);
163 
164 private:
165 	Common::SeekableReadStream *decompressMidi(Common::SeekableReadStream *stream);
166 };
167 
168 class MusicPlayerMac_v2 : public MusicPlayerMidi {
169 public:
170 	MusicPlayerMac_v2(GroovieEngine *vm);
171 
172 protected:
173 	bool load(uint32 fileref, bool loop);
174 };
175 
176 class MusicPlayerIOS : public MusicPlayer {
177 public:
178 	MusicPlayerIOS(GroovieEngine *vm);
179 	~MusicPlayerIOS();
180 
181 protected:
182 	void updateVolume();
183 	bool load(uint32 fileref, bool loop);
184 	void unload();
185 
186 private:
187 	Audio::SoundHandle _handle;
188 };
189 
190 } // End of Groovie namespace
191 
192 #endif // GROOVIE_MUSIC_H
193