forked from kode54/libupse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upse_ps1_spu_base.c
260 lines (203 loc) · 6.05 KB
/
upse_ps1_spu_base.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
/*
* UPSE: the unix playstation sound emulator.
*
* Filename: upse_ps1_spu_base.c
* Purpose: libupse: PS1 SPU base code
*
* Copyright (c) 2007 William Pitcock <[email protected]>
* Portions copyright (c) 2002 Pete Bernert <[email protected]>
*
* UPSE is free software, released under the GNU General Public License,
* version 2.
*
* A copy of the GNU General Public License, version 2, is included in
* the UPSE source kit as COPYING.
*
* UPSE is offered without any warranty of any kind, explicit or implicit.
*/
#define _IN_SPU
#include "upse-types.h"
#include "upse-internal.h"
#include "upse-ps1-spu-base.h"
#include "upse-spu-internal.h"
#include "upse-ps1-spu-features.h"
#include "upse.h"
#include "upse-ps1-memory-manager.h"
#include "Neill/spu.h"
#include "Neill/spucore.h"
////////////////////////////////////////////////////////////////////////
// MAIN SPU FUNCTION
// here is the main job handler... thread, timer or direct func call
// basically the whole sound processing is done in this fat func!
////////////////////////////////////////////////////////////////////////
// Counting to 65536 results in full volume offage.
void upse_ps1_spu_setlength(upse_spu_state_t *spu, s32 stop, s32 fade)
{
_ENTER;
_DEBUG("stop [%d] fade [%d]", stop, fade);
if (stop == 0)
{
spu->decaybegin = 0;
_LEAVE;
}
else
{
stop = (stop * 441) / 10;
fade = (fade * 441) / 10;
spu->decaybegin = stop;
spu->decayend = stop + fade;
}
_LEAVE;
}
void upse_ps1_spu_setvolume(upse_spu_state_t *spu, int volume)
{
_ENTER;
_LEAVE;
}
int upse_ps1_spu_seek(upse_module_instance_t *ins, u32 t)
{
upse_spu_state_t *spu = ins->spu;
_ENTER;
spu->seektime = t * 441 / 10;
if (spu->seektime > spu->sampcount)
{
_LEAVE;
return 1;
}
_LEAVE;
return 0;
}
#define CLIP(_x) {if(_x>32767) _x=32767; if(_x<-32767) _x=-32767;}
int upse_ps1_spu_render(upse_spu_state_t *spu, u32 cycles)
{
s32 dosampies;
s32 temp;
spu->nextirq += cycles;
dosampies = spu->nextirq / 384;
if (!dosampies)
return (1);
spu->nextirq -= dosampies * 384;
temp = dosampies;
spu_render( spu->pCore, spu->pS, temp );
for ( dosampies = 0; dosampies < temp; dosampies++ )
{
if (spu->decaybegin != 0 && spu->sampcount >= spu->decaybegin)
{
s32 dmul;
if (spu->decaybegin != 0)
{
s32 sl = spu->pS[dosampies * 2];
s32 sr = spu->pS[dosampies * 2 + 1];
if (spu->sampcount >= spu->decayend)
return (0);
dmul = 256 - (256 * (spu->sampcount - spu->decaybegin) / (spu->decayend - spu->decaybegin));
_DEBUG("dmul: %d", dmul);
sl = (sl * dmul) >> 8;
sr = (sr * dmul) >> 8;
spu->pS[dosampies * 2] = sl;
spu->pS[dosampies * 2 + 1] = sr;
}
}
spu->sampcount++;
}
spu->pS += 2 * temp;
return (1);
}
void upse_ps1_spu_stop(upse_module_instance_t *ins)
{
upse_spu_state_t *spu = ins->spu;
spu->decaybegin = 1;
spu->decayend = 0;
}
void upse_ps1_spu_set_audio_callback(upse_module_instance_t *ins, upse_audio_callback_func_t func, const void *user_data)
{
upse_spu_state_t *spu = ins->spu;
_ENTER;
spu->cb = func;
spu->cb_userdata = user_data;
_DEBUG("set audio callback function to <%p>", spu->cb);
_DEBUG("set audio callback userdata to <%p>", spu->cb_userdata);
_LEAVE;
}
void upse_ps1_spu_finalize(upse_spu_state_t *spu)
{
if ((spu->seektime != (u32) ~ 0) && spu->seektime > spu->sampcount)
{
spu->pS = (s16 *) spu->pSpuBuffer;
if (spu->cb != NULL)
spu->cb(0, 0, spu->cb_userdata);
}
else if ((u8 *) spu->pS > ((u8 *) spu->pSpuBuffer + 1024))
{
#ifdef FEAT_NYQUIST_MODULATION
upse_spu_nyquist_filter_process(spu, (s16 *) spu->pSpuBuffer, ((u8 *) spu->pS - (u8 *) spu->pSpuBuffer) / 4);
#endif
#ifdef FEAT_CORLETT_IIR_FILTERS
upse_spu_lowpass_filter_process(spu, (s16 *) spu->pSpuBuffer, ((u8 *) spu->pS - (u8 *) spu->pSpuBuffer) / 4);
#endif
if (spu->cb != NULL)
spu->cb((u8 *) spu->pSpuBuffer, (u8 *) spu->pS - (u8 *) spu->pSpuBuffer, spu->cb_userdata);
spu->pS = (s16 *) spu->pSpuBuffer;
}
}
int upse_ps1_spu_finalize_count(upse_spu_state_t *spu, s16 ** s)
{
if ((spu->seektime != (u32) ~ 0) && spu->seektime > spu->sampcount)
{
spu->pS = (s16 *) spu->pSpuBuffer;
*s = NULL;
return 1;
}
else if ((u8 *) spu->pS > ((u8 *) spu->pSpuBuffer + 1024))
{
unsigned samples_rendered = ( (u8 *) spu->pS - (u8 *) spu->pSpuBuffer ) / 4;
#ifdef FEAT_NYQUIST_MODULATION
upse_spu_nyquist_filter_process(spu, (s16 *) spu->pSpuBuffer, ((u8 *) spu->pS - (u8 *) spu->pSpuBuffer) / 4);
#endif
#ifdef FEAT_CORLETT_IIR_FILTERS
upse_spu_lowpass_filter_process(spu, (s16 *) spu->pSpuBuffer, samples_rendered);
#endif
spu->pS = (s16 *) spu->pSpuBuffer;
*s = spu->pS;
return samples_rendered;
}
return 0;
}
////////////////////////////////////////////////////////////////////////
// INIT/EXIT STUFF
////////////////////////////////////////////////////////////////////////
static void
upse_ps1_spu_setup_streams(upse_spu_state_t *spu)
{
if (spu == NULL)
return;
spu->pS = (s16 *) spu->pSpuBuffer;
}
upse_spu_state_t *
upse_ps1_spu_open(upse_module_instance_t *ins)
{
static int initialized = 0;
upse_spu_state_t *spu = calloc(sizeof(upse_spu_state_t), 1);
if ( !initialized )
{
spu_init();
spucore_init();
initialized = 1;
}
spu->pCore = calloc(spu_get_state_size(1), 1);
spu_clear_state(spu->pCore, 1);
spu->sampcount = spu->nextirq = 0;
spu->seektime = (u32) ~ 0;
spu->ins = ins;
upse_ps1_spu_setup_streams(spu); // prepare streaming
upse_spu_lowpass_filter_redesign(spu, 44100);
return spu;
}
void
upse_ps1_spu_close(upse_spu_state_t *spu)
{
if (spu == NULL)
return;
free(spu->pCore);
free(spu);
}