ps2sdk  1.1
A collection of Open Source libraries used for developing applications on Sony's PlayStation 2® (PS2).
playwav2.c
Go to the documentation of this file.
1 /*
2 # _____ ___ ____ ___ ____
3 # ____| | ____| | | |____|
4 # | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
5 #-----------------------------------------------------------------------
6 # Copyright 2005, ps2dev - http://www.ps2dev.org
7 # Licenced under GNU Library General Public License version 2
8 # Review ps2sdk README & LICENSE files for further details.
9 #
10 # audsrv sample using callbacks
11 */
12 
13 #include <stdio.h>
14 #include <string.h>
15 
16 #include <kernel.h>
17 #include <sifrpc.h>
18 #include <loadfile.h>
19 #include <tamtypes.h>
20 
21 #include <audsrv.h>
22 
23 static int fillbuffer(void *arg)
24 {
25  iSignalSema((int)arg);
26  return 0;
27 }
28 
29 int main(int argc, char **argv)
30 {
31  int ret;
32  int played;
33  int err;
34  int bytes;
35  char chunk[2048];
36  FILE *wav;
37  ee_sema_t sema;
38  int fillbuffer_sema;
39  struct audsrv_fmt_t format;
40 
41  SifInitRpc(0);
42 
43  printf("sample: kicking IRXs\n");
44  ret = SifLoadModule("rom0:LIBSD", 0, NULL);
45  printf("libsd loadmodule %d\n", ret);
46 
47  printf("sample: loading audsrv\n");
48  ret = SifLoadModule("host:audsrv.irx", 0, NULL);
49  printf("audsrv loadmodule %d\n", ret);
50 
51  ret = audsrv_init();
52  if (ret != 0)
53  {
54  printf("sample: failed to initialize audsrv\n");
55  printf("audsrv returned error string: %s\n", audsrv_get_error_string());
56  return 1;
57  }
58 
59  format.bits = 16;
60  format.freq = 22050;
61  format.channels = 2;
62  err = audsrv_set_format(&format);
63  printf("set format returned %d\n", err);
64  printf("audsrv returned error string: %s\n", audsrv_get_error_string());
65 
67 
68  sema.init_count = 0;
69  sema.max_count = 1;
70  sema.option = 0;
71  fillbuffer_sema = CreateSema(&sema);
72 
73  err = audsrv_on_fillbuf(sizeof(chunk), fillbuffer, (void *)fillbuffer_sema);
74  if (err != AUDSRV_ERR_NOERROR)
75  {
76  printf("audsrv_on_fillbuf failed with err=%d\n", err);
77  goto loser;
78  }
79 
80  wav = fopen("host:song_22k.wav", "rb");
81  if (wav == NULL)
82  {
83  printf("failed to open wav file\n");
84  audsrv_quit();
85  return 1;
86  }
87 
88  fseek(wav, 0x30, SEEK_SET);
89 
90  printf("starting play loop\n");
91  played = 0;
92  bytes = 0;
93  while (1)
94  {
95  ret = fread(chunk, 1, sizeof(chunk), wav);
96  if (ret > 0)
97  {
98  WaitSema(fillbuffer_sema);
99  audsrv_play_audio(chunk, ret);
100  }
101 
102  if (ret < sizeof(chunk))
103  {
104  /* no more data */
105  break;
106  }
107 
108  played++;
109  bytes = bytes + ret;
110 
111  if (played % 8 == 0)
112  {
113  printf("\r%d bytes sent..", bytes);
114  }
115 
116  if (played == 512) break;
117  }
118 
119  fclose(wav);
120 
121 loser:
122  printf("sample: stopping audsrv\n");
123  audsrv_quit();
124 
125  printf("sample: ended\n");
126  return 0;
127 }
const char * audsrv_get_error_string()
Definition: audsrv_rpc.c:438
int audsrv_init()
Definition: audsrv_rpc.c:296
#define AUDSRV_ERR_NOERROR
Definition: audsrv.h:28
int audsrv_play_audio(const char *chunk, int bytes)
Definition: audsrv_rpc.c:228
#define MAX_VOLUME
Definition: audsrv.h:25
int audsrv_set_volume(int volume)
Definition: audsrv_rpc.c:157
int audsrv_on_fillbuf(int amount, audsrv_callback_t cb, void *arg)
Definition: audsrv_rpc.c:471
int audsrv_set_format(struct audsrv_fmt_t *fmt)
Definition: audsrv_rpc.c:133
int audsrv_quit()
Definition: audsrv_rpc.c:117
int SifLoadModule(const char *path, int arg_len, const char *args)
s32 CreateSema(ee_sema_t *sema)
s32 iSignalSema(s32 sema_id)
s32 WaitSema(s32 sema_id)
static int fillbuffer(void *arg)
Definition: playwav2.c:23
int main(int argc, char **argv)
Definition: playwav2.c:29
void SifInitRpc(int mode)
int bits
Definition: audsrv.h:46
int freq
Definition: audsrv.h:44
int channels
Definition: audsrv.h:48
int init_count
Definition: kernel.h:218
int max_count
Definition: kernel.h:217
u32 option
Definition: kernel.h:221
#define NULL
Definition: tamtypes.h:91