PS2SDK
PS2 Homebrew Libraries
Loading...
Searching...
No Matches
audsrv_rpc.c File Reference
#include <stdio.h>
#include <kernel.h>
#include <sifrpc.h>
#include <tamtypes.h>
#include <string.h>
#include <iopheap.h>
#include <audsrv.h>
#include "audsrv_rpc.h"
+ Include dependency graph for audsrv_rpc.c:

Go to the source code of this file.

Macros

#define MIN(a, b)   ((a) <= (b)) ? (a) : (b)
 

Functions

static unsigned int sbuff[4096] __attribute__ ((aligned(64)))
 
static unsigned char rpc_server_stack[0x1800] __attribute__ ((aligned(16)))
 
static void set_error (int err)
 
int audsrv_get_error ()
 
static int call_rpc_1 (int func, int arg)
 
static int call_rpc_2 (int func, int arg1, int arg2)
 
static int call_rpc_3 (int func, int arg1, int arg2, int arg3)
 
int audsrv_quit ()
 
int audsrv_set_format (struct audsrv_fmt_t *fmt)
 
int audsrv_wait_audio (int bytes)
 
int audsrv_set_volume (int volume)
 
int audsrv_play_cd (int track)
 
int audsrv_play_sectors (int start, int end)
 
int audsrv_stop_cd ()
 
int audsrv_get_cdpos ()
 
int audsrv_get_trackpos ()
 
int audsrv_get_numtracks ()
 
int audsrv_get_track_offset (int track)
 
int audsrv_pause_cd ()
 
int audsrv_resume_cd ()
 
int audsrv_get_cd_type ()
 
int audsrv_get_cd_status ()
 
int audsrv_play_audio (const char *chunk, int bytes)
 
int audsrv_stop_audio ()
 
static void * audsrv_ee_rpc_handler (int fnum, void *buffer, int len)
 
static void rpc_server_thread (void *arg)
 
int audsrv_init ()
 
int audsrv_adpcm_init ()
 
int audsrv_adpcm_set_volume_and_pan (int ch, int volume, int pan)
 
int audsrv_load_adpcm (audsrv_adpcm_t *adpcm, void *buffer, int size)
 
int audsrv_ch_play_adpcm (int ch, audsrv_adpcm_t *adpcm)
 
int audsrv_is_adpcm_playing (int ch, audsrv_adpcm_t *adpcm)
 
int audsrv_free_adpcm (audsrv_adpcm_t *adpcm)
 
const char * audsrv_get_error_string ()
 
int audsrv_on_cdda_stop (audsrv_callback_t cb, void *arg)
 
int audsrv_on_fillbuf (int amount, audsrv_callback_t cb, void *arg)
 
int audsrv_available ()
 
int audsrv_queued ()
 

Variables

static struct t_SifRpcClientData cd0
 
static struct t_SifRpcDataQueue cb_queue
 
static struct t_SifRpcServerData cb_srv
 
void * _gp
 
static int initialized = 0
 
static int rpc_server_thread_id
 
static int audsrv_error = AUDSRV_ERR_NOERROR
 
static int completion_sema
 
static audsrv_callback_t on_cdda_stop = NULL
 
static void * on_cdda_stop_arg = NULL
 
static audsrv_callback_t on_fillbuf = NULL
 
static void * on_fillbuf_arg = NULL
 
static const unsigned short vol_values [26]
 

Detailed Description

audsrv EE-side RPC code.

Definition in file audsrv_rpc.c.

Macro Definition Documentation

◆ MIN

#define MIN (   a,
 
)    ((a) <= (b)) ? (a) : (b)

Definition at line 26 of file audsrv_rpc.c.

Function Documentation

◆ set_error()

static void set_error ( int  err)
static

Internal function to set last error

Parameters
err

Definition at line 61 of file audsrv_rpc.c.

Referenced by audsrv_init(), audsrv_play_audio(), audsrv_quit(), audsrv_set_format(), call_rpc_1(), call_rpc_2(), and call_rpc_3().

◆ audsrv_get_error()

int audsrv_get_error ( )

Returns the last error audsrv raised

Returns
error code

Definition at line 66 of file audsrv_rpc.c.

Referenced by audsrv_get_error_string().

◆ call_rpc_1()

static int call_rpc_1 ( int  func,
int  arg 
)
static

◆ call_rpc_2()

