/
ns_adbfx.c
267 lines (208 loc) · 6.43 KB
/
ns_adbfx.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
#include <stdlib.h>
#include <dos.h>
#include <conio.h>
#include "ns_dpmi.h"
#include "ns_task.h"
#include "ns_cards.h"
#include "ns_user.h"
#include "ns_adbfx.h"
#include "ns_sb.h"
#include "ns_sbdef.h"
#include "ns_sbmus.h"
#include "ns_muldf.h"
#include "ns_fxm.h"
#include "m_misc.h"
#include "doomstat.h"
#include "options.h"
static int ADBFX_Installed = 0;
static char *ADBFX_BufferStart;
static char *ADBFX_CurrentBuffer;
static int ADBFX_BufferNum = 0;
static int ADBFX_NumBuffers = 0;
static int ADBFX_TransferLength = 0;
static int ADBFX_CurrentLength = 0;
static int ADBFX_Device = 0;
static char *ADBFX_SoundPtr;
volatile int ADBFX_SoundPlaying;
static task *ADBFX_Timer;
void (*ADBFX_CallBack)(void);
/*---------------------------------------------------------------------
Function: ADBFX_ServiceInterrupt
Handles interrupt generated by sound card at the end of a voice
transfer. Calls the user supplied callback function.
---------------------------------------------------------------------*/
const unsigned char AdlibLUTdb[256] = {
63, 63, 56, 51, 48, 46, 43, 42,
40, 39, 38, 36, 35, 34, 34, 33,
32, 31, 31, 30, 29, 29, 28, 28,
27, 27, 26, 26, 26, 25, 25, 24,
24, 24, 23, 23, 23, 22, 22, 22,
21, 21, 21, 21, 20, 20, 20, 20,
19, 19, 19, 19, 18, 18, 18, 18,
18, 17, 17, 17, 17, 17, 16, 16,
16, 16, 16, 15, 15, 15, 15, 15,
15, 14, 14, 14, 14, 14, 14, 14,
13, 13, 13, 13, 13, 13, 13, 12,
12, 12, 12, 12, 12, 12, 12, 11,
11, 11, 11, 11, 11, 11, 11, 10,
10, 10, 10, 10, 10, 10, 10, 10,
10, 9, 9, 9, 9, 9, 9, 9, 9,
9, 9, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0};
static void ADBFX_UpdatePointer()
{
ADBFX_SoundPtr++;
ADBFX_CurrentLength--;
if (ADBFX_CurrentLength == 0)
{
// Keep track of current buffer
ADBFX_CurrentBuffer += ADBFX_TransferLength;
ADBFX_BufferNum++;
if (ADBFX_BufferNum >= ADBFX_NumBuffers)
{
ADBFX_BufferNum = 0;
ADBFX_CurrentBuffer = ADBFX_BufferStart;
}
ADBFX_CurrentLength = ADBFX_TransferLength;
ADBFX_SoundPtr = ADBFX_CurrentBuffer;
// Call the caller's callback function
if (ADBFX_CallBack != NULL)
{
MV_ServiceVoc();
}
}
}
static void ADBFX_ServiceInterrupt_ISA(task *Task)
{
unsigned char value = AdlibLUTdb[(unsigned char)(*ADBFX_SoundPtr)];
outp(ADLIB_PORT + 1, value);
ADBFX_UpdatePointer();
}
static void ADBFX_ServiceInterrupt_OPLxLPT(task *Task)
{
int lpt_data = ADLIB_PORT;
int lpt_ctrl = ADLIB_PORT + 2;
unsigned char value = AdlibLUTdb[(unsigned char)(*ADBFX_SoundPtr)];
outp(lpt_data, value);
outp(lpt_ctrl, 12);
outp(lpt_ctrl, 8);
outp(lpt_ctrl, 12);
ADBFX_UpdatePointer();
}
/*---------------------------------------------------------------------
Function: ADBFX_StopPlayback
Ends the transfer of digitized sound to the Sound Source.
---------------------------------------------------------------------*/
void ADBFX_StopPlayback(void)
{
if (ADBFX_SoundPlaying)
{
TS_Terminate(ADBFX_Timer);
ADBFX_SoundPlaying = 0;
ADBFX_BufferStart = NULL;
AL_Reset();
}
}
/*---------------------------------------------------------------------
Function: ADBFX_BeginBufferedPlayback
Begins multibuffered playback of digitized sound on the Sound Source.
---------------------------------------------------------------------*/
int ADBFX_BeginBufferedPlayback(
char *BufferStart,
int BufferSize,
int NumDivisions,
void (*CallBackFunc)(void))
{
if (ADBFX_SoundPlaying)
{
ADBFX_StopPlayback();
}
ADBFX_CallBack = CallBackFunc;
ADBFX_BufferStart = BufferStart;
ADBFX_CurrentBuffer = BufferStart;
ADBFX_SoundPtr = BufferStart;
// VITI95: OPTIMIZE
ADBFX_TransferLength = BufferSize / NumDivisions;
ADBFX_CurrentLength = ADBFX_TransferLength;
ADBFX_BufferNum = 0;
ADBFX_NumBuffers = NumDivisions;
ADBFX_SoundPlaying = 1;
switch (ADBFX_Device)
{
case Adlib:
ADBFX_Timer = TS_ScheduleTask(ADBFX_ServiceInterrupt_ISA, FX_MixRate, 1, NULL);
break;
case OPL2LPT:
case OPL3LPT:
ADBFX_Timer = TS_ScheduleTask(ADBFX_ServiceInterrupt_OPLxLPT, FX_MixRate, 1, NULL);
break;
}
TS_Dispatch();
return (ADBFX_Ok);
}
/*---------------------------------------------------------------------
Function: ADBFX_Init
Initializes the Sound Source prepares the module to play digitized
sounds.
---------------------------------------------------------------------*/
int ADBFX_Init(int soundcard, int address)
{
int i;
if (ADBFX_Installed)
{
ADBFX_Shutdown();
}
if (address <= 0)
address = ADLIB_PORT;
ADBFX_Device = soundcard;
AL_Init(soundcard, address);
AL_Reset();
AL_SendOutputToPort(ADLIB_PORT, 0x20, 0x21);
AL_SendOutputToPort(ADLIB_PORT, 0x60, 0xF0);
AL_SendOutputToPort(ADLIB_PORT, 0x80, 0xF0);
AL_SendOutputToPort(ADLIB_PORT, 0xC0, 0x01);
AL_SendOutputToPort(ADLIB_PORT, 0xE0, 0x00);
AL_SendOutputToPort(ADLIB_PORT, 0x43, 0x3F);
AL_SendOutputToPort(ADLIB_PORT, 0xB0, 0x01);
AL_SendOutputToPort(ADLIB_PORT, 0xA0, 0x8F);
AL_SendOutputToPort(ADLIB_PORT, 0xB0, 0x2E);
/* Wait */
for (i = 0; i < 500; i++)
{
inp(ADLIB_PORT);
}
AL_SendOutputToPort(ADLIB_PORT, 0xB0, 0x20);
AL_SendOutputToPort(ADLIB_PORT, 0xA0, 0x00);
/* First sample */
AL_SendOutputToPort(ADLIB_PORT, 0x40, 0x00);
ADBFX_SoundPlaying = 0;
ADBFX_CallBack = NULL;
ADBFX_BufferStart = NULL;
ADBFX_Installed = 1;
return (ADBFX_Ok);
}
/*---------------------------------------------------------------------
Function: ADBFX_Shutdown
Ends transfer of sound data to the Sound Source.
---------------------------------------------------------------------*/
void ADBFX_Shutdown(void)
{
ADBFX_StopPlayback();
ADBFX_SoundPlaying = 0;
ADBFX_BufferStart = NULL;
ADBFX_CallBack = NULL;
ADBFX_Installed = 0;
}