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 // Imports from ID_SD_A.ASM
\r
74 /*extern*/ void SDL_SetDS(void),
\r
75 SDL_IndicatePC(boolean on);
\r
76 /*extern*/ void interrupt SDL_t0ExtremeAsmService(void),
\r
77 SDL_t0FastAsmService(void),
\r
78 SDL_t0SlowAsmService(void);
\r
81 boolean SoundSourcePresent,
\r
83 SoundBlasterPresent,SBProPresent,
\r
84 NeedsDigitized,NeedsMusic,
\r
91 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
114 static void (*SoundUserHook)(void);
\r
115 soundnames SoundNumber,DigiNumber;
\r
116 word SoundPriority,DigiPriority;
\r
117 int LeftPosition,RightPosition;
\r
118 void interrupt (*t0OldService)(void);
\r
122 word NumDigi,DigiLeft,DigiPage;
\r
123 word _seg *DigiList;
\r
124 word DigiLastStart,DigiLastEnd;
\r
125 boolean DigiPlaying;
\r
126 static boolean DigiMissed,DigiLastSegment;
\r
127 static memptr DigiNextAddr;
\r
128 static word DigiNextLen;
\r
131 // SoundBlaster variables
\r
132 static boolean sbNoCheck,sbNoProCheck;
\r
133 static volatile boolean sbSamplePlaying;
\r
134 static byte sbOldIntMask = -1;
\r
135 static volatile byte huge *sbNextSegPtr;
\r
136 static byte sbDMA = 1,
\r
137 sbDMAa1 = 0x83,sbDMAa2 = 2,sbDMAa3 = 3,
\r
138 sba1Vals[] = {0x87,0x83,0,0x82},
\r
139 sba2Vals[] = {0,2,0,6},
\r
140 sba3Vals[] = {1,3,0,7};
\r
141 static int sbLocation = -1,sbInterrupt = 7,sbIntVec = 0xf,
\r
142 sbIntVectors[] = {-1,-1,0xa,0xb,-1,0xd,-1,0xf,-1,-1,-1};
\r
143 static volatile dword sbNextSegLen;
\r
144 static volatile SampledSound huge *sbSamples;
\r
145 static void interrupt (*sbOldIntHand)(void);
\r
146 static byte sbpOldFMMix,sbpOldVOCMix;
\r
148 // SoundSource variables
\r
151 word ssControl,ssStatus,ssData;
\r
153 volatile byte far *ssSample;
\r
154 volatile dword ssLengthLeft;
\r
157 // PC Sound variables
\r
158 volatile byte pcLastSample,far *pcSound;
\r
159 dword pcLengthLeft;
\r
160 word pcSoundLookup[255];
\r
166 dword alLengthLeft;
\r
168 Instrument alZeroInst;
\r
170 // This table maps channel numbers to carrier and modulator op cells
\r
171 static byte carriers[9] = { 3, 4, 5,11,12,13,19,20,21},
\r
172 modifiers[9] = { 0, 1, 2, 8, 9,10,16,17,18},
\r
173 // This table maps percussive voice numbers to op cells
\r
174 pcarriers[5] = {19,0xff,0xff,0xff,0xff},
\r
175 pmodifiers[5] = {16,17,18,20,21};
\r
177 // Sequencer variables
\r
179 static word alFXReg;
\r
180 static ActiveTrack *tracks[sqMaxTracks];//,
\r
181 //-- mytracks[sqMaxTracks];
\r
182 static word sqMode,sqFadeStep;
\r
183 word far *sqHack,far *sqHackPtr,sqHackLen,sqHackSeqLen;
\r
186 // Internal routines
\r
187 void SDL_DigitizedDone(void);
\r
189 ///////////////////////////////////////////////////////////////////////////
\r
191 // SDL_SetTimer0() - Sets system timer 0 to the specified speed
\r
193 ///////////////////////////////////////////////////////////////////////////
\r
196 SDL_SetTimer0(word speed)
\r
198 #ifndef TPROF // If using Borland's profiling, don't screw with the timer
\r
202 outportb(0x43,0x36); // Change timer 0
\r
203 outportb(0x40,speed);
\r
204 outportb(0x40,speed >> 8);
\r
205 // Kludge to handle special case for digitized PC sounds
\r
206 if (TimerDivisor == (1192030 / (TickBase * 100)))
\r
207 TimerDivisor = (1192030 / (TickBase * 10));
\r
209 TimerDivisor = speed;
\r
213 TimerDivisor = 0x10000;
\r
217 ///////////////////////////////////////////////////////////////////////////
\r
219 // SDL_SetIntsPerSec() - Uses SDL_SetTimer0() to set the number of
\r
220 // interrupts generated by system timer 0 per second
\r
222 ///////////////////////////////////////////////////////////////////////////
\r
224 SDL_SetIntsPerSec(word ints)
\r
227 SDL_SetTimer0(1192030 / ints);
\r
231 SDL_SetTimerSpeed(void)
\r
234 void interrupt (*isr)(void);
\r
236 if ((DigiMode == sds_PC) && DigiPlaying)
\r
238 rate = TickBase * 100;
\r
239 isr = SDL_t0ExtremeAsmService;
\r
243 (MusicMode == smm_AdLib)
\r
244 || ((DigiMode == sds_SoundSource) && DigiPlaying)
\r
247 rate = TickBase * 10;
\r
248 isr = SDL_t0FastAsmService;
\r
252 rate = TickBase * 2;
\r
253 isr = SDL_t0SlowAsmService;
\r
256 if (rate != TimerRate)
\r
259 SDL_SetIntsPerSec(rate);
\r
264 ///////////////////////////////////////////////////////////////////////////
\r
266 // SDL_TimingService() - Used by SDL_InitDelay() to determine a timing
\r
267 // value for the current system that we're running on
\r
269 ///////////////////////////////////////////////////////////////////////////
\r
270 //static void interrupt
\r
272 SDL_TimingService(void)
\r
280 outportb(0x20,0x20); // Ack interrupt
\r
283 ///////////////////////////////////////////////////////////////////////////
\r
285 // SDL_InitDelay() - Sets up TimerDelay's for SDL_Delay()
\r
287 ///////////////////////////////////////////////////////////////////////////
\r
289 SDL_InitDelay(void)
\r
294 setvect(8,SDL_TimingService); // Set to my timer 0 ISR
\r
296 SDL_SetIntsPerSec(1000); // Time 1ms
\r
298 for (i = 0,timer = 0;i < 10;i++) // Do timing test 10 times
\r
301 xor dx,dx // Zero DX
\r
302 mov cx,0xffff // Put starting value in CX
\r
303 mov [TimerDone],cx // TimerDone = false - 1
\r
304 #ifdef __BORLANDC__
\r
308 #ifdef __BORLANDC__
\r
312 jnz startloop // Make sure we're at the start
\r
313 #ifdef __BORLANDC__
\r
317 #ifdef __BORLANDC__
\r
320 test [TimerDone],1 // See if TimerDone flag got hit
\r
321 jnz done // Yep - drop out of the loop
\r
323 #ifdef __BORLANDC__
\r
331 if (0xffff - TimerVal > timer)
\r
332 timer = 0xffff - TimerVal;
\r
334 timer += timer / 2; // Use some slop
\r
335 TimerDelay10 = timer / (1000 / 10);
\r
336 TimerDelay25 = timer / (1000 / 25);
\r
337 TimerDelay100 = timer / (1000 / 100);
\r
339 SDL_SetTimer0(0); // Reset timer 0
\r
341 setvect(8,t0OldService); // Set back to old ISR
\r
344 ///////////////////////////////////////////////////////////////////////////
\r
346 // SDL_Delay() - Delays the specified amount of time
\r
348 ///////////////////////////////////////////////////////////////////////////
\r
350 SDL_Delay(word delay)
\r
357 #ifdef __BORLANDC__
\r
361 #ifdef __BORLANDC__
\r
364 test [TimerDone],0 // Useless code - just for timing equivilency
\r
367 #ifdef __BORLANDC__
\r
380 ///////////////////////////////////////////////////////////////////////////
\r
382 // SDL_PCPlaySample() - Plays the specified sample on the PC speaker
\r
384 ///////////////////////////////////////////////////////////////////////////
\r
390 SDL_PCPlaySample(byte huge *data,dword len)
\r
395 SDL_IndicatePC(true);
\r
397 pcLengthLeft = len;
\r
398 pcSound = (volatile byte far *)data;
\r
403 ///////////////////////////////////////////////////////////////////////////
\r
405 // SDL_PCStopSample() - Stops a sample playing on the PC speaker
\r
407 ///////////////////////////////////////////////////////////////////////////
\r
413 SDL_PCStopSample(void)
\r
418 /*(long)*/pcSound = 0;
\r
420 SDL_IndicatePC(false);
\r
422 asm in al,0x61 // Turn the speaker off
\r
423 asm and al,0xfd // ~2
\r
429 ///////////////////////////////////////////////////////////////////////////
\r
431 // SDL_PCPlaySound() - Plays the specified sound on the PC speaker
\r
433 ///////////////////////////////////////////////////////////////////////////
\r
439 SDL_PCPlaySound(PCSound far *sound)
\r
445 pcLengthLeft = sound->common.length;
\r
446 pcSound = sound->data;
\r
451 ///////////////////////////////////////////////////////////////////////////
\r
453 // SDL_PCStopSound() - Stops the current sound playing on the PC Speaker
\r
455 ///////////////////////////////////////////////////////////////////////////
\r
461 SDL_PCStopSound(void)
\r
466 /*(long)*/pcSound = 0;
\r
468 asm in al,0x61 // Turn the speaker off
\r
469 asm and al,0xfd // ~2
\r
475 ///////////////////////////////////////////////////////////////////////////
\r
477 // SDL_PCService() - Handles playing the next sample in a PC sound
\r
479 ///////////////////////////////////////////////////////////////////////////
\r
481 SDL_PCService(void)
\r
489 if (s != pcLastSample)
\r
495 if (s) // We have a frequency!
\r
497 t = pcSoundLookup[s];
\r
500 asm mov al,0xb6 // Write to channel 2 (speaker) timer
\r
503 asm out 42h,al // Low byte
\r
505 asm out 42h,al // High byte
\r
507 asm in al,0x61 // Turn the speaker & gate on
\r
511 else // Time for some silence
\r
513 asm in al,0x61 // Turn the speaker & gate off
\r
514 asm and al,0xfc // ~3
\r
521 if (!(--pcLengthLeft))
\r
524 SDL_SoundFinished();
\r
529 ///////////////////////////////////////////////////////////////////////////
\r
531 // SDL_ShutPC() - Turns off the pc speaker
\r
533 ///////////////////////////////////////////////////////////////////////////
\r
542 asm in al,0x61 // Turn the speaker & gate off
\r
543 asm and al,0xfc // ~3
\r
550 // Stuff for digitized sounds
\r
553 SDL_LoadDigiSegment(word page, global_game_variables_t *gvar)
\r
557 #if 0 // for debugging
\r
558 asm mov dx,STATUS_REGISTER_1
\r
560 asm mov dx,ATR_INDEX
\r
561 asm mov al,ATR_OVERSCAN
\r
563 asm mov al,10 // bright green
\r
567 addr = PM_GetSoundPage(page);
\r
568 PM_SetPageLock(gvar->pm.fi.PMSoundStart + page,pml_Locked, gvar);
\r
570 #if 0 // for debugging
\r
571 asm mov dx,STATUS_REGISTER_1
\r
573 asm mov dx,ATR_INDEX
\r
574 asm mov al,ATR_OVERSCAN
\r
576 asm mov al,3 // blue
\r
578 asm mov al,0x20 // normal
\r
586 SDL_PlayDigiSegment(memptr addr,word len)
\r
591 SDL_PCPlaySample(addr,len);
\r
593 //SS case sds_SoundSource:
\r
594 //SS SDL_SSPlaySample(addr,len);
\r
596 //SB case sds_SoundBlaster:
\r
597 //SB SDL_SBPlaySample(addr,len);
\r
603 SD_StopDigitized(global_game_variables_t *gvar)
\r
611 DigiNextAddr = nil;
\r
613 DigiMissed = false;
\r
614 DigiPlaying = false;
\r
615 DigiNumber = DigiPriority = 0;
\r
616 SoundPositioned = false;
\r
617 if ((DigiMode == sds_PC) && (SoundMode == sdm_PC))
\r
618 SDL_SoundFinished();
\r
623 SDL_PCStopSample();
\r
625 //SS case sds_SoundSource:
\r
626 //SS SDL_SSStopSample();
\r
628 //SB case sds_SoundBlaster:
\r
629 //SB SDL_SBStopSample();
\r
635 for (i = DigiLastStart;i < DigiLastEnd;i++)
\r
636 PM_SetPageLock(i + gvar->pm.fi.PMSoundStart,pml_Unlocked, gvar);
\r
642 SD_Poll(global_game_variables_t *gvar)
\r
644 if (DigiLeft && !DigiNextAddr)
\r
646 DigiNextLen = (DigiLeft >= PMPageSize)? PMPageSize : (DigiLeft % PMPageSize);
\r
647 DigiLeft -= DigiNextLen;
\r
649 DigiLastSegment = true;
\r
650 DigiNextAddr = SDL_LoadDigiSegment(DigiPage++, gvar);
\r
652 if (DigiMissed && DigiNextAddr)
\r
654 SDL_PlayDigiSegment(DigiNextAddr,DigiNextLen);
\r
655 DigiNextAddr = nil;
\r
656 DigiMissed = false;
\r
657 if (DigiLastSegment)
\r
659 DigiPlaying = false;
\r
660 DigiLastSegment = false;
\r
663 SDL_SetTimerSpeed();
\r
667 SD_SetPosition(int leftpos,int rightpos, global_game_variables_t *gvar)
\r
675 || ((leftpos == 15) && (rightpos == 15))
\r
677 Quit(gvar, "SD_SetPosition: Illegal position");
\r
681 //SB case sds_SoundBlaster:
\r
682 //SB SDL_PositionSBP(leftpos,rightpos);
\r
688 SD_PlayDigitized(word which,int leftpos,int rightpos, global_game_variables_t *gvar)
\r
696 SD_StopDigitized(gvar);
\r
697 if (which >= NumDigi)
\r
698 Quit(gvar, "SD_PlayDigitized: bad sound number");
\r
700 SD_SetPosition(leftpos,rightpos, gvar);
\r
702 DigiPage = DigiList[(which * 2) + 0];
\r
703 DigiLeft = DigiList[(which * 2) + 1];
\r
705 DigiLastStart = DigiPage;
\r
706 DigiLastEnd = DigiPage + ((DigiLeft + (PMPageSize - 1)) / PMPageSize);
\r
708 len = (DigiLeft >= PMPageSize)? PMPageSize : (DigiLeft % PMPageSize);
\r
709 addr = SDL_LoadDigiSegment(DigiPage++, gvar);
\r
711 DigiPlaying = true;
\r
712 DigiLastSegment = false;
\r
714 SDL_PlayDigiSegment(addr,len);
\r
717 DigiLastSegment = true;
\r
723 SDL_DigitizedDone(void)
\r
727 SDL_PlayDigiSegment(DigiNextAddr,DigiNextLen);
\r
728 DigiNextAddr = nil;
\r
729 DigiMissed = false;
\r
733 if (DigiLastSegment)
\r
735 DigiPlaying = false;
\r
736 DigiLastSegment = false;
\r
737 if ((DigiMode == sds_PC) && (SoundMode == sdm_PC))
\r
739 SDL_SoundFinished();
\r
742 DigiNumber = DigiPriority = 0;
\r
743 SoundPositioned = false;
\r
751 SD_SetDigiDevice(SDSMode mode, global_game_variables_t *gvar)
\r
753 boolean devicenotpresent;
\r
755 if (mode == DigiMode)
\r
758 SD_StopDigitized(gvar);
\r
760 devicenotpresent = false;
\r
763 case sds_SoundBlaster:
\r
764 if (!SoundBlasterPresent)
\r
766 if (SoundSourcePresent)
\r
767 mode = sds_SoundSource;
\r
769 devicenotpresent = true;
\r
772 case sds_SoundSource:
\r
773 if (!SoundSourcePresent)
\r
774 devicenotpresent = true;
\r
778 if (!devicenotpresent)
\r
780 //SS if (DigiMode == sds_SoundSource)
\r
785 //SS if (mode == sds_SoundSource)
\r
786 //SS SDL_StartSS();
\r
788 SDL_SetTimerSpeed();
\r
793 SDL_SetupDigi(global_game_variables_t *gvar)
\r
800 PM_UnlockMainMem(gvar);
\r
801 MM_GetPtr(&list,PMPageSize, gvar);
\r
802 PM_CheckMainMem(gvar);
\r
803 p = (word far *)MK_FP(PM_GetPage(gvar->pm.fi.ChunksInFile - 1, gvar),0);
\r
804 _fmemcpy((void far *)list,(void far *)p,PMPageSize);
\r
805 pg = gvar->pm.fi.PMSoundStart;
\r
806 for (i = 0;i < PMPageSize / (sizeof(word) * 2);i++,p += 2)
\r
808 if (pg >= gvar->pm.fi.ChunksInFile - 1)
\r
810 pg += (p[1] + (PMPageSize - 1)) / PMPageSize;
\r
812 PM_UnlockMainMem(gvar);
\r
813 MM_GetPtr(MEMPTRCONV DigiList,i * sizeof(word) * 2, gvar);
\r
814 _fmemcpy((void far *)DigiList,(void far *)list,i * sizeof(word) * 2);
\r
815 MM_FreePtr(&list, gvar);
\r
818 for (i = 0;i < LASTSOUND;i++)
\r
824 ///////////////////////////////////////////////////////////////////////////
\r
826 // alOut(n,b) - Puts b in AdLib card register n
\r
828 ///////////////////////////////////////////////////////////////////////////
\r
830 alOut(byte n,byte b)
\r
839 SDL_Delay(TimerDelay10);
\r
856 SDL_Delay(TimerDelay25);
\r
901 ///////////////////////////////////////////////////////////////////////////
\r
903 // SDL_SetInstrument() - Puts an instrument into a generator
\r
905 ///////////////////////////////////////////////////////////////////////////
\r
907 SDL_SetInstrument(int track,int which,Instrument far *inst,boolean percussive)
\r
913 c = pcarriers[which];
\r
914 m = pmodifiers[which];
\r
918 c = carriers[which];
\r
919 m = modifiers[which];
\r
922 tracks[track - 1]->inst = *inst;
\r
923 tracks[track - 1]->percussive = percussive;
\r
925 alOut(m + alChar,inst->mChar);
\r
926 alOut(m + alScale,inst->mScale);
\r
927 alOut(m + alAttack,inst->mAttack);
\r
928 alOut(m + alSus,inst->mSus);
\r
929 alOut(m + alWave,inst->mWave);
\r
931 // Most percussive instruments only use one cell
\r
934 alOut(c + alChar,inst->cChar);
\r
935 alOut(c + alScale,inst->cScale);
\r
936 alOut(c + alAttack,inst->cAttack);
\r
937 alOut(c + alSus,inst->cSus);
\r
938 alOut(c + alWave,inst->cWave);
\r
941 alOut(which + alFeedCon,inst->nConn); // DEBUG - I think this is right
\r
945 ///////////////////////////////////////////////////////////////////////////
\r
947 // SDL_ALStopSound() - Turns off any sound effects playing through the
\r
950 ///////////////////////////////////////////////////////////////////////////
\r
956 SDL_ALStopSound(void)
\r
961 /*(long)*/alSound = 0;
\r
962 alOut(alFreqH + 0,0);
\r
968 SDL_AlSetFXInst(Instrument far *inst)
\r
974 alOut(m + alChar,inst->mChar);
\r
975 alOut(m + alScale,inst->mScale);
\r
976 alOut(m + alAttack,inst->mAttack);
\r
977 alOut(m + alSus,inst->mSus);
\r
978 alOut(m + alWave,inst->mWave);
\r
979 alOut(c + alChar,inst->cChar);
\r
980 alOut(c + alScale,inst->cScale);
\r
981 alOut(c + alAttack,inst->cAttack);
\r
982 alOut(c + alSus,inst->cSus);
\r
983 alOut(c + alWave,inst->cWave);
\r
985 // Note: Switch commenting on these lines for old MUSE compatibility
\r
986 // alOut(alFeedCon,inst->nConn);
\r
987 alOut(alFeedCon,0);
\r
990 ///////////////////////////////////////////////////////////////////////////
\r
992 // SDL_ALPlaySound() - Plays the specified sound on the AdLib card
\r
994 ///////////////////////////////////////////////////////////////////////////
\r
1000 SDL_ALPlaySound(AdLibSound far *sound, global_game_variables_t *gvar)
\r
1002 Instrument __far *inst;
\r
1005 SDL_ALStopSound();
\r
1010 alLengthLeft = sound->common.length;
\r
1011 data = sound->data;
\r
1014 alSound = (byte far *)data;
\r
1015 alBlock = ((sound->block & 7) << 2) | 0x20;
\r
1016 inst = &sound->inst;
\r
1018 if (!(inst->mSus | inst->cSus))
\r
1021 Quit(gvar, "SDL_ALPlaySound() - Bad instrument");
\r
1024 SDL_AlSetFXInst(&alZeroInst); // DEBUG
\r
1025 SDL_AlSetFXInst(inst);
\r
1031 ///////////////////////////////////////////////////////////////////////////
\r
1033 // SDL_ALSoundService() - Plays the next sample out through the AdLib card
\r
1035 ///////////////////////////////////////////////////////////////////////////
\r
1038 SDL_ALSoundService(void)
\r
1046 alOut(alFreqH + 0,0);
\r
1049 alOut(alFreqL + 0,s);
\r
1050 alOut(alFreqH + 0,alBlock);
\r
1053 if (!(--alLengthLeft))
\r
1055 /*(long)*/alSound = 0;
\r
1056 alOut(alFreqH + 0,0);
\r
1057 SDL_SoundFinished();
\r
1064 ///////////////////////////////////////////////////////////////////////////
\r
1066 // SDL_SelectMeasure() - sets up sequencing variables for a given track
\r
1068 ///////////////////////////////////////////////////////////////////////////
\r
1070 SDL_SelectMeasure(ActiveTrack *track)
\r
1072 track->seq = track->moods[track->mood];
\r
1073 track->nextevent = 0;
\r
1079 SDL_ALService(void)
\r
1089 while (sqHackLen && (sqHackTime <= alTimeCount))
\r
1092 sqHackTime = alTimeCount + *sqHackPtr++;
\r
1102 sqHackPtr = (word far *)sqHack;
\r
1103 sqHackLen = sqHackSeqLen;
\r
1104 alTimeCount = sqHackTime = 0;
\r
1109 ///////////////////////////////////////////////////////////////////////////
\r
1111 // SDL_ShutAL() - Shuts down the AdLib card for sound effects
\r
1113 ///////////////////////////////////////////////////////////////////////////
\r
1120 alOut(alEffects,0);
\r
1121 alOut(alFreqH + 0,0);
\r
1122 SDL_AlSetFXInst(&alZeroInst);
\r
1128 ///////////////////////////////////////////////////////////////////////////
\r
1130 // SDL_CleanAL() - Totally shuts down the AdLib card
\r
1132 ///////////////////////////////////////////////////////////////////////////
\r
1141 alOut(alEffects,0);
\r
1142 for (i = 1;i < 0xf5;i++)
\r
1148 ///////////////////////////////////////////////////////////////////////////
\r
1150 // SDL_StartAL() - Starts up the AdLib card for sound effects
\r
1152 ///////////////////////////////////////////////////////////////////////////
\r
1157 alOut(alEffects,alFXReg);
\r
1158 SDL_AlSetFXInst(&alZeroInst);
\r
1161 ///////////////////////////////////////////////////////////////////////////
\r
1163 // SDL_DetectAdLib() - Determines if there's an AdLib (or SoundBlaster
\r
1164 // emulating an AdLib) present
\r
1166 ///////////////////////////////////////////////////////////////////////////
\r
1168 SDL_DetectAdLib(void)
\r
1170 byte status1,status2;
\r
1173 alOut(4,0x60); // Reset T1 & T2
\r
1174 alOut(4,0x80); // Reset IRQ
\r
1175 status1 = readstat();
\r
1176 alOut(2,0xff); // Set timer 1
\r
1177 alOut(4,0x21); // Start timer 1
\r
1179 SDL_Delay(TimerDelay100);
\r
1184 #ifdef __BORLANDC__
\r
1188 #ifdef __BORLANDC__
\r
1196 status2 = readstat();
\r
1200 if (((status1 & 0xe0) == 0x00) && ((status2 & 0xe0) == 0xc0))
\r
1202 for (i = 1;i <= 0xf5;i++) // Zero all the registers
\r
1205 alOut(1,0x20); // Set WSE=1
\r
1206 alOut(8,0); // Set CSM=0 & SEL=0
\r
1215 ///////////////////////////////////////////////////////////////////////////
\r
1217 // SDL_t0Service() - My timer 0 ISR which handles the different timings and
\r
1218 // dispatches to whatever other routines are appropriate
\r
1220 ///////////////////////////////////////////////////////////////////////////
\r
1221 static void interrupt
\r
1222 SDL_t0Service(void)
\r
1224 static word count = 1;
\r
1226 #if 1 // for debugging
\r
1227 asm mov dx,STATUS_REGISTER_1
\r
1229 asm mov dx,ATR_INDEX
\r
1230 asm mov al,ATR_OVERSCAN
\r
1232 asm mov al,4 // red
\r
1238 if ((MusicMode == smm_AdLib) || (DigiMode == sds_SoundSource))
\r
1242 // if (!(++count & 7))
\r
1243 if (!(++count % 10))
\r
1247 if (SoundUserHook)
\r
1250 // if (!(count & 3))
\r
1253 switch (SoundMode)
\r
1259 SDL_ALSoundService();
\r
1266 if (!(++count & 1))
\r
1270 if (SoundUserHook)
\r
1273 switch (SoundMode)
\r
1279 SDL_ALSoundService();
\r
1284 asm mov ax,[WORD PTR TimerCount]
\r
1285 asm add ax,[WORD PTR TimerDivisor]
\r
1286 asm mov [WORD PTR TimerCount],ax
\r
1288 t0OldService(); // If we overflow a word, time to call old int handler
\r
1291 outportb(0x20,0x20); // Ack the interrupt
\r
1294 #if 1 // 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
1308 ////////////////////////////////////////////////////////////////////////////
\r
1310 // SDL_ShutDevice() - turns off whatever device was being used for sound fx
\r
1312 ////////////////////////////////////////////////////////////////////////////
\r
1314 SDL_ShutDevice(void)
\r
1316 switch (SoundMode)
\r
1325 SoundMode = sdm_Off;
\r
1328 ///////////////////////////////////////////////////////////////////////////
\r
1330 // SDL_CleanDevice() - totally shuts down all sound devices
\r
1332 ///////////////////////////////////////////////////////////////////////////
\r
1334 SDL_CleanDevice(void)
\r
1336 if ((SoundMode == sdm_AdLib) || (MusicMode == smm_AdLib))
\r
1340 ///////////////////////////////////////////////////////////////////////////
\r
1342 // SDL_StartDevice() - turns on whatever device is to be used for sound fx
\r
1344 ///////////////////////////////////////////////////////////////////////////
\r
1346 SDL_StartDevice(void)
\r
1348 switch (SoundMode)
\r
1354 SoundNumber = SoundPriority = 0;
\r
1357 // Public routines
\r
1359 ///////////////////////////////////////////////////////////////////////////
\r
1361 // SD_SetSoundMode() - Sets which sound hardware to use for sound effects
\r
1363 ///////////////////////////////////////////////////////////////////////////
\r
1365 SD_SetSoundMode(SDMode mode, global_game_variables_t *gvar)
\r
1367 boolean result = false;
\r
1370 SD_StopSound(gvar);
\r
1373 if ((mode == sdm_AdLib) && !AdLibPresent)
\r
1379 NeedsDigitized = false;
\r
1383 tableoffset = STARTPCSOUNDS;
\r
1384 NeedsDigitized = false;
\r
1390 tableoffset = STARTADLIBSOUNDS;
\r
1391 NeedsDigitized = false;
\r
1400 if (result && (mode != SoundMode))
\r
1405 SoundTable = (word *)(&gvar->ca.audiosegs[tableoffset]);
\r
1407 SDL_StartDevice();
\r
1410 SDL_SetTimerSpeed();
\r
1415 ///////////////////////////////////////////////////////////////////////////
\r
1417 // SD_SetMusicMode() - sets the device to use for background music
\r
1419 ///////////////////////////////////////////////////////////////////////////
\r
1421 SD_SetMusicMode(SMMode mode)
\r
1423 boolean result = false;
\r
1425 SD_FadeOutMusic();
\r
1426 while (SD_MusicPlaying())
\r
1432 NeedsMusic = false;
\r
1438 NeedsMusic = true;
\r
1447 SDL_SetTimerSpeed();
\r
1452 ///////////////////////////////////////////////////////////////////////////
\r
1454 // SD_Startup() - starts up the Sound Mgr
\r
1455 // Detects all additional sound hardware and installs my ISR
\r
1457 ///////////////////////////////////////////////////////////////////////////
\r
1459 SD_Startup(global_game_variables_t *gvar)
\r
1468 ssIsTandy = false;
\r
1469 //SS ssNoCheck = false;
\r
1470 alNoCheck = false;
\r
1471 //SB sbNoCheck = false;
\r
1472 //SB sbNoProCheck = false;
\r
1474 for (i = 1;i < _argc;i++)
\r
1476 switch (US_CheckParm(_argv[i],ParmStrings))
\r
1478 case 0: // No AdLib detection
\r
1481 //SB case 1: // No SoundBlaster detection
\r
1482 //SB sbNoCheck = true;
\r
1484 //SB case 2: // No SoundBlaster Pro detection
\r
1485 //SB sbNoProCheck = true;
\r
1488 //SS ssNoCheck = true; // No Sound Source detection
\r
1490 case 4: // Tandy Sound Source handling
\r
1493 //SS case 5: // Sound Source present at LPT1
\r
1495 //SS ssNoCheck = SoundSourcePresent = true;
\r
1497 //SS case 6: // Sound Source present at LPT2
\r
1499 //SS ssNoCheck = SoundSourcePresent = true;
\r
1501 //SS case 7: // Sound Source present at LPT3
\r
1503 //SS ssNoCheck = SoundSourcePresent = true;
\r
1509 SoundUserHook = 0;
\r
1511 t0OldService = getvect(8); // Get old timer 0 ISR
\r
1513 LocalTime = TimeCount = alTimeCount = 0;
\r
1515 SD_SetSoundMode(sdm_Off, gvar);
\r
1516 SD_SetMusicMode(smm_Off);
\r
1518 //SS if (!ssNoCheck)
\r
1519 //SS SoundSourcePresent = SDL_DetectSoundSource();
\r
1523 AdLibPresent = SDL_DetectAdLib();
\r
1524 //SB if (AdLibPresent) && !sbNoCheck)
\r
1526 //SB int port = -1;
\r
1527 //SB char *env = getenv("BLASTER");
\r
1533 //SB while (isspace(*env))
\r
1536 //SB switch (toupper(*env))
\r
1539 //SB temp = strtol(env + 1,&env,16);
\r
1542 //SB (temp >= 0x210)
\r
1543 //SB && (temp <= 0x260)
\r
1544 //SB && (!(temp & 0x00f))
\r
1546 //SB port = (temp - 0x200) >> 4;
\r
1548 //SB Quit(gvar, "SD_Startup: Unsupported address value in BLASTER");
\r
1551 //SB temp = strtol(env + 1,&env,10);
\r
1555 //SB && (temp <= 10)
\r
1556 //SB && (sbIntVectors[temp] != -1)
\r
1559 //SB sbInterrupt = temp;
\r
1560 //SB sbIntVec = sbIntVectors[sbInterrupt];
\r
1563 //SB Quit(gvar, "SD_Startup: Unsupported interrupt value in BLASTER");
\r
1566 //SB temp = strtol(env + 1,&env,10);
\r
1567 //SB if ((temp == 0) || (temp == 1) || (temp == 3))
\r
1568 //SB SDL_SBSetDMA(temp);
\r
1570 //SB Quit(gvar, "SD_Startup: Unsupported DMA value in BLASTER");
\r
1573 //SB while (isspace(*env))
\r
1575 //SB while (*env && !isspace(*env))
\r
1581 //SB SoundBlasterPresent = SDL_DetectSoundBlaster(port);
\r
1585 for (i = 0;i < 255;i++)
\r
1586 pcSoundLookup[i] = i * 60;
\r
1588 //SB if (SoundBlasterPresent)
\r
1589 //SB SDL_StartSB();
\r
1591 SDL_SetupDigi(gvar);
\r
1593 SD_Started = true;
\r
1596 ///////////////////////////////////////////////////////////////////////////
\r
1598 // SD_Default() - Sets up the default behaviour for the Sound Mgr whether
\r
1599 // the config file was present or not.
\r
1601 ///////////////////////////////////////////////////////////////////////////
\r
1603 SD_Default(boolean gotit,SDMode sd,SMMode sm, global_game_variables_t *gvar)
\r
1605 boolean gotsd,gotsm;
\r
1607 gotsd = gotsm = gotit;
\r
1609 if (gotsd) // Make sure requested sound hardware is available
\r
1614 gotsd = AdLibPresent;
\r
1625 if (sd != SoundMode)
\r
1626 SD_SetSoundMode(sd, gvar);
\r
1629 if (gotsm) // Make sure requested music hardware is available
\r
1634 gotsm = AdLibPresent;
\r
1643 if (sm != MusicMode)
\r
1644 SD_SetMusicMode(sm);
\r
1647 ///////////////////////////////////////////////////////////////////////////
\r
1649 // SD_Shutdown() - shuts down the Sound Mgr
\r
1650 // Removes sound ISR and turns off whatever sound hardware was active
\r
1652 ///////////////////////////////////////////////////////////////////////////
\r
1654 SD_Shutdown(global_game_variables_t *gvar)
\r
1660 SD_StopSound(gvar);
\r
1662 SDL_CleanDevice();
\r
1664 //SB if (SoundBlasterPresent)
\r
1665 //SB SDL_ShutSB();
\r
1667 //SS if (SoundSourcePresent)
\r
1668 //SS SDL_ShutSS();
\r
1675 setvect(8,t0OldService);
\r
1679 SD_Started = false;
\r
1682 ///////////////////////////////////////////////////////////////////////////
\r
1684 // SD_SetUserHook() - sets the routine that the Sound Mgr calls every 1/70th
\r
1685 // of a second from its timer 0 ISR
\r
1687 ///////////////////////////////////////////////////////////////////////////
\r
1689 SD_SetUserHook(void (* hook)(void))
\r
1691 SoundUserHook = hook;
\r
1694 ///////////////////////////////////////////////////////////////////////////
\r
1696 // SD_PositionSound() - Sets up a stereo imaging location for the next
\r
1697 // sound to be played. Each channel ranges from 0 to 15.
\r
1699 ///////////////////////////////////////////////////////////////////////////
\r
1701 SD_PositionSound(int leftvol,int rightvol)
\r
1703 LeftPosition = leftvol;
\r
1704 RightPosition = rightvol;
\r
1705 nextsoundpos = true;
\r
1708 ///////////////////////////////////////////////////////////////////////////
\r
1710 // SD_PlaySound() - plays the specified sound on the appropriate hardware
\r
1712 ///////////////////////////////////////////////////////////////////////////
\r
1714 SD_PlaySound(soundnames sound, global_game_variables_t *gvar)
\r
1717 SoundCommon far *s;
\r
1720 lp = LeftPosition;
\r
1721 rp = RightPosition;
\r
1723 RightPosition = 0;
\r
1725 ispos = nextsoundpos;
\r
1726 nextsoundpos = false;
\r
1731 s = MK_FP(SoundTable[sound],0);
\r
1732 if ((SoundMode != sdm_Off) && !s)
\r
1733 Quit(gvar, "SD_PlaySound() - Uncached sound");
\r
1735 if ((DigiMode != sds_Off) && (DigiMap[sound] != -1))
\r
1737 if ((DigiMode == sds_PC) && (SoundMode == sdm_PC))
\r
1739 if (s->priority < SoundPriority)
\r
1742 SDL_PCStopSound();
\r
1744 SD_PlayDigitized(DigiMap[sound],lp,rp, gvar);
\r
1745 SoundPositioned = ispos;
\r
1746 SoundNumber = sound;
\r
1747 SoundPriority = s->priority;
\r
1753 if (DigiPriority && !DigiNumber)
\r
1756 Quit(gvar, "SD_PlaySound: Priority without a sound");
\r
1760 if (s->priority < DigiPriority)
\r
1763 SD_PlayDigitized(DigiMap[sound],lp,rp, gvar);
\r
1764 SoundPositioned = ispos;
\r
1765 DigiNumber = sound;
\r
1766 DigiPriority = s->priority;
\r
1772 if (SoundMode == sdm_Off)
\r
1775 Quit(gvar, "SD_PlaySound() - Zero length sound");
\r
1776 if (s->priority < SoundPriority)
\r
1779 switch (SoundMode)
\r
1782 SDL_PCPlaySound((void far *)s);
\r
1785 SDL_ALPlaySound((void far *)s, gvar);
\r
1789 SoundNumber = sound;
\r
1790 SoundPriority = s->priority;
\r
1795 ///////////////////////////////////////////////////////////////////////////
\r
1797 // SD_SoundPlaying() - returns the sound number that's playing, or 0 if
\r
1798 // no sound is playing
\r
1800 ///////////////////////////////////////////////////////////////////////////
\r
1802 SD_SoundPlaying(void)
\r
1804 boolean result = false;
\r
1806 switch (SoundMode)
\r
1809 result = pcSound? true : false;
\r
1812 result = alSound? true : false;
\r
1817 return(SoundNumber);
\r
1822 ///////////////////////////////////////////////////////////////////////////
\r
1824 // SD_StopSound() - if a sound is playing, stops it
\r
1826 ///////////////////////////////////////////////////////////////////////////
\r
1828 SD_StopSound(global_game_variables_t *gvar)
\r
1831 SD_StopDigitized(gvar);
\r
1833 switch (SoundMode)
\r
1836 SDL_PCStopSound();
\r
1839 SDL_ALStopSound();
\r
1843 SoundPositioned = false;
\r
1845 SDL_SoundFinished();
\r
1848 ///////////////////////////////////////////////////////////////////////////
\r
1850 // SD_WaitSoundDone() - waits until the current sound is done playing
\r
1852 ///////////////////////////////////////////////////////////////////////////
\r
1854 SD_WaitSoundDone(void)
\r
1856 while (SD_SoundPlaying())
\r
1860 ///////////////////////////////////////////////////////////////////////////
\r
1862 // SD_MusicOn() - turns on the sequencer
\r
1864 ///////////////////////////////////////////////////////////////////////////
\r
1871 ///////////////////////////////////////////////////////////////////////////
\r
1873 // SD_MusicOff() - turns off the sequencer and any playing notes
\r
1875 ///////////////////////////////////////////////////////////////////////////
\r
1882 switch (MusicMode)
\r
1886 alOut(alEffects,0);
\r
1887 for (i = 0;i < sqMaxTracks;i++)
\r
1888 alOut(alFreqH + i + 1,0);
\r
1894 ///////////////////////////////////////////////////////////////////////////
\r
1896 // SD_StartMusic() - starts playing the music pointed to
\r
1898 ///////////////////////////////////////////////////////////////////////////
\r
1900 SD_StartMusic(MusicGroup far *music)
\r
1906 if (MusicMode == smm_AdLib)
\r
1908 sqHackPtr = sqHack = music->values;
\r
1909 sqHackSeqLen = sqHackLen = music->length;
\r
1918 ///////////////////////////////////////////////////////////////////////////
\r
1920 // SD_FadeOutMusic() - starts fading out the music. Call SD_MusicPlaying()
\r
1921 // to see if the fadeout is complete
\r
1923 ///////////////////////////////////////////////////////////////////////////
\r
1925 SD_FadeOutMusic(void)
\r
1927 switch (MusicMode)
\r
1930 // DEBUG - quick hack to turn the music off
\r
1936 ///////////////////////////////////////////////////////////////////////////
\r
1938 // SD_MusicPlaying() - returns true if music is currently playing, false if
\r
1941 ///////////////////////////////////////////////////////////////////////////
\r
1943 SD_MusicPlaying(void)
\r
1947 switch (MusicMode)
\r
1951 // DEBUG - not written
\r
1971 void SDL_turnOnPCSpeaker(word timerval)
\r
1987 void SDL_turnOffPCSpeaker()
\r
1996 void SDL_setPCSpeaker(byte val)
\r
2011 if(*pcSound!=pcLastSample)
\r
2013 pcLastSample=*pcSound;
\r
2016 SDL_turnOnPCSpeaker(pcLastSample*60);
\r
2018 SDL_turnOffPCSpeaker();
\r
2025 SoundNumber=(soundnames)0;
\r
2027 SDL_turnOffPCSpeaker();
\r
2031 if(alSound && !alNoIRQ)
\r
2035 alOutInIRQ(alFreqL,*alSound);
\r
2036 alOutInIRQ(alFreqH,alBlock);
\r
2038 else alOutInIRQ(alFreqH,0);
\r
2044 SoundNumber=(soundnames)0;
\r
2046 alOutInIRQ(alFreqH,0);
\r
2069 if(sqActive && !alNoIRQ)
\r
2075 if(sqHackTime>alTimeCount) break;
\r
2076 sqHackTime=alTimeCount+*(sqHackPtr+1);
\r
2077 alOutInIRQ(*(byte *)sqHackPtr,*(((byte *)sqHackPtr)+1));
\r
2087 sqHackLen=sqHackSeqLen;
\r
2095 if(!(inp(ssStatus)&0x40))
\r
2097 outp(ssData,*ssSample++);
\r
2098 outp(ssControl,ssOff);
\r
2101 outp(ssControl,ssOn);
\r
2108 SDL_DigitizedDoneInIRQ();
\r
2113 TimerCount+=TimerDivisor;
\r
2114 if(*((word *)&TimerCount+1))
\r
2116 *((word *)&TimerCount+1)=0;
\r
2125 // Timer 0 ISR for 7000Hz interrupts
\r
2126 void interrupt SDL_t0ExtremeAsmService(void)
\r
2132 SDL_setPCSpeaker(((*pcSound++)&0x80)>>6);
\r
2137 SDL_turnOffPCSpeaker();
\r
2138 SDL_DigitizedDoneInIRQ();
\r
2152 // Timer 0 ISR for 7000Hz interrupts
\r
2153 void interrupt __SDL_t0ExtremeAsmService()
\r
2159 SDL_setPCSpeaker(((*pcSound++)&0x80)>>6);
\r
2164 SDL_turnOffPCSpeaker();
\r
2165 SDL_DigitizedDoneInIRQ();
\r
2179 // Timer 0 ISR for 700Hz interrupts
\r
2180 void interrupt SDL_t0FastAsmService(void)
\r
2185 // Timer 0 ISR for 140Hz interrupts
\r
2186 void interrupt SDL_t0SlowAsmService(void)
\r
2197 TimerCount+=TimerDivisor;
\r
2198 if(*((word *)&TimerCount+1))
\r
2200 *((word *)&TimerCount+1)=0;
\r
2207 void SDL_IndicatePC(boolean ind)
\r
2213 SDL_DigitizedDoneInIRQ(void)
\r
2217 SDL_PlayDigiSegment(DigiNextAddr,DigiNextLen/*,true*/);
\r
2218 DigiNextAddr = nil;
\r
2219 DigiMissed = false;
\r
2223 if (DigiLastSegment)
\r
2225 DigiPlaying = false;
\r
2226 DigiLastSegment = false;
\r
2227 if ((DigiMode == sds_PC) && (SoundMode == sdm_PC))
\r
2229 SDL_SoundFinished();
\r
2233 DigiNumber = (soundnames) 0;
\r
2236 SoundPositioned = false;
\r
2239 DigiMissed = true;
\r
2243 // Inside an interrupt handler interrupts should already be disabled
\r
2244 // so don't disable them again and cause V86 exceptions which cost
\r
2245 // aprox. 300 processor tics!
\r
2248 void alOutInIRQ(byte n,byte b)
\r