static int call_rpc_2 ( int  func,
int  arg1,
int  arg2 
)
static

Internal function to simplify RPC calling

Parameters
funcprocedure to invoke
arg1optional argument
arg2optional argument
Returns
value returned by RPC server

Definition at line 99 of file audsrv_rpc.c.

References set_error().

Referenced by audsrv_ch_play_adpcm(), audsrv_is_adpcm_playing(), and audsrv_play_sectors().

◆ call_rpc_3()

static int call_rpc_3 ( int  func,
int  arg1,
int  arg2,
int  arg3 
)
static

Internal function to simplify RPC calling

Parameters
funcprocedure to invoke
arg1optional argument
arg2optional argument
arg3optional argument
Returns
value returned by RPC server

Definition at line 124 of file audsrv_rpc.c.

References set_error().

Referenced by audsrv_adpcm_set_volume_and_pan().

◆ audsrv_quit()

int audsrv_quit ( )

Shutdowns audsrv

Returns
AUDSRV_ERR_NOERROR

Definition at line 143 of file audsrv_rpc.c.

◆ audsrv_set_format()

int audsrv_set_format ( struct audsrv_fmt_t fmt)

Configures audio stream

Parameters
fmtoutput specification structure
Returns
0 on success, or one of the error codes otherwise

This sets up audsrv to accept stream in this format and convert it to SPU2's native format if required. Note: it is possible to change the format at any point. You might want to stop audio prior to that, to prevent mismatched audio output.

Definition at line 159 of file audsrv_rpc.c.

References audsrv_fmt_t::bits, audsrv_fmt_t::channels, audsrv_fmt_t::freq, and set_error().

Referenced by rpc_command().

◆ audsrv_wait_audio()

int audsrv_wait_audio ( int  bytes)

Blocks until there is enough space to enqueue chunk

Parameters
bytessize of chunk requested to be enqueued (in bytes)
Returns
error code

Blocks until there are enough space to store the upcoming chunk in audsrv's internal ring buffer.

Definition at line 178 of file audsrv_rpc.c.

◆ audsrv_set_volume()

int audsrv_set_volume ( int  volume)

Sets output volume

Parameters
volvolume in percentage
Returns
error code

Definition at line 183 of file audsrv_rpc.c.

◆ audsrv_play_cd()

int audsrv_play_cd ( int  track)

Starts playing the request track

Parameters
tracksegment to play
Returns
status code

Definition at line 197 of file audsrv_rpc.c.

◆ audsrv_play_sectors()

int audsrv_play_sectors ( int  start,
int  end 
)

Starts playing at a specific sector

Parameters
startfirst sector to play
endlast sector to play
Returns
status code

Definition at line 202 of file audsrv_rpc.c.

References call_rpc_2().

◆ audsrv_stop_cd()

int audsrv_stop_cd ( )

Stops CD from playing.

Returns
status code

Definition at line 207 of file audsrv_rpc.c.

◆ audsrv_get_cdpos()

int audsrv_get_cdpos ( )

Returns the current playing sector

Returns
sector number

CDDA type discs have sector size of 2352 bytes. There are 75 such sectors per second.

Definition at line 214 of file audsrv_rpc.c.

◆ audsrv_get_trackpos()

int audsrv_get_trackpos ( )

Returns the current playing sector, relative to track

Returns
sector number

There are 75 sectors a second. To translate this position to mm:ss:ff use the following: mm = sector / (75*60) ss = (sector / 75) % 60 ff = sector % 75

where ff is the frame number, 1/75th of a second.

Definition at line 219 of file audsrv_rpc.c.

◆ audsrv_get_numtracks()

int audsrv_get_numtracks ( )

Returns the number of tracks available on the CD in tray

Returns
positive track count, or negative error status code

Definition at line 224 of file audsrv_rpc.c.

◆ audsrv_get_track_offset()

int audsrv_get_track_offset ( int  track)

Returns the first sector for the given track

Parameters
tracktrack index, must be between 1 and the trackcount
Returns
sector number, or negative status code

Definition at line 229 of file audsrv_rpc.c.

◆ audsrv_pause_cd()

int audsrv_pause_cd ( )

Pauses CDDA playing

Returns
error status code

If CDDA is paused, no operation is taken

Definition at line 234 of file audsrv_rpc.c.

References call_rpc_1().

◆ audsrv_resume_cd()

