ps2sdk  1.1
A collection of Open Source libraries used for developing applications on Sony's PlayStation 2® (PS2).
ps2snd.c
Go to the documentation of this file.
1 /*
2 # _____ ___ ____ ___ ____
3 # ____| | ____| | | |____|
4 # | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
5 #-----------------------------------------------------------------------
6 # Copyright 2005, James Lee (jbit<at>jbit<dot>net)
7 # Licenced under Academic Free License version 2.0
8 # Review ps2sdk README & LICENSE files for further details.
9 */
10 
11 #include <string.h>
12 #include <kernel.h>
13 #include <sifrpc.h>
14 #include <iopheap.h>
15 #include <stdio.h>
16 #include <ps2snd.h>
17 
19 static int sd_started = 0;
20 
21 int sceSdInit(int flag)
22 {
23  s32 buf[1] ALIGNED(64);
24 
25  if (!sd_started)
26  {
27 
28  while(1)
29  {
30  if (SifBindRpc(&sd_client, BINDID_PS2SND, 0) < 0)
31  {
32  printf("%s: Failed to bind to rpc '0x%08x'\n", __FUNCTION__, BINDID_PS2SND);
33  return(-1);
34  }
35 
36  if (sd_client.server != NULL)
37  break;
38 
39  nopdelay();
40  }
41 
42  }
43 
44  buf[0] = flag;
45  SifCallRpc(&sd_client, PS2SND_Init, 0, buf, 4, buf, 4, NULL, NULL);
46 
47  return(buf[0]);
48 }
49 
50 void sceSdSetParam(u16 entry, u16 value)
51 {
52  u32 buf[2] ALIGNED(64);
53  buf[0] = entry;
54  buf[1] = value;
55  SifCallRpc(&sd_client, PS2SND_SetParam, 0, buf, 8, NULL, 0, NULL, NULL);
56 }
57 
59 {
60  u32 buf[1] ALIGNED(64);
61  buf[0] = entry;
62  SifCallRpc(&sd_client, PS2SND_GetParam, 0, buf, 4, buf, 4, NULL, NULL);
63  return(buf[0]);
64 }
65 
66 void sceSdSetSwitch(u16 entry, u32 value)
67 {
68  u32 buf[2] ALIGNED(64);
69  buf[0] = entry;
70  buf[1] = value;
71  SifCallRpc(&sd_client, PS2SND_SetSwitch, 0, buf, 8, NULL, 0, NULL, NULL);
72 }
73 
75 {
76  u32 buf[1] ALIGNED(64);
77  buf[0] = entry;
78  SifCallRpc(&sd_client, PS2SND_GetSwitch, 0, buf, 4, buf, 4, NULL, NULL);
79  return(buf[0]);
80 }
81 
82 void sceSdSetAddr(u16 entry, u32 value)
83 {
84  u32 buf[2] ALIGNED(64);
85  buf[0] = entry;
86  buf[1] = value;
87  SifCallRpc(&sd_client, PS2SND_SetAddr, 0, buf, 8, NULL, 0, NULL, NULL);
88 }
89 
91 {
92  u32 buf[1] ALIGNED(64);
93  buf[0] = entry;
94  SifCallRpc(&sd_client, PS2SND_GetAddr, 0, buf, 4, buf, 4, NULL, NULL);
95  return(buf[0]);
96 }
97 
98 void sceSdSetCoreAttr(u16 entry, u16 value)
99 {
100  u32 buf[2] ALIGNED(64);
101  buf[0] = entry;
102  buf[1] = value;
103  SifCallRpc(&sd_client, PS2SND_SetCoreAttr, 0, buf, 8, NULL, 0, NULL, NULL);
104 }
105 
107 {
108  u32 buf[1] ALIGNED(64);
109  buf[0] = entry;
110  SifCallRpc(&sd_client, PS2SND_GetCoreAttr, 0, buf, 4, buf, 4, NULL, NULL);
111  return(buf[0]);
112 }
113 
114 u16 sceSdNote2Pitch(u16 center_note, u16 center_fine, u16 note, s16 fine)
115 {
116  /* TODO: These functions were documented for the ps1 once... */
117  return(0);
118 }
119 
120 u16 sceSdPitch2Note(u16 center_note, u16 center_fine, u16 pitch)
121 {
122  /* TODO: These functions were documented for the ps1 once... */
123  return(0);
124 }
125 
126 int sceSdProcBatch(sceSdBatch* batch, u32 returns[], u32 num)
127 {
128  /* TODO */
129  return(-1);
130 }
131 
132 int sceSdProcBatchEx(sceSdBatch* batch, u32 returns[], u32 num, u32 voice)
133 {
134  /* TODO */
135  return(-1);
136 }
137 
138 
139 int sceSdVoiceTrans(s16 channel, u16 mode, u8 *m_addr, u32 *s_addr, u32 size)
140 {
141  u32 buf[5] ALIGNED(64);
142  ((s32 *)buf)[0] = channel;
143  buf[1] = mode;
144  buf[2] = (u32)m_addr;
145  buf[3] = (u32)s_addr;
146  buf[4] = size;
147 
148  SifCallRpc(&sd_client, PS2SND_VoiceTrans, 0, buf, 20, buf, 4, NULL, NULL);
149  return(((s32 *)buf)[0]);
150 }
151 
152 int sceSdBlockTrans(s16 channel, u16 mode, u8 *m_addr, u32 size, ...)
153 {
154  return(-1);
155 }
156 
158 {
159  s32 buf[2] ALIGNED(64);
160  buf[0] = channel;
161  buf[1] = flag;
162 
163  SifCallRpc(&sd_client, PS2SND_VoiceTransStatus, 0, buf, 8, buf, 4, NULL, NULL);
164  return(((u32 *)buf)[0]);
165 }
166 
168 {
169  s32 buf[2] ALIGNED(64);
170  buf[0] = channel;
171  buf[1] = flag;
172 
173  SifCallRpc(&sd_client, PS2SND_BlockTransStatus, 0, buf, 8, buf, 4, NULL, NULL);
174  return(((u32 *)buf)[0]);
175 }
176 
177 //void* sceSdSetTransCallback (u16 channel, void SD_TRANS_CBProc(void *) );
178 //void *sceSdSetIRQCallback( void SD_IRQ_CBProc(void *) );
179 
180 int sceSdSetEffectAttr (int core, sceSdEffectAttr *attr)
181 {
182  s32 buf[1+((sizeof(sceSdEffectAttr)+3)/4)] ALIGNED(64);
183  buf[0] = core;
184  memcpy(&buf[1], attr, sizeof(sceSdEffectAttr));
185  SifCallRpc(&sd_client, PS2SND_SetEffectAttr, 0, buf, 4+sizeof(sceSdEffectAttr), buf, 4, NULL, NULL);
186  return(buf[0]);
187 }
188 
189 void sceSdGetEffectAttr (int core, sceSdEffectAttr *attr)
190 {
191  s32 buf[((sizeof(sceSdEffectAttr)+3)/4)] ALIGNED(64);
192  buf[0] = core;
193  SifCallRpc(&sd_client, PS2SND_GetEffectAttr, 0, buf, 4, buf, sizeof(sceSdEffectAttr), NULL, NULL);
194  memcpy(attr, buf, sizeof(sceSdEffectAttr));
195 }
196 
197 int sceSdClearEffectWorkArea (int core, int channel, int effect_mode)
198 {
199  s32 buf[3] ALIGNED(64);
200  buf[0] = core;
201  buf[1] = channel;
202  buf[2] = effect_mode;
203  SifCallRpc(&sd_client, PS2SND_ClearEffectWorkArea, 0, buf, 12, buf, 4, NULL, NULL);
204  return(buf[0]);
205 }
206 
207 
209 {
210  u32 buf[1] ALIGNED(64);
212  return(buf[0]);
213 }
214 
215 int sndStreamOpen(char *file, u32 voices, u32 flags, u32 bufaddr, u32 bufsize)
216 {
217  u32 buf[32] ALIGNED(64);
218  buf[0] = voices;
219  buf[1] = flags;
220  buf[2] = bufaddr;
221  buf[3] = bufsize;
222  strncpy((char*)&buf[4], file, 27*4);
223  buf[31] = 0;
224 
225  SifCallRpc(&sd_client, PS2SND_StreamOpen, 0, buf, 128, buf, 4, NULL, NULL);
226  return(((s32 *)buf)[0]);
227 }
228 
229 
230 int sndStreamClose(void)
231 {
232  s32 buf[1] ALIGNED(64);
233  SifCallRpc(&sd_client, PS2SND_StreamClose, 0, NULL, 0, buf, 4, NULL, NULL);
234  return(buf[0]);
235 }
236 
237 int sndStreamPlay(void)
238 {
239  s32 buf[1] ALIGNED(64);
240  SifCallRpc(&sd_client, PS2SND_StreamPlay, 0, NULL, 0, buf, 4, NULL, NULL);
241  return(buf[0]);
242 }
243 
244 
245 int sndStreamPause(void)
246 {
247  s32 buf[1] ALIGNED(64);
248  SifCallRpc(&sd_client, PS2SND_StreamPause, 0, NULL, 0, buf, 4, NULL, NULL);
249  return(buf[0]);
250 }
251 
252 int sndStreamSetPosition(int block)
253 {
254  s32 buf[1] ALIGNED(64);
255  buf[0] = block;
256  SifCallRpc(&sd_client, PS2SND_StreamSetPosition, 0, buf, 4, buf, 4, NULL, NULL);
257  return(buf[0]);
258 }
259 
260 int sndStreamSetVolume(int left, int right)
261 {
262  s32 buf[2] ALIGNED(64);
263  buf[0] = left;
264  buf[1] = right;
265  SifCallRpc(&sd_client, PS2SND_StreamSetVolume, 0, buf, 8, buf, 4, NULL, NULL);
266  return(buf[0]);
267 }
268 
270 {
271  s32 buf[1] ALIGNED(64);
273  return(buf[0]);
274 }
275 
276 int sndLoadSample(void *buf, u32 spuaddr, int size)
277 {
278  void *iopbuf;
279  int id, iopfree;
280  SifDmaTransfer_t sifdma;
281 
282  iopfree = sndQueryMaxFreeMemSize()/2;
283  if (size>iopfree)
284  {
285  return(-1);
286  }
287 
288  iopbuf = SifAllocIopHeap(size);
289  if (iopbuf==0)
290  return(-1);
291 
292  FlushCache(0);
293 
294  sifdma.src = buf;
295  sifdma.dest = iopbuf;
296  sifdma.size = size;
297  sifdma.attr = 0;
298 
299  id = SifSetDma(&sifdma, 1);
300  while(SifDmaStat(id) >= 0);;
301  FlushCache(0);
302 
303  sceSdVoiceTrans(0, SD_TRANS_WRITE, iopbuf, (void*)spuaddr, size);
304  sceSdVoiceTransStatus(0, 1);
305 
306  SifFreeIopHeap(iopbuf);
307 
308  return(size);
309 }
void * SifAllocIopHeap(int size)
int SifFreeIopHeap(void *addr)
u32 SifSetDma(SifDmaTransfer_t *sdd, s32 len)
static void nopdelay(void)
Definition: kernel.h:141
#define ALIGNED(x)
Definition: kernel.h:50
void FlushCache(s32 operation)
s32 SifDmaStat(u32 id)
#define SD_TRANS_WRITE
Definition: libsd-common.h:47
int sndLoadSample(void *buf, u32 spuaddr, int size)
Definition: ps2snd.c:276
void sceSdSetParam(u16 entry, u16 value)
Definition: ps2snd.c:50
void sceSdSetAddr(u16 entry, u32 value)
Definition: ps2snd.c:82
int sceSdSetEffectAttr(int core, sceSdEffectAttr *attr)
Definition: ps2snd.c:180
void sceSdSetSwitch(u16 entry, u32 value)
Definition: ps2snd.c:66
u32 sndQueryMaxFreeMemSize(void)
Definition: ps2snd.c:208
int sceSdProcBatchEx(sceSdBatch *batch, u32 returns[], u32 num, u32 voice)
Definition: ps2snd.c:132
int sceSdBlockTrans(s16 channel, u16 mode, u8 *m_addr, u32 size,...)
Definition: ps2snd.c:152
int sndStreamSetPosition(int block)
Definition: ps2snd.c:252
u32 sceSdGetAddr(u16 entry)
Definition: ps2snd.c:90
u32 sceSdGetSwitch(u16 entry)
Definition: ps2snd.c:74
void sceSdSetCoreAttr(u16 entry, u16 value)
Definition: ps2snd.c:98
u16 sceSdGetParam(u16 entry)
Definition: ps2snd.c:58
int sndStreamSetVolume(int left, int right)
Definition: ps2snd.c:260
u16 sceSdNote2Pitch(u16 center_note, u16 center_fine, u16 note, s16 fine)
Definition: ps2snd.c:114
int sndStreamOpen(char *file, u32 voices, u32 flags, u32 bufaddr, u32 bufsize)
Definition: ps2snd.c:215
int sceSdClearEffectWorkArea(int core, int channel, int effect_mode)
Definition: ps2snd.c:197
int sndStreamPlay(void)
Definition: ps2snd.c:237
int sceSdVoiceTrans(s16 channel, u16 mode, u8 *m_addr, u32 *s_addr, u32 size)
Definition: ps2snd.c:139
int sndStreamPause(void)
Definition: ps2snd.c:245
u32 sceSdVoiceTransStatus(s16 channel, s16 flag)
Definition: ps2snd.c:157
int sndStreamClose(void)
Definition: ps2snd.c:230
int sndStreamGetPosition(void)
Definition: ps2snd.c:269
int sceSdInit(int flag)
Definition: ps2snd.c:21
void sceSdGetEffectAttr(int core, sceSdEffectAttr *attr)
Definition: ps2snd.c:189
int sceSdProcBatch(sceSdBatch *batch, u32 returns[], u32 num)
Definition: ps2snd.c:126
static SifRpcClientData_t sd_client
Definition: ps2snd.c:18
static int sd_started
Definition: ps2snd.c:19
u16 sceSdGetCoreAttr(u16 entry)
Definition: ps2snd.c:106
u32 sceSdBlockTransStatus(s16 channel, s16 flag)
Definition: ps2snd.c:167
u16 sceSdPitch2Note(u16 center_note, u16 center_fine, u16 pitch)
Definition: ps2snd.c:120
#define PS2SND_VoiceTrans
Definition: ps2snd.h:41
#define PS2SND_ClearEffectWorkArea
Definition: ps2snd.h:49
#define PS2SND_VoiceTransStatus
Definition: ps2snd.h:43
#define PS2SND_StreamSetVolume
Definition: ps2snd.h:59
#define PS2SND_StreamPlay
Definition: ps2snd.h:55
#define PS2SND_StreamSetPosition
Definition: ps2snd.h:57
#define PS2SND_SetEffectAttr
Definition: ps2snd.h:47
#define PS2SND_StreamClose
Definition: ps2snd.h:54
#define PS2SND_GetCoreAttr
Definition: ps2snd.h:36
#define PS2SND_GetParam
Definition: ps2snd.h:30
#define PS2SND_SetCoreAttr
Definition: ps2snd.h:35
#define PS2SND_QueryMaxFreeMemSize
Definition: ps2snd.h:62
#define PS2SND_GetSwitch
Definition: ps2snd.h:32
#define PS2SND_GetEffectAttr
Definition: ps2snd.h:48
#define PS2SND_StreamPause
Definition: ps2snd.h:56
#define PS2SND_SetParam
Definition: ps2snd.h:29
#define PS2SND_GetAddr
Definition: ps2snd.h:34
#define PS2SND_SetAddr
Definition: ps2snd.h:33
#define PS2SND_StreamGetPosition
Definition: ps2snd.h:58
#define PS2SND_BlockTransStatus
Definition: ps2snd.h:44
#define BINDID_PS2SND
Definition: ps2snd.h:26
#define PS2SND_StreamOpen
Definition: ps2snd.h:53
#define PS2SND_SetSwitch
Definition: ps2snd.h:31
#define PS2SND_Init
Definition: ps2snd.h:28
s32 mode
Definition: rpc_client.c:15
int SifBindRpc(SifRpcClientData_t *client, int rpc_number, int mode)
int SifCallRpc(SifRpcClientData_t *client, int rpc_number, int mode, void *send, int ssize, void *receive, int rsize, SifRpcEndFunc_t end_function, void *end_param)
void * dest
Definition: sifdma.h:55
void * src
Definition: sifdma.h:54
struct t_SifRpcServerData * server
Definition: sifrpc.h:142
#define NULL
Definition: tamtypes.h:91
signed int s32
Definition: tamtypes.h:58
unsigned int u32
Definition: tamtypes.h:30
signed short s16
Definition: tamtypes.h:52
unsigned short u16
Definition: tamtypes.h:24
unsigned char u8
Definition: tamtypes.h:23