-
Notifications
You must be signed in to change notification settings - Fork 1
/
a_pcfx.c
236 lines (175 loc) · 5.7 KB
/
a_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
/*
Copyright (C) 1994-1995 Apogee Software, Ltd.
Copyright (C) 2023 Frenkel Smeijers
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 <conio.h>
#include <dos.h>
#include "id_heads.h"
#include "a_inter.h"
#include "a_pcfx.h"
#include "a_taskmn.h"
#define PCFX_MinVoiceHandle 1
static void PCFX_Service(task *Task);
static int32_t PCFX_LengthLeft;
static uint8_t *PCFX_Sound = NULL;
static int32_t PCFX_LastSample;
static task *PCFX_ServiceTask = NULL;
static int32_t PCFX_VoiceHandle = PCFX_MinVoiceHandle;
static boolean PCFX_Installed = false;
/*---------------------------------------------------------------------
Function: PCFX_Stop
Halts playback of the currently playing sound effect.
---------------------------------------------------------------------*/
void PCFX_Stop(int32_t handle)
{
uint32_t flags;
if ((handle != PCFX_VoiceHandle) || (PCFX_Sound == NULL))
return;
flags = DisableInterrupts();
// Turn off speaker
outp(0x61, inp(0x61) & 0xfc);
PCFX_Sound = NULL;
PCFX_LengthLeft = 0;
PCFX_LastSample = 0;
RestoreInterrupts(flags);
}
/*---------------------------------------------------------------------
Function: PCFX_Service
Task Manager routine to perform the playback of a sound effect.
---------------------------------------------------------------------*/
static void PCFX_Service(task *Task)
{
uint32_t value;
UNUSED(Task);
if (PCFX_Sound)
{
value = *(int16_t *)PCFX_Sound;
PCFX_Sound += sizeof(int16_t);
if (value != PCFX_LastSample)
{
PCFX_LastSample = value;
if (value)
{
outp(0x43, 0xb6);
outp(0x42, LOBYTE(value));
outp(0x42, HIBYTE(value));
outp(0x61, inp(0x61) | 0x3);
} else
outp(0x61, inp(0x61) & 0xfc);
}
if (--PCFX_LengthLeft == 0)
PCFX_Stop(PCFX_VoiceHandle);
}
}
/*---------------------------------------------------------------------
Function: PCFX_Play
Starts playback of a Muse sound effect.
---------------------------------------------------------------------*/
typedef struct
{
uint32_t length;
uint8_t data[];
} PCSound;
static int32_t ASS_PCFX_Play(PCSound *sound)
{
uint32_t flags;
PCFX_Stop(PCFX_VoiceHandle);
PCFX_VoiceHandle++;
if (PCFX_VoiceHandle < PCFX_MinVoiceHandle)
PCFX_VoiceHandle = PCFX_MinVoiceHandle;
flags = DisableInterrupts();
PCFX_LengthLeft = sound->length;
PCFX_Sound = &sound->data[0];
RestoreInterrupts(flags);
return PCFX_VoiceHandle;
}
static const uint16_t divisors[] = {
0,
6818, 6628, 6449, 6279, 6087, 5906, 5736, 5575,
5423, 5279, 5120, 4971, 4830, 4697, 4554, 4435,
4307, 4186, 4058, 3950, 3836, 3728, 3615, 3519,
3418, 3323, 3224, 3131, 3043, 2960, 2875, 2794,
2711, 2633, 2560, 2485, 2415, 2348, 2281, 2213,
2153, 2089, 2032, 1975, 1918, 1864, 1810, 1757,
1709, 1659, 1612, 1565, 1521, 1478, 1435, 1395,
1355, 1316, 1280, 1242, 1207, 1173, 1140, 1107,
1075, 1045, 1015, 986, 959, 931, 905, 879,
854, 829, 806, 783, 760, 739, 718, 697,
677, 658, 640, 621, 604, 586, 570, 553,
538, 522, 507, 493, 479, 465, 452, 439,
427, 415, 403, 391, 380, 369, 359, 348,
339, 329, 319, 310, 302, 293, 285, 276,
269, 261, 253, 246, 239, 232, 226, 219,
213, 207, 201, 195, 190, 184, 179,
};
typedef struct {
uint32_t length;
uint16_t data[0x10000];
} pcspkmuse_t;
static pcspkmuse_t pcspkmuse;
typedef struct {
uint16_t type; // 0 = PC Speaker
uint16_t length;
uint8_t data[];
} dmxpcs_t;
int32_t PCFX_Play(void *vdata)
{
dmxpcs_t *dmxpcs = (dmxpcs_t *)vdata;
uint_fast16_t i;
pcspkmuse.length = dmxpcs->length;
for (i = 0; i < dmxpcs->length; i++)
pcspkmuse.data[i] = divisors[dmxpcs->data[i]];
return ASS_PCFX_Play((PCSound *)&pcspkmuse);
}
/*---------------------------------------------------------------------
Function: PCFX_SoundPlaying
Checks if a sound effect is currently playing.
---------------------------------------------------------------------*/
boolean PCFX_SoundPlaying(int32_t handle)
{
return (handle == PCFX_VoiceHandle) && (PCFX_LengthLeft > 0);
}
/*---------------------------------------------------------------------
Function: PCFX_Init
Initializes the sound effect engine.
---------------------------------------------------------------------*/
void PCFX_Init(int32_t ticrate)
{
if (PCFX_Installed)
return;
PCFX_Stop(PCFX_VoiceHandle);
PCFX_ServiceTask = TS_ScheduleTask(&PCFX_Service, ticrate, 2, 0);
TS_Dispatch();
PCFX_Installed = true;
}
/*---------------------------------------------------------------------
Function: PCFX_Shutdown
Ends the use of the sound effect engine.
---------------------------------------------------------------------*/
void PCFX_Shutdown(void)
{
if (PCFX_Installed)
{
PCFX_Stop(PCFX_VoiceHandle);
TS_Terminate(PCFX_ServiceTask);
PCFX_Installed = false;
}
}