2022-01-03 09:51:53 +00:00
|
|
|
//
|
|
|
|
// libvgmDecoder.mm
|
|
|
|
// libvgmPlayer
|
|
|
|
//
|
|
|
|
// Created by Christopher Snowhill on 1/02/22.
|
|
|
|
// Copyright 2022 __LoSnoCo__. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#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 sampleRate = 44100;
|
|
|
|
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-02-07 05:49:27 +00:00
|
|
|
BOOL repeatOne = IsRepeatOneSet();
|
|
|
|
uint32_t maxLoops = repeatOne ? 0 : 2;
|
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-02-07 05:49:27 +00:00
|
|
|
pCfg.fadeSmpls = sampleRate * 4; // fade over 4 seconds
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
mainPlr->SetOutputSettings(sampleRate, numChannels, numBitsPerSample, smplAlloc);
|
|
|
|
|
|
|
|
[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-02-07 05:49:27 +00:00
|
|
|
- (int)readAudio:(void*)buf frames:(UInt32)frames {
|
|
|
|
if([self trackEnded])
|
2022-01-03 09:51:53 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
BOOL repeatOne = IsRepeatOneSet();
|
|
|
|
uint32_t maxLoops = repeatOne ? 0 : 2;
|
|
|
|
|
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
|
|
|
|
|
|
|
return framesDone;
|
|
|
|
}
|
|
|
|
|
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
|