audio help

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
sexdwarf
Posts: 34
Joined: Thu Jul 14, 2005 12:07 am

audio help

Post by sexdwarf »

I've modified the audio sample in svn (polyphonic example) to load one a wav file and play at the start of my program. So far it works great but I am having two issues:

1) What is the size limit? I can load a 2.5 mb wav file but 3mb and above fail.

2) I attempted to turn looping on on one of my wav files but this does not seem to work:

Code: Select all

wavout_wavinfo_t *wav_intro;

/* main routine */
int main(int argc, char *argv[])
{
...
    wavInit();
    wav_intro = wavLoad("intro.wav");
    wav_intro->playloop = 1;
    wavPlay(wav_intro);
I even attempted to hardcode playloop to 1 within wave.h
Am I wrong in assuming this is not implemented yet?

Thanks in advance...[/code]
...isn't it nice, sugar and spice...
...luring disco dollies to a life of vice...
Shine
Posts: 728
Joined: Fri Dec 03, 2004 12:10 pm
Location: Germany

Re: audio help

Post by Shine »

The polyphonic example doesn't have wavInit(), wavLoad() and wavPlay(), but looks like some useful functions. Can you post the source of it? This might help discovering the bug, too :-)
sexdwarf
Posts: 34
Joined: Thu Jul 14, 2005 12:07 am

Re: audio help

Post by sexdwarf »

Shine wrote:The polyphonic example doesn't have wavInit(), wavLoad() and wavPlay(), but looks like some useful functions. Can you post the source of it? This might help discovering the bug, too :-)
Really? I could've sworn that's where I got it from... :p I need more sleep!

OK, basically it's two files; wave.c and wave.h - both seem to be derived from the pre-SDK sound.c however I am noticing that not everything is implemented (looping is a good example).

wave.c:

Code: Select all


#include <pspkernel.h>
#include <pspdebug.h>
#include <pspaudiolib.h>
#include <pspaudio.h>
#include <pspdisplay.h>
#include <pspctrl.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <limits.h>
#include "wave.h"
#define printf	pspDebugScreenPrintf

extern 	char	*g_boot_path;

#define SND_MAXSLOT 16

wavout_wavinfo_t wavout_snd_wavinfo&#91;SND_MAXSLOT&#93;;
int wavout_snd_playing&#91;SND_MAXSLOT&#93;;

short	*samples;
unsigned long req;

static void wavout_snd_callback&#40;short *_buf, unsigned long _reqn&#41;
&#123;
	int i,slot;
	wavout_wavinfo_t *wi;
	unsigned long ptr,frac;
	short *buf=_buf;

	samples = _buf;
	req = _reqn;

	for &#40;slot=0; slot<SND_MAXSLOT; slot++&#41;
	&#123;
		pspDebugScreenSetXY&#40;0,3+slot&#41;;
		if &#40;wavout_snd_playing&#91;slot&#93;&#41;
			printf&#40;"%02d active  ",slot&#41;;
		else
			printf&#40;"%02d inactive",slot&#41;;
	&#125;

	for &#40;i=0; i<_reqn; i++&#41;
	&#123;
		int outr=0,outl=0;

		for &#40;slot=0; slot<SND_MAXSLOT; slot++&#41;
		&#123;
			if &#40;!wavout_snd_playing&#91;slot&#93;&#41; continue;


			wi=&wavout_snd_wavinfo&#91;slot&#93;;
			frac=wi->playptr_frac+wi->rateratio;
			wi->playptr=ptr=wi->playptr+&#40;frac>>16&#41;;
			wi->playptr_frac=&#40;frac&0xffff&#41;;

			if &#40;ptr>=wi->samplecount&#41;
			&#123;
				wavout_snd_playing&#91;slot&#93;=0;
				break;
			&#125;
			short *src=&#40;short *&#41;wi->wavdata;
			if &#40;wi->channels==1&#41;
			&#123;
				outl+=src&#91;ptr&#93;;
				outr+=src&#91;ptr&#93;;
			&#125;
			else
			&#123;
				outl+=src&#91;ptr*2&#93;;
				outr+=src&#91;ptr*2+1&#93;;
			&#125;
		&#125;
		if &#40;outl<-32768&#41;
			outl=-32768;
		else if &#40;outl>32767&#41;
			outl=32767;
		if &#40;outr<-32768&#41;
			outr=-32768;
		else if &#40;outr>32767&#41;
			outr=32767;

		*&#40;buf++&#41;=outl;
		*&#40;buf++&#41;=outr;
	&#125;
&#125;

void wavHalt&#40;char *string,char *name&#41;
&#123;
	pspDebugScreenInit&#40;&#41;;
	pspDebugScreenSetBackColor&#40;0x00FF0000&#41;;
	pspDebugScreenSetTextColor&#40;0xFFFFFFFF&#41;;
	pspDebugScreenClear&#40;&#41;;
	printf&#40;"I regret to inform you file %s\nfailed with error %s\n",name,string&#41;;
&#125;

int wavInit&#40;&#41;
&#123;
	int i;
	pspAudioInit&#40;&#41;;
	pspAudioSetChannelCallback&#40;0, wavout_snd_callback&#41;;

	for &#40;i=0; i<SND_MAXSLOT; i++&#41;
	&#123;
		wavout_snd_playing&#91;i&#93;=0;
	&#125;
	return 0;
&#125;


//stop all
void wavoutStopPlay&#40;&#41;
&#123;
	int i;
	for &#40;i=0; i<SND_MAXSLOT; i++&#41;
		wavout_snd_playing&#91;i&#93;=0;
&#125;

//return 0 if success
int wavPlay&#40;wavout_wavinfo_t *wi&#41;
&#123;
	int i;
	wavout_wavinfo_t *wid;
	for &#40;i=0; i<SND_MAXSLOT; i++&#41; if &#40;wavout_snd_playing&#91;i&#93;==0&#41; break;
	if &#40;i==SND_MAXSLOT&#41; return -1;
	wid=&wavout_snd_wavinfo&#91;i&#93;;
	wid->channels=wi->channels;
	wid->samplerate=wi->samplerate;
	wid->samplecount=wi->samplecount;
	wid->datalength=wi->datalength;
	wid->wavdata=wi->wavdata;
	wid->rateratio=wi->rateratio;
	wid->playptr=0;
	wid->playptr_frac=0;
	wid->playloop=0;
	wavout_snd_playing&#91;i&#93;=1;
	return 0;
&#125;

//return waveinfo * if success

wavout_wavinfo_t *wavLoad&#40;char *filename&#41;
&#123;
	unsigned int filelen;
	int fd;
	unsigned long channels;
	unsigned long samplerate;
	unsigned long blocksize;
	unsigned long bitpersample;
	unsigned long datalength;
	unsigned long samplecount;

	char 			*wavfile;
	char			path_name&#91;256&#93;;

	wavout_wavinfo_t *wi;

	SceIoStat stat;

	sprintf&#40;path_name,"%s%s",g_boot_path,filename&#41;;

	fd = sceIoOpen&#40;path_name, PSP_O_RDONLY, 0777&#41;;

	if &#40;fd<0&#41;
	&#123;
		wavHalt&#40;"file failed",path_name&#41;;
		return NULL;
	&#125;

	sceIoGetstat&#40;path_name,&stat&#41;;

	wi = malloc&#40;stat.st_size + sizeof&#40;wavout_wavinfo_t&#41;&#41;;

	wavfile = &#40;char*&#41;&#40;wi+sizeof&#40;wavout_wavinfo_t&#41;&#41;;
	filelen = sceIoRead&#40;fd, wavfile, stat.st_size&#41;;
	sceIoClose&#40;fd&#41;;


	if &#40;memcmp&#40;wavfile,"RIFF",4&#41;!=0&#41;
	&#123;
		wavHalt&#40;"format error not RIFF",path_name&#41;;
		free&#40;wi&#41;;
		return NULL;
	&#125;

	if &#40;memcmp&#40;wavfile+8,"WAVEfmt \x10\x00\x00\x00\x01\x00",14&#41;!=0&#41;
	&#123;
		wavHalt&#40;"format error no WAVEfmt string",path_name&#41;;
		free&#40;wi&#41;;
		return NULL;
	&#125;

	channels=*&#40;short *&#41;&#40;wavfile+0x16&#41;;
	samplerate=*&#40;long *&#41;&#40;wavfile+0x18&#41;;
	blocksize=*&#40;short *&#41;&#40;wavfile+0x20&#41;;
	bitpersample=*&#40;short *&#41;&#40;wavfile+0x22&#41;;

	if &#40;memcmp&#40;wavfile+0x24,"data",4&#41;!=0&#41;
	&#123;
		wavHalt&#40;"no data chunk found",path_name&#41;;
		free&#40;wi&#41;;
		return NULL;
	&#125;

	datalength=*&#40;unsigned long *&#41;&#40;wavfile+0x28&#41;;

	if &#40;datalength+0x2c>filelen&#41;
	&#123;
		wavHalt&#40;"buffer is supposed to be bigger than this",path_name&#41;;
		free&#40;wi&#41;;
		return NULL;
	&#125;

	if &#40;channels!=2 && channels!=1&#41;
	&#123;
		wavHalt&#40;"not Mono or Stereo sample",path_name&#41;;
		free&#40;wi&#41;;
		return NULL;
	&#125;

	if &#40;samplerate>100000 || samplerate<2000&#41;
	&#123;
		wavHalt&#40;"sample rate is wrong",path_name&#41;;
		free&#40;wi&#41;;
		return NULL;
	&#125;

	if &#40;blocksize!=channels*2&#41;
	&#123;
		wavHalt&#40;"BLOCKSIZE MISMATCH",path_name&#41;;
		free&#40;wi&#41;;
		return NULL;
&#125;

	if &#40;bitpersample!=16&#41;
	&#123;
		wavHalt&#40;"Bits Per Sample Error",path_name&#41;;
		free&#40;wi&#41;;
		return NULL;
	&#125;

	if &#40;channels==2&#41;
	&#123;
		samplecount=datalength/4;
	&#125;
	else
	&#123;
		samplecount=datalength/2;
	&#125;

	if &#40;samplecount<=0&#41;
	&#123;
		wavHalt&#40;"no samples",path_name&#41;;
		free&#40;wi&#41;;
		return NULL;
	&#125;

	wi->channels=channels;
	wi->samplerate=samplerate;
	wi->samplecount=samplecount;
	wi->datalength=datalength;
	wi->wavdata=wavfile+0x2c;
	wi->rateratio=&#40;samplerate*0x4000&#41;/11025;
	wi->playptr=0;
	wi->playptr_frac=0;
	wi->playloop=0;

	return wi;
&#125;
wave.h:

Code: Select all


typedef struct &#123;
	unsigned long channels;
	unsigned long samplerate;
	unsigned long samplecount;
	unsigned long datalength;
	unsigned long rateratio;		// samplerate / 44100 * 0x10000
	unsigned long playptr;
	unsigned long playptr_frac;
	int 					playloop;
	char 					*wavdata;
&#125; wavout_wavinfo_t;

int 	wavInit&#40;&#41;;
int 	wavPlay&#40;wavout_wavinfo_t *wi&#41;;
wavout_wavinfo_t *wavLoad&#40;char *filename&#41;;
Then in your main.c add to the top:

Code: Select all

#include <pspaudiolib.h>
#include "wave.h"
and to load a wav file from the current directory:

Code: Select all

    wavInit&#40;&#41;;
    wav_intro = wavLoad&#40;"test.wav"&#41;;
    wav_intro->playloop = 1;
    wavPlay&#40;wav_intro&#41;;

and last but not least - adding for comparison - sound.c:

Code: Select all

#define PGA_CHANNELS 3
#define PGA_SAMPLES 256
#define MAXVOLUME 0x8000
#define SND1_MAXSLOT 16
#define PB_N 32
#define WAVFILEMAX_BG 8*1024*1024
#define O_RDONLY    0x0001 
#define O_WRONLY    0x0002 
#define O_RDWR      0x0003 
#define O_NBLOCK    0x0010 
#define O_APPEND    0x0100 
#define O_CREAT     0x0200 
#define O_TRUNC     0x0400 
#define O_NOWAIT    0x8000 
#define MAX_PATH 512
#define NULL 0
//---------- variables
int pga_ready=0;
int pga_handle&#91;PGA_CHANNELS&#93;;
short pga_sndbuf&#91;PGA_CHANNELS&#93;&#91;2&#93;&#91;PGA_SAMPLES&#93;&#91;2&#93;;
int pga_threadhandle&#91;PGA_CHANNELS&#93;;
volatile int pga_terminate=0;

typedef int &#40;*pg_threadfunc_t&#41;&#40;int args, void *argp&#41;;

typedef struct &#123;
	unsigned long channels;
	unsigned long samplerate;
	unsigned long samplecount;
	unsigned long datalength;
	char *wavdata;
	unsigned long rateratio;		// samplerate / 44100 * 0x10000
	unsigned long playptr;
	unsigned long playptr_frac;
	int playloop;
&#125; wavout_wavinfo_t;

wavout_wavinfo_t *wavout_snd0_wavinfo=0;
wavout_wavinfo_t wavout_snd1_wavinfo&#91;SND1_MAXSLOT&#93;;

int wavout_snd1_playing&#91;SND1_MAXSLOT&#93;;


int wavout_snd0_ready=0;
unsigned long wavout_snd0_playptr=0;
int wavout_snd0_playend=0;


short powerbuf&#91;PB_N&#93;&#91;256&#93;;
unsigned int powerbuf_in=0;

short powersrc&#91;256&#93;;
short powersrc2&#91;256&#93;;


char wavdata_bg&#91;WAVFILEMAX_BG&#93;;
wavout_wavinfo_t wavinfo_bg;
char pg_mypath&#91;MAX_PATH&#93;;
char pg_workdir&#91;MAX_PATH&#93;;

//------------ Tableau de merde
int brtbl&#91;&#93;=&#123;
  0,128, 64,192, 32,160, 96,224, 16,144, 80,208, 48,176,112,240,  8,136, 72,200, 40,168,104,232, 24,152, 88,216, 56,184,120,248,
  4,132, 68,196, 36,164,100,228, 20,148, 84,212, 52,180,116,244, 12,140, 76,204, 44,172,108,236, 28,156, 92,220, 60,188,124,252,
  2,130, 66,194, 34,162, 98,226, 18,146, 82,210, 50,178,114,242, 10,138, 74,202, 42,170,106,234, 26,154, 90,218, 58,186,122,250,
  6,134, 70,198, 38,166,102,230, 22,150, 86,214, 54,182,118,246, 14,142, 78,206, 46,174,110,238, 30,158, 94,222, 62,190,126,254,
  1,129, 65,193, 33,161, 97,225, 17,145, 81,209, 49,177,113,241,  9,137, 73,201, 41,169,105,233, 25,153, 89,217, 57,185,121,249,
  5,133, 69,197, 37,165,101,229, 21,149, 85,213, 53,181,117,245, 13,141, 77,205, 45,173,109,237, 29,157, 93,221, 61,189,125,253,
  3,131, 67,195, 35,163, 99,227, 19,147, 83,211, 51,179,115,243, 11,139, 75,203, 43,171,107,235, 27,155, 91,219, 59,187,123,251,
  7,135, 71,199, 39,167,103,231, 23,151, 87,215, 55,183,119,247, 15,143, 79,207, 47,175,111,239, 31,159, 95,223, 63,191,127,255,
&#125;;

int mm1&#91;8&#93;&#91;128&#93;=&#123;
&#123;  16384,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0, &#125;,
&#123;  16384,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0, &#125;,
&#123;  16384, 11585,     0,-11585,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0, &#125;,
&#123;  16384, 15136, 11585,  6269,     0, -6269,-11585,-15136,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0, &#125;,
&#123;  16384, 16069, 15136, 13622, 11585,  9102,  6269,  3196,     0, -3196, -6269, -9102,-11585,-13622,-15136,-16069,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0, &#125;,
&#123;  16384, 16305, 16069, 15678, 15136, 14449, 13622, 12665, 11585, 10393,  9102,  7723,  6269,  4756,  3196,  1605,     0, -1605, -3196, -4756, -6269, -7723, -9102,-10393,-11585,-12665,-13622,-14449,-15136,-15678,-16069,-16305,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0, &#125;,
&#123;  16384, 16364, 16305, 16206, 16069, 15892, 15678, 15426, 15136, 14810, 14449, 14053, 13622, 13159, 12665, 12139, 11585, 11002, 10393,  9759,  9102,  8423,  7723,  7005,  6269,  5519,  4756,  3980,  3196,  2404,  1605,   803,     0,  -803, -1605, -2404, -3196, -3980, -4756, -5519, -6269, -7005, -7723, -8423, -9102, -9759,-10393,-11002,-11585,-12139,-12665,-13159,-13622,-14053,-14449,-14810,-15136,-15426,-15678,-15892,-16069,-16206,-16305,-16364,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0, &#125;,
&#123;  16384, 16379, 16364, 16339, 16305, 16260, 16206, 16142, 16069, 15985, 15892, 15790, 15678, 15557, 15426, 15286, 15136, 14978, 14810, 14634, 14449, 14255, 14053, 13842, 13622, 13395, 13159, 12916, 12665, 12406, 12139, 11866, 11585, 11297, 11002, 10701, 10393, 10079,  9759,  9434,  9102,  8765,  8423,  8075,  7723,  7366,  7005,  6639,  6269,  5896,  5519,  5139,  4756,  4369,  3980,  3589,  3196,  2801,  2404,  2005,  1605,  1205,   803,   402,     0,  -402,  -803, -1205, -1605, -2005, -2404, -2801, -3196, -3589, -3980, -4369, -4756, -5139, -5519, -5896, -6269, -6639, -7005, -7366, -7723, -8075, -8423, -8765, -9102, -9434, -9759,-10079,-10393,-10701,-11002,-11297,-11585,-11866,-12139,-12406,-12665,-12916,-13159,-13395,-13622,-13842,-14053,-14255,-14449,-14634,-14810,-14978,-15136,-15286,-15426,-15557,-15678,-15790,-15892,-15985,-16069,-16142,-16206,-16260,-16305,-16339,-16364,-16379, &#125;,
&#125;;
int mm2&#91;8&#93;&#91;128&#93;=&#123;
&#123;      0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0, &#125;,
&#123;      0,-16384,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0, &#125;,
&#123;      0,-11585,-16384,-11585,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0, &#125;,
&#123;      0, -6269,-11585,-15136,-16383,-15136,-11585, -6269,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0, &#125;,
&#123;      0, -3196, -6269, -9102,-11585,-13622,-15136,-16069,-16383,-16069,-15136,-13622,-11585, -9102, -6269, -3196,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0, &#125;,
&#123;      0, -1605, -3196, -4756, -6269, -7723, -9102,-10393,-11585,-12665,-13622,-14449,-15136,-15678,-16069,-16305,-16384,-16305,-16069,-15678,-15136,-14449,-13622,-12665,-11585,-10393, -9102, -7723, -6269, -4756, -3196, -1605,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0, &#125;,
&#123;      0,  -803, -1605, -2404, -3196, -3980, -4756, -5519, -6269, -7005, -7723, -8423, -9102, -9759,-10393,-11002,-11585,-12139,-12665,-13159,-13622,-14053,-14449,-14810,-15136,-15426,-15678,-15892,-16069,-16206,-16305,-16364,-16384,-16364,-16305,-16206,-16069,-15892,-15678,-15426,-15136,-14810,-14449,-14053,-13622,-13159,-12665,-12139,-11585,-11002,-10393, -9759, -9102, -8423, -7723, -7005, -6269, -5519, -4756, -3980, -3196, -2404, -1605,  -803,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0, &#125;,
&#123;      0,  -402,  -803, -1205, -1605, -2005, -2404, -2801, -3196, -3589, -3980, -4369, -4756, -5139, -5519, -5896, -6269, -6639, -7005, -7366, -7723, -8075, -8423, -8765, -9102, -9434, -9759,-10079,-10393,-10701,-11002,-11297,-11585,-11866,-12139,-12406,-12665,-12916,-13159,-13395,-13622,-13842,-14053,-14255,-14449,-14634,-14810,-14978,-15136,-15286,-15426,-15557,-15678,-15790,-15892,-15985,-16069,-16142,-16206,-16260,-16305,-16339,-16364,-16379,-16384,-16379,-16364,-16339,-16305,-16260,-16206,-16142,-16069,-15985,-15892,-15790,-15678,-15557,-15426,-15286,-15136,-14978,-14810,-14634,-14449,-14255,-14053,-13842,-13622,-13395,-13159,-12916,-12665,-12406,-12139,-11866,-11585,-11297,-11002,-10701,-10393,-10079, -9759, -9434, -9102, -8765, -8423, -8075, -7723, -7366, -7005, -6639, -6269, -5896, -5519, -5139, -4756, -4369, -3980, -3589, -3196, -2801, -2404, -2005, -1605, -1205,  -803,  -402, &#125;,
&#125;;

const unsigned char sqrtbl&#91;&#93;=&#123;
	  0,   1,   1,   1,   2,   2,   2,   2,   2,   3,   3,   3,   3,   3,   3,   3,
	  4,   4,   4,   4,   4,   4,   4,   4,   4,   5,   5,   5,   5,   5,   5,   5,
	  5,   5,   5,   5,   6,   6,   6,   6,   6,   6,   6,   6,   6,   6,   6,   6,
	  6,   7,   7,   7,   7,   7,   7,   7,   7,   7,   7,   7,   7,   7,   7,   7,
	  8,   8,   8,   8,   8,   8,   8,   8,   8,   8,   8,   8,   8,   8,   8,   8,
	  8,   9,   9,   9,   9,   9,   9,   9,   9,   9,   9,   9,   9,   9,   9,   9,
	  9,   9,   9,   9,  10,  10,  10,  10,  10,  10,  10,  10,  10,  10,  10,  10,
	 10,  10,  10,  10,  10,  10,  10,  10,  10,  11,  11,  11,  11,  11,  11,  11,
	 11,  11,  11,  11,  11,  11,  11,  11,  11,  11,  11,  11,  11,  11,  11,  11,
	 12,  12,  12,  12,  12,  12,  12,  12,  12,  12,  12,  12,  12,  12,  12,  12,
	 12,  12,  12,  12,  12,  12,  12,  12,  12,  13,  13,  13,  13,  13,  13,  13,
	 13,  13,  13,  13,  13,  13,  13,  13,  13,  13,  13,  13,  13,  13,  13,  13,
	 13,  13,  13,  13,  14,  14,  14,  14,  14,  14,  14,  14,  14,  14,  14,  14,
	 14,  14,  14,  14,  14,  14,  14,  14,  14,  14,  14,  14,  14,  14,  14,  14,
	 14,  15,  15,  15,  15,  15,  15,  15,  15,  15,  15,  15,  15,  15,  15,  15,
	 15,  15,  15,  15,  15,  15,  15,  15,  15,  15,  15,  15,  15,  15,  15,  15,
	&#125;;
//---------- proto
int pgaInit&#40;&#41;;
void &#40;*pga_channel_callback&#91;PGA_CHANNELS&#93;&#41;&#40;void *buf, unsigned long reqn&#41;;
static int pga_channel_thread&#40;int args, void *argp&#41;;
void pga_channel_thread_callback&#40;int channel, void *buf, unsigned long reqn&#41;;
void pgaSetChannelCallback&#40;int channel, void *callback&#41;;
int pgaOutBlocking&#40;unsigned long channel,unsigned long vol1,unsigned long vol2,void *buf&#41;;

static void wavout_snd0_callback&#40;short *_buf, unsigned long _reqn&#41;;
static void wavout_snd1_callback&#40;short *_buf, unsigned long _reqn&#41;;
void powercalc&#40;short *in&#41;;
unsigned long sqri&#40;unsigned long d&#41;;
int wavoutLoadWav&#40;const char *filename, wavout_wavinfo_t *wi, void *buf, unsigned long buflen&#41;;

int pgfOpen&#40;const char *filename, unsigned long flag&#41;;
void pgfClose&#40;int fd&#41;;
int pgfRead&#40;int fd, void *data, int size&#41;;

void wavoutStopPlay0&#40;&#41;;
void wavoutStartPlay0&#40;wavout_wavinfo_t *wi&#41;;

//---------- function
int pgaInit&#40;&#41;
&#123;
	int i,ret;
	int failed=0;
	char str&#91;32&#93;;

	pga_terminate=0;
	pga_ready=0;

	for &#40;i=0; i<PGA_CHANNELS; i++&#41; &#123;
		pga_handle&#91;i&#93;=-1;
		pga_threadhandle&#91;i&#93;=-1;
		pga_channel_callback&#91;i&#93;=0;
	&#125;
	for &#40;i=0; i<PGA_CHANNELS; i++&#41; &#123;
		if &#40;&#40;pga_handle&#91;i&#93;=sceAudio_3&#40;-1,PGA_SAMPLES,0&#41;&#41;<0&#41; failed=1;
	&#125;
	if &#40;failed&#41; &#123;
		for &#40;i=0; i<PGA_CHANNELS; i++&#41; &#123;
			if &#40;pga_handle&#91;i&#93;!=-1&#41; sceAudio_4&#40;pga_handle&#91;i&#93;&#41;;
			pga_handle&#91;i&#93;=-1;
		&#125;
		return -1;
	&#125;
	pga_ready=1;

	strcpy&#40;str,"pgasnd0"&#41;;
	for &#40;i=0; i<PGA_CHANNELS; i++&#41; &#123;
		str&#91;6&#93;='0'+i;
		pga_threadhandle&#91;i&#93;=sceKernelCreateThread&#40;str,&#40;pg_threadfunc_t&#41;&pga_channel_thread,0x12,0x10000,0,NULL&#41;;
		if &#40;pga_threadhandle&#91;i&#93;<0&#41; &#123;
			pga_threadhandle&#91;i&#93;=-1;
			failed=1;
			break;
		&#125;
		ret=sceKernelStartThread&#40;pga_threadhandle&#91;i&#93;,sizeof&#40;i&#41;,&i&#41;;
		if &#40;ret!=0&#41; &#123;
			failed=1;
			break;
		&#125;
	&#125;
	if &#40;failed&#41; &#123;
		pga_terminate=1;
		for &#40;i=0; i<PGA_CHANNELS; i++&#41; &#123;
			if &#40;pga_threadhandle&#91;i&#93;!=-1&#41; &#123;
				sceKernelWaitThreadEnd&#40;pga_threadhandle&#91;i&#93;,NULL&#41;;
				sceKernelDeleteThread&#40;pga_threadhandle&#91;i&#93;&#41;;
			&#125;
			pga_threadhandle&#91;i&#93;=-1;
		&#125;
		pga_ready=0;
		return -1;
	&#125;
	return 0;
&#125;

static int pga_channel_thread&#40;int args, void *argp&#41;
&#123;
	volatile int bufidx=0;
	int channel=*&#40;int *&#41;argp;
	
	while &#40;pga_terminate==0&#41; &#123;
		void *bufptr=&pga_sndbuf&#91;channel&#93;&#91;bufidx&#93;;
		void &#40;*callback&#41;&#40;void *buf, unsigned long reqn&#41;;
		callback=pga_channel_callback&#91;channel&#93;;
		if &#40;callback&#41; &#123;
			callback&#40;bufptr,PGA_SAMPLES&#41;;
		&#125; else &#123;
			unsigned long *ptr=bufptr;
			int i;
			for &#40;i=0; i<PGA_SAMPLES; ++i&#41; *&#40;ptr++&#41;=0;
		&#125;
		pgaOutBlocking&#40;channel,0x8000,0x8000,bufptr&#41;;
		bufidx=&#40;bufidx?0&#58;1&#41;;
	&#125;
	sceKernelExitThread&#40;0&#41;;
	return 0;
&#125;


void pga_channel_thread_callback&#40;int channel, void *buf, unsigned long reqn&#41;
&#123;
	void &#40;*callback&#41;&#40;void *buf, unsigned long reqn&#41;;
	callback=pga_channel_callback&#91;channel&#93;;
&#125;


void pgaSetChannelCallback&#40;int channel, void *callback&#41;
&#123;
	pga_channel_callback&#91;channel&#93;=callback;
&#125;

int pgaOutBlocking&#40;unsigned long channel,unsigned long vol1,unsigned long vol2,void *buf&#41;
&#123;
	if &#40;!pga_ready&#41; return -1;
	if &#40;channel>=PGA_CHANNELS&#41; return -1;
	if &#40;vol1>MAXVOLUME&#41; vol1=MAXVOLUME;
	if &#40;vol2>MAXVOLUME&#41; vol2=MAXVOLUME;
	return sceAudio_2&#40;pga_handle&#91;channel&#93;,vol1,vol2,buf&#41;;
&#125;

int wavoutInit&#40;&#41;
&#123;
	int i;
	
	wavout_snd0_wavinfo=0;
	
	for &#40;i=0; i<SND1_MAXSLOT; i++&#41; &#123;
		wavout_snd1_playing&#91;i&#93;=0;
	&#125;

	pgaSetChannelCallback&#40;0,wavout_snd0_callback&#41;;
	pgaSetChannelCallback&#40;1,wavout_snd1_callback&#41;;
	return 0;
&#125;

static void wavout_snd0_callback&#40;short *_buf, unsigned long _reqn&#41;
&#123;
	static int power&#91;128&#93;;
	
	unsigned long i;
	unsigned long ptr,frac,rr,max;
	int channels;
	char *src;
	short *buf=_buf;
	unsigned long reqn=_reqn;
	
	wavout_wavinfo_t *wi=wavout_snd0_wavinfo;

	if &#40;wi==0&#41; &#123;
		wavout_snd0_ready=1;
		memset&#40;buf,0,reqn*4&#41;;
		return;
	&#125;
	
	wavout_snd0_ready=0;
	
	ptr=wi->playptr;
	frac=wi->playptr_frac;
	rr=wi->rateratio;
	max=wi->samplecount;
	channels=wi->channels;
	src=wi->wavdata;

	for &#40;; reqn>0; --reqn&#41; &#123;
		frac+=rr;
		ptr+=&#40;frac>>16&#41;;
		frac&=0xffff;
		if &#40;ptr>=max&#41; &#123;
			if &#40;wi->playloop&#41; &#123;
				ptr=0;
			&#125; else &#123;
				for &#40;; reqn>0; --reqn&#41; &#123;
					*&#40;buf++&#41;=0;
					*&#40;buf++&#41;=0;
				&#125;
				goto playend;
			&#125;
		&#125;
		if &#40;channels==1&#41; &#123;
			buf&#91;0&#93;=buf&#91;1&#93;=*&#40;short *&#41;&#40;src+ptr*2&#41;;
			buf+=2;
		&#125; else &#123;
			buf&#91;0&#93;=*&#40;short *&#41;&#40;src+ptr*4&#41;;
			buf&#91;1&#93;=*&#40;short *&#41;&#40;src+ptr*4+2&#41;;
			buf+=2;
		&#125;
	&#125;

	powercalc&#40;_buf&#41;;	//’P‚Éwave‚ð�o‚·‚¾‚¯‚È‚ç•s—v

	wavout_snd0_playptr=ptr;
	wi->playptr=ptr;
	wi->playptr_frac=frac;
	return;
	
playend&#58;
	wavout_snd0_playend=1;
	return;
&#125;

static void wavout_snd1_callback&#40;short *_buf, unsigned long _reqn&#41;
&#123;
	unsigned long i,slot;
	wavout_wavinfo_t *wi;
	unsigned long ptr,frac;
	short *buf=_buf;
	
	for &#40;i=0; i<_reqn; i++&#41; &#123;
		int outr=0,outl=0;
		for &#40;slot=0; slot<SND1_MAXSLOT; slot++&#41; &#123;
			if &#40;!wavout_snd1_playing&#91;slot&#93;&#41; continue;
			wi=&wavout_snd1_wavinfo&#91;slot&#93;;
			frac=wi->playptr_frac+wi->rateratio;
			wi->playptr=ptr=wi->playptr+&#40;frac>>16&#41;;
			wi->playptr_frac=&#40;frac&0xffff&#41;;
			if &#40;ptr>=wi->samplecount&#41; &#123;
				wavout_snd1_playing&#91;slot&#93;=0;
				break;
			&#125;
			short *src=&#40;short *&#41;wi->wavdata;
			if &#40;wi->channels==1&#41; &#123;
				outl+=src&#91;ptr&#93;;
				outr+=src&#91;ptr&#93;;
			&#125; else &#123;
				outl+=src&#91;ptr*2&#93;;
				outr+=src&#91;ptr*2+1&#93;;
			&#125;
		&#125;
		if &#40;outl<-32768&#41; outl=-32768;
		else if &#40;outl>32767&#41; outl=32767;
		if &#40;outr<-32768&#41; outr=-32768;
		else if &#40;outr>32767&#41; outr=32767;
		*&#40;buf++&#41;=outl;
		*&#40;buf++&#41;=outr;
	&#125;
&#125;

unsigned long sqri&#40;unsigned long d&#41;
&#123;
	unsigned char c;
	unsigned long r;
	
	
	if &#40;d==0&#41; return 0;
	r=	&#40;c=sqrtbl&#91;&#40;&#40;unsigned char *&#41;&d&#41;&#91;3&#93;&#93;&#41;?&#40;&#40;unsigned long&#41;c<<12&#41;&#58;&#40;
		&#40;c=sqrtbl&#91;&#40;&#40;unsigned char *&#41;&d&#41;&#91;2&#93;&#93;&#41;?&#40;&#40;unsigned long&#41;c<<8&#41;&#58;&#40;
		&#40;c=sqrtbl&#91;&#40;&#40;unsigned char *&#41;&d&#41;&#91;1&#93;&#93;&#41;?&#40;&#40;unsigned long&#41;c<<4&#41;&#58;&#40;
		sqrtbl&#91;&#40;&#40;unsigned char *&#41;&d&#41;&#91;0&#93;&#93; &#41;&#41;&#41;;
	r=&#40;d/r+r&#41;>>1;
	r=&#40;d/r+r&#41;>>1;
	r=&#40;d/r+r&#41;>>1;
	r=&#40;d/r+r&#41;>>1;
	return r;

&#125;

void powercalc&#40;short *in&#41;
&#123;
	&#123;//work with sources
		int i,j,sum;
		for &#40;i=0; i<256-16; i++&#41; powersrc&#91;i&#93;=powersrc&#91;i+16&#93;;
		sum=0;
		for &#40;i=0; i<16; i++&#41; &#123;
			powersrc&#91;256-16+i&#93;=&#40; &#40;int&#41;in&#91;i*32&#93;+&#40;int&#41;in&#91;i*32+8&#93;+&#40;int&#41;in&#91;i*32+16&#93;+&#40;int&#41;in&#91;i*32+24&#93; &#41;/4;
		&#125;
	&#125;
	&#123;
		int i;
		for &#40;i=0; i<256; i++&#41; powersrc2&#91;i&#93;=powersrc&#91;i&#93;;
		for &#40;i=0; i<32; i++&#41; &#123;
			powersrc2&#91;i&#93;=&#40;&#40;int&#41;powersrc2&#91;i&#93;&#41;*i/32;
			powersrc2&#91;255-i&#93;=&#40;&#40;int&#41;powersrc2&#91;255-i&#93;&#41;*i/32;
		&#125;
	&#125;
	
	long m=8;
	long n=256;

	long m1,m2;

	long i,i1,j,k,i2,l,l1,l2;

	int ix&#91;256&#93;,iy&#91;256&#93;;
	int tx,ty,pw;

	//shuffle
	for &#40;i=0; i<256; i++&#41; &#123;
		ix&#91;i&#93;=&#40;int&#41;&#40;powersrc2&#91;brtbl&#91;i&#93;&#93;>>1&#41;;
		iy&#91;i&#93;=0;
	&#125;

	//fft main
	l2 = 1;
	for &#40;l=0; l<8; l++&#41; &#123;
		l1 = l2;
		l2 <<= 1;
		for &#40;j=0; j<l1; j++&#41; &#123;
			m1=mm1&#91;l&#93;&#91;j&#93;/64;
			m2=mm2&#91;l&#93;&#91;j&#93;/64;
			for &#40;i=j; i<n; i+=l2&#41; &#123;
				i1 = i + l1;
				tx = &#40; m1 * ix&#91;i1&#93; - m2 * iy&#91;i1&#93; &#41;/256;
				ty = &#40; m1 * iy&#91;i1&#93; + m2 * ix&#91;i1&#93; &#41;/256;
				ix&#91;i1&#93; = ix&#91;i&#93; - tx; 
				iy&#91;i1&#93; = iy&#91;i&#93; - ty;
				ix&#91;i&#93; += tx;
				iy&#91;i&#93; += ty;
			&#125;
		&#125;
	&#125;

	//scale & normalize
	short *pp=powerbuf&#91;powerbuf_in&#93;;
	for &#40;i=0; i<128; i++&#41; &#123;
		//tx,ty &#58; re,im  forward fft scaling=256, fixed point +-16384
		tx=ix&#91;i&#93;/256;
		ty=iy&#91;i&#93;/256;
		pw=&#40;sqri&#40;tx*tx+ty*ty&#41;&#41;;
		//normalize it 
		pw=pw*&#40;j+8&#41;/512;
		if &#40;pw>127&#41; pw=127;
		*&#40;pp++&#41;=pw;
	&#125;
	powerbuf_in=&#40;&#40;powerbuf_in+1&#41;&&#40;PB_N-1&#41;&#41;;

   return;
&#125;

int wavoutLoadWav&#40;const char *filename, wavout_wavinfo_t *wi, void *buf, unsigned long buflen&#41;
&#123;
	unsigned int filelen;
	int fd;
	unsigned long channels;
	unsigned long samplerate;
	unsigned long blocksize;
	unsigned long bitpersample;
	unsigned long datalength;
	unsigned long samplecount;
	unsigned long i;
	
	char *wavfile=buf;
	wi->wavdata=NULL;

	fd=pgfOpen&#40;filename,O_RDONLY&#41;;
	if &#40;fd<0&#41; return -1;
	
	filelen=pgfRead&#40;fd,wavfile,buflen&#41;;
	pgfClose&#40;fd&#41;;
	if &#40;filelen>=buflen&#41; &#123;
		//too long
		return -1;
	&#125;
	
	if &#40;memcmp&#40;wavfile,"RIFF",4&#41;!=0&#41; &#123;
//		pgcPuts&#40;"format err"&#41;;
		return -1;
	&#125;
	
	if &#40;memcmp&#40;wavfile+8,"WAVEfmt \x10\x00\x00\x00\x01\x00",14&#41;!=0&#41; &#123;
//		pgcPuts&#40;"format err"&#41;;
		return -1;
	&#125;
	
	channels=*&#40;short *&#41;&#40;wavfile+0x16&#41;;
	samplerate=*&#40;long *&#41;&#40;wavfile+0x18&#41;;
	blocksize=*&#40;short *&#41;&#40;wavfile+0x20&#41;;
	bitpersample=*&#40;short *&#41;&#40;wavfile+0x22&#41;;
	
	if &#40;memcmp&#40;wavfile+0x24,"data",4&#41;!=0&#41; &#123;
//		pgcPuts&#40;"format err"&#41;;
		return -1;
	&#125;
	
	datalength=*&#40;unsigned long *&#41;&#40;wavfile+0x28&#41;;
	
	if &#40;datalength+0x2c>filelen&#41; &#123;
//		pgcPuts&#40;"format err"&#41;;
		return -1;
	&#125;
	
	if &#40;channels!=2 && channels!=1&#41; &#123;
//		pgcPuts&#40;"format err, channel"&#41;;
		return -1;
	&#125;
	
//	if &#40;samplerate!=44100 && samplerate!=22050 && samplerate!=11025&#41; &#123;
	if &#40;samplerate>100000 || samplerate<2000&#41; &#123;
//		pgcPuts&#40;"format err, samplerate"&#41;;
		return -1;
	&#125;
	
	if &#40;blocksize!=channels*2&#41; &#123;
//		pgcPuts&#40;"format err, blocksize"&#41;;
		return -1;
	&#125;
	
	if &#40;bitpersample!=16&#41; &#123;
//		pgcPuts&#40;"format err, bitpersample"&#41;;
		return -1;
	&#125;
	
	if &#40;channels==2&#41; &#123;
		samplecount=datalength/4;
	&#125; else &#123;
		samplecount=datalength/2;
	&#125;
	if &#40;samplecount<=0&#41; &#123;
//		pgcPuts&#40;"format err, samplecount"&#41;;
		return -1;
	&#125;
	
	wi->channels=channels;
	wi->samplerate=samplerate;
	wi->samplecount=samplecount;
	wi->datalength=datalength;
	wi->wavdata=wavfile+0x2c;
	wi->rateratio=&#40;samplerate*0x4000&#41;/11025;
	wi->playptr=0;
	wi->playptr_frac=0;
	wi->playloop=0;
	
	return 0;
&#125;

int pgfOpen&#40;const char *filename, unsigned long flag&#41;
&#123;
	char fn&#91;MAX_PATH*2&#93;;
	if &#40;strchr&#40;filename,'&#58;'&#41;!=NULL || *filename=='/' || *filename=='\\'&#41; &#123;
		return sceIoOpen&#40;filename,flag&#41;;
	&#125; else &#123;
		strcpy&#40;fn,pg_workdir&#41;;
		strcat&#40;fn,filename&#41;;
		return sceIoOpen&#40;fn,flag&#41;;
	&#125;
&#125;

void pgfClose&#40;int fd&#41;
&#123;
	sceIoClose&#40;fd&#41;;
&#125;

int pgfRead&#40;int fd, void *data, int size&#41;
&#123;
	return sceIoRead&#40;fd,data,size&#41;;
&#125;

void wavoutStopPlay0&#40;&#41;
&#123;
	if &#40;wavout_snd0_wavinfo!=0&#41; &#123;
		while &#40;wavout_snd0_ready&#41; sceDisplayWaitVblankStart&#40;&#41;;;
		wavout_snd0_wavinfo=0;
		while &#40;!wavout_snd0_ready&#41; sceDisplayWaitVblankStart&#40;&#41;;;
	&#125;
&#125;

void wavoutStartPlay0&#40;wavout_wavinfo_t *wi&#41;
&#123;
	wavoutStopPlay0&#40;&#41;;
	while &#40;!wavout_snd0_ready&#41; sceDisplayWaitVblankStart&#40;&#41;;;
	wavout_snd0_playptr=0;
	wavout_snd0_playend=0;
	wavout_snd0_wavinfo=wi;
	while &#40;wavout_snd0_ready&#41; sceDisplayWaitVblankStart&#40;&#41;;;
&#125;
ok, so from the source's wave.c and sound.c you can see that the looping part within the wavout_snd?_callback function is lacking in wave.c - I attempted adding checks - resetting ptr = 0 etc like in sound.c but that doesn't help...I also attempted to add "is_playing" to the wavinfo struct but i think i messed up as that just caused the same wav to play overlapped over all the channels/slots but still died after one iteration of playing it... time to go to work!
...isn't it nice, sugar and spice...
...luring disco dollies to a life of vice...
sexdwarf
Posts: 34
Joined: Thu Jul 14, 2005 12:07 am

Post by sexdwarf »

almmost forgot:

Makefile:

Code: Select all

TARGET = sexdwarf_demo
OBJS = main.o wave.o

INCDIR = 
CFLAGS = -O2 -G0 -Wall
CXXFLAGS = $&#40;CFLAGS&#41; -fno-exceptions -fno-rtti
ASFLAGS = $&#40;CFLAGS&#41;

LIBDIR =
LIBS = -lpspumd -lpspaudiolib -lpspaudio -lm -lpspgu
LDFLAGS =

EXTRA_TARGETS = EBOOT.PBP
PSP_EBOOT_TITLE = Sexdwarf Demo

PSPSDK=$&#40;shell psp-config --pspsdk-path&#41;
include $&#40;PSPSDK&#41;/lib/build.mak
...isn't it nice, sugar and spice...
...luring disco dollies to a life of vice...
fantomasfr
Posts: 11
Joined: Fri Jul 08, 2005 9:02 pm

Post by fantomasfr »

//------------ Tableau de merde
hahahaha :)
sexdwarf
Posts: 34
Joined: Thu Jul 14, 2005 12:07 am

