* 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

View File

@@ -1,61 +1,61 @@
//-------------------------------------------------------------------------------------
//
// JGE is a hardware accelerated 2D game SDK for PSP/Windows.
//
// 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) 2008 Alexander Berl <raphael@fx-world.org>
// Copyright (c) 2008 WilLoW :--) <wagic.the.homebrew@gmail.com>
//
//-------------------------------------------------------------------------------------
#ifndef _JMP3_
#define _JMP3_
#include <string>
class JMP3
{
protected:
static bool loadModules();
int m_volume;
int m_samplesPlayed;
int m_inBufferSize, m_outBufferSize;
char m_inBuffer[16*1024]; // ?
short m_outBuffer[16*(1152/2)]; //?
int m_numChannels;
int m_samplingRate;
bool m_loop;
int m_lastDecoded;
int m_playTime;
public:
int m_paused;
int m_channel;
int m_mp3Handle;
int m_fileHandle;
int m_fileSize;
char m_fileName[256];
static JMP3* mInstance;
JMP3(const std::string& filename, int inBufferSize= 16*1024, int outBufferSize =16*(1152/2));
~JMP3();
static void init();
bool fillBuffers();
bool load(const std::string& filename, int inBufferSize, int outBufferSize);
bool unload();
bool update();
bool play();
bool pause();
bool setLoop(bool loop);
int setVolume(int volume);
int playTime() const;
int playTimeMinutes();
int playTimeSeconds();
};
#endif
//-------------------------------------------------------------------------------------
//
// JGE is a hardware accelerated 2D game SDK for PSP/Windows.
//
// 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) 2008 Alexander Berl <raphael@fx-world.org>
// Copyright (c) 2008 WilLoW :--) <wagic.the.homebrew@gmail.com>
//
//-------------------------------------------------------------------------------------
#ifndef _JMP3_
#define _JMP3_
#include <string>
class JMP3
{
protected:
static bool loadModules();
int m_volume;
int m_samplesPlayed;
int m_inBufferSize, m_outBufferSize;
char m_inBuffer[16*1024] __attribute__((aligned(64))); // ?
short m_outBuffer[16*(1152/2)] __attribute__((aligned(64))); //?
int m_numChannels;
int m_samplingRate;
bool m_loop;
int m_lastDecoded;
int m_playTime;
public:
int m_paused;
int m_channel;
int m_mp3Handle;
int m_fileHandle;
int m_fileSize;
char m_fileName[256];
static JMP3* mInstance;
JMP3();
~JMP3();
static void init();
bool fillBuffers();
bool load(const std::string& filename, int inBufferSize = 16*1024, int outBufferSize = 16 * (1152/2));
bool unload();
bool update();
bool play();
bool pause();
bool setLoop(bool loop);
int setVolume(int volume);
int playTime() const;
int playTimeMinutes();
int playTimeSeconds();
};
#endif

View File

@@ -18,9 +18,8 @@ void JMP3::init() {
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) {
load(filename, inBufferSize,outBufferSize);
}
JMP3::~JMP3() {
@@ -50,21 +49,21 @@ bool JMP3::fillBuffers() {
if (ret < 0)
return false;
if (sceIoLseek32(m_fileHandle, pos, SEEK_SET) < 0) {
// Re-open the file because file handel can be invalidated by suspend/resume.
sceIoClose(m_fileHandle);
m_fileHandle = sceIoOpen(m_fileName, PSP_O_RDONLY, 0777);
if (m_fileHandle < 0)
return false;
if (sceIoLseek32(m_fileHandle, 0, SEEK_END) != m_fileSize
|| sceIoLseek32(m_fileHandle, pos, SEEK_SET) < 0) {
sceIoClose(m_fileHandle);
m_fileHandle = -1;
return false;
}
}
int readLength = sceIoRead(m_fileHandle, dest, length);
if (sceIoLseek32(m_fileHandle, pos, SEEK_SET) < 0) {
// Re-open the file because file handel can be invalidated by suspend/resume.
sceIoClose(m_fileHandle);
m_fileHandle = sceIoOpen(m_fileName, PSP_O_RDONLY, 0777);
if (m_fileHandle < 0)
return false;
if (sceIoLseek32(m_fileHandle, 0, SEEK_END) != m_fileSize
|| sceIoLseek32(m_fileHandle, pos, SEEK_SET) < 0) {
sceIoClose(m_fileHandle);
m_fileHandle = -1;
return false;
}
}
int readLength = sceIoRead(m_fileHandle, dest, length);
if (readLength < 0)
return false;
@@ -88,35 +87,29 @@ bool JMP3::load(const std::string& filename, int inBufferSize, int outBufferSize
// return false;
m_fileHandle = sceIoOpen(filename.c_str(), PSP_O_RDONLY, 0777);
if (m_fileHandle < 0)
return false;
// Memorise the full path for reloading with decode thread.
if ( getcwd(m_fileName, sizeof(m_fileName)) ){
int len = strnlen(m_fileName, sizeof(m_fileName));
if (len + filename.size() <= sizeof(m_fileName) - 2){
m_fileName[len++] = '/';
strcpy(m_fileName + len, filename.c_str());
}else{
m_fileName[0] = NULL;
}
}
int ret = sceMp3InitResource();
if (ret < 0)
if (m_fileHandle < 0)
return false;
// Memorise the full path for reloading with decode thread.
if ( getcwd(m_fileName, sizeof(m_fileName)) ){
int len = strnlen(m_fileName, sizeof(m_fileName));
if (len + filename.size() <= sizeof(m_fileName) - 2){
m_fileName[len++] = '/';
strcpy(m_fileName + len, filename.c_str());
}else{
m_fileName[0] = NULL;
}
}
int ret = sceMp3InitResource();
if (ret < 0)
return false;
SceMp3InitArg initArgs;
int fileSize = sceIoLseek32(m_fileHandle, 0, SEEK_END);
sceIoLseek32(m_fileHandle, 0, SEEK_SET);
m_fileSize = fileSize;
unsigned char* testbuffer = new unsigned char[7456];
sceIoRead(m_fileHandle, testbuffer, 7456);
delete testbuffer;
m_fileSize = fileSize;
initArgs.unk1 = 0;
initArgs.unk2 = 0;
@@ -165,8 +158,8 @@ bool JMP3::unload() {
}
bool JMP3::update() {
int retry = 8;//FIXME:magic number
JMP3_update_start:
int retry = 8;//FIXME:magic number
JMP3_update_start:
if (!m_paused) {
if (sceMp3CheckStreamDataNeeded(m_mp3Handle) > 0) {
@@ -190,11 +183,11 @@ bool JMP3::update() {
// Okay, let's see if we can't get something outputted :/
if (numDecoded == 0 || ((unsigned)numDecoded == 0x80671402)) {
if (retry-- > 0){
//give me a recovery chance after suspend/resume...
sceKernelDelayThread(1);
goto JMP3_update_start;
}
if (retry-- > 0){
//give me a recovery chance after suspend/resume...
sceKernelDelayThread(1);
goto JMP3_update_start;
}
sceMp3ResetPlayPosition(m_mp3Handle);
if (!m_loop)
@@ -212,7 +205,7 @@ bool JMP3::update() {
// Output
m_samplesPlayed += sceAudioSRCOutputBlocking(m_volume, tempBuffer);
m_playTime = (m_samplingRate > 0) ? (m_samplesPlayed / (m_samplingRate/1000)) : 0;
m_lastDecoded = numDecoded;
m_lastDecoded = numDecoded;
}
}

View File

@@ -1,174 +1,179 @@
//-------------------------------------------------------------------------------------
//
// JGE is a hardware accelerated 2D game SDK for PSP/Windows.
//
// 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>
//
//-------------------------------------------------------------------------------------
#include "../include/JSoundSystem.h"
#include "../include/JAudio.h"
#include "../include/JMP3.h"
#include <string>
using std::string;
JMusic::JMusic()
{
mTrack = NULL;
}
JMusic::~JMusic()
{
JSoundSystem::GetInstance()->StopMusic(this);
if (mTrack)
delete mTrack;
}
void JMusic::Update(){
}
int JMusic::getPlayTime(){
if (mTrack) return mTrack->playTime();
return 0;
}
JSample::JSample()
{
mSample = NULL;
}
JSample::~JSample()
{
if (mSample)
releaseWaveData(mSample);
}
JSoundSystem* JSoundSystem::mInstance = NULL;
JSoundSystem* JSoundSystem::GetInstance()
{
if (mInstance == NULL)
{
mInstance = new JSoundSystem();
mInstance->InitSoundSystem();
}
return mInstance;
}
void JSoundSystem::Destroy()
{
if (mInstance)
{
mInstance->DestroySoundSystem();
delete mInstance;
mInstance = NULL;
}
}
JSoundSystem::JSoundSystem()
{
}
JSoundSystem::~JSoundSystem()
{
}
void JSoundSystem::InitSoundSystem()
{
audioInit();
}
void JSoundSystem::DestroySoundSystem()
{
audioDestroy();
}
JMusic *JSoundSystem::LoadMusic(const char *fileName)
{
#ifdef RESPATH
string s = RESPATH"/";
#else
string s = "Res/";
#endif
s.append(fileName);
JMusic *music = new JMusic();
if (music)
{
music->mTrack = new JMP3(s);
}
JMP3::mInstance = music->mTrack;
return music;
}
JSample *JSoundSystem::LoadSample(const char *fileName)
{
char s[strlen(fileName)+1];
strcpy(s, fileName);
JSample *sample = new JSample();
if (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::PlaySample(JSample *sample)
{
playWaveMem(sample->mSample, 0);
}
void JSoundSystem::SetVolume(int volume)
{
JMP3 * mp3 = JMP3::mInstance;
if (mp3) mp3->setVolume(volume);
}
void JSoundSystem::StopMusic(JMusic *music)
{
StopMP3();
}
void JSoundSystem::ResumeMusic(JMusic *music)
{
ResumeMP3(music->mTrack);
}
//-------------------------------------------------------------------------------------
//
// JGE is a hardware accelerated 2D game SDK for PSP/Windows.
//
// 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>
//
//-------------------------------------------------------------------------------------
#include "../include/JSoundSystem.h"
#include "../include/JAudio.h"
#include "../include/JMP3.h"
#include <string>
using std::string;
JMusic::JMusic()
{
mTrack = NULL;
}
JMusic::~JMusic()
{
JSoundSystem::GetInstance()->StopMusic(this);
if (mTrack)
delete mTrack;
}
void JMusic::Update(){
}
int JMusic::getPlayTime(){
if (mTrack) return mTrack->playTime();
return 0;
}
JSample::JSample()
{
mSample = NULL;
}
JSample::~JSample()
{
if (mSample)
releaseWaveData(mSample);
}
JSoundSystem* JSoundSystem::mInstance = NULL;
JSoundSystem* JSoundSystem::GetInstance()
{
if (mInstance == NULL)
{
mInstance = new JSoundSystem();
mInstance->InitSoundSystem();
}
return mInstance;
}
void JSoundSystem::Destroy()
{
if (mInstance)
{
mInstance->DestroySoundSystem();
delete mInstance;
mInstance = NULL;
}
}
JSoundSystem::JSoundSystem()
{
}
JSoundSystem::~JSoundSystem()
{
}
void JSoundSystem::InitSoundSystem()
{
audioInit();
}
void JSoundSystem::DestroySoundSystem()
{
audioDestroy();
}
JMusic *JSoundSystem::LoadMusic(const char *fileName)
{
#ifdef RESPATH
string s = RESPATH"/";
#else
string s = "Res/";
#endif
s.append(fileName);
JMusic *music = new JMusic();
if (music)
{
music->mTrack = new JMP3();
if (!music->mTrack->load(s))
{
free(music->mTrack);
music->mTrack = NULL;
}
}
JMP3::mInstance = music->mTrack;
return music;
}
JSample *JSoundSystem::LoadSample(const char *fileName)
{
char s[strlen(fileName)+1];
strcpy(s, fileName);
JSample *sample = new JSample();
if (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::PlaySample(JSample *sample)
{
playWaveMem(sample->mSample, 0);
}
void JSoundSystem::SetVolume(int volume)
{
JMP3 * mp3 = JMP3::mInstance;
if (mp3) mp3->setVolume(volume);
}
void JSoundSystem::StopMusic(JMusic *music)
{
StopMP3();
}
void JSoundSystem::ResumeMusic(JMusic *music)
{
ResumeMP3(music->mTrack);
}