Advertising (This ad goes away for registered users. You can Login or Register)

simplistic audio callback?

Forum rules
Forum rule Nº 15 is strictly enforced in this subforum.
Locked
grief3r
Posts: 358
Joined: Sat Nov 09, 2013 4:12 am

simplistic audio callback?

Post by grief3r »

The following sample code crashes the device
i believe the problem has something to do with filling the audio buffer
tested on the latest firmware
makes me wonder whether there is even memory already allocated for the audio buffer

Code: Select all


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


#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <limits.h>

/* 
   This part of the code is more or less identical to the sdktest sample 
*/

/* Define the module info section */
PSP_MODULE_INFO("AUDIOLIBDEMO", PSP_MODULE_USER, 1, 1);
/* Define the main thread's attribute value (optional) */
PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER | THREAD_ATTR_VFPU);
/* Define printf, just to make typing easier */
PSP_HEAP_SIZE_MAX();
#define printf	pspDebugScreenPrintf

/* Exit callback */
int exitCallback(int arg1, int arg2, void *common) {
	sceKernelExitGame();
	return 0;
}
const float PI = 3.1415926535897932f;
const int sampleRate = 11025;
float frequency = 440.0f;
float time = 0;
int function = 0;

typedef struct {
        short l, r;
} sample_t;

pspAudioCallback_t audioCallback(void* buf, unsigned int length, void *userdata) {
	 const float sampleLength = 1.0f / sampleRate;
	//const float scaleFactor = SHRT_MAX - 1.0f;
    //the amplitude of the wave
	const float scaleFactor = 8000;
	static float freq0 = 440.f;
    sample_t* ubuf = (sample_t*) buf;
	int i;
	
	if (frequency != freq0) {
	        time *= (freq0 / frequency);
	}
	
	for (i = 0; i < length; i++) {
	    short s = (short) (scaleFactor * sinf(2.0f * PI * frequency * time));
		ubuf[i].l = s;
		ubuf[i].r = s;
		time += sampleLength;
	}
	
	if (time * frequency > 1.0f) {
	    double d;
		time = modf(time * frequency, &d) / frequency;
	}
	freq0 = frequency;
	//return (void*)audioCallback;
}

/* Callback thread */
int callbackThread(SceSize args, void *argp) {
	int cbid;

	cbid = sceKernelCreateCallback("Exit Callback", exitCallback, NULL);
	sceKernelRegisterExitCallback(cbid);
	sceKernelSleepThreadCB();

	return 0;
}

/* Sets up the callback thread and returns its thread id */
int setupCallbacks(void) {
	int thid = 0;

	thid = sceKernelCreateThread("update_thread", callbackThread, 0x11, 0xFA0, 0, 0);
	if (thid >= 0) {
		sceKernelStartThread(thid, 0, 0);
	}
	return thid;
}

int main(void) {
	pspDebugScreenInit();
	setupCallbacks();
	pspAudioInit();
	pspAudioSetChannelCallback(0, audioCallback, NULL);

	//sceCtrlSetSamplingCycle(0);
	//sceCtrlSetSamplingMode(PSP_CTRL_MODE_ANALOG);


	//sceDisplayWaitVblankStart();
	//while(1) {
	//pspDebugScreenSetXY(0, 0);
	//blit_string(50,50,"debug",0x7FFFFFFF,0x11111111);
	//pspDebugScreenPuts("sample");
	//printf("freq = %.2f \n", frequency);
	//pspDebugScreenSetXY(0,1);
	//printf("debug");
	//sceDisplayWaitVblankStart();
	//}
	//sceKernelExitGame();

	//controlFrequency();
	
	while(1) {
		pspDebugScreenSetXY(0,2);
		printf("test");
		sceDisplayWaitVblankStart();
	}
	sceKernelExitGame();
	return 0;
}
Advertising
PSV1001 2.61 FieldRunners
PSP1001 6.60 Pro-C
PSP 3001 6.20 Pro-C2
Locked

Return to “Programming and Security”