Post by sexdwarf »

here's what i've narrowed it down to at this point -

on line 58 to 62 in wave.c:

Code: Select all

         if &#40;ptr>=wi->samplecount&#41; 
         &#123; 
            wavout_snd_playing&#91;slot&#93;=0; 
            break; 
         &#125;
here it basically sees if the current sample is at the end of the audio file loaded, if it is, it simply turns playing off...

using the sound.c as a reference i notice that it checks the playloop variable to determine if it should restart the playback; so i made the following modification to my wave.c:

Code: Select all

        if &#40;ptr>=wi->samplecount&#41;
        &#123;
                if &#40;wi->playloop&#41;
                &#123;
                    ptr=0;
                &#125;
                else
                &#123;
                    wavout_snd_playing&#91;slot&#93;=0;
                    wi->playing = 0;
                    break;
                &#125;
        &#125;
however this does not seem to be enough to get looping to work (then again, who am i kidding, there's no reason it should be that simple ;)) but it was worth a try...
...isn't it nice, sugar and spice...
...luring disco dollies to a life of vice...
User avatar
jpadams
Posts: 23
Joined: Sat Jul 02, 2005 1:59 pm
Location: Los Angeles
Contact:

Post by jpadams »

that code is from my site at http://www.deadsoul.com/psp/wavloader.zip
I've submitted it to this thread http://forums.ps2dev.org/viewtopic.php?t=2395

i just ported Nem's wav loader , so i never tested looping will look at that
i've had a 5mb sample work . so maybe its a specific sample issue
maybe you could post a link to the sample so i can check it out ?

- Jed
sexdwarf
Posts: 34
Joined: Thu Jul 14, 2005 12:07 am

Post by sexdwarf »

jpadams wrote:that code is from my site at http://www.deadsoul.com/psp/wavloader.zip
I've submitted it to this thread http://forums.ps2dev.org/viewtopic.php?t=2395
oops, my bad for saying it was from the svn/samples (though, imho, it should be since i believe wave loading is something that people want/need)
i just ported Nem's wav loader , so i never tested looping will look at that i've had a 5mb sample work . so maybe its a specific sample issue
maybe you could post a link to the sample so i can check it out ?
i'll upload it to my server in a little bit - i do recall comparing the bit rate/channels etc with one of the tests waves in your zip file, and i know they were the same in that sence... tried a few wav's and had issues with any files that were bigger than 3mb...good work on the port, hopefully we can figure the looping stuff out ;)
...isn't it nice, sugar and spice...
...luring disco dollies to a life of vice...
User avatar
jpadams
Posts: 23
Joined: Sat Jul 02, 2005 1:59 pm
Location: Los Angeles
Contact:

