summaryrefslogtreecommitdiff
path: root/lib/ultralib/src/audio/synthInternals.h
blob: c6c476c76587fcfcb7fe0ebc3a7a373cc696a6b9 (plain)
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
/*====================================================================
 * audioInternals.h
 *
 * Synopsis:
 *
 * Copyright 1993, Silicon Graphics, Inc.
 * All Rights Reserved.
 *
 * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics,
 * Inc.; the contents of this file may not be disclosed to third
 * parties, copied or duplicated in any form, in whole or in part,
 * without the prior written permission of Silicon Graphics, Inc.
 *
 * RESTRICTED RIGHTS LEGEND:
 * Use, duplication or disclosure by the Government is subject to
 * restrictions as set forth in subdivision (c)(1)(ii) of the Rights
 * in Technical Data and Computer Software clause at DFARS
 * 252.227-7013, and/or in similar or successor clauses in the FAR,
 * DOD or NASA FAR Supplement. Unpublished - rights reserved under the
 * Copyright Laws of the United States.
 *====================================================================*/

#ifndef __audioInternals__
#define __audioInternals__

#include <libaudio.h>
#include <PR/os_version.h>

/*
 * filter message ids
 */
enum {
    AL_FILTER_FREE_VOICE,
    AL_FILTER_SET_SOURCE,
    AL_FILTER_ADD_SOURCE,
    AL_FILTER_ADD_UPDATE,
    AL_FILTER_RESET,
    AL_FILTER_SET_WAVETABLE,
/*    AL_FILTER_SET_DMA_PROC,*/
/*    AL_FILTER_SKIP_LOOP,*/
    AL_FILTER_SET_DRAM,
    AL_FILTER_SET_PITCH,
    AL_FILTER_SET_UNITY_PITCH,
    AL_FILTER_START,
/*    AL_FILTER_SET_DECAY,*/
/*    AL_FILTER_SET_FC,*/
    AL_FILTER_SET_STATE,
    AL_FILTER_SET_VOLUME,
    AL_FILTER_SET_PAN,
    AL_FILTER_START_VOICE_ALT,
    AL_FILTER_START_VOICE,
    AL_FILTER_STOP_VOICE,
    AL_FILTER_SET_FXAMT
};

#define AL_MAX_RSP_SAMPLES      160
    
/*
 * buffer locations based on AL_MAX_RSP_SAMPLES
 */
#define AL_DECODER_IN	        0
#define	AL_RESAMPLER_OUT	0
#define AL_TEMP_0	        0
#define	AL_DECODER_OUT	        320
#define	AL_TEMP_1	        320
#define	AL_TEMP_2	        640
#define	AL_MAIN_L_OUT	        1088
#define	AL_MAIN_R_OUT	        1408
#define	AL_AUX_L_OUT	        1728
#define	AL_AUX_R_OUT	        2048

/*
 * filter types
 */
enum {
    AL_ADPCM,
    AL_RESAMPLE,
    AL_BUFFER,
    AL_SAVE,
    AL_ENVMIX,
    AL_FX,
    AL_AUXBUS,
    AL_MAINBUS
};

typedef struct ALParam_s {
    struct ALParam_s    *next;
    s32                 delta;
    s16                 type;
    union {
        f32             f;
        s32             i;
    } data;
    union {
        f32             f;
        s32             i;
    } moredata;
    union {
        f32             f;
        s32             i;
    } stillmoredata;
    union {
        f32             f;
        s32             i;
    } yetstillmoredata;
} ALParam;

typedef struct {
    struct ALParam_s            *next;
    s32                         delta;
    s16                         type;
    s16                         unity;  /* disable resampler */
    f32                         pitch;
    s16                         volume;
    ALPan                       pan;
    u8                          fxMix;
    s32                         samples;
    struct ALWaveTable_s        *wave;
} ALStartParamAlt;

typedef struct {
    struct ALParam_s            *next;
    s32                         delta;
    s16                         type;
    s16                         unity;  /* disable resampler */
    struct ALWaveTable_s        *wave;
} ALStartParam;

typedef struct {
    struct ALParam_s    *next;
    s32                 delta;
    s16                 type;
    struct PVoice_s     *pvoice;
} ALFreeParam;

typedef Acmd *(*ALCmdHandler)(void *, s16 *, s32, s32, Acmd *);
typedef s32   (*ALSetParam)(void *, s32, void *);

typedef struct ALFilter_s {
    struct ALFilter_s   *source;
    ALCmdHandler        handler;
    ALSetParam          setParam;
    s16                 inp;
    s16                 outp;
    s32                 type;
} ALFilter;

void    alFilterNew(ALFilter *f, ALCmdHandler h, ALSetParam s, s32 type);

#define AL_MAX_ADPCM_STATES     3       /* Depends on number of subframes
                                         * per frame and loop length
                                         */
typedef struct {
    ALFilter                    filter;
    ADPCM_STATE                 *state;
    ADPCM_STATE                 *lstate;
    ALRawLoop                   loop;
    struct ALWaveTable_s        *table;
    s32                         bookSize;
    ALDMAproc                   dma;
    void                        *dmaState;
    s32                         sample;
    s32                         lastsam;
    s32                         first;
    s32                         memin; 
} ALLoadFilter;

void    alLoadNew(ALLoadFilter *f, ALDMANew dma, ALHeap *hp);
Acmd    *alAdpcmPull(void *f, s16 *outp, s32 byteCount, s32 sampleOffset, Acmd *p);
Acmd    *alRaw16Pull(void *f, s16 *outp, s32 byteCount, s32 sampleOffset, Acmd *p);
s32     alLoadParam(void *filter, s32 paramID, void *param);

