aboutsummaryrefslogtreecommitdiff
path: root/mp3_cd.cpp
blob: 82d54d30b76abaac4f4087ae7a094ab76d16d136 (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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/* ScummVM - Scumm Interpreter
 * Copyright (C) 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$
 */

#include "stdafx.h"
#include "scumm.h"

#ifdef COMPRESSED_SOUND_FILE__
#include "mp3_cd.h"
#include <SDL.h>

int _current_mp3_cd_track;
struct xing _vbr_header;
struct mad_header _mad_header;
long _mp3_size;
FILE *_mp3_track;
MixerChannel *_mc;

// From xing.c in MAD

int xing_parse(struct xing *xing, struct mad_bitptr ptr, unsigned int bitlen)
{

	xing->flags = 0;

	if (bitlen < 64 || mad_bit_read(&ptr, 32) != XING_MAGIC)
		goto fail;

	xing->flags = mad_bit_read(&ptr, 32);
	bitlen -= 64;

	if (xing->flags & XING_FRAMES) {
		if (bitlen < 32)
			goto fail;

		xing->frames = mad_bit_read(&ptr, 32);
		bitlen -= 32;
	}

	if (xing->flags & XING_BYTES) {
		if (bitlen < 32)
			goto fail;

		xing->bytes = mad_bit_read(&ptr, 32);
		bitlen -= 32;
	}

	if (xing->flags & XING_TOC) {
		int i;

		if (bitlen < 800)
			goto fail;

		for (i = 0; i < 100; ++i)
			xing->toc[i] = (unsigned char)mad_bit_read(&ptr, 8);

		bitlen -= 800;
	}

	if (xing->flags & XING_SCALE) {
		if (bitlen < 32)
			goto fail;

		xing->scale = mad_bit_read(&ptr, 32);
		bitlen -= 32;
	}

	return 0;

fail:
	xing->flags = 0;
	return -1;
}

// Borrowed from Winamp plugin in_mad.c

bool parse_xing_vbr_tag()
{
	struct mad_stream stream;
	struct mad_frame frame;
	unsigned char buffer[8192];
	unsigned int buflen = 0;
	int count = 0, result = 0;

	_vbr_header.flags = 0;

	mad_stream_init(&stream);
	mad_frame_init(&frame);

	fseek(_mp3_track, 0, SEEK_SET);

	while (1) {
		if (buflen < sizeof(buffer)) {
			uint16 bytes;

			bytes = fread(buffer + buflen, 1, sizeof(buffer) - buflen, _mp3_track);
			if (bytes <= 0) {
				if (bytes == -1)
					result = -1;
				break;
			}

			buflen += bytes;
		}

		mad_stream_buffer(&stream, buffer, buflen);

		while (1) {
			if (mad_frame_decode(&frame, &stream) == -1) {
				if (!MAD_RECOVERABLE(stream.error))
					break;

				if (stream.error != MAD_ERROR_BADCRC)
					continue;
			}

			if (count++ ||
					xing_parse(&_vbr_header, stream.anc_ptr, stream.anc_bitlen)
					== -1)
				break;
		}

		if (count || stream.error != MAD_ERROR_BUFLEN)
			break;

		memmove(buffer, stream.next_frame,
						buflen = &buffer[buflen] - stream.next_frame);
	}

	if (count)
		memcpy(&_mad_header, &frame.header, sizeof(mad_header));
	else
		result = -1;

	mad_frame_finish(&frame);
	mad_stream_finish(&stream);

	return (result != -1);
}

uint32 calc_cd_file_offset(int start_frame)
{
	long offset;

	if (!_vbr_header.flags) {
		float frame_size;
		//mad_timer_t timer;  - recode with timer

		/* Constant bit rate - perhaps not fully accurate */
		frame_size = 144 * _mad_header.bitrate / _mad_header.samplerate;
		offset = (long)((float)start_frame / (float)CD_FPS * 1000 /
										(float)((float)1152 / (float)_mad_header.samplerate *
														1000) * (float)(frame_size + 0.5));
	} else {
		/* DOES NOT WORK AT THE MOMENT */
		/* see Xing SDK */
		long a;
		float fa, fb, fx;
		float percent = (float)start_frame / (float)CD_FPS * 1000 /
			((float)((float)1152 / (float)_mad_header.samplerate * 1000) *
			 _vbr_header.frames) * 100;

		if (percent < 0.0f)
			percent = 0.0f;
		if (percent > 100.0f)
			percent = 100.0f;

		a = (int)percent;
		if (a > 99)
			a = 99;
		fa = _vbr_header.toc[a];
		if (a < 99) {
			fb = _vbr_header.toc[a + 1];
		} else {
			fb = 256.0f;
		}

		fx = fa + (fb - fa) * (percent - a);

		offset = (int)((1.0f / 256.0f) * fx * _vbr_header.bytes);

	}

	return offset;
}

bool mp3_cd_play(Scumm *s, int track, int num_loops, int start_frame,
								 int end_frame)
{

	// See if we are already playing this track, else try to open it

	if (_current_mp3_cd_track != track) {
		char track_name[1024];

		sprintf(track_name, "%strack%d.mp3", s->_gameDataPath, track);
		_mp3_track = fopen(track_name, "rb");
		if (!_mp3_track) {
			warning("No CD and track %d not available in mp3 format", track);
			return false;
		}

		if (!parse_xing_vbr_tag()) {
			warning("Error parsing file header - ignoring file", track);
			fclose(_mp3_track);
			return false;
		}

		if (_vbr_header.flags) {
			if (!((_vbr_header.flags & XING_TOC) &&
						(_vbr_header.flags & XING_BYTES) &&
						(_vbr_header.flags & XING_FRAMES)
					)) {
				warning("Missing required part of VBR header - ignoring file");
				fclose(_mp3_track);
				_vbr_header.flags = 0;
				return false;
			}
		}
		// Allocate the music mixer if necessary

		if (!_mc) {
			_mc = s->allocateMixer();
			if (!_mc) {
				warning("No mixer channel available for MP3 music");
				return false;
			}
		}

		fseek(_mp3_track, 0, SEEK_END);
		_mp3_size = ftell(_mp3_track);
		fseek(_mp3_track, 0, SEEK_SET);

		_mc->type = MIXER_MP3_CDMUSIC;
		_mc->sound_data.mp3_cdmusic.file = _mp3_track;
		_mc->sound_data.mp3_cdmusic.playing = false;
		_mc->sound_data.mp3_cdmusic.buffer_size = 200000;
		_mc->_sfx_sound = malloc(_mc->sound_data.mp3_cdmusic.buffer_size);

		/* see if it's enough */
		mad_stream_init(&_mc->sound_data.mp3.stream);
		if (_mad_header.samplerate == 44100)
			mad_stream_options((mad_stream *) & _mc->sound_data.mp3.stream,
												 MAD_OPTION_HALFSAMPLERATE);

		mad_frame_init(&_mc->sound_data.mp3.frame);
		mad_synth_init(&_mc->sound_data.mp3.synth);

		_current_mp3_cd_track = track;
	}


	if (_current_mp3_cd_track == track) {

		uint32 where;

		// See where we want to go
		where = calc_cd_file_offset(start_frame);

		if (start_frame < 0 || end_frame < 0) {
			warning("Negative index in frame");
			return false;
		}

		mad_timer_set(&_mc->sound_data.mp3_cdmusic.duration,
									0, end_frame, CD_FPS);

		fseek(_mp3_track, where, SEEK_SET);
		_mc->sound_data.mp3_cdmusic.playing = true;

		return true;
	}

	return false;
}

#endif