int audsrv_resume_cd ( )

Resumes CDDA playing

Returns
error status code

If CDDA was not paused, no operation is taken

Definition at line 239 of file audsrv_rpc.c.

References call_rpc_1().

◆ audsrv_get_cd_type()

int audsrv_get_cd_type ( )

Returns the type of disc currently in tray

Returns
value as defined in libcdvd, negative on error

Definition at line 244 of file audsrv_rpc.c.

◆ audsrv_get_cd_status()

int audsrv_get_cd_status ( )

Returns the status of the CD tray (open, closed, seeking etc.)

Returns
value as defined in libcdvd, negative on error

Definition at line 249 of file audsrv_rpc.c.

◆ audsrv_play_audio()

int audsrv_play_audio ( const char *  chunk,
int  bytes 
)

Uploads audio buffer to SPU

Parameters
chunkaudio buffer
bytessize of chunk in bytes
Returns
positive number of bytes sent to processor or negative error status

Plays an audio buffer; It will not interrupt a playing buffer, rather queue it up and play it as soon as possible without interfering with fluent streaming. The buffer and buflen are given in host format (i.e, 11025hz 8bit stereo.)

Definition at line 254 of file audsrv_rpc.c.

◆ audsrv_stop_audio()

int audsrv_stop_audio ( )

Stops audio from playing.

Returns
status code

Definition at line 292 of file audsrv_rpc.c.

◆ audsrv_ee_rpc_handler()

static void * audsrv_ee_rpc_handler ( int  fnum,
void *  buffer,
int  len 
)
static

Definition at line 299 of file audsrv_rpc.c.

◆ rpc_server_thread()

static void rpc_server_thread ( void *  arg)
static

Definition at line 317 of file audsrv_rpc.c.

◆ audsrv_init()

int audsrv_init ( )

Initializes audsrv library

Returns
error code

Definition at line 328 of file audsrv_rpc.c.

◆ audsrv_adpcm_init()

int audsrv_adpcm_init ( )

Initializes adpcm unit of audsrv

Returns
zero on success, negative value on error

Frees up all memory taken by samples, and stops all voices from being played. This can be called multiple times

Definition at line 394 of file audsrv_rpc.c.

◆ audsrv_adpcm_set_volume_and_pan()

int audsrv_adpcm_set_volume_and_pan ( int  ch,
int  vol,
int  pan 
)

Sets output volume for the specified voice channel.

Parameters
chVoice channel ID
volvolume in percentage (0-100)
panleft/right offset [-100 .. 0 .. 100]
Returns
0 on success, negative otherwise

Definition at line 399 of file audsrv_rpc.c.

References call_rpc_3(), MAX_VOLUME, and MIN_VOLUME.

◆ audsrv_load_adpcm()

int audsrv_load_adpcm ( audsrv_adpcm_t adpcm,
void *  buffer,
int  size 
)

Uploads a sample to SPU2 memory

Parameters
adpcmadpcm descriptor structure
bufferpointer to adpcm sample
sizesize of sample (including the header)
Returns
zero on success, negative error code otherwise

Definition at line 430 of file audsrv_rpc.c.

References AUDSRV_ERR_NOERROR.

Referenced by rpc_command().

◆ audsrv_ch_play_adpcm()

int audsrv_ch_play_adpcm ( int  ch,
audsrv_adpcm_t adpcm 
)

Plays an adpcm sample already uploaded with audsrv_load_adpcm()

Parameters
chchannel identifier. Specifies one of the 24 voice channel to play the ADPCM channel on.
idsample identifier, as specified in load()
Returns
channel identifier on success, negative value on error

When ch is set to an invalid channel ID, the sample will be played in an unoccupied channel. If all 24 channels are used, then -AUDSRV_ERR_NO_MORE_CHANNELS is returned. When ch is set to a valid channel ID, -AUDSRV_ERR_NO_MORE_CHANNELS is returned if the channel is currently in use. Trying to play a sample which is unavailable will result in -AUDSRV_ERR_ARGS

Definition at line 481 of file audsrv_rpc.c.

References call_rpc_2().

Referenced by rpc_command().

◆ audsrv_is_adpcm_playing()

int audsrv_is_adpcm_playing ( int  ch,
audsrv_adpcm_t adpcm 
)

Check if a sample is currently playing on the given channel

Returns
1 if playing, 0 if not

Definition at line 487 of file audsrv_rpc.c.

