furnace/src/engine/filePlayer.cpp

307 lines
7.4 KiB
C++
Raw Normal View History

2025-10-26 21:02:05 -05:00
/**
* Furnace Tracker - multi-system chiptune tracker
* Copyright (C) 2021-2025 tildearrow and contributors
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "filePlayer.h"
#include "../ta-log.h"
#include <inttypes.h>
2025-10-26 21:02:05 -05:00
#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 8
2025-10-27 01:43:03 -05:00
#define DIV_FPCACHE_DISCARD_SIZE 4096
2025-10-26 21:02:05 -05:00
void DivFilePlayer::fillBlocksNear(size_t pos) {
logV("DivFilePlayer: fillBlocksNear(%" PRIu64 ")",pos);
2025-10-27 01:43:03 -05:00
// don't if file isn't present
if (!blocks) return;
// don't if there was an I/O error
if (fileError) return;
// don't read anything if we cannot seek
// (if this is set the file is already read in its entirety)
if (!si.seekable) return;
size_t firstBlock=pos>>DIV_FPCACHE_BLOCK_SHIFT;
size_t lastBlock=firstBlock+DIV_FPCACHE_BLOCKS_FROM_FILL;
if (lastBlock>=numBlocks) lastBlock=numBlocks-1;
// don't read if we're after end of file
if (firstBlock>lastBlock) return;
bool needToFill=false;
for (size_t i=firstBlock; i<=lastBlock; i++) {
if (!blocks[i]) {
needToFill=true;
firstBlock=i;
2025-10-27 01:43:03 -05:00
break;
}
}
if (!needToFill) return;
// check whether we need to seek
sf_count_t curSeek=sf_seek(sf,0,SEEK_CUR);
2025-10-27 01:43:03 -05:00
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
}
}
}
2025-10-27 01:43:03 -05:00
// 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,DIV_FPCACHE_BLOCK_SIZE*si.channels*sizeof(float));
2025-10-27 01:43:03 -05:00
}
logV("- reading block %" PRIu64,i);
2025-10-27 01:43:03 -05:00
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
}
}
2025-10-26 21:02:05 -05:00
}
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;
}
}
}
2025-10-26 21:02:05 -05:00
}
size_t DivFilePlayer::getPos() {
return playPos;
}
size_t DivFilePlayer::setPos(size_t newPos, unsigned int offset) {
2025-10-27 01:43:03 -05:00
playPos=newPos;
return playPos;
}
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;
2025-10-26 21:02:05 -05:00
}
void DivFilePlayer::play(unsigned int offset) {
logV("DivFilePlayer: playing");
2025-10-26 21:02:05 -05:00
playing=true;
}
void DivFilePlayer::stop(unsigned int offset) {
logV("DivFilePlayer: stopping");
2025-10-26 21:02:05 -05:00
playing=false;
}
bool DivFilePlayer::closeFile() {
if (sf==NULL) return false;
2025-10-27 01:43:03 -05:00
sfw.doClose();
2025-10-26 21:02:05 -05:00
sf=NULL;
2025-10-27 01:43:03 -05:00
playing=false;
for (size_t i=0; i<numBlocks; i++) {
if (blocks[i]) {
delete[] blocks[i];
blocks[i]=NULL;
}
}
numBlocks=0;
delete[] blocks;
blocks=NULL;
delete[] discardBuf;
discardBuf=NULL;
return true;
2025-10-26 21:02:05 -05:00
}
bool DivFilePlayer::loadFile(const char* path) {
if (sf!=NULL) closeFile();
logD("DivFilePlayer: opening file...");
2025-10-27 01:43:03 -05:00
sf=sfw.doOpen(path,SFM_READ,&si);
if (sf==NULL) {
logE("could not open file!");
2025-10-27 01:43:03 -05:00
return false;
}
logV("- samples: %d",si.frames);
logV("- channels: %d",si.channels);
logV("- rate: %d",si.samplerate);
2025-10-27 01:43:03 -05:00
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;
2025-10-27 01:43:03 -05:00
rateAccum=0;
fileError=false;
// read the entire file if not seekable
if (!si.seekable) {
logV("file not seekable - reading...");
2025-10-27 01:43:03 -05:00
for (size_t i=0; i<numBlocks; i++) {
blocks[i]=new float[DIV_FPCACHE_BLOCK_SIZE*si.channels];
}
for (size_t i=0; i<numBlocks; 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
break;
}
}
} else {
logV("file is seekable");
// read the first couple blocks
fillBlocksNear(0);
2025-10-27 01:43:03 -05:00
}
discardBuf=new float[DIV_FPCACHE_DISCARD_SIZE*si.channels];
return true;
2025-10-26 21:02:05 -05:00
}
String DivFilePlayer::getLastError() {
return lastError;
}
const SF_INFO& DivFilePlayer::getFileInfo() {
return si;
}
2025-10-26 21:02:05 -05:00
void DivFilePlayer::setOutputRate(int rate) {
if (rate<1) return;
outRate=rate;
2025-10-26 21:02:05 -05:00
}
float DivFilePlayer::getVolume() {
2025-10-27 01:43:03 -05:00
return volume;
2025-10-26 21:02:05 -05:00
}
void DivFilePlayer::setVolume(float vol) {
2025-10-27 01:43:03 -05:00
volume=vol;
2025-10-26 21:02:05 -05:00
}
DivFilePlayer::DivFilePlayer():
2025-10-27 01:43:03 -05:00
discardBuf(NULL),
2025-10-26 21:02:05 -05:00
blocks(NULL),
2025-10-27 01:43:03 -05:00
numBlocks(0),
2025-10-26 21:02:05 -05:00
sf(NULL),
playPos(0),
lastBlock(SIZE_MAX),
2025-10-26 21:02:05 -05:00
outRate(44100),
rateAccum(0),
volume(1.0f),
playing(false),
fileError(false) {
2025-10-27 01:43:03 -05:00
memset(&si,0,sizeof(SF_INFO));
2025-10-26 21:02:05 -05:00
}
DivFilePlayer::~DivFilePlayer() {
closeFile();
}