text
stringlengths
0
2.2M
return 0;
}
AyInstance::~AyInstance()
{
delete mChip;
}
Ay::Ay()
{
mBaseSamplerate = 44100;
mChannels = 2;
mOps = 0;
mYm = false;
mChipspeed = 1774400;
mCpuspeed = 50;
mLooppos = 0;
mLength = 0;
}
Ay::~Ay()
{
stop();
delete[] mOps;
}
result Ay::loadMem(const unsigned char *aMem, unsigned int aLength, bool aCopy, bool aTakeOwnership)
{
if (!aMem || aLength == 0)
return INVALID_PARAMETER;
MemoryFile mf;
int res = mf.openMem(aMem, aLength, aCopy, aTakeOwnership);
if (res != SO_NO_ERROR)
{
return res;
}
res = loadFile(&mf);
return res;
}
result Ay::load(const char *aFilename)
{
if (!aFilename)
return INVALID_PARAMETER;
DiskFile df;
int res = df.open(aFilename);
if (res != SO_NO_ERROR)
{
return res;
}
res = loadFile(&df);
return res;
}
result Ay::loadFile(File *aFile)
{
if (aFile == NULL)
return INVALID_PARAMETER;
// Expect a file wih header and at least one reg write
if (aFile->length() < 34) return FILE_LOAD_FAILED;
aFile->seek(0);
if (aFile->read32() != 'PIHC') return FILE_LOAD_FAILED; // CHIP
if (aFile->read32() != 'ENUT') return FILE_LOAD_FAILED; // TUNE
int dataofs = aFile->read16();
int chiptype = aFile->read8();
// check if this file is for AY / YM, turbosound or turbosound next
if (!(chiptype == 1 || chiptype == 2 || chiptype == 3)) return FILE_LOAD_FAILED;
int flags = aFile->read8();
int kchunks = aFile->read16();
int lastchunk = aFile->read16();
mLength = (kchunks - 1) * 1024 + lastchunk;
mLooppos = aFile->read16() * 1024 + aFile->read16();
mCpuspeed = aFile->read32();
mChipspeed = aFile->read32();
mYm = false;
if (flags & 64) mYm = true;
mOps = new unsigned short[mLength];
aFile->seek(dataofs);
if (flags & 1)
{
// uncompressed
aFile->read((unsigned char*)mOps, mLength);
}
else
{
// compressed
int len = aFile->length() - dataofs;
unsigned char* buf = new unsigned char[len];
aFile->read(buf, len);
int bufofs = 0;
for (int i = 0; i < kchunks; i++)
{
bufofs += zx7_decompress(buf + bufofs, ((unsigned char*)mOps) + i * 1024);
}
delete[] buf;
}
return SO_NO_ERROR;
}