1 /** ADAMEm: Coleco ADAM emulator ********************************************/
2 /** **/
3 /** Sound.c **/
4 /** **/
5 /** This file contains the system-independent part of the Coleco sound **/
6 /** hardware emulation routines **/
7 /** **/
8 /** Copyright (C) Marcel de Kogel 1996,1997,1998,1999 **/
9 /** You are not allowed to distribute this software commercially **/
10 /** Please, notify me, if you make any changes to this file **/
11 /****************************************************************************/
12
13 #ifdef SOUND
14
15 #include "Coleco.h"
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19
20 #ifdef ZLIB
21 #include <zlib.h>
22 #define fopen gzopen
23 #define fclose gzclose
24 #define fread(B,N,L,F) gzread(F,B,(L)*(N))
25 #endif
26
27 char szSoundFileName[256];
28
29 struct sample_info sample_params[15];
30 short *sample_ptr[15];
31
32 int soundmode=255;
33 int mastervolume=10;
34 int panning=0;
35 int chorus=0;
36 int reverb=7;
37
38 static struct SoundDriver_struct SoundDriver;
39
40 #ifdef MSDOS
41 struct _SB_Info SB_Info;
GetSBInfo(void)42 static void GetSBInfo (void)
43 {
44 char *blaster=getenv ("BLASTER");
45 memset (&SB_Info,0,sizeof(SB_Info));
46 if (blaster)
47 {
48 strupr (blaster);
49 while (*blaster)
50 {
51 while (*blaster==' ' || *blaster=='\t')
52 ++blaster;
53 switch (*blaster++)
54 {
55 case 'A': SB_Info.baseport=(word)strtol(blaster,NULL,16);
56 break;
57 case 'I': SB_Info.irq=(byte)strtol(blaster,NULL,10);
58 break;
59 case 'D': SB_Info.dma_low=(byte)strtol(blaster,NULL,10);
60 break;
61 case 'H': SB_Info.dma_high=(byte)strtol(blaster,NULL,10);
62 break;
63 case 'T': SB_Info.type=(byte)strtol(blaster,NULL,10);
64 break;
65 case 'E': SB_Info.emu_baseport=(word)strtol(blaster,NULL,16);
66 break;
67 case 'P': SB_Info.mpu_baseport=(word)strtol(blaster,NULL,16);
68 break;
69 }
70 while (*blaster && *blaster!=' ' && *blaster!='\t')
71 ++blaster;
72 }
73 }
74 }
75 #endif
76
InitSound(void)77 int InitSound (void)
78 {
79 int mode;
80 int init_done=0;
81 if (panning<0) panning=0;
82 if (panning>100) panning=100;
83 if (reverb>100) reverb=100;
84 if (reverb<0) reverb=0;
85 if (chorus>100) chorus=100;
86 if (chorus<0) chorus=0;
87 mode=soundmode;
88 soundmode=0;
89 if (!mode)
90 return 1;
91 if (mastervolume<0) mastervolume=0;
92 if (mastervolume>15) mastervolume=15;
93 if (Verbose)
94 {
95 printf (" Detecting sound card...\n");
96 fflush (stdout);
97 }
98 #ifdef MSDOS
99 GetSBInfo ();
100 if (mode>6 || mode<0) mode=255;
101 if (mode==255)
102 {
103 init_done=1;
104 if (AWE_SoundDriver.Init())
105 {
106 SoundDriver=AWE_SoundDriver;
107 mode=6;
108 }
109 else
110 if (GUS_SoundDriver.Init())
111 {
112 SoundDriver=GUS_SoundDriver;
113 mode=5;
114 }
115 else
116 if (SB_SoundDriver.Init())
117 {
118 SoundDriver=SB_SoundDriver;
119 mode=4;
120 }
121 else
122 if (Adlib_SoundDriver.Init())
123 {
124 SoundDriver=Adlib_SoundDriver;
125 mode=2;
126 }
127 else
128 if (Speaker_SoundDriver.Init())
129 {
130 SoundDriver=Speaker_SoundDriver;
131 mode=1;
132 }
133 else
134 {
135 init_done=0;
136 mode=0;
137 }
138 }
139 else
140 switch (mode)
141 {
142 case 6:
143 SoundDriver=AWE_SoundDriver;
144 break;
145 case 5:
146 SoundDriver=GUS_SoundDriver;
147 break;
148 case 4:
149 SoundDriver=SB_SoundDriver;
150 break;
151 case 3:
152 SoundDriver=Combined_SoundDriver;
153 break;
154 case 2:
155 SoundDriver=Adlib_SoundDriver;
156 break;
157 default:
158 SoundDriver=Speaker_SoundDriver;
159 break;
160 }
161 #else
162 SoundDriver=SB_SoundDriver;
163 #endif
164 if (init_done)
165 soundmode=1;
166 else if (mode)
167 soundmode=SoundDriver.Init();
168 if (!soundmode)
169 return 1;
170 SoundDriver.SetMasterVolume (mastervolume);
171 return 2;
172 }
173
TrashSound(void)174 void TrashSound (void)
175 {
176 if (soundmode)
177 SoundDriver.Reset ();
178 }
179
StopSound(void)180 void StopSound (void)
181 {
182 if (soundmode)
183 SoundDriver.Stop ();
184 }
185
ResumeSound(void)186 void ResumeSound (void)
187 {
188 if (soundmode)
189 SoundDriver.Resume ();
190 }
191
Sound(int r,int v)192 void Sound (int r,int v)
193 {
194 if (soundmode)
195 SoundDriver.WriteSoundReg (r,v);
196 }
197
DecreaseSoundVolume(void)198 void DecreaseSoundVolume (void)
199 {
200 if (mastervolume)
201 SoundDriver.SetMasterVolume (--mastervolume);
202 }
203
IncreaseSoundVolume(void)204 void IncreaseSoundVolume (void)
205 {
206 if (mastervolume<15)
207 SoundDriver.SetMasterVolume (++mastervolume);
208 }
209
ToggleSound(void)210 void ToggleSound (void)
211 {
212 if (soundmode)
213 SoundDriver.Toggle ();
214 }
215
ToggleSoundChannel(int channel)216 void ToggleSoundChannel (int channel)
217 {
218 if (soundmode)
219 SoundDriver.ToggleChannel (channel);
220 }
221
LoadSamples(void)222 int LoadSamples (void)
223 {
224 FILE *f;
225 int i;
226 if (Verbose) printf (" Opening %s... ",szSoundFileName);
227 f=fopen (szSoundFileName,"rb");
228 if (!f) { if (Verbose) puts("NOT FOUND");return 0; }
229 if (Verbose) printf ("OK\n Loading samples... ");
230 if (fread (sample_params,1,sizeof(sample_params),f)!=sizeof(sample_params))
231 {
232 if (Verbose) puts ("READ ERROR");
233 return 0;
234 }
235 for (i=0;i<15;++i)
236 {
237 sample_ptr[i]=malloc (sample_params[i].len*2);
238 if (!sample_ptr[i])
239 {
240 if (Verbose) puts ("OUT OF MEMORY");
241 FreeSamples ();
242 return 0;
243 }
244 if (fread (sample_ptr[i],1,sample_params[i].len*2,f)!=sample_params[i].len*2)
245 {
246 if (Verbose) puts ("READ ERROR");
247 FreeSamples ();
248 return 0;
249 }
250 }
251 fclose (f);
252 if (Verbose) puts ("OK");
253 return 1;
254 }
255
FreeSamples(void)256 void FreeSamples (void)
257 {
258 int i;
259 for (i=0;i<15;++i)
260 if (sample_ptr[i])
261 {
262 free (sample_ptr[i]);
263 sample_ptr[i]=0;
264 }
265 }
266
267 #else /* SOUND */
268
InitSound(void)269 int InitSound (void) { return 0; }
TrashSound(void)270 void TrashSound (void) { };
StopSound(void)271 void StopSound (void) { };
ResumeSound(void)272 void ResumeSound (void) { };
Sound(int r,int v)273 void Sound (int r,int v) { };
DecreaseSoundVolume(void)274 void DecreaseSoundVolume (void) { };
IncreaseSoundVolume(void)275 void IncreaseSoundVolume (void) { };
ToggleSound(void)276 void ToggleSound (void) { };
ToggleSoundChannel(int channel)277 void ToggleSoundChannel (int channel) { };
278
279 #endif
280