PS2SDK
PS2 Homebrew Libraries
Loading...
Searching...
No Matches
audsrv.c File Reference
#include <stdio.h>
#include <thbase.h>
#include <thsemap.h>
#include <loadcore.h>
#include <sysmem.h>
#include <intrman.h>
#include <sifcmd.h>
#include <libsd.h>
#include <sysclib.h>
#include <audsrv.h>
#include "audsrv_internal.h"
#include "common.h"
#include "rpc_server.h"
#include "rpc_client.h"
#include "upsamplers.h"
#include "hw.h"
#include "spu.h"
#include "debug_printf.h"
+ Include dependency graph for audsrv.c:

Go to the source code of this file.

Macros

#define MODNAME   "audsrv"
 
#define VERSION   "0.93"
 

Functions

 IRX_ID (MODNAME, 1, 4)
 
static u8 core1_buf[0x1000] __attribute__ ((aligned(16)))
 
static void play_thread (void *arg)
 
static int transfer_complete (void *arg)
 
static void update_volume ()
 
int audsrv_stop_audio ()
 
int audsrv_format_ok (int freq, int bits, int channels)
 
int audsrv_set_format (int freq, int bits, int channels)
 
int audsrv_init ()
 
int audsrv_available ()
 
int audsrv_queued ()
 
int audsrv_wait_audio (int buflen)
 
int audsrv_play_audio (const char *buf, int buflen)
 
int audsrv_set_volume (int vol)
 
int audsrv_set_threshold (int amount)
 
int audsrv_quit ()
 
int _start (int argc, char *argv[])
 

Variables

static int core1_volume = MAX_VOLUME
 
static int core1_freq = 0
 
static int core1_bits = 0
 
static int core1_channels = 0
 
static int core1_sample_shift = 0
 
static int initialized = 0
 
static int playing = 0
 
static char ringbuf [20480]
 
static int ringbuf_size = sizeof(ringbuf)
 
static int readpos
 
static int writepos
 
static int play_tid = 0
 
static int queue_sema = 0
 
static int transfer_sema = 0
 
static int fillbuf_threshold = 0
 
static int format_changed = 0
 
static short rendered_left [512]
 
static short rendered_right [512]
 
struct irx_export_table _exp_audsrv
 

Detailed Description

audsrv IOP server

Definition in file audsrv.c.

Macro Definition Documentation

◆ MODNAME

#define MODNAME   "audsrv"

Definition at line 35 of file audsrv.c.

◆ VERSION

#define VERSION   "0.93"

Definition at line 36 of file audsrv.c.

Function Documentation

◆ __attribute__()

static u8 core1_buf[0x1000] __attribute__ ( (aligned(16))  )
static

double buffer for streaming

◆ play_thread()

static void play_thread ( void *  arg)
static

Main playing thread

Parameters
argnot used

This is the main playing thread. It feeds the SPU with upsampled, demux'd audio data, from what has been queued beforehand. The stream is constructed as a ring buffer. This thread only ends with TerminateThread, and is usually asleep, waiting for SPU to complete playing the current wave. SPU plays 2048 bytes blocks, which yields that this thread wakes and sleeps 93.75 times a second

Definition at line 429 of file audsrv.c.

References audsrv_available(), core1_bits, core1_channels, core1_freq, CpuResumeIntr(), CpuSuspendIntr(), fillbuf_threshold, find_upsampler(), format_changed, playing, queue_sema, readpos, ringbuf, ringbuf_size, and transfer_sema.

Referenced by audsrv_init().

◆ transfer_complete()

static int transfer_complete ( void *  arg)
static

Transfer complete callback.

Parameters
argnot used
Returns
true, always

Generated by SPU2, when a block was transmitted and now putting to upload a second block to the other buffer.

Definition at line 99 of file audsrv.c.

References transfer_sema.

Referenced by audsrv_init().

◆ update_volume()

static void update_volume ( )
static

