]> 4ch.mooo.com Git - 16.git/blob - src/v2/source/ENGINE/SOUND.C
6c100175d9b9045ed3cad436309d90314058e4c4
[16.git] / src / v2 / source / ENGINE / SOUND.C
1 /*\r
2 Copyright (C) 1998 BJ Eirich (aka vecna)\r
3 This program is free software; you can redistribute it and/or\r
4 modify it under the terms of the GNU General Public License\r
5 as published by the Free Software Foundation; either version 2\r
6 of the License, or (at your option) any later version.\r
7 This program is distributed in the hope that it will be useful,\r
8 but WITHOUT ANY WARRANTY; without even the implied warranty of\r
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\r
10 See the GNU General Public Lic\r
11 See the GNU General Public License for more details.\r
12 You should have received a copy of the GNU General Public License\r
13 along with this program; if not, write to the Free Software\r
14 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.\r
15 */\r
16 \r
17 #define SOUND_H\r
18 #include "verge.h"\r
19 \r
20 // ================================= Data ====================================\r
21 \r
22 UNIMOD *mf;\r
23 SAMPLE *sfx[100];\r
24 int nsfx=0;\r
25 char playing[60];\r
26 \r
27 // ================================= Code ====================================\r
28 \r
29 void PlaySound(int, int, int);\r
30 \r
31 void tickhandler()\r
32 {\r
33   MP_HandleTick();\r
34   MD_SetBPM(mp_bpm);\r
35 }\r
36 \r
37 void InitMusicSystem()\r
38 {\r
39   memset(playing, 0, 60);\r
40   ML_RegisterLoader(&load_mod);\r
41   ML_RegisterLoader(&load_s3m);\r
42   ML_RegisterLoader(&load_uni);\r
43   ML_RegisterLoader(&load_xm);\r
44 \r
45   MD_RegisterDriver(&drv_nos);\r
46 //  MD_RegisterDriver(&drv_ss);\r
47   MD_RegisterDriver(&drv_sb);\r
48   MD_RegisterDriver(&drv_gus);\r
49 \r
50   MD_RegisterPlayer(tickhandler);\r
51   if (!MD_Init())\r
52   {\r
53     printf("Driver error: %s.\n",myerr);\r
54     exit(-1);\r
55   }\r
56 }\r
57 \r
58 void PlayMusic(char *fname)\r
59 {\r
60   strlwr(fname);\r
61   if (!strcmp(fname,playing)) return;\r
62   if (!MP_Ready())\r
63   {\r
64     MD_PlayStop();\r
65     ML_Free(mf);\r
66   }\r
67   memcpy(playing, fname, strlen(fname));\r
68   if (!(mf=ML_LoadFN(fname))) err(myerr);\r
69   MP_Init(mf);\r
70   md_numchn=mf->numchn+2;\r
71   mp_loop=1; mp_volume=100;\r
72   MD_PlayStart();\r
73   PlaySound(0, 0, 0);\r
74   PlaySound(0, 0, 0);\r
75 }\r
76 \r
77 void StopMusic()\r
78 {\r
79   if (MP_Ready()) return;\r
80   MD_PlayStop();\r
81   ML_Free(mf);\r
82 }\r
83 \r
84 int CacheSound(char *fname)\r
85 {\r
86   if (!(sfx[nsfx]=MW_LoadWavFN(fname)))\r
87     err("WAV [%s] load error: %s", fname, myerr);\r
88   return nsfx++;\r
89 }\r
90 \r
91 void FreeAllSounds()\r
92 {\r
93   int i;\r
94 \r
95   for (i=0; i<nsfx; i++)\r
96       MW_FreeWav(sfx[i]);\r
97   nsfx=0;\r
98 }\r
99 \r
100 void PlaySound(int index, int vol, int pan)\r
101 {\r
102   static int switcher=1;\r
103   int chanl;\r
104 \r
105   if (index >= nsfx) return;\r
106   switcher ^= 1;\r
107   chanl=md_numchn-1-switcher;\r
108   MD_VoiceSetVolume(chanl, vol);\r
109   MD_VoiceSetPanning(chanl, pan);\r
110   MD_VoiceSetFrequency(chanl, sfx[index]->c2spd);\r
111   MD_VoicePlay(chanl, sfx[index]->handle, 0, sfx[index]->length,\r
112                0, 0, sfx[index]->flags);\r
113 }\r