More audio functions

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

Moderators: cheriff, TyRaNiD

Post Reply
crazyc
Posts: 408
Joined: Fri Jun 17, 2005 10:13 am

More audio functions

Post by crazyc »

These functions bypass the software mixer (the mixer seems to have more then a little overhead) and (apparently) support an assortment of sample rates in hardware. I get a visible performance improvement by using them with a source stream at a sample rate of 22050. I don't know what will happen if you use them with the regular mixed audio functions.

Code: Select all

int sceAudio_38553111(unsigned short samples, unsigned short freq, char)  // play with conversion?
						// samples must be < 4112
						// freq == &#123;48000, 44100, 32000, 24000, 22050, 16000, 12000, 11050, 8000&#125;
						// the last param can only be 2, maybe number of channels?

int sceAudio_5C37C0AE&#40;void&#41;			// disable conversion?

int sceAudio_E0727056&#40;int volume, void *buffer&#41; // blocking output
						// volume max is probably PSP_AUDIO_VOLUME_MAX but can be set to anything
cooleyes
Posts: 123
Joined: Thu May 18, 2006 3:30 pm

Post by cooleyes »

good job!
thanks!
crazyc
Posts: 408
Joined: Fri Jun 17, 2005 10:13 am

Post by crazyc »

Speaking of this, if the developer of LightMP3 reads this, I have a patch for you.
mypspdev
Posts: 178
Joined: Wed Jul 11, 2007 10:30 pm

Post by mypspdev »

crazyc wrote:Speaking of this, if the developer of LightMP3 reads this, I have a patch for you.
very interesting, thanks.
Some users here are involved in porting of Flite to PSP / SDK (Flite is text-to-speech, text-to-wave, file.txt-to-speech, file.txt-to-wave).
Output is 8khz or 16khz. Of course it's preferred 8khz: quality is ok, RAM involvement is less.
We could be interested on this way avoiding oversamplig loops.
Could anyone please explain some more on patch or better propose an example of how to output sounds using this approch.
Thanks
TakutoKaneshiro
Posts: 4
Joined: Fri Sep 07, 2007 7:00 pm

Post by TakutoKaneshiro »

Yes, Im too want to know more about these audio functions. Im currently doing port of one good PC game and have a problem with SDL sound.
Can somebody explain, how to link against those functions (they arent in PSPSDK, am I right?) and make use of them?

Is sceAudio_38553111(unsigned short samples, unsigned short freq, char) used for initializing sound engine?
And sceAudio_E0727056(int volume, void *buffer) must be called in in order to play buffer?
crazyc
Posts: 408
Joined: Fri Jun 17, 2005 10:13 am

Post by crazyc »

Here's the patch, it should explain how their used.

Code: Select all

diff -ru ./pspaudiolib.c ../../src/pspaudiolib.c
--- ./pspaudiolib.c	2007-08-30 10&#58;37&#58;58.000000000 -0500
+++ ../../src/pspaudiolib.c	2007-09-08 15&#58;26&#58;05.828125000 -0500
@@ -18,6 +18,10 @@
 
 #include "pspaudiolib.h"
 
+int sceAudio_38553111&#40;unsigned short samples, unsigned short freq, char&#41;;
+int sceAudio_5C37C0AE&#40;void&#41;;
+int sceAudio_E0727056&#40;int volume, void *buffer&#41;;
+
 static int audio_ready=0;
 static short audio_sndbuf&#91;PSP_NUM_AUDIO_CHANNELS&#93;&#91;2&#93;&#91;PSP_NUM_AUDIO_SAMPLES&#93;&#91;2&#93;;
 
@@ -27,12 +31,14 @@
 
 void pspAudioSetVolume&#40;int channel, int left, int right&#41;
 &#123;
-  AudioStatus&#91;channel&#93;.volumeright = right;
-  AudioStatus&#91;channel&#93;.volumeleft  = left;
+ 	if&#40;channel >= PSP_NUM_AUDIO_CHANNELS&#41; return;
+	AudioStatus&#91;channel&#93;.volumeright = right;
+	AudioStatus&#91;channel&#93;.volumeleft  = left;
 &#125;
 
 void pspAudioChannelThreadCallback&#40;int channel, void *buf, unsigned int reqn&#41;
 &#123;
+	if&#40;channel >= PSP_NUM_AUDIO_CHANNELS&#41; return;
         pspAudioCallback_t callback;
         callback=AudioStatus&#91;channel&#93;.callback;
 &#125;
@@ -40,7 +46,8 @@
 
 void pspAudioSetChannelCallback&#40;int channel, pspAudioCallback_t callback, void *pdata&#41;
 &#123;
-        volatile psp_audio_channelinfo *pci = &AudioStatus&#91;channel&#93;;
+        if&#40;channel >= PSP_NUM_AUDIO_CHANNELS&#41; return;
+	volatile psp_audio_channelinfo *pci = &AudioStatus&#91;channel&#93;;
         pci->callback=0;
         pci->pdata=pdata;
         pci->callback=callback;
@@ -52,16 +59,18 @@
         if &#40;channel>=PSP_NUM_AUDIO_CHANNELS&#41; return -1;
         if &#40;vol1>PSP_VOLUME_MAX&#41; vol1=PSP_VOLUME_MAX;
         if &#40;vol2>PSP_VOLUME_MAX&#41; vol2=PSP_VOLUME_MAX;
-        return sceAudioOutputPannedBlocking&#40;AudioStatus&#91;channel&#93;.handle,vol1,vol2,buf&#41;;
+	return sceAudio_E0727056&#40;vol1,buf&#41;;
 &#125;
 
+static SceUID play_sema;
+
 static int AudioChannelThread&#40;int args, void *argp&#41;
 &#123;
         volatile int bufidx=0;
         int channel=*&#40;int *&#41;argp;
-
+	
         while &#40;audio_terminate==0&#41; &#123;
-                void *bufptr=&audio_sndbuf&#91;channel&#93;&#91;bufidx&#93;;
+		void *bufptr=&audio_sndbuf&#91;channel&#93;&#91;bufidx&#93;;
                 pspAudioCallback_t callback;
                 callback=AudioStatus&#91;channel&#93;.callback;
                 if &#40;callback&#41; &#123;
@@ -71,8 +80,11 @@
                         int i;
                         for &#40;i=0; i<PSP_NUM_AUDIO_SAMPLES; ++i&#41; *&#40;ptr++&#41;=0;
                 &#125;
-                pspAudioOutBlocking&#40;channel,AudioStatus&#91;channel&#93;.volumeleft,AudioStatus&#91;channel&#93;.volumeright,bufptr&#41;;
-                bufidx=&#40;bufidx?0&#58;1&#41;;
+//                pspAudioOutBlocking&#40;channel,AudioStatus&#91;channel&#93;.volumeleft,AudioStatus&#91;channel&#93;.volumeright,bufptr&#41;;
+		sceKernelWaitSema&#40;play_sema, 1, 0&#41;;
+		sceAudio_E0727056&#40;AudioStatus&#91;0&#93;.volumeright,bufptr&#41;;
+		sceKernelSignalSema&#40;play_sema, 1&#41;;
+		bufidx=&#40;bufidx?0&#58;1&#41;;
         &#125;
         sceKernelExitThread&#40;0&#41;;
         return 0;
@@ -82,7 +94,30 @@
 
 /******************************************************************************/
 
-
+int pspAudioSetFrequency&#40;unsigned short freq&#41;
+&#123;
+	int ret = 0;
+	switch&#40;freq&#41; &#123;
+		case 8000&#58;
+		case 12000&#58;
+		case 16000&#58;
+		case 24000&#58;
+		case 32000&#58;
+		case 48000&#58;
+		case 11025&#58;
+		case 22050&#58;
+		case 44100&#58;
+			break;
+		default&#58;
+			return -1;
+	&#125;
+	sceKernelWaitSema&#40;play_sema, 1, 0&#41;;
+	sceAudio_5C37C0AE&#40;&#41;;
+	if&#40;sceAudio_38553111&#40;PSP_NUM_AUDIO_SAMPLES,freq,2&#41;<0&#41; ret = -1;
+	sceKernelSignalSema&#40;play_sema, 1&#41;;
+	return ret;
+&#125;
+	
 
 int pspAudioInit&#40;&#41;
 &#123;
@@ -93,6 +128,8 @@
         audio_terminate=0;
         audio_ready=0;
 
+	play_sema = sceKernelCreateSema&#40;"play_sema", 6, 1, 1, 0&#41;;
+	
         for &#40;i=0; i<PSP_NUM_AUDIO_CHANNELS; i++&#41; &#123;
             AudioStatus&#91;i&#93;.handle = -1;
             AudioStatus&#91;i&#93;.threadhandle = -1;
@@ -102,13 +139,14 @@
             AudioStatus&#91;i&#93;.pdata = 0;
         &#125;
         for &#40;i=0; i<PSP_NUM_AUDIO_CHANNELS; i++&#41; &#123;
-                if &#40;&#40;AudioStatus&#91;i&#93;.handle = sceAudioChReserve&#40;-1,PSP_NUM_AUDIO_SAMPLES,0&#41;&#41;<0&#41;
+		if&#40;pspAudioSetFrequency&#40;44100&#41;<0&#41;
       failed=1;
         &#125;
         if &#40;failed&#41; &#123;
                 for &#40;i=0; i<PSP_NUM_AUDIO_CHANNELS; i++&#41; &#123;
                         if &#40;AudioStatus&#91;i&#93;.handle != -1&#41;
-        sceAudioChRelease&#40;AudioStatus&#91;i&#93;.handle&#41;;
+        //sceAudioChRelease&#40;AudioStatus&#91;i&#93;.handle&#41;;
+			sceAudio_5C37C0AE&#40;&#41;;
                         AudioStatus&#91;i&#93;.handle = -1;
                 &#125;
                 return -1;
@@ -169,7 +207,7 @@
 
         for &#40;i=0; i<PSP_NUM_AUDIO_CHANNELS; i++&#41; &#123;
                 if &#40;AudioStatus&#91;i&#93;.handle != -1&#41; &#123;
-                        sceAudioChRelease&#40;AudioStatus&#91;i&#93;.handle&#41;;
+			sceAudio_5C37C0AE&#40;&#41;;
                         AudioStatus&#91;i&#93;.handle = -1;
                 &#125;
         &#125;
diff -ru ./pspaudiolib.h ../../src/pspaudiolib.h
--- ./pspaudiolib.h	2007-08-30 10&#58;37&#58;34.000000000 -0500
+++ ../../src/pspaudiolib.h	2007-09-03 09&#58;52&#58;06.093750000 -0500
@@ -19,7 +19,7 @@
 #endif
 
 //I need just one channel, more CPU free&#58;
-#define PSP_NUM_AUDIO_CHANNELS 2
+#define PSP_NUM_AUDIO_CHANNELS 1
 //#define PSP_NUM_AUDIO_CHANNELS 4
 
 /** This is the number of frames you can update per callback, a frame being
@@ -48,6 +48,7 @@
 void pspAudioChannelThreadCallback&#40;int channel, void *buf, unsigned int reqn&#41;;
 void pspAudioSetChannelCallback&#40;int channel, pspAudioCallback_t callback, void *pdata&#41;;
 int  pspAudioOutBlocking&#40;unsigned int channel, unsigned int vol1, unsigned int vol2, void *buf&#41;;
+int  pspAudioSetFrequency&#40;unsigned short freq&#41;;
 
 #ifdef __cplusplus
 &#125;
J.F.
Posts: 2906
Joined: Sun Feb 22, 2004 11:41 am

Post by J.F. »

Nice! For apps where you either only need one channel, or do the mixing yourself, this will be more useful than the regular audio lib. :)
crazyc
Posts: 408
Joined: Fri Jun 17, 2005 10:13 am

Post by crazyc »

In the 3.xx firmwares you have to wait until sceAudioOutput2GetRestSample returns 0 before calling sceAudio_5C37C0AE. Also sceAudioOutput2OutputBlocking only calls sceAudio_E0727056, sceAudioOutput2Release only calls sceAudio_5C37C0AE and

Code: Select all

int sceAudioOutput2Reserve&#40;int samples&#41; &#123;
 return sceAudio_38553111&#40;samples, 44100, 2&#41;;
&#125;
Post Reply