-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPCFX.C
548 lines (406 loc) · 12.3 KB
/
PCFX.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
/*
Copyright (C) 1994-1995 Apogee Software, Ltd.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/**********************************************************************
module: PCFX.C
author: James R. Dose
date: April 1, 1994
Low level routines to support PC sound effects created by Muse.
(c) Copyright 1994 James R. Dose. All Rights Reserved.
**********************************************************************/
#include <dos.h>
#include <stdlib.h>
#include <conio.h>
//#include "dpmi.h"
#include "dpmiapi.h"
#include "task_man.h"
#include "interrup.h"
#include "pcfx.h"
#define TRUE ( 1 == 1 )
#define FALSE ( !TRUE )
static void PCFX_Service( task *Task );
static long PCFX_LengthLeft;
static char *PCFX_Sound = NULL;
static int PCFX_LastSample;
static short PCFX_Lookup[ 256 ];
static int PCFX_UseLookupFlag = FALSE;
static int PCFX_Priority;
static unsigned long PCFX_CallBackVal;
static void ( *PCFX_CallBackFunc )( unsigned long ) = NULL;
static int PCFX_TotalVolume = PCFX_MaxVolume;
static task *PCFX_ServiceTask = NULL;
static int PCFX_VoiceHandle = PCFX_MinVoiceHandle;
int PCFX_Installed = FALSE;
int PCFX_ErrorCode = PCFX_Ok;
#define PCFX_SetErrorCode( status ) \
PCFX_ErrorCode = ( status );
/*---------------------------------------------------------------------
Function: PCFX_ErrorString
Returns a pointer to the error message associated with an error
number. A -1 returns a pointer the current error.
---------------------------------------------------------------------*/
char *PCFX_ErrorString
(
int ErrorNumber
)
{
char *ErrorString;
switch( ErrorNumber )
{
case PCFX_Warning :
case PCFX_Error :
ErrorString = PCFX_ErrorString( PCFX_ErrorCode );
break;
case PCFX_Ok :
ErrorString = "PC FX ok.";
break;
case PCFX_NoVoices :
ErrorString = "No free voices available to PC FX.";
break;
case PCFX_VoiceNotFound :
ErrorString = "No voice with matching handle found.";
break;
case PCFX_DPMI_Error :
ErrorString = "DPMI Error in PCFX.";
break;
default :
ErrorString = "Unknown PC FX error code.";
break;
}
return( ErrorString );
}
/**********************************************************************
Memory locked functions:
**********************************************************************/
#define PCFX_LockStart PCFX_Stop
/*---------------------------------------------------------------------
Function: PCFX_Stop
Halts playback of the currently playing sound effect.
---------------------------------------------------------------------*/
int PCFX_Stop
(
int handle
)
{
unsigned flags;
if ( ( handle != PCFX_VoiceHandle ) || ( PCFX_Sound == NULL ) )
{
PCFX_SetErrorCode( PCFX_VoiceNotFound );
return( PCFX_Warning );
}
flags = DisableInterrupts();
// Turn off speaker
outp( 0x61, inp( 0x61 ) & 0xfc );
PCFX_Sound = NULL;
PCFX_LengthLeft = 0;
PCFX_Priority = 0;
PCFX_LastSample = 0;
RestoreInterrupts( flags );
if ( PCFX_CallBackFunc )
{
PCFX_CallBackFunc( PCFX_CallBackVal );
}
return( PCFX_Ok );
}
/*---------------------------------------------------------------------
Function: PCFX_Service
Task Manager routine to perform the playback of a sound effect.
---------------------------------------------------------------------*/
static void PCFX_Service
(
task *Task
)
{
unsigned value;
if ( PCFX_Sound )
{
if ( PCFX_UseLookupFlag )
{
value = PCFX_Lookup[ *PCFX_Sound ];
PCFX_Sound++;
}
else
{
value = *( short int * )PCFX_Sound;
PCFX_Sound += sizeof( short int );
}
if ( ( PCFX_TotalVolume > 0 ) && ( value != PCFX_LastSample ) )
{
PCFX_LastSample = value;
if ( value )
{
outp( 0x43, 0xb6 );
outp( 0x42, value );
outp( 0x42, value >> 8 );
outp( 0x61, inp( 0x61 ) | 0x3 );
}
else
{
outp( 0x61, inp( 0x61 ) & 0xfc );
}
}
if ( --PCFX_LengthLeft == 0 )
{
PCFX_Stop( PCFX_VoiceHandle );
}
}
}
/*---------------------------------------------------------------------
Function: PCFX_VoiceAvailable
Checks if a voice can be play at the specified priority.
---------------------------------------------------------------------*/
int PCFX_VoiceAvailable
(
int priority
)
{
if ( priority < PCFX_Priority )
{
return( FALSE );
}
return( TRUE );
}
/*---------------------------------------------------------------------
Function: PCFX_Play
Starts playback of a Muse sound effect.
---------------------------------------------------------------------*/
int PCFX_Play
(
PCSound *sound,
int priority,
unsigned long callbackval
)
{
unsigned flags;
if ( priority < PCFX_Priority )
{
PCFX_SetErrorCode( PCFX_NoVoices );
return( PCFX_Warning );
}
PCFX_Stop( PCFX_VoiceHandle );
PCFX_VoiceHandle++;
if ( PCFX_VoiceHandle < PCFX_MinVoiceHandle )
{
PCFX_VoiceHandle = PCFX_MinVoiceHandle;
}
flags = DisableInterrupts();
PCFX_LengthLeft = sound->length;
if ( !PCFX_UseLookupFlag )
{
PCFX_LengthLeft >>= 1;
}
PCFX_Priority = priority;
PCFX_Sound = &sound->data;
PCFX_CallBackVal = callbackval;
RestoreInterrupts( flags );
return( PCFX_VoiceHandle );
}
/*---------------------------------------------------------------------
Function: PCFX_SoundPlaying
Checks if a sound effect is currently playing.
---------------------------------------------------------------------*/
int PCFX_SoundPlaying
(
int handle
)
{
int status;
status = FALSE;
if ( ( handle == PCFX_VoiceHandle ) && ( PCFX_LengthLeft > 0 ) )
{
status = TRUE;
}
return( status );
}
/*---------------------------------------------------------------------
Function: PCFX_SetTotalVolume
Sets the total volume of the sound effects.
---------------------------------------------------------------------*/
int PCFX_SetTotalVolume
(
int volume
)
{
unsigned flags;
flags = DisableInterrupts();
volume = max( volume, 0 );
volume = min( volume, PCFX_MaxVolume );
PCFX_TotalVolume = volume;
if ( volume == 0 )
{
outp( 0x61, inp( 0x61 ) & 0xfc );
}
RestoreInterrupts( flags );
return( PCFX_Ok );
}
/*---------------------------------------------------------------------
Function: PCFX_GetTotalVolume
Returns the total volume of the sound effects.
---------------------------------------------------------------------*/
int PCFX_GetTotalVolume
(
void
)
{
return( PCFX_TotalVolume );
}
/*---------------------------------------------------------------------
Function: PCFX_LockEnd
Used for determining the length of the functions to lock in memory.
---------------------------------------------------------------------*/
static void PCFX_LockEnd
(
void
)
{
}
/*---------------------------------------------------------------------
Function: PCFX_UseLookup
Sets up a pitch lookup table for PC sound effects.
---------------------------------------------------------------------*/
void PCFX_UseLookup
(
int use,
unsigned value
)
{
int pitch;
int index;
PCFX_Stop( PCFX_VoiceHandle );
PCFX_UseLookupFlag = use;
if ( use )
{
pitch = 0;
for( index = 0; index < 256; index++ )
{
PCFX_Lookup[ index ] = pitch;
pitch += value;
}
}
}
/*---------------------------------------------------------------------
Function: PCFX_SetCallBack
Set the function to call when a voice stops.
---------------------------------------------------------------------*/
void PCFX_SetCallBack
(
void ( *function )( unsigned long )
)
{
PCFX_CallBackFunc = function;
}
/*---------------------------------------------------------------------
Function: PCFX_Init
Initializes the sound effect engine.
---------------------------------------------------------------------*/
int PCFX_Init
(
void
)
{
int status;
if ( PCFX_Installed )
{
PCFX_Shutdown();
}
status = PCFX_LockMemory();
if ( status != PCFX_Ok )
{
PCFX_UnlockMemory();
return( status );
}
PCFX_UseLookup( TRUE, 60 );
PCFX_Stop( PCFX_VoiceHandle );
PCFX_ServiceTask = TS_ScheduleTask( &PCFX_Service, 140, 2, NULL );
TS_Dispatch();
PCFX_CallBackFunc = NULL;
PCFX_Installed = TRUE;
PCFX_SetErrorCode( PCFX_Ok );
return( PCFX_Ok );
}
/*---------------------------------------------------------------------
Function: PCFX_Shutdown
Ends the use of the sound effect engine.
---------------------------------------------------------------------*/
int PCFX_Shutdown
(
void
)
{
if ( PCFX_Installed )
{
PCFX_Stop( PCFX_VoiceHandle );
TS_Terminate( PCFX_ServiceTask );
PCFX_UnlockMemory();
PCFX_Installed = FALSE;
}
PCFX_SetErrorCode( PCFX_Ok );
return( PCFX_Ok );
}
/*---------------------------------------------------------------------
Function: PCFX_UnlockMemory
Unlocks all neccessary data.
---------------------------------------------------------------------*/
void PCFX_UnlockMemory
(
void
)
{
DPMI_UnlockMemoryRegion( PCFX_LockStart, PCFX_LockEnd );
DPMI_Unlock( PCFX_LengthLeft );
DPMI_Unlock( PCFX_Sound );
DPMI_Unlock( PCFX_LastSample );
DPMI_Unlock( PCFX_Lookup );
DPMI_Unlock( PCFX_UseLookupFlag );
DPMI_Unlock( PCFX_Priority );
DPMI_Unlock( PCFX_CallBackVal );
DPMI_Unlock( PCFX_CallBackFunc );
DPMI_Unlock( PCFX_TotalVolume );
DPMI_Unlock( PCFX_ServiceTask );
DPMI_Unlock( PCFX_VoiceHandle );
DPMI_Unlock( PCFX_Installed );
DPMI_Unlock( PCFX_ErrorCode );
}
/*---------------------------------------------------------------------
Function: PCFX_LockMemory
Locks all neccessary data.
---------------------------------------------------------------------*/
int PCFX_LockMemory
(
void
)
{
int status;
status = DPMI_LockMemoryRegion( PCFX_LockStart, PCFX_LockEnd );
status |= DPMI_Lock( PCFX_LengthLeft );
status |= DPMI_Lock( PCFX_Sound );
status |= DPMI_Lock( PCFX_LastSample );
status |= DPMI_Lock( PCFX_Lookup );
status |= DPMI_Lock( PCFX_UseLookupFlag );
status |= DPMI_Lock( PCFX_Priority );
status |= DPMI_Lock( PCFX_CallBackVal );
status |= DPMI_Lock( PCFX_CallBackFunc );
status |= DPMI_Lock( PCFX_TotalVolume );
status |= DPMI_Lock( PCFX_ServiceTask );
status |= DPMI_Lock( PCFX_VoiceHandle );
status |= DPMI_Lock( PCFX_Installed );
status |= DPMI_Lock( PCFX_ErrorCode );
if ( status != DPMI_Ok )
{
PCFX_UnlockMemory();
PCFX_SetErrorCode( PCFX_DPMI_Error );
return( PCFX_Error );
}
return( PCFX_Ok );
}