/[svn]/libgig/trunk/src/gig.cpp
ViewVC logotype

Diff of /libgig/trunk/src/gig.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2985 by schoenebeck, Tue Sep 20 22:13:37 2016 UTC revision 3115 by schoenebeck, Sat Apr 15 20:17:05 2017 UTC
# Line 269  namespace { Line 269  namespace {
269       * steps.       * steps.
270       *       *
271       * Once the whole data was processed by __calculateCRC(), one should       * Once the whole data was processed by __calculateCRC(), one should
272       * call __encodeCRC() to get the final CRC result.       * call __finalizeCRC() to get the final CRC result.
273       *       *
274       * @param buf     - pointer to data the CRC shall be calculated of       * @param buf     - pointer to data the CRC shall be calculated of
275       * @param bufSize - size of the data to be processed       * @param bufSize - size of the data to be processed
276       * @param crc     - variable the CRC sum shall be stored to       * @param crc     - variable the CRC sum shall be stored to
277       */       */
278      static void __calculateCRC(unsigned char* buf, int bufSize, uint32_t& crc) {      static void __calculateCRC(unsigned char* buf, size_t bufSize, uint32_t& crc) {
279          for (int i = 0 ; i < bufSize ; i++) {          for (size_t i = 0 ; i < bufSize ; i++) {
280              crc = __CRCTable[(crc ^ buf[i]) & 0xff] ^ (crc >> 8);              crc = __CRCTable[(crc ^ buf[i]) & 0xff] ^ (crc >> 8);
281          }          }
282      }      }
# Line 286  namespace { Line 286  namespace {
286       *       *
287       * @param crc - variable previously passed to __calculateCRC()       * @param crc - variable previously passed to __calculateCRC()
288       */       */
289      inline static uint32_t __encodeCRC(const uint32_t& crc) {      inline static void __finalizeCRC(uint32_t& crc) {
290          return crc ^ 0xffffffff;          crc ^= 0xffffffff;
291      }      }
292    
293    
# Line 338  namespace { Line 338  namespace {
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 }
# Line 349  namespace { Line 352  namespace {
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) {
# Line 1281  namespace { Line 1294  namespace {
1294          // if this is the last write, update the checksum chunk in the          // if this is the last write, update the checksum chunk in the
1295          // file          // file
1296          if (pCkData->GetPos() == pCkData->GetSize()) {          if (pCkData->GetPos() == pCkData->GetSize()) {
1297                __finalizeCRC(crc);
1298              File* pFile = static_cast<File*>(GetParent());              File* pFile = static_cast<File*>(GetParent());
1299              pFile->SetSampleChecksum(this, __encodeCRC(crc));              pFile->SetSampleChecksum(this, crc);
1300          }          }
1301          return res;          return res;
1302      }      }
# Line 1342  namespace { Line 1356  namespace {
1356      }      }
1357    
1358      /**      /**
1359         * Returns the CRC-32 checksum of the sample's raw wave form data at the
1360         * time when this sample's wave form data was modified for the last time
1361         * by calling Write(). This checksum only covers the raw wave form data,
1362         * not any meta informations like i.e. bit depth or loop points. Since
1363         * this method just returns the checksum stored for this sample i.e. when
1364         * the gig file was loaded, this method returns immediately. So it does no
1365         * recalcuation of the checksum with the currently available sample wave
1366         * form data.
1367         *
1368         * @see VerifyWaveData()
1369         */
1370        uint32_t Sample::GetWaveDataCRC32Checksum() {
1371            return crc;
1372        }
1373    
1374        /**
1375       * 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
1376       * Sample's raw wave data is intentionally modified (i.e. by calling       * Sample's raw wave data is intentionally modified (i.e. by calling
1377       * 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
# Line 1357  namespace { Line 1387  namespace {
1387       * 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
1388       * explicitly to fulfill this task.       * explicitly to fulfill this task.
1389       *       *
1390         * @param pActually - (optional) if provided, will be set to the actually
1391         *                    calculated checksum of the current raw wave form data,
1392         *                    you can get the expected checksum instead by calling
1393         *                    GetWaveDataCRC32Checksum()
1394       * @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
1395       * @throws Exception if no checksum had been stored to disk for this       * @throws Exception if no checksum had been stored to disk for this
1396       *         sample yet, or on I/O issues       *         sample yet, or on I/O issues
1397         * @see GetWaveDataCRC32Checksum()
1398       */       */
1399      bool Sample::VerifyWaveData() {      bool Sample::VerifyWaveData(uint32_t* pActually) {
1400          File* pFile = static_cast<File*>(GetParent());          //File* pFile = static_cast<File*>(GetParent());
         const uint32_t referenceCRC = pFile->GetSampleChecksum(this);  
1401          uint32_t crc = CalculateWaveDataChecksum();          uint32_t crc = CalculateWaveDataChecksum();
1402          return crc == referenceCRC;          if (pActually) *pActually = crc;
1403            return crc == this->crc;
1404      }      }
1405    
1406      uint32_t Sample::CalculateWaveDataChecksum() {      uint32_t Sample::CalculateWaveDataChecksum() {
# Line 1382  namespace { Line 1417  namespace {
1417              if (nRead <= 0) break;              if (nRead <= 0) break;
1418              __calculateCRC(&buffer[0], nRead * FrameSize, crc);              __calculateCRC(&buffer[0], nRead * FrameSize, crc);
1419          }          }
1420          __encodeCRC(crc);          __finalizeCRC(crc);
1421          return crc;          return crc;
1422      }      }
1423    
# Line 1596  namespace { Line 1631  namespace {
1631              EG2Attack                       = 0.0;              EG2Attack                       = 0.0;
1632              EG2Decay1                       = 0.005;              EG2Decay1                       = 0.005;
1633              EG2Sustain                      = 1000;              EG2Sustain                      = 1000;
1634              EG2Release                      = 0.3;              EG2Release                      = 60;
1635              LFO2ControlDepth                = 0;              LFO2ControlDepth                = 0;
1636              LFO2Frequency                   = 1.0;              LFO2Frequency                   = 1.0;
1637              LFO2InternalDepth               = 0;              LFO2InternalDepth               = 0;
# Line 1795  namespace { Line 1830  namespace {
1830    
1831          // update '3ewa' chunk with DimensionRegion's current settings          // update '3ewa' chunk with DimensionRegion's current settings
1832    
1833          const uint32_t chunksize = _3ewa->GetNewSize();          const uint32_t chunksize = (uint32_t) _3ewa->GetNewSize();
1834          store32(&pData[0], chunksize); // unknown, always chunk size?          store32(&pData[0], chunksize); // unknown, always chunk size?
1835    
1836          const int32_t lfo3freq = (int32_t) GIG_EXP_ENCODE(LFO3Frequency);          const int32_t lfo3freq = (int32_t) GIG_EXP_ENCODE(LFO3Frequency);
# Line 3763  namespace { Line 3798  namespace {
3798      DimensionRegion* Region::GetDimensionRegionByValue(const uint DimValues[8]) {      DimensionRegion* Region::GetDimensionRegionByValue(const uint DimValues[8]) {
3799          uint8_t bits;          uint8_t bits;
3800          int veldim = -1;          int veldim = -1;
3801          int velbitpos;          int velbitpos = 0;
3802          int bitpos = 0;          int bitpos = 0;
3803          int dimregidx = 0;          int dimregidx = 0;
3804          for (uint i = 0; i < Dimensions; i++) {          for (uint i = 0; i < Dimensions; i++) {
# Line 3812  namespace { Line 3847  namespace {
3847      int Region::GetDimensionRegionIndexByValue(const uint DimValues[8]) {      int Region::GetDimensionRegionIndexByValue(const uint DimValues[8]) {
3848          uint8_t bits;          uint8_t bits;
3849          int veldim = -1;          int veldim = -1;
3850          int velbitpos;          int velbitpos = 0;
3851          int bitpos = 0;          int bitpos = 0;
3852          int dimregidx = 0;          int dimregidx = 0;
3853          for (uint i = 0; i < Dimensions; i++) {          for (uint i = 0; i < Dimensions; i++) {
# Line 4161  namespace { Line 4196  namespace {
4196              // to handle potential future extensions of the header              // to handle potential future extensions of the header
4197              ckScri->SetPos(sizeof(int32_t) + headerSize);              ckScri->SetPos(sizeof(int32_t) + headerSize);
4198              // read actual script data              // read actual script data
4199              uint32_t scriptSize = ckScri->GetSize() - ckScri->GetPos();              uint32_t scriptSize = uint32_t(ckScri->GetSize() - ckScri->GetPos());
4200              data.resize(scriptSize);              data.resize(scriptSize);
4201              for (int i = 0; i < scriptSize; ++i)              for (int i = 0; i < scriptSize; ++i)
4202                  data[i] = ckScri->ReadUint8();                  data[i] = ckScri->ReadUint8();
# Line 4212  namespace { Line 4247  namespace {
4247          // recalculate CRC32 check sum          // recalculate CRC32 check sum
4248          __resetCRC(crc);          __resetCRC(crc);
4249          __calculateCRC(&data[0], data.size(), crc);          __calculateCRC(&data[0], data.size(), crc);
4250          __encodeCRC(crc);          __finalizeCRC(crc);
4251          // make sure chunk exists and has the required size          // make sure chunk exists and has the required size
4252          const int chunkSize = 7*sizeof(int32_t) + Name.size() + data.size();          const file_offset_t chunkSize = (file_offset_t) 7*sizeof(int32_t) + Name.size() + data.size();
4253          if (!pChunk) pChunk = pGroup->pList->AddSubChunk(CHUNK_ID_SCRI, chunkSize);          if (!pChunk) pChunk = pGroup->pList->AddSubChunk(CHUNK_ID_SCRI, chunkSize);
4254          else pChunk->Resize(chunkSize);          else pChunk->Resize(chunkSize);
4255          // fill the chunk data to be written to disk          // fill the chunk data to be written to disk
4256          uint8_t* pData = (uint8_t*) pChunk->LoadChunkData();          uint8_t* pData = (uint8_t*) pChunk->LoadChunkData();
4257          int pos = 0;          int pos = 0;
4258          store32(&pData[pos], 6*sizeof(int32_t) + Name.size()); // total header size          store32(&pData[pos], uint32_t(6*sizeof(int32_t) + Name.size())); // total header size
4259          pos += sizeof(int32_t);          pos += sizeof(int32_t);
4260          store32(&pData[pos], Compression);          store32(&pData[pos], Compression);
4261          pos += sizeof(int32_t);          pos += sizeof(int32_t);
# Line 4232  namespace { Line 4267  namespace {
4267          pos += sizeof(int32_t);          pos += sizeof(int32_t);
4268          store32(&pData[pos], crc);          store32(&pData[pos], crc);
4269          pos += sizeof(int32_t);          pos += sizeof(int32_t);
4270          store32(&pData[pos], Name.size());          store32(&pData[pos], (uint32_t) Name.size());
4271          pos += sizeof(int32_t);          pos += sizeof(int32_t);
4272          for (int i = 0; i < Name.size(); ++i, ++pos)          for (int i = 0; i < Name.size(); ++i, ++pos)
4273              pData[pos] = Name[i];              pData[pos] = Name[i];
# Line 4409  namespace { Line 4444  namespace {
4444          EffectSend = 0;          EffectSend = 0;
4445          Attenuation = 0;          Attenuation = 0;
4446          FineTune = 0;          FineTune = 0;
4447          PitchbendRange = 0;          PitchbendRange = 2;
4448          PianoReleaseMode = false;          PianoReleaseMode = false;
4449          DimensionKeyRange.low = 0;          DimensionKeyRange.low = 0;
4450          DimensionKeyRange.high = 0;          DimensionKeyRange.high = 0;
# Line 4584  namespace { Line 4619  namespace {
4619    
4620             RIFF::List* lst3LS = pCkInstrument->GetSubList(LIST_TYPE_3LS);             RIFF::List* lst3LS = pCkInstrument->GetSubList(LIST_TYPE_3LS);
4621             if (!lst3LS) lst3LS = pCkInstrument->AddSubList(LIST_TYPE_3LS);             if (!lst3LS) lst3LS = pCkInstrument->AddSubList(LIST_TYPE_3LS);
4622             const int slotCount = pScriptRefs->size();             const int slotCount = (int) pScriptRefs->size();
4623             const int headerSize = 3 * sizeof(uint32_t);             const int headerSize = 3 * sizeof(uint32_t);
4624             const int slotSize  = 2 * sizeof(uint32_t);             const int slotSize  = 2 * sizeof(uint32_t);
4625             const int totalChunkSize = headerSize + slotCount * slotSize;             const int totalChunkSize = headerSize + slotCount * slotSize;
# Line 4620  namespace { Line 4655  namespace {
4655         if (pScriptRefs && pScriptRefs->size() > 0) {         if (pScriptRefs && pScriptRefs->size() > 0) {
4656             RIFF::List* lst3LS = pCkInstrument->GetSubList(LIST_TYPE_3LS);             RIFF::List* lst3LS = pCkInstrument->GetSubList(LIST_TYPE_3LS);
4657             RIFF::Chunk* ckSCSL = lst3LS->GetSubChunk(CHUNK_ID_SCSL);             RIFF::Chunk* ckSCSL = lst3LS->GetSubChunk(CHUNK_ID_SCSL);
4658             const int slotCount = pScriptRefs->size();             const int slotCount = (int) pScriptRefs->size();
4659             const int headerSize = 3 * sizeof(uint32_t);             const int headerSize = 3 * sizeof(uint32_t);
4660             ckSCSL->SetPos(headerSize);             ckSCSL->SetPos(headerSize);
4661             for (int i = 0; i < slotCount; ++i) {             for (int i = 0; i < slotCount; ++i) {
4662                 uint32_t fileOffset =                 uint32_t fileOffset = uint32_t(
4663                      (*pScriptRefs)[i].script->pChunk->GetFilePos() -                      (*pScriptRefs)[i].script->pChunk->GetFilePos() -
4664                      (*pScriptRefs)[i].script->pChunk->GetPos() -                      (*pScriptRefs)[i].script->pChunk->GetPos() -
4665                      CHUNK_HEADER_SIZE(ckSCSL->GetFile()->GetFileOffsetSize());                      CHUNK_HEADER_SIZE(ckSCSL->GetFile()->GetFileOffsetSize())
4666                   );
4667                 ckSCSL->WriteUint32(&fileOffset);                 ckSCSL->WriteUint32(&fileOffset);
4668                 // jump over flags entry (containing the bypass flag)                 // jump over flags entry (containing the bypass flag)
4669                 ckSCSL->SetPos(sizeof(uint32_t), RIFF::stream_curpos);                 ckSCSL->SetPos(sizeof(uint32_t), RIFF::stream_curpos);
# Line 4687  namespace { Line 4723  namespace {
4723          RIFF::List* rgn = lrgn->AddSubList(LIST_TYPE_RGN);          RIFF::List* rgn = lrgn->AddSubList(LIST_TYPE_RGN);
4724          Region* pNewRegion = new Region(this, rgn);          Region* pNewRegion = new Region(this, rgn);
4725          pRegions->push_back(pNewRegion);          pRegions->push_back(pNewRegion);
4726          Regions = pRegions->size();          Regions = (uint32_t) pRegions->size();
4727          // update Region key table for fast lookup          // update Region key table for fast lookup
4728          UpdateRegionKeyTable();          UpdateRegionKeyTable();
4729          // done          // done
# Line 4833  namespace { Line 4869  namespace {
4869                  for (uint s = 0; group->GetScript(s); ++s) {                  for (uint s = 0; group->GetScript(s); ++s) {
4870                      Script* script = group->GetScript(s);                      Script* script = group->GetScript(s);
4871                      if (script->pChunk) {                      if (script->pChunk) {
4872                          uint32_t offset = script->pChunk->GetFilePos() -                          uint32_t offset = uint32_t(
4873                                            script->pChunk->GetPos() -                              script->pChunk->GetFilePos() -
4874                                            CHUNK_HEADER_SIZE(script->pChunk->GetFile()->GetFileOffsetSize());                              script->pChunk->GetPos() -
4875                                CHUNK_HEADER_SIZE(script->pChunk->GetFile()->GetFileOffsetSize())
4876                            );
4877                          if (offset == soughtOffset)                          if (offset == soughtOffset)
4878                          {                          {
4879                              _ScriptPooolRef ref;                              _ScriptPooolRef ref;
# Line 4960  namespace { Line 4998  namespace {
4998       */       */
4999      void Instrument::RemoveScript(Script* pScript) {      void Instrument::RemoveScript(Script* pScript) {
5000          LoadScripts();          LoadScripts();
5001          for (int i = pScriptRefs->size() - 1; i >= 0; --i) {          for (ssize_t i = pScriptRefs->size() - 1; i >= 0; --i) {
5002              if ((*pScriptRefs)[i].script == pScript) {              if ((*pScriptRefs)[i].script == pScript) {
5003                  pScriptRefs->erase( pScriptRefs->begin() + i );                  pScriptRefs->erase( pScriptRefs->begin() + i );
5004              }              }
# Line 4982  namespace { Line 5020  namespace {
5020       * gigedit.       * gigedit.
5021       */       */
5022      uint Instrument::ScriptSlotCount() const {      uint Instrument::ScriptSlotCount() const {
5023          return pScriptRefs ? pScriptRefs->size() : scriptPoolFileOffsets.size();          return uint(pScriptRefs ? pScriptRefs->size() : scriptPoolFileOffsets.size());
5024      }      }
5025    
5026      /** @brief Whether script execution shall be skipped.      /** @brief Whether script execution shall be skipped.
# Line 5406  namespace { Line 5444  namespace {
5444              }              }
5445          }          }
5446          String name(pRIFF->GetFileName());          String name(pRIFF->GetFileName());
5447          int nameLen = name.length();          int nameLen = (int) name.length();
5448          char suffix[6];          char suffix[6];
5449          if (nameLen > 4 && name.substr(nameLen - 4) == ".gig") nameLen -= 4;          if (nameLen > 4 && name.substr(nameLen - 4) == ".gig") nameLen -= 4;
5450    
# Line 5422  namespace { Line 5460  namespace {
5460                          __notify_progress(pProgress, subprogress);                          __notify_progress(pProgress, subprogress);
5461    
5462                          file_offset_t waveFileOffset = wave->GetFilePos();                          file_offset_t waveFileOffset = wave->GetFilePos();
5463                          pSamples->push_back(new Sample(this, wave, waveFileOffset - wvplFileOffset, fileNo));                          pSamples->push_back(new Sample(this, wave, waveFileOffset - wvplFileOffset, fileNo, iSampleIndex));
5464    
5465                          iSampleIndex++;                          iSampleIndex++;
5466                      }                      }
# Line 5668  namespace { Line 5706  namespace {
5706          uint32_t one = 1;          uint32_t one = 1;
5707          _3crc->WriteUint32(&one); // always 1          _3crc->WriteUint32(&one); // always 1
5708          _3crc->WriteUint32(&crc);          _3crc->WriteUint32(&crc);
   
         // reload CRC table to RAM to keep it persistent over several subsequent save operations  
         _3crc->ReleaseChunkData();  
         _3crc->LoadChunkData();  
5709      }      }
5710    
5711      uint32_t File::GetSampleChecksum(Sample* pSample) {      uint32_t File::GetSampleChecksum(Sample* pSample) {
5712            // get the index of the sample
5713            int iWaveIndex = GetWaveTableIndexOf(pSample);
5714            if (iWaveIndex < 0) throw gig::Exception("Could not retrieve reference crc of sample, could not resolve sample's wave table index");
5715    
5716            return GetSampleChecksumByIndex(iWaveIndex);
5717        }
5718    
5719        uint32_t File::GetSampleChecksumByIndex(int index) {
5720            if (index < 0) throw gig::Exception("Could not retrieve reference crc of sample, invalid wave pool index of sample");
5721    
5722          RIFF::Chunk* _3crc = pRIFF->GetSubChunk(CHUNK_ID_3CRC);          RIFF::Chunk* _3crc = pRIFF->GetSubChunk(CHUNK_ID_3CRC);
5723          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");
5724          uint8_t* pData = (uint8_t*) _3crc->LoadChunkData();          uint8_t* pData = (uint8_t*) _3crc->LoadChunkData();
5725          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");
5726    
         // 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");  
   
5727          // read the CRC-32 checksum directly from disk          // read the CRC-32 checksum directly from disk
5728          size_t pos = iWaveIndex * 8;          size_t pos = index * 8;
5729          if (pos + 8 > _3crc->GetNewSize())          if (pos + 8 > _3crc->GetNewSize())
5730              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");
5731    
5732          uint32_t one = load32(&pData[pos]); // always 1          uint32_t one = load32(&pData[pos]); // always 1
5733          if (one != 1)          if (one != 1)
5734              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");
5735    
5736          return load32(&pData[pos+4]);          return load32(&pData[pos+4]);
5737      }      }
5738        
5739      int File::GetWaveTableIndexOf(gig::Sample* pSample) {      int File::GetWaveTableIndexOf(gig::Sample* pSample) {
5740          if (!pSamples) GetFirstSample(); // make sure sample chunks were scanned          if (!pSamples) GetFirstSample(); // make sure sample chunks were scanned
5741          File::SampleList::iterator iter = pSamples->begin();          File::SampleList::iterator iter = pSamples->begin();
# Line 5720  namespace { Line 5760  namespace {
5760          if (!pSamples) GetFirstSample(); // make sure sample chunks were scanned          if (!pSamples) GetFirstSample(); // make sure sample chunks were scanned
5761          if (_3crc->GetNewSize() != pSamples->size() * 8) return false;          if (_3crc->GetNewSize() != pSamples->size() * 8) return false;
5762    
5763          const int n = _3crc->GetNewSize() / 8;          const file_offset_t n = _3crc->GetNewSize() / 8;
5764    
5765          uint32_t* pData = (uint32_t*) _3crc->LoadChunkData();          uint32_t* pData = (uint32_t*) _3crc->LoadChunkData();
5766          if (!pData) return false;          if (!pData) return false;
5767    
5768          for (int i = 0; i < n; ++i) {          for (file_offset_t i = 0; i < n; ++i) {
5769              uint32_t one = pData[i*2];              uint32_t one = pData[i*2];
5770              if (one != 1) return false;              if (one != 1) return false;
5771          }          }
# Line 5759  namespace { Line 5799  namespace {
5799          RIFF::Chunk* _3crc = pRIFF->GetSubChunk(CHUNK_ID_3CRC);          RIFF::Chunk* _3crc = pRIFF->GetSubChunk(CHUNK_ID_3CRC);
5800          if (!_3crc) {          if (!_3crc) {
5801              _3crc = pRIFF->AddSubChunk(CHUNK_ID_3CRC, pSamples->size() * 8);              _3crc = pRIFF->AddSubChunk(CHUNK_ID_3CRC, pSamples->size() * 8);
5802                // the order of einf and 3crc is not the same in v2 and v3
5803                RIFF::Chunk* einf = pRIFF->GetSubChunk(CHUNK_ID_EINF);
5804                if (einf && pVersion && pVersion->major == 3) pRIFF->MoveSubChunk(_3crc, einf);
5805              bRequiresSave = true;              bRequiresSave = true;
5806          } else if (_3crc->GetNewSize() != pSamples->size() * 8) {          } else if (_3crc->GetNewSize() != pSamples->size() * 8) {
5807              _3crc->Resize(pSamples->size() * 8);              _3crc->Resize(pSamples->size() * 8);
# Line 5788  namespace { Line 5831  namespace {
5831                      gig::Sample* pSample = (gig::Sample*) *iter;                      gig::Sample* pSample = (gig::Sample*) *iter;
5832                      int index = GetWaveTableIndexOf(pSample);                      int index = GetWaveTableIndexOf(pSample);
5833                      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");
5834                      uint32_t crc = pSample->CalculateWaveDataChecksum();                      pSample->crc  = pSample->CalculateWaveDataChecksum();
5835                      SetSampleChecksum(pSample, crc);                      SetSampleChecksum(pSample, pSample->crc);
5836                  }                  }
5837              }              }
5838          }          }
# Line 6056  namespace { Line 6099  namespace {
6099              lst3LS = NULL;              lst3LS = NULL;
6100          }          }
6101    
         // 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();  
   
6102          // first update base class's chunks          // first update base class's chunks
6103          DLS::File::UpdateChunks(pProgress);          DLS::File::UpdateChunks(pProgress);
6104    
# Line 6119  namespace { Line 6157  namespace {
6157          // Note that there are several fields with unknown use. These          // Note that there are several fields with unknown use. These
6158          // are set to zero.          // are set to zero.
6159    
6160          int sublen = pSamples->size() / 8 + 49;          int sublen = int(pSamples->size() / 8 + 49);
6161          int einfSize = (Instruments + 1) * sublen;          int einfSize = (Instruments + 1) * sublen;
6162    
6163          RIFF::Chunk* einf = pRIFF->GetSubChunk(CHUNK_ID_EINF);          RIFF::Chunk* einf = pRIFF->GetSubChunk(CHUNK_ID_EINF);
# Line 6192  namespace { Line 6230  namespace {
6230                  store32(&pData[(instrumentIdx + 1) * sublen + 24], nbloops);                  store32(&pData[(instrumentIdx + 1) * sublen + 24], nbloops);
6231                  // next 8 bytes unknown                  // next 8 bytes unknown
6232                  store32(&pData[(instrumentIdx + 1) * sublen + 36], instrumentIdx);                  store32(&pData[(instrumentIdx + 1) * sublen + 36], instrumentIdx);
6233                  store32(&pData[(instrumentIdx + 1) * sublen + 40], pSamples->size());                  store32(&pData[(instrumentIdx + 1) * sublen + 40], (uint32_t) pSamples->size());
6234                  // next 4 bytes unknown                  // next 4 bytes unknown
6235    
6236                  totnbregions += instrument->Regions;                  totnbregions += instrument->Regions;
# Line 6210  namespace { Line 6248  namespace {
6248              store32(&pData[24], totnbloops);              store32(&pData[24], totnbloops);
6249              // next 8 bytes unknown              // next 8 bytes unknown
6250              // next 4 bytes unknown, not always 0              // next 4 bytes unknown, not always 0
6251              store32(&pData[40], pSamples->size());              store32(&pData[40], (uint32_t) pSamples->size());
6252              // next 4 bytes unknown              // next 4 bytes unknown
6253          }          }
6254    
6255          // update 3crc chunk          // update 3crc chunk
6256    
6257          // The 3crc chunk contains CRC-32 checksums for the          // The 3crc chunk contains CRC-32 checksums for the
6258          // samples. The actual checksum values will be filled in          // samples. When saving a gig file to disk, we first update the 3CRC
6259          // later, by Sample::Write.          // chunk here (in RAM) with the old crc values which we read from the
6260            // 3CRC chunk when we opened the file (available with gig::Sample::crc
6261            // member variable). This step is required, because samples might have
6262            // been deleted by the user since the file was opened, which in turn
6263            // changes the order of the (i.e. old) checksums within the 3crc chunk.
6264            // If a sample was conciously modified by the user (that is if
6265            // Sample::Write() was called later on) then Sample::Write() will just
6266            // update the respective individual checksum(s) directly on disk and
6267            // leaves all other sample checksums untouched.
6268    
6269            RIFF::Chunk* _3crc = pRIFF->GetSubChunk(CHUNK_ID_3CRC);
6270          if (_3crc) {          if (_3crc) {
6271              _3crc->Resize(pSamples->size() * 8);              _3crc->Resize(pSamples->size() * 8);
6272          } else if (newFile) {          } else /*if (newFile)*/ {
6273              _3crc = pRIFF->AddSubChunk(CHUNK_ID_3CRC, pSamples->size() * 8);              _3crc = pRIFF->AddSubChunk(CHUNK_ID_3CRC, pSamples->size() * 8);
             _3crc->LoadChunkData();  
   
6274              // 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
6275              if (einf && pVersion && pVersion->major == 3) pRIFF->MoveSubChunk(_3crc, einf);              if (einf && pVersion && pVersion->major == 3) pRIFF->MoveSubChunk(_3crc, einf);
6276          }          }
6277            { // must be performed in RAM here ...
6278                uint32_t* pData = (uint32_t*) _3crc->LoadChunkData();
6279                if (pData) {
6280                    File::SampleList::iterator iter = pSamples->begin();
6281                    File::SampleList::iterator end  = pSamples->end();
6282                    for (int index = 0; iter != end; ++iter, ++index) {
6283                        gig::Sample* pSample = (gig::Sample*) *iter;
6284                        pData[index*2]   = 1; // always 1
6285                        pData[index*2+1] = pSample->crc;
6286                    }
6287                }
6288            }
6289      }      }
6290            
6291      void File::UpdateFileOffsets() {      void File::UpdateFileOffsets() {

Legend:
Removed from v.2985  
changed lines
  Added in v.3115

  ViewVC Help
Powered by ViewVC