forked from libretro/jumpnbump-libretro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dj.c
431 lines (350 loc) · 8.89 KB
/
dj.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
#include <limits.h>
#include "globals.h"
#include "micromod.h"
sfx_data sounds[NUM_SFX];
#define MAX_CHANNELS 32
typedef struct {
/* loop flag */
int loop;
/* The channel step amount... */
unsigned int step;
/* ... and a 0.16 bit remainder of last step. */
unsigned int stepremainder;
unsigned int samplerate;
/* The channel data pointers, start and end. */
signed short* data;
signed short* startdata;
signed short* enddata;
/* Hardware left and right channel volume lookup. */
int leftvol;
int rightvol;
} channel_info_t;
channel_info_t channelinfo[MAX_CHANNELS];
/* Sample rate in samples/second */
int audio_rate = 44100;
int global_sfx_volume = 0;
/*
// This function loops all active (internal) sound
// channels, retrieves a given number of samples
// from the raw sound data, modifies it according
// to the current (internal) channel parameters,
// mixes the per channel samples into the given
// mixing buffer, and clamping it to the allowed
// range.
//
// This function currently supports only 16bit.
*/
static void stopchan(int i)
{
if (channelinfo[i].data) {
memset(&channelinfo[i], 0, sizeof(channel_info_t));
}
}
/*
// This function adds a sound to the
// list of currently active sounds,
// which is maintained as a given number
// (eight, usually) of internal channels.
// Returns a handle.
*/
int addsfx(signed short *data, int len, int loop, int samplerate, int channel)
{
stopchan(channel);
/* We will handle the new SFX. */
/* Set pointer to raw data. */
channelinfo[channel].data = data;
channelinfo[channel].startdata = data;
/* Set pointer to end of raw data. */
channelinfo[channel].enddata = channelinfo[channel].data + len - 1;
channelinfo[channel].samplerate = samplerate;
channelinfo[channel].loop = loop;
channelinfo[channel].stepremainder = 0;
return channel;
}
static void updateSoundParams(int slot, int volume)
{
int rightvol;
int leftvol;
/*
// Set stepping
// MWM 2000-12-24: Calculates proportion of channel samplerate
// to global samplerate for mixing purposes.
// Patched to shift left *then* divide, to minimize roundoff errors
// as well as to use SAMPLERATE as defined above, not to assume 11025 Hz
*/
channelinfo[slot].step = ((channelinfo[slot].samplerate<<16)/audio_rate);
leftvol = volume;
rightvol= volume;
/* Sanity check, clamp volume. */
if (rightvol < 0)
rightvol = 0;
if (rightvol > 127)
rightvol = 127;
if (leftvol < 0)
leftvol = 0;
if (leftvol > 127)
leftvol = 127;
channelinfo[slot].leftvol = leftvol;
channelinfo[slot].rightvol = rightvol;
}
void mix_sound(signed short *stream, int len)
{
/* Mix current sound data. */
/* Data, from raw sound, for right and left. */
register int sample;
register int dl;
register int dr;
/* Pointers in audio stream, left, right, end. */
signed short* leftout;
signed short* rightout;
signed short* leftend;
/* Step in stream, left and right, thus two. */
int step;
/* Mixing channel index. */
int chan;
/* Left and right channel */
/* are in audio stream, alternating. */
leftout = (signed short *)stream;
rightout = ((signed short *)stream)+1;
step = 2;
/* Determine end, for left channel only */
/* (right channel is implicit). */
leftend = leftout + (len/4)*step;
/* Mix sounds into the mixing buffer. */
/* Loop over step*SAMPLECOUNT, */
/* that is 512 values for two channels. */
while (leftout != leftend) {
/* Reset left/right value. */
dl = *leftout * 256;
dr = *rightout * 256;
/* Love thy L2 chache - made this a loop. */
/* Now more channels could be set at compile time */
/* as well. Thus loop those channels. */
for ( chan = 0; chan < MAX_CHANNELS; chan++ ) {
/* Check channel, if active. */
if (channelinfo[chan].data) {
/* Get the raw data from the channel. */
/* no filtering */
/* sample = *channelinfo[chan].data; */
/* linear filtering */
sample = (int)(((int)channelinfo[chan].data[0] * (int)(0x10000 - channelinfo[chan].stepremainder))
+ ((int)channelinfo[chan].data[1] * (int)(channelinfo[chan].stepremainder))) >> 16;
/* Add left and right part */
/* for this channel (sound) */
/* to the current data. */
/* Adjust volume accordingly. */
dl += sample * (channelinfo[chan].leftvol * global_sfx_volume) / 128;
dr += sample * (channelinfo[chan].rightvol * global_sfx_volume) / 128;
/* Increment index ??? */
channelinfo[chan].stepremainder += channelinfo[chan].step;
/* MSB is next sample??? */
channelinfo[chan].data += channelinfo[chan].stepremainder >> 16;
/* Limit to LSB??? */
channelinfo[chan].stepremainder &= 0xffff;
/* Check whether we are done. */
if (channelinfo[chan].data >= channelinfo[chan].enddata) {
if (channelinfo[chan].loop) {
channelinfo[chan].data = channelinfo[chan].startdata;
} else {
stopchan(chan);
}
}
}
}
/* Clamp to range. Left hardware channel. */
/* Has been char instead of short. */
/* if (dl > 127) *leftout = 127; */
/* else if (dl < -128) *leftout = -128; */
/* else *leftout = dl; */
dl = dl / 256;
dr = dr / 256;
if (dl > SHRT_MAX)
*leftout = SHRT_MAX;
else if (dl < SHRT_MIN)
*leftout = SHRT_MIN;
else
*leftout = (signed short)dl;
/* Same for right hardware channel. */
if (dr > SHRT_MAX)
*rightout = SHRT_MAX;
else if (dr < SHRT_MIN)
*rightout = SHRT_MIN;
else
*rightout = (signed short)dr;
/* Increment current pointers in stream */
leftout += step;
rightout += step;
}
}
char dj_init(void)
{
if (main_info.no_sound)
return 0;
memset(channelinfo, 0, sizeof(channelinfo));
memset(sounds, 0, sizeof(sounds));
return 0;
}
void dj_deinit(void)
{
if (main_info.no_sound)
return;
}
void dj_start(void)
{
}
void dj_stop(void)
{
}
char dj_autodetect_sd(void)
{
return 0;
}
char dj_set_stereo(char flag)
{
return 0;
}
void dj_set_auto_mix(char flag)
{
}
unsigned short dj_set_mixing_freq(unsigned short freq)
{
return freq;
}
void dj_set_dma_time(unsigned short time)
{
}
void dj_set_nosound(char flag)
{
}
/* sfx handling */
char dj_set_num_sfx_channels(char num_channels)
{
return num_channels;
}
void dj_set_sfx_volume(char volume)
{
if (main_info.no_sound)
return;
global_sfx_volume = volume*2;
}
void dj_play_sfx(unsigned char sfx_num, unsigned short freq, char volume, char panning, unsigned short delay, int channel)
{
int slot;
if (main_info.music_no_sound || main_info.no_sound)
return;
if (channel<0) {
for (slot=0; slot<MAX_CHANNELS; slot++)
if (channelinfo[slot].data==NULL)
break;
if (slot>=MAX_CHANNELS)
return;
} else
slot = channel;
addsfx((short *)sounds[sfx_num].buf, sounds[sfx_num].length, sounds[sfx_num].loop, freq, slot);
updateSoundParams(slot, volume*2);
}
char dj_get_sfx_settings(unsigned char sfx_num, sfx_data *data)
{
if (main_info.no_sound)
return 0;
memcpy(data, &sounds[sfx_num], sizeof(sfx_data));
return 0;
}
char dj_set_sfx_settings(unsigned char sfx_num, sfx_data *data)
{
if (main_info.no_sound)
return 0;
memcpy(&sounds[sfx_num], data, sizeof(sfx_data));
return 0;
}
void dj_set_sfx_channel_volume(char channel_num, char volume)
{
if (main_info.no_sound)
return;
updateSoundParams(channel_num, volume*2);
}
void dj_stop_sfx_channel(char channel_num)
{
if (main_info.no_sound)
return;
stopchan(channel_num);
}
char dj_load_sfx(unsigned char * file_handle, char *filename, int file_length, char sfx_type, unsigned char sfx_num)
{
unsigned int i;
unsigned char *src;
unsigned short *dest;
if (main_info.no_sound)
return 0;
sounds[sfx_num].buf = (unsigned char*) malloc(file_length);
memcpy(sounds[sfx_num].buf, file_handle, file_length);
sounds[sfx_num].length = file_length / 2;
src = sounds[sfx_num].buf;
dest = (unsigned short *)sounds[sfx_num].buf;
for (i=0; i<sounds[sfx_num].length; i++)
{
unsigned short temp;
temp = src[0] + (src[1] << 8);
*dest = temp;
src += 2;
dest++;
}
return 0;
}
void dj_free_sfx(unsigned char sfx_num)
{
if (main_info.no_sound)
return;
free(sounds[sfx_num].buf);
memset(&sounds[sfx_num], 0, sizeof(sfx_data));
}
/* mod handling */
char dj_ready_mod(char mod_num)
{
unsigned char *fp;
if (main_info.no_sound)
return 0;
switch (mod_num) {
case MOD_MENU:
fp = dat_open("jump.mod");
break;
case MOD_GAME:
fp = dat_open("bump.mod");
break;
case MOD_SCORES:
fp = dat_open("scores.mod");
break;
default:
fprintf(stderr, "bogus parameter to dj_ready_mod()\n");
fp = NULL;
break;
}
if (fp == NULL) {
return 0;
}
return micromod_initialise((signed char*) fp, audio_rate);
}
char dj_start_mod(void)
{
if (main_info.no_sound)
return 0;
return 0;
}
void dj_stop_mod(void)
{
if (main_info.no_sound)
return;
}
void dj_set_mod_volume(char volume)
{
if (main_info.no_sound)
return;
micromod_set_gain(volume);
}
char dj_load_mod(unsigned char * file_handle, char *filename, char mod_num)
{
return 0;
}
void dj_free_mod(char mod_num)
{
}