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/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 //#define SD_USECATA3DSETTIMERSPEED
\r
75 // Imports from ID_SD_A.ASM
\r
76 /*extern*/ void SDL_SetDS(void);
\r
77 /*extern*/ void interrupt SDL_t0ExtremeAsmService(void);//,
\r
78 // SDL_t0FastAsmService(void),
\r
79 // SDL_t0SlowAsmService(void);
\r
80 void SDL_IndicatePC(boolean on),
\r
81 SDL_DigitizedDoneInIRQ();
\r
84 boolean AdLibPresent,
\r
85 NeedsDigitized,NeedsMusic,
\r
92 word *SoundTable; // Really * _seg *SoundTable, but that don't work
\r
94 int DigiMap[LASTSOUND];
\r
96 // Internal variables
\r
97 static boolean SD_Started;
\r
98 boolean nextsoundpos;
\r
99 word/*boolean_+*/ TimerDone;
\r
100 word TimerVal,TimerDelay10,TimerDelay25,TimerDelay100;
\r
101 dword TimerDivisor,TimerCount;
\r
102 static char *ParmStrings[] =
\r
107 static void (*SoundUserHook)(void);
\r
108 soundnames SoundNumber,DigiNumber;
\r
109 word SoundPriority,DigiPriority;
\r
110 int LeftPosition,RightPosition;
\r
111 void interrupt (*t0OldService)(void);
\r
115 word NumDigi,DigiLeft,DigiPage;
\r
116 word _seg *DigiList;
\r
117 word DigiLastStart,DigiLastEnd;
\r
118 boolean DigiPlaying;
\r
119 static boolean DigiMissed,DigiLastSegment;
\r
120 static memptr DigiNextAddr;
\r
121 static word DigiNextLen;
\r
123 // PC Sound variables
\r
124 volatile byte pcLastSample,far *pcSound;
\r
125 dword pcLengthLeft;
\r
126 word pcSoundLookup[255];
\r
132 dword alLengthLeft;
\r
134 Instrument alZeroInst;
\r
136 // This table maps channel numbers to carrier and modulator op cells
\r
137 static byte carriers[9] = { 3, 4, 5,11,12,13,19,20,21},
\r
138 modifiers[9] = { 0, 1, 2, 8, 9,10,16,17,18},
\r
139 // This table maps percussive voice numbers to op cells
\r
140 pcarriers[5] = {19,0xff,0xff,0xff,0xff},
\r
141 pmodifiers[5] = {16,17,18,20,21};
\r
143 // Sequencer variables
\r
145 static word alFXReg;
\r
146 static ActiveTrack *tracks[sqMaxTracks];//,
\r
147 //-- mytracks[sqMaxTracks];
\r
148 //--static word sqMode,sqFadeStep;
\r
149 word far *sqHack,far *sqHackPtr,sqHackLen,sqHackSeqLen;
\r
152 // Internal routines
\r
153 void SDL_DigitizedDone(void);
\r
155 //Assembly functions
\r
162 void SDL_turnOnPCSpeaker(word timerval)
\r
178 void SDL_turnOffPCSpeaker()
\r
187 void SDL_setPCSpeaker(byte val)
\r
202 if(*pcSound!=pcLastSample)
\r
204 pcLastSample=*pcSound;
\r
207 SDL_turnOnPCSpeaker(pcLastSample*60);
\r
209 SDL_turnOffPCSpeaker();
\r
216 SoundNumber=(soundnames)0;
\r
218 SDL_turnOffPCSpeaker();
\r
222 if(alSound && !alNoIRQ)
\r
226 alOut/*InIRQ*/(alFreqL,*alSound);
\r
227 alOut/*InIRQ*/(alFreqH,alBlock);
\r
229 else alOut/*InIRQ*/(alFreqH,0);
\r
235 SoundNumber=(soundnames)0;
\r
237 alOut/*InIRQ*/(alFreqH,0);
\r
260 if(sqActive && !alNoIRQ)
\r
266 if(sqHackTime>alTimeCount) break;
\r
267 sqHackTime=alTimeCount+*(sqHackPtr+1);
\r
268 alOut/*InIRQ*/(*(byte *)sqHackPtr,*(((byte *)sqHackPtr)+1));
\r
278 sqHackLen=sqHackSeqLen;
\r
286 //SS if(!(inp(ssStatus)&0x40))
\r
288 //SS outp(ssData,*ssSample++);
\r
289 //SS outp(ssControl,ssOff);
\r
292 //SS outp(ssControl,ssOn);
\r
295 //SS ssLengthLeft--;
\r
296 //SS if(!ssLengthLeft)
\r
299 //SS SDL_DigitizedDoneInIRQ();
\r
304 TimerCount+=TimerDivisor;
\r
305 if(*((word *)&TimerCount+1))
\r
307 *((word *)&TimerCount+1)=0;
\r
316 // Timer 0 ISR for 700Hz interrupts
\r
317 void interrupt SDL_t0FastAsmService(void)
\r
322 // Timer 0 ISR for 140Hz interrupts
\r
323 void interrupt SDL_t0SlowAsmService(void)
\r
334 TimerCount+=TimerDivisor;
\r
335 if(*((word *)&TimerCount+1))
\r
337 *((word *)&TimerCount+1)=0;
\r
345 ///////////////////////////////////////////////////////////////////////////
\r
347 // SDL_SetTimer0() - Sets system timer 0 to the specified speed
\r
349 ///////////////////////////////////////////////////////////////////////////
\r
352 SDL_SetTimer0(word speed)
\r
354 #ifndef TPROF // If using Borland's profiling, don't screw with the timer
\r
358 outportb(0x43,0x36); // Change timer 0
\r
359 outportb(0x40,speed);
\r
360 outportb(0x40,speed >> 8);
\r
361 // Kludge to handle special case for digitized PC sounds
\r
362 if (TimerDivisor == (1192030 / (TickBase * 100)))
\r
363 TimerDivisor = (1192030 / (TickBase * 10));
\r
365 TimerDivisor = speed;
\r
369 TimerDivisor = 0x10000;
\r
373 ///////////////////////////////////////////////////////////////////////////
\r
375 // SDL_SetIntsPerSec() - Uses SDL_SetTimer0() to set the number of
\r
376 // interrupts generated by system timer 0 per second
\r
378 ///////////////////////////////////////////////////////////////////////////
\r
380 SDL_SetIntsPerSec(word ints)
\r
383 SDL_SetTimer0(1192030 / ints);
\r
386 #ifndef SD_USECATA3DSETTIMERSPEED
\r
388 SDL_SetTimerSpeed(void)
\r
391 void interrupt (*isr)(void);
\r
393 if ((DigiMode == sds_PC) && DigiPlaying)
\r
395 rate = TickBase * 100;
\r
396 isr = SDL_t0ExtremeAsmService;
\r
400 (MusicMode == smm_AdLib)
\r
401 || ((DigiMode == sds_SoundSource) && DigiPlaying)
\r
404 rate = TickBase * 10;
\r
405 isr = SDL_t0FastAsmService;
\r
409 rate = TickBase * 2;
\r
410 isr = SDL_t0SlowAsmService;
\r
413 if (rate != TimerRate)
\r
416 SDL_SetIntsPerSec(rate);
\r
422 SDL_SetTimerSpeed(void)
\r
426 if (MusicMode == smm_AdLib)
\r
427 rate = TickBase * 8;
\r
429 rate = TickBase * 2;
\r
430 SDL_SetIntsPerSec(rate);
\r
434 ///////////////////////////////////////////////////////////////////////////
\r
436 // SDL_TimingService() - Used by SDL_InitDelay() to determine a timing
\r
437 // value for the current system that we're running on
\r
439 ///////////////////////////////////////////////////////////////////////////
\r
440 //static void interrupt
\r
442 SDL_TimingService(void)
\r
450 outportb(0x20,0x20); // Ack interrupt
\r
452 #ifdef SD_USECATA3DSETTIMERSPEED
\r
453 ///////////////////////////////////////////////////////////////////////////
\r
455 // SDL_InitDelay() - Sets up TimerDelay's for SDL_Delay()
\r
457 ///////////////////////////////////////////////////////////////////////////
\r
459 SDL_InitDelay(void)
\r
464 setvect(8,SDL_TimingService); // Set to my timer 0 ISR
\r
466 SDL_SetIntsPerSec(1000); // Time 1ms
\r
468 for (i = 0,timer = 0;i < 10;i++) // Do timing test 10 times
\r
471 xor dx,dx // Zero DX
\r
472 mov cx,0xffff // Put starting value in CX
\r
473 mov [TimerDone],cx // TimerDone = false - 1
\r
474 #ifdef __BORLANDC__
\r
478 #ifdef __BORLANDC__
\r
482 jnz startloop // Make sure we're at the start
\r
483 #ifdef __BORLANDC__
\r
487 #ifdef __BORLANDC__
\r
490 test [TimerDone],1 // See if TimerDone flag got hit
\r
491 jnz done // Yep - drop out of the loop
\r
493 #ifdef __BORLANDC__
\r
501 if (0xffff - TimerVal > timer)
\r
502 timer = 0xffff - TimerVal;
\r
504 timer += timer / 2; // Use some slop
\r
505 TimerDelay10 = timer / (1000 / 10);
\r
506 TimerDelay25 = timer / (1000 / 25);
\r
507 TimerDelay100 = timer / (1000 / 100);
\r
509 SDL_SetTimer0(0); // Reset timer 0
\r
511 setvect(8,t0OldService); // Set back to old ISR
\r
514 ///////////////////////////////////////////////////////////////////////////
\r
516 // SDL_Delay() - Delays the specified amount of time
\r
518 ///////////////////////////////////////////////////////////////////////////
\r
520 SDL_Delay(word delay)
\r
527 #ifdef __BORLANDC__
\r
531 #ifdef __BORLANDC__
\r
534 test [TimerDone],0 // Useless code - just for timing equivilency
\r
537 #ifdef __BORLANDC__
\r
550 ///////////////////////////////////////////////////////////////////////////
\r
552 // SDL_PCPlaySample() - Plays the specified sample on the PC speaker
\r
554 ///////////////////////////////////////////////////////////////////////////
\r
560 SDL_PCPlaySample(byte huge *data,dword len)
\r
565 SDL_IndicatePC(true);
\r
567 pcLengthLeft = len;
\r
568 pcSound = (volatile byte far *)data;
\r
573 ///////////////////////////////////////////////////////////////////////////
\r
575 // SDL_PCStopSample() - Stops a sample playing on the PC speaker
\r
577 ///////////////////////////////////////////////////////////////////////////
\r
583 SDL_PCStopSample(void)
\r
588 /*(long)*/pcSound = 0;
\r
590 SDL_IndicatePC(false);
\r
592 asm in al,0x61 // Turn the speaker off
\r
593 asm and al,0xfd // ~2
\r
599 ///////////////////////////////////////////////////////////////////////////
\r
601 // SDL_PCPlaySound() - Plays the specified sound on the PC speaker
\r
603 ///////////////////////////////////////////////////////////////////////////
\r
609 SDL_PCPlaySound(PCSound far *sound)
\r
615 pcLengthLeft = sound->common.length;
\r
616 pcSound = sound->data;
\r
621 ///////////////////////////////////////////////////////////////////////////
\r
623 // SDL_PCStopSound() - Stops the current sound playing on the PC Speaker
\r
625 ///////////////////////////////////////////////////////////////////////////
\r
631 SDL_PCStopSound(void)
\r
636 /*(long)*/pcSound = 0;
\r
638 asm in al,0x61 // Turn the speaker off
\r
639 asm and al,0xfd // ~2
\r
645 ///////////////////////////////////////////////////////////////////////////
\r
647 // SDL_PCService() - Handles playing the next sample in a PC sound
\r
649 ///////////////////////////////////////////////////////////////////////////
\r
651 SDL_PCService(void)
\r
659 if (s != pcLastSample)
\r
665 if (s) // We have a frequency!
\r
667 t = pcSoundLookup[s];
\r
670 asm mov al,0xb6 // Write to channel 2 (speaker) timer
\r
673 asm out 42h,al // Low byte
\r
675 asm out 42h,al // High byte
\r
677 asm in al,0x61 // Turn the speaker & gate on
\r
681 else // Time for some silence
\r
683 asm in al,0x61 // Turn the speaker & gate off
\r
684 asm and al,0xfc // ~3
\r
691 if (!(--pcLengthLeft))
\r
694 SDL_SoundFinished();
\r
699 ///////////////////////////////////////////////////////////////////////////
\r
701 // SDL_ShutPC() - Turns off the pc speaker
\r
703 ///////////////////////////////////////////////////////////////////////////
\r
712 asm in al,0x61 // Turn the speaker & gate off
\r
713 asm and al,0xfc // ~3
\r
720 // Stuff for digitized sounds
\r
723 SDL_LoadDigiSegment(word page, global_game_variables_t *gvar)
\r
727 #if 0 // for debugging
\r
728 asm mov dx,STATUS_REGISTER_1
\r
730 asm mov dx,ATR_INDEX
\r
731 asm mov al,ATR_OVERSCAN
\r
733 asm mov al,10 // bright green
\r
737 addr = PM_GetSoundPage(page);
\r
738 PM_SetPageLock(gvar->pm.fi.PMSoundStart + page,pml_Locked, gvar);
\r
740 #if 0 // for debugging
\r
741 asm mov dx,STATUS_REGISTER_1
\r
743 asm mov dx,ATR_INDEX
\r
744 asm mov al,ATR_OVERSCAN
\r
746 asm mov al,3 // blue
\r
748 asm mov al,0x20 // normal
\r
756 SDL_PlayDigiSegment(memptr addr,word len)
\r
761 SDL_PCPlaySample(addr,len);
\r
763 //SS case sds_SoundSource:
\r
764 //SS SDL_SSPlaySample(addr,len);
\r
766 //SB case sds_SoundBlaster:
\r
767 //SB SDL_SBPlaySample(addr,len);
\r
773 SD_StopDigitized(global_game_variables_t *gvar)
\r
781 DigiNextAddr = nil;
\r
783 DigiMissed = false;
\r
784 DigiPlaying = false;
\r
785 DigiNumber = DigiPriority = 0;
\r
786 SoundPositioned = false;
\r
787 if ((DigiMode == sds_PC) && (SoundMode == sdm_PC))
\r
788 SDL_SoundFinished();
\r
793 SDL_PCStopSample();
\r
795 //SS case sds_SoundSource:
\r
796 //SS SDL_SSStopSample();
\r
798 //SB case sds_SoundBlaster:
\r
799 //SB SDL_SBStopSample();
\r
805 for (i = DigiLastStart;i < DigiLastEnd;i++)
\r
806 PM_SetPageLock(i + gvar->pm.fi.PMSoundStart,pml_Unlocked, gvar);
\r
812 SD_Poll(global_game_variables_t *gvar)
\r
814 if (DigiLeft && !DigiNextAddr)
\r
816 DigiNextLen = (DigiLeft >= PMPageSize)? PMPageSize : (DigiLeft % PMPageSize);
\r
817 DigiLeft -= DigiNextLen;
\r
819 DigiLastSegment = true;
\r
820 DigiNextAddr = SDL_LoadDigiSegment(DigiPage++, gvar);
\r
822 if (DigiMissed && DigiNextAddr)
\r
824 SDL_PlayDigiSegment(DigiNextAddr,DigiNextLen);
\r
825 DigiNextAddr = nil;
\r
826 DigiMissed = false;
\r
827 if (DigiLastSegment)
\r
829 DigiPlaying = false;
\r
830 DigiLastSegment = false;
\r
833 SDL_SetTimerSpeed();
\r
837 SD_SetPosition(int leftpos,int rightpos, global_game_variables_t *gvar)
\r
845 || ((leftpos == 15) && (rightpos == 15))
\r
847 Quit(gvar, "SD_SetPosition: Illegal position");
\r
851 //SB case sds_SoundBlaster:
\r
852 //SB SDL_PositionSBP(leftpos,rightpos);
\r
858 SD_PlayDigitized(word which,int leftpos,int rightpos, global_game_variables_t *gvar)
\r
866 SD_StopDigitized(gvar);
\r
867 if (which >= NumDigi)
\r
868 Quit(gvar, "SD_PlayDigitized: bad sound number");
\r
870 SD_SetPosition(leftpos,rightpos, gvar);
\r
872 DigiPage = DigiList[(which * 2) + 0];
\r
873 DigiLeft = DigiList[(which * 2) + 1];
\r
875 DigiLastStart = DigiPage;
\r
876 DigiLastEnd = DigiPage + ((DigiLeft + (PMPageSize - 1)) / PMPageSize);
\r
878 len = (DigiLeft >= PMPageSize)? PMPageSize : (DigiLeft % PMPageSize);
\r
879 addr = SDL_LoadDigiSegment(DigiPage++, gvar);
\r
881 DigiPlaying = true;
\r
882 DigiLastSegment = false;
\r
884 SDL_PlayDigiSegment(addr,len);
\r
887 DigiLastSegment = true;
\r
893 SDL_DigitizedDone(void)
\r
897 SDL_PlayDigiSegment(DigiNextAddr,DigiNextLen);
\r
898 DigiNextAddr = nil;
\r
899 DigiMissed = false;
\r
903 if (DigiLastSegment)
\r
905 DigiPlaying = false;
\r
906 DigiLastSegment = false;
\r
907 if ((DigiMode == sds_PC) && (SoundMode == sdm_PC))
\r
909 SDL_SoundFinished();
\r
912 DigiNumber = DigiPriority = 0;
\r
913 SoundPositioned = false;
\r
921 SD_SetDigiDevice(SDSMode mode, global_game_variables_t *gvar)
\r
923 boolean devicenotpresent;
\r
925 if (mode == DigiMode)
\r
928 SD_StopDigitized(gvar);
\r
930 devicenotpresent = false;
\r
931 //SBSS switch (mode)
\r
933 //SB case sds_SoundBlaster:
\r
934 //SB if (!SoundBlasterPresent)
\r
936 //SB if (SoundSourcePresent)
\r
937 //SB mode = sds_SoundSource;
\r
939 //SB devicenotpresent = true;
\r
942 //SS case sds_SoundSource:
\r
943 //SS if (!SoundSourcePresent)
\r
944 //SS devicenotpresent = true;
\r
948 if (!devicenotpresent)
\r
950 //SS if (DigiMode == sds_SoundSource)
\r
955 //SS if (mode == sds_SoundSource)
\r
956 //SS SDL_StartSS();
\r
958 SDL_SetTimerSpeed();
\r
963 SDL_SetupDigi(global_game_variables_t *gvar)
\r
970 PM_UnlockMainMem(gvar);
\r
971 MM_GetPtr(&list,PMPageSize, gvar);
\r
972 PM_CheckMainMem(gvar);
\r
973 p = (word far *)MK_FP(PM_GetPage(gvar->pm.fi.ChunksInFile - 1, gvar),0);
\r
974 _fmemcpy((void far *)list,(void far *)p,PMPageSize);
\r
975 pg = gvar->pm.fi.PMSoundStart;
\r
976 for (i = 0;i < PMPageSize / (sizeof(word) * 2);i++,p += 2)
\r
978 if (pg >= gvar->pm.fi.ChunksInFile - 1)
\r
980 pg += (p[1] + (PMPageSize - 1)) / PMPageSize;
\r
982 PM_UnlockMainMem(gvar);
\r
983 MM_GetPtr(MEMPTRCONV DigiList,i * sizeof(word) * 2, gvar);
\r
984 _fmemcpy((void far *)DigiList,(void far *)list,i * sizeof(word) * 2);
\r
985 MM_FreePtr(&list, gvar);
\r
988 for (i = 0;i < LASTSOUND;i++)
\r
994 ///////////////////////////////////////////////////////////////////////////
\r
996 // alOut(n,b) - Puts b in AdLib card register n
\r
998 ///////////////////////////////////////////////////////////////////////////
\r
1000 alOut(byte n,byte b)
\r
1009 SDL_Delay(TimerDelay10);
\r
1026 SDL_Delay(TimerDelay25);
\r
1071 ///////////////////////////////////////////////////////////////////////////
\r
1073 // SDL_SetInstrument() - Puts an instrument into a generator
\r
1075 ///////////////////////////////////////////////////////////////////////////
\r
1078 SDL_SetInstrument(int track,int which,Instrument far *inst,boolean percussive)
\r
1084 c = pcarriers[which];
\r
1085 m = pmodifiers[which];
\r
1089 c = carriers[which];
\r
1090 m = modifiers[which];
\r
1093 tracks[track - 1]->inst = *inst;
\r
1094 tracks[track - 1]->percussive = percussive;
\r
1096 alOut(m + alChar,inst->mChar);
\r
1097 alOut(m + alScale,inst->mScale);
\r
1098 alOut(m + alAttack,inst->mAttack);
\r
1099 alOut(m + alSus,inst->mSus);
\r
1100 alOut(m + alWave,inst->mWave);
\r
1102 // Most percussive instruments only use one cell
\r
1105 alOut(c + alChar,inst->cChar);
\r
1106 alOut(c + alScale,inst->cScale);
\r
1107 alOut(c + alAttack,inst->cAttack);
\r
1108 alOut(c + alSus,inst->cSus);
\r
1109 alOut(c + alWave,inst->cWave);
\r
1112 alOut(which + alFeedCon,inst->nConn); // DEBUG - I think this is right
\r
1116 ///////////////////////////////////////////////////////////////////////////
\r
1118 // SDL_ALStopSound() - Turns off any sound effects playing through the
\r
1121 ///////////////////////////////////////////////////////////////////////////
\r
1127 SDL_ALStopSound(void)
\r
1132 /*(long)*/alSound = 0;
\r
1133 alOut(alFreqH + 0,0);
\r
1139 SDL_AlSetFXInst(Instrument far *inst)
\r
1145 alOut(m + alChar,inst->mChar);
\r
1146 alOut(m + alScale,inst->mScale);
\r
1147 alOut(m + alAttack,inst->mAttack);
\r
1148 alOut(m + alSus,inst->mSus);
\r
1149 alOut(m + alWave,inst->mWave);
\r
1150 alOut(c + alChar,inst->cChar);
\r
1151 alOut(c + alScale,inst->cScale);
\r
1152 alOut(c + alAttack,inst->cAttack);
\r
1153 alOut(c + alSus,inst->cSus);
\r
1154 alOut(c + alWave,inst->cWave);
\r
1156 // Note: Switch commenting on these lines for old MUSE compatibility
\r
1157 // alOut(alFeedCon,inst->nConn);
\r
1158 alOut(alFeedCon,0);
\r
1161 ///////////////////////////////////////////////////////////////////////////
\r
1163 // SDL_ALPlaySound() - Plays the specified sound on the AdLib card
\r
1165 ///////////////////////////////////////////////////////////////////////////
\r
1171 SDL_ALPlaySound(AdLibSound far *sound, global_game_variables_t *gvar)
\r
1173 Instrument __far *inst;
\r
1176 SDL_ALStopSound();
\r
1181 alLengthLeft = sound->common.length;
\r
1182 data = sound->data;
\r
1185 alSound = (byte far *)data;
\r
1186 alBlock = ((sound->block & 7) << 2) | 0x20;
\r
1187 inst = &sound->inst;
\r
1189 if (!(inst->mSus | inst->cSus))
\r
1192 Quit(gvar, "SDL_ALPlaySound() - Bad instrument");
\r
1195 SDL_AlSetFXInst(&alZeroInst); // DEBUG
\r
1196 SDL_AlSetFXInst(inst);
\r
1202 ///////////////////////////////////////////////////////////////////////////
\r
1204 // SDL_ALSoundService() - Plays the next sample out through the AdLib card
\r
1206 ///////////////////////////////////////////////////////////////////////////
\r
1209 SDL_ALSoundService(void)
\r
1217 alOut(alFreqH + 0,0);
\r
1220 alOut(alFreqL + 0,s);
\r
1221 alOut(alFreqH + 0,alBlock);
\r
1224 if (!(--alLengthLeft))
\r
1226 /*(long)*/alSound = 0;
\r
1227 alOut(alFreqH + 0,0);
\r
1228 SDL_SoundFinished();
\r
1235 ///////////////////////////////////////////////////////////////////////////
\r
1237 // SDL_SelectMeasure() - sets up sequencing variables for a given track
\r
1239 ///////////////////////////////////////////////////////////////////////////
\r
1241 SDL_SelectMeasure(ActiveTrack *track)
\r
1243 track->seq = track->moods[track->mood];
\r
1244 track->nextevent = 0;
\r
1250 SDL_ALService(void)
\r
1260 while (sqHackLen && (sqHackTime <= alTimeCount))
\r
1263 sqHackTime = alTimeCount + *sqHackPtr++;
\r
1273 sqHackPtr = (word far *)sqHack;
\r
1274 sqHackLen = sqHackSeqLen;
\r
1275 alTimeCount = sqHackTime = 0;
\r
1280 ///////////////////////////////////////////////////////////////////////////
\r
1282 // SDL_ShutAL() - Shuts down the AdLib card for sound effects
\r
1284 ///////////////////////////////////////////////////////////////////////////
\r
1291 alOut(alEffects,0);
\r
1292 alOut(alFreqH + 0,0);
\r
1293 SDL_AlSetFXInst(&alZeroInst);
\r
1299 ///////////////////////////////////////////////////////////////////////////
\r
1301 // SDL_CleanAL() - Totally shuts down the AdLib card
\r
1303 ///////////////////////////////////////////////////////////////////////////
\r
1312 alOut(alEffects,0);
\r
1313 for (i = 1;i < 0xf5;i++)
\r
1319 ///////////////////////////////////////////////////////////////////////////
\r
1321 // SDL_StartAL() - Starts up the AdLib card for sound effects
\r
1323 ///////////////////////////////////////////////////////////////////////////
\r
1328 alOut(alEffects,alFXReg);
\r
1329 SDL_AlSetFXInst(&alZeroInst);
\r
1332 ///////////////////////////////////////////////////////////////////////////
\r
1334 // SDL_DetectAdLib() - Determines if there's an AdLib (or SoundBlaster
\r
1335 // emulating an AdLib) present
\r
1337 ///////////////////////////////////////////////////////////////////////////
\r
1339 SDL_DetectAdLib(void)
\r
1341 byte status1,status2;
\r
1344 alOut(4,0x60); // Reset T1 & T2
\r
1345 alOut(4,0x80); // Reset IRQ
\r
1346 status1 = readstat();
\r
1347 alOut(2,0xff); // Set timer 1
\r
1348 alOut(4,0x21); // Start timer 1
\r
1349 SDL_Delay(TimerDelay100);
\r
1355 #ifdef __BORLANDC__
\r
1359 #ifdef __BORLANDC__
\r
1367 status2 = readstat();
\r
1371 if (((status1 & 0xe0) == 0x00) && ((status2 & 0xe0) == 0xc0))
\r
1373 for (i = 1;i <= 0xf5;i++) // Zero all the registers
\r
1376 alOut(1,0x20); // Set WSE=1
\r
1377 alOut(8,0); // Set CSM=0 & SEL=0
\r
1385 ///////////////////////////////////////////////////////////////////////////
\r
1387 // SDL_t0Service() - My timer 0 ISR which handles the different timings and
\r
1388 // dispatches to whatever other routines are appropriate
\r
1390 ///////////////////////////////////////////////////////////////////////////
\r
1391 //static void interrupt
\r
1393 SDL_t0Service(void)
\r
1395 static word count = 1;
\r
1396 boolean myackflag = 0;
\r
1398 //00#if 0 // for debugging
\r
1399 asm mov dx,STATUS_REGISTER_1
\r
1401 asm mov dx,ATR_INDEX
\r
1402 asm mov al,ATR_OVERSCAN
\r
1404 asm mov al,4 // red
\r
1410 if ((MusicMode == smm_AdLib) || (DigiMode == sds_SoundSource))
\r
1413 //SS SDL_SSService();
\r
1414 // if (!(++count & 7))
\r
1415 if (!(++count % 10))
\r
1419 if (SoundUserHook)
\r
1422 // if (!(count & 3))
\r
1425 switch (SoundMode)
\r
1431 SDL_ALSoundService();
\r
1438 if (!(++count & 1))
\r
1442 if (SoundUserHook)
\r
1445 switch (SoundMode)
\r
1451 SDL_ALSoundService();
\r
1457 mov ax,[WORD PTR TimerCount]
\r
1458 add ax,[WORD PTR TimerDivisor]
\r
1459 mov [WORD PTR TimerCount],ax
\r
1462 #ifdef __BORLANDC__
\r
1466 #ifdef __BORLANDC__
\r
1470 #ifdef __BORLANDC__
\r
1474 #ifdef __WATCOMC__
\r
1478 t0OldService(); // If we overflow a word, time to call old int handler
\r
1480 outportb(0x20,0x20); // Ack the interrupt
\r
1482 //00#if 0 // for debugging
\r
1483 asm mov dx,STATUS_REGISTER_1
\r
1485 asm mov dx,ATR_INDEX
\r
1486 asm mov al,ATR_OVERSCAN
\r
1488 asm mov al,3 // blue
\r
1490 asm mov al,0x20 // normal
\r
1495 ////////////////////////////////////////////////////////////////////////////
\r
1497 // SDL_ShutDevice() - turns off whatever device was being used for sound fx
\r
1499 ////////////////////////////////////////////////////////////////////////////
\r
1501 SDL_ShutDevice(void)
\r
1503 switch (SoundMode)
\r
1512 SoundMode = sdm_Off;
\r
1515 ///////////////////////////////////////////////////////////////////////////
\r
1517 // SDL_CleanDevice() - totally shuts down all sound devices
\r
1519 ///////////////////////////////////////////////////////////////////////////
\r
1521 SDL_CleanDevice(void)
\r
1523 if ((SoundMode == sdm_AdLib) || (MusicMode == smm_AdLib))
\r
1527 ///////////////////////////////////////////////////////////////////////////
\r
1529 // SDL_StartDevice() - turns on whatever device is to be used for sound fx
\r
1531 ///////////////////////////////////////////////////////////////////////////
\r
1533 SDL_StartDevice(void)
\r
1535 switch (SoundMode)
\r
1541 SoundNumber = SoundPriority = 0;
\r
1545 SDL_SetTimerSpeed(void)
\r
1549 if (MusicMode == smm_AdLib)
\r
1550 rate = TickBase * 8;
\r
1552 rate = TickBase * 2;
\r
1553 SDL_SetIntsPerSec(rate);
\r
1556 // Public routines
\r
1558 ///////////////////////////////////////////////////////////////////////////
\r
1560 // SD_SetSoundMode() - Sets which sound hardware to use for sound effects
\r
1562 ///////////////////////////////////////////////////////////////////////////
\r
1564 SD_SetSoundMode(SDMode mode, global_game_variables_t *gvar)
\r
1566 boolean result = false;
\r
1569 SD_StopSound(gvar);
\r
1572 if ((mode == sdm_AdLib) && !AdLibPresent)
\r
1578 NeedsDigitized = false;
\r
1582 tableoffset = STARTPCSOUNDS;
\r
1583 NeedsDigitized = false;
\r
1589 tableoffset = STARTADLIBSOUNDS;
\r
1590 NeedsDigitized = false;
\r
1599 if (result && (mode != SoundMode))
\r
1604 SoundTable = (word *)(&gvar->ca.audiosegs[tableoffset]);
\r
1606 SDL_StartDevice();
\r
1609 SDL_SetTimerSpeed();
\r
1614 ///////////////////////////////////////////////////////////////////////////
\r
1616 // SD_SetMusicMode() - sets the device to use for background music
\r
1618 ///////////////////////////////////////////////////////////////////////////
\r
1620 SD_SetMusicMode(SMMode mode)
\r
1622 boolean result = false;
\r
1624 SD_FadeOutMusic();
\r
1625 while (SD_MusicPlaying())
\r
1631 NeedsMusic = false;
\r
1637 NeedsMusic = true;
\r
1646 SDL_SetTimerSpeed();
\r
1651 ///////////////////////////////////////////////////////////////////////////
\r
1653 // SD_Startup() - starts up the Sound Mgr
\r
1654 // Detects all additional sound hardware and installs my ISR
\r
1656 ///////////////////////////////////////////////////////////////////////////
\r
1658 SD_Startup(global_game_variables_t *gvar)
\r
1664 #ifndef SD_USECATA3DSETTIMERSPEED
\r
1667 ssIsTandy = false;
\r
1668 //SS ssNoCheck = false;
\r
1669 alNoCheck = false;
\r
1670 //SB sbNoCheck = false;
\r
1671 //SB sbNoProCheck = false;
\r
1673 for (i = 1;i < _argc;i++)
\r
1675 switch (US_CheckParm(_argv[i],ParmStrings))
\r
1677 case 0: // No AdLib detection
\r
1680 //SB case 1: // No SoundBlaster detection
\r
1681 //SB sbNoCheck = true;
\r
1683 //SB case 2: // No SoundBlaster Pro detection
\r
1684 //SB sbNoProCheck = true;
\r
1687 //SS ssNoCheck = true; // No Sound Source detection
\r
1689 case 4: // Tandy Sound Source handling
\r
1692 //SS case 5: // Sound Source present at LPT1
\r
1694 //SS ssNoCheck = SoundSourcePresent = true;
\r
1696 //SS case 6: // Sound Source present at LPT2
\r
1698 //SS ssNoCheck = SoundSourcePresent = true;
\r
1700 //SS case 7: // Sound Source present at LPT3
\r
1702 //SS ssNoCheck = SoundSourcePresent = true;
\r
1708 SoundUserHook = 0;
\r
1710 t0OldService = getvect(8); // Get old timer 0 ISR
\r
1711 #ifdef SD_USECATA3DSETTIMERSPEED
\r
1712 //++++ SDL_InitDelay(); // SDL_InitDelay() uses t0OldService
\r
1714 setvect(8,SDL_t0Service); // Set to my timer 0 ISR
\r
1716 LocalTime = TimeCount = alTimeCount = 0;
\r
1718 SD_SetSoundMode(sdm_Off, gvar);
\r
1719 SD_SetMusicMode(smm_Off);
\r
1721 //SS if (!ssNoCheck)
\r
1722 //SS SoundSourcePresent = SDL_DetectSoundSource();
\r
1726 AdLibPresent = SDL_DetectAdLib();
\r
1727 //SB if (AdLibPresent) && !sbNoCheck)
\r
1729 //SB int port = -1;
\r
1730 //SB char *env = getenv("BLASTER");
\r
1736 //SB while (isspace(*env))
\r
1739 //SB switch (toupper(*env))
\r
1742 //SB temp = strtol(env + 1,&env,16);
\r
1745 //SB (temp >= 0x210)
\r
1746 //SB && (temp <= 0x260)
\r
1747 //SB && (!(temp & 0x00f))
\r
1749 //SB port = (temp - 0x200) >> 4;
\r
1751 //SB Quit(gvar, "SD_Startup: Unsupported address value in BLASTER");
\r
1754 //SB temp = strtol(env + 1,&env,10);
\r
1758 //SB && (temp <= 10)
\r
1759 //SB && (sbIntVectors[temp] != -1)
\r
1762 //SB sbInterrupt = temp;
\r
1763 //SB sbIntVec = sbIntVectors[sbInterrupt];
\r
1766 //SB Quit(gvar, "SD_Startup: Unsupported interrupt value in BLASTER");
\r
1769 //SB temp = strtol(env + 1,&env,10);
\r
1770 //SB if ((temp == 0) || (temp == 1) || (temp == 3))
\r
1771 //SB SDL_SBSetDMA(temp);
\r
1773 //SB Quit(gvar, "SD_Startup: Unsupported DMA value in BLASTER");
\r
1776 //SB while (isspace(*env))
\r
1778 //SB while (*env && !isspace(*env))
\r
1784 //SB SoundBlasterPresent = SDL_DetectSoundBlaster(port);
\r
1788 for (i = 0;i < 255;i++)
\r
1789 pcSoundLookup[i] = i * 60;
\r
1791 //SB if (SoundBlasterPresent)
\r
1792 //SB SDL_StartSB();
\r
1794 //++++ SDL_SetupDigi(gvar);
\r
1796 SD_Started = true;
\r
1799 ///////////////////////////////////////////////////////////////////////////
\r
1801 // SD_Default() - Sets up the default behaviour for the Sound Mgr whether
\r
1802 // the config file was present or not.
\r
1804 ///////////////////////////////////////////////////////////////////////////
\r
1806 SD_Default(boolean gotit,SDMode sd,SMMode sm, global_game_variables_t *gvar)
\r
1808 boolean gotsd,gotsm;
\r
1810 gotsd = gotsm = gotit;
\r
1812 if (gotsd) // Make sure requested sound hardware is available
\r
1817 gotsd = AdLibPresent;
\r
1828 if (sd != SoundMode)
\r
1829 SD_SetSoundMode(sd, gvar);
\r
1832 if (gotsm) // Make sure requested music hardware is available
\r
1837 gotsm = AdLibPresent;
\r
1846 if (sm != MusicMode)
\r
1847 SD_SetMusicMode(sm);
\r
1850 ///////////////////////////////////////////////////////////////////////////
\r
1852 // SD_Shutdown() - shuts down the Sound Mgr
\r
1853 // Removes sound ISR and turns off whatever sound hardware was active
\r
1855 ///////////////////////////////////////////////////////////////////////////
\r
1857 SD_Shutdown(global_game_variables_t *gvar)
\r
1863 SD_StopSound(gvar);
\r
1865 SDL_CleanDevice();
\r
1867 //SB if (SoundBlasterPresent)
\r
1868 //SB SDL_ShutSB();
\r
1870 //SS if (SoundSourcePresent)
\r
1871 //SS SDL_ShutSS();
\r
1878 setvect(8,t0OldService);
\r
1882 SD_Started = false;
\r
1885 ///////////////////////////////////////////////////////////////////////////
\r
1887 // SD_SetUserHook() - sets the routine that the Sound Mgr calls every 1/70th
\r
1888 // of a second from its timer 0 ISR
\r
1890 ///////////////////////////////////////////////////////////////////////////
\r
1892 SD_SetUserHook(void (* hook)(void))
\r
1894 SoundUserHook = hook;
\r
1897 ///////////////////////////////////////////////////////////////////////////
\r
1899 // SD_PositionSound() - Sets up a stereo imaging location for the next
\r
1900 // sound to be played. Each channel ranges from 0 to 15.
\r
1902 ///////////////////////////////////////////////////////////////////////////
\r
1904 SD_PositionSound(int leftvol,int rightvol)
\r
1906 LeftPosition = leftvol;
\r
1907 RightPosition = rightvol;
\r
1908 nextsoundpos = true;
\r
1911 ///////////////////////////////////////////////////////////////////////////
\r
1913 // SD_PlaySound() - plays the specified sound on the appropriate hardware
\r
1915 ///////////////////////////////////////////////////////////////////////////
\r
1917 SD_PlaySound(soundnames sound, global_game_variables_t *gvar)
\r
1920 SoundCommon far *s;
\r
1923 lp = LeftPosition;
\r
1924 rp = RightPosition;
\r
1926 RightPosition = 0;
\r
1928 ispos = nextsoundpos;
\r
1929 nextsoundpos = false;
\r
1934 s = MK_FP(SoundTable[sound],0);
\r
1935 if ((SoundMode != sdm_Off) && !s)
\r
1936 Quit(gvar, "SD_PlaySound() - Uncached sound");
\r
1938 if ((DigiMode != sds_Off) && (DigiMap[sound] != -1))
\r
1940 if ((DigiMode == sds_PC) && (SoundMode == sdm_PC))
\r
1942 if (s->priority < SoundPriority)
\r
1945 SDL_PCStopSound();
\r
1947 SD_PlayDigitized(DigiMap[sound],lp,rp, gvar);
\r
1948 SoundPositioned = ispos;
\r
1949 SoundNumber = sound;
\r
1950 SoundPriority = s->priority;
\r
1956 if (DigiPriority && !DigiNumber)
\r
1959 Quit(gvar, "SD_PlaySound: Priority without a sound");
\r
1963 if (s->priority < DigiPriority)
\r
1966 SD_PlayDigitized(DigiMap[sound],lp,rp, gvar);
\r
1967 SoundPositioned = ispos;
\r
1968 DigiNumber = sound;
\r
1969 DigiPriority = s->priority;
\r
1975 if (SoundMode == sdm_Off)
\r
1978 Quit(gvar, "SD_PlaySound() - Zero length sound");
\r
1979 if (s->priority < SoundPriority)
\r
1982 switch (SoundMode)
\r
1985 SDL_PCPlaySound((void far *)s);
\r
1988 SDL_ALPlaySound((void far *)s, gvar);
\r
1992 SoundNumber = sound;
\r
1993 SoundPriority = s->priority;
\r
1998 ///////////////////////////////////////////////////////////////////////////
\r
2000 // SD_SoundPlaying() - returns the sound number that's playing, or 0 if
\r
2001 // no sound is playing
\r
2003 ///////////////////////////////////////////////////////////////////////////
\r
2005 SD_SoundPlaying(void)
\r
2007 boolean result = false;
\r
2009 switch (SoundMode)
\r
2012 result = pcSound? true : false;
\r
2015 result = alSound? true : false;
\r
2020 return(SoundNumber);
\r
2025 ///////////////////////////////////////////////////////////////////////////
\r
2027 // SD_StopSound() - if a sound is playing, stops it
\r
2029 ///////////////////////////////////////////////////////////////////////////
\r
2031 SD_StopSound(global_game_variables_t *gvar)
\r
2034 SD_StopDigitized(gvar);
\r
2036 switch (SoundMode)
\r
2039 SDL_PCStopSound();
\r
2042 SDL_ALStopSound();
\r
2046 SoundPositioned = false;
\r
2048 SDL_SoundFinished();
\r
2051 ///////////////////////////////////////////////////////////////////////////
\r
2053 // SD_WaitSoundDone() - waits until the current sound is done playing
\r
2055 ///////////////////////////////////////////////////////////////////////////
\r
2057 SD_WaitSoundDone(void)
\r
2059 while (SD_SoundPlaying())
\r
2063 ///////////////////////////////////////////////////////////////////////////
\r
2065 // SD_MusicOn() - turns on the sequencer
\r
2067 ///////////////////////////////////////////////////////////////////////////
\r
2074 ///////////////////////////////////////////////////////////////////////////
\r
2076 // SD_MusicOff() - turns off the sequencer and any playing notes
\r
2078 ///////////////////////////////////////////////////////////////////////////
\r
2085 switch (MusicMode)
\r
2089 alOut(alEffects,0);
\r
2090 for (i = 0;i < sqMaxTracks;i++)
\r
2091 alOut(alFreqH + i + 1,0);
\r
2097 ///////////////////////////////////////////////////////////////////////////
\r
2099 // SD_StartMusic() - starts playing the music pointed to
\r
2101 ///////////////////////////////////////////////////////////////////////////
\r
2103 SD_StartMusic(MusicGroup far *music)
\r
2109 if (MusicMode == smm_AdLib)
\r
2111 sqHackPtr = sqHack = music->values;
\r
2112 sqHackSeqLen = sqHackLen = music->length;
\r
2121 ///////////////////////////////////////////////////////////////////////////
\r
2123 // SD_FadeOutMusic() - starts fading out the music. Call SD_MusicPlaying()
\r
2124 // to see if the fadeout is complete
\r
2126 ///////////////////////////////////////////////////////////////////////////
\r
2128 SD_FadeOutMusic(void)
\r
2130 switch (MusicMode)
\r
2133 // DEBUG - quick hack to turn the music off
\r
2139 ///////////////////////////////////////////////////////////////////////////
\r
2141 // SD_MusicPlaying() - returns true if music is currently playing, false if
\r
2144 ///////////////////////////////////////////////////////////////////////////
\r
2146 SD_MusicPlaying(void)
\r
2150 switch (MusicMode)
\r
2154 // DEBUG - not written
\r
2168 //variables for these assembly functions
\r
2169 volatile boolean pcindicate;
\r
2170 volatile unsigned/*boolean*/ MyDS;
\r
2183 // Timer 0 ISR for 7000Hz interrupts
\r
2184 void interrupt SDL_t0ExtremeAsmService(void)
\r
2190 SDL_setPCSpeaker(((*pcSound++)&0x80)>>6);
\r
2195 SDL_turnOffPCSpeaker();
\r
2196 SDL_DigitizedDoneInIRQ();
\r
2212 void SDL_IndicatePC(boolean ind)
\r
2218 SDL_DigitizedDoneInIRQ(void)
\r
2222 SDL_PlayDigiSegment(DigiNextAddr,DigiNextLen/*,true*/);
\r
2223 DigiNextAddr = nil;
\r
2224 DigiMissed = false;
\r
2228 if (DigiLastSegment)
\r
2230 DigiPlaying = false;
\r
2231 DigiLastSegment = false;
\r
2232 if ((DigiMode == sds_PC) && (SoundMode == sdm_PC))
\r
2234 SDL_SoundFinished();
\r
2238 DigiNumber = (soundnames) 0;
\r
2241 SoundPositioned = false;
\r
2244 DigiMissed = true;
\r
2249 // Inside an interrupt handler interrupts should already be disabled
\r
2250 // so don't disable them again and cause V86 exceptions which cost
\r
2251 // aprox. 300 processor tics!
\r
2254 void alOutInIRQ(byte n,byte b)
\r