aboutsummaryrefslogtreecommitdiff
path: root/sound/mixer.h
blob: 3ffb9ceb8193e892a08662dfdc057ea903414262 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/* ScummVM - Scumm Interpreter
 * Copyright (C) 2001  Ludvig Strigeus
 * Copyright (C) 2001/2002 The ScummVM project
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Header$
 *
 */

#ifndef MIXER_H
#define MIXER_H

#ifdef COMPRESSED_SOUND_FILE
#include <mad.h>
#endif

typedef uint32 PlayingSoundHandle;
class SoundMixer {
private:
	class Channel {
	public:
		bool _to_be_destroyed;
		virtual void mix(int16 *data, uint len) = 0;
		void destroy() {
			_to_be_destroyed = true;
		} virtual void real_destroy() = 0;
		virtual void append(void *sound, uint32 size);
#ifdef COMPRESSED_SOUND_FILE
		virtual bool sound_finished();
#endif
	};

	class Channel_RAW:public Channel {
		SoundMixer *_mixer;
		void *_ptr;
		uint32 _pos;
		uint32 _size;
		uint32 _fp_speed;
		uint32 _fp_pos;
		uint32 _realsize, _rate;
		byte _flags;


	public:
		void mix(int16 *data, uint len);
		  Channel_RAW(SoundMixer *mixer, void *sound, uint32 size, uint rate, byte flags);
		void real_destroy();
	};

	class Channel_STREAM:public Channel {
		SoundMixer *_mixer;
		byte *_ptr;
		byte *_end_of_data;
		byte *_pos;
		uint32 _fp_speed;
		uint32 _fp_pos;
		uint32 _buffer_size;
		uint32 _rate;
		byte _flags;

	public:
		void append(void *sound, uint32 size);
		void mix(int16 *data, uint len);
		  Channel_STREAM(SoundMixer *mixer, void *sound, uint32 size, uint rate, byte flags);
		void real_destroy();
	};

#ifdef COMPRESSED_SOUND_FILE

	class Channel_MP3:public Channel {
		SoundMixer *_mixer;
		void *_ptr;
		struct mad_stream _stream;
		struct mad_frame _frame;
		struct mad_synth _synth;
		uint32 _silence_cut;
		uint32 _pos_in_frame;
		uint32 _position;
		uint32 _size;
		byte _flags;

	public:
		void mix(int16 *data, uint len);
		  Channel_MP3(SoundMixer *mixer, void *sound, uint size, byte flags);
		void real_destroy();

	};

	class Channel_MP3_CDMUSIC:public Channel {
		SoundMixer *_mixer;
		void *_ptr;
		struct mad_stream _stream;
		struct mad_frame _frame;
		struct mad_synth _synth;
		uint32 _pos_in_frame;
		uint32 _size;
		uint32 _buffer_size;
		mad_timer_t _duration;
		FILE *_file;
		bool _initialized;
	public:
		void mix(int16 *data, uint len);
		  Channel_MP3_CDMUSIC(SoundMixer *mixer, FILE * file, mad_timer_t duration);
		void real_destroy();
		bool sound_finished();
	};

#endif

	static void on_generate_samples(void *s, byte *samples, int len);

public:
	typedef void PremixProc (void *param, int16 *data, uint len);

	OSystem *_syst;
	void *_mutex;

	uint _output_rate;

	int16 *_volume_table;
	int _music_volume;

	bool _paused;

	enum {
		NUM_CHANNELS = 16,
	};

	void *_premix_param;
	PremixProc *_premix_proc;

	Channel *_channels[NUM_CHANNELS];
	PlayingSoundHandle *_handles[NUM_CHANNELS];

	int insert_at(PlayingSoundHandle *handle, int index, Channel * chan);
	void append(void *data, uint32 len);
	void uninsert(Channel * chan);

	/* start playing a raw sound */
	enum {
		/* Do *NOT* change any of these flags without looking at the code in mixer.cpp */
		FLAG_UNSIGNED = 1,					/* unsigned samples */
		FLAG_STEREO = 2,						/* sound is in stereo */
		FLAG_16BITS = 4,						/* sound is 16 bits wide */
		FLAG_AUTOFREE = 8,					/* sound buffer is freed automagically at the end of playing */
		FLAG_FILE = 16,							/* sound is a FILE * that's read from */
	};
	int play_raw(PlayingSoundHandle *handle, void *sound, uint32 size, uint rate, byte flags);
	int play_stream(PlayingSoundHandle *handle, int index, void *sound, uint32 size, uint rate,
									byte flags);
#ifdef COMPRESSED_SOUND_FILE
	int play_mp3(PlayingSoundHandle *handle, void *sound, uint32 size, byte flags);
	int play_mp3_cdtrack(PlayingSoundHandle *handle, FILE * file, mad_timer_t duration);
#endif

	/* Premix procedure, useful when using fmopl adlib */
	void setup_premix(void *param, PremixProc *proc);

	/* mix */
	void mix(int16 *buf, uint len);

	/* stop all currently playing sounds */
	void stop_all();

	/* stop playing a specific sound */
	void stop(PlayingSoundHandle psh);
	void stop(int index);

	/* append to existing sound */
	int append(int index, void *sound, uint32 size, uint rate, byte flags);

	/* is any channel active? */
	bool has_active_channel();

	/* bind to the OSystem object => mixer will be
	 * invoked automatically when samples need
	 * to be generated */
	bool bind_to_system(OSystem *syst);

	/* set the volume, 0-256 */
	void set_volume(int volume);
	void set_music_volume(int volume);

	/* pause - unpause */
	void pause(bool paused);

};

struct MP3OffsetTable {					/* Compressed Sound (.SO3) */
	int org_offset;
	int new_offset;
	int num_tags;
	int compressed_size;
};

#endif