forked from LemonHaze420/DCPopulous
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsoundsystem.cpp
1657 lines (1398 loc) · 42.1 KB
/
soundsystem.cpp
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 "SoundSystem.h"
#include "SafeProgramming.h"
#include "Object.h"
#include "GameDefines.h"
#include <windows.h>
#include <mmsystem.h>
#include "misc.h"
//#pragma optimize("", off) // We're having problems...
// ----
#define GET_INSTRUMENT(a) (((a & 0xF0000000) >> 24) | ((a & 0xF000) >> 12))
#define GET_PITCH(a) ((a & 0xFFF0000) >> 16)
#define GET_EFFECT_CODE(a) ((a & 0xFFF) >> 8)
#define GET_EFFECT_PARAM(a) (a & 0xFF)
#define AUDIO_THRESHOLD 8500
// Static initialisation.
int CSoundSystem::SoundSystemCount = 0;
// Sample format.
//#define SOUND_SAMPLE_FORMAT WAVE_FORMAT_2S16 // Defined in "GameDefines.h"
#define SOUND_BUFFER_LENGTH_IN_SECONDS 0.12f
#ifdef COMPAQ_SOUND
// IPaqs are slow to declare a buffer finished with. Need fewer, but longer buffers.
// Longer buffers result in a time lag between the code
// issuing a sample play command and it actually being
// played. This configuration seems to be the best.
// Short or longer buffers cause stutter.
#define SOUND_BUFFER_LENGTH (3072)
#define NUMBER_OF_QUEUED_BUFFERS 2
#else
// All other machines are much better about the sound code
// and have a response time half that of the IPaq.
#define SOUND_BUFFER_LENGTH (1024)
#define NUMBER_OF_QUEUED_BUFFERS 3
#endif
//#define MOD_NOTES_PER_SEC 3
#define MOD_FRAME ((1323 * ModTempo) / 6) /* 11025 / (4 * (125 / 60)) */
#define RIGHT_VOLUME 0xFF
#define LEFT_VOLUME 0xFF
#define MASTER_VOLUME 0x3F//0x7F
#define ASSUMED_MOD_SAMPLE_FREQ 8278
// ----
CSoundSystem::CSoundSystem()
{
int i;
ModTempo = 6; // Mods normally play back at a tempo of 6.
SoundSystemID = SoundSystemCount;
SoundSystemCount++;
PlatformRef = NULL;
// Device info.
DeviceID = -1; // Assume that no device will be found.
ZeroMemory(&DeviceCaps, sizeof(DeviceCaps));
// Sample playback info.
for (i = MAX_SOUND_BUFFERS; i--;)
{
BufferArray[i] = NULL;
}
for (i = MAX_SIMULTANEOUS_SAMPLES; i--;)
{
PlayData[i].SampleRef = NULL;
PlayData[i].Index = 0;
}
NextBuffer = NUMBER_OF_QUEUED_BUFFERS;
// MOD
// Position info
for (i = MAX_POSITIONS; i--;)
PositionArray[i] = 0;
NumberOfPositions = 0;
NumberOfPatterns = 0;
// CurrentPosition = 0;
// Note info.
//memset((void*)PatternArray, 0, (sizeof(PatternStruct) * MAX_POSITIONS));
// Samples
for (i = NUM_OF_MOD_SAMPLES; i--;)
ModSampleArray[i] = NULL;
for (i = 4; i--;)
{
ModPlayData[i].SampleRef = NULL; // Pointer to sample object
ModPlayData[i].Index = 0; // Pointer to where we are up to playing
ModPlayData[i].Count = 0; // The number of times the sample is to be played.
ModPlayData[i].Pause = 0.0f; // Delay time.
ModPlayData[i].PauseRemaining = 0; // Amount of pause time remaining.
ModPlayData[i].Volume = 0; // Volume for this 'channel'
ModPlayData[i].Pitch = 0; // Current sample pitch.
// ModPlayData[i].TimeUntilNextNote = 0; // Kind of global property, but then again not.
// ModPlayData[i].CurrentPosition = 0; // Array in to PositionArray, which is in turn an index into PatternArray.
// ModPlayData[i].CurrentNote = 0;
}
GotOldVolume = false;
ModPlaying = false;
}
// ----
CSoundSystem::~CSoundSystem()
{
PlaySound(NULL, NULL, NULL); // Erm...
int i;
// Delete all play buffer.
for (i = MAX_SOUND_BUFFERS; i--;)
SAFELY_DELETE_ARRAY(BufferArray[i]);
// Delete any and all mod samples
for (i = NUM_OF_MOD_SAMPLES; i--;)
SAFELY_DELETE(ModSampleArray[i]);
if (DeviceID != -1)
{
if (OldVolume)
{
// Restore old volume.
if (waveOutSetVolume(DeviceHandle, OldVolume) == MMSYSERR_NOERROR)
{
//OutputDebugString(TEXT("Set old volume\n"));
}
}
// Kill the device.
waveOutReset(DeviceHandle);
waveOutClose(DeviceHandle);
}
}
// ----
bool CSoundSystem::Init(CPlatform *_PlatformRef, int _Volume)
{
#ifdef DISABLE_AUDIO
return true;
#endif
WAVEOUTCAPS EnumeratedDeviceCaps;
TCHAR buffer[1024];
int i;
if (SoundSystemID > 1)
return false;
if ( (_Volume < 0)
|| (_Volume > 10))
return false;
// ----
// Store imported data
PlatformRef = _PlatformRef;
// Store volume
MasterVolume = _Volume;
// Reset and restore any old device we opened.
if (DeviceID != -1)
{
// Restore old volume.
if (waveOutSetVolume(DeviceHandle, OldVolume) == MMSYSERR_NOERROR)
{
//OutputDebugString(TEXT("Set old volume\n"));
}
waveOutReset(DeviceHandle);
waveOutClose(DeviceHandle);
}
// ----
// MOD
// Position info
for (i = MAX_POSITIONS; i--;)
PositionArray[i] = 0;
NumberOfPositions = 0;
NumberOfPatterns = 0;
// CurrentPosition = 0;
// Note info.
//memset((void*)&PatternArray, 0, (sizeof(PatternStruct) * MAX_POSITIONS));
// Samples
for (i = NUM_OF_MOD_SAMPLES; i--;)
SAFELY_DELETE(ModSampleArray[i]);
// ----
// Device info.
DeviceID = -1; // Assume that no device will be found.
memset((void*)&DeviceCaps, 0, sizeof(DeviceCaps));
// Sample playback info.
for (i = MAX_SOUND_BUFFERS; i--;)
BufferArray[i] = NULL;
for (i = MAX_SIMULTANEOUS_SAMPLES; i--;)
{
PlayData[i].SampleRef = NULL;
PlayData[i].Index = 0;
ModPlayData[i].SampleRef = NULL;
ModPlayData[i].Index = 0;
}
NextBuffer = NUMBER_OF_QUEUED_BUFFERS - 1;
// ----
// Loop through all the available devices, attempting to
// find the bext one (what does 'best' mean?)
for (i = waveOutGetNumDevs(); i--;)
{
if (waveOutGetDevCaps(i, &EnumeratedDeviceCaps, sizeof(EnumeratedDeviceCaps)) == MMSYSERR_NOERROR)
{
/*
WORD wMid; // manufacturer ID
WORD wPid; // product ID
MMVERSION vDriverVersion; // version of the driver
TCHAR szPname[MAXPNAMELEN]; // product name (NULL terminated string)
DWORD dwFormats; // formats supported
WORD wChannels; // number of sources supported
WORD wReserved1; // packing
DWORD dwSupport; // functionality supported by driver
*/
/*
WAVE_FORMAT_1M08 => 11.025 kHz, Mono, 8-bit
WAVE_FORMAT_1S08 => 11.025 kHz, Stereo, 8-bit
WAVE_FORMAT_1M16 => 11.025 kHz, Mono, 16-bit
WAVE_FORMAT_1S16 => 11.025 kHz, Stereo, 16-bit
WAVE_FORMAT_2M08 => 22.05 kHz, Mono, 8-bit
WAVE_FORMAT_2S08 => 22.05 kHz, Stereo, 8-bit
WAVE_FORMAT_2M16 => 22.05 kHz, Mono, 16-bit
WAVE_FORMAT_2S16 => 22.05 kHz, Stereo, 16-bit
*/
swprintf( buffer,
TEXT("Device %d\n\tManufacturer %d\n\tProduct %d\n\tDriver Version %d\n\tProduct Name %s\n\tFormats %d\n\tNumber of channels %d \n\tReserved %d\n\tFunctionality %d\n"),
i,
EnumeratedDeviceCaps.wMid,
EnumeratedDeviceCaps.wPid,
EnumeratedDeviceCaps.vDriverVersion,
EnumeratedDeviceCaps.szPname,
EnumeratedDeviceCaps.dwFormats,
EnumeratedDeviceCaps.wChannels,
EnumeratedDeviceCaps.wReserved1,
EnumeratedDeviceCaps.dwSupport);
OutputDebugString(buffer);
if (DeviceID > -1)
{
// Is this device better than the one we've already got?
if ( (EnumeratedDeviceCaps.wChannels > DeviceCaps.wChannels)
&& (EnumeratedDeviceCaps.dwSupport & WAVE_FORMAT_1M16))
{
DeviceID = i;
memcpy((void*)&DeviceCaps, (void*)&EnumeratedDeviceCaps, sizeof(DeviceCaps));
}
}
else
{
// NEW: Assign the first sound device that comes along.
// Will try and get the favoured device if we can.
// Assign first enumerated device that supports the required sample format.
//if (EnumeratedDeviceCaps.dwSupport & SOUND_SAMPLE_FORMAT)
//{
DeviceID = i;
memcpy((void*)&DeviceCaps, (void*)&EnumeratedDeviceCaps, sizeof(DeviceCaps));
//}
}
}
}
// If we enumerated a device then lets create two buffers.
if (DeviceID > -1)
{
WAVEFORMATEX fmt;
/*
WORD wFormatTag;
WORD nChannels;
DWORD nSamplesPerSec;
DWORD nAvgBytesPerSec;
WORD nBlockAlign;
WORD wBitsPerSample;
WORD cbSize;
*/
fmt.wFormatTag = 1;
fmt.nChannels = 2;
fmt.nSamplesPerSec = 11025;
fmt.nAvgBytesPerSec = 11025 * 2 * 2; // 16 bit stereo
fmt.nBlockAlign = 4;
fmt.wBitsPerSample = 16;
fmt.cbSize = 0;
// Open wave device
if (waveOutOpen(&DeviceHandle,
DeviceID,
&fmt,
NULL,
(DWORD)this,
WAVE_MAPPED) == MMSYSERR_NOERROR)
{
//OutputDebugString(TEXT("Managed to open sound out device.\n"));
if (waveOutPause(DeviceHandle) == MMSYSERR_NOERROR)
{
//OutputDebugString(TEXT("Managed to pause sound out device.\n"));
// Set volume
DWORD Volume;
Volume = ( (((RIGHT_VOLUME * _Volume * 25) & 0xFFFF) << 16)
| ((LEFT_VOLUME * _Volume * 25) & 0xFFFF));
if (!GotOldVolume)
{
if (waveOutGetVolume(DeviceHandle, &OldVolume) != MMSYSERR_NOERROR)
{
OutputDebugString(TEXT("Unable to store old volume.\n"));
}
else
GotOldVolume = true;
}
if (waveOutSetVolume(DeviceHandle, Volume) == MMSYSERR_NOERROR)
{
//OutputDebugString(TEXT("Set volume.\n"));
}
// Create our play buffers
for (int i = MAX_SOUND_BUFFERS; i--;)
{
BufferArray[i] = new short[SOUND_BUFFER_LENGTH];
memset(BufferArray[i], 0, SOUND_BUFFER_LENGTH);
//OutputDebugString(TEXT("Created buffer.\n"));
}
for (i = NUMBER_OF_QUEUED_BUFFERS; i--;)
{
WaveHeaderArray[i].lpData = (char*)BufferArray[i];
WaveHeaderArray[i].dwBufferLength = 2 * SOUND_BUFFER_LENGTH;
WaveHeaderArray[i].dwFlags = 0;
WaveHeaderArray[i].dwUser = 0;
WaveHeaderArray[i].dwLoops = 1;
if (waveOutPrepareHeader(DeviceHandle, &WaveHeaderArray[i], sizeof(WAVEHDR)) == MMSYSERR_NOERROR)
{
//if (
waveOutWrite(DeviceHandle, &WaveHeaderArray[i], sizeof(WAVEHDR));
//== MMSYSERR_NOERROR)
//OutputDebugString(TEXT("Wrote out sound.\n"));
}
}
//if (
waveOutRestart(DeviceHandle);// == MMSYSERR_NOERROR)
//OutputDebugString(TEXT("Managed to restart sound out device.\n"));
}
}
else
{
DeviceID = -1; // Fail to open, thus give up.
// return false;
}
}
return true;
}
// ----
bool CSoundSystem::PlaySample( CSample* _Sound,
int _Channel,
int _Frequency,
int _Count,
float _Pause)
{
#ifdef DISABLE_AUDIO
return true;
#endif
/*if (_Unique)
{
// Check that is sample isn't already playing
for (int i = MAX_SIMULTANEOUS_SAMPLES; i--;)
{
if (PlayData[i].SampleRef == _Sound)
return false;
}
}*/
// Check if room to play another sample
//for (int i = MAX_SIMULTANEOUS_SAMPLES; i--;)
{
//if (!PlayData[i].SampleRef)
if ((_Channel >= 0) && (_Channel < MAX_SIMULTANEOUS_SAMPLES))
{
// Found a slot.
PlayData[_Channel].SampleRef = _Sound;
PlayData[_Channel].Index = 0;
PlayData[_Channel].Count = _Count;
if (_Pause < 0.0f)
PlayData[_Channel].Pause = -_Pause;
else
PlayData[_Channel].Pause = _Pause;
PlayData[_Channel].PauseRemaining = 0;
PlayData[_Channel].Pitch = _Frequency;
return true;
}
}
return false;
}
// ----
// Loads all the samples, and assorted data.
struct TSI_Struct
{
// char Name[22];
int Length; // In Bytes
int FTune; // Don't care
int Volume; // Why?
int RStart; // Repeat start point in bytes
int RLength; // Repeat length in bytes
};
// ----
bool CSoundSystem::LoadMod(CString *_FileName)
{
#ifdef DISABLE_AUDIO
return true;
#endif
FILE* FH;
char Buffer[32];
int i, j, k;
// Need some temp space to hold info about sample positions, etc
TSI_Struct TempSampleInfo[NUM_OF_MOD_SAMPLES];
if ( (_FileName)
&& (_FileName->GetString() != 0))
{
CString ModPath;
ModPath = PlatformRef->GetPath(EP_AudioData);
ModPath += _FileName;
// Note info.
memset((void*)PatternArray, 0, (sizeof(PatternStruct) * MAX_POSITIONS));
// Get rid of any previous info.
for (i = NUM_OF_MOD_SAMPLES; i--;)
SAFELY_DELETE(ModSampleArray[i]);
for (i = MAX_POSITIONS; i--;)
PositionArray[i] = 0;
NumberOfPositions = 0;
NumberOfPatterns = 0;
//memset(PatternArray, 0, sizeof(NotesStruct) * MAX_POSITIONS * 64);
for (i = 4; i--;)
{
ModPlayData[i].SampleRef = NULL;
ModPlayData[i].Index = 0;
ModPlayData[i].CurrentNote = 0;
ModPlayData[i].CurrentPosition = 0;
ModPlayData[i].TimeUntilNextNote = MOD_FRAME;
ModPlayData[i].Pitch = 0;
}
// Now load new mod
FH = FOPEN(ModPath.GetString(), "rb");
if (FH)
{
// Now what to do first?
// Feature Start Length
// Read song name [0] [20]
// Instrument 0 Name [20] [22]
// Length [42] [2]
// FineTuning [44] [1]
// Volume [45] [1]
// RepeatStart [46] [2]
// RepeatLen [48] [2]
// Seek to the start of the first sample description.
fread(Buffer, 20, 1, FH);
//if (fseek(FH, 20, SEEK_SET))
//{
// fclose(FH);
// return false;
//}
// 1. Strip out sample info.
for (i = 0; i < NUM_OF_MOD_SAMPLES; i++)
{
// Read the 30 bytes we want
fread(Buffer, 30, 1, FH);
//TempSampleInfo[i].Name
short cB;
cB = *((WORD*)(Buffer + 22));
TempSampleInfo[i].Length = 2 * EndianInt16(cB);
TempSampleInfo[i].FTune = *(Buffer + 24);
TempSampleInfo[i].Volume = *(Buffer + 25);
cB = *((WORD*)(Buffer + 26));
TempSampleInfo[i].RStart = 2 * EndianInt16(cB);
cB = *((WORD*)(Buffer + 28));
TempSampleInfo[i].RLength = 2 * EndianInt16(cB);
}
}
else
return false;
// Now need to read pattern data.
// File pointer should be in the correct place.
fread(&NumberOfPositions, 1, 1, FH); // NumberOfPositions ????????????????
fread(Buffer, 1, 1, FH); // Ignored (127)
fread(PositionArray, 128, 1, FH); // Positions
fread(Buffer, 4, 1, FH); // Ignored (M!K!).
CString con;
con = Buffer;
OutputDebugString(con.GetString());
// Calculate the highest pattern number.
NumberOfPatterns = -1;
for (i = 0; i < NumberOfPositions; i++)
{
if ( (NumberOfPatterns == -1)
|| (PositionArray[i] > NumberOfPatterns))
NumberOfPatterns = PositionArray[i];
}
// Read in position info
for (i = 0; i <= NumberOfPatterns; i++)
{
fread((void*)&PatternArray[i], 1024, 1, FH);
// Swap the data's endian-ness. (I'll make words up if I wish).
for (j = 0; j < 64; j++)
{
for (k = 0; k < 4; k++)
{
PatternArray[i].NotesArray[j].Note[k] = EndianInt32(PatternArray[i].NotesArray[j].Note[k]);
}
}
}
// Load the samples.
// Position file pointer first. - might be in the right place, depends on how I read the data above.
for (i = 0; i < NUM_OF_MOD_SAMPLES; i++)
{
// The file pointer will incremented by the 'ModInit' method.
if (TempSampleInfo[i].Length > 0)
{
NEW(ModSampleArray[i], CSample());
ODS("Sample ");
ODI(i);
ODS(" is ");
ODI(TempSampleInfo[i].Length);
ODSN(" bytes long.");
//sprintf(coo, TEXT("Sample %d is %d bytes long."), i, TempSampleInfo[i].Length);
if (!(ModSampleArray[i]->ModInit( FH,
TempSampleInfo[i].Length,
TempSampleInfo[i].RStart,
TempSampleInfo[i].RStart + TempSampleInfo[i].RLength)))
{
// Delete if it failed to init correctly.
SAFELY_DELETE(ModSampleArray[i]);
OutputDebugString(TEXT("Incorrect sample data!\n"));
fclose(FH);
return false;
}
else
{
ODS("Sample ");
ODI(i);
ODS("s repeat is ");
ODI(TempSampleInfo[i].RLength);
ODSN(" bytes long.");
//wsprintf(coo, TEXT("Sample %ds repeat is %d bytes long"), i, TempSampleInfo[i].RStart + TempSampleInfo[i].RLength);
}
}
}
// Close the file.
fclose(FH);
// Init the play back data...
for (i =0; i < 4; i++)
{
// Update note info here.
ModPlayData[i].CurrentNote = 0;
ModPlayData[i].CurrentPosition = 0;
ModPlayData[i].Volume = 64; // Default volume.
DWORD ThisColumnNote = PatternArray[PositionArray[ModPlayData[i].CurrentPosition]].NotesArray[ModPlayData[i].CurrentNote].Note[i];
int Instrument = GET_INSTRUMENT(ThisColumnNote);
// Process instrument info.
if ( (Instrument > 0)
&& (Instrument <= NUM_OF_MOD_SAMPLES))
{
ModPlayData[i].SampleRef = ModSampleArray[Instrument - 1];
ModPlayData[i].Index = 0; // Assumption!
ModPlayData[i].Count = 1; // This too.
ModPlayData[i].PauseRemaining = 0;
}
else
{
if (Instrument != 0)
{
OutputDebugString(TEXT("Invalid instrument\n"));
}
}
// Process pitch info.
if (GET_PITCH(ThisColumnNote) > 0)
{
ModPlayData[i].Pitch = GET_PITCH(ThisColumnNote);
}
for (int l = 0; l < 4; l++) // Scan across all four channels for effects.
{
DWORD ColumnSearch = PatternArray[PositionArray[ModPlayData[i].CurrentPosition]].NotesArray[ModPlayData[i].CurrentNote].Note[l];
// Process effects.
switch (GET_EFFECT_CODE(ColumnSearch))
{
case 0x0A:
if (l == i) // this effect is localised
{
if (GET_EFFECT_PARAM(ColumnSearch) < 0x0F)
{
ModPlayData[i].Volume += (GET_EFFECT_PARAM(ColumnSearch) >> 4);
}
else
{
ModPlayData[i].Volume -= GET_EFFECT_PARAM(ColumnSearch);
}
}
break;
case 0x0B:
// Jump position.
ModPlayData[i].CurrentPosition = GET_EFFECT_PARAM(ColumnSearch);
if (ModPlayData[i].CurrentPosition >= NumberOfPositions)
ModPlayData[i].CurrentPosition = 0;
ModPlayData[i].CurrentNote = 0;
break;
case 0x0C:
if (l == i) // this effect is localised
{
// Change volume - for the particular column.
ModPlayData[i].Volume = GET_EFFECT_PARAM(ColumnSearch);
}
break;
case 0x0D:
// Break pattern.
ModPlayData[i].CurrentPosition++;
if (ModPlayData[i].CurrentPosition >= NumberOfPositions)
ModPlayData[i].CurrentPosition = 0;
ModPlayData[i].CurrentNote = GET_EFFECT_PARAM(ColumnSearch);
break;
case 0x0F:
ModTempo = GET_EFFECT_PARAM(ColumnSearch) - 1;
break;
case 0:
break;
default:
break;
}
}
// Reset the 'timer'.
ModPlayData[i].TimeUntilNextNote = MOD_FRAME; // Blah, blah, blah... (this needs to be finished :o)
}
return true;
}
return false;
}
// ----
void CSoundSystem::PlayMod()
{
// Init the play back data...
for (int i =0; i < 4; i++)
{
// Update note info here.
ModPlayData[i].CurrentNote = 0;
ModPlayData[i].CurrentPosition = 0;
ModPlayData[i].Volume = 64; // Default volume.
DWORD ThisColumnNote = PatternArray[PositionArray[ModPlayData[i].CurrentPosition]].NotesArray[ModPlayData[i].CurrentNote].Note[i];
int Instrument = GET_INSTRUMENT(ThisColumnNote);
// Process instrument info.
if ( (Instrument > 0)
&& (Instrument <= NUM_OF_MOD_SAMPLES))
{
ModPlayData[i].SampleRef = ModSampleArray[Instrument - 1];
ModPlayData[i].Index = 0; // Assumption!
ModPlayData[i].Count = 1; // This too.
ModPlayData[i].PauseRemaining = 0;
}
else
{
if (Instrument != 0)
{
OutputDebugString(TEXT("Invalid instrument\n"));
}
}
// Process pitch info.
if (GET_PITCH(ThisColumnNote) > 0)
{
ModPlayData[i].Pitch = GET_PITCH(ThisColumnNote);
}
for (int l = 0; l < 4; l++) // Scan across all four channels for effects.
{
DWORD ColumnSearch = PatternArray[PositionArray[ModPlayData[i].CurrentPosition]].NotesArray[ModPlayData[i].CurrentNote].Note[l];
// Process effects.
switch (GET_EFFECT_CODE(ColumnSearch))
{
case 0x0A:
if (l == i) // this effect is localised
{
if (GET_EFFECT_PARAM(ColumnSearch) < 0x0F)
{
ModPlayData[i].Volume += (GET_EFFECT_PARAM(ColumnSearch) >> 4);
}
else
{
ModPlayData[i].Volume -= GET_EFFECT_PARAM(ColumnSearch);
}
}
break;
case 0x0B:
// Jump position.
ModPlayData[i].CurrentPosition = GET_EFFECT_PARAM(ColumnSearch);
if (ModPlayData[i].CurrentPosition >= NumberOfPositions)
ModPlayData[i].CurrentPosition = 0;
ModPlayData[i].CurrentNote = 0;
break;
case 0x0C:
if (l == i) // this effect is localised
{
// Change volume - for the particular column.
ModPlayData[i].Volume = GET_EFFECT_PARAM(ColumnSearch);
}
break;
case 0x0D:
// Break pattern.
ModPlayData[i].CurrentPosition++;
if (ModPlayData[i].CurrentPosition >= NumberOfPositions)
ModPlayData[i].CurrentPosition = 0;
ModPlayData[i].CurrentNote = GET_EFFECT_PARAM(ColumnSearch);
break;
case 0x0F:
ModTempo = GET_EFFECT_PARAM(ColumnSearch) - 1;
break;
case 0:
break;
default:
break;
}
}
// Reset the 'timer'.
ModPlayData[i].TimeUntilNextNote = MOD_FRAME; // Blah, blah, blah... (this needs to be finished :o)
}
ModPlaying = true;
}
// ----
bool CSoundSystem::Process()
{
#ifdef DISABLE_AUDIO
return true;
#endif
if (SoundSystemID != 0)
return false;
bool Continue = true;
while (Continue)
{
// Free old buffer.
int unprep = NextBuffer - NUMBER_OF_QUEUED_BUFFERS + 1;
while (unprep < 0)
unprep += MAX_SOUND_BUFFERS;
if (WaveHeaderArray[unprep].dwFlags & WHDR_DONE)
{
// Free cleaned buffer
waveOutUnprepareHeader( DeviceHandle,
&WaveHeaderArray[unprep],
sizeof(WAVEHDR));
// Increment buffer to fill.
NextBuffer++;
if (NextBuffer >= MAX_SOUND_BUFFERS)
NextBuffer = 0;
// Clear buffer.
memset(BufferArray[NextBuffer], 0, 2 * SOUND_BUFFER_LENGTH);
// ----
// Do the individual sound mixing routines.
ProcessAudio();
ProcessMod();
// ----
// Prepare header
WaveHeaderArray[NextBuffer].lpData = (char*)BufferArray[NextBuffer];
WaveHeaderArray[NextBuffer].dwBufferLength = 2 * SOUND_BUFFER_LENGTH;
WaveHeaderArray[NextBuffer].dwFlags = 0;
WaveHeaderArray[NextBuffer].dwUser = 0;
WaveHeaderArray[NextBuffer].dwLoops = 1;
if (waveOutPrepareHeader(DeviceHandle, &WaveHeaderArray[NextBuffer], sizeof(WAVEHDR)) == MMSYSERR_NOERROR)
{
if (waveOutWrite(DeviceHandle, &WaveHeaderArray[NextBuffer], sizeof(WAVEHDR)) == MMSYSERR_NOERROR)
{
//OutputDebugString(TEXT("Primary CSoundSystem: Successfully queued next chunk...\n"));
}
}
}
else
{
Continue = false;
}
}
return true;
}
// ----
// Stop playback of all samples (so that it is safe to
bool CSoundSystem::StopAllSamples()
{
#ifdef DISABLE_AUDIO
return true;
#endif
for (int i = MAX_SIMULTANEOUS_SAMPLES; i--;)
{
PlayData[i].SampleRef = NULL;
PlayData[i].Index = 0;
}
return (waveOutReset(DeviceHandle) == MMSYSERR_NOERROR);
}
// ----
void CSoundSystem::StopMod()
{
#ifdef DISABLE_AUDIO
return;
#endif
ModPlaying = false;
/*
for (int i =0; i < 4; i++)
{
// Update note info here.
ModPlayData[i].CurrentNote = 0;
ModPlayData[i].CurrentPosition = 0;
ModPlayData[i].Volume = 64; // Default volume.
DWORD ThisColumnNote = PatternArray[PositionArray[ModPlayData[i].CurrentPosition]].NotesArray[ModPlayData[i].CurrentNote].Note[i];
int Instrument = GET_INSTRUMENT(ThisColumnNote);
// Process instrument info.
if ( (Instrument > 0)
&& (Instrument <= NUM_OF_MOD_SAMPLES))
{
ModPlayData[i].SampleRef = ModSampleArray[Instrument - 1];
ModPlayData[i].Index = 0; // Assumption!
ModPlayData[i].Count = 1; // This too.
ModPlayData[i].PauseRemaining = 0;
}
else
{
if (Instrument != 0)
{
OutputDebugString(TEXT("Invalid instrument\n"));
}
}
// Process pitch info.
if (GET_PITCH(ThisColumnNote) > 0)
{
ModPlayData[i].Pitch = GET_PITCH(ThisColumnNote);
}
// Reset the 'timer'.
ModPlayData[i].TimeUntilNextNote = MOD_FRAME;
}*/
}
// ----
bool CSoundSystem::SetVolume(int _Volume)
{
#ifdef DISABLE_AUDIO
return true;
#endif
if ( (_Volume < 0)
|| (_Volume > 10))
return false;
// Set volume
DWORD Volume;
Volume = ( (((RIGHT_VOLUME * _Volume * 25) & 0xFFFF) << 16)
| ((LEFT_VOLUME * _Volume * 25) & 0xFFFF));
return (waveOutSetVolume(DeviceHandle, Volume) == MMSYSERR_NOERROR);
}
// ----
#define PITCH_SCALE (3546894.6f * 0.75f)
#define MUSIC_THRESHOLD (8000)
#define PITCH_NEM (33075)
bool CSoundSystem::ProcessAudio()
{
#ifdef DISABLE_AUDIO
return true;
#endif
short* MixingBufferIndex;
short* PlayBufferIndex = NULL;
int AmountToWrite;
short existing;
long additiveL;
long result;
int StepSize;
int Step;
int TotalSteps;
int i;
// For each channel.
for (i = 0; i < MAX_SIMULTANEOUS_SAMPLES; i++)
{
// Grab pointer to main mixing buffer.
MixingBufferIndex = (short*)BufferArray[NextBuffer];
int MixRemaining = SOUND_BUFFER_LENGTH >> 1;
// While room in that buffer...
while (MixRemaining > 0)
{
// Some play back ratio (i.e that affects the pitch).
if (PlayData[i].Pitch > 0)
{
StepSize = /*PITCH_NEM /*/ PlayData[i].Pitch;
}