1 /* Project 16 Source Code~
\r
2 * Copyright (C) 2012-2017 sparky4 & pngwen & andrius4669 & joncampbell123 & yakui-lover
\r
4 * This file is part of Project 16.
\r
6 * Project 16 is free software; you can redistribute it and/or modify
\r
7 * it under the terms of the GNU General Public License as published by
\r
8 * the Free Software Foundation; either version 3 of the License, or
\r
9 * (at your option) any later version.
\r
11 * Project 16 is distributed in the hope that it will be useful,
\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 * GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License
\r
17 * along with this program. If not, see <http://www.gnu.org/licenses/>, or
\r
18 * write to the Free Software Foundation, Inc., 51 Franklin Street,
\r
19 * Fifth Floor, Boston, MA 02110-1301 USA.
\r
25 // ID_SD.c - Sound Manager for Wolfenstein 3D
\r
27 // By Jason Blochowiak
\r
28 // Open Watcom port by sparky4
\r
32 // This module handles dealing with generating sound on the appropriate
\r
35 // Depends on: User Mgr (for parm checking)
\r
39 // SoundSourcePresent - Sound Source thingie present?
\r
40 // SoundBlasterPresent - SoundBlaster card present?
\r
41 // AdLibPresent - AdLib card present?
\r
42 // SoundMode - What device is used for sound effects
\r
43 // (Use SM_SetSoundMode() to set)
\r
44 // MusicMode - What device is used for music
\r
45 // (Use SM_SetMusicMode() to set)
\r
46 // DigiMode - What device is used for digitized sound effects
\r
47 // (Use SM_SetDigiDevice() to set)
\r
50 // NeedsDigitized - load digitized sounds?
\r
51 // NeedsMusic - load music?
\r
54 #pragma hdrstop // Wierdo thing with MUSE
\r
58 #ifndef _MUSE_ // Will be defined in ID_Types.h
\r
59 #include "src/lib/id_sd.h"
\r
61 #include "src/lib/16_head.h"
\r
66 #define SDL_SoundFinished() {SoundNumber = SoundPriority = 0;}
\r
68 // Macros for AdLib stuff
\r
69 #define selreg(n) outportb(alFMAddr,n)
\r
70 #define writereg(n) outportb(alFMData,n)
\r
71 #define readstat() inportb(alFMStatus)
\r
73 #define SD_USECATA3DSETTIMERSPEED
\r
75 // Imports from ID_SD_A.ASM
\r
77 /*extern*/ void SDL_SetDS(void);
\r
78 /*extern*/ void interrupt SDL_t0ExtremeAsmService(void),
\r
79 SDL_t0FastAsmService(void),
\r
80 SDL_t0SlowAsmService(void);
\r
82 void SDL_IndicatePC(boolean on);
\r
85 boolean AdLibPresent,
\r
86 NeedsDigitized,NeedsMusic,
\r
93 word *SoundTable; // Really * _seg *SoundTable, but that don't work
\r
95 int DigiMap[LASTSOUND];
\r
97 // Internal variables
\r
98 static boolean SD_Started;
\r
99 boolean nextsoundpos;
\r
100 word/*boolean_+*/ TimerDone;
\r
101 word TimerVal,TimerDelay10,TimerDelay25,TimerDelay100;
\r
102 dword TimerDivisor,TimerCount;
\r
103 static char *ParmStrings[] =
\r
108 static void (*SoundUserHook)(void);
\r
109 soundnames SoundNumber,DigiNumber;
\r
110 word SoundPriority,DigiPriority;
\r
111 int LeftPosition,RightPosition;
\r
112 void interrupt (*t0OldService)(void);
\r
116 word NumDigi,DigiLeft,DigiPage;
\r
117 word _seg *DigiList;
\r
118 word DigiLastStart,DigiLastEnd;
\r
119 boolean DigiPlaying;
\r
120 static boolean DigiMissed,DigiLastSegment;
\r
121 static memptr DigiNextAddr;
\r
122 static word DigiNextLen;
\r
123 boolean pcindicate;
\r
125 // PC Sound variables
\r
126 volatile byte pcLastSample,far *pcSound;
\r
127 dword pcLengthLeft;
\r
128 word pcSoundLookup[255];
\r
134 dword alLengthLeft;
\r
136 Instrument alZeroInst;
\r
138 // This table maps channel numbers to carrier and modulator op cells
\r
139 static byte carriers[9] = { 3, 4, 5,11,12,13,19,20,21},
\r
140 modifiers[9] = { 0, 1, 2, 8, 9,10,16,17,18},
\r
141 // This table maps percussive voice numbers to op cells
\r
142 pcarriers[5] = {19,0xff,0xff,0xff,0xff},
\r
143 pmodifiers[5] = {16,17,18,20,21};
\r
145 // Sequencer variables
\r
147 static word alFXReg;
\r
148 static ActiveTrack *tracks[sqMaxTracks];//,
\r
149 //-- mytracks[sqMaxTracks];
\r
150 //--static word sqMode,sqFadeStep;
\r
151 word far *sqHack,far *sqHackPtr,sqHackLen,sqHackSeqLen;
\r
154 // Internal routines
\r
155 void SDL_DigitizedDone(void);
\r
157 ///////////////////////////////////////////////////////////////////////////
\r
159 // SDL_SetTimer0() - Sets system timer 0 to the specified speed
\r
161 ///////////////////////////////////////////////////////////////////////////
\r
164 SDL_SetTimer0(word speed)
\r
166 #ifndef TPROF // If using Borland's profiling, don't screw with the timer
\r
170 outportb(0x43,0x36); // Change timer 0
\r
171 outportb(0x40,speed);
\r
172 outportb(0x40,speed >> 8);
\r
173 // Kludge to handle special case for digitized PC sounds
\r
174 if (TimerDivisor == (1192030 / (TickBase * 100)))
\r
175 TimerDivisor = (1192030 / (TickBase * 10));
\r
177 TimerDivisor = speed;
\r
181 TimerDivisor = 0x10000;
\r
185 ///////////////////////////////////////////////////////////////////////////
\r
187 // SDL_SetIntsPerSec() - Uses SDL_SetTimer0() to set the number of
\r
188 // interrupts generated by system timer 0 per second
\r
190 ///////////////////////////////////////////////////////////////////////////
\r
192 SDL_SetIntsPerSec(word ints)
\r
195 SDL_SetTimer0(1192030 / ints);
\r
198 #ifndef SD_USECATA3DSETTIMERSPEED
\r
200 SDL_SetTimerSpeed(void)
\r
203 void interrupt (*isr)(void);
\r
205 if ((DigiMode == sds_PC) && DigiPlaying)
\r
207 rate = TickBase * 100;
\r
208 isr = SDL_t0ExtremeAsmService;
\r
212 (MusicMode == smm_AdLib)
\r
213 || ((DigiMode == sds_SoundSource) && DigiPlaying)
\r
216 rate = TickBase * 10;
\r
217 isr = SDL_t0FastAsmService;
\r
221 rate = TickBase * 2;
\r
222 isr = SDL_t0SlowAsmService;
\r
225 if (rate != TimerRate)
\r
228 SDL_SetIntsPerSec(rate);
\r
234 SDL_SetTimerSpeed(void)
\r
238 if (MusicMode == smm_AdLib)
\r
239 rate = TickBase * 8;
\r
241 rate = TickBase * 2;
\r
242 SDL_SetIntsPerSec(rate);
\r
246 ///////////////////////////////////////////////////////////////////////////
\r
248 // SDL_TimingService() - Used by SDL_InitDelay() to determine a timing
\r
249 // value for the current system that we're running on
\r
251 ///////////////////////////////////////////////////////////////////////////
\r
252 //static void interrupt
\r
254 SDL_TimingService(void)
\r
262 outportb(0x20,0x20); // Ack interrupt
\r
265 ///////////////////////////////////////////////////////////////////////////
\r
267 // SDL_InitDelay() - Sets up TimerDelay's for SDL_Delay()
\r
269 ///////////////////////////////////////////////////////////////////////////
\r
271 SDL_InitDelay(void)
\r
276 setvect(8,SDL_TimingService); // Set to my timer 0 ISR
\r
278 SDL_SetIntsPerSec(1000); // Time 1ms
\r
280 for (i = 0,timer = 0;i < 10;i++) // Do timing test 10 times
\r
283 xor dx,dx // Zero DX
\r
284 mov cx,0xffff // Put starting value in CX
\r
285 mov [TimerDone],cx // TimerDone = false - 1
\r
286 #ifdef __BORLANDC__
\r
290 #ifdef __BORLANDC__
\r
294 jnz startloop // Make sure we're at the start
\r
295 #ifdef __BORLANDC__
\r
299 #ifdef __BORLANDC__
\r
302 test [TimerDone],1 // See if TimerDone flag got hit
\r
303 jnz done // Yep - drop out of the loop
\r
305 #ifdef __BORLANDC__
\r
313 if (0xffff - TimerVal > timer)
\r
314 timer = 0xffff - TimerVal;
\r
316 timer += timer / 2; // Use some slop
\r
317 TimerDelay10 = timer / (1000 / 10);
\r
318 TimerDelay25 = timer / (1000 / 25);
\r
319 TimerDelay100 = timer / (1000 / 100);
\r
321 SDL_SetTimer0(0); // Reset timer 0
\r
323 setvect(8,t0OldService); // Set back to old ISR
\r
326 ///////////////////////////////////////////////////////////////////////////
\r
328 // SDL_Delay() - Delays the specified amount of time
\r
330 ///////////////////////////////////////////////////////////////////////////
\r
332 SDL_Delay(word delay)
\r
339 #ifdef __BORLANDC__
\r
343 #ifdef __BORLANDC__
\r
346 test [TimerDone],0 // Useless code - just for timing equivilency
\r
349 #ifdef __BORLANDC__
\r
362 ///////////////////////////////////////////////////////////////////////////
\r
364 // SDL_PCPlaySample() - Plays the specified sample on the PC speaker
\r
366 ///////////////////////////////////////////////////////////////////////////
\r
372 SDL_PCPlaySample(byte huge *data,dword len)
\r
377 SDL_IndicatePC(true);
\r
379 pcLengthLeft = len;
\r
380 pcSound = (volatile byte far *)data;
\r
385 ///////////////////////////////////////////////////////////////////////////
\r
387 // SDL_PCStopSample() - Stops a sample playing on the PC speaker
\r
389 ///////////////////////////////////////////////////////////////////////////
\r
395 SDL_PCStopSample(void)
\r
400 /*(long)*/pcSound = 0;
\r
402 SDL_IndicatePC(false);
\r
404 asm in al,0x61 // Turn the speaker off
\r
405 asm and al,0xfd // ~2
\r
411 ///////////////////////////////////////////////////////////////////////////
\r
413 // SDL_PCPlaySound() - Plays the specified sound on the PC speaker
\r
415 ///////////////////////////////////////////////////////////////////////////
\r
421 SDL_PCPlaySound(PCSound far *sound)
\r
427 pcLengthLeft = sound->common.length;
\r
428 pcSound = sound->data;
\r
433 ///////////////////////////////////////////////////////////////////////////
\r
435 // SDL_PCStopSound() - Stops the current sound playing on the PC Speaker
\r
437 ///////////////////////////////////////////////////////////////////////////
\r
443 SDL_PCStopSound(void)
\r
448 /*(long)*/pcSound = 0;
\r
450 asm in al,0x61 // Turn the speaker off
\r
451 asm and al,0xfd // ~2
\r
457 ///////////////////////////////////////////////////////////////////////////
\r
459 // SDL_PCService() - Handles playing the next sample in a PC sound
\r
461 ///////////////////////////////////////////////////////////////////////////
\r
463 SDL_PCService(void)
\r
471 if (s != pcLastSample)
\r
477 if (s) // We have a frequency!
\r
479 t = pcSoundLookup[s];
\r
482 asm mov al,0xb6 // Write to channel 2 (speaker) timer
\r
485 asm out 42h,al // Low byte
\r
487 asm out 42h,al // High byte
\r
489 asm in al,0x61 // Turn the speaker & gate on
\r
493 else // Time for some silence
\r
495 asm in al,0x61 // Turn the speaker & gate off
\r
496 asm and al,0xfc // ~3
\r
503 if (!(--pcLengthLeft))
\r
506 SDL_SoundFinished();
\r
511 ///////////////////////////////////////////////////////////////////////////
\r
513 // SDL_ShutPC() - Turns off the pc speaker
\r
515 ///////////////////////////////////////////////////////////////////////////
\r
524 asm in al,0x61 // Turn the speaker & gate off
\r
525 asm and al,0xfc // ~3
\r
532 // Stuff for digitized sounds
\r
535 SDL_LoadDigiSegment(word page, global_game_variables_t *gvar)
\r
539 #if 0 // for debugging
\r
540 asm mov dx,STATUS_REGISTER_1
\r
542 asm mov dx,ATR_INDEX
\r
543 asm mov al,ATR_OVERSCAN
\r
545 asm mov al,10 // bright green
\r
549 addr = PM_GetSoundPage(page);
\r
550 PM_SetPageLock(gvar->pm.fi.PMSoundStart + page,pml_Locked, gvar);
\r
552 #if 0 // for debugging
\r
553 asm mov dx,STATUS_REGISTER_1
\r
555 asm mov dx,ATR_INDEX
\r
556 asm mov al,ATR_OVERSCAN
\r
558 asm mov al,3 // blue
\r
560 asm mov al,0x20 // normal
\r
568 SDL_PlayDigiSegment(memptr addr,word len)
\r
573 SDL_PCPlaySample(addr,len);
\r
575 //SS case sds_SoundSource:
\r
576 //SS SDL_SSPlaySample(addr,len);
\r
578 //SB case sds_SoundBlaster:
\r
579 //SB SDL_SBPlaySample(addr,len);
\r
585 SD_StopDigitized(global_game_variables_t *gvar)
\r
593 DigiNextAddr = nil;
\r
595 DigiMissed = false;
\r
596 DigiPlaying = false;
\r
597 DigiNumber = DigiPriority = 0;
\r
598 SoundPositioned = false;
\r
599 if ((DigiMode == sds_PC) && (SoundMode == sdm_PC))
\r
600 SDL_SoundFinished();
\r
605 SDL_PCStopSample();
\r
607 //SS case sds_SoundSource:
\r
608 //SS SDL_SSStopSample();
\r
610 //SB case sds_SoundBlaster:
\r
611 //SB SDL_SBStopSample();
\r
617 for (i = DigiLastStart;i < DigiLastEnd;i++)
\r
618 PM_SetPageLock(i + gvar->pm.fi.PMSoundStart,pml_Unlocked, gvar);
\r
624 SD_Poll(global_game_variables_t *gvar)
\r
626 if (DigiLeft && !DigiNextAddr)
\r
628 DigiNextLen = (DigiLeft >= PMPageSize)? PMPageSize : (DigiLeft % PMPageSize);
\r
629 DigiLeft -= DigiNextLen;
\r
631 DigiLastSegment = true;
\r
632 DigiNextAddr = SDL_LoadDigiSegment(DigiPage++, gvar);
\r
634 if (DigiMissed && DigiNextAddr)
\r
636 SDL_PlayDigiSegment(DigiNextAddr,DigiNextLen);
\r
637 DigiNextAddr = nil;
\r
638 DigiMissed = false;
\r
639 if (DigiLastSegment)
\r
641 DigiPlaying = false;
\r
642 DigiLastSegment = false;
\r
645 SDL_SetTimerSpeed();
\r
649 SD_SetPosition(int leftpos,int rightpos, global_game_variables_t *gvar)
\r
657 || ((leftpos == 15) && (rightpos == 15))
\r
659 Quit(gvar, "SD_SetPosition: Illegal position");
\r
663 //SB case sds_SoundBlaster:
\r
664 //SB SDL_PositionSBP(leftpos,rightpos);
\r
670 SD_PlayDigitized(word which,int leftpos,int rightpos, global_game_variables_t *gvar)
\r
678 SD_StopDigitized(gvar);
\r
679 if (which >= NumDigi)
\r
680 Quit(gvar, "SD_PlayDigitized: bad sound number");
\r
682 SD_SetPosition(leftpos,rightpos, gvar);
\r
684 DigiPage = DigiList[(which * 2) + 0];
\r
685 DigiLeft = DigiList[(which * 2) + 1];
\r
687 DigiLastStart = DigiPage;
\r
688 DigiLastEnd = DigiPage + ((DigiLeft + (PMPageSize - 1)) / PMPageSize);
\r
690 len = (DigiLeft >= PMPageSize)? PMPageSize : (DigiLeft % PMPageSize);
\r
691 addr = SDL_LoadDigiSegment(DigiPage++, gvar);
\r
693 DigiPlaying = true;
\r
694 DigiLastSegment = false;
\r
696 SDL_PlayDigiSegment(addr,len);
\r
699 DigiLastSegment = true;
\r
705 SDL_DigitizedDone(void)
\r
709 SDL_PlayDigiSegment(DigiNextAddr,DigiNextLen);
\r
710 DigiNextAddr = nil;
\r
711 DigiMissed = false;
\r
715 if (DigiLastSegment)
\r
717 DigiPlaying = false;
\r
718 DigiLastSegment = false;
\r
719 if ((DigiMode == sds_PC) && (SoundMode == sdm_PC))
\r
721 SDL_SoundFinished();
\r
724 DigiNumber = DigiPriority = 0;
\r
725 SoundPositioned = false;
\r
733 SD_SetDigiDevice(SDSMode mode, global_game_variables_t *gvar)
\r
735 boolean devicenotpresent;
\r
737 if (mode == DigiMode)
\r
740 SD_StopDigitized(gvar);
\r
742 devicenotpresent = false;
\r
745 case sds_SoundBlaster:
\r
746 if (!SoundBlasterPresent)
\r
748 if (SoundSourcePresent)
\r
749 mode = sds_SoundSource;
\r
751 devicenotpresent = true;
\r
754 case sds_SoundSource:
\r
755 if (!SoundSourcePresent)
\r
756 devicenotpresent = true;
\r
760 if (!devicenotpresent)
\r
762 //SS if (DigiMode == sds_SoundSource)
\r
767 //SS if (mode == sds_SoundSource)
\r
768 //SS SDL_StartSS();
\r
770 SDL_SetTimerSpeed();
\r
775 SDL_SetupDigi(global_game_variables_t *gvar)
\r
782 PM_UnlockMainMem(gvar);
\r
783 MM_GetPtr(&list,PMPageSize, gvar);
\r
784 PM_CheckMainMem(gvar);
\r
785 p = (word far *)MK_FP(PM_GetPage(gvar->pm.fi.ChunksInFile - 1, gvar),0);
\r
786 _fmemcpy((void far *)list,(void far *)p,PMPageSize);
\r
787 pg = gvar->pm.fi.PMSoundStart;
\r
788 for (i = 0;i < PMPageSize / (sizeof(word) * 2);i++,p += 2)
\r
790 if (pg >= gvar->pm.fi.ChunksInFile - 1)
\r
792 pg += (p[1] + (PMPageSize - 1)) / PMPageSize;
\r
794 PM_UnlockMainMem(gvar);
\r
795 MM_GetPtr(MEMPTRCONV DigiList,i * sizeof(word) * 2, gvar);
\r
796 _fmemcpy((void far *)DigiList,(void far *)list,i * sizeof(word) * 2);
\r
797 MM_FreePtr(&list, gvar);
\r
800 for (i = 0;i < LASTSOUND;i++)
\r
806 ///////////////////////////////////////////////////////////////////////////
\r
808 // alOut(n,b) - Puts b in AdLib card register n
\r
810 ///////////////////////////////////////////////////////////////////////////
\r
812 alOut(byte n,byte b)
\r
821 SDL_Delay(TimerDelay10);
\r
838 SDL_Delay(TimerDelay25);
\r
883 ///////////////////////////////////////////////////////////////////////////
\r
885 // SDL_SetInstrument() - Puts an instrument into a generator
\r
887 ///////////////////////////////////////////////////////////////////////////
\r
890 SDL_SetInstrument(int track,int which,Instrument far *inst,boolean percussive)
\r
896 c = pcarriers[which];
\r
897 m = pmodifiers[which];
\r
901 c = carriers[which];
\r
902 m = modifiers[which];
\r
905 tracks[track - 1]->inst = *inst;
\r
906 tracks[track - 1]->percussive = percussive;
\r
908 alOut(m + alChar,inst->mChar);
\r
909 alOut(m + alScale,inst->mScale);
\r
910 alOut(m + alAttack,inst->mAttack);
\r
911 alOut(m + alSus,inst->mSus);
\r
912 alOut(m + alWave,inst->mWave);
\r
914 // Most percussive instruments only use one cell
\r
917 alOut(c + alChar,inst->cChar);
\r
918 alOut(c + alScale,inst->cScale);
\r
919 alOut(c + alAttack,inst->cAttack);
\r
920 alOut(c + alSus,inst->cSus);
\r
921 alOut(c + alWave,inst->cWave);
\r
924 alOut(which + alFeedCon,inst->nConn); // DEBUG - I think this is right
\r
928 ///////////////////////////////////////////////////////////////////////////
\r
930 // SDL_ALStopSound() - Turns off any sound effects playing through the
\r
933 ///////////////////////////////////////////////////////////////////////////
\r
939 SDL_ALStopSound(void)
\r
944 /*(long)*/alSound = 0;
\r
945 alOut(alFreqH + 0,0);
\r
951 SDL_AlSetFXInst(Instrument far *inst)
\r
957 alOut(m + alChar,inst->mChar);
\r
958 alOut(m + alScale,inst->mScale);
\r
959 alOut(m + alAttack,inst->mAttack);
\r
960 alOut(m + alSus,inst->mSus);
\r
961 alOut(m + alWave,inst->mWave);
\r
962 alOut(c + alChar,inst->cChar);
\r
963 alOut(c + alScale,inst->cScale);
\r
964 alOut(c + alAttack,inst->cAttack);
\r
965 alOut(c + alSus,inst->cSus);
\r
966 alOut(c + alWave,inst->cWave);
\r
968 // Note: Switch commenting on these lines for old MUSE compatibility
\r
969 // alOut(alFeedCon,inst->nConn);
\r
970 alOut(alFeedCon,0);
\r
973 ///////////////////////////////////////////////////////////////////////////
\r
975 // SDL_ALPlaySound() - Plays the specified sound on the AdLib card
\r
977 ///////////////////////////////////////////////////////////////////////////
\r
983 SDL_ALPlaySound(AdLibSound far *sound, global_game_variables_t *gvar)
\r
985 Instrument __far *inst;
\r
993 alLengthLeft = sound->common.length;
\r
994 data = sound->data;
\r
997 alSound = (byte far *)data;
\r
998 alBlock = ((sound->block & 7) << 2) | 0x20;
\r
999 inst = &sound->inst;
\r
1001 if (!(inst->mSus | inst->cSus))
\r
1004 Quit(gvar, "SDL_ALPlaySound() - Bad instrument");
\r
1007 SDL_AlSetFXInst(&alZeroInst); // DEBUG
\r
1008 SDL_AlSetFXInst(inst);
\r
1014 ///////////////////////////////////////////////////////////////////////////
\r
1016 // SDL_ALSoundService() - Plays the next sample out through the AdLib card
\r
1018 ///////////////////////////////////////////////////////////////////////////
\r
1021 SDL_ALSoundService(void)
\r
1029 alOut(alFreqH + 0,0);
\r
1032 alOut(alFreqL + 0,s);
\r
1033 alOut(alFreqH + 0,alBlock);
\r
1036 if (!(--alLengthLeft))
\r
1038 /*(long)*/alSound = 0;
\r
1039 alOut(alFreqH + 0,0);
\r
1040 SDL_SoundFinished();
\r
1047 ///////////////////////////////////////////////////////////////////////////
\r
1049 // SDL_SelectMeasure() - sets up sequencing variables for a given track
\r
1051 ///////////////////////////////////////////////////////////////////////////
\r
1053 SDL_SelectMeasure(ActiveTrack *track)
\r
1055 track->seq = track->moods[track->mood];
\r
1056 track->nextevent = 0;
\r
1062 SDL_ALService(void)
\r
1072 while (sqHackLen && (sqHackTime <= alTimeCount))
\r
1075 sqHackTime = alTimeCount + *sqHackPtr++;
\r
1085 sqHackPtr = (word far *)sqHack;
\r
1086 sqHackLen = sqHackSeqLen;
\r
1087 alTimeCount = sqHackTime = 0;
\r
1092 ///////////////////////////////////////////////////////////////////////////
\r
1094 // SDL_ShutAL() - Shuts down the AdLib card for sound effects
\r
1096 ///////////////////////////////////////////////////////////////////////////
\r
1103 alOut(alEffects,0);
\r
1104 alOut(alFreqH + 0,0);
\r
1105 SDL_AlSetFXInst(&alZeroInst);
\r
1111 ///////////////////////////////////////////////////////////////////////////
\r
1113 // SDL_CleanAL() - Totally shuts down the AdLib card
\r
1115 ///////////////////////////////////////////////////////////////////////////
\r
1124 alOut(alEffects,0);
\r
1125 for (i = 1;i < 0xf5;i++)
\r
1131 ///////////////////////////////////////////////////////////////////////////
\r
1133 // SDL_StartAL() - Starts up the AdLib card for sound effects
\r
1135 ///////////////////////////////////////////////////////////////////////////
\r
1140 alOut(alEffects,alFXReg);
\r
1141 SDL_AlSetFXInst(&alZeroInst);
\r
1144 ///////////////////////////////////////////////////////////////////////////
\r
1146 // SDL_DetectAdLib() - Determines if there's an AdLib (or SoundBlaster
\r
1147 // emulating an AdLib) present
\r
1149 ///////////////////////////////////////////////////////////////////////////
\r
1151 SDL_DetectAdLib(void)
\r
1153 byte status1,status2;
\r
1156 alOut(4,0x60); // Reset T1 & T2
\r
1157 alOut(4,0x80); // Reset IRQ
\r
1158 status1 = readstat();
\r
1159 alOut(2,0xff); // Set timer 1
\r
1160 alOut(4,0x21); // Start timer 1
\r
1161 SDL_Delay(TimerDelay100);
\r
1167 #ifdef __BORLANDC__
\r
1171 #ifdef __BORLANDC__
\r
1179 status2 = readstat();
\r
1183 if (((status1 & 0xe0) == 0x00) && ((status2 & 0xe0) == 0xc0))
\r
1185 for (i = 1;i <= 0xf5;i++) // Zero all the registers
\r
1188 alOut(1,0x20); // Set WSE=1
\r
1189 alOut(8,0); // Set CSM=0 & SEL=0
\r
1197 ///////////////////////////////////////////////////////////////////////////
\r
1199 // SDL_t0Service() - My timer 0 ISR which handles the different timings and
\r
1200 // dispatches to whatever other routines are appropriate
\r
1202 ///////////////////////////////////////////////////////////////////////////
\r
1203 //static void interrupt
\r
1205 SDL_t0Service(void)
\r
1207 static word count = 1;
\r
1208 boolean myackflag = 0;
\r
1210 //00#if 0 // for debugging
\r
1211 asm mov dx,STATUS_REGISTER_1
\r
1213 asm mov dx,ATR_INDEX
\r
1214 asm mov al,ATR_OVERSCAN
\r
1216 asm mov al,4 // red
\r
1222 if ((MusicMode == smm_AdLib) || (DigiMode == sds_SoundSource))
\r
1225 //SS SDL_SSService();
\r
1226 // if (!(++count & 7))
\r
1227 if (!(++count % 10))
\r
1231 if (SoundUserHook)
\r
1234 // if (!(count & 3))
\r
1237 switch (SoundMode)
\r
1243 SDL_ALSoundService();
\r
1250 if (!(++count & 1))
\r
1254 if (SoundUserHook)
\r
1257 switch (SoundMode)
\r
1263 SDL_ALSoundService();
\r
1269 mov ax,[WORD PTR TimerCount]
\r
1270 add ax,[WORD PTR TimerDivisor]
\r
1271 mov [WORD PTR TimerCount],ax
\r
1274 #ifdef __BORLANDC__
\r
1278 #ifdef __BORLANDC__
\r
1282 #ifdef __BORLANDC__
\r
1286 #ifdef __WATCOMC__
\r
1290 t0OldService(); // If we overflow a word, time to call old int handler
\r
1292 outportb(0x20,0x20); // Ack the interrupt
\r
1294 //00#if 0 // for debugging
\r
1295 asm mov dx,STATUS_REGISTER_1
\r
1297 asm mov dx,ATR_INDEX
\r
1298 asm mov al,ATR_OVERSCAN
\r
1300 asm mov al,3 // blue
\r
1302 asm mov al,0x20 // normal
\r
1307 ////////////////////////////////////////////////////////////////////////////
\r
1309 // SDL_ShutDevice() - turns off whatever device was being used for sound fx
\r
1311 ////////////////////////////////////////////////////////////////////////////
\r
1313 SDL_ShutDevice(void)
\r
1315 switch (SoundMode)
\r
1324 SoundMode = sdm_Off;
\r
1327 ///////////////////////////////////////////////////////////////////////////
\r
1329 // SDL_CleanDevice() - totally shuts down all sound devices
\r
1331 ///////////////////////////////////////////////////////////////////////////
\r
1333 SDL_CleanDevice(void)
\r
1335 if ((SoundMode == sdm_AdLib) || (MusicMode == smm_AdLib))
\r
1339 ///////////////////////////////////////////////////////////////////////////
\r
1341 // SDL_StartDevice() - turns on whatever device is to be used for sound fx
\r
1343 ///////////////////////////////////////////////////////////////////////////
\r
1345 SDL_StartDevice(void)
\r
1347 switch (SoundMode)
\r
1353 SoundNumber = SoundPriority = 0;
\r
1357 SDL_SetTimerSpeed(void)
\r
1361 if (MusicMode == smm_AdLib)
\r
1362 rate = TickBase * 8;
\r
1364 rate = TickBase * 2;
\r
1365 SDL_SetIntsPerSec(rate);
\r
1368 // Public routines
\r
1370 ///////////////////////////////////////////////////////////////////////////
\r
1372 // SD_SetSoundMode() - Sets which sound hardware to use for sound effects
\r
1374 ///////////////////////////////////////////////////////////////////////////
\r
1376 SD_SetSoundMode(SDMode mode, global_game_variables_t *gvar)
\r
1378 boolean result = false;
\r
1381 SD_StopSound(gvar);
\r
1384 if ((mode == sdm_AdLib) && !AdLibPresent)
\r
1390 NeedsDigitized = false;
\r
1394 tableoffset = STARTPCSOUNDS;
\r
1395 NeedsDigitized = false;
\r
1401 tableoffset = STARTADLIBSOUNDS;
\r
1402 NeedsDigitized = false;
\r
1411 if (result && (mode != SoundMode))
\r
1416 SoundTable = (word *)(&gvar->ca.audiosegs[tableoffset]);
\r
1418 SDL_StartDevice();
\r
1421 SDL_SetTimerSpeed();
\r
1426 ///////////////////////////////////////////////////////////////////////////
\r
1428 // SD_SetMusicMode() - sets the device to use for background music
\r
1430 ///////////////////////////////////////////////////////////////////////////
\r
1432 SD_SetMusicMode(SMMode mode)
\r
1434 boolean result = false;
\r
1436 SD_FadeOutMusic();
\r
1437 while (SD_MusicPlaying())
\r
1443 NeedsMusic = false;
\r
1449 NeedsMusic = true;
\r
1458 SDL_SetTimerSpeed();
\r
1463 ///////////////////////////////////////////////////////////////////////////
\r
1465 // SD_Startup() - starts up the Sound Mgr
\r
1466 // Detects all additional sound hardware and installs my ISR
\r
1468 ///////////////////////////////////////////////////////////////////////////
\r
1470 SD_Startup(global_game_variables_t *gvar)
\r
1479 ssIsTandy = false;
\r
1480 //SS ssNoCheck = false;
\r
1481 alNoCheck = false;
\r
1482 //SB sbNoCheck = false;
\r
1483 //SB sbNoProCheck = false;
\r
1485 for (i = 1;i < _argc;i++)
\r
1487 switch (US_CheckParm(_argv[i],ParmStrings))
\r
1489 case 0: // No AdLib detection
\r
1492 //SB case 1: // No SoundBlaster detection
\r
1493 //SB sbNoCheck = true;
\r
1495 //SB case 2: // No SoundBlaster Pro detection
\r
1496 //SB sbNoProCheck = true;
\r
1499 //SS ssNoCheck = true; // No Sound Source detection
\r
1501 case 4: // Tandy Sound Source handling
\r
1504 //SS case 5: // Sound Source present at LPT1
\r
1506 //SS ssNoCheck = SoundSourcePresent = true;
\r
1508 //SS case 6: // Sound Source present at LPT2
\r
1510 //SS ssNoCheck = SoundSourcePresent = true;
\r
1512 //SS case 7: // Sound Source present at LPT3
\r
1514 //SS ssNoCheck = SoundSourcePresent = true;
\r
1520 SoundUserHook = 0;
\r
1522 t0OldService = getvect(8); // Get old timer 0 ISR
\r
1524 SDL_InitDelay(); // SDL_InitDelay() uses t0OldService
\r
1526 setvect(8,SDL_t0Service); // Set to my timer 0 ISR
\r
1527 LocalTime = TimeCount = alTimeCount = 0;
\r
1529 SD_SetSoundMode(sdm_Off, gvar);
\r
1530 SD_SetMusicMode(smm_Off);
\r
1532 //SS if (!ssNoCheck)
\r
1533 //SS SoundSourcePresent = SDL_DetectSoundSource();
\r
1537 AdLibPresent = SDL_DetectAdLib();
\r
1538 //SB if (AdLibPresent) && !sbNoCheck)
\r
1540 //SB int port = -1;
\r
1541 //SB char *env = getenv("BLASTER");
\r
1547 //SB while (isspace(*env))
\r
1550 //SB switch (toupper(*env))
\r
1553 //SB temp = strtol(env + 1,&env,16);
\r
1556 //SB (temp >= 0x210)
\r
1557 //SB && (temp <= 0x260)
\r
1558 //SB && (!(temp & 0x00f))
\r
1560 //SB port = (temp - 0x200) >> 4;
\r
1562 //SB Quit(gvar, "SD_Startup: Unsupported address value in BLASTER");
\r
1565 //SB temp = strtol(env + 1,&env,10);
\r
1569 //SB && (temp <= 10)
\r
1570 //SB && (sbIntVectors[temp] != -1)
\r
1573 //SB sbInterrupt = temp;
\r
1574 //SB sbIntVec = sbIntVectors[sbInterrupt];
\r
1577 //SB Quit(gvar, "SD_Startup: Unsupported interrupt value in BLASTER");
\r
1580 //SB temp = strtol(env + 1,&env,10);
\r
1581 //SB if ((temp == 0) || (temp == 1) || (temp == 3))
\r
1582 //SB SDL_SBSetDMA(temp);
\r
1584 //SB Quit(gvar, "SD_Startup: Unsupported DMA value in BLASTER");
\r
1587 //SB while (isspace(*env))
\r
1589 //SB while (*env && !isspace(*env))
\r
1595 //SB SoundBlasterPresent = SDL_DetectSoundBlaster(port);
\r
1599 for (i = 0;i < 255;i++)
\r
1600 pcSoundLookup[i] = i * 60;
\r
1602 //SB if (SoundBlasterPresent)
\r
1603 //SB SDL_StartSB();
\r
1605 SDL_SetupDigi(gvar);
\r
1607 SD_Started = true;
\r
1610 ///////////////////////////////////////////////////////////////////////////
\r
1612 // SD_Default() - Sets up the default behaviour for the Sound Mgr whether
\r
1613 // the config file was present or not.
\r
1615 ///////////////////////////////////////////////////////////////////////////
\r
1617 SD_Default(boolean gotit,SDMode sd,SMMode sm, global_game_variables_t *gvar)
\r
1619 boolean gotsd,gotsm;
\r
1621 gotsd = gotsm = gotit;
\r
1623 if (gotsd) // Make sure requested sound hardware is available
\r
1628 gotsd = AdLibPresent;
\r
1639 if (sd != SoundMode)
\r
1640 SD_SetSoundMode(sd, gvar);
\r
1643 if (gotsm) // Make sure requested music hardware is available
\r
1648 gotsm = AdLibPresent;
\r
1657 if (sm != MusicMode)
\r
1658 SD_SetMusicMode(sm);
\r
1661 ///////////////////////////////////////////////////////////////////////////
\r
1663 // SD_Shutdown() - shuts down the Sound Mgr
\r
1664 // Removes sound ISR and turns off whatever sound hardware was active
\r
1666 ///////////////////////////////////////////////////////////////////////////
\r
1668 SD_Shutdown(global_game_variables_t *gvar)
\r
1674 SD_StopSound(gvar);
\r
1676 SDL_CleanDevice();
\r
1678 //SB if (SoundBlasterPresent)
\r
1679 //SB SDL_ShutSB();
\r
1681 //SS if (SoundSourcePresent)
\r
1682 //SS SDL_ShutSS();
\r
1689 setvect(8,t0OldService);
\r
1693 SD_Started = false;
\r
1696 ///////////////////////////////////////////////////////////////////////////
\r
1698 // SD_SetUserHook() - sets the routine that the Sound Mgr calls every 1/70th
\r
1699 // of a second from its timer 0 ISR
\r
1701 ///////////////////////////////////////////////////////////////////////////
\r
1703 SD_SetUserHook(void (* hook)(void))
\r
1705 SoundUserHook = hook;
\r
1708 ///////////////////////////////////////////////////////////////////////////
\r
1710 // SD_PositionSound() - Sets up a stereo imaging location for the next
\r
1711 // sound to be played. Each channel ranges from 0 to 15.
\r
1713 ///////////////////////////////////////////////////////////////////////////
\r
1715 SD_PositionSound(int leftvol,int rightvol)
\r
1717 LeftPosition = leftvol;
\r
1718 RightPosition = rightvol;
\r
1719 nextsoundpos = true;
\r
1722 ///////////////////////////////////////////////////////////////////////////
\r
1724 // SD_PlaySound() - plays the specified sound on the appropriate hardware
\r
1726 ///////////////////////////////////////////////////////////////////////////
\r
1728 SD_PlaySound(soundnames sound, global_game_variables_t *gvar)
\r
1731 SoundCommon far *s;
\r
1734 lp = LeftPosition;
\r
1735 rp = RightPosition;
\r
1737 RightPosition = 0;
\r
1739 ispos = nextsoundpos;
\r
1740 nextsoundpos = false;
\r
1745 s = MK_FP(SoundTable[sound],0);
\r
1746 if ((SoundMode != sdm_Off) && !s)
\r
1747 Quit(gvar, "SD_PlaySound() - Uncached sound");
\r
1749 if ((DigiMode != sds_Off) && (DigiMap[sound] != -1))
\r
1751 if ((DigiMode == sds_PC) && (SoundMode == sdm_PC))
\r
1753 if (s->priority < SoundPriority)
\r
1756 SDL_PCStopSound();
\r
1758 SD_PlayDigitized(DigiMap[sound],lp,rp, gvar);
\r
1759 SoundPositioned = ispos;
\r
1760 SoundNumber = sound;
\r
1761 SoundPriority = s->priority;
\r
1767 if (DigiPriority && !DigiNumber)
\r
1770 Quit(gvar, "SD_PlaySound: Priority without a sound");
\r
1774 if (s->priority < DigiPriority)
\r
1777 SD_PlayDigitized(DigiMap[sound],lp,rp, gvar);
\r
1778 SoundPositioned = ispos;
\r
1779 DigiNumber = sound;
\r
1780 DigiPriority = s->priority;
\r
1786 if (SoundMode == sdm_Off)
\r
1789 Quit(gvar, "SD_PlaySound() - Zero length sound");
\r
1790 if (s->priority < SoundPriority)
\r
1793 switch (SoundMode)
\r
1796 SDL_PCPlaySound((void far *)s);
\r
1799 SDL_ALPlaySound((void far *)s, gvar);
\r
1803 SoundNumber = sound;
\r
1804 SoundPriority = s->priority;
\r
1809 ///////////////////////////////////////////////////////////////////////////
\r
1811 // SD_SoundPlaying() - returns the sound number that's playing, or 0 if
\r
1812 // no sound is playing
\r
1814 ///////////////////////////////////////////////////////////////////////////
\r
1816 SD_SoundPlaying(void)
\r
1818 boolean result = false;
\r
1820 switch (SoundMode)
\r
1823 result = pcSound? true : false;
\r
1826 result = alSound? true : false;
\r
1831 return(SoundNumber);
\r
1836 ///////////////////////////////////////////////////////////////////////////
\r
1838 // SD_StopSound() - if a sound is playing, stops it
\r
1840 ///////////////////////////////////////////////////////////////////////////
\r
1842 SD_StopSound(global_game_variables_t *gvar)
\r
1845 SD_StopDigitized(gvar);
\r
1847 switch (SoundMode)
\r
1850 SDL_PCStopSound();
\r
1853 SDL_ALStopSound();
\r
1857 SoundPositioned = false;
\r
1859 SDL_SoundFinished();
\r
1862 ///////////////////////////////////////////////////////////////////////////
\r
1864 // SD_WaitSoundDone() - waits until the current sound is done playing
\r
1866 ///////////////////////////////////////////////////////////////////////////
\r
1868 SD_WaitSoundDone(void)
\r
1870 while (SD_SoundPlaying())
\r
1874 ///////////////////////////////////////////////////////////////////////////
\r
1876 // SD_MusicOn() - turns on the sequencer
\r
1878 ///////////////////////////////////////////////////////////////////////////
\r
1885 ///////////////////////////////////////////////////////////////////////////
\r
1887 // SD_MusicOff() - turns off the sequencer and any playing notes
\r
1889 ///////////////////////////////////////////////////////////////////////////
\r
1896 switch (MusicMode)
\r
1900 alOut(alEffects,0);
\r
1901 for (i = 0;i < sqMaxTracks;i++)
\r
1902 alOut(alFreqH + i + 1,0);
\r
1908 ///////////////////////////////////////////////////////////////////////////
\r
1910 // SD_StartMusic() - starts playing the music pointed to
\r
1912 ///////////////////////////////////////////////////////////////////////////
\r
1914 SD_StartMusic(MusicGroup far *music)
\r
1920 if (MusicMode == smm_AdLib)
\r
1922 sqHackPtr = sqHack = music->values;
\r
1923 sqHackSeqLen = sqHackLen = music->length;
\r
1932 ///////////////////////////////////////////////////////////////////////////
\r
1934 // SD_FadeOutMusic() - starts fading out the music. Call SD_MusicPlaying()
\r
1935 // to see if the fadeout is complete
\r
1937 ///////////////////////////////////////////////////////////////////////////
\r
1939 SD_FadeOutMusic(void)
\r
1941 switch (MusicMode)
\r
1944 // DEBUG - quick hack to turn the music off
\r
1950 ///////////////////////////////////////////////////////////////////////////
\r
1952 // SD_MusicPlaying() - returns true if music is currently playing, false if
\r
1955 ///////////////////////////////////////////////////////////////////////////
\r
1957 SD_MusicPlaying(void)
\r
1961 switch (MusicMode)
\r
1965 // DEBUG - not written
\r
1985 void SDL_turnOnPCSpeaker(word timerval)
\r
2001 void SDL_turnOffPCSpeaker()
\r
2010 void SDL_setPCSpeaker(byte val)
\r
2025 if(*pcSound!=pcLastSample)
\r
2027 pcLastSample=*pcSound;
\r
2030 SDL_turnOnPCSpeaker(pcLastSample*60);
\r
2032 SDL_turnOffPCSpeaker();
\r
2039 SoundNumber=(soundnames)0;
\r
2041 SDL_turnOffPCSpeaker();
\r
2045 if(alSound && !alNoIRQ)
\r
2049 alOutInIRQ(alFreqL,*alSound);
\r
2050 alOutInIRQ(alFreqH,alBlock);
\r
2052 else alOutInIRQ(alFreqH,0);
\r
2058 SoundNumber=(soundnames)0;
\r
2060 alOutInIRQ(alFreqH,0);
\r
2083 if(sqActive && !alNoIRQ)
\r
2089 if(sqHackTime>alTimeCount) break;
\r
2090 sqHackTime=alTimeCount+*(sqHackPtr+1);
\r
2091 alOutInIRQ(*(byte *)sqHackPtr,*(((byte *)sqHackPtr)+1));
\r
2101 sqHackLen=sqHackSeqLen;
\r
2109 if(!(inp(ssStatus)&0x40))
\r
2111 outp(ssData,*ssSample++);
\r
2112 outp(ssControl,ssOff);
\r
2115 outp(ssControl,ssOn);
\r
2122 SDL_DigitizedDoneInIRQ();
\r
2127 TimerCount+=TimerDivisor;
\r
2128 if(*((word *)&TimerCount+1))
\r
2130 *((word *)&TimerCount+1)=0;
\r
2139 // Timer 0 ISR for 7000Hz interrupts
\r
2140 void interrupt SDL_t0ExtremeAsmService(void)
\r
2146 SDL_setPCSpeaker(((*pcSound++)&0x80)>>6);
\r
2151 SDL_turnOffPCSpeaker();
\r
2152 SDL_DigitizedDoneInIRQ();
\r
2166 // Timer 0 ISR for 7000Hz interrupts
\r
2167 void interrupt __SDL_t0ExtremeAsmService()
\r
2173 SDL_setPCSpeaker(((*pcSound++)&0x80)>>6);
\r
2178 SDL_turnOffPCSpeaker();
\r
2179 SDL_DigitizedDoneInIRQ();
\r
2193 // Timer 0 ISR for 700Hz interrupts
\r
2194 void interrupt SDL_t0FastAsmService(void)
\r
2199 // Timer 0 ISR for 140Hz interrupts
\r
2200 void interrupt SDL_t0SlowAsmService(void)
\r
2211 TimerCount+=TimerDivisor;
\r
2212 if(*((word *)&TimerCount+1))
\r
2214 *((word *)&TimerCount+1)=0;
\r
2221 void SDL_IndicatePC(boolean ind)
\r
2227 SDL_DigitizedDoneInIRQ(void)
\r
2231 SDL_PlayDigiSegment(DigiNextAddr,DigiNextLen/*,true*/);
\r
2232 DigiNextAddr = nil;
\r
2233 DigiMissed = false;
\r
2237 if (DigiLastSegment)
\r
2239 DigiPlaying = false;
\r
2240 DigiLastSegment = false;
\r
2241 if ((DigiMode == sds_PC) && (SoundMode == sdm_PC))
\r
2243 SDL_SoundFinished();
\r
2247 DigiNumber = (soundnames) 0;
\r
2250 SoundPositioned = false;
\r
2253 DigiMissed = true;
\r
2257 // Inside an interrupt handler interrupts should already be disabled
\r
2258 // so don't disable them again and cause V86 exceptions which cost
\r
2259 // aprox. 300 processor tics!
\r
2262 void alOutInIRQ(byte n,byte b)
\r