Apply volume changes, or keep mute if not playing

Definition at line 108 of file audsrv.c.

References core1_volume, MAX_VOLUME, and playing.

Referenced by audsrv_play_audio(), audsrv_set_volume(), and audsrv_stop_audio().

◆ audsrv_stop_audio()

int audsrv_stop_audio ( )

Stops all audio playing

Returns
0, always

Mutes output and stops accepting audio blocks; also, clears callbacks.

Definition at line 139 of file audsrv.c.

References AUDSRV_ERR_NOERROR, call_rpc_1(), fillbuf_threshold, playing, and update_volume().

Referenced by audsrv_init(), audsrv_quit(), and rpc_command().

◆ audsrv_format_ok()

int audsrv_format_ok ( int  freq,
int  bits,
int  channels 
)

Checks if the format noted by frequency and depth is supported

Parameters
freqfrequency in hz
bitsbits per sample (8, 16)
channelschannels
Returns
positive on success, zero value on failure

Definition at line 156 of file audsrv.c.

References find_upsampler().

Referenced by audsrv_set_format(), and rpc_command().

◆ audsrv_set_format()

int audsrv_set_format ( int  freq,
int  bits,
int  channels 
)

Configures audio stream

Parameters
freqfrequency in hz
bitsbits per sample (8, 16)
channelsnumber of channels
Returns
0 on success, negative if configuration is not supported

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 178 of file audsrv.c.

References AUDSRV_ERR_NOERROR, audsrv_format_ok(), core1_bits, core1_channels, core1_freq, core1_sample_shift, format_changed, readpos, ringbuf_size, and writepos.

Referenced by audsrv_init().

◆ audsrv_init()

int audsrv_init ( )

◆ audsrv_available()

int audsrv_available ( )

Returns the number of bytes that can be queued

Returns
byte count

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 278 of file audsrv.c.

References call_rpc_1(), readpos, ringbuf_size, and writepos.

Referenced by audsrv_play_audio(), audsrv_wait_audio(), play_thread(), and rpc_command().

◆ 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 295 of file audsrv.c.

References call_rpc_1(), readpos, ringbuf_size, and writepos.

Referenced by rpc_command().

◆ audsrv_wait_audio()

int audsrv_wait_audio ( int  buflen)

Blocks until there is enough space to enqueue chunk

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

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

Definition at line 314 of file audsrv.c.

References audsrv_available(), AUDSRV_ERR_NOERROR, call_rpc_1(), queue_sema, and ringbuf_size.

Referenced by rpc_command().

◆ audsrv_play_audio()

int audsrv_play_audio ( const char *  buf,
int  buflen 
)

Uploads audio buffer to SPU

Parameters
bufaudio chunk
buflensize 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 344 of file audsrv.c.

References audsrv_available(), AUDSRV_ERR_NOERROR, initialized, playing, readpos, ringbuf, ringbuf_size, set_error(), update_volume(), and writepos.

Referenced by rpc_command().

◆ audsrv_set_volume()

int audsrv_set_volume ( int  vol)

Sets output volume

Parameters
volvolume in SPU2 units [0 .. 0x3fff]
Returns
0 on success, negative otherwise

Definition at line 393 of file audsrv.c.

References AUDSRV_ERR_NOERROR, call_rpc_1(), core1_volume, MAX_VOLUME, MIN_VOLUME, and update_volume().

Referenced by rpc_command().

◆ audsrv_set_threshold()

int audsrv_set_threshold ( int  amount)

Definition at line 406 of file audsrv.c.

◆ audsrv_quit()

int audsrv_quit ( )

Shutdowns audsrv

Returns
AUDSRV_ERR_NOERROR

Definition at line 512 of file audsrv.c.

References audsrv_adpcm_init(), AUDSRV_ERR_NOERROR, audsrv_stop_audio(), audsrv_stop_cd(), play_tid, queue_sema, set_error(), and transfer_sema.

Referenced by rpc_command().