Post by jpadams »

try this

Code: Select all

		if &#40;wi->playloop&#41;
		&#123;
			wi->playptr=0;
			wi->playptr_frac=0;
			ptr = 0;
		&#125;
		else
		&#123;
			wavout_snd_playing&#91;slot&#93;=0;
			break;
		&#125;
its a pain to test while at work ( and am crunching 12 hrs daily right now )

NOTE i've update the Zip file on my site with these changes
Last edited by jpadams on Tue Jul 19, 2005 4:55 am, edited 1 time in total.
sexdwarf
Posts: 34
Joined: Thu Jul 14, 2005 12:07 am

Post by sexdwarf »

jpadams wrote:its a pain to test while at work ( and am crunching 12 hrs daily right now )
great will have to test when i get home - know all about those 12 hour days sadly enough - thanks again :)
...isn't it nice, sugar and spice...
...luring disco dollies to a life of vice...
shifty
Posts: 32
Joined: Thu Jun 16, 2005 8:59 am
Location: MIT
Contact:

nem's wav loader, bug

Post by shifty »

hey all, just wanted to let you know, there's a bug in
nem's wav loader. It uses almost exclusively hardcoded
offsets...and from time to time you will find exceptions.

One of the most painful I found while coding PSPKick is
that the WAVE header length is not always 0x10. Sometimes
it's 0x12. Therefore, you might want to update your code
like this:
// size of WAVE chunk, typically 0x10, sometimes 0x12
WAVElen =*(unsigned long *)(wavfile+0x10);

