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
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
|
/* 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$
*
*/
#include "stdafx.h"
#include "scumm.h"
void SoundMixer::uninsert(Channel * chan)
{
for (int i = 0; i != NUM_CHANNELS; i++) {
if (_channels[i] == chan) {
if (_handles[i]) {
*_handles[i] = 0;
_handles[i] = NULL;
}
_channels[i] = NULL;
return;
}
}
error("SoundMixer::channel_deleted chan not found");
}
int SoundMixer::append(int index, void *sound, uint32 size, uint rate, byte flags)
{
_syst->lock_mutex(_mutex);
Channel *chan = _channels[index];
if (!chan) {
warning("Trying to stream to an unexistant streamer ");
play_stream(NULL, index, sound, size, rate, flags);
chan = _channels[index];
} else {
chan->append(sound, size);
}
_syst->unlock_mutex(_mutex);
return 1;
}
int SoundMixer::insert_at(PlayingSoundHandle *handle, int index, Channel * chan)
{
if (_channels[index] != NULL) {
error("Trying to put a mixer where it cannot go ");
}
_channels[index] = chan;
_handles[index] = handle;
if (handle)
*handle = index + 1;
return index;
}
int SoundMixer::insert(PlayingSoundHandle *handle, Channel * chan)
{
for (int i = 0; i != NUM_CHANNELS; i++) {
if (_channels[i] == NULL) {
return insert_at(handle, i, chan);
}
}
warning("SoundMixer::insert out of mixer slots");
chan->real_destroy();
return -1;
}
int SoundMixer::play_raw(PlayingSoundHandle *handle, void *sound, uint32 size, uint rate,
byte flags)
{
return insert(handle, new Channel_RAW(this, sound, size, rate, flags));
}
int SoundMixer::play_stream(PlayingSoundHandle *handle, int idx, void *sound, uint32 size,
uint rate, byte flags)
{
return insert_at(handle, idx, new Channel_STREAM(this, sound, size, rate, flags));
}
#ifdef COMPRESSED_SOUND_FILE
int SoundMixer::play_mp3(PlayingSoundHandle *handle, void *sound, uint32 size, byte flags)
{
return insert(handle, new Channel_MP3(this, sound, size, flags));
}
int SoundMixer::play_mp3_cdtrack(PlayingSoundHandle *handle, FILE * file, mad_timer_t duration)
{
/* Stop the previously playing CD track (if any) */
return insert(handle, new Channel_MP3_CDMUSIC(this, file, duration));
}
#endif
void SoundMixer::mix(int16 *buf, uint len)
{
if (_premix_proc) {
int i;
_premix_proc(_premix_param, buf, len);
for (i = (len - 1); i >= 0; i--) {
buf[2 * i] = buf[2 * i + 1] = buf[i];
}
} else {
/* no premixer available, zero the buf out */
memset(buf, 0, 2 * len * sizeof(int16));
}
/* Arisme : moved to let iMUSE generate the events */
if (_paused) {
memset(buf, 0, 2 * len * sizeof(int16));
return;
}
_syst->lock_mutex(_mutex);
/* now mix all channels */
for (int i = 0; i != NUM_CHANNELS; i++)
if (_channels[i])
_channels[i]->mix(buf, len);
_syst->unlock_mutex(_mutex);
}
void SoundMixer::on_generate_samples(void *s, byte *samples, int len)
{
((SoundMixer *)s)->mix((int16 *)samples, len >> 2);
}
bool SoundMixer::bind_to_system(OSystem *syst)
{
_volume_table = (int16 *)calloc(256 * sizeof(int16), 1);
uint rate = (uint) syst->property(OSystem::PROP_GET_SAMPLE_RATE, 0);
_output_rate = rate;
_syst = syst;
_mutex = _syst->create_mutex();
if (rate == 0)
error("OSystem returned invalid sample rate");
return syst->set_sound_proc(this, on_generate_samples, OSystem::SOUND_16BIT);
}
void SoundMixer::stop_all()
{
for (int i = 0; i != NUM_CHANNELS; i++)
if (_channels[i])
_channels[i]->destroy();
}
void SoundMixer::stop(PlayingSoundHandle psh)
{
if (psh && _channels[psh - 1])
_channels[psh - 1]->destroy();
}
void SoundMixer::stop(int index)
{
if (_channels[index])
_channels[index]->destroy();
}
void SoundMixer::pause(bool paused)
{
_paused = paused;
}
bool SoundMixer::has_active_channel()
{
for (int i = 0; i != NUM_CHANNELS; i++)
if (_channels[i])
return true;
return false;
}
void SoundMixer::setup_premix(void *param, PremixProc *proc)
{
_premix_param = param;
_premix_proc = proc;
}
void SoundMixer::set_volume(int volume)
{
for (int i = 0; i != 256; i++)
_volume_table[i] = ((int8)i) * volume;
}
void SoundMixer::set_music_volume(int volume)
{
_music_volume = volume;
}
#ifdef COMPRESSED_SOUND_FILE
bool SoundMixer::Channel::sound_finished()
{
warning("sound_finished should never be called on a non-MP3 mixer ");
return false;
}
#endif
void SoundMixer::Channel::append(void *sound, uint32 size)
{
error("append method should never be called on something else than a _STREAM mixer ");
}
/* RAW mixer */
SoundMixer::Channel_RAW::Channel_RAW(SoundMixer *mixer, void *sound, uint32 size, uint rate,
byte flags)
{
_mixer = mixer;
_flags = flags;
_ptr = sound;
_pos = 0;
_fp_pos = 0;
_fp_speed = (1 << 16) * rate / mixer->_output_rate;
_to_be_destroyed = false;
_realsize = size;
/* adjust the magnitute to prevent division error */
while (size & 0xFFFF0000)
size >>= 1, rate = (rate >> 1) + 1;
_rate = rate;
_size = size * mixer->_output_rate / rate;
if (_flags & FLAG_16BITS)
_size = _size >> 1;
if (_flags & FLAG_STEREO)
_size = _size >> 1;
}
static int16 *mix_signed_mono_8(int16 *data, uint * len_ptr, byte **s_ptr, uint32 *fp_pos_ptr,
int fp_speed, const int16 *vol_tab, byte *s_end)
{
uint32 fp_pos = *fp_pos_ptr;
byte *s = *s_ptr;
uint len = *len_ptr;
do {
fp_pos += fp_speed;
*data++ += vol_tab[*s];
*data++ += vol_tab[*s];
s += fp_pos >> 16;
fp_pos &= 0x0000FFFF;
} while ((--len) && (s < s_end));
*fp_pos_ptr = fp_pos;
*s_ptr = s;
*len_ptr = len;
return data;
}
static int16 *mix_unsigned_mono_8(int16 *data, uint * len_ptr, byte **s_ptr, uint32 *fp_pos_ptr,
int fp_speed, const int16 *vol_tab, byte *s_end)
{
#if OLD
uint32 fp_pos = *fp_pos_ptr;
byte *s = *s_ptr;
uint len = *len_ptr;
do {
fp_pos += fp_speed;
*data++ += vol_tab[*s ^ 0x80];
*data++ += vol_tab[*s ^ 0x80];
s += fp_pos >> 16;
fp_pos &= 0x0000FFFF;
} while ((--len) && (s < s_end));
*fp_pos_ptr = fp_pos;
*s_ptr = s;
*len_ptr = len;
return data;
#else
uint32 fp_pos = *fp_pos_ptr;
byte *s = *s_ptr;
uint len = *len_ptr;
int x0, x1, x2, x3;
int a, b, c, d;
int inc = 1, result, t;
x0 = x1 = vol_tab[*s ^ 0x80];
x2 = vol_tab[*(s+1) ^ 0x80];
x3 = vol_tab[*(s+2) ^ 0x80];
do {
a = ((-x0*2)+(x1*5)-(x2*4)+x3);
b = ((x0+x2-(2*x1))*6) << 8;
c = ((-4*x0)+x1+(x2*4)-x3) << 8;
d = (x1*6) << 8;
do {
t = fp_pos >> 8;
result = (a*t + b) >> 8;
result = (result * t + c) >> 8;
result = (result * t + d) >> 8;
result = (result/3 + 1) >> 1;
*data++ += result;
*data++ += result;
fp_pos += fp_speed;
inc = fp_pos >> 16;
s += inc;
fp_pos &= 0x0000FFFF;
} while ((--len) && !inc && (s < s_end));
x0 = x1;
x1 = x2;
x2 = x3;
if (s+2 < s_end)
x3 = vol_tab[*(s+2) ^ 0x80];
} while (len && (s < s_end));
*fp_pos_ptr = fp_pos;
*s_ptr = s;
*len_ptr = len;
return data;
#endif
}
static int16 *mix_signed_stereo_8(int16 *data, uint * len_ptr, byte **s_ptr, uint32 *fp_pos_ptr,
int fp_speed, const int16 *vol_tab, byte *s_end)
{
warning("Mixing stereo signed 8 bit is not supported yet ");
return data;
}
static int16 *mix_unsigned_stereo_8(int16 *data, uint * len_ptr, byte **s_ptr, uint32 *fp_pos_ptr,
int fp_speed, const int16 *vol_tab, byte *s_end)
{
uint32 fp_pos = *fp_pos_ptr;
byte *s = *s_ptr;
uint len = *len_ptr;
do {
fp_pos += fp_speed;
*data++ += vol_tab[*s ^ 0x80];
*data++ += vol_tab[*(s + 1) ^ 0x80];
s += (fp_pos >> 16) << 1;
fp_pos &= 0x0000FFFF;
} while ((--len) && (s < s_end));
*fp_pos_ptr = fp_pos;
*s_ptr = s;
*len_ptr = len;
return data;
}
static int16 *mix_signed_mono_16(int16 *data, uint * len_ptr, byte **s_ptr, uint32 *fp_pos_ptr,
int fp_speed, const int16 *vol_tab, byte *s_end)
{
uint32 fp_pos = *fp_pos_ptr;
unsigned char volume = ((int)vol_tab[1]) * 32 / 255;
byte *s = *s_ptr;
uint len = *len_ptr;
do {
int16 sample = (((int16)(*s << 8) | *(s + 1)) * volume) / 32;
fp_pos += fp_speed;
*data++ += sample;
*data++ += sample;
s += (fp_pos >> 16) << 1;
fp_pos &= 0x0000FFFF;
} while ((--len) && (s < s_end));
*fp_pos_ptr = fp_pos;
*s_ptr = s;
*len_ptr = len;
return data;
}
static int16 *mix_unsigned_mono_16(int16 *data, uint * len_ptr, byte **s_ptr, uint32 *fp_pos_ptr,
int fp_speed, const int16 *vol_tab, byte *s_end)
{
warning("Mixing mono unsigned 16 bit is not supported yet ");
return data;
}
static int16 *mix_signed_stereo_16(int16 *data, uint * len_ptr, byte **s_ptr, uint32 *fp_pos_ptr,
int fp_speed, const int16 *vol_tab, byte *s_end)
{
uint32 fp_pos = *fp_pos_ptr;
unsigned char volume = ((int)vol_tab[1]) * 32 / 255;
byte *s = *s_ptr;
uint len = *len_ptr;
do {
fp_pos += fp_speed;
*data++ += (((int16)(*(s) << 8) | *(s + 1)) * volume) / 32;
*data++ += (((int16)(*(s + 2) << 8) | *(s + 3)) * volume) / 32;
s += (fp_pos >> 16) << 2;
fp_pos &= 0x0000FFFF;
} while ((--len) && (s < s_end));
*fp_pos_ptr = fp_pos;
*s_ptr = s;
*len_ptr = len;
return data;
}
static int16 *mix_unsigned_stereo_16(int16 *data, uint * len_ptr, byte **s_ptr, uint32 *fp_pos_ptr,
int fp_speed, const int16 *vol_tab, byte *s_end)
{
warning("Mixing stereo unsigned 16 bit is not supported yet ");
return data;
}
static int16 *(*mixer_helper_table[16]) (int16 *data, uint * len_ptr, byte **s_ptr,
uint32 *fp_pos_ptr, int fp_speed, const int16 *vol_tab,
byte *s_end) = {
mix_signed_mono_8, mix_unsigned_mono_8, mix_signed_stereo_8, mix_unsigned_stereo_8,
mix_signed_mono_16, mix_unsigned_mono_16, mix_signed_stereo_16, mix_unsigned_stereo_16};
void SoundMixer::Channel_RAW::mix(int16 *data, uint len)
{
byte *s, *s_org = NULL;
uint32 fp_pos;
byte *end;
if (_to_be_destroyed) {
real_destroy();
return;
}
if (len > _size)
len = _size;
_size -= len;
/*
* simple support for fread() reading of samples
*/
if (_flags & FLAG_FILE) {
/* determine how many samples to read from the file */
uint num = len * _fp_speed >> 16;
s_org = (byte *)malloc(num);
if (s_org == NULL)
error("Channel_RAW::mix out of memory");
uint num_read = fread(s_org, 1, num, (FILE *) _ptr);
if (num - num_read != 0)
memset(s_org + num_read, 0x80, num - num_read);
s = s_org;
fp_pos = 0;
end = s_org + num;
} else {
s = (byte *)_ptr + _pos;
fp_pos = _fp_pos;
end = (byte *)_ptr + _realsize;
}
const uint32 fp_speed = _fp_speed;
const int16 *vol_tab = _mixer->_volume_table;
mixer_helper_table[_flags & 0x07] (data, &len, &s, &fp_pos, fp_speed, vol_tab, end);
_pos = s - (byte *)_ptr;
_fp_pos = fp_pos;
if (_flags & FLAG_FILE) {
free(s_org);
}
if (_size < 1)
real_destroy();
}
void SoundMixer::Channel_RAW::real_destroy()
{
if (_flags & FLAG_AUTOFREE)
free(_ptr);
_mixer->uninsert(this);
delete this;
}
/* STREAM mixer */
SoundMixer::Channel_STREAM::Channel_STREAM(SoundMixer *mixer, void *sound, uint32 size, uint rate,
byte flags)
{
_mixer = mixer;
_flags = flags;
_buffer_size = 1024 * size;
_ptr = (byte *)malloc(_buffer_size);
memcpy(_ptr, sound, size);
_end_of_data = _ptr + size;
if (_flags & FLAG_AUTOFREE)
free(sound);
_pos = _ptr;
_fp_pos = 0;
_fp_speed = (1 << 16) * rate / mixer->_output_rate;
_to_be_destroyed = false;
/* adjust the magnitute to prevent division error */
while (size & 0xFFFF0000)
size >>= 1, rate = (rate >> 1) + 1;
_rate = rate;
}
void SoundMixer::Channel_STREAM::append(void *data, uint32 len)
{
byte *new_end = _end_of_data + len;
byte *cur_pos = _pos; /* This is just to prevent the variable to move during the tests :-) */
if (new_end > (_ptr + _buffer_size)) {
/* Wrap-around case */
if ((_end_of_data < cur_pos) || (new_end >= cur_pos)) {
warning("Mixer full... Trying to not break too much ");
return;
}
memcpy(_end_of_data, data, (_ptr + _buffer_size) - _end_of_data);
memcpy(_ptr, (byte *)data + ((_ptr + _buffer_size) - _end_of_data),
len - ((_ptr + _buffer_size) - _end_of_data));
} else {
if ((_end_of_data < cur_pos) && (new_end >= cur_pos)) {
warning("Mixer full... Trying to not break too much ");
return;
}
memcpy(_end_of_data, data, len);
}
_end_of_data = new_end;
}
void SoundMixer::Channel_STREAM::mix(int16 *data, uint len)
{
uint32 fp_pos;
const uint32 fp_speed = _fp_speed;
const int16 *vol_tab = _mixer->_volume_table;
byte *end_of_data = _end_of_data;
if (_to_be_destroyed) {
real_destroy();
return;
}
fp_pos = _fp_pos;
if (_pos < end_of_data) {
mixer_helper_table[_flags & 0x07] (data, &len, &_pos, &fp_pos, fp_speed, vol_tab, end_of_data);
} else {
mixer_helper_table[_flags & 0x07] (data, &len, &_pos, &fp_pos, fp_speed, vol_tab,
_ptr + _buffer_size);
if (len != 0) {
_pos = _ptr;
mixer_helper_table[_flags & 0x07] (data, &len, &_pos, &fp_pos, fp_speed, vol_tab,
end_of_data);
} else
_to_be_destroyed = true;
}
if (len != 0) {
// FIXME: BBrox, what does this mean? :)
// Commented by Ender to remove non-existant
// streamer bug in Dig smush movies.
//warning("Streaming underflow of %d bytes", len);
//real_destroy();
//return;
}
_fp_pos = fp_pos;
}
void SoundMixer::Channel_STREAM::real_destroy()
{
free(_ptr);
_mixer->uninsert(this);
delete this;
}
/* MP3 mixer goes here */
#ifdef COMPRESSED_SOUND_FILE
SoundMixer::Channel_MP3::Channel_MP3(SoundMixer *mixer, void *sound, uint size, byte flags)
{
_mixer = mixer;
_flags = flags;
_pos_in_frame = 0xFFFFFFFF;
_position = 0;
_size = size;
_ptr = sound;
_to_be_destroyed = false;
mad_stream_init(&_stream);
#ifdef _WIN32_WCE
// 11 kHz on WinCE
mad_stream_options(&_stream, MAD_OPTION_HALFSAMPLERATE);
#endif
mad_frame_init(&_frame);
mad_synth_init(&_synth);
/* This variable is the number of samples to cut at the start of the MP3
file. This is needed to have lip-sync as the MP3 file have some miliseconds
of blank at the start (as, I suppose, the MP3 compression algorithm need to
have some silence at the start to really be efficient and to not distort
too much the start of the sample).
This value was found by experimenting out. If you recompress differently your
.SO3 file, you may have to change this value.
When using Lame, it seems that the sound starts to have some volume about 50 ms
from the start of the sound => we skip about 1024 samples.
*/
_silence_cut = 1024;
}
static inline int scale_sample(mad_fixed_t sample)
{
/* round */
sample += (1L << (MAD_F_FRACBITS - 16));
/* clip */
if (sample >= MAD_F_ONE)
sample = MAD_F_ONE - 1;
else if (sample < -MAD_F_ONE)
sample = -MAD_F_ONE;
/* quantize and scale to not saturate when mixing a lot of channels */
return sample >> (MAD_F_FRACBITS + 2 - 16);
}
void SoundMixer::Channel_MP3::mix(int16 *data, uint len)
{
mad_fixed_t const *ch;
const int16 *vol_tab = _mixer->_volume_table;
unsigned char volume = ((int)vol_tab[1]) * 32 / 255;
if (_to_be_destroyed) {
real_destroy();
return;
}
while (1) {
ch = _synth.pcm.samples[0] + _pos_in_frame;
while ((_pos_in_frame < _synth.pcm.length) && (len > 0)) {
if (_silence_cut > 0) {
_silence_cut--;
} else {
int16 sample = (int16)((scale_sample(*ch++) * volume) / 32);
*data++ += sample;
*data++ += sample;
len--;
}
_pos_in_frame++;
}
if (len == 0)
return;
if (_position >= _size) {
real_destroy();
return;
}
mad_stream_buffer(&_stream, ((unsigned char *)_ptr) + _position,
_size + MAD_BUFFER_GUARD - _position);
if (mad_frame_decode(&_frame, &_stream) == -1) {
/* End of audio... */
if (_stream.error == MAD_ERROR_BUFLEN) {
real_destroy();
return;
} else if (!MAD_RECOVERABLE(_stream.error)) {
error("MAD frame decode error !");
}
}
mad_synth_frame(&_synth, &_frame);
_pos_in_frame = 0;
_position = (unsigned char *)_stream.next_frame - (unsigned char *)_ptr;
}
}
void SoundMixer::Channel_MP3::real_destroy()
{
if (_flags & FLAG_AUTOFREE)
free(_ptr);
_mixer->uninsert(this);
mad_synth_finish(&_synth);
mad_frame_finish(&_frame);
mad_stream_finish(&_stream);
delete this;
}
/* MP3 CD music */
#define MP3CD_BUFFERING_SIZE 131072
SoundMixer::Channel_MP3_CDMUSIC::Channel_MP3_CDMUSIC(SoundMixer *mixer, FILE * file,
mad_timer_t duration)
{
_mixer = mixer;
_file = file;
_duration = duration;
_initialized = false;
_buffer_size = MP3CD_BUFFERING_SIZE;
_ptr = malloc(MP3CD_BUFFERING_SIZE);
_to_be_destroyed = false;
mad_stream_init(&_stream);
#ifdef _WIN32_WCE
// 11 kHz on WinCE
mad_stream_options(&_stream, MAD_OPTION_HALFSAMPLERATE);
#endif
mad_frame_init(&_frame);
mad_synth_init(&_synth);
}
void SoundMixer::Channel_MP3_CDMUSIC::mix(int16 *data, uint len)
{
mad_fixed_t const *ch;
mad_timer_t frame_duration;
unsigned char volume = _mixer->_music_volume * 32 / 255;
if (_to_be_destroyed) {
real_destroy();
return;
}
if (!_initialized) {
int skip_loop;
// just skipped
memset(_ptr, 0, _buffer_size);
_size = fread(_ptr, 1, _buffer_size, _file);
if (!_size) {
real_destroy();
return;
}
// Resync
mad_stream_buffer(&_stream, (unsigned char *)_ptr, _size);
skip_loop = 2;
while (skip_loop != 0) {
if (mad_frame_decode(&_frame, &_stream) == 0) {
/* Do not decrease duration - see if it's a problem */
skip_loop--;
if (skip_loop == 0) {
mad_synth_frame(&_synth, &_frame);
}
} else {
if (!MAD_RECOVERABLE(_stream.error)) {
debug(1, "Unrecoverable error while skipping !");
real_destroy();
return;
}
}
}
// We are supposed to be in synch
mad_frame_mute(&_frame);
mad_synth_mute(&_synth);
// Resume decoding
if (mad_frame_decode(&_frame, &_stream) == 0) {
_pos_in_frame = 0;
_initialized = true;
} else {
debug(1, "Cannot resume decoding");
real_destroy();
return;
}
}
while (1) {
// Get samples, play samples ...
ch = _synth.pcm.samples[0] + _pos_in_frame;
while ((_pos_in_frame < _synth.pcm.length) && (len > 0)) {
int16 sample = (int16)((scale_sample(*ch++) * volume) / 32);
*data++ += sample;
*data++ += sample;
len--;
_pos_in_frame++;
}
if (len == 0) {
return;
}
// See if we have finished
// May be incorrect to check the size at the end of a frame but I suppose
// they are short enough :)
frame_duration = _frame.header.duration;
mad_timer_negate(&frame_duration);
mad_timer_add(&_duration, frame_duration);
if (mad_frame_decode(&_frame, &_stream) == -1) {
if (_stream.error == MAD_ERROR_BUFLEN) {
int not_decoded;
if (!_stream.next_frame) {
memset(_ptr, 0, _buffer_size + MAD_BUFFER_GUARD);
_size = fread(_ptr, 1, _buffer_size, _file);
not_decoded = 0;
} else {
not_decoded = _stream.bufend - _stream.next_frame;
memcpy(_ptr, _stream.next_frame, not_decoded);
_size = fread((unsigned char *)_ptr + not_decoded, 1, _buffer_size - not_decoded, _file);
}
_stream.error = (enum mad_error)0;
// Restream
mad_stream_buffer(&_stream, (unsigned char *)_ptr, _size + not_decoded);
if (mad_frame_decode(&_frame, &_stream) == -1) {
debug(1, "Error decoding after restream %d !", _stream.error);
}
} else if (!MAD_RECOVERABLE(_stream.error)) {
error("MAD frame decode error in MP3 CDMUSIC !");
}
}
mad_synth_frame(&_synth, &_frame);
_pos_in_frame = 0;
}
}
bool SoundMixer::Channel_MP3_CDMUSIC::sound_finished()
{
return mad_timer_compare(_duration, mad_timer_zero) <= 0;
}
void SoundMixer::Channel_MP3_CDMUSIC::real_destroy()
{
free(_ptr);
_mixer->uninsert(this);
mad_synth_finish(&_synth);
mad_frame_finish(&_frame);
mad_stream_finish(&_stream);
delete this;
}
#endif
|