◆ _start()

int _start ( int  argc,
char *  argv[] 
)

IRX _start function

Returns
zero on success

Initializes interrupts and the RPC thread. Oh, and greets the user.

Definition at line 556 of file audsrv.c.

References _exp_audsrv, audsrv_adpcm_init(), and CpuEnableIntr().

Variable Documentation

◆ core1_volume

int core1_volume = MAX_VOLUME
static

core1 (sfx) volume

Definition at line 41 of file audsrv.c.

Referenced by audsrv_set_volume(), and update_volume().

◆ core1_freq

int core1_freq = 0
static

frequency set by user

Definition at line 43 of file audsrv.c.

Referenced by audsrv_set_format(), and play_thread().

◆ core1_bits

int core1_bits = 0
static

bits per sample, set by user

Definition at line 45 of file audsrv.c.

Referenced by audsrv_set_format(), and play_thread().

◆ core1_channels

int core1_channels = 0
static

number of audio channels

Definition at line 47 of file audsrv.c.

Referenced by audsrv_set_format(), and play_thread().

◆ core1_sample_shift

int core1_sample_shift = 0
static

shift count from bytes to samples

Definition at line 49 of file audsrv.c.

Referenced by audsrv_set_format().

◆ initialized

int initialized = 0
static

initialization status

Definition at line 53 of file audsrv.c.

Referenced by audsrv_init(), and audsrv_play_audio().

◆ playing

int playing = 0
static

playing (not mute) status

Definition at line 55 of file audsrv.c.

Referenced by audsrv_play_audio(), audsrv_stop_audio(), play_thread(), and update_volume().

◆ ringbuf

char ringbuf[20480]
static

ring buffer itself

Definition at line 60 of file audsrv.c.

Referenced by audsrv_play_audio(), and play_thread().

◆ ringbuf_size

int ringbuf_size = sizeof(ringbuf)
static

size of ring buffer in bytes

Definition at line 62 of file audsrv.c.

Referenced by audsrv_available(), audsrv_play_audio(), audsrv_queued(), audsrv_set_format(), audsrv_wait_audio(), and play_thread().

◆ readpos

int readpos
static

reading head pointer

Definition at line 64 of file audsrv.c.

Referenced by audsrv_available(), audsrv_init(), audsrv_play_audio(), audsrv_queued(), audsrv_set_format(), and play_thread().

◆ writepos

int writepos
static

writing head pointer

Definition at line 66 of file audsrv.c.

Referenced by audsrv_available(), audsrv_init(), audsrv_play_audio(), audsrv_queued(), and audsrv_set_format().

◆ play_tid

int play_tid = 0
static

playing thread id

Definition at line 69 of file audsrv.c.

Referenced by audsrv_init(), and audsrv_quit().

◆ queue_sema

int queue_sema = 0
static

semaphore for wait_audio()

Definition at line 71 of file audsrv.c.

Referenced by audsrv_init(), audsrv_quit(), audsrv_wait_audio(), and play_thread().

◆ transfer_sema

int transfer_sema = 0
static

SPU2 transfer complete semaphore

Definition at line 73 of file audsrv.c.

Referenced by audsrv_init(), audsrv_quit(), play_thread(), and transfer_complete().

◆ fillbuf_threshold

int fillbuf_threshold = 0
static

threshold to initiate a callback

Definition at line 75 of file audsrv.c.

Referenced by audsrv_stop_audio(), and play_thread().

◆ format_changed

int format_changed = 0
static

boolean to notify when format has changed

Definition at line 78 of file audsrv.c.

Referenced by audsrv_set_format(), and play_thread().

◆ rendered_left

short rendered_left[512]
static

Definition at line 83 of file audsrv.c.

◆ rendered_right

short rendered_right[512]
static

Definition at line 84 of file audsrv.c.

◆ _exp_audsrv

struct irx_export_table _exp_audsrv
extern

exports table

Referenced by _start().