reference player prototype

poor performance
no playback sync yet
This commit is contained in:
tildearrow 2025-10-27 05:15:47 -05:00
parent 9bbfdc6f43
commit f77aafb44d
13 changed files with 336 additions and 15 deletions

View file

@ -1648,6 +1648,16 @@ void DivEngine::getCommandStream(std::vector<DivCommand>& where) {
BUSY_END;
}
DivFilePlayer* DivEngine::getFilePlayer() {
if (curFilePlayer==NULL) {
BUSY_BEGIN_SOFT;
curFilePlayer=new DivFilePlayer;
curFilePlayer->setOutputRate(got.rate);
BUSY_END;
}
return curFilePlayer;
}
void DivEngine::playSub(bool preserveDrift, int goalRow) {
logV("playSub() called");
std::chrono::high_resolution_clock::time_point timeStart=std::chrono::high_resolution_clock::now();
@ -3419,6 +3429,12 @@ void DivEngine::autoPatchbay() {
}
}
// file player
song.patchbay.reserve(DIV_MAX_OUTPUTS);
for (unsigned int j=0; j<DIV_MAX_OUTPUTS; j++) {
song.patchbay.push_back(0xffc00000|j|(j<<16));
}
// wave/sample preview
song.patchbay.reserve(DIV_MAX_OUTPUTS);
for (unsigned int j=0; j<DIV_MAX_OUTPUTS; j++) {
@ -4302,6 +4318,10 @@ bool DivEngine::quit(bool saveConfig) {
metroBuf=NULL;
metroBufLen=0;
}
if (curFilePlayer!=NULL) {
delete curFilePlayer;
curFilePlayer=NULL;
}
if (yrw801ROM!=NULL) delete[] yrw801ROM;
if (tg100ROM!=NULL) delete[] tg100ROM;
if (mu5ROM!=NULL) delete[] mu5ROM;

View file

@ -28,6 +28,7 @@
#include "dataErrors.h"
#include "safeWriter.h"
#include "cmdStream.h"
#include "filePlayer.h"
#include "../audio/taAudio.h"
#include "blip_buf.h"
#include <functional>
@ -54,8 +55,8 @@ class DivWorkPool;
#define DIV_UNSTABLE
#define DIV_VERSION "dev237"
#define DIV_ENGINE_VERSION 237
#define DIV_VERSION "dev238"
#define DIV_ENGINE_VERSION 238
// for imports
#define DIV_VERSION_MOD 0xff01
#define DIV_VERSION_FC 0xff02
@ -587,6 +588,7 @@ class DivEngine {
int samp_temp, samp_prevSample;
short* samp_bbIn;
short* samp_bbOut;
unsigned char* metroTick;
size_t metroTickLen;
float* metroBuf;
@ -596,6 +598,10 @@ class DivEngine {
float metroVol;
float previewVol;
float* filePlayerBuf[DIV_MAX_OUTPUTS];
size_t filePlayerBufLen;
DivFilePlayer* curFilePlayer;
size_t totalProcessed;
unsigned int renderPoolThreads;
@ -738,12 +744,17 @@ class DivEngine {
void createNewFromDefaults();
// load a file.
bool load(unsigned char* f, size_t length, const char* nameHint=NULL);
// play a binary command stream.
bool playStream(unsigned char* f, size_t length);
// get the playing stream.
DivCSPlayer* getStreamPlayer();
// destroy command stream player.
bool killStream();
// get the audio file player.
DivFilePlayer* getFilePlayer();
// save as .dmf.
SafeWriter* saveDMF(unsigned char version);
// save as .fur.
@ -1552,6 +1563,8 @@ class DivEngine {
metroAmp(0.0f),
metroVol(1.0f),
previewVol(1.0f),
filePlayerBufLen(0),
curFilePlayer(NULL),
totalProcessed(0),
renderPoolThreads(0),
renderPool(NULL),
@ -1584,6 +1597,7 @@ class DivEngine {
memset(oscBuf,0,DIV_MAX_OUTPUTS*(sizeof(float*)));
memset(exportChannelMask,1,DIV_MAX_CHANS*sizeof(bool));
memset(chipPeak,0,DIV_MAX_CHIPS*DIV_MAX_OUTPUTS*sizeof(float));
memset(filePlayerBuf,0,DIV_MAX_OUTPUTS*sizeof(float));
for (int i=0; i<DIV_MAX_CHIP_DEFS; i++) {
sysFileMapFur[i]=DIV_SYSTEM_NULL;

View file

@ -18,15 +18,19 @@
*/
#include "filePlayer.h"
#include "../ta-log.h"
#include <inttypes.h>
#define DIV_FPCACHE_BLOCK_SHIFT 16
#define DIV_FPCACHE_BLOCK_SIZE (1U<<DIV_FPCACHE_BLOCK_SHIFT)
#define DIV_FPCACHE_BLOCK_MASK (DIV_FPCACHE_BLOCK_SIZE-1)
#define DIV_FPCACHE_BLOCKS_FROM_FILL 2
#define DIV_FPCACHE_BLOCKS_FROM_FILL 8
#define DIV_FPCACHE_DISCARD_SIZE 4096
void DivFilePlayer::fillBlocksNear(size_t pos) {
logV("DivFilePlayer: fillBlocksNear(%" PRIu64 ")",pos);
// don't if file isn't present
if (!blocks) return;
@ -48,7 +52,7 @@ void DivFilePlayer::fillBlocksNear(size_t pos) {
for (size_t i=firstBlock; i<=lastBlock; i++) {
if (!blocks[i]) {
needToFill=true;
firstBlock=blocks[i];
firstBlock=i;
break;
}
}
@ -56,21 +60,42 @@ void DivFilePlayer::fillBlocksNear(size_t pos) {
if (!needToFill) return;
// check whether we need to seek
sf_count_seek curSeek=sf_seek(sf,0,SEEK_CUR);
sf_count_t curSeek=sf_seek(sf,0,SEEK_CUR);
if (curSeek==-1) {
// I/O error
fileError=true;
return;
}
if ((curSeek&DIV_FPCACHE_BLOCK_MASK)!=0 || (((size_t)curSeek)>>DIV_FPCACHE_BLOCK_SHIFT)!=firstBlock) {
// we need to seek
logV("- seeking");
// we seek to a previous position in order to compensate for possible decoding differences when seeking
// (usually in lossy codecs)
sf_count_t seekWhere=firstBlock<<DIV_FPCACHE_BLOCK_SHIFT;
if (seekWhere<DIV_FPCACHE_DISCARD_SIZE) {
curSeek=sf_seek(sf,0,SEEK_SET);
// discard
if (sf_readf_float(sf,discardBuf,seekWhere)!=seekWhere) {
// this is a problem
}
} else {
seekWhere-=DIV_FPCACHE_DISCARD_SIZE;
curSeek=sf_seek(sf,seekWhere,SEEK_SET);
// discard
if (sf_readf_float(sf,discardBuf,DIV_FPCACHE_DISCARD_SIZE)!=DIV_FPCACHE_DISCARD_SIZE) {
// this is a problem
}
}
}
// read blocks
for (size_t i=firstBlock; i<=lastBlock; i++) {
if (!blocks[i]) {
blocks[i]=new float[DIV_FPCACHE_BLOCK_SIZE*si.channels];
memset(blocks[i],0,
memset(blocks[i],0,DIV_FPCACHE_BLOCK_SIZE*si.channels*sizeof(float));
}
logV("- reading block %" PRIu64,i);
sf_count_t totalRead=sf_readf_float(sf,blocks[i],DIV_FPCACHE_BLOCK_SIZE);
if (totalRead<DIV_FPCACHE_BLOCK_SIZE) {
// we've reached end of file
@ -79,22 +104,96 @@ void DivFilePlayer::fillBlocksNear(size_t pos) {
}
void DivFilePlayer::mix(float** buf, int chans, unsigned int size) {
// fill with zero if we don't have a file
if (sf==NULL) {
for (int i=0; i<chans; i++) {
memset(buf[i],0,size*sizeof(float));
}
return;
}
for (unsigned int i=0; i<size; i++) {
if (playing) {
size_t blockIndex=playPos>>DIV_FPCACHE_BLOCK_SHIFT;
if (blockIndex!=lastBlock) {
fillBlocksNear(playPos);
lastBlock=blockIndex;
}
if (blockIndex>=numBlocks) {
// stop here
for (int j=0; j<chans; j++) {
buf[j][i]=0.0f;
}
continue;
}
float* block=blocks[blockIndex];
size_t posInBlock=(playPos&DIV_FPCACHE_BLOCK_MASK)*si.channels;
// put
if (block==NULL) {
for (int j=0; j<chans; j++) {
buf[j][i]=0.0f;
}
} else if (si.channels==1) {
for (int j=0; j<chans; j++) {
buf[j][i]=block[posInBlock]*volume;
}
} else {
for (int j=0; j<chans; j++) {
if (j>=si.channels) {
buf[j][i]=0.0f;
} else {
buf[j][i]=block[posInBlock++]*volume;
}
}
}
// advance
rateAccum+=si.samplerate;
while (rateAccum>=outRate) {
rateAccum-=outRate;
playPos++;
if (playPos>=(size_t)si.frames) {
playPos=0;
}
}
} else {
for (int j=0; j<chans; j++) {
buf[j][i]=0.0f;
}
}
}
}
size_t DivFilePlayer::getPos() {
return playPos;
}
size_t DivFilePlayer::setPos(size_t newPos, unsigned int offset=0) {
size_t DivFilePlayer::setPos(size_t newPos, unsigned int offset) {
playPos=newPos;
return playPos;
}
void DivFilePlayer::play(unsigned int offset=0) {
bool DivFilePlayer::isBlockPresent(size_t pos) {
if (blocks==NULL) return false;
return (blocks[pos>>DIV_FPCACHE_BLOCK_SHIFT]!=NULL);
}
bool DivFilePlayer::isLoaded() {
return (sf!=NULL);
}
bool DivFilePlayer::isPlaying() {
return playing;
}
void DivFilePlayer::play(unsigned int offset) {
logV("DivFilePlayer: playing");
playing=true;
}
void DivFilePlayer::stop(unsigned int offset=0) {
void DivFilePlayer::stop(unsigned int offset) {
logV("DivFilePlayer: stopping");
playing=false;
}
@ -116,24 +215,36 @@ bool DivFilePlayer::closeFile() {
delete[] discardBuf;
discardBuf=NULL;
return true;
}
bool DivFilePlayer::loadFile(const char* path) {
if (sf!=NULL) closeFile();
logD("DivFilePlayer: opening file...");
sf=sfw.doOpen(path,SFM_READ,&si);
if (sf==NULL) {
logE("could not open file!");
return false;
}
logV("- samples: %d",si.frames);
logV("- channels: %d",si.channels);
logV("- rate: %d",si.samplerate);
numBlocks=(DIV_FPCACHE_BLOCK_MASK+si.frames)>>DIV_FPCACHE_BLOCK_SHIFT;
blocks=new float*[numBlocks];
memset(blocks,0,numBlocks*sizeof(void*));
playPos=0;
lastBlock=SIZE_MAX;
rateAccum=0;
fileError=false;
// read the entire file if not seekable
if (!si.seekable) {
logV("file not seekable - reading...");
for (size_t i=0; i<numBlocks; i++) {
blocks[i]=new float[DIV_FPCACHE_BLOCK_SIZE*si.channels];
}
@ -144,17 +255,27 @@ bool DivFilePlayer::loadFile(const char* path) {
break;
}
}
} else {
logV("file is seekable");
// read the first couple blocks
fillBlocksNear(0);
}
discardBuf=new float[DIV_FPCACHE_DISCARD_SIZE*si.channels];
return true;
}
String DivFilePlayer::getLastError() {
return lastError;
}
const SF_INFO& DivFilePlayer::getFileInfo() {
return si;
}
void DivFilePlayer::setOutputRate(int rate) {
outRate=0;
if (rate<1) return;
outRate=rate;
}
float DivFilePlayer::getVolume() {
@ -171,6 +292,7 @@ DivFilePlayer::DivFilePlayer():
numBlocks(0),
sf(NULL),
playPos(0),
lastBlock(SIZE_MAX),
outRate(44100),
rateAccum(0),
volume(1.0f),

View file

@ -41,6 +41,7 @@ class DivFilePlayer {
SF_INFO si;
size_t playPos;
size_t lastBlock;
int outRate;
int rateAccum;
float volume;
@ -56,6 +57,8 @@ class DivFilePlayer {
size_t getPos();
size_t setPos(size_t newPos, unsigned int offset=0);
bool isBlockPresent(size_t pos);
bool isLoaded();
bool isPlaying();
void play(unsigned int offset=0);
void stop(unsigned int offset=0);
@ -63,6 +66,7 @@ class DivFilePlayer {
bool loadFile(const char* path);
String getLastError();
const SF_INFO& getFileInfo();
void setOutputRate(int rate);
float getVolume();
void setVolume(float vol);
@ -70,3 +74,5 @@ class DivFilePlayer {
DivFilePlayer();
~DivFilePlayer();
};
#endif

View file

@ -3237,6 +3237,19 @@ void DivEngine::nextBuf(float** in, float** out, int inChans, int outChans, unsi
renderPool->wait();
}
// process file player
// resize file player audio buffer if necessary
if (filePlayerBufLen<size) {
for (int i=0; i<DIV_MAX_OUTPUTS; i++) {
if (filePlayerBuf[i]!=NULL) delete[] filePlayerBuf[i];
filePlayerBuf[i]=new float[size];
}
filePlayerBufLen=size;
}
if (curFilePlayer!=NULL) {
curFilePlayer->mix(filePlayerBuf,outChans,size);
}
// process metronome
// resize the metronome's audio buffer if necessary
if (metroBufLen<size || metroBuf==NULL) {
@ -3317,6 +3330,11 @@ void DivEngine::nextBuf(float** in, float** out, int inChans, int outChans, unsi
out[destSubPort][j]+=((float)disCont[srcPortSet].bbOut[srcSubPort][j]/32768.0)*vol;
}
}
} else if (srcPortSet==0xffc) {
// file player
for (size_t j=0; j<size; j++) {
out[destSubPort][j]+=filePlayerBuf[srcSubPort][j];
}
} else if (srcPortSet==0xffd) {
// sample preview
for (size_t j=0; j<size; j++) {