When the WAVE Chunk header is offset, everything else gets
offset by the same amount! No more hardcoded offsets!

This also has interesting ramifications
when you compute later headers, because you can't read the lengths
of e.g. the "data" header with a 32-bit read...why? because it's
no longer 32-bit word-aligned! You'll want something like this:

// location of datalength
idx = 0x14 + WAVElen + 4; // 0x10 = WAVElen, +4 for the len, +4 for "data"

datalength=
(*(unsigned short *)(wavfile+idx)) +
(*(unsigned short *)(wavfile+idx+2))*65536;

// where the good stuff is
datastart = idx + 4; // typically 0x2c. sometimes 0x2e

I hope this helps! BTW WAVElen is about the only field
that can't get moved around.

happy music making.
sexdwarf
Posts: 34
Joined: Thu Jul 14, 2005 12:07 am

Post by sexdwarf »

ok the looping worked, thanks!

do you mind if i rework your files? first of all i would like to remove the hardcoded offsets as stated by shifty, additonally i would like to incorporate a proper chunk extraction routine, i.e. to handle non-PCM files, and to handle the RIFF format properly...

the reason my larger files weren't playing was not because of the size, but due to WAV1 encoding instead of PCM... the WAV1 structure requires an additional FACT chunk before the DATA chunk which interferes with the current wave.c... to ensure that the WAV1 format plays on the PSP i removed the FACT chunk manually, and the playback worked perfectly with a 6.5 mb file...
...isn't it nice, sugar and spice...
...luring disco dollies to a life of vice...
User avatar
jpadams
Posts: 23
Joined: Sat Jul 02, 2005 1:59 pm
Location: Los Angeles
Contact:

Post by jpadams »

re-work away
and pm me or mail me a copy when your done :)

- j
JJPeerless
Posts: 82
Joined: Mon Jun 20, 2005 3:32 am

Post by JJPeerless »

ive been playing with your wave.c and wave.h files..trying to use them to play sound effects in my game..

but first i just tried using your sample main.c and using my own wav files...

it froze quite often if i loaded 4 of my own wav files..all under 100kb..so size isnt the problem... all 44kHz...it would just freeze..but if i only loaded 1 at a time..it would playback just fine..

sometimes i could load 2..its weird

i dont understand why its so unstable..

could you maybe help me out..
User avatar
jpadams
Posts: 23
Joined: Sat Jul 02, 2005 1:59 pm
Location: Los Angeles
Contact:

Post by jpadams »

put the waves somewhere we can test them
i never claimed the code was stable ( it works fine for me )
i'll look into it for you if you can supply samples that break it

- J
JJPeerless
Posts: 82
Joined: Mon Jun 20, 2005 3:32 am

Post by JJPeerless »

http://teamdiscordia.com/sounds.zip

those are the sounds im trying to use..
User avatar
jpadams
Posts: 23
Joined: Sat Jul 02, 2005 1:59 pm
Location: Los Angeles
Contact:

Post by jpadams »

