338 |
* ('wvpl') list chunk |
* ('wvpl') list chunk |
339 |
* @param fileNo - number of an extension file where this sample |
* @param fileNo - number of an extension file where this sample |
340 |
* is located, 0 otherwise |
* is located, 0 otherwise |
341 |
|
* @param index - wave pool index of sample (may be -1 on new sample) |
342 |
*/ |
*/ |
343 |
Sample::Sample(File* pFile, RIFF::List* waveList, file_offset_t WavePoolOffset, unsigned long fileNo) : DLS::Sample((DLS::File*) pFile, waveList, WavePoolOffset) { |
Sample::Sample(File* pFile, RIFF::List* waveList, file_offset_t WavePoolOffset, unsigned long fileNo, int index) |
344 |
|
: DLS::Sample((DLS::File*) pFile, waveList, WavePoolOffset) |
345 |
|
{ |
346 |
static const DLS::Info::string_length_t fixedStringLengths[] = { |
static const DLS::Info::string_length_t fixedStringLengths[] = { |
347 |
{ CHUNK_ID_INAM, 64 }, |
{ CHUNK_ID_INAM, 64 }, |
348 |
{ 0, 0 } |
{ 0, 0 } |
352 |
FileNo = fileNo; |
FileNo = fileNo; |
353 |
|
|
354 |
__resetCRC(crc); |
__resetCRC(crc); |
355 |
|
// if this is not a new sample, try to get the sample's already existing |
356 |
|
// CRC32 checksum from disk, this checksum will reflect the sample's CRC32 |
357 |
|
// checksum of the time when the sample was consciously modified by the |
358 |
|
// user for the last time (by calling Sample::Write() that is). |
359 |
|
if (index >= 0) { // not a new file ... |
360 |
|
try { |
361 |
|
uint32_t crc = pFile->GetSampleChecksumByIndex(index); |
362 |
|
this->crc = crc; |
363 |
|
} catch (...) {} |
364 |
|
} |
365 |
|
|
366 |
pCk3gix = waveList->GetSubChunk(CHUNK_ID_3GIX); |
pCk3gix = waveList->GetSubChunk(CHUNK_ID_3GIX); |
367 |
if (pCk3gix) { |
if (pCk3gix) { |
1355 |
} |
} |
1356 |
|
|
1357 |
/** |
/** |
1358 |
|
* Returns the CRC-32 checksum of the sample's raw wave form data at the |
1359 |
|
* time when this sample's wave form data was modified for the last time |
1360 |
|
* by calling Write(). This checksum only covers the raw wave form data, |
1361 |
|
* not any meta informations like i.e. bit depth or loop points. Since |
1362 |
|
* this method just returns the checksum stored for this sample i.e. when |
1363 |
|
* the gig file was loaded, this method returns immediately. So it does no |
1364 |
|
* recalcuation of the checksum with the currently available sample wave |
1365 |
|
* form data. |
1366 |
|
* |
1367 |
|
* @see VerifyWaveData() |
1368 |
|
*/ |
1369 |
|
uint32_t Sample::GetWaveDataCRC32Checksum() { |
1370 |
|
return crc; |
1371 |
|
} |
1372 |
|
|
1373 |
|
/** |
1374 |
* Checks the integrity of this sample's raw audio wave data. Whenever a |
* Checks the integrity of this sample's raw audio wave data. Whenever a |
1375 |
* Sample's raw wave data is intentionally modified (i.e. by calling |
* Sample's raw wave data is intentionally modified (i.e. by calling |
1376 |
* Write() and supplying the new raw audio wave form data) a CRC32 checksum |
* Write() and supplying the new raw audio wave form data) a CRC32 checksum |
1386 |
* default whenever a gig file is loaded. So this method must be called |
* default whenever a gig file is loaded. So this method must be called |
1387 |
* explicitly to fulfill this task. |
* explicitly to fulfill this task. |
1388 |
* |
* |
1389 |
|
* @param pActually - (optional) if provided, will be set to the actually |
1390 |
|
* calculated checksum of the current raw wave form data, |
1391 |
|
* you can get the expected checksum instead by calling |
1392 |
|
* GetWaveDataCRC32Checksum() |
1393 |
* @returns true if sample is OK or false if the sample is damaged |
* @returns true if sample is OK or false if the sample is damaged |
1394 |
* @throws Exception if no checksum had been stored to disk for this |
* @throws Exception if no checksum had been stored to disk for this |
1395 |
* sample yet, or on I/O issues |
* sample yet, or on I/O issues |
1396 |
|
* @see GetWaveDataCRC32Checksum() |
1397 |
*/ |
*/ |
1398 |
bool Sample::VerifyWaveData() { |
bool Sample::VerifyWaveData(uint32_t* pActually) { |
1399 |
File* pFile = static_cast<File*>(GetParent()); |
File* pFile = static_cast<File*>(GetParent()); |
|
const uint32_t referenceCRC = pFile->GetSampleChecksum(this); |
|
1400 |
uint32_t crc = CalculateWaveDataChecksum(); |
uint32_t crc = CalculateWaveDataChecksum(); |
1401 |
return crc == referenceCRC; |
if (pActually) *pActually = crc; |
1402 |
|
return crc == this->crc; |
1403 |
} |
} |
1404 |
|
|
1405 |
uint32_t Sample::CalculateWaveDataChecksum() { |
uint32_t Sample::CalculateWaveDataChecksum() { |
5456 |
__notify_progress(pProgress, subprogress); |
__notify_progress(pProgress, subprogress); |
5457 |
|
|
5458 |
file_offset_t waveFileOffset = wave->GetFilePos(); |
file_offset_t waveFileOffset = wave->GetFilePos(); |
5459 |
pSamples->push_back(new Sample(this, wave, waveFileOffset - wvplFileOffset, fileNo)); |
pSamples->push_back(new Sample(this, wave, waveFileOffset - wvplFileOffset, fileNo, iSampleIndex)); |
5460 |
|
|
5461 |
iSampleIndex++; |
iSampleIndex++; |
5462 |
} |
} |
5702 |
uint32_t one = 1; |
uint32_t one = 1; |
5703 |
_3crc->WriteUint32(&one); // always 1 |
_3crc->WriteUint32(&one); // always 1 |
5704 |
_3crc->WriteUint32(&crc); |
_3crc->WriteUint32(&crc); |
|
|
|
|
// reload CRC table to RAM to keep it persistent over several subsequent save operations |
|
|
_3crc->ReleaseChunkData(); |
|
|
_3crc->LoadChunkData(); |
|
5705 |
} |
} |
5706 |
|
|
5707 |
uint32_t File::GetSampleChecksum(Sample* pSample) { |
uint32_t File::GetSampleChecksum(Sample* pSample) { |
5708 |
|
// get the index of the sample |
5709 |
|
int iWaveIndex = GetWaveTableIndexOf(pSample); |
5710 |
|
if (iWaveIndex < 0) throw gig::Exception("Could not retrieve reference crc of sample, could not resolve sample's wave table index"); |
5711 |
|
|
5712 |
|
return GetSampleChecksumByIndex(iWaveIndex); |
5713 |
|
} |
5714 |
|
|
5715 |
|
uint32_t File::GetSampleChecksumByIndex(int index) { |
5716 |
|
if (index < 0) throw gig::Exception("Could not retrieve reference crc of sample, invalid wave pool index of sample"); |
5717 |
|
|
5718 |
RIFF::Chunk* _3crc = pRIFF->GetSubChunk(CHUNK_ID_3CRC); |
RIFF::Chunk* _3crc = pRIFF->GetSubChunk(CHUNK_ID_3CRC); |
5719 |
if (!_3crc) throw gig::Exception("Could not retrieve reference crc of sample, no checksums stored for this file yet"); |
if (!_3crc) throw gig::Exception("Could not retrieve reference crc of sample, no checksums stored for this file yet"); |
5720 |
uint8_t* pData = (uint8_t*) _3crc->LoadChunkData(); |
uint8_t* pData = (uint8_t*) _3crc->LoadChunkData(); |
5721 |
if (!pData) throw gig::Exception("Could not retrieve reference crc of sample, no checksums stored for this file yet"); |
if (!pData) throw gig::Exception("Could not retrieve reference crc of sample, no checksums stored for this file yet"); |
5722 |
|
|
|
// get the index of the sample |
|
|
int iWaveIndex = GetWaveTableIndexOf(pSample); |
|
|
if (iWaveIndex < 0) throw gig::Exception("Could not retrieve reference crc of sample, could not resolve sample's wave table index"); |
|
|
|
|
5723 |
// read the CRC-32 checksum directly from disk |
// read the CRC-32 checksum directly from disk |
5724 |
size_t pos = iWaveIndex * 8; |
size_t pos = index * 8; |
5725 |
if (pos + 8 > _3crc->GetNewSize()) |
if (pos + 8 > _3crc->GetNewSize()) |
5726 |
throw gig::Exception("Could not retrieve reference crc of sample, could not seek to required position in crc chunk"); |
throw gig::Exception("Could not retrieve reference crc of sample, could not seek to required position in crc chunk"); |
5727 |
|
|
5728 |
uint32_t one = load32(&pData[pos]); // always 1 |
uint32_t one = load32(&pData[pos]); // always 1 |
5729 |
if (one != 1) |
if (one != 1) |
5730 |
throw gig::Exception("Could not verify sample, because reference checksum table is damaged"); |
throw gig::Exception("Could not retrieve reference crc of sample, because reference checksum table is damaged"); |
5731 |
|
|
5732 |
return load32(&pData[pos+4]); |
return load32(&pData[pos+4]); |
5733 |
} |
} |
5734 |
|
|
5735 |
int File::GetWaveTableIndexOf(gig::Sample* pSample) { |
int File::GetWaveTableIndexOf(gig::Sample* pSample) { |
5736 |
if (!pSamples) GetFirstSample(); // make sure sample chunks were scanned |
if (!pSamples) GetFirstSample(); // make sure sample chunks were scanned |
5737 |
File::SampleList::iterator iter = pSamples->begin(); |
File::SampleList::iterator iter = pSamples->begin(); |
5795 |
RIFF::Chunk* _3crc = pRIFF->GetSubChunk(CHUNK_ID_3CRC); |
RIFF::Chunk* _3crc = pRIFF->GetSubChunk(CHUNK_ID_3CRC); |
5796 |
if (!_3crc) { |
if (!_3crc) { |
5797 |
_3crc = pRIFF->AddSubChunk(CHUNK_ID_3CRC, pSamples->size() * 8); |
_3crc = pRIFF->AddSubChunk(CHUNK_ID_3CRC, pSamples->size() * 8); |
5798 |
|
// the order of einf and 3crc is not the same in v2 and v3 |
5799 |
|
RIFF::Chunk* einf = pRIFF->GetSubChunk(CHUNK_ID_EINF); |
5800 |
|
if (einf && pVersion && pVersion->major == 3) pRIFF->MoveSubChunk(_3crc, einf); |
5801 |
bRequiresSave = true; |
bRequiresSave = true; |
5802 |
} else if (_3crc->GetNewSize() != pSamples->size() * 8) { |
} else if (_3crc->GetNewSize() != pSamples->size() * 8) { |
5803 |
_3crc->Resize(pSamples->size() * 8); |
_3crc->Resize(pSamples->size() * 8); |
5827 |
gig::Sample* pSample = (gig::Sample*) *iter; |
gig::Sample* pSample = (gig::Sample*) *iter; |
5828 |
int index = GetWaveTableIndexOf(pSample); |
int index = GetWaveTableIndexOf(pSample); |
5829 |
if (index < 0) throw gig::Exception("Could not rebuild crc table for samples, wave table index of a sample could not be resolved"); |
if (index < 0) throw gig::Exception("Could not rebuild crc table for samples, wave table index of a sample could not be resolved"); |
5830 |
uint32_t crc = pSample->CalculateWaveDataChecksum(); |
pSample->crc = pSample->CalculateWaveDataChecksum(); |
5831 |
SetSampleChecksum(pSample, crc); |
SetSampleChecksum(pSample, pSample->crc); |
5832 |
} |
} |
5833 |
} |
} |
5834 |
} |
} |
6095 |
lst3LS = NULL; |
lst3LS = NULL; |
6096 |
} |
} |
6097 |
|
|
|
// if there is a 3CRC chunk, make sure it is loaded into RAM, otherwise |
|
|
// its data might get lost or damaged on file structure changes |
|
|
RIFF::Chunk* _3crc = pRIFF->GetSubChunk(CHUNK_ID_3CRC); |
|
|
if (_3crc) _3crc->LoadChunkData(); |
|
|
|
|
6098 |
// first update base class's chunks |
// first update base class's chunks |
6099 |
DLS::File::UpdateChunks(pProgress); |
DLS::File::UpdateChunks(pProgress); |
6100 |
|
|
6251 |
// update 3crc chunk |
// update 3crc chunk |
6252 |
|
|
6253 |
// The 3crc chunk contains CRC-32 checksums for the |
// The 3crc chunk contains CRC-32 checksums for the |
6254 |
// samples. The actual checksum values will be filled in |
// samples. When saving a gig file to disk, we first update the 3CRC |
6255 |
// later, by Sample::Write. |
// chunk here (in RAM) with the old crc values which we read from the |
6256 |
|
// 3CRC chunk when we opened the file (available with gig::Sample::crc |
6257 |
|
// member variable). This step is required, because samples might have |
6258 |
|
// been deleted by the user since the file was opened, which in turn |
6259 |
|
// changes the order of the (i.e. old) checksums within the 3crc chunk. |
6260 |
|
// If a sample was conciously modified by the user (that is if |
6261 |
|
// Sample::Write() was called later on) then Sample::Write() will just |
6262 |
|
// update the respective individual checksum(s) directly on disk and |
6263 |
|
// leaves all other sample checksums untouched. |
6264 |
|
|
6265 |
|
RIFF::Chunk* _3crc = pRIFF->GetSubChunk(CHUNK_ID_3CRC); |
6266 |
if (_3crc) { |
if (_3crc) { |
6267 |
_3crc->Resize(pSamples->size() * 8); |
_3crc->Resize(pSamples->size() * 8); |
6268 |
} else if (newFile) { |
} else /*if (newFile)*/ { |
6269 |
_3crc = pRIFF->AddSubChunk(CHUNK_ID_3CRC, pSamples->size() * 8); |
_3crc = pRIFF->AddSubChunk(CHUNK_ID_3CRC, pSamples->size() * 8); |
|
_3crc->LoadChunkData(); |
|
|
|
|
6270 |
// the order of einf and 3crc is not the same in v2 and v3 |
// the order of einf and 3crc is not the same in v2 and v3 |
6271 |
if (einf && pVersion && pVersion->major == 3) pRIFF->MoveSubChunk(_3crc, einf); |
if (einf && pVersion && pVersion->major == 3) pRIFF->MoveSubChunk(_3crc, einf); |
6272 |
} |
} |
6273 |
|
{ // must be performed in RAM here ... |
6274 |
|
uint32_t* pData = (uint32_t*) _3crc->LoadChunkData(); |
6275 |
|
if (pData) { |
6276 |
|
File::SampleList::iterator iter = pSamples->begin(); |
6277 |
|
File::SampleList::iterator end = pSamples->end(); |
6278 |
|
for (int index = 0; iter != end; ++iter, ++index) { |
6279 |
|
gig::Sample* pSample = (gig::Sample*) *iter; |
6280 |
|
pData[index*2] = 1; // always 1 |
6281 |
|
pData[index*2+1] = pSample->crc; |
6282 |
|
} |
6283 |
|
} |
6284 |
|
} |
6285 |
} |
} |
6286 |
|
|
6287 |
void File::UpdateFileOffsets() { |
void File::UpdateFileOffsets() { |