typedef struct ALResampler_s {
    ALFilter            filter;
    RESAMPLE_STATE      *state;
    f32                 ratio;
    s32			upitch;
    f32		        delta;
    s32			first;
    ALParam		*ctrlList;
    ALParam		*ctrlTail;
    s32                 motion;
} ALResampler;

typedef struct {
    s16		        fc;
    s16		        fgain;
    union {
        s16		fccoef[16];
        s64             force_aligned;
    } fcvec;
    POLEF_STATE		*fstate;
    s32			first;
} ALLowPass;

typedef struct {
    u32		input;
    u32		output;
    s16		ffcoef;
    s16		fbcoef;
    s16		gain;
    f32		rsinc;
    f32		rsval;
    s32		rsdelta;
    f32		rsgain;
    ALLowPass	*lp;
    ALResampler	*rs;
} ALDelay;

typedef s32   (*ALSetFXParam)(void *, s32, void *);
typedef struct {
    struct ALFilter_s   filter;
    s16			*base;
    s16			*input;
    u32			length;
    ALDelay		*delay;
    u8			section_count;
    ALSetFXParam        paramHdl;
} ALFx;

void    alFxNew(ALFx *r, ALSynConfig *c, ALHeap *hp);
Acmd    *alFxPull(void *f, s16 *outp, s32 out, s32 sampleOffset, Acmd *p);
s32     alFxParam(void *filter, s32 paramID, void *param);
s32     alFxParamHdl(void *filter, s32 paramID, void *param);

#define AL_MAX_MAIN_BUS_SOURCES       1
typedef struct ALMainBus_s {
    ALFilter            filter;
    s32                 sourceCount;
    s32                 maxSources;
    ALFilter            **sources;
} ALMainBus;

void    alMainBusNew(ALMainBus *m, void *ptr, s32 len);
Acmd    *alMainBusPull(void *f, s16 *outp, s32 outCount, s32 sampleOffset, Acmd *p);
s32     alMainBusParam(void *filter, s32 paramID, void *param);

#define AL_MAX_AUX_BUS_SOURCES       8
#define AL_MAX_AUX_BUS_FX	     1
typedef struct ALAuxBus_s {
    ALFilter            filter;
    s32                 sourceCount;
    s32                 maxSources;
    ALFilter            **sources;
    ALFx		fx[AL_MAX_AUX_BUS_FX];
} ALAuxBus;

void    alAuxBusNew(ALAuxBus *m, void *ptr, s32 len);
Acmd    *alAuxBusPull(void *f, s16 *outp, s32 outCount, s32 sampleOffset, Acmd *p);
s32     alAuxBusParam(void *filter, s32 paramID, void *param);

void    alResampleNew(ALResampler *r, ALHeap *hp);
Acmd    *alResamplePull(void *f, s16 *outp, s32 out, s32 sampleOffset, Acmd *p);
s32     alResampleParam(void *f, s32 paramID, void *param);

typedef struct ALSave_s {
    ALFilter            filter;
    s32	       		dramout;
    s32                 first;
} ALSave;

void    alSaveNew(ALSave *r);
Acmd    *alSavePull(void *f, s16 *outp, s32 outCount, s32 sampleOffset, Acmd *p);
s32     alSaveParam(void *f, s32 paramID, void *param);

typedef struct ALEnvMixer_s {
    ALFilter            filter;
    ENVMIX_STATE	*state;
    s16		        pan;
    s16		        volume;
    s16		        cvolL;
    s16		        cvolR;
    s16		        dryamt;
    s16		        wetamt;
    u16                 lratl;
    s16                 lratm;
    s16                 ltgt;
    u16                 rratl;
    s16                 rratm;
    s16                 rtgt;
    s32                 delta;
    s32                 segEnd;
    s32			first;
    ALParam		*ctrlList;
    ALParam		*ctrlTail;
    ALFilter            **sources;
    s32                 motion;
} ALEnvMixer;

void    alEnvmixerNew(ALEnvMixer *e, ALHeap *hp);
Acmd    *alEnvmixerPull(void *f, s16 *outp, s32 out, s32 sampleOffset, Acmd *p);
s32     alEnvmixerParam(void *filter, s32 paramID, void *param);


/*
 * heap stuff
 */
typedef struct {
    s32         magic;  /* check structure integrety                    */
    s32         size;   /* size of this allocated block                 */
    u8          *file;  /* file that this alloc was called from         */
    s32         line;   /* line that it was called from                 */
    s32         count;  /* heap call number                             */
    s32         pad0;
    s32         pad1;
    s32         pad2;   /* Make it 32 bytes                             */
} HeapInfo;

#define AL_CACHE_ALIGN  15

/*
 * synth stuff
 */

typedef struct PVoice_s {
    ALLink               node;
    struct ALVoice_s    *vvoice;
    ALFilter            *channelKnob;
    ALLoadFilter        decoder;
    ALResampler         resampler;
    ALEnvMixer		envmixer;
    s32                 offset;
} PVoice;

/*
 * prototypes for private driver functions
 */
ALParam         *__allocParam(void);
void            __freeParam(ALParam *param);
void            _freePVoice(ALSynth *drvr, PVoice *pvoice);
void            _collectPVoices(ALSynth *drvr);

s32             _timeToSamples(ALSynth *ALSynth, s32 micros);
ALMicroTime     _samplesToTime(ALSynth *synth, s32 samples);

// This was renamed to have a leading underscore in 2.0J
#if BUILD_VERSION < VERSION_J
#define _init_lpfilter init_lpfilter
#endif

void            _init_lpfilter(ALLowPass *lp);

#endif