they are mono samples
interesting i never tried them :)

- J
JJPeerless
Posts: 82
Joined: Mon Jun 20, 2005 3:32 am

Post by JJPeerless »

...the 4 test.wav files in your sample are mono files as well??
JJPeerless
Posts: 82
Joined: Mon Jun 20, 2005 3:32 am

Post by JJPeerless »

did you get it to work?
User avatar
jpadams
Posts: 23
Joined: Sat Jul 02, 2005 1:59 pm
Location: Los Angeles
Contact:

Post by jpadams »

yes i stopped everything i was doing to fix this :)
still looking , hold tight could be a while

- J
JJPeerless
Posts: 82
Joined: Mon Jun 20, 2005 3:32 am

Post by JJPeerless »

is that sarcastic? i cant really tell..maybe your just really devoted to this..haha

=)
sexdwarf
Posts: 34
Joined: Thu Jul 14, 2005 12:07 am

Post by sexdwarf »

jpadams wrote:re-work away
and pm me or mail me a copy when your done :)

- j
will do - i have started on it, but with a busy scheduale at work i prolly won't be done until the weekend... it properly detects 11 wave/riff formats, and will be setup for most of the options listed in the following specs:

http://www.sonicspot.com/guide/wavefiles.html
http://www.borg.com/~jglatt/tech/wave.htm