References call_rpc_2().

Referenced by rpc_command().

◆ audsrv_free_adpcm()

int audsrv_free_adpcm ( audsrv_adpcm_t adpcm)

Remove an adpcm sample uploaded with audsrv_load_adpcm() from the list of loaded sounds

Parameters
idsample identifier, as specified in load()

SPU memory is freed only when there are no sounds in the list that where loaded after the ones that have been freed

Definition at line 492 of file audsrv_rpc.c.

References call_rpc_1().

◆ audsrv_get_error_string()

const char * audsrv_get_error_string ( )

Translates audsrv_get_error() response to readable string

Returns
string representation of error code

Definition at line 498 of file audsrv_rpc.c.

References AUDSRV_ERR_NOERROR, and audsrv_get_error().

◆ audsrv_on_cdda_stop()

int audsrv_on_cdda_stop ( audsrv_callback_t  cb,
void *  arg 
)

Installs a callback function upon completion of a cdda track

Parameters
cbyour callback
argextra parameter to pass to callback function later
Returns
status code

Definition at line 524 of file audsrv_rpc.c.

References AUDSRV_ERR_NOERROR.

◆ audsrv_on_fillbuf()

int audsrv_on_fillbuf ( int  amount,
audsrv_callback_t  cb,
void *  arg 
)

Installs a callback function to be called when ringbuffer has enough space to transmit the request number of bytes.

Parameters
bytesrequest a callback when this amount of bytes is available
cbyour callback
argextra parameter to pass to callback function later
Returns
AUDSRV_ERR_NOERROR, AUDSRV_ERR_ARGS if amount is greater than sizeof(ringbuf)

Definition at line 531 of file audsrv_rpc.c.

References AUDSRV_ERR_NOERROR, and call_rpc_1().

◆ audsrv_available()

int audsrv_available ( )

Returns the number of bytes that can be queued

Returns
number of bytes left in the ring buffer

Returns the number of bytes that are available in the ring buffer. This is the total bytes that can be queued, without collision of the reading head with the writing head.

Definition at line 549 of file audsrv_rpc.c.

◆ audsrv_queued()

int audsrv_queued ( )

Returns the number of bytes already in queue

Returns
byte count

Returns the number of bytes that are already in the ring buffer.

Definition at line 554 of file audsrv_rpc.c.

Variable Documentation

◆ cd0

struct t_SifRpcClientData cd0
static

Definition at line 29 of file audsrv_rpc.c.

◆ cb_queue

struct t_SifRpcDataQueue cb_queue
static

Definition at line 31 of file audsrv_rpc.c.

◆ cb_srv

struct t_SifRpcServerData cb_srv
static

Definition at line 32 of file audsrv_rpc.c.

◆ initialized

int initialized = 0
static

Definition at line 37 of file audsrv_rpc.c.

◆ rpc_server_thread_id

int rpc_server_thread_id
static

Definition at line 38 of file audsrv_rpc.c.

◆ audsrv_error

int audsrv_error = AUDSRV_ERR_NOERROR
static

Definition at line 39 of file audsrv_rpc.c.

◆ completion_sema

int completion_sema
static

Definition at line 40 of file audsrv_rpc.c.

◆ on_cdda_stop

audsrv_callback_t on_cdda_stop = NULL
static

Definition at line 42 of file audsrv_rpc.c.

◆ on_cdda_stop_arg

void* on_cdda_stop_arg = NULL
static

Definition at line 43 of file audsrv_rpc.c.

◆ on_fillbuf

audsrv_callback_t on_fillbuf = NULL
static

Definition at line 45 of file audsrv_rpc.c.

◆ on_fillbuf_arg

void* on_fillbuf_arg = NULL
static

Definition at line 46 of file audsrv_rpc.c.

◆ vol_values

const unsigned short vol_values[26]
static
Initial value:
=
{
0x0000,
0x0000, 0x0096, 0x0190, 0x0230, 0x0320,
0x042E, 0x0532, 0x05FA, 0x06C2, 0x088E,
0x09F6, 0x0BC2, 0x0DC0, 0x0FF0, 0x118A,
0x1482, 0x1752, 0x1B4E, 0x1F40, 0x2378,
0x28D2, 0x2EFE, 0x34F8, 0x3A5C, 0x3FFF
}

Definition at line 48 of file audsrv_rpc.c.