libvgm: Add exception handling, and move buffer
Move the player buffer off the stack, as well as adding exception handlers to the init, playback, and shutdown code. Signed-off-by: Christopher Snowhill <kode54@gmail.com>
This commit is contained in:
parent
0263367639
commit
5b51e664e2
2 changed files with 111 additions and 85 deletions
|
@ -19,6 +19,7 @@
|
|||
DATA_LOADER* dLoad;
|
||||
PlayerA* mainPlr;
|
||||
id<CogSource> source;
|
||||
UINT8* sampleBuffer;
|
||||
double sampleRate;
|
||||
long loopCount;
|
||||
double fadeTime;
|
||||
|
|
|
@ -24,6 +24,8 @@
|
|||
|
||||
@implementation libvgmDecoder
|
||||
|
||||
enum { bufferSize = 1024 };
|
||||
|
||||
#ifdef _DEBUG
|
||||
const int logLevel = DEVLOG_DEBUG;
|
||||
#else
|
||||
|
@ -99,6 +101,7 @@ const int masterVol = 0x10000; // Fixed point 16.16
|
|||
fileData = NULL;
|
||||
dLoad = NULL;
|
||||
mainPlr = NULL;
|
||||
sampleBuffer = NULL;
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
@ -130,6 +133,7 @@ const int masterVol = 0x10000; // Fixed point 16.16
|
|||
BOOL repeatOne = IsRepeatOneSet();
|
||||
uint32_t maxLoops = repeatOne ? 0 : (uint32_t)loopCount;
|
||||
|
||||
try {
|
||||
mainPlr = new PlayerA;
|
||||
mainPlr->RegisterPlayerEngine(new VGMPlayer);
|
||||
mainPlr->RegisterPlayerEngine(new S98Player);
|
||||
|
@ -186,6 +190,14 @@ const int masterVol = 0x10000; // Fixed point 16.16
|
|||
[self setTrackEnded:NO];
|
||||
|
||||
mainPlr->Start();
|
||||
} catch(std::exception& e) {
|
||||
ALog(@"Exception caught opening file: %s\n", e.what());
|
||||
return NO;
|
||||
}
|
||||
|
||||
sampleBuffer = (UINT8*) malloc(bufferSize * numChannels * (numBitsPerSample / 8));
|
||||
if(!sampleBuffer)
|
||||
return NO;
|
||||
|
||||
[self willChangeValueForKey:@"properties"];
|
||||
[self didChangeValueForKey:@"properties"];
|
||||
|
@ -215,15 +227,18 @@ const int masterVol = 0x10000; // Fixed point 16.16
|
|||
id audioChunkClass = NSClassFromString(@"AudioChunk");
|
||||
AudioChunk* chunk = [[audioChunkClass alloc] initWithProperties:[self properties]];
|
||||
|
||||
int frames = 1024;
|
||||
const size_t bytesPerFrame = [chunk format].mBytesPerFrame;
|
||||
uint8_t buffer[frames * bytesPerFrame];
|
||||
int frames = bufferSize;
|
||||
const int bytesPerFrame = [chunk format].mBytesPerFrame;
|
||||
|
||||
void* buf = (void*)buffer;
|
||||
void* buf = (void*)sampleBuffer;
|
||||
|
||||
BOOL repeatOne = IsRepeatOneSet();
|
||||
uint32_t maxLoops = repeatOne ? 0 : (uint32_t)loopCount;
|
||||
|
||||
double streamTimestamp;
|
||||
UInt32 framesDone = 0;
|
||||
|
||||
try {
|
||||
PlayerBase* player = mainPlr->GetPlayer();
|
||||
mainPlr->SetLoopCount(maxLoops);
|
||||
if(player->GetPlayerType() == FCC_VGM) {
|
||||
|
@ -231,31 +246,33 @@ const int masterVol = 0x10000; // Fixed point 16.16
|
|||
mainPlr->SetLoopCount(vgmplay->GetModifiedLoopCount(maxLoops));
|
||||
}
|
||||
|
||||
double streamTimestamp = mainPlr->GetCurTime(0);
|
||||
|
||||
UInt32 framesDone = 0;
|
||||
streamTimestamp = mainPlr->GetCurTime(0);
|
||||
|
||||
while(framesDone < frames) {
|
||||
UInt32 framesToDo = frames - framesDone;
|
||||
if(framesToDo > smplAlloc)
|
||||
framesToDo = smplAlloc;
|
||||
|
||||
int numSamples = framesToDo * numChannels * (numBitsPerSample / 8);
|
||||
int numSamples = framesToDo * bytesPerFrame;
|
||||
|
||||
UINT32 numRendered = mainPlr->Render(numSamples, buf);
|
||||
|
||||
buf = (void*)(((uint8_t*)buf) + numRendered);
|
||||
|
||||
UINT32 framesRendered = numRendered / (numChannels * (numBitsPerSample / 8));
|
||||
UINT32 framesRendered = numRendered / bytesPerFrame;
|
||||
|
||||
framesDone += framesRendered;
|
||||
|
||||
if(framesRendered < framesToDo)
|
||||
break;
|
||||
}
|
||||
} catch(std::exception& e) {
|
||||
ALog(@"Exception caught while playing track: %s\n", e.what());
|
||||
return nil;
|
||||
}
|
||||
|
||||
[chunk setStreamTimestamp:streamTimestamp];
|
||||
[chunk assignSamples:buffer frameCount:framesDone];
|
||||
[chunk assignSamples:sampleBuffer frameCount:framesDone];
|
||||
|
||||
return chunk;
|
||||
}
|
||||
|
@ -269,6 +286,11 @@ const int masterVol = 0x10000; // Fixed point 16.16
|
|||
}
|
||||
|
||||
- (void)close {
|
||||
if(sampleBuffer) {
|
||||
free(sampleBuffer);
|
||||
sampleBuffer = NULL;
|
||||
}
|
||||
try {
|
||||
if(mainPlr) {
|
||||
mainPlr->Stop();
|
||||
mainPlr->UnloadFile();
|
||||
|
@ -280,6 +302,9 @@ const int masterVol = 0x10000; // Fixed point 16.16
|
|||
DataLoader_Deinit(dLoad);
|
||||
dLoad = NULL;
|
||||
}
|
||||
} catch(std::exception& e) {
|
||||
ALog(@"Exception caught cleaning up player: %s\n", e.what());
|
||||
}
|
||||
if(fileData) {
|
||||
free(fileData);
|
||||
fileData = NULL;
|
||||
|
|
Loading…
Reference in a new issue