i'm also going to see about integrating some kind of stream buffering so it doesn't require waiting for the full file to load before playing - not to mention that would allow for signicantly larger files to be played (for testing purposes I tried a 14.6, 29.7 and 48.9 mb files, all three crashed the system, which isn't that much of a suprise really, but still)
...isn't it nice, sugar and spice...
...luring disco dollies to a life of vice...
User avatar
jpadams
Posts: 23
Joined: Sat Jul 02, 2005 1:59 pm
Location: Los Angeles
Contact:

Post by jpadams »

JJPeerless wrote:is that sarcastic? i cant really tell..maybe your just really devoted to this..haha

=)
erm yes a tad sarcastic
you gave me less than 30 mins to look into the problem ,
assuming i was doing it right away ,
which i wasn't i was tangled in real work :)

- j
sexdwarf
Posts: 34
Joined: Thu Jul 14, 2005 12:07 am

Post by sexdwarf »

ok due to a little personal issue i got delayed - but i am near completion - it can load 5 formats (that i test, could load others too), stereo and mono, has looping etc... only issue i am having is loading more than one file at a time....once i have that straightened out it'll be available!
...isn't it nice, sugar and spice...
...luring disco dollies to a life of vice...
User avatar
jpadams
Posts: 23
Joined: Sat Jul 02, 2005 1:59 pm
Location: Los Angeles
Contact:

Post by jpadams »

sweet
if you need any help lemme know
then maybe we can get it in the svn :)
User avatar
jpadams
Posts: 23
Joined: Sat Jul 02, 2005 1:59 pm
Location: Los Angeles
Contact:

Post by jpadams »

this fixed the bug with jjpeerless's samples not working

line 218

Code: Select all

wi = memalign&#40;8,stat.st_size + sizeof&#40;wavout_wavinfo_t&#41;&#41;;
wavfile = &#40;char*&#41;&#40;wi&#41;+sizeof&#40;wavout_wavinfo_t&#41;;
JJPeerless
Posts: 82
Joined: Mon Jun 20, 2005 3:32 am

Post by JJPeerless »

thats already how it is in the wave.c file..

or did you already update the wave.c that is in that zip file...
User avatar
jpadams
Posts: 23
Joined: Sat Jul 02, 2005 1:59 pm
Location: Los Angeles
Contact:

Post by jpadams »

yes i updated the zip
User avatar
jpadams
Posts: 23
Joined: Sat Jul 02, 2005 1:59 pm
Location: Los Angeles
Contact:

Post by jpadams »

i've removed all psp related projects
including the wavloader source

from deadsoul.com to

http://labs.wonderbyte.com

cheers
- j
3rddek
Posts: 1
Joined: Tue Aug 15, 2006 4:29 pm

Post by 3rddek »

hey JP is your wavloader still around somewhere? All your hrefs are dead. Did it end up in the PSPSDK samples?
Post Reply