1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - include - snd - common
3   File:     exchannel.h
4 
5   Copyright 2004-2008 Nintendo. All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law. They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Date:: 2008-09-18#$
14   $Rev: 8573 $
15   $Author: okubata_ryoma $
16  *---------------------------------------------------------------------------*/
17 
18 #ifndef NITRO_SND_COMMON_EX_CHANNEL_H_
19 #define NITRO_SND_COMMON_EX_CHANNEL_H_
20 
21 #include <nitro/types.h>
22 #include <nitro/snd/common/channel.h>
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 /******************************************************************************
29 	Macro Definitions
30  ******************************************************************************/
31 
32 #define SND_EX_CHANNEL_PRIO_HIGHEST 255
33 #define SND_EX_CHANNEL_PRIO_LOWEST    1
34 
35 #define SND_EX_CHANNEL_RELEASE_PRIO 1
36 #define SND_EX_CHANNEL_STOP_PRIO    0
37 
38 #define SND_EX_CHANNEL_USER_PAN_MIN -128
39 #define SND_EX_CHANNEL_USER_PAN_MAX  127
40 
41 #define SND_EX_CHANNEL_ENV_DECAY_SHIFT 7
42 
43 #define SND_EX_CHANNEL_UPDATE_START  (0x01 << 0)
44 #define SND_EX_CHANNEL_UPDATE_STOP   (0x01 << 1)
45 #define SND_EX_CHANNEL_UPDATE_TIMER  (0x01 << 2)
46 #define SND_EX_CHANNEL_UPDATE_VOLUME (0x01 << 3)
47 #define SND_EX_CHANNEL_UPDATE_PAN    (0x01 << 4)
48 
49 #define SND_LOCK_IMPLIED_ALLOC_CHANNEL ( 1 << 0 )
50 
51 /******************************************************************************
52 	Structure Declaration
53  ******************************************************************************/
54 
55 struct SNDExChannel;
56 
57 /******************************************************************************
58 	Enum Definitions
59  ******************************************************************************/
60 
61 typedef enum SNDExChannelType
62 {
63     SND_EX_CHANNEL_PCM,
64     SND_EX_CHANNEL_PSG,
65     SND_EX_CHANNEL_NOISE
66 }
67 SNDExChannelType;
68 
69 typedef enum SNDExChannelCallbackStatus
70 {
71     SND_EX_CHANNEL_CALLBACK_DROP,
72     SND_EX_CHANNEL_CALLBACK_FINISH
73 }
74 SNDExChannelCallbackStatus;
75 
76 typedef enum SNDEnvStatus
77 {
78     SND_ENV_ATTACK,
79     SND_ENV_DECAY,
80     SND_ENV_SUSTAIN,
81     SND_ENV_RELEASE
82 }
83 SNDEnvStatus;
84 
85 typedef enum SNDLfoTarget
86 {
87     SND_LFO_PITCH,
88     SND_LFO_VOLUME,
89     SND_LFO_PAN
90 }
91 SNDLfoTarget;
92 
93 /******************************************************************************
94 	Type Definitions
95  ******************************************************************************/
96 
97 typedef void (*SNDExChannelCallback) (struct SNDExChannel *ch_p,
98                                       SNDExChannelCallbackStatus status, void *userData);
99 
100 /******************************************************************************
101 	Structure Definitions
102  ******************************************************************************/
103 
104 #if !(defined(SDK_WIN32) || defined(SDK_FROM_TOOL))
105 
106 typedef struct SNDWaveParam
107 {
108     u8      format;                    // enum SNDWaveFormat
109     u8      loopflag;                  // boolean
110     u16     rate;                      // sampling rate
111     u16     timer;                     // = SND_TIMER_CLOCK / rate
112     u16     loopstart;                 // Loop-Start-Point [word length]
113     u32     looplen;                   // Loop-Length [word length]
114 }
115 SNDWaveParam;                          // total 12 Bytes
116 
117 #else
118 
119 typedef struct SNDWaveParam
120 {
121     u8      format;                    // enum SNDWaveFormat
122     u8      loopflag;                  // boolean
123     u16     rate;                      // sampling rate
124     u16     timer;                     // = SND_TIMER_CLOCK / rate
125     u32     loopstart;                 // Loop-Start-Point [sample length]
126     u32     looplen;                   // Loop-Length [sample length]
127 }
128 SNDWaveParam;
129 
130 #endif
131 
132 typedef struct SNDLfoParam
133 {
134     u8      target;                    // enum SNDLfoTarget
135     u8      speed;                     // At 256, one sample at a time
136     u8      depth;                     // At 128, multiplied by 1.0
137     u8      range;
138     u16     delay;
139 }
140 SNDLfoParam;
141 
142 typedef struct SNDLfo
143 {
144     struct SNDLfoParam param;
145 
146     u16     delay_counter;
147     u16     counter;
148 }
149 SNDLfo;
150 
151 typedef struct SNDExChannel
152 {
153     u8      myNo;
154     u8      type;                      // SNDExChannelType
155     u8      env_status;                // SNDEnvStatus
156     u8      active_flag:1;
157     u8      start_flag:1;
158     u8      auto_sweep:1;
159     u8      sync_flag:5;
160 
161     u8      pan_range;
162     u8      original_key;
163     s16     user_decay2;
164 
165     u8      key;
166     u8      velocity;
167     s8      init_pan;
168     s8      user_pan;
169 
170     s16     user_decay;
171     s16     user_pitch;
172 
173     s32     env_decay;                 // Attenuation due to envelope release
174 
175     s32     sweep_counter;
176     s32     sweep_length;
177 
178     u8      attack;
179     u8      sustain;
180     u16     decay;
181     u16     release;
182 
183     u8      prio;
184     u8      pan;
185     u16     volume;
186     u16     timer;
187 
188     struct SNDLfo lfo;
189     s16     sweep_pitch;
190 
191     s32     length;
192 
193     struct SNDWaveParam wave;
194     union
195     {
196         const void *data;
197         SNDDuty duty;
198     };
199 
200     SNDExChannelCallback callback;
201     void   *callback_data;
202 
203     struct SNDExChannel *nextLink;
204 }
205 SNDExChannel;
206 
207 
208 /******************************************************************************
209 	Public Function Declarations
210  ******************************************************************************/
211 
212 #ifdef SDK_ARM7
213 
214 void    SND_ExChannelInit(void);
215 void    SND_ExChannelMain(BOOL doPeriodicProc);
216 void    SND_UpdateExChannel(void);
217 
218 struct SNDExChannel *SND_AllocExChannel(u32 chBitMask,
219                                         int prio,
220                                         BOOL strongRequest,
221                                         SNDExChannelCallback callback, void *callbackData);
222 void    SND_FreeExChannel(struct SNDExChannel *ch_p);
223 
224 BOOL    SND_StartExChannelPcm(SNDExChannel *ch_p,
225                               const struct SNDWaveParam *wave, const void *data, s32 length);
226 BOOL    SND_StartExChannelPsg(struct SNDExChannel *ch_p, SNDDuty duty, s32 length);
227 BOOL    SND_StartExChannelNoise(struct SNDExChannel *ch_p, s32 length);
228 
229 void    SND_ReleaseExChannel(struct SNDExChannel *ch_p);
230 
231 BOOL    SND_IsExChannelActive(struct SNDExChannel *ch_p);
232 
233 void    SND_InvalidateWave(const void *start, const void *end);
234 
235 //-----------------------------------------------------------------------------
236 // Envelope
237 
238 s32     SND_UpdateExChannelEnvelope(SNDExChannel *ch_p, BOOL doPeriodicProc);
239 
240 void    SND_SetExChannelAttack(struct SNDExChannel *ch_p, int attack);
241 void    SND_SetExChannelDecay(struct SNDExChannel *ch_p, int decay);
242 void    SND_SetExChannelSustain(struct SNDExChannel *ch_p, int sustain);
243 void    SND_SetExChannelRelease(struct SNDExChannel *ch_p, int release);
244 
245 //-----------------------------------------------------------------------------
246 // LFO
247 
248 void    SND_InitLfoParam(SNDLfoParam *lfo);
249 void    SND_StartLfo(SNDLfo *lfo);
250 void    SND_UpdateLfo(SNDLfo *lfo);
251 s32     SND_GetLfoValue(SNDLfo *lfo);
252 
253 //-----------------------------------------------------------------------------
254 // channel lock
255 
256 void    SND_LockChannel(u32 chBitMask, u32 flags);
257 void    SND_UnlockChannel(u32 chBitMask, u32 flags);
258 void    SND_StopUnlockedChannel(u32 chBitMask, u32 flags);
259 u32     SND_GetLockedChannel(u32 flags);
260 
261 #endif /* SDK_ARM7 */
262 
263 #ifdef __cplusplus
264 } /* extern "C" */
265 #endif
266 
267 #endif /* NITRO_SND_COMMON_EX_CHANNEL_H_ */
268