1 /* Project 16 Source Code~
\r
2 * Copyright (C) 2012-2022 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/16_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 // Imports from ID_SD_A.ASM
\r
74 /*extern*/ void SDL_SetDS(void);
\r
75 /*extern*/ void interrupt SDL_t0ExtremeAsmService(void);//,
\r
76 // SDL_t0FastAsmService(void),
\r
77 // SDL_t0SlowAsmService(void);
\r
78 void SDL_IndicatePC(boolean on),
\r
79 SDL_DigitizedDoneInIRQ();
\r
82 boolean AdLibPresent,
\r
83 NeedsDigitized,NeedsMusic,
\r
90 word *SoundTable; // Really * _seg *SoundTable, but that don't work
\r
92 int DigiMap[LASTSOUND];
\r
94 // Internal variables
\r
95 static boolean SD_Started;
\r
96 boolean nextsoundpos;
\r
97 word/*boolean_+*/ TimerDone;
\r
98 word TimerVal,TimerDelay10,TimerDelay25,TimerDelay100;
\r
99 dword TimerDivisor,TimerCount;
\r
100 static char *ParmStrings[] =
\r
105 static void (*SoundUserHook)(void);
\r
106 soundnames SoundNumber,DigiNumber;
\r
107 word SoundPriority,DigiPriority;
\r
108 int LeftPosition,RightPosition;
\r
109 void interrupt (*t0OldService)(void);
\r
113 word NumDigi,DigiLeft,DigiPage;
\r
114 word _seg *DigiList;
\r
115 word DigiLastStart,DigiLastEnd;
\r
116 boolean DigiPlaying;
\r
117 static boolean DigiMissed,DigiLastSegment;
\r
118 static memptr DigiNextAddr;
\r
119 static word DigiNextLen;
\r
121 // PC Sound variables
\r
122 volatile byte pcLastSample,far *pcSound;
\r
123 dword pcLengthLeft;
\r
124 word pcSoundLookup[255];
\r
130 dword alLengthLeft;
\r
132 Instrument alZeroInst;
\r
134 // This table maps channel numbers to carrier and modulator op cells
\r
135 static byte carriers[9] = { 3, 4, 5,11,12,13,19,20,21},
\r
136 modifiers[9] = { 0, 1, 2, 8, 9,10,16,17,18},
\r
137 // This table maps percussive voice numbers to op cells
\r
138 pcarriers[5] = {19,0xff,0xff,0xff,0xff},
\r
139 pmodifiers[5] = {16,17,18,20,21};
\r
141 // Sequencer variables
\r
143 static word alFXReg;
\r
144 static ActiveTrack *tracks[sqMaxTracks];//,
\r
145 //-- mytracks[sqMaxTracks];
\r
146 //--static word sqMode,sqFadeStep;
\r
147 word far *sqHack,far *sqHackPtr,sqHackLen,sqHackSeqLen;
\r
150 // Internal routines
\r
151 void SDL_DigitizedDone(void);
\r
153 //Assembly functions
\r
160 void SDL_turnOnPCSpeaker(word timerval)
\r
176 void SDL_turnOffPCSpeaker()
\r
185 void SDL_setPCSpeaker(byte val)
\r
200 if(*pcSound!=pcLastSample)
\r
202 pcLastSample=*pcSound;
\r
205 SDL_turnOnPCSpeaker(pcLastSample*60);
\r
207 SDL_turnOffPCSpeaker();
\r
214 SoundNumber=(soundnames)0;
\r
216 SDL_turnOffPCSpeaker();
\r
220 if(alSound && !alNoIRQ)
\r
224 alOut/*InIRQ*/(alFreqL,*alSound);
\r
225 alOut/*InIRQ*/(alFreqH,alBlock);
\r
227 else alOut/*InIRQ*/(alFreqH,0);
\r
233 SoundNumber=(soundnames)0;
\r
235 alOut/*InIRQ*/(alFreqH,0);
\r
258 if(sqActive && !alNoIRQ)
\r
264 if(sqHackTime>alTimeCount) break;
\r
265 sqHackTime=alTimeCount+*(sqHackPtr+1);
\r
266 alOut/*InIRQ*/(*(byte *)sqHackPtr,*(((byte *)sqHackPtr)+1));
\r
276 sqHackLen=sqHackSeqLen;
\r
284 //SS if(!(inp(ssStatus)&0x40))
\r
286 //SS outp(ssData,*ssSample++);
\r
287 //SS outp(ssControl,ssOff);
\r
290 //SS outp(ssControl,ssOn);
\r
293 //SS ssLengthLeft--;
\r
294 //SS if(!ssLengthLeft)
\r
297 //SS SDL_DigitizedDoneInIRQ();
\r
302 TimerCount+=TimerDivisor;
\r
303 if(*((word *)&TimerCount+1))
\r
305 *((word *)&TimerCount+1)=0;
\r
314 // Timer 0 ISR for 700Hz interrupts
\r
315 void interrupt SDL_t0FastAsmService(void)
\r
320 // Timer 0 ISR for 140Hz interrupts
\r
321 void interrupt SDL_t0SlowAsmService(void)
\r
332 TimerCount+=TimerDivisor;
\r
333 if(*((word *)&TimerCount+1))
\r
335 *((word *)&TimerCount+1)=0;
\r
343 ///////////////////////////////////////////////////////////////////////////
\r
345 // SDL_SetTimer0() - Sets system timer 0 to the specified speed
\r
347 ///////////////////////////////////////////////////////////////////////////
\r
350 SDL_SetTimer0(word speed)
\r
352 #ifndef TPROF // If using Borland's profiling, don't screw with the timer
\r
356 outportb(0x43,0x36); // Change timer 0
\r
357 outportb(0x40,speed);
\r
358 outportb(0x40,speed >> 8);
\r
359 // Kludge to handle special case for digitized PC sounds
\r
360 if (TimerDivisor == (1192030 / (TickBase * 100)))
\r
361 TimerDivisor = (1192030 / (TickBase * 10));
\r
363 TimerDivisor = speed;
\r
367 TimerDivisor = 0x10000;
\r
371 ///////////////////////////////////////////////////////////////////////////
\r
373 // SDL_SetIntsPerSec() - Uses SDL_SetTimer0() to set the number of
\r
374 // interrupts generated by system timer 0 per second
\r
376 ///////////////////////////////////////////////////////////////////////////
\r
378 SDL_SetIntsPerSec(word ints)
\r
381 SDL_SetTimer0(1192030 / ints);
\r
384 #ifndef SD_USECATA3DSETTIMERSPEED
\r
386 SDL_SetTimerSpeed(void)
\r
389 void interrupt (*isr)(void);
\r
391 if ((DigiMode == sds_PC) && DigiPlaying)
\r
393 rate = TickBase * 100;
\r
394 isr = SDL_t0ExtremeAsmService;
\r
398 (MusicMode == smm_AdLib)
\r
399 || ((DigiMode == sds_SoundSource) && DigiPlaying)
\r
402 rate = TickBase * 10;
\r
403 isr = SDL_t0FastAsmService;
\r
407 rate = TickBase * 2;
\r
408 isr = SDL_t0SlowAsmService;
\r
411 if (rate != TimerRate)
\r
414 SDL_SetIntsPerSec(rate);
\r
420 SDL_SetTimerSpeed(void)
\r
424 if (MusicMode == smm_AdLib)
\r
425 rate = TickBase * 8;
\r
427 rate = TickBase * 2;
\r
428 SDL_SetIntsPerSec(rate);
\r
432 ///////////////////////////////////////////////////////////////////////////
\r
434 // SDL_TimingService() - Used by SDL_InitDelay() to determine a timing
\r
435 // value for the current system that we're running on
\r
437 ///////////////////////////////////////////////////////////////////////////
\r
438 //static void interrupt
\r
440 SDL_TimingService(void)
\r
448 outportb(0x20,0x20); // Ack interrupt
\r
450 #ifdef SD_USECATA3DSETTIMERSPEED
\r
451 ///////////////////////////////////////////////////////////////////////////
\r
453 // SDL_InitDelay() - Sets up TimerDelay's for SDL_Delay()
\r
455 ///////////////////////////////////////////////////////////////////////////
\r
457 SDL_InitDelay(void)
\r
462 //++++ setvect(8,SDL_TimingService); // Set to my timer 0 ISR
\r
464 SDL_SetIntsPerSec(1000); // Time 1ms
\r
466 for (i = 0,timer = 0;i < 10;i++) // Do timing test 10 times
\r
469 xor dx,dx // Zero DX
\r
470 mov cx,0xffff // Put starting value in CX
\r
471 mov [TimerDone],cx // TimerDone = false - 1
\r
472 #ifdef __BORLANDC__
\r
476 #ifdef __BORLANDC__
\r
480 jnz startloop // Make sure we're at the start
\r
481 #ifdef __BORLANDC__
\r
485 #ifdef __BORLANDC__
\r
488 test [TimerDone],1 // See if TimerDone flag got hit
\r
489 jnz done // Yep - drop out of the loop
\r
491 #ifdef __BORLANDC__
\r
499 if (0xffff - TimerVal > timer)
\r
500 timer = 0xffff - TimerVal;
\r
502 timer += timer / 2; // Use some slop
\r
503 TimerDelay10 = timer / (1000 / 10);
\r
504 TimerDelay25 = timer / (1000 / 25);
\r
505 TimerDelay100 = timer / (1000 / 100);
\r
507 SDL_SetTimer0(0); // Reset timer 0
\r
509 setvect(8,t0OldService); // Set back to old ISR
\r
512 ///////////////////////////////////////////////////////////////////////////
\r
514 // SDL_Delay() - Delays the specified amount of time
\r
516 ///////////////////////////////////////////////////////////////////////////
\r
518 SDL_Delay(word delay)
\r
525 #ifdef __BORLANDC__
\r
529 #ifdef __BORLANDC__
\r
532 test [TimerDone],0 // Useless code - just for timing equivilency
\r
535 #ifdef __BORLANDC__
\r
548 ///////////////////////////////////////////////////////////////////////////
\r
550 // SDL_PCPlaySample() - Plays the specified sample on the PC speaker
\r
552 ///////////////////////////////////////////////////////////////////////////
\r
558 SDL_PCPlaySample(byte huge *data,dword len)
\r
563 SDL_IndicatePC(true);
\r
565 pcLengthLeft = len;
\r
566 pcSound = (volatile byte far *)data;
\r
571 ///////////////////////////////////////////////////////////////////////////
\r
573 // SDL_PCStopSample() - Stops a sample playing on the PC speaker
\r
575 ///////////////////////////////////////////////////////////////////////////
\r
581 SDL_PCStopSample(void)
\r
586 /*(long)*/pcSound = 0;
\r
588 SDL_IndicatePC(false);
\r
590 asm in al,0x61 // Turn the speaker off
\r
591 asm and al,0xfd // ~2
\r
597 ///////////////////////////////////////////////////////////////////////////
\r
599 // SDL_PCPlaySound() - Plays the specified sound on the PC speaker
\r
601 ///////////////////////////////////////////////////////////////////////////
\r
607 SDL_PCPlaySound(PCSound far *sound)
\r
613 pcLengthLeft = sound->common.length;
\r
614 pcSound = sound->data;
\r
619 ///////////////////////////////////////////////////////////////////////////
\r
621 // SDL_PCStopSound() - Stops the current sound playing on the PC Speaker
\r
623 ///////////////////////////////////////////////////////////////////////////
\r
629 SDL_PCStopSound(void)
\r
634 /*(long)*/pcSound = 0;
\r
636 asm in al,0x61 // Turn the speaker off
\r
637 asm and al,0xfd // ~2
\r
643 ///////////////////////////////////////////////////////////////////////////
\r
645 // SDL_PCService() - Handles playing the next sample in a PC sound
\r
647 ///////////////////////////////////////////////////////////////////////////
\r
649 SDL_PCService(void)
\r
657 if (s != pcLastSample)
\r
663 if (s) // We have a frequency!
\r
665 t = pcSoundLookup[s];
\r
668 asm mov al,0xb6 // Write to channel 2 (speaker) timer
\r
671 asm out 42h,al // Low byte
\r
673 asm out 42h,al // High byte
\r
675 asm in al,0x61 // Turn the speaker & gate on
\r
679 else // Time for some silence
\r
681 asm in al,0x61 // Turn the speaker & gate off
\r
682 asm and al,0xfc // ~3
\r
689 if (!(--pcLengthLeft))
\r
692 SDL_SoundFinished();
\r
697 ///////////////////////////////////////////////////////////////////////////
\r
699 // SDL_ShutPC() - Turns off the pc speaker
\r
701 ///////////////////////////////////////////////////////////////////////////
\r
710 asm in al,0x61 // Turn the speaker & gate off
\r
711 asm and al,0xfc // ~3
\r
718 // Stuff for digitized sounds
\r
721 SDL_LoadDigiSegment(word page, global_game_variables_t *gvar)
\r
725 #if 0 // for debugging
\r
726 asm mov dx,STATUS_REGISTER_1
\r
728 asm mov dx,ATR_INDEX
\r
729 asm mov al,ATR_OVERSCAN
\r
731 asm mov al,10 // bright green
\r
735 addr = PM_GetSoundPage(page);
\r
736 PM_SetPageLock(gvar->pm.fi.PMSoundStart + page,pml_Locked, gvar);
\r
738 #if 0 // for debugging
\r
739 asm mov dx,STATUS_REGISTER_1
\r
741 asm mov dx,ATR_INDEX
\r
742 asm mov al,ATR_OVERSCAN
\r
744 asm mov al,3 // blue
\r
746 asm mov al,0x20 // normal
\r
754 SDL_PlayDigiSegment(memptr addr,word len)
\r
759 SDL_PCPlaySample(addr,len);
\r
761 //SS case sds_SoundSource:
\r
762 //SS SDL_SSPlaySample(addr,len);
\r
764 //SB case sds_SoundBlaster:
\r
765 //SB SDL_SBPlaySample(addr,len);
\r
771 SD_StopDigitized(global_game_variables_t *gvar)
\r
779 DigiNextAddr = nil;
\r
781 DigiMissed = false;
\r
782 DigiPlaying = false;
\r
783 DigiNumber = DigiPriority = 0;
\r
784 SoundPositioned = false;
\r
785 if ((DigiMode == sds_PC) && (SoundMode == sdm_PC))
\r
786 SDL_SoundFinished();
\r
791 SDL_PCStopSample();
\r
793 //SS case sds_SoundSource:
\r
794 //SS SDL_SSStopSample();
\r
796 //SB case sds_SoundBlaster:
\r
797 //SB SDL_SBStopSample();
\r
803 for (i = DigiLastStart;i < DigiLastEnd;i++)
\r
804 PM_SetPageLock(i + gvar->pm.fi.PMSoundStart,pml_Unlocked, gvar);
\r
810 SD_Poll(global_game_variables_t *gvar)
\r
812 if (DigiLeft && !DigiNextAddr)
\r
814 DigiNextLen = (DigiLeft >= PMPageSize)? PMPageSize : (DigiLeft % PMPageSize);
\r
815 DigiLeft -= DigiNextLen;
\r
817 DigiLastSegment = true;
\r
818 DigiNextAddr = SDL_LoadDigiSegment(DigiPage++, gvar);
\r
820 if (DigiMissed && DigiNextAddr)
\r
822 SDL_PlayDigiSegment(DigiNextAddr,DigiNextLen);
\r
823 DigiNextAddr = nil;
\r
824 DigiMissed = false;
\r
825 if (DigiLastSegment)
\r
827 DigiPlaying = false;
\r
828 DigiLastSegment = false;
\r
831 SDL_SetTimerSpeed();
\r
835 SD_SetPosition(int leftpos,int rightpos, global_game_variables_t *gvar)
\r
843 || ((leftpos == 15) && (rightpos == 15))
\r
845 Quit(gvar, "SD_SetPosition: Illegal position");
\r
849 //SB case sds_SoundBlaster:
\r
850 //SB SDL_PositionSBP(leftpos,rightpos);
\r
856 SD_PlayDigitized(word which,int leftpos,int rightpos, global_game_variables_t *gvar)
\r
864 SD_StopDigitized(gvar);
\r
865 if (which >= NumDigi)
\r
866 Quit(gvar, "SD_PlayDigitized: bad sound number");
\r
868 SD_SetPosition(leftpos,rightpos, gvar);
\r
870 DigiPage = DigiList[(which * 2) + 0];
\r
871 DigiLeft = DigiList[(which * 2) + 1];
\r
873 DigiLastStart = DigiPage;
\r
874 DigiLastEnd = DigiPage + ((DigiLeft + (PMPageSize - 1)) / PMPageSize);
\r
876 len = (DigiLeft >= PMPageSize)? PMPageSize : (DigiLeft % PMPageSize);
\r
877 addr = SDL_LoadDigiSegment(DigiPage++, gvar);
\r
879 DigiPlaying = true;
\r
880 DigiLastSegment = false;
\r
882 SDL_PlayDigiSegment(addr,len);
\r
885 DigiLastSegment = true;
\r
891 SDL_DigitizedDone(void)
\r
895 SDL_PlayDigiSegment(DigiNextAddr,DigiNextLen);
\r
896 DigiNextAddr = nil;
\r
897 DigiMissed = false;
\r
901 if (DigiLastSegment)
\r
903 DigiPlaying = false;
\r
904 DigiLastSegment = false;
\r
905 if ((DigiMode == sds_PC) && (SoundMode == sdm_PC))
\r
907 SDL_SoundFinished();
\r
910 DigiNumber = DigiPriority = 0;
\r
911 SoundPositioned = false;
\r
919 SD_SetDigiDevice(SDSMode mode, global_game_variables_t *gvar)
\r
921 boolean devicenotpresent;
\r
923 if (mode == DigiMode)
\r
926 SD_StopDigitized(gvar);
\r
928 devicenotpresent = false;
\r
929 //SBSS switch (mode)
\r
931 //SB case sds_SoundBlaster:
\r
932 //SB if (!SoundBlasterPresent)
\r
934 //SB if (SoundSourcePresent)
\r
935 //SB mode = sds_SoundSource;
\r
937 //SB devicenotpresent = true;
\r
940 //SS case sds_SoundSource:
\r
941 //SS if (!SoundSourcePresent)
\r
942 //SS devicenotpresent = true;
\r
946 if (!devicenotpresent)
\r
948 //SS if (DigiMode == sds_SoundSource)
\r
953 //SS if (mode == sds_SoundSource)
\r
954 //SS SDL_StartSS();
\r
956 SDL_SetTimerSpeed();
\r
961 SDL_SetupDigi(global_game_variables_t *gvar)
\r
968 PM_UnlockMainMem(gvar);
\r
969 MM_GetPtr(&list,PMPageSize, gvar);
\r
970 PM_CheckMainMem(gvar);
\r
971 p = (word far *)MK_FP(PM_GetPage(gvar->pm.fi.ChunksInFile - 1, gvar),0);
\r
972 _fmemcpy((void far *)list,(void far *)p,PMPageSize);
\r
973 pg = gvar->pm.fi.PMSoundStart;
\r
974 for (i = 0;i < PMPageSize / (sizeof(word) * 2);i++,p += 2)
\r
976 if (pg >= gvar->pm.fi.ChunksInFile - 1)
\r
978 pg += (p[1] + (PMPageSize - 1)) / PMPageSize;
\r
980 PM_UnlockMainMem(gvar);
\r
981 MM_GetPtr(MEMPTRCONV DigiList,i * sizeof(word) * 2, gvar);
\r
982 _fmemcpy((void far *)DigiList,(void far *)list,i * sizeof(word) * 2);
\r
983 MM_FreePtr(&list, gvar);
\r
986 for (i = 0;i < LASTSOUND;i++)
\r
992 ///////////////////////////////////////////////////////////////////////////
\r
994 // alOut(n,b) - Puts b in AdLib card register n
\r
996 ///////////////////////////////////////////////////////////////////////////
\r
998 alOut(byte n,byte b)
\r
1007 SDL_Delay(TimerDelay10);
\r
1024 SDL_Delay(TimerDelay25);
\r
1069 ///////////////////////////////////////////////////////////////////////////
\r
1071 // SDL_SetInstrument() - Puts an instrument into a generator
\r
1073 ///////////////////////////////////////////////////////////////////////////
\r
1076 SDL_SetInstrument(int track,int which,Instrument far *inst,boolean percussive)
\r
1082 c = pcarriers[which];
\r
1083 m = pmodifiers[which];
\r
1087 c = carriers[which];
\r
1088 m = modifiers[which];
\r
1091 tracks[track - 1]->inst = *inst;
\r
1092 tracks[track - 1]->percussive = percussive;
\r
1094 alOut(m + alChar,inst->mChar);
\r
1095 alOut(m + alScale,inst->mScale);
\r
1096 alOut(m + alAttack,inst->mAttack);
\r
1097 alOut(m + alSus,inst->mSus);
\r
1098 alOut(m + alWave,inst->mWave);
\r
1100 // Most percussive instruments only use one cell
\r
1103 alOut(c + alChar,inst->cChar);
\r
1104 alOut(c + alScale,inst->cScale);
\r
1105 alOut(c + alAttack,inst->cAttack);
\r
1106 alOut(c + alSus,inst->cSus);
\r
1107 alOut(c + alWave,inst->cWave);
\r
1110 alOut(which + alFeedCon,inst->nConn); // DEBUG - I think this is right
\r
1114 ///////////////////////////////////////////////////////////////////////////
\r
1116 // SDL_ALStopSound() - Turns off any sound effects playing through the
\r
1119 ///////////////////////////////////////////////////////////////////////////
\r
1125 SDL_ALStopSound(void)
\r
1130 /*(long)*/alSound = 0;
\r
1131 alOut(alFreqH + 0,0);
\r
1137 SDL_AlSetFXInst(Instrument far *inst)
\r
1143 alOut(m + alChar,inst->mChar);
\r
1144 alOut(m + alScale,inst->mScale);
\r
1145 alOut(m + alAttack,inst->mAttack);
\r
1146 alOut(m + alSus,inst->mSus);
\r
1147 alOut(m + alWave,inst->mWave);
\r
1148 alOut(c + alChar,inst->cChar);
\r
1149 alOut(c + alScale,inst->cScale);
\r
1150 alOut(c + alAttack,inst->cAttack);
\r
1151 alOut(c + alSus,inst->cSus);
\r
1152 alOut(c + alWave,inst->cWave);
\r
1154 // Note: Switch commenting on these lines for old MUSE compatibility
\r
1155 // alOut(alFeedCon,inst->nConn);
\r
1156 alOut(alFeedCon,0);
\r
1159 ///////////////////////////////////////////////////////////////////////////
\r
1161 // SDL_ALPlaySound() - Plays the specified sound on the AdLib card
\r
1163 ///////////////////////////////////////////////////////////////////////////
\r
1169 SDL_ALPlaySound(AdLibSound far *sound, global_game_variables_t *gvar)
\r
1171 Instrument __far *inst;
\r
1174 SDL_ALStopSound();
\r
1179 alLengthLeft = sound->common.length;
\r
1180 data = sound->data;
\r
1183 alSound = (byte far *)data;
\r
1184 alBlock = ((sound->block & 7) << 2) | 0x20;
\r
1185 inst = &sound->inst;
\r
1187 if (!(inst->mSus | inst->cSus))
\r
1190 Quit(gvar, "SDL_ALPlaySound() - Bad instrument");
\r
1193 SDL_AlSetFXInst(&alZeroInst); // DEBUG
\r
1194 SDL_AlSetFXInst(inst);
\r
1200 ///////////////////////////////////////////////////////////////////////////
\r
1202 // SDL_ALSoundService() - Plays the next sample out through the AdLib card
\r
1204 ///////////////////////////////////////////////////////////////////////////
\r
1207 SDL_ALSoundService(void)
\r
1215 alOut(alFreqH + 0,0);
\r
1218 alOut(alFreqL + 0,s);
\r
1219 alOut(alFreqH + 0,alBlock);
\r
1222 if (!(--alLengthLeft))
\r
1224 /*(long)*/alSound = 0;
\r
1225 alOut(alFreqH + 0,0);
\r
1226 SDL_SoundFinished();
\r
1233 ///////////////////////////////////////////////////////////////////////////
\r
1235 // SDL_SelectMeasure() - sets up sequencing variables for a given track
\r
1237 ///////////////////////////////////////////////////////////////////////////
\r
1239 SDL_SelectMeasure(ActiveTrack *track)
\r
1241 track->seq = track->moods[track->mood];
\r
1242 track->nextevent = 0;
\r
1248 SDL_ALService(void)
\r
1258 while (sqHackLen && (sqHackTime <= alTimeCount))
\r
1261 sqHackTime = alTimeCount + *sqHackPtr++;
\r
1271 sqHackPtr = (word far *)sqHack;
\r
1272 sqHackLen = sqHackSeqLen;
\r
1273 alTimeCount = sqHackTime = 0;
\r
1278 ///////////////////////////////////////////////////////////////////////////
\r
1280 // SDL_ShutAL() - Shuts down the AdLib card for sound effects
\r
1282 ///////////////////////////////////////////////////////////////////////////
\r
1289 alOut(alEffects,0);
\r
1290 alOut(alFreqH + 0,0);
\r
1291 SDL_AlSetFXInst(&alZeroInst);
\r
1297 ///////////////////////////////////////////////////////////////////////////
\r
1299 // SDL_CleanAL() - Totally shuts down the AdLib card
\r
1301 ///////////////////////////////////////////////////////////////////////////
\r
1310 alOut(alEffects,0);
\r
1311 for (i = 1;i < 0xf5;i++)
\r
1317 ///////////////////////////////////////////////////////////////////////////
\r
1319 // SDL_StartAL() - Starts up the AdLib card for sound effects
\r
1321 ///////////////////////////////////////////////////////////////////////////
\r
1326 alOut(alEffects,alFXReg);
\r
1327 SDL_AlSetFXInst(&alZeroInst);
\r
1330 ///////////////////////////////////////////////////////////////////////////
\r
1332 // SDL_DetectAdLib() - Determines if there's an AdLib (or SoundBlaster
\r
1333 // emulating an AdLib) present
\r
1335 ///////////////////////////////////////////////////////////////////////////
\r
1337 SDL_DetectAdLib(void)
\r
1339 byte status1,status2;
\r
1342 alOut(4,0x60); // Reset T1 & T2
\r
1343 alOut(4,0x80); // Reset IRQ
\r
1344 status1 = readstat();
\r
1345 alOut(2,0xff); // Set timer 1
\r
1346 alOut(4,0x21); // Start timer 1
\r
1347 SDL_Delay(TimerDelay100);
\r
1353 #ifdef __BORLANDC__
\r
1357 #ifdef __BORLANDC__
\r
1365 status2 = readstat();
\r
1369 if (((status1 & 0xe0) == 0x00) && ((status2 & 0xe0) == 0xc0))
\r
1371 for (i = 1;i <= 0xf5;i++) // Zero all the registers
\r
1374 alOut(1,0x20); // Set WSE=1
\r
1375 alOut(8,0); // Set CSM=0 & SEL=0
\r
1383 ///////////////////////////////////////////////////////////////////////////
\r
1385 // SDL_t0Service() - My timer 0 ISR which handles the different timings and
\r
1386 // dispatches to whatever other routines are appropriate
\r
1388 ///////////////////////////////////////////////////////////////////////////
\r
1389 //static void interrupt
\r
1391 SDL_t0Service(void)
\r
1393 static word count = 1;
\r
1394 boolean myackflag = 0;
\r
1396 //00#if 0 // for debugging
\r
1397 asm mov dx,STATUS_REGISTER_1
\r
1399 asm mov dx,ATR_INDEX
\r
1400 asm mov al,ATR_OVERSCAN
\r
1402 asm mov al,4 // red
\r
1408 if ((MusicMode == smm_AdLib) || (DigiMode == sds_SoundSource))
\r
1411 //SS SDL_SSService();
\r
1412 // if (!(++count & 7))
\r
1413 if (!(++count % 10))
\r
1417 if (SoundUserHook)
\r
1420 // if (!(count & 3))
\r
1423 switch (SoundMode)
\r
1429 SDL_ALSoundService();
\r
1436 if (!(++count & 1))
\r
1440 if (SoundUserHook)
\r
1443 switch (SoundMode)
\r
1449 SDL_ALSoundService();
\r
1455 mov ax,[WORD PTR TimerCount]
\r
1456 add ax,[WORD PTR TimerDivisor]
\r
1457 mov [WORD PTR TimerCount],ax
\r
1460 #ifdef __BORLANDC__
\r
1464 #ifdef __BORLANDC__
\r
1468 #ifdef __BORLANDC__
\r
1472 #ifdef __WATCOMC__
\r
1476 t0OldService(); // If we overflow a word, time to call old int handler
\r
1478 outportb(0x20,0x20); // Ack the interrupt
\r
1480 //00#if 0 // for debugging
\r
1481 asm mov dx,STATUS_REGISTER_1
\r
1483 asm mov dx,ATR_INDEX
\r
1484 asm mov al,ATR_OVERSCAN
\r
1486 asm mov al,3 // blue
\r
1488 asm mov al,0x20 // normal
\r
1493 ////////////////////////////////////////////////////////////////////////////
\r
1495 // SDL_ShutDevice() - turns off whatever device was being used for sound fx
\r
1497 ////////////////////////////////////////////////////////////////////////////
\r
1499 SDL_ShutDevice(void)
\r
1501 switch (SoundMode)
\r
1510 SoundMode = sdm_Off;
\r
1513 ///////////////////////////////////////////////////////////////////////////
\r
1515 // SDL_CleanDevice() - totally shuts down all sound devices
\r
1517 ///////////////////////////////////////////////////////////////////////////
\r
1519 SDL_CleanDevice(void)
\r
1521 if ((SoundMode == sdm_AdLib) || (MusicMode == smm_AdLib))
\r
1525 ///////////////////////////////////////////////////////////////////////////
\r
1527 // SDL_StartDevice() - turns on whatever device is to be used for sound fx
\r
1529 ///////////////////////////////////////////////////////////////////////////
\r
1531 SDL_StartDevice(void)
\r
1533 switch (SoundMode)
\r
1539 SoundNumber = SoundPriority = 0;
\r
1543 SDL_SetTimerSpeed(void)
\r
1547 if (MusicMode == smm_AdLib)
\r
1548 rate = TickBase * 8;
\r
1550 rate = TickBase * 2;
\r
1551 SDL_SetIntsPerSec(rate);
\r
1554 // Public routines
\r
1556 ///////////////////////////////////////////////////////////////////////////
\r
1558 // SD_SetSoundMode() - Sets which sound hardware to use for sound effects
\r
1560 ///////////////////////////////////////////////////////////////////////////
\r
1562 SD_SetSoundMode(SDMode mode, global_game_variables_t *gvar)
\r
1564 boolean result = false;
\r
1567 SD_StopSound(gvar);
\r
1570 if ((mode == sdm_AdLib) && !AdLibPresent)
\r
1576 NeedsDigitized = false;
\r
1580 tableoffset = STARTPCSOUNDS;
\r
1581 NeedsDigitized = false;
\r
1587 tableoffset = STARTADLIBSOUNDS;
\r
1588 NeedsDigitized = false;
\r
1597 if (result && (mode != SoundMode))
\r
1602 SoundTable = (word *)(&gvar->ca.audiosegs[tableoffset]);
\r
1604 SDL_StartDevice();
\r
1607 SDL_SetTimerSpeed();
\r
1612 ///////////////////////////////////////////////////////////////////////////
\r
1614 // SD_SetMusicMode() - sets the device to use for background music
\r
1616 ///////////////////////////////////////////////////////////////////////////
\r
1618 SD_SetMusicMode(SMMode mode)
\r
1620 boolean result = false;
\r
1622 SD_FadeOutMusic();
\r
1623 while (SD_MusicPlaying())
\r
1629 NeedsMusic = false;
\r
1635 NeedsMusic = true;
\r
1644 SDL_SetTimerSpeed();
\r
1649 ///////////////////////////////////////////////////////////////////////////
\r
1651 // SD_Startup() - starts up the Sound Mgr
\r
1652 // Detects all additional sound hardware and installs my ISR
\r
1654 ///////////////////////////////////////////////////////////////////////////
\r
1656 SD_Startup(global_game_variables_t *gvar)
\r
1662 #ifndef SD_USECATA3DSETTIMERSPEED
\r
1665 ssIsTandy = false;
\r
1666 //SS ssNoCheck = false;
\r
1667 alNoCheck = false;
\r
1668 //SB sbNoCheck = false;
\r
1669 //SB sbNoProCheck = false;
\r
1671 for (i = 1;i < _argc;i++)
\r
1673 switch (US_CheckParm(_argv[i],ParmStrings))
\r
1675 case 0: // No AdLib detection
\r
1678 //SB case 1: // No SoundBlaster detection
\r
1679 //SB sbNoCheck = true;
\r
1681 //SB case 2: // No SoundBlaster Pro detection
\r
1682 //SB sbNoProCheck = true;
\r
1685 //SS ssNoCheck = true; // No Sound Source detection
\r
1687 case 4: // Tandy Sound Source handling
\r
1690 //SS case 5: // Sound Source present at LPT1
\r
1692 //SS ssNoCheck = SoundSourcePresent = true;
\r
1694 //SS case 6: // Sound Source present at LPT2
\r
1696 //SS ssNoCheck = SoundSourcePresent = true;
\r
1698 //SS case 7: // Sound Source present at LPT3
\r
1700 //SS ssNoCheck = SoundSourcePresent = true;
\r
1706 SoundUserHook = 0;
\r
1708 t0OldService = getvect(8); // Get old timer 0 ISR
\r
1709 #ifdef SD_USECATA3DSETTIMERSPEED
\r
1710 SDL_InitDelay(); // SDL_InitDelay() uses t0OldService
\r
1712 setvect(8,SDL_t0Service); // Set to my timer 0 ISR
\r
1714 LocalTime = TimeCount = alTimeCount = 0;
\r
1716 SD_SetSoundMode(sdm_Off, gvar);
\r
1717 SD_SetMusicMode(smm_Off);
\r
1719 //SS if (!ssNoCheck)
\r
1720 //SS SoundSourcePresent = SDL_DetectSoundSource();
\r
1724 AdLibPresent = SDL_DetectAdLib();
\r
1725 //SB if (AdLibPresent) && !sbNoCheck)
\r
1727 //SB int port = -1;
\r
1728 //SB char *env = getenv("BLASTER");
\r
1734 //SB while (isspace(*env))
\r
1737 //SB switch (toupper(*env))
\r
1740 //SB temp = strtol(env + 1,&env,16);
\r
1743 //SB (temp >= 0x210)
\r
1744 //SB && (temp <= 0x260)
\r
1745 //SB && (!(temp & 0x00f))
\r
1747 //SB port = (temp - 0x200) >> 4;
\r
1749 //SB Quit(gvar, "SD_Startup: Unsupported address value in BLASTER");
\r
1752 //SB temp = strtol(env + 1,&env,10);
\r
1756 //SB && (temp <= 10)
\r
1757 //SB && (sbIntVectors[temp] != -1)
\r
1760 //SB sbInterrupt = temp;
\r
1761 //SB sbIntVec = sbIntVectors[sbInterrupt];
\r
1764 //SB Quit(gvar, "SD_Startup: Unsupported interrupt value in BLASTER");
\r
1767 //SB temp = strtol(env + 1,&env,10);
\r
1768 //SB if ((temp == 0) || (temp == 1) || (temp == 3))
\r
1769 //SB SDL_SBSetDMA(temp);
\r
1771 //SB Quit(gvar, "SD_Startup: Unsupported DMA value in BLASTER");
\r
1774 //SB while (isspace(*env))
\r
1776 //SB while (*env && !isspace(*env))
\r
1782 //SB SoundBlasterPresent = SDL_DetectSoundBlaster(port);
\r
1786 for (i = 0;i < 255;i++)
\r
1787 pcSoundLookup[i] = i * 60;
\r
1789 //SB if (SoundBlasterPresent)
\r
1790 //SB SDL_StartSB();
\r
1792 //++++ SDL_SetupDigi(gvar);
\r
1794 SD_Started = true;
\r
1797 ///////////////////////////////////////////////////////////////////////////
\r
1799 // SD_Default() - Sets up the default behaviour for the Sound Mgr whether
\r
1800 // the config file was present or not.
\r
1802 ///////////////////////////////////////////////////////////////////////////
\r
1804 SD_Default(boolean gotit,SDMode sd,SMMode sm, global_game_variables_t *gvar)
\r
1806 boolean gotsd,gotsm;
\r
1808 gotsd = gotsm = gotit;
\r
1810 if (gotsd) // Make sure requested sound hardware is available
\r
1815 gotsd = AdLibPresent;
\r
1826 if (sd != SoundMode)
\r
1827 SD_SetSoundMode(sd, gvar);
\r
1830 if (gotsm) // Make sure requested music hardware is available
\r
1835 gotsm = AdLibPresent;
\r
1844 if (sm != MusicMode)
\r
1845 SD_SetMusicMode(sm);
\r
1848 ///////////////////////////////////////////////////////////////////////////
\r
1850 // SD_Shutdown() - shuts down the Sound Mgr
\r
1851 // Removes sound ISR and turns off whatever sound hardware was active
\r
1853 ///////////////////////////////////////////////////////////////////////////
\r
1855 SD_Shutdown(global_game_variables_t *gvar)
\r
1861 SD_StopSound(gvar);
\r
1863 SDL_CleanDevice();
\r
1865 //SB if (SoundBlasterPresent)
\r
1866 //SB SDL_ShutSB();
\r
1868 //SS if (SoundSourcePresent)
\r
1869 //SS SDL_ShutSS();
\r
1876 setvect(8,t0OldService);
\r
1880 SD_Started = false;
\r
1883 ///////////////////////////////////////////////////////////////////////////
\r
1885 // SD_SetUserHook() - sets the routine that the Sound Mgr calls every 1/70th
\r
1886 // of a second from its timer 0 ISR
\r
1888 ///////////////////////////////////////////////////////////////////////////
\r
1890 SD_SetUserHook(void (* hook)(void))
\r
1892 SoundUserHook = hook;
\r
1895 ///////////////////////////////////////////////////////////////////////////
\r
1897 // SD_PositionSound() - Sets up a stereo imaging location for the next
\r
1898 // sound to be played. Each channel ranges from 0 to 15.
\r
1900 ///////////////////////////////////////////////////////////////////////////
\r
1902 SD_PositionSound(int leftvol,int rightvol)
\r
1904 LeftPosition = leftvol;
\r
1905 RightPosition = rightvol;
\r
1906 nextsoundpos = true;
\r
1909 ///////////////////////////////////////////////////////////////////////////
\r
1911 // SD_PlaySound() - plays the specified sound on the appropriate hardware
\r
1913 ///////////////////////////////////////////////////////////////////////////
\r
1915 SD_PlaySound(soundnames sound, global_game_variables_t *gvar)
\r
1918 SoundCommon far *s;
\r
1921 lp = LeftPosition;
\r
1922 rp = RightPosition;
\r
1924 RightPosition = 0;
\r
1926 ispos = nextsoundpos;
\r
1927 nextsoundpos = false;
\r
1932 s = MK_FP(SoundTable[sound],0);
\r
1933 if ((SoundMode != sdm_Off) && !s)
\r
1934 Quit(gvar, "SD_PlaySound() - Uncached sound");
\r
1936 if ((DigiMode != sds_Off) && (DigiMap[sound] != -1))
\r
1938 if ((DigiMode == sds_PC) && (SoundMode == sdm_PC))
\r
1940 if (s->priority < SoundPriority)
\r
1943 SDL_PCStopSound();
\r
1945 SD_PlayDigitized(DigiMap[sound],lp,rp, gvar);
\r
1946 SoundPositioned = ispos;
\r
1947 SoundNumber = sound;
\r
1948 SoundPriority = s->priority;
\r
1954 if (DigiPriority && !DigiNumber)
\r
1957 Quit(gvar, "SD_PlaySound: Priority without a sound");
\r
1961 if (s->priority < DigiPriority)
\r
1964 SD_PlayDigitized(DigiMap[sound],lp,rp, gvar);
\r
1965 SoundPositioned = ispos;
\r
1966 DigiNumber = sound;
\r
1967 DigiPriority = s->priority;
\r
1973 if (SoundMode == sdm_Off)
\r
1976 Quit(gvar, "SD_PlaySound() - Zero length sound");
\r
1977 if (s->priority < SoundPriority)
\r
1980 switch (SoundMode)
\r
1983 SDL_PCPlaySound((void far *)s);
\r
1986 SDL_ALPlaySound((void far *)s, gvar);
\r
1990 SoundNumber = sound;
\r
1991 SoundPriority = s->priority;
\r
1996 ///////////////////////////////////////////////////////////////////////////
\r
1998 // SD_SoundPlaying() - returns the sound number that's playing, or 0 if
\r
1999 // no sound is playing
\r
2001 ///////////////////////////////////////////////////////////////////////////
\r
2003 SD_SoundPlaying(void)
\r
2005 boolean result = false;
\r
2007 switch (SoundMode)
\r
2010 result = pcSound? true : false;
\r
2013 result = alSound? true : false;
\r
2018 return(SoundNumber);
\r
2023 ///////////////////////////////////////////////////////////////////////////
\r
2025 // SD_StopSound() - if a sound is playing, stops it
\r
2027 ///////////////////////////////////////////////////////////////////////////
\r
2029 SD_StopSound(global_game_variables_t *gvar)
\r
2032 SD_StopDigitized(gvar);
\r
2034 switch (SoundMode)
\r
2037 SDL_PCStopSound();
\r
2040 SDL_ALStopSound();
\r
2044 SoundPositioned = false;
\r
2046 SDL_SoundFinished();
\r
2049 ///////////////////////////////////////////////////////////////////////////
\r
2051 // SD_WaitSoundDone() - waits until the current sound is done playing
\r
2053 ///////////////////////////////////////////////////////////////////////////
\r
2055 SD_WaitSoundDone(void)
\r
2057 while (SD_SoundPlaying())
\r
2061 ///////////////////////////////////////////////////////////////////////////
\r
2063 // SD_MusicOn() - turns on the sequencer
\r
2065 ///////////////////////////////////////////////////////////////////////////
\r
2072 ///////////////////////////////////////////////////////////////////////////
\r
2074 // SD_MusicOff() - turns off the sequencer and any playing notes
\r
2076 ///////////////////////////////////////////////////////////////////////////
\r
2083 switch (MusicMode)
\r
2087 alOut(alEffects,0);
\r
2088 for (i = 0;i < sqMaxTracks;i++)
\r
2089 alOut(alFreqH + i + 1,0);
\r
2095 ///////////////////////////////////////////////////////////////////////////
\r
2097 // SD_StartMusic() - starts playing the music pointed to
\r
2099 ///////////////////////////////////////////////////////////////////////////
\r
2101 SD_StartMusic(MusicGroup far *music)
\r
2107 if (MusicMode == smm_AdLib)
\r
2109 sqHackPtr = sqHack = music->values;
\r
2110 sqHackSeqLen = sqHackLen = music->length;
\r
2119 ///////////////////////////////////////////////////////////////////////////
\r
2121 // SD_FadeOutMusic() - starts fading out the music. Call SD_MusicPlaying()
\r
2122 // to see if the fadeout is complete
\r
2124 ///////////////////////////////////////////////////////////////////////////
\r
2126 SD_FadeOutMusic(void)
\r
2128 switch (MusicMode)
\r
2131 // DEBUG - quick hack to turn the music off
\r
2137 ///////////////////////////////////////////////////////////////////////////
\r
2139 // SD_MusicPlaying() - returns true if music is currently playing, false if
\r
2142 ///////////////////////////////////////////////////////////////////////////
\r
2144 SD_MusicPlaying(void)
\r
2148 switch (MusicMode)
\r
2152 // DEBUG - not written
\r
2166 //variables for these assembly functions
\r
2167 volatile boolean pcindicate;
\r
2168 volatile unsigned/*boolean*/ MyDS;
\r
2181 // Timer 0 ISR for 7000Hz interrupts
\r
2182 void interrupt SDL_t0ExtremeAsmService(void)
\r
2188 SDL_setPCSpeaker(((*pcSound++)&0x80)>>6);
\r
2193 SDL_turnOffPCSpeaker();
\r
2194 SDL_DigitizedDoneInIRQ();
\r
2210 void SDL_IndicatePC(boolean ind)
\r
2216 SDL_DigitizedDoneInIRQ(void)
\r
2220 SDL_PlayDigiSegment(DigiNextAddr,DigiNextLen/*,true*/);
\r
2221 DigiNextAddr = nil;
\r
2222 DigiMissed = false;
\r
2226 if (DigiLastSegment)
\r
2228 DigiPlaying = false;
\r
2229 DigiLastSegment = false;
\r
2230 if ((DigiMode == sds_PC) && (SoundMode == sdm_PC))
\r
2232 SDL_SoundFinished();
\r
2236 DigiNumber = (soundnames) 0;
\r
2239 SoundPositioned = false;
\r
2242 DigiMissed = true;
\r
2247 // Inside an interrupt handler interrupts should already be disabled
\r
2248 // so don't disable them again and cause V86 exceptions which cost
\r
2249 // aprox. 300 processor tics!
\r
2252 void alOutInIRQ(byte n,byte b)
\r