1 | #include <stdio.h> |
---|
2 | #include <stdlib.h> |
---|
3 | #include <string.h> |
---|
4 | #include <fcntl.h> |
---|
5 | #include <io.h> |
---|
6 | |
---|
7 | #include "sound.hpp" |
---|
8 | extern "C" { |
---|
9 | #include "dmx.h" |
---|
10 | } ; |
---|
11 | |
---|
12 | #define NO_CARD 0 |
---|
13 | |
---|
14 | int dma=0,irq=0,addr=0; |
---|
15 | unsigned long card=NO_CARD,music_card=NO_CARD; |
---|
16 | |
---|
17 | char *erm[] = |
---|
18 | { "", |
---|
19 | "could not find card address (port)\n", |
---|
20 | "could not allocate DMA buffer\n" |
---|
21 | "interrupt not detected\n" |
---|
22 | "DMA channel could not be identified\n"}; |
---|
23 | |
---|
24 | #define NUMBER_CHANNELS 4 // default number of sound channels |
---|
25 | #define SAMPLE_RATE 22050 // default sampling rate |
---|
26 | |
---|
27 | void sound_uninit() |
---|
28 | { |
---|
29 | DMX_DeInit(); |
---|
30 | } |
---|
31 | |
---|
32 | void print_sound_options() |
---|
33 | { |
---|
34 | printf(" Sound cards supported :\n" |
---|
35 | " -sblaster Sound Blaster\n" |
---|
36 | " -gravis Gravis Ultrasound\n" |
---|
37 | " -proaudio Pro Audio Spectrum\n" |
---|
38 | " -speaker PC speaker\n" |
---|
39 | " Music cards supports :\n" |
---|
40 | " -adlib\n" |
---|
41 | " -mpu401\n" |
---|
42 | " -gravis\n" |
---|
43 | " -speaker PC speaker\n" |
---|
44 | " Sound options :\n" |
---|
45 | " -samplerate 5000..50000 default :%d\n" |
---|
46 | " -channels 1..4\n default : %d\n",SAMPLE_RATE, |
---|
47 | NUMBER_CHANNELS); |
---|
48 | } |
---|
49 | |
---|
50 | |
---|
51 | int sound_init(int argc, char **argv) |
---|
52 | { |
---|
53 | int i,return_code, |
---|
54 | sr=SAMPLE_RATE,nc=NUMBER_CHANNELS; |
---|
55 | unsigned short ver; |
---|
56 | |
---|
57 | for (i=1;i<argc;i++) |
---|
58 | { |
---|
59 | if (!strcmp(argv[i],"-samplerate")) |
---|
60 | { |
---|
61 | i++; |
---|
62 | if (atoi(argv[i])<5000 || atoi(argv[i])>50000) |
---|
63 | printf("Bad sample rate : valid range is 5000..50000, default is %d",SAMPLE_RATE); |
---|
64 | else |
---|
65 | { |
---|
66 | printf("Sample rate set to %d\n",sr); |
---|
67 | sr=SAMPLE_RATE; |
---|
68 | } |
---|
69 | } else if (!strcmp(argv[i],"-channels")) |
---|
70 | { |
---|
71 | i++; |
---|
72 | if (atoi(argv[i])<1 || atoi(argv[i])>4) |
---|
73 | printf("Bad # of channels : valid range is 1..4, default is %d",NUMBER_CHANNELS); |
---|
74 | else |
---|
75 | { |
---|
76 | printf("# of channels set to %d\n",nc); |
---|
77 | nc=NUMBER_CHANNELS; |
---|
78 | } |
---|
79 | } else if (!strcmp(argv[i],"-sblaster")) |
---|
80 | { card=AHW_SOUND_BLASTER; |
---|
81 | if (music_card==NO_CARD) |
---|
82 | music_card=AHW_ADLIB; |
---|
83 | } |
---|
84 | else if (!strcmp(argv[i],"-proaudio")) |
---|
85 | { |
---|
86 | card=AHW_MEDIA_VISION; |
---|
87 | if (music_card==NO_CARD) |
---|
88 | music_card=AHW_ADLIB; |
---|
89 | } |
---|
90 | else if (!strcmp(argv[i],"-gravis") || !strcmp(argv[i],"-gus")) |
---|
91 | music_card=card=AHW_ULTRA_SOUND; |
---|
92 | else if (!strcmp(argv[i],"-speaker")) |
---|
93 | music_card=AHW_PC_SPEAKER; |
---|
94 | else if (!strcmp(argv[i],"-adlib")) |
---|
95 | music_card=AHW_ADLIB; |
---|
96 | else if (!strcmp(argv[i],"mpu401")) |
---|
97 | music_card=AHW_MPU_401; |
---|
98 | else if (!strcmp(argv[i],"-detect")) |
---|
99 | music_card=card=AHW_ANY; |
---|
100 | else if (!strcmp(argv[i],"-irq")) |
---|
101 | { i++; |
---|
102 | irq=atoi(argv[i]); |
---|
103 | } else if (!strcmp(argv[i],"-dma")) |
---|
104 | { i++; |
---|
105 | dma=atoi(argv[i]); |
---|
106 | } else if (!strcmp(argv[i],"-addr")) |
---|
107 | { i++; |
---|
108 | addr=atoi(argv[i]); |
---|
109 | } |
---|
110 | } |
---|
111 | |
---|
112 | if (card==NO_CARD && music_card==NO_CARD) |
---|
113 | return 0; |
---|
114 | |
---|
115 | TSM_Install(140); |
---|
116 | atexit(TSM_Remove); |
---|
117 | |
---|
118 | if (card==AHW_ANY || card==AHW_MEDIA_VISION) |
---|
119 | { |
---|
120 | if ( MV_Detect() == 0 ) |
---|
121 | { |
---|
122 | printf( "Detected MEDIA VISION card. (Pro Audio)\n" ); |
---|
123 | card=AHW_MEDIA_VISION; |
---|
124 | music_card=AHW_ADLIB; |
---|
125 | } |
---|
126 | else if (card==AHW_MEDIA_VISION) |
---|
127 | { |
---|
128 | printf("Warning : Detect failed for MEDIA VISION (Pro Audio) card.\n"); |
---|
129 | card=NO_CARD; |
---|
130 | } |
---|
131 | } |
---|
132 | |
---|
133 | if (card==AHW_ANY || card==AHW_ULTRA_SOUND) |
---|
134 | { |
---|
135 | if ( GF1_Detect() == 0 ) |
---|
136 | { |
---|
137 | printf( "Detected GRAVIS Ultrasound card.\n" ); |
---|
138 | music_card=card=AHW_ULTRA_SOUND; |
---|
139 | } |
---|
140 | else if (card==AHW_ULTRA_SOUND) |
---|
141 | { |
---|
142 | printf("Warning : Detect failed for GRAVIS Ultrasound card.\n"); |
---|
143 | card=NO_CARD; |
---|
144 | } |
---|
145 | } |
---|
146 | |
---|
147 | if (card==AHW_ANY) |
---|
148 | { |
---|
149 | if (!addr || !dma || !irq) |
---|
150 | { |
---|
151 | addr = dma = -1; irq = 0; |
---|
152 | return_code=SB_Detect( &addr, &irq, &dma, &ver); |
---|
153 | if (!return_code) |
---|
154 | printf( "SBlaster defaults are Port:%xh Irq:%d DMA:%d\n", addr, irq, dma ); |
---|
155 | |
---|
156 | printf( "Looking for a SOUND BLASTER card...\n" ); |
---|
157 | addr = irq = dma = -1; |
---|
158 | return_code = SB_Detect( &addr, &irq, &dma, &ver ); |
---|
159 | if (!return_code) |
---|
160 | { |
---|
161 | printf( "Found SoundBlaster card\n" ); |
---|
162 | printf( "ADDR:%03xh IRQ:%d DMA:%d Ver:%2x.%02x\n",addr, irq, dma, ver>>8,ver&0xff); |
---|
163 | SB_SetCard( addr, irq, dma ); |
---|
164 | } |
---|
165 | } else SB_SetCard( addr, irq, dma ); |
---|
166 | } |
---|
167 | |
---|
168 | if (music_card==AHW_ANY || music_card==AHW_ADLIB) |
---|
169 | { |
---|
170 | int waitstates,board_type; |
---|
171 | if (AL_Detect(&waitstates,&board_type)==0) |
---|
172 | { |
---|
173 | printf("Detected Adlib music card, waitstates=%d, board type=%d\n", |
---|
174 | waitstates,board_type); |
---|
175 | music_card=AHW_ADLIB; |
---|
176 | } else if (music_card==AHW_ADLIB) |
---|
177 | { |
---|
178 | printf("Warning : Adlib not detected\n"); |
---|
179 | music_card=NO_CARD; |
---|
180 | } |
---|
181 | } |
---|
182 | |
---|
183 | if (music_card==AHW_MPU_401 || music_card==AHW_MPU_401) |
---|
184 | { |
---|
185 | int type; |
---|
186 | if (MPU_Detect(&addr,&type)==0) |
---|
187 | { |
---|
188 | printf("Detected MPU-401, port address = %x(hex), type=",addr); |
---|
189 | switch (type) |
---|
190 | { |
---|
191 | case 0 : printf("GENERAL MIDI\n"); break; |
---|
192 | case 1 : printf("SCC-1\n"); break; |
---|
193 | default : printf("unknown\n"); |
---|
194 | } |
---|
195 | } else if (music_card==AHW_MPU_401) |
---|
196 | { |
---|
197 | printf("Failed to detect MPU 401\n"); |
---|
198 | music_card=NO_CARD; |
---|
199 | } |
---|
200 | } |
---|
201 | |
---|
202 | if (card==NO_CARD && music_card==NO_CARD) |
---|
203 | return 0; |
---|
204 | |
---|
205 | printf( "Initializing Sound effect subsystem...\n" ); |
---|
206 | return_code = DMX_Init( 140, 20, music_card, card); |
---|
207 | printf("Initialized\n"); |
---|
208 | |
---|
209 | if (!return_code) |
---|
210 | { |
---|
211 | printf("Sound card init failed\n"); |
---|
212 | card=NO_CARD; |
---|
213 | return 0; |
---|
214 | } |
---|
215 | |
---|
216 | WAV_PlayMode( nc, sr); |
---|
217 | return 1; |
---|
218 | } |
---|
219 | |
---|
220 | |
---|
221 | void modify_path(char *filename, char *new_filename) |
---|
222 | { |
---|
223 | char *n=new_filename; |
---|
224 | #ifdef __WATCOMC__ |
---|
225 | while (*filename) |
---|
226 | { |
---|
227 | if ((*filename)=='/') |
---|
228 | *n='\\'; |
---|
229 | else *n=*filename; |
---|
230 | filename++; |
---|
231 | n++; |
---|
232 | } |
---|
233 | *n=0; |
---|
234 | #else |
---|
235 | strcpy(new_filename,filename); |
---|
236 | #endif |
---|
237 | } |
---|
238 | |
---|
239 | |
---|
240 | sound_effect::sound_effect(char *filename) |
---|
241 | { |
---|
242 | char nf[100]; |
---|
243 | modify_path(filename,nf); |
---|
244 | data=WAV_LoadPatch(nf); |
---|
245 | if (!data) |
---|
246 | { |
---|
247 | printf("error loading %s\n",nf); |
---|
248 | exit(1); |
---|
249 | } |
---|
250 | } |
---|
251 | |
---|
252 | void sound_effect::play(int volume, int pitch, int panpot) |
---|
253 | { |
---|
254 | if (card!=NO_CARD && data) |
---|
255 | SFX_PlayPatch(data,pitch,panpot,volume,0,1); |
---|
256 | } |
---|
257 | |
---|
258 | |
---|
259 | |
---|
260 | song::song(char *filename) |
---|
261 | { |
---|
262 | Name=strcpy((char *)malloc(strlen(filename)+1),filename); |
---|
263 | |
---|
264 | int fh = open(filename, O_RDONLY|O_BINARY); |
---|
265 | if ( fh==-1) |
---|
266 | { |
---|
267 | printf("Unable to open song %s\n",filename); |
---|
268 | data=NULL; |
---|
269 | } |
---|
270 | else |
---|
271 | { |
---|
272 | unsigned long fl=filelength(fh); |
---|
273 | if (fl<sizeof(DMX_HEADER) || !memcmp( data, "MUS", 3 )) |
---|
274 | { |
---|
275 | data=NULL; |
---|
276 | printf("Bad music file %s\n",filename); |
---|
277 | } |
---|
278 | else |
---|
279 | { |
---|
280 | data=(unsigned char *)malloc(fl); |
---|
281 | read(fh,data,fl); |
---|
282 | song_id=MUS_RegisterSong(data); |
---|
283 | if (song_id==-1) |
---|
284 | { |
---|
285 | printf("Error registering song %s\n",name()); |
---|
286 | free(data); |
---|
287 | data=NULL; |
---|
288 | } |
---|
289 | |
---|
290 | } |
---|
291 | close(fh); |
---|
292 | } |
---|
293 | } |
---|
294 | |
---|
295 | |
---|
296 | song::~song() |
---|
297 | { |
---|
298 | if (playing()) |
---|
299 | stop(); |
---|
300 | MUS_UnregisterSong(song_id); |
---|
301 | if (data) free(data); |
---|
302 | free(Name); |
---|
303 | } |
---|
304 | |
---|
305 | |
---|
306 | void song::play(unsigned char volume) |
---|
307 | { |
---|
308 | if (song_id!=-1) |
---|
309 | MUS_PlaySong(song_id,volume); |
---|
310 | } |
---|
311 | |
---|
312 | int song::playing() |
---|
313 | { |
---|
314 | if (song_id==-1) return 0; |
---|
315 | else return MUS_QrySongPlaying(song_id); |
---|
316 | } |
---|
317 | |
---|
318 | void song::stop(long fadeout_time) // time in ms |
---|
319 | { |
---|
320 | if (song_id!=-1) |
---|
321 | MUS_FadeOutSong(song_id,fadeout_time); |
---|
322 | } |
---|
323 | |
---|
324 | |
---|
325 | |
---|
326 | void set_music_volume(int volume) // 0...127 |
---|
327 | { |
---|
328 | if (volume!=0) |
---|
329 | MUS_SetMasterVolume(volume); |
---|
330 | } |
---|