2022-01-03 09:51:53 +00:00
|
|
|
//
|
|
|
|
// libvgmDecoder.mm
|
|
|
|
// libvgmPlayer
|
|
|
|
//
|
|
|
|
// Created by Christopher Snowhill on 1/02/22.
|
2023-02-06 00:46:31 +00:00
|
|
|
// Copyright 2022-2023 __LoSnoCo__. All rights reserved.
|
2022-01-03 09:51:53 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
#import "libvgmDecoder.h"
|
|
|
|
|
|
|
|
#import "Logging.h"
|
|
|
|
|
|
|
|
#import "PlaylistController.h"
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
#import <libvgm/emu/EmuCores.h>
|
2022-01-03 09:51:53 +00:00
|
|
|
#import <libvgm/emu/Resampler.h>
|
|
|
|
#import <libvgm/emu/SoundDevs.h>
|
2022-02-07 05:49:27 +00:00
|
|
|
#import <libvgm/player/droplayer.hpp>
|
|
|
|
#import <libvgm/player/gymplayer.hpp>
|
|
|
|
#import <libvgm/player/s98player.hpp>
|
|
|
|
#import <libvgm/player/vgmplayer.hpp>
|
|
|
|
#import <libvgm/utils/FileLoader.h>
|
|
|
|
#import <libvgm/utils/MemoryLoader.h>
|
2022-01-03 09:51:53 +00:00
|
|
|
|
|
|
|
@implementation libvgmDecoder
|
|
|
|
|
|
|
|
#ifdef _DEBUG
|
|
|
|
const int logLevel = DEVLOG_DEBUG;
|
|
|
|
#else
|
|
|
|
const int logLevel = DEVLOG_INFO;
|
|
|
|
#endif
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
static UINT8 FilePlayCallback(PlayerBase* player, void* userParam, UINT8 evtType, void* evtParam) {
|
|
|
|
libvgmDecoder* decoder = (__bridge libvgmDecoder*)(userParam);
|
|
|
|
switch(evtType) {
|
|
|
|
case PLREVT_START:
|
|
|
|
// printf("Playback started.\n");
|
|
|
|
break;
|
|
|
|
case PLREVT_STOP:
|
|
|
|
// printf("Playback stopped.\n");
|
|
|
|
break;
|
|
|
|
case PLREVT_LOOP: {
|
|
|
|
// UINT32* curLoop = (UINT32*)evtParam;
|
|
|
|
// if (player->GetState() & PLAYSTATE_SEEK)
|
2022-01-03 09:51:53 +00:00
|
|
|
// break;
|
2022-02-07 05:49:27 +00:00
|
|
|
} break;
|
|
|
|
case PLREVT_END:
|
|
|
|
if([decoder trackEnded])
|
|
|
|
break;
|
|
|
|
[decoder setTrackEnded:YES];
|
2022-01-03 09:51:53 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0x00;
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "yrw801.h"
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
static DATA_LOADER* RequestFileCallback(void* userParam, PlayerBase* player, const char* fileName) {
|
2022-01-03 09:51:53 +00:00
|
|
|
DATA_LOADER* dLoad;
|
2022-02-07 05:49:27 +00:00
|
|
|
if(strcmp(fileName, "yrw801.rom") == 0) {
|
2022-01-03 09:51:53 +00:00
|
|
|
dLoad = MemoryLoader_Init(yrw801_rom, sizeof(yrw801_rom));
|
2022-02-07 05:49:27 +00:00
|
|
|
} else {
|
2022-01-03 09:51:53 +00:00
|
|
|
dLoad = FileLoader_Init(fileName);
|
|
|
|
}
|
|
|
|
UINT8 retVal = DataLoader_Load(dLoad);
|
2022-02-07 05:49:27 +00:00
|
|
|
if(!retVal)
|
2022-01-03 09:51:53 +00:00
|
|
|
return dLoad;
|
|
|
|
DataLoader_Deinit(dLoad);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
static const char* LogLevel2Str(UINT8 level) {
|
|
|
|
static const char* LVL_NAMES[6] = { " ??? ", "Error", "Warn ", "Info ", "Debug", "Trace" };
|
|
|
|
if(level >= (sizeof(LVL_NAMES) / sizeof(LVL_NAMES[0])))
|
2022-01-03 09:51:53 +00:00
|
|
|
level = 0;
|
|
|
|
return LVL_NAMES[level];
|
|
|
|
}
|
|
|
|
|
|
|
|
static void PlayerLogCallback(void* userParam, PlayerBase* player, UINT8 level, UINT8 srcType,
|
2022-02-07 05:49:27 +00:00
|
|
|
const char* srcTag, const char* message) {
|
|
|
|
if(level > logLevel)
|
|
|
|
return; // don't print messages with higher verbosity than current log level
|
|
|
|
if(srcType == PLRLOGSRC_PLR) {
|
2022-01-08 03:07:46 +00:00
|
|
|
ALog(@"[%s] %s: %s", LogLevel2Str(level), player->GetPlayerName(), message);
|
2022-02-07 05:49:27 +00:00
|
|
|
} else {
|
2022-01-08 03:07:46 +00:00
|
|
|
ALog(@"[%s] %s %s: %s", LogLevel2Str(level), player->GetPlayerName(), srcTag, message);
|
2022-02-07 05:49:27 +00:00
|
|
|
}
|
2022-01-03 09:51:53 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const int numChannels = 2;
|
|
|
|
const int numBitsPerSample = 24;
|
|
|
|
const int smplAlloc = 2048;
|
|
|
|
const int masterVol = 0x10000; // Fixed point 16.16
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (id)init {
|
|
|
|
self = [super init];
|
|
|
|
if(self) {
|
2022-01-03 09:51:53 +00:00
|
|
|
fileData = NULL;
|
|
|
|
dLoad = NULL;
|
2022-02-07 05:49:27 +00:00
|
|
|
mainPlr = NULL;
|
|
|
|
}
|
|
|
|
return self;
|
2022-01-03 09:51:53 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (BOOL)open:(id<CogSource>)s {
|
2022-01-03 09:51:53 +00:00
|
|
|
[self setSource:s];
|
2022-02-07 05:49:27 +00:00
|
|
|
|
|
|
|
// We need file-size to use GME
|
|
|
|
if(![source seekable]) {
|
2022-01-03 09:51:53 +00:00
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
2022-07-02 05:06:08 +00:00
|
|
|
sampleRate = [[[[NSUserDefaultsController sharedUserDefaultsController] defaults] valueForKey:@"synthSampleRate"] doubleValue];
|
|
|
|
if(sampleRate < 8000.0) {
|
|
|
|
sampleRate = 44100.0;
|
|
|
|
} else if(sampleRate > 192000.0) {
|
|
|
|
sampleRate = 192000.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
loopCount = [[[[NSUserDefaultsController sharedUserDefaultsController] defaults] valueForKey:@"synthDefaultLoopCount"] longValue];
|
|
|
|
if(loopCount < 1) {
|
|
|
|
loopCount = 1;
|
|
|
|
} else if(loopCount > 10) {
|
|
|
|
loopCount = 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
fadeTime = [[[[NSUserDefaultsController sharedUserDefaultsController] defaults] valueForKey:@"synthDefaultFadeSeconds"] doubleValue];
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
BOOL repeatOne = IsRepeatOneSet();
|
2022-07-02 05:06:08 +00:00
|
|
|
uint32_t maxLoops = repeatOne ? 0 : (uint32_t)loopCount;
|
2022-01-03 09:51:53 +00:00
|
|
|
|
|
|
|
mainPlr = new PlayerA;
|
|
|
|
mainPlr->RegisterPlayerEngine(new VGMPlayer);
|
|
|
|
mainPlr->RegisterPlayerEngine(new S98Player);
|
|
|
|
mainPlr->RegisterPlayerEngine(new DROPlayer);
|
|
|
|
mainPlr->RegisterPlayerEngine(new GYMPlayer);
|
2022-02-07 05:49:27 +00:00
|
|
|
mainPlr->SetEventCallback(FilePlayCallback, (__bridge void*)(self));
|
2022-01-03 09:51:53 +00:00
|
|
|
mainPlr->SetFileReqCallback(RequestFileCallback, NULL);
|
|
|
|
mainPlr->SetLogCallback(PlayerLogCallback, NULL);
|
|
|
|
{
|
|
|
|
PlayerA::Config pCfg = mainPlr->GetConfiguration();
|
|
|
|
pCfg.masterVol = masterVol;
|
|
|
|
pCfg.loopCount = maxLoops;
|
2022-07-02 05:06:08 +00:00
|
|
|
pCfg.fadeSmpls = (int)ceil(sampleRate * fadeTime); // fade over configured duration
|
2022-02-07 05:49:27 +00:00
|
|
|
pCfg.endSilenceSmpls = sampleRate / 2; // 0.5 seconds of silence at the end
|
2022-01-03 09:51:53 +00:00
|
|
|
pCfg.pbSpeed = 1.0;
|
|
|
|
mainPlr->SetConfiguration(pCfg);
|
|
|
|
}
|
2022-07-02 05:06:08 +00:00
|
|
|
mainPlr->SetOutputSettings((int)ceil(sampleRate), numChannels, numBitsPerSample, smplAlloc);
|
2022-01-03 09:51:53 +00:00
|
|
|
|
|
|
|
[source seek:0 whence:SEEK_END];
|
|
|
|
size_t size = [source tell];
|
|
|
|
[source seek:0 whence:SEEK_SET];
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
fileData = (UINT8*)malloc(size);
|
|
|
|
if(!fileData)
|
2022-01-03 09:51:53 +00:00
|
|
|
return NO;
|
|
|
|
|
|
|
|
size_t bytesRead = [source read:fileData amount:size];
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
if(bytesRead != size)
|
2022-01-03 09:51:53 +00:00
|
|
|
return NO;
|
|
|
|
|
|
|
|
dLoad = MemoryLoader_Init(fileData, (unsigned int)size);
|
2022-02-07 05:49:27 +00:00
|
|
|
if(!dLoad)
|
2022-01-03 09:51:53 +00:00
|
|
|
return NO;
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
DataLoader_SetPreloadBytes(dLoad, 0x100);
|
|
|
|
if(DataLoader_Load(dLoad))
|
2022-01-03 09:51:53 +00:00
|
|
|
return NO;
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
if(mainPlr->LoadFile(dLoad))
|
2022-01-03 09:51:53 +00:00
|
|
|
return NO;
|
|
|
|
|
|
|
|
PlayerBase* player = mainPlr->GetPlayer();
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
mainPlr->SetLoopCount(maxLoops);
|
|
|
|
if(player->GetPlayerType() == FCC_VGM) {
|
|
|
|
VGMPlayer* vgmplay = dynamic_cast<VGMPlayer*>(player);
|
|
|
|
mainPlr->SetLoopCount(vgmplay->GetModifiedLoopCount(maxLoops));
|
|
|
|
}
|
2022-01-03 09:51:53 +00:00
|
|
|
|
|
|
|
length = player->Tick2Second(player->GetTotalTicks()) * sampleRate;
|
|
|
|
|
|
|
|
[self setTrackEnded:NO];
|
2022-02-07 05:49:27 +00:00
|
|
|
|
|
|
|
mainPlr->Start();
|
2022-01-03 09:51:53 +00:00
|
|
|
|
|
|
|
[self willChangeValueForKey:@"properties"];
|
|
|
|
[self didChangeValueForKey:@"properties"];
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2022-01-03 09:51:53 +00:00
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (NSDictionary*)properties {
|
2022-06-17 13:39:02 +00:00
|
|
|
return @{ @"bitrate": @(0),
|
|
|
|
@"sampleRate": @(sampleRate),
|
|
|
|
@"totalFrames": @(length),
|
|
|
|
@"bitsPerSample": @(numBitsPerSample),
|
|
|
|
@"channels": @(numChannels),
|
|
|
|
@"seekable": @(YES),
|
|
|
|
@"endian": @"host",
|
|
|
|
@"encoding": @"synthesized" };
|
2022-01-03 09:51:53 +00:00
|
|
|
}
|
|
|
|
|
2022-02-09 03:56:39 +00:00
|
|
|
- (NSDictionary *)metadata {
|
|
|
|
return @{};
|
|
|
|
}
|
|
|
|
|
2022-07-10 22:14:47 +00:00
|
|
|
- (AudioChunk*)readAudio {
|
2022-02-07 05:49:27 +00:00
|
|
|
if([self trackEnded])
|
2022-07-10 22:14:47 +00:00
|
|
|
return nil;
|
|
|
|
|
|
|
|
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];
|
|
|
|
|
|
|
|
void* buf = (void*)buffer;
|
2022-01-03 09:51:53 +00:00
|
|
|
|
|
|
|
BOOL repeatOne = IsRepeatOneSet();
|
2022-07-02 05:06:08 +00:00
|
|
|
uint32_t maxLoops = repeatOne ? 0 : (uint32_t)loopCount;
|
2022-01-03 09:51:53 +00:00
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
PlayerBase* player = mainPlr->GetPlayer();
|
2022-01-03 09:51:53 +00:00
|
|
|
mainPlr->SetLoopCount(maxLoops);
|
2022-02-07 05:49:27 +00:00
|
|
|
if(player->GetPlayerType() == FCC_VGM) {
|
2022-01-03 09:51:53 +00:00
|
|
|
VGMPlayer* vgmplay = dynamic_cast<VGMPlayer*>(player);
|
|
|
|
mainPlr->SetLoopCount(vgmplay->GetModifiedLoopCount(maxLoops));
|
|
|
|
}
|
2022-02-07 05:49:27 +00:00
|
|
|
|
|
|
|
UInt32 framesDone = 0;
|
|
|
|
|
|
|
|
while(framesDone < frames) {
|
|
|
|
UInt32 framesToDo = frames - framesDone;
|
|
|
|
if(framesToDo > smplAlloc)
|
|
|
|
framesToDo = smplAlloc;
|
|
|
|
|
|
|
|
int numSamples = framesToDo * numChannels * (numBitsPerSample / 8);
|
|
|
|
|
|
|
|
mainPlr->Render(numSamples, buf);
|
|
|
|
|
|
|
|
buf = (void*)(((uint8_t*)buf) + numSamples);
|
|
|
|
|
|
|
|
framesDone += framesToDo;
|
|
|
|
}
|
2022-01-03 09:51:53 +00:00
|
|
|
|
2022-07-10 22:14:47 +00:00
|
|
|
[chunk assignSamples:buffer frameCount:framesDone];
|
|
|
|
|
|
|
|
return chunk;
|
2022-01-03 09:51:53 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (long)seek:(long)frame {
|
|
|
|
[self setTrackEnded:NO];
|
|
|
|
|
2022-01-03 09:51:53 +00:00
|
|
|
mainPlr->Seek(PLAYPOS_SAMPLE, (unsigned int)frame);
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2022-01-03 09:51:53 +00:00
|
|
|
return frame;
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)close {
|
|
|
|
if(mainPlr) {
|
2022-01-03 09:51:53 +00:00
|
|
|
mainPlr->Stop();
|
|
|
|
mainPlr->UnloadFile();
|
|
|
|
|
|
|
|
delete mainPlr;
|
|
|
|
mainPlr = NULL;
|
|
|
|
}
|
2022-02-07 05:49:27 +00:00
|
|
|
if(dLoad) {
|
2022-01-03 09:51:53 +00:00
|
|
|
DataLoader_Deinit(dLoad);
|
|
|
|
dLoad = NULL;
|
|
|
|
}
|
2022-02-07 05:49:27 +00:00
|
|
|
if(fileData) {
|
2022-01-03 09:51:53 +00:00
|
|
|
free(fileData);
|
|
|
|
fileData = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)dealloc {
|
|
|
|
[self close];
|
2022-01-03 09:51:53 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
+ (NSArray*)fileTypes {
|
2022-01-19 02:12:57 +00:00
|
|
|
return @[@"vgm", @"vgz", @"s98", @"dro", @"gym"];
|
2022-01-03 09:51:53 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
+ (NSArray*)mimeTypes {
|
2022-01-03 09:51:53 +00:00
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
+ (float)priority {
|
|
|
|
return 1.25;
|
2022-01-03 09:51:53 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
+ (NSArray*)fileTypeAssociations {
|
|
|
|
NSMutableArray* ret = [[NSMutableArray alloc] init];
|
|
|
|
[ret addObject:@"libvgm Files"];
|
|
|
|
[ret addObject:@"vg.icns"];
|
|
|
|
[ret addObjectsFromArray:[self fileTypes]];
|
|
|
|
|
|
|
|
return @[[NSArray arrayWithArray:ret]];
|
2022-01-18 11:06:03 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)setSource:(id<CogSource>)s {
|
2022-01-03 09:51:53 +00:00
|
|
|
source = s;
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (id<CogSource>)source {
|
2022-01-03 09:51:53 +00:00
|
|
|
return source;
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (BOOL)trackEnded {
|
2022-01-03 09:51:53 +00:00
|
|
|
return trackEnded;
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)setTrackEnded:(BOOL)ended {
|
2022-01-03 09:51:53 +00:00
|
|
|
trackEnded = ended;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|