aboutsummaryrefslogtreecommitdiff
path: root/queen/sound.cpp
blob: 4cd38fa26f40bf4d6cace5f15cfc599b79ae813f (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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
/* ScummVM - Scumm Interpreter
 * Copyright (C) 2003 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 "common/file.h"
#include "common/util.h"
#include "queen/input.h"
#include "queen/resource.h"
#include "queen/sound.h"

#define	SB_HEADER_SIZE	110
#define	STOP_MUSIC	-1

namespace Queen {

#ifdef USE_VORBIS
// These are wrapper functions to allow using a File object to
// provide data to the OggVorbis_File object.

struct file_info {
	File *file;
	int start, curr_pos;
	size_t len;
};

static size_t read_wrap(void *ptr, size_t size, size_t nmemb, void *datasource) {
	file_info *f = (file_info *) datasource;
	int result;

	nmemb *= size;
	if (f->curr_pos > (int) f->len)
		nmemb = 0;
	else if (nmemb > f->len - f->curr_pos)
		nmemb = f->len - f->curr_pos;
	result = f->file->read(ptr, nmemb);
	if (result == -1) {
		f->curr_pos = f->file->pos() - f->start;
		return (size_t) -1;
	} else {
		f->curr_pos += result;
		return result / size;
	}
}

static int seek_wrap(void *datasource, ogg_int64_t offset, int whence) {
	file_info *f = (file_info *) datasource;

	if (whence == SEEK_SET)
		offset += f->start;
	else if (whence == SEEK_END) {
		offset += f->start + f->len;
		whence = SEEK_SET;
	}

	f->file->seek(offset, whence);
	f->curr_pos = f->file->pos() - f->start;
	return f->curr_pos;
}

static int close_wrap(void *datasource) {
	file_info *f = (file_info *) datasource;

	f->file->close();
	delete f;
	return 0;
}

static long tell_wrap(void *datasource) {
	file_info *f = (file_info *) datasource;

	return f->curr_pos;
}

static ov_callbacks g_File_wrap = {
	read_wrap, seek_wrap, close_wrap, tell_wrap
};
#endif

Sound::Sound(SoundMixer *mixer, Input *input, Resource  *resource) : 
  _mixer(mixer), _input(input), _resource(resource), _lastOverride(0), _currentSong(0), _sfxHandle(0) {
}

Sound::~Sound() {
}

Sound *Sound::giveSound(SoundMixer *mixer, Input *input, Resource *resource, uint8 compression) {
	switch(compression) {
		case COMPRESSION_NONE:
				return new SBSound(mixer, input, resource);
				break;
		case COMPRESSION_MP3:
				#ifndef USE_MAD
					warning("Using MP3 compressed datafile, but MP3 support not compiled in");
					return new SilentSound(mixer, input, resource);
				#else
					return new MP3Sound(mixer, input, resource);

				#endif
				break;
		case COMPRESSION_OGG:
				#ifndef USE_VORBIS
					warning("Using OGG compressed datafile, but OGG support not compiled in");
					return new SilentSound(mixer, input, resource);
				#else
					return new OGGSound(mixer, input, resource);
				#endif
				break;
		default:
				warning("Unknown compression type");
				return new SilentSound(mixer, input, resource);
	}
}

void Sound::waitSfxFinished() {
	while(_sfxHandle != 0)
		_input->delay(10);
}

void Sound::playSong(int16 songNum) {
	if (songNum == STOP_MUSIC) {
		//stop the music
		//_fanfareCount = _fluteCount = 0;
		//_oldName[0] = 0;
		return;
	}

	// Remap some songs because there is less amiga music
	// and we want lastoverride to work properly
	switch(songNum) {
		case 1:
		case 2:
			songNum = 39;
			break;

		case 37:
		case 52:
		case 196:
			songNum = 90;
			break;

		case 38:
		case 89:
			songNum = 3;
			break;

		case 24:
		case 158:
			songNum = 117;
			break;

		case 71:
		case 72:
		case 73:
		case 75:
			songNum = 133;
			break;

		case 203:
			songNum = 67;
			break;

		case 145:
			songNum = 140;
			break;

		case 53:
		case 204:
			songNum = 44;
			break;

		case 136:
		case 142:
		case 179:
			songNum = 86;
			break;

		case 101:
		case 102:
		case 143:
			songNum = 188;
			break;

		case 65:
		case 62:
			songNum = 69;
			break;

		case 118:
		case 119:
			songNum = 137;
			break;

		case 130:
		case 131:
			songNum = 59;
			break;

		case 174:
		case 175:
			songNum = 57;
			break;

		case 171:
		case 121:
			songNum = 137;
			break;

		case 138:
		case 170:
		case 149:
			songNum = 28;
			break;

		case 122:
		case 180:
		case 83:
		case 98:
			songNum = 83;
			break;

		case 20:
		case 33:
			songNum = 34;
			break;

		case 29:
		case 35:
			songNum = 36;
			break;

		case 7:
		case 9:
		case 10:
			songNum = 11;
			break;

		case 110:
			songNum = 94;
			break;

		case 111:
			songNum = 95;
			break;

		case 30:
			songNum = 43;
			break;

		case 76:
			songNum = 27;
			break;

		case 194:
		case 195:
			songNum = 32;
			break;
	}

	// Sound effects and volume changes exit before setting lastoverride
	// 04-19-95 01:31pm Fix sfx bug
	if(_lastOverride != 32 && _lastOverride != 44) {
		switch(songNum) {
			// Volume normal
			case 5:
				warning("Sound::playSong() -> alter_current_volume() not implemented yet");
    				//alter_current_volume();
    				return;
	
			// Volume soft
			case 15:
    				//volumecontrol(-1,(20*VOLUME)/100);
    				return;
		
			// Volume medium
			case 14:
    				//volumecontrol(-1,(32*VOLUME)/100);
    				return;

			// Open door
			case 25:
				sfxPlay("116BSSSS");
				return;

			// Close door
			case 26:
    				sfxPlay("105ASSSS");
				return;

			// Light switch
			case 56:
				sfxPlay("27SSSSSS");
				return;

			// Hydraulic doors open
			case 57:
				sfxPlay("96SSSSSS");
				return;

			// Hydraulic doors close
			case 58:
				sfxPlay("97SSSSSS");
				return;

			// Metallic door slams
			case 59:
				sfxPlay("105SSSSS");
				return;

			// Oracle rezzes in
			case 63:
				sfxPlay("132SSSSS");
				return;

			// Cloth slide 1
			case 27:
				sfxPlay("135SSSSS");
				return;
	
			// Splash
			case 83:
				sfxPlay("18SSSSSS");
				return;

			// Agression enhancer
			case 85:
				sfxPlay("138BSSSS");
				return;
		
			// Dino Ray
			case 68:
				sfxPlay("138SSSSS");
				return;

			// Dino transformation
			case 140:
				sfxPlay("55BSSSSS");
				return;

			// Experimental laser
			case 141:
				sfxPlay("55SSSSSS");
				return;

			// Plane hatch open
			case 94:
				sfxPlay("3SSSSSSS");
				return;

			// Plane hatch close
			case 95:
				sfxPlay("4SSSSSSS");
				return;

			// Oracle rezzes out
			case 117:
				sfxPlay("70SSSSSS");
				return;

			// Dino horn
			case 124:
				sfxPlay("103SSSSS");
				return;

			// Punch
			case 127:
				sfxPlay("128SSSSS");
				return;

			// Body hits ground
			case 128:
				sfxPlay("129SSSSS");
				return;

			// Explosion
			case 137:
				sfxPlay("88SSSSSS");
				return;
	
			// Stone door grind 1
			case 86:
				sfxPlay("1001SSSS");
				return;

			// Stone door grind 2
			case 188:
				sfxPlay("1002SSSS");
				return;

			// Cloth slide 2
			case 28:
				sfxPlay("1005SSSS");
				return;

			// Rattle bars
			case 151:
				sfxPlay("115SSSSS");
				return;

			// Door dissolves
			case 152:
				sfxPlay("56SSSSSS");
				return;

			// Altar slides
			case 153:
				sfxPlay("85SSSSSS");
				return;

			// Pull lever
			case 166:
				sfxPlay("1008SSSS");
				return;

			// Zap Frank
			case 182:
				sfxPlay("1023SSSS");
				return;

			// Splorch
			case 69:
				sfxPlay("137ASSSS");
				return;

			// Robot laser
			case 70:
				sfxPlay("61SSSSSS");
				return;

			// Pick hits stone
			case 133:
				sfxPlay("71SSSSSS");
				return;

			// Press button
			case 165:
				sfxPlay("1007SSSS");
				return;
		}
	}


	// Exit if trying to play a song that's already playing
	// and instruments are already loaded i.e. last song wasn't -1
	if (songNum == _lastOverride /* && oldName[0] != 0 */ )

	// Play songs
	switch(songNum) {
		// Hotel
		case 39:
			//goSong("hotel",1);
			break;

		case 19:
			//goSong("hotel",3);
			break;

		case 34:
			//goSong("hotel",2);
			break;

		case 36:
			//goSong("hotel",4);
			//fanfarerestore=lastoverride;
			//fanfarecount=60;
			break;

		// Jungle
		case 40:
			//goSong("jung",1);
			//fanfarerestore=lastoverride;
			//fanfarecount=80;
			//flutecount=100;
			break;

		case 3:
			//goSong("jung",2);
			//flutecount=100;
			break;

		// Temple
		case 54:
			//goSong("temple",1);
			break;

		case 12:
			//goSong("temple",2);
			break;

		case 11:
			//goSong("temple",3);
			break;

		case 31:
			//goSong("temple",4);
			//fanfarerestore=lastoverride;
			//fanfarecount=80;
			break;

		// Floda
		case 41:
			//goSong("floda",4);
			//fanfarerestore=lastoverride;
			//fanfarecount=60;
			break;

		case 13:
			//goSong("floda",3);
			break;

		case 16:
			//goSong("floda",1);
			break;
			
		case 17:
			//goSong("floda",2);
			break;

		case 43:
			//goSong("floda",5);
			break;

		// Prequel (End credits)
		case 67:
			//goSong("title",1);
			// 05-19-95 03:24pm Removed to fix A500 Bug
			//fanfarerestore=90;
			//fanfarecount=550;
			break;

			// Title (Intro credits)
		case 88:
			//goSong("title",1);
			break;

		// Valley
		case 90:
			//goSong("awestruk",1);
			break;

		// Confrontation
		case 91:
			//goSong("'jungle'",1);
			break;

		// Frank
		case 46:
			//goSong("frank",1);
			break;

		// Trader Bob
		case 6:
			//goSong("bob",1);
			break;

		// Azura
		case 44:
			//goSong("azura",1);
			break;

		// Amazon Fortress
		case 21:
			//goSong("fort",1);
			break;

		// Rocket
		case 32:
			//goSong("rocket",1);
			break;

		// Robot
		case 92:
			//goSong("robot",1);
			break;

		// Song not handled by amiga
		default:
			return;
	}


	_lastOverride = _currentSong = songNum;

}


int SBSound::playSound(byte *sound, uint32 size) {
	byte flags = 0 | SoundMixer::FLAG_UNSIGNED | SoundMixer::FLAG_AUTOFREE;
	return _mixer->playRaw(&_sfxHandle, sound, size, 11025, flags);
}

void SBSound::sfxPlay(const char *base) {
	char name[13];
	strcpy(name, base);
	//alter filename to add zeros and append ".SB"
	for (int i = 0; i < 8; i++) {
		if (name[i] == ' ')
			name[i] = '0';
	}
	strcat(name, ".SB");

	waitSfxFinished();
	
	if (_resource->exists(name)) 
		playSound(_resource->loadFileMalloc(name, SB_HEADER_SIZE), _resource->fileSize(name) - SB_HEADER_SIZE);
}

#ifdef USE_MAD
void MP3Sound::sfxPlay(const char *base) {
	char name[13];
	strcpy(name, base);
	//alter filename to add zeros and append ".SB"
	for (int i = 0; i < 8; i++) {
		if (name[i] == ' ')
			name[i] = '0';
	}
	strcat(name, ".SB");
	
	waitSfxFinished();

	if (_resource->exists(name)) 
		_mixer->playMP3(&_sfxHandle, _resource->giveCompressedSound(name), _resource->fileSize(name));
}
#endif

#ifdef USE_VORBIS
void OGGSound::sfxPlay(const char *base) {
	char name[13];
	strcpy(name, base);
	//alter filename to add zeros and append ".SB"
	for (int i = 0; i < 8; i++) {
		if (name[i] == ' ')
			name[i] = '0';
	}
	strcat(name, ".SB");

	waitSfxFinished();
	
	if (_resource->exists(name)) {
		OggVorbis_File *oggFile = new OggVorbis_File;
		file_info *f = new file_info;

		f->file = _resource->giveCompressedSound(name);
		f->start = _resource->fileOffset(name);
		f->len = _resource->fileSize(name);
		f->curr_pos = 0;

		if (ov_open_callbacks((void *)f, oggFile, NULL, 0, g_File_wrap) < 0) {
			delete oggFile;
			delete f;
		} else
			_mixer->playVorbis(&_sfxHandle, oggFile, 0, false);
	}
}
#endif

} //End of namespace Queen