I_SOUND.C 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. // I_SOUND.C
  2. #include <stdio.h>
  3. #include "h2def.h"
  4. #include "dmx.h"
  5. #include "sounds.h"
  6. #include "i_sound.h"
  7. /*
  8. ===============
  9. =
  10. = I_StartupTimer
  11. =
  12. ===============
  13. */
  14. int tsm_ID = -1;
  15. void I_StartupTimer (void)
  16. {
  17. #ifndef NOTIMER
  18. extern int I_TimerISR(void);
  19. ST_Message(" I_StartupTimer()\n");
  20. // installs master timer. Must be done before StartupTimer()!
  21. TSM_Install(SND_TICRATE);
  22. tsm_ID = TSM_NewService (I_TimerISR, 35, 255, 0); // max priority
  23. if (tsm_ID == -1)
  24. {
  25. I_Error("Can't register 35 Hz timer w/ DMX library");
  26. }
  27. #endif
  28. }
  29. void I_ShutdownTimer (void)
  30. {
  31. TSM_DelService(tsm_ID);
  32. TSM_Remove();
  33. }
  34. /*
  35. *
  36. * SOUND HEADER & DATA
  37. *
  38. *
  39. */
  40. // sound information
  41. #if 0
  42. const char *dnames[] = {"None",
  43. "PC_Speaker",
  44. "Adlib",
  45. "Sound_Blaster",
  46. "ProAudio_Spectrum16",
  47. "Gravis_Ultrasound",
  48. "MPU",
  49. "AWE32"
  50. };
  51. #endif
  52. const char snd_prefixen[] = { 'P', 'P', 'A', 'S', 'S', 'S', 'M',
  53. 'M', 'M', 'S' };
  54. int snd_Channels;
  55. int snd_DesiredMusicDevice, snd_DesiredSfxDevice;
  56. int snd_MusicDevice, // current music card # (index to dmxCodes)
  57. snd_SfxDevice, // current sfx card # (index to dmxCodes)
  58. snd_MaxVolume, // maximum volume for sound
  59. snd_MusicVolume; // maximum volume for music
  60. int dmxCodes[NUM_SCARDS]; // the dmx code for a given card
  61. int snd_SBport, snd_SBirq, snd_SBdma; // sound blaster variables
  62. int snd_Mport; // midi variables
  63. extern boolean snd_MusicAvail, // whether music is available
  64. snd_SfxAvail; // whether sfx are available
  65. void I_PauseSong(int handle)
  66. {
  67. MUS_PauseSong(handle);
  68. }
  69. void I_ResumeSong(int handle)
  70. {
  71. MUS_ResumeSong(handle);
  72. }
  73. void I_SetMusicVolume(int volume)
  74. {
  75. MUS_SetMasterVolume(volume*8);
  76. // snd_MusicVolume = volume;
  77. }
  78. void I_SetSfxVolume(int volume)
  79. {
  80. snd_MaxVolume = volume; // THROW AWAY?
  81. }
  82. /*
  83. *
  84. * SONG API
  85. *
  86. */
  87. int I_RegisterSong(void *data)
  88. {
  89. int rc = MUS_RegisterSong(data);
  90. #ifdef SNDDEBUG
  91. if (rc<0) ST_Message(" MUS_Reg() returned %d\n", rc);
  92. #endif
  93. return rc;
  94. }
  95. void I_UnRegisterSong(int handle)
  96. {
  97. int rc = MUS_UnregisterSong(handle);
  98. #ifdef SNDDEBUG
  99. if (rc < 0) ST_Message(" MUS_Unreg() returned %d\n", rc);
  100. #endif
  101. }
  102. int I_QrySongPlaying(int handle)
  103. {
  104. int rc = MUS_QrySongPlaying(handle);
  105. #ifdef SNDDEBUG
  106. if (rc < 0) ST_Message(" MUS_QrySP() returned %d\n", rc);
  107. #endif
  108. return rc;
  109. }
  110. // Stops a song. MUST be called before I_UnregisterSong().
  111. void I_StopSong(int handle)
  112. {
  113. int rc;
  114. rc = MUS_StopSong(handle);
  115. #ifdef SNDDEBUG
  116. if (rc < 0) ST_Message(" MUS_StopSong() returned %d\n", rc);
  117. #endif
  118. /*
  119. // Fucking kluge pause
  120. {
  121. int s;
  122. extern volatile int ticcount;
  123. for (s=ticcount ; ticcount - s < 10 ; );
  124. }
  125. */
  126. }
  127. void I_PlaySong(int handle, boolean looping)
  128. {
  129. int rc;
  130. rc = MUS_ChainSong(handle, looping ? handle : -1);
  131. #ifdef SNDDEBUG
  132. if (rc < 0) ST_Message(" MUS_ChainSong() returned %d\n", rc);
  133. #endif
  134. rc = MUS_PlaySong(handle, snd_MusicVolume);
  135. #ifdef SNDDEBUG
  136. if (rc < 0) ST_Message(" MUS_PlaySong() returned %d\n", rc);
  137. #endif
  138. }
  139. /*
  140. *
  141. * SOUND FX API
  142. *
  143. */
  144. // Gets lump nums of the named sound. Returns pointer which will be
  145. // passed to I_StartSound() when you want to start an SFX. Must be
  146. // sure to pass this to UngetSoundEffect() so that they can be
  147. // freed!
  148. int I_GetSfxLumpNum(sfxinfo_t *sound)
  149. {
  150. return W_GetNumForName(sound->lumpname);
  151. }
  152. int I_StartSound (int id, void *data, int vol, int sep, int pitch, int priority)
  153. {
  154. return SFX_PlayPatch(data, pitch, sep, vol, 0, 0);
  155. }
  156. void I_StopSound(int handle)
  157. {
  158. // extern volatile long gDmaCount;
  159. // long waittocount;
  160. SFX_StopPatch(handle);
  161. // waittocount = gDmaCount + 2;
  162. // while (gDmaCount < waittocount) ;
  163. }
  164. int I_SoundIsPlaying(int handle)
  165. {
  166. return SFX_Playing(handle);
  167. }
  168. void I_UpdateSoundParams(int handle, int vol, int sep, int pitch)
  169. {
  170. SFX_SetOrigin(handle, pitch, sep, vol);
  171. }
  172. /*
  173. *
  174. * SOUND STARTUP STUFF
  175. *
  176. *
  177. */
  178. //
  179. // Why PC's Suck, Reason #8712
  180. //
  181. void I_sndArbitrateCards(void)
  182. {
  183. char tmp[160];
  184. boolean gus, adlib, pc, sb, midi;
  185. int i, rc, mputype, p, opltype, wait, dmxlump;
  186. snd_MusicDevice = snd_DesiredMusicDevice;
  187. snd_SfxDevice = snd_DesiredSfxDevice;
  188. // check command-line parameters- overrides config file
  189. //
  190. if (M_CheckParm("-nosound")) snd_MusicDevice = snd_SfxDevice = snd_none;
  191. if (M_CheckParm("-nosfx")) snd_SfxDevice = snd_none;
  192. if (M_CheckParm("-nomusic")) snd_MusicDevice = snd_none;
  193. if (snd_MusicDevice > snd_MPU && snd_MusicDevice <= snd_MPU3)
  194. snd_MusicDevice = snd_MPU;
  195. if (snd_MusicDevice == snd_SB)
  196. snd_MusicDevice = snd_Adlib;
  197. if (snd_MusicDevice == snd_PAS)
  198. snd_MusicDevice = snd_Adlib;
  199. // figure out what i've got to initialize
  200. //
  201. gus = snd_MusicDevice == snd_GUS || snd_SfxDevice == snd_GUS;
  202. sb = snd_SfxDevice == snd_SB || snd_MusicDevice == snd_SB;
  203. adlib = snd_MusicDevice == snd_Adlib ;
  204. pc = snd_SfxDevice == snd_PC;
  205. midi = snd_MusicDevice == snd_MPU;
  206. // initialize whatever i've got
  207. //
  208. if (gus)
  209. {
  210. if (GF1_Detect()) ST_Message(" Dude. The GUS ain't responding.\n");
  211. else
  212. {
  213. dmxlump = W_GetNumForName("dmxgus");
  214. GF1_SetMap(W_CacheLumpNum(dmxlump, PU_CACHE), lumpinfo[dmxlump].size);
  215. }
  216. }
  217. if (sb)
  218. {
  219. if(debugmode)
  220. {
  221. ST_Message(" Sound cfg p=0x%x, i=%d, d=%d\n",
  222. snd_SBport, snd_SBirq, snd_SBdma);
  223. }
  224. if (SB_Detect(&snd_SBport, &snd_SBirq, &snd_SBdma, 0))
  225. {
  226. ST_Message(" SB isn't responding at p=0x%x, i=%d, d=%d\n",
  227. snd_SBport, snd_SBirq, snd_SBdma);
  228. }
  229. else SB_SetCard(snd_SBport, snd_SBirq, snd_SBdma);
  230. if(debugmode)
  231. {
  232. ST_Message(" SB_Detect returned p=0x%x, i=%d, d=%d\n",
  233. snd_SBport, snd_SBirq, snd_SBdma);
  234. }
  235. }
  236. if (adlib)
  237. {
  238. if (AL_Detect(&wait,0))
  239. {
  240. ST_Message(" Dude. The Adlib isn't responding.\n");
  241. }
  242. else
  243. {
  244. AL_SetCard(wait, W_CacheLumpName("genmidi", PU_STATIC));
  245. }
  246. }
  247. if (midi)
  248. {
  249. if (debugmode)
  250. {
  251. ST_Message(" cfg p=0x%x\n", snd_Mport);
  252. }
  253. if (MPU_Detect(&snd_Mport, &i))
  254. {
  255. ST_Message(" The MPU-401 isn't reponding @ p=0x%x.\n", snd_Mport);
  256. }
  257. else MPU_SetCard(snd_Mport);
  258. }
  259. }
  260. // inits all sound stuff
  261. void I_StartupSound (void)
  262. {
  263. int rc, i;
  264. if (debugmode)
  265. ST_Message("I_StartupSound: Hope you hear a pop.\n");
  266. // initialize dmxCodes[]
  267. dmxCodes[0] = 0;
  268. dmxCodes[snd_PC] = AHW_PC_SPEAKER;
  269. dmxCodes[snd_Adlib] = AHW_ADLIB;
  270. dmxCodes[snd_SB] = AHW_SOUND_BLASTER;
  271. dmxCodes[snd_PAS] = AHW_MEDIA_VISION;
  272. dmxCodes[snd_GUS] = AHW_ULTRA_SOUND;
  273. dmxCodes[snd_MPU] = AHW_MPU_401;
  274. dmxCodes[snd_MPU2] = AHW_MPU_401;
  275. dmxCodes[snd_MPU3] = AHW_MPU_401;
  276. dmxCodes[snd_AWE] = AHW_AWE32;
  277. dmxCodes[snd_CDMUSIC] = 0;
  278. // inits sound library timer stuff
  279. I_StartupTimer();
  280. // pick the sound cards i'm going to use
  281. //
  282. I_sndArbitrateCards();
  283. if (debugmode)
  284. {
  285. ST_Message(" Music device #%d & dmxCode=%d,", snd_MusicDevice,
  286. dmxCodes[snd_MusicDevice]);
  287. ST_Message(" Sfx device #%d & dmxCode=%d\n", snd_SfxDevice,
  288. dmxCodes[snd_SfxDevice]);
  289. }
  290. // inits DMX sound library
  291. ST_Message(" Calling DMX_Init...");
  292. rc = DMX_Init(SND_TICRATE, SND_MAXSONGS, dmxCodes[snd_MusicDevice],
  293. dmxCodes[snd_SfxDevice]);
  294. if (debugmode)
  295. {
  296. ST_Message(" DMX_Init() returned %d\n", rc);
  297. }
  298. }
  299. // shuts down all sound stuff
  300. void I_ShutdownSound (void)
  301. {
  302. DMX_DeInit();
  303. I_ShutdownTimer();
  304. }
  305. void I_SetChannels(int channels)
  306. {
  307. WAV_PlayMode(channels, SND_SAMPLERATE);
  308. }