Questions of multi-threading
-
- Posts: 8
- Joined: Thu Aug 24, 2006 12:30 pm
- Location: St. Louis, MO, U.S.A.
Questions of multi-threading
Hello all, I'm creating a homebrew game and I'm trying to add sound to it. I've got it working with the mp3 sample in the sdk, but it is in my main thread so it slows down my game significantly. I was wondering if there was a sample in the sdk that deals with multi-threading or any homebrew that uses multi-threading that I can learn from. Also is there anything I should know about how threads are prioritized so as to get smooth performance. Thanks and sorry if this has been asked before.
(Horrible example) but bakon abuses multithreading.
You'd find a place that you need to start the new thread at, then do something like
SceUID new_thid = sceKernelCreateThread("Name", &function, 0x18, 0x512, 0);
if(new_thid) sceKernelStartThread(new_thid, 0, NULL);
or however you did it in your start stuff(Don't have the toolchain installed on this machine, and my memory is kinda iffy atm, flu sucks)
then once you're done, you could do something like
SceUID thid_to_end = sceKernelGetThreadId();
sceKernelTerminateDeleteThread(thid_to_end);
or however. There are more than one way to do it, just depends how you want it set up, I suppose.
Just keep in mind you'll probably need a new loop to send it through, and you'd have to use delays.
You'd find a place that you need to start the new thread at, then do something like
SceUID new_thid = sceKernelCreateThread("Name", &function, 0x18, 0x512, 0);
if(new_thid) sceKernelStartThread(new_thid, 0, NULL);
or however you did it in your start stuff(Don't have the toolchain installed on this machine, and my memory is kinda iffy atm, flu sucks)
then once you're done, you could do something like
SceUID thid_to_end = sceKernelGetThreadId();
sceKernelTerminateDeleteThread(thid_to_end);
or however. There are more than one way to do it, just depends how you want it set up, I suppose.
Just keep in mind you'll probably need a new loop to send it through, and you'd have to use delays.
Programming with:
Geany + Latest PSPSDK from svn
Geany + Latest PSPSDK from svn
-
- Posts: 8
- Joined: Thu Aug 24, 2006 12:30 pm
- Location: St. Louis, MO, U.S.A.
Thanks, I tried it and got it to compile with no errors but I'm not getting any sound. Here's my code.
I call setupAudio() before entering my main loop. Any suggestions.
Code: Select all
#include <pspkernel.h>
#include <pspdebug.h>
#include <pspctrl.h>
#include <pspdisplay.h>
#include <stdio.h>
#include <pspaudio.h>
#include <pspmp3.h>
#include <psputility.h>
#define MP3FILE "./Resources/Music/music.mp3"
int fd;
int handle;
int samplingRate;
int numChannels;
// Input and Output buffers
char mp3Buf[16*1024] __attribute__((aligned(64)));
short pcmBuf[16*(1152/2)] __attribute__((aligned(64)));
int fillStreamBuffer(int fd, int handle)
{
char* dst;
int write;
int pos;
// Get Info on the stream (where to fill to, how much to fill, where to fill from)
sceMp3GetInfoToAddStreamData( handle, &dst, &write, &pos);
// Seek file to position requested
sceIoLseek32(fd, pos, SEEK_SET);
// Read the amount of data
int read = sceIoRead(fd, dst, write);
if (!read)
return 0;
// Notify mp3 library about how much we really wrote to the stream buffer
sceMp3NotifyAddStreamData(handle, read);
return (pos>0);
}
int audio()
{
static int channel = -1;
static int lastDecoded = 0;
static int volume = PSP_AUDIO_VOLUME_MAX;
if (sceMp3CheckStreamDataNeeded(handle) > 0)
fillStreamBuffer(fd, handle);
// Decode some samples
short* buf;
int bytesDecoded;
bytesDecoded = sceMp3Decode(handle, &buf);
if (bytesDecoded<=0)
sceMp3CheckStreamDataNeeded(handle);
// Nothing more to decode? Must have reached end of input buffer
if (bytesDecoded==0 || bytesDecoded==0x80671402)
sceMp3ResetPlayPosition(handle);
else
{
// Reserve the Audio channel for our output if not yet done
if (channel<0 || lastDecoded != bytesDecoded)
{
if (channel>=0)
sceAudioSRCChRelease();
channel = sceAudioSRCChReserve(bytesDecoded / (2 * numChannels), samplingRate, numChannels);
}
// Output the decoded samples and accumulate the number of played samples to get the playtime
sceAudioSRCOutputBlocking(volume, buf);
}
return 0;
}
int setupAudio()
{
// Load modules
sceUtilityLoadModule(PSP_MODULE_AV_AVCODEC);
sceUtilityLoadModule(PSP_MODULE_AV_MP3);
// Init mp3 resources
sceMp3InitResource();
// Open the input file
fd = sceIoOpen(MP3FILE, PSP_O_RDONLY, 0777);
// Reserve a mp3 handle for our playback
SceMp3InitArg mp3Init;
mp3Init.mp3StreamStart = 0;
mp3Init.mp3StreamEnd = sceIoLseek32(fd, 0, SEEK_END);
mp3Init.unk1 = 0;
mp3Init.unk2 = 0;
mp3Init.mp3Buf = mp3Buf;
mp3Init.mp3BufSize = sizeof(mp3Buf);
mp3Init.pcmBuf = pcmBuf;
mp3Init.pcmBufSize = sizeof(pcmBuf);
handle = sceMp3ReserveMp3Handle(&mp3Init);
// Fill the stream buffer with some data so that sceMp3Init has something to work with
fillStreamBuffer(fd, handle);
sceMp3Init(handle);
samplingRate = sceMp3GetSamplingRate(handle);
numChannels = sceMp3GetMp3ChannelNum(handle);
int thid = 0;
thid = sceKernelCreateThread("audio_thread", audio, 0x06, 0x10000, 0, 0);
if(thid >= 0)
{
sceKernelStartThread(thid, 0, 0);
}
return 0;
}
-
- Posts: 8
- Joined: Thu Aug 24, 2006 12:30 pm
- Location: St. Louis, MO, U.S.A.
Indeed, you were correct. I did need it to be in a loop. But my audio thread is still never being executed. Do I need to tell it in my main while function to go to my audio thread, or do I need to make the main while function a thread also. I'm sorry but I'm new to threads and coding for the PSP. Thanks.
-
- Posts: 8
- Joined: Thu Aug 24, 2006 12:30 pm
- Location: St. Louis, MO, U.S.A.