* Make it so that we do not crash trying to play an MP3 that failed
  to load.
This commit is contained in:
jean.chalard
2009-06-11 14:47:21 +00:00
parent c1565450e5
commit 3639ac291f
3 changed files with 281 additions and 283 deletions
+61 -61
View File
@@ -1,61 +1,61 @@
//------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------
// //
// JGE is a hardware accelerated 2D game SDK for PSP/Windows. // JGE is a hardware accelerated 2D game SDK for PSP/Windows.
// //
// Licensed under the BSD license, see LICENSE in JGE root for details. // Licensed under the BSD license, see LICENSE in JGE root for details.
// //
// Copyright (c) 2007 James Hui (a.k.a. Dr.Watson) <jhkhui@gmail.com> // Copyright (c) 2007 James Hui (a.k.a. Dr.Watson) <jhkhui@gmail.com>
// Copyright (c) 2008 Alexander Berl <raphael@fx-world.org> // Copyright (c) 2008 Alexander Berl <raphael@fx-world.org>
// Copyright (c) 2008 WilLoW :--) <wagic.the.homebrew@gmail.com> // Copyright (c) 2008 WilLoW :--) <wagic.the.homebrew@gmail.com>
// //
//------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------
#ifndef _JMP3_ #ifndef _JMP3_
#define _JMP3_ #define _JMP3_
#include <string> #include <string>
class JMP3 class JMP3
{ {
protected: protected:
static bool loadModules(); static bool loadModules();
int m_volume; int m_volume;
int m_samplesPlayed; int m_samplesPlayed;
int m_inBufferSize, m_outBufferSize; int m_inBufferSize, m_outBufferSize;
char m_inBuffer[16*1024]; // ? char m_inBuffer[16*1024] __attribute__((aligned(64))); // ?
short m_outBuffer[16*(1152/2)]; //? short m_outBuffer[16*(1152/2)] __attribute__((aligned(64))); //?
int m_numChannels; int m_numChannels;
int m_samplingRate; int m_samplingRate;
bool m_loop; bool m_loop;
int m_lastDecoded; int m_lastDecoded;
int m_playTime; int m_playTime;
public: public:
int m_paused; int m_paused;
int m_channel; int m_channel;
int m_mp3Handle; int m_mp3Handle;
int m_fileHandle; int m_fileHandle;
int m_fileSize; int m_fileSize;
char m_fileName[256]; char m_fileName[256];
static JMP3* mInstance; static JMP3* mInstance;
JMP3(const std::string& filename, int inBufferSize= 16*1024, int outBufferSize =16*(1152/2)); JMP3();
~JMP3(); ~JMP3();
static void init(); static void init();
bool fillBuffers(); bool fillBuffers();
bool load(const std::string& filename, int inBufferSize, int outBufferSize); bool load(const std::string& filename, int inBufferSize = 16*1024, int outBufferSize = 16 * (1152/2));
bool unload(); bool unload();
bool update(); bool update();
bool play(); bool play();
bool pause(); bool pause();
bool setLoop(bool loop); bool setLoop(bool loop);
int setVolume(int volume); int setVolume(int volume);
int playTime() const; int playTime() const;
int playTimeMinutes(); int playTimeMinutes();
int playTimeSeconds(); int playTimeSeconds();
}; };
#endif #endif
+41 -48
View File
@@ -18,9 +18,8 @@ void JMP3::init() {
loadModules(); loadModules();
} }
JMP3::JMP3(const std::string& filename, int inBufferSize, int outBufferSize) : JMP3::JMP3() :
m_volume(PSP_AUDIO_VOLUME_MAX), m_samplesPlayed(0), m_paused(true) { m_volume(PSP_AUDIO_VOLUME_MAX), m_samplesPlayed(0), m_paused(true) {
load(filename, inBufferSize,outBufferSize);
} }
JMP3::~JMP3() { JMP3::~JMP3() {
@@ -50,21 +49,21 @@ bool JMP3::fillBuffers() {
if (ret < 0) if (ret < 0)
return false; return false;
if (sceIoLseek32(m_fileHandle, pos, SEEK_SET) < 0) { if (sceIoLseek32(m_fileHandle, pos, SEEK_SET) < 0) {
// Re-open the file because file handel can be invalidated by suspend/resume. // Re-open the file because file handel can be invalidated by suspend/resume.
sceIoClose(m_fileHandle); sceIoClose(m_fileHandle);
m_fileHandle = sceIoOpen(m_fileName, PSP_O_RDONLY, 0777); m_fileHandle = sceIoOpen(m_fileName, PSP_O_RDONLY, 0777);
if (m_fileHandle < 0) if (m_fileHandle < 0)
return false; return false;
if (sceIoLseek32(m_fileHandle, 0, SEEK_END) != m_fileSize if (sceIoLseek32(m_fileHandle, 0, SEEK_END) != m_fileSize
|| sceIoLseek32(m_fileHandle, pos, SEEK_SET) < 0) { || sceIoLseek32(m_fileHandle, pos, SEEK_SET) < 0) {
sceIoClose(m_fileHandle); sceIoClose(m_fileHandle);
m_fileHandle = -1; m_fileHandle = -1;
return false; return false;
} }
} }
int readLength = sceIoRead(m_fileHandle, dest, length); int readLength = sceIoRead(m_fileHandle, dest, length);
if (readLength < 0) if (readLength < 0)
return false; return false;
@@ -88,35 +87,29 @@ bool JMP3::load(const std::string& filename, int inBufferSize, int outBufferSize
// return false; // return false;
m_fileHandle = sceIoOpen(filename.c_str(), PSP_O_RDONLY, 0777); m_fileHandle = sceIoOpen(filename.c_str(), PSP_O_RDONLY, 0777);
if (m_fileHandle < 0) if (m_fileHandle < 0)
return false; return false;
// Memorise the full path for reloading with decode thread. // Memorise the full path for reloading with decode thread.
if ( getcwd(m_fileName, sizeof(m_fileName)) ){ if ( getcwd(m_fileName, sizeof(m_fileName)) ){
int len = strnlen(m_fileName, sizeof(m_fileName)); int len = strnlen(m_fileName, sizeof(m_fileName));
if (len + filename.size() <= sizeof(m_fileName) - 2){ if (len + filename.size() <= sizeof(m_fileName) - 2){
m_fileName[len++] = '/'; m_fileName[len++] = '/';
strcpy(m_fileName + len, filename.c_str()); strcpy(m_fileName + len, filename.c_str());
}else{ }else{
m_fileName[0] = NULL; m_fileName[0] = NULL;
} }
} }
int ret = sceMp3InitResource(); int ret = sceMp3InitResource();
if (ret < 0) if (ret < 0)
return false; return false;
SceMp3InitArg initArgs; SceMp3InitArg initArgs;
int fileSize = sceIoLseek32(m_fileHandle, 0, SEEK_END); int fileSize = sceIoLseek32(m_fileHandle, 0, SEEK_END);
sceIoLseek32(m_fileHandle, 0, SEEK_SET); sceIoLseek32(m_fileHandle, 0, SEEK_SET);
m_fileSize = fileSize; m_fileSize = fileSize;
unsigned char* testbuffer = new unsigned char[7456];
sceIoRead(m_fileHandle, testbuffer, 7456);
delete testbuffer;
initArgs.unk1 = 0; initArgs.unk1 = 0;
initArgs.unk2 = 0; initArgs.unk2 = 0;
@@ -165,8 +158,8 @@ bool JMP3::unload() {
} }
bool JMP3::update() { bool JMP3::update() {
int retry = 8;//FIXME:magic number int retry = 8;//FIXME:magic number
JMP3_update_start: JMP3_update_start:
if (!m_paused) { if (!m_paused) {
if (sceMp3CheckStreamDataNeeded(m_mp3Handle) > 0) { if (sceMp3CheckStreamDataNeeded(m_mp3Handle) > 0) {
@@ -190,11 +183,11 @@ bool JMP3::update() {
// Okay, let's see if we can't get something outputted :/ // Okay, let's see if we can't get something outputted :/
if (numDecoded == 0 || ((unsigned)numDecoded == 0x80671402)) { if (numDecoded == 0 || ((unsigned)numDecoded == 0x80671402)) {
if (retry-- > 0){ if (retry-- > 0){
//give me a recovery chance after suspend/resume... //give me a recovery chance after suspend/resume...
sceKernelDelayThread(1); sceKernelDelayThread(1);
goto JMP3_update_start; goto JMP3_update_start;
} }
sceMp3ResetPlayPosition(m_mp3Handle); sceMp3ResetPlayPosition(m_mp3Handle);
if (!m_loop) if (!m_loop)
@@ -212,7 +205,7 @@ bool JMP3::update() {
// Output // Output
m_samplesPlayed += sceAudioSRCOutputBlocking(m_volume, tempBuffer); m_samplesPlayed += sceAudioSRCOutputBlocking(m_volume, tempBuffer);
m_playTime = (m_samplingRate > 0) ? (m_samplesPlayed / (m_samplingRate/1000)) : 0; m_playTime = (m_samplingRate > 0) ? (m_samplesPlayed / (m_samplingRate/1000)) : 0;
m_lastDecoded = numDecoded; m_lastDecoded = numDecoded;
} }
} }
+179 -174
View File
@@ -1,174 +1,179 @@
//------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------
// //
// JGE is a hardware accelerated 2D game SDK for PSP/Windows. // JGE is a hardware accelerated 2D game SDK for PSP/Windows.
// //
// Licensed under the BSD license, see LICENSE in JGE root for details. // Licensed under the BSD license, see LICENSE in JGE root for details.
// //
// Copyright (c) 2007 James Hui (a.k.a. Dr.Watson) <jhkhui@gmail.com> // Copyright (c) 2007 James Hui (a.k.a. Dr.Watson) <jhkhui@gmail.com>
// //
//------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------
#include "../include/JSoundSystem.h" #include "../include/JSoundSystem.h"
#include "../include/JAudio.h" #include "../include/JAudio.h"
#include "../include/JMP3.h" #include "../include/JMP3.h"
#include <string> #include <string>
using std::string; using std::string;
JMusic::JMusic() JMusic::JMusic()
{ {
mTrack = NULL; mTrack = NULL;
} }
JMusic::~JMusic() JMusic::~JMusic()
{ {
JSoundSystem::GetInstance()->StopMusic(this); JSoundSystem::GetInstance()->StopMusic(this);
if (mTrack) if (mTrack)
delete mTrack; delete mTrack;
} }
void JMusic::Update(){ void JMusic::Update(){
} }
int JMusic::getPlayTime(){ int JMusic::getPlayTime(){
if (mTrack) return mTrack->playTime(); if (mTrack) return mTrack->playTime();
return 0; return 0;
} }
JSample::JSample() JSample::JSample()
{ {
mSample = NULL; mSample = NULL;
} }
JSample::~JSample() JSample::~JSample()
{ {
if (mSample) if (mSample)
releaseWaveData(mSample); releaseWaveData(mSample);
} }
JSoundSystem* JSoundSystem::mInstance = NULL; JSoundSystem* JSoundSystem::mInstance = NULL;
JSoundSystem* JSoundSystem::GetInstance() JSoundSystem* JSoundSystem::GetInstance()
{ {
if (mInstance == NULL) if (mInstance == NULL)
{ {
mInstance = new JSoundSystem(); mInstance = new JSoundSystem();
mInstance->InitSoundSystem(); mInstance->InitSoundSystem();
} }
return mInstance; return mInstance;
} }
void JSoundSystem::Destroy() void JSoundSystem::Destroy()
{ {
if (mInstance) if (mInstance)
{ {
mInstance->DestroySoundSystem(); mInstance->DestroySoundSystem();
delete mInstance; delete mInstance;
mInstance = NULL; mInstance = NULL;
} }
} }
JSoundSystem::JSoundSystem() JSoundSystem::JSoundSystem()
{ {
} }
JSoundSystem::~JSoundSystem() JSoundSystem::~JSoundSystem()
{ {
} }
void JSoundSystem::InitSoundSystem() void JSoundSystem::InitSoundSystem()
{ {
audioInit(); audioInit();
} }
void JSoundSystem::DestroySoundSystem() void JSoundSystem::DestroySoundSystem()
{ {
audioDestroy(); audioDestroy();
} }
JMusic *JSoundSystem::LoadMusic(const char *fileName) JMusic *JSoundSystem::LoadMusic(const char *fileName)
{ {
#ifdef RESPATH #ifdef RESPATH
string s = RESPATH"/"; string s = RESPATH"/";
#else #else
string s = "Res/"; string s = "Res/";
#endif #endif
s.append(fileName); s.append(fileName);
JMusic *music = new JMusic(); JMusic *music = new JMusic();
if (music) if (music)
{ {
music->mTrack = new JMP3(s); music->mTrack = new JMP3();
} if (!music->mTrack->load(s))
JMP3::mInstance = music->mTrack; {
return music; free(music->mTrack);
} music->mTrack = NULL;
}
}
JSample *JSoundSystem::LoadSample(const char *fileName) JMP3::mInstance = music->mTrack;
{ return music;
char s[strlen(fileName)+1]; }
strcpy(s, fileName);
JSample *sample = new JSample(); JSample *JSoundSystem::LoadSample(const char *fileName)
if (sample) {
{ char s[strlen(fileName)+1];
sample->mSample = new WAVDATA; strcpy(s, fileName);
loadWaveData(sample->mSample, s, 1);
} JSample *sample = new JSample();
if (sample)
return sample; {
} sample->mSample = new WAVDATA;
loadWaveData(sample->mSample, s, 1);
}
return sample;
void JSoundSystem::PlayMusic(JMusic *music, bool looping) }
{
if (music->mTrack)
PlayMP3(music->mTrack, looping);
} void JSoundSystem::PlayMusic(JMusic *music, bool looping)
{
void JSoundSystem::PlaySample(JSample *sample) if (music->mTrack)
{ PlayMP3(music->mTrack, looping);
}
playWaveMem(sample->mSample, 0);
}
void JSoundSystem::PlaySample(JSample *sample)
{
void JSoundSystem::SetVolume(int volume)
{ playWaveMem(sample->mSample, 0);
JMP3 * mp3 = JMP3::mInstance; }
if (mp3) mp3->setVolume(volume);
}
void JSoundSystem::SetVolume(int volume)
{
void JSoundSystem::StopMusic(JMusic *music) JMP3 * mp3 = JMP3::mInstance;
{ if (mp3) mp3->setVolume(volume);
StopMP3(); }
}
void JSoundSystem::StopMusic(JMusic *music)
{
void JSoundSystem::ResumeMusic(JMusic *music) StopMP3();
{
ResumeMP3(music->mTrack); }
}
void JSoundSystem::ResumeMusic(JMusic *music)
{
ResumeMP3(music->mTrack);
}