-
Notifications
You must be signed in to change notification settings - Fork 13
/
marssound.c
1248 lines (1020 loc) · 25.2 KB
/
marssound.c
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
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "doomdef.h"
#include "mars.h"
#include "mars_ringbuf.h"
#include "sounds.h"
#define MAX_SAMPLES 316 // 70Hz
//#define MAX_SAMPLES 440 // 50Hz
//#define MAX_SAMPLES 734 // 30Hz
#define SAMPLE_RATE 22050
#define SAMPLE_MIN 2
#define SAMPLE_MAX 1032
#define SAMPLE_CENTER (SAMPLE_MAX-SAMPLE_MIN)/2
#define SPATIALIZATION_RATE 15 // Hz
// this many samples between each spatialization call
#define SPATIALIZATION_SRATE (int)(MAX_SAMPLES*(((float)SAMPLE_RATE/MAX_SAMPLES)/SPATIALIZATION_RATE))
// when to clip out sounds
// Does not fit the large outdoor areas.
#define S_CLIPPING_DIST (1224 * FRACUNIT)
// Distance tp origin when sounds should be maxed out.
// This should relate to movement clipping resolution
// (see BLOCKMAP handling).
// In the source code release: (160*FRACUNIT). Changed back to the
// Vanilla value of 200 (why was this changed?)
#define S_CLOSE_DIST (200 * FRACUNIT)
// The range over which sound attenuates
#define S_ATTENUATOR (S_CLIPPING_DIST - S_CLOSE_DIST)
// Stereo separation
#define S_STEREO_SWING (96 * FRACUNIT)
// WAV
#define S_LE_SHORT(chunk) (((chunk)[0]>>8)|(((chunk)[0]&0xff) << 8))
#define S_LE_LONG(chunk) (S_LE_SHORT(chunk) | (S_LE_SHORT(chunk+1)<<16))
#define S_WAV_FORMAT_PCM 0x1
#define S_WAV_FORMAT_IMA_ADPCM 0x11
#define S_WAV_FORMAT_EXTENSIBLE 0xfffe
#define S_SEP_VOL_TO_MCD(sep,vol) do { if ((sep) > 254) (sep) = 254; (vol) <<= 2; /* 64 -> 256 max */ if ((vol) > 255) (vol) = 255; } while (0)
#define S_USE_MEGACD_DRV() (mcd_avail && (sfxdriver == sfxdriver_auto || sfxdriver == sfxdriver_mcd))
enum
{
SNDCMD_NONE,
SNDCMD_CLEAR,
SNDCMD_STARTSND,
SNDCMD_STARTORGSND
};
static uint8_t snd_bufidx = 0;
int16_t __attribute__((aligned(16))) snd_buffer[2][MAX_SAMPLES * 2];
static uint8_t snd_init = 0;
unsigned snd_nopaintcount = 0;
static VINT *vgm_tracks;
uint8_t *vgm_ptr;
sfxchannel_t pcmchannel;
int __attribute__((aligned(16))) pcm_data[4];
sfxchannel_t sfxchannels[SFXCHANNELS];
VINT sfxvolume = 64; /* range 0 - 64 */
VINT musicvolume = 64; /* range 0 - 64 */
VINT musictype = mustype_fm;
static VINT curmusic, muslooping = 0, curcdtrack = cdtrack_none;
int samplecount = 0;
static marsrb_t soundcmds = { 0 };
VINT sfxdriver = sfxdriver_auto, mcd_avail = 0; // 0 - auto, 2 - megacd, 2 - 32x
static sfxchannel_t *S_AllocateChannel(mobj_t* mobj, unsigned sound_id, int vol, getsoundpos_t getpos);
static void S_SetChannelData(sfxchannel_t* channel);
int S_PaintChannel4IMA(void* mixer, int16_t* buffer, int32_t cnt, int32_t scale) ATTR_DATA_CACHE_ALIGN;
int S_PaintChannel4IMA2x(void* mixer, int16_t* buffer, int32_t cnt, int32_t scale) ATTR_DATA_CACHE_ALIGN;
void S_PaintChannel8(void* mixer, int16_t* buffer, int32_t cnt, int32_t scale) ATTR_DATA_CACHE_ALIGN;
static int S_PaintChannel(sfxchannel_t *ch, int16_t* buffer, int painted) ATTR_DATA_CACHE_ALIGN;
static void S_StartSoundEx(mobj_t *mobj, int sound_id, getsoundpos_t getpos);
static void S_SpatializeAt(fixed_t*origin, mobj_t* listener, int* pvol, int* psep) ATTR_DATA_CACHE_ALIGN;
static void S_Spatialize(mobj_t* mobj, int* pvol, int* psep, getsoundpos_t getpos) ATTR_DATA_CACHE_ALIGN;
static void S_SpatializeAll(void) ATTR_DATA_CACHE_ALIGN;
static void S_Update(int16_t* buffer) ATTR_DATA_CACHE_ALIGN;
static void S_UpdatePCM(void) ATTR_DATA_CACHE_ALIGN;
static void S_Sec_DMA1Handler(void);
static void S_Pri_CmdHandler(void);
static void S_Sec_DMA1Handler(void);
/*
==================
=
= S_Init
=
==================
*/
void S_Init(void)
{
int i;
int initmusictype;
VINT tmp_tracks[100];
int start, end;
for (i = 0; i < SFXCHANNELS; i++)
sfxchannels[i].data = NULL;
// check if CD is available, reset the driver option if not
mcd_avail = S_CDAvailable() & 0x1;
if (!mcd_avail)
sfxdriver = sfxdriver_auto;
/* init sound effects */
start = W_CheckNumForName("DS_START");
end = W_CheckNumForName("DS_END");
if (start >= 0 && end > 0)
{
for (i=1 ; i < NUMSFX ; i++)
{
S_sfx[i].lump = W_CheckRangeForName(S_sfxnames[i], start, end);
}
}
else
{
for (i=1 ; i < NUMSFX ; i++)
{
S_sfx[i].lump = W_CheckNumForName(S_sfxnames[i]);
}
}
if (mcd_avail)
{
for (i=1 ; i < NUMSFX ; i++)
{
int lump = S_sfx[i].lump;
if (lump < 0) {
continue;
}
Mars_MCDLoadSfx(i, W_POINTLUMPNUM(lump), W_LumpLength(lump));
}
}
/* init music */
num_music = 0;
muslooping = 0;
S_StopSong();
for (i = 1; i < numlumps; i++)
{
char name[9];
D_memcpy(name, W_GetNameForNum(i), 8);
name[8] = 0;
if (D_strncasecmp("VGM_", name, 4))
continue;
tmp_tracks[num_music++] = i;
if (num_music == (int)sizeof(tmp_tracks) / sizeof(tmp_tracks[0]))
break;
}
if (num_music > 0)
{
vgm_tracks = Z_Malloc(sizeof(*vgm_tracks) * num_music, PU_STATIC);
for (i = 0; i < num_music; i++) {
vgm_tracks[i] = tmp_tracks[i];
}
}
Mars_RB_ResetAll(&soundcmds);
Mars_SetPriCmdCallback(&S_Pri_CmdHandler);
Mars_InitSoundDMA(1);
// FIXME: this is ugly, get rid of global variables!
// musictype is now set to value from SRAM.
// force proper initialization by resetting it to 'none',
// so that S_SetMusicType won't ignore the new value
initmusictype = musictype;
musictype = mustype_none;
S_SetMusicType(initmusictype);
}
/*
==================
=
= S_Clear
=
==================
*/
void S_Clear (void)
{
volatile int tic;
if (S_USE_MEGACD_DRV())
{
D_memset(sfxchannels, 0, sizeof(*sfxchannels) * SFXCHANNELS);
Mars_MCDClearSfx();
Mars_MCDFlushSfx();
return;
}
uint16_t *p = (uint16_t*)Mars_RB_GetWriteBuf(&soundcmds, 8, false);
if (!p)
return;
*p++ = SNDCMD_CLEAR;
Mars_RB_CommitWrite(&soundcmds);
/* do not wait the reader indefinitely */
/* a hack to get Gens KMod working */
for (tic = I_GetTime(); I_GetTime() < tic + 10; )
{
if (Mars_RB_Len(&soundcmds) == 0)
break;
}
}
void S_RestartSounds (void)
{
}
void S_SetSoundDriver (int newdrv)
{
S_Clear();
sfxdriver = newdrv;
switch (newdrv) {
case sfxdriver_pwm:
Mars_RB_ResetAll(&soundcmds);
Mars_InitSoundDMA(0);
break;
}
}
/*
==================
=
= S_SpatializeAt
=
==================
*/
static void S_SpatializeAt(fixed_t* origin, mobj_t* listener, int* pvol, int* psep)
{
int dist_approx;
int dx, dy;
angle_t angle;
int vol, sep;
dx = D_abs(origin[0] - listener->x);
dy = D_abs(origin[1] - listener->y);
dist_approx = dx + dy - ((dx < dy ? dx : dy) >> 1);
if (dist_approx > S_CLIPPING_DIST)
{
vol = 0;
sep = 128;
}
else
{
// angle of source to listener
angle = R_PointToAngle2(listener->x, listener->y,
origin[0], origin[1]);
if (angle > listener->angle)
angle = angle - listener->angle;
else
angle = angle + (0xffffffff - listener->angle);
angle >>= ANGLETOFINESHIFT;
sep = FixedMul(S_STEREO_SWING, finesine(angle));
sep >>= FRACBITS;
sep = 128 - sep;
if (sep < 0)
sep = 0;
else if (sep > 255)
sep = 255;
if (dist_approx < S_CLOSE_DIST)
vol = sfxvolume;
else if (dist_approx >= S_CLIPPING_DIST)
vol = 0;
else
{
vol = sfxvolume * (S_CLIPPING_DIST - dist_approx);
vol = (unsigned)vol / S_ATTENUATOR;
if (vol > sfxvolume)
vol = sfxvolume;
}
}
*pvol = vol;
*psep = sep;
}
/*
==================
=
= S_Spatialize
=
==================
*/
static void S_Spatialize(mobj_t* mobj, int *pvol, int *psep, getsoundpos_t getpos)
{
int vol, sep;
player_t* player = &players[consoleplayer];
player_t* player2 = &players[consoleplayer^1];
vol = sfxvolume;
sep = 128;
if (mobj)
{
fixed_t origin[2];
if (getpos)
{
getpos(mobj, origin);
}
else
{
origin[0] = mobj->x, origin[1] = mobj->y;
}
if (mobj != player->mo)
S_SpatializeAt(origin, player->mo, &vol, &sep);
if (splitscreen && player2->mo)
{
int vol2, sep2;
vol2 = sfxvolume;
sep2 = 255;
if (mobj != player2->mo)
S_SpatializeAt(origin, player2->mo, &vol2, &sep2);
sep = 128 + (vol2 - vol) * 2;
vol = vol2 > vol ? vol2 : vol;
if (sep < 0) sep = 0;
else if (sep > 255) sep = 255;
}
}
*pvol = vol;
*psep = sep;
}
/*
==================
=
= S_SpatializeAll
=
==================
*/
static void S_SpatializeAll(void)
{
int i;
mobj_t *mo;
for (i = 0; i < SFXCHANNELS; i++)
{
sfxchannel_t* ch = &sfxchannels[i];
if (!ch->data)
continue;
mo = ch->mobj;
if (mo)
{
int vol, sep;
/* */
/* spatialize */
/* */
if (!ch->getpos)
{
Mars_ClearCacheLine(&mo->x);
Mars_ClearCacheLine(&mo->y);
}
S_Spatialize(mo, &vol, &sep, ch->getpos);
if (!vol)
{
// inaudible
ch->data = NULL;
continue;
}
ch->volume = vol;
ch->pan = sep;
}
}
}
/*
==================
=
= S_StartSoundEx
=
==================
*/
static void S_StartSoundEx(mobj_t *mobj, int sound_id, getsoundpos_t getpos)
{
int vol, sep;
sfxinfo_t *sfx;
/* Get sound effect data pointer */
if (sound_id <= 0 || sound_id >= NUMSFX)
return;
sfx = &S_sfx[sound_id];
if (sfx->lump < 0)
return;
/* */
/* spatialize */
/* */
S_Spatialize(mobj, &vol, &sep, getpos);
if (!vol)
return; /* too far away */
// HACK: boost volume for item pickups
if (sound_id == sfx_itemup)
vol <<= 1;
if (S_USE_MEGACD_DRV())
{
sfxchannel_t *ch;
ch = S_AllocateChannel(mobj, sound_id, vol, getpos);
if (!ch)
return;
S_SEP_VOL_TO_MCD(sep, vol);
if (sound_id == sfx_itemup && sep == 128)
sep = 255; // full volume from both channels
Mars_MCDPlaySfx((ch - sfxchannels) + 1, sound_id, sep, vol);
return;
}
uint16_t* p = (uint16_t*)Mars_RB_GetWriteBuf(&soundcmds, 8, false);
if (!p)
return;
if (getpos)
{
*p++ = SNDCMD_STARTORGSND;
*p++ = sound_id;
*(int*)p = (intptr_t)mobj, p += 2;
*(int*)p = (intptr_t)getpos, p += 2;
*p++ = vol;
}
else
{
*p++ = SNDCMD_STARTSND;
*p++ = sound_id;
*(int*)p = (intptr_t)mobj, p += 2;
*p++ = vol;
}
Mars_RB_CommitWrite(&soundcmds);
}
void S_StartSound(mobj_t* mobj, int sound_id)
{
S_StartSoundEx(mobj, sound_id, NULL);
}
void S_StartPositionedSound(mobj_t* mobj, int sound_id, getsoundpos_t getpos)
{
S_StartSoundEx(mobj, sound_id, getpos);
}
/*
===================
=
= S_UpdateSounds
=
===================
*/
void S_PreUpdateSounds(void)
{
if (S_USE_MEGACD_DRV())
{
int i;
sfxchannel_t *ch;
int status, bit;
bit = 1;
status = Mars_MCDGetSfxPlaybackStatus();
for (ch = sfxchannels, i = 0; i < SFXCHANNELS; i++, ch++, bit <<= 1)
{
if (!ch->data)
continue;
if (!(status & bit)) {
// stopped
ch->data = NULL;
} else {
ch->position = 1;
}
}
}
}
void S_UpdateSounds(void)
{
static VINT oldmusvol = -1;
if (oldmusvol != musicvolume) {
if (S_CDAvailable()) {
int vol = musicvolume*4;
Mars_SetMusicVolume(vol > 255 ? 255 : vol);
}
oldmusvol = musicvolume;
}
if (S_USE_MEGACD_DRV())
{
int i;
sfxchannel_t *ch;
// spatalize
S_SpatializeAll();
for (ch = sfxchannels, i = 0; i < SFXCHANNELS; i++, ch++)
{
int vol, sep;
if (!ch->data)
continue;
sep = ch->pan;
vol = ch->volume;
S_SEP_VOL_TO_MCD(sep, vol);
Mars_MCDUpdateSfx(i + 1, sep, vol);
}
Mars_MCDFlushSfx();
}
}
void S_SetMusicType(int newtype)
{
int savemus, savecd;
if (newtype < mustype_none || newtype > mustype_cd)
return;
if (musictype == newtype)
return;
if (newtype == mustype_cd && !S_CDAvailable())
return;
// restart the current track
savemus = curmusic;
savecd = curcdtrack;
if (musictype != mustype_none)
S_StopSong();
curmusic = mus_none;
musictype = newtype;
curcdtrack = cdtrack_none;
S_StartSong(savemus, muslooping, savecd);
}
int S_CDAvailable(void)
{
/* recheck cd and get number of tracks */
Mars_UpdateCD();
return mars_cd_ok;
}
int S_SongForMapnum(int mapnum)
{
int i;
VINT songs[100];
VINT numsongs;
numsongs = 0;
for (i = 0; i < num_music; i++) {
VINT mus = vgm_tracks[i];
if (mus == gameinfo.titleMus)
continue;
if (mus == gameinfo.intermissionMus)
continue;
if (mus == gameinfo.victoryMus)
continue;
songs[numsongs++] = mus;
if (numsongs == sizeof(songs) / sizeof(songs[0]))
break;
}
if (numsongs == 0)
return mus_none;
return songs[(mapnum - 1) % numsongs];
}
void S_StartSong(int musiclump, int looping, int cdtrack)
{
int playtrack = 0;
if (musictype == mustype_cd)
{
if (cdtrack == cdtrack_none)
{
S_StopSong();
return;
}
if (S_CDAvailable())
{
int num_map_tracks = (int)mars_num_cd_tracks + cdtrack_lastmap;
/* there is a disc with at least enough tracks */
if (cdtrack <= cdtrack_title)
playtrack = cdtrack + mars_num_cd_tracks;
else if (num_map_tracks > 0)
playtrack = 1 + (cdtrack - 1) % num_map_tracks;
else
playtrack = cdtrack_intermission + mars_num_cd_tracks;
}
if (playtrack < 0)
return;
if (curcdtrack == cdtrack && muslooping == looping)
return;
}
else if (musictype == mustype_fm)
{
int i;
for (i = 0; i < num_music; i++)
{
if (vgm_tracks[i] == musiclump)
{
playtrack = i + 1;
break;
}
}
if (musiclump == mus_none || playtrack == 0)
{
S_StopSong();
return;
}
if (curmusic == musiclump && muslooping == looping)
return;
}
curmusic = musiclump;
curcdtrack = cdtrack;
muslooping = looping;
if (musictype == mustype_none)
return;
if (musictype == mustype_cd)
{
Mars_PlayTrack(1, playtrack, NULL, 0, looping);
return;
}
Mars_StopTrack(); // stop the playback before flipping pages
S_Clear();
vgm_ptr = (uint8_t *)W_POINTLUMPNUM(musiclump);
Mars_PlayTrack(0, playtrack, vgm_ptr, W_LumpLength(musiclump), looping);
}
void S_StopSong(void)
{
Mars_StopTrack();
curmusic = mus_none;
curcdtrack = cdtrack_none;
}
static void S_ClearPCM(void)
{
D_memset(pcm_data, 0, sizeof(pcm_data));
D_memset(&pcmchannel, 0, sizeof(pcmchannel));
pcmchannel.volume = /*musicvolume*/40; /* 64 seems to be too loud */
pcmchannel.pan = 128;
}
static void S_UpdatePCM(void)
{
int inc, len, offs, flag;
Mars_ClearCacheLine(pcm_data);
inc = pcm_data[0]; /* cache read line loads all vars at once to cache */
len = pcm_data[1];
offs = pcm_data[2];
flag = pcm_data[3];
if (flag & 1)
{
if (offs == -1)
{
pcmchannel.data = NULL;
((volatile short *)pcm_data)[7] = 0; // unset bit 0 for flag
return;
}
/* do once */
pcmchannel.position = 0;
pcmchannel.increment = inc;
pcmchannel.length = len;
pcmchannel.data = vgm_ptr + offs;
((volatile short *)pcm_data)[7] = 0; // unset bit 0 for flag
}
}
static int S_PaintChannel(sfxchannel_t *ch, int16_t* buffer, int painted)
{
if (!ch->data)
return 0;
if (ch->width == 4)
{
int i = MAX_SAMPLES;
int32_t *end = (int32_t *)buffer + MAX_SAMPLES;
int (*paintch)(void* mixer, int16_t* buffer, int32_t cnt, int32_t scale);
// general mixing routine
paintch = S_PaintChannel4IMA;
if ((ch->increment < (1 << 14)) && !(ch->increment & (ch->increment-1))) {
// optimized 2x upsampling mixer: from 11kHz to 22kHz or 44kHz
paintch = S_PaintChannel4IMA2x;
}
do
{
if (ch->position >= ch->length)
{
// advance to next block
ch->data += ch->block_size-3;
ch->length = 0;
}
if (!ch->length)
{
uint8_t *block = (uint8_t *)ch->data;
int block_size = ch->block_size;
if (block_size > ch->remaining_bytes)
block_size = ch->remaining_bytes;
if (block_size < 4)
{
// EOF
ch->data = NULL;
break;
}
ch->position = 1 << 14;
ch->prev_pos = -1; // force output of initial predictor
ch->length = ((block_size-3) << 1) << 14;
// initial step_index : initial predictor
ch->loop_length = (((unsigned)block[2]*2) << 16) | ((unsigned)block[1] << 8) | block[0];
ch->data += 3;
ch->remaining_bytes -= block_size;
}
i = paintch(ch, (int16_t *)(end - i), i, 64);
} while (i > 0);
painted = MAX_SAMPLES - i;
}
else
{
S_PaintChannel8(ch, buffer, MAX_SAMPLES, 64);
if (ch->position >= ch->length)
{
ch->data = NULL;
}
painted = MAX_SAMPLES;
}
return painted;
}
static void S_Update(int16_t* buffer)
{
int i;
int32_t *b2;
int c, l, h;
mobj_t* mo;
int painted = 0;
S_UpdatePCM();
Mars_ClearCacheLine(&sfxvolume);
Mars_ClearCacheLine(&musicvolume);
Mars_ClearCacheLine(&sfxdriver);
if (pcmchannel.data)
{
snd_nopaintcount = 0;
}
else
{
if (S_USE_MEGACD_DRV())
{
snd_nopaintcount++;
}
else
{
for (i = 0; i < SFXCHANNELS; i++)
{
if (sfxchannels[i].data)
break;
}
if (i == SFXCHANNELS)
snd_nopaintcount++;
else
snd_nopaintcount = 0;
}
}
if (snd_nopaintcount > 2)
{
// we haven't painted the sound channels to the output buffer
// for over two frames, both buffers are cleared and safe to
// point the DMA to
return;
}
b2 = (int32_t *)buffer;
for (i = 0; i < MAX_SAMPLES; i++)
{
*b2++ = 0;
}
/* keep updating the channel until done */
painted += S_PaintChannel(&pcmchannel, buffer, painted);
if (!S_USE_MEGACD_DRV())
{
boolean spatialize = samplecount >= SPATIALIZATION_SRATE;
if (spatialize)
{
for (i = 0; i < MAXPLAYERS; i++)
{
player_t* player = &players[i];
if (!playeringame[i])
continue;
Mars_ClearCacheLine(&player->mo);
mo = player->mo;
Mars_ClearCacheLine(&mo->x);
Mars_ClearCacheLine(&mo->y);
Mars_ClearCacheLine(&mo->angle);
}
S_SpatializeAll();
}
for (i = 0; i < SFXCHANNELS; i++)
{
sfxchannel_t* ch = &sfxchannels[i];
if (!ch->data)
continue;
painted += S_PaintChannel(ch, buffer, painted);
}
}
if (samplecount >= SPATIALIZATION_SRATE)
samplecount -= SPATIALIZATION_SRATE;
samplecount += MAX_SAMPLES;
#ifdef MARS
// force GCC into keeping constants in registers as it
// is stupid enough to reload them on each loop iteration
__asm volatile("mov %1,%0\n\t" : "=&r" (c) : "r"(SAMPLE_CENTER));
__asm volatile("mov %1,%0\n\t" : "=&r" (l) : "r"(SAMPLE_MIN));
__asm volatile("mov %1,%0\n\t" : "=&r" (h) : "r"(SAMPLE_MAX));
#else
c = SAMPLE_CENTER, l = SAMPLE_MIN, h = SAMPLE_MAX;
#endif
// convert buffer from s16 pcm samples to u16 pwm samples
b2 = (int32_t *)buffer;
for (i = 0; i < MAX_SAMPLES; i++)
{
int s, s1, s2;
s = (int16_t)(*b2 >> 16) + c;
s1 = (s < l) ? l : (s > h) ? h : s;
s = (int16_t)(*b2 ) + c;
s2 = (s < l) ? l : (s > h) ? h : s;
*b2++ = (s1 << 16) | s2;
}
}
static void S_Sec_DMA1Handler(void)
{
Mars_ClearCacheLine(&sfxdriver);
if (snd_nopaintcount > 2 && S_USE_MEGACD_DRV())
{
snd_init = 0;
Mars_RB_ResetRead(&soundcmds);
return;
}
// start DMA on buffer and fill the other one
SH2_DMA_SAR1 = (uintptr_t)snd_buffer[snd_bufidx];
SH2_DMA_TCR1 = MAX_SAMPLES; // number longs
SH2_DMA_CHCR1 = 0x18E5; // dest fixed, src incr, size long, ext req, dack mem to dev, dack hi, dack edge, dreq rising edge, cycle-steal, dual addr, intr enabled, clear TE, dma enabled
snd_bufidx ^= 1; // flip audio buffer
Mars_Sec_ReadSoundCmds();
S_Update(snd_buffer[snd_bufidx]);
}
/*
==================
=
= S_AllocateChannel
=
==================
*/
static sfxchannel_t *S_AllocateChannel(mobj_t* mobj, unsigned sound_id, int vol, getsoundpos_t getpos)
{
sfxchannel_t* channel, * newchannel;
int i;
int length;
sfxinfo_t* sfx;
sfx_t* md_data;
newchannel = NULL;
sfx = &S_sfx[sound_id];
md_data = W_POINTLUMPNUM(sfx->lump);
length = md_data->samples;
if (length < 4)
return NULL;
/* reject sounds started at the same instant and singular sounds */
for (channel = sfxchannels, i = 0; i < SFXCHANNELS; i++, channel++)
{
if (channel->sfx == sfx)
{
if (channel->position <= 0) /* ADPCM has the position set to -1 initially */
{
if (channel->volume < vol)
{
newchannel = channel;
goto gotchannel; /* overlay this */
}
return NULL; /* exact sound already started */
}
if (sfx->singularity)
{
newchannel = channel; /* overlay this */
goto gotchannel;
}
}
if (mobj && channel->mobj == mobj)
{ /* cut off whatever was coming from this origin */
newchannel = channel;
goto gotchannel;
}