/[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 2912 by schoenebeck, Tue May 17 14:30:10 2016 UTC revision 2989 by schoenebeck, Sat Sep 24 14:00:46 2016 UTC
# Line 318  namespace { Line 318  namespace {
318  // *************** Sample ***************  // *************** Sample ***************
319  // *  // *
320    
321      unsigned int Sample::Instances = 0;      size_t       Sample::Instances = 0;
322      buffer_t     Sample::InternalDecompressionBuffer;      buffer_t     Sample::InternalDecompressionBuffer;
323    
324      /** @brief Constructor.      /** @brief Constructor.
# 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 789  namespace { Line 802  namespace {
802       * FormatTag must be DLS_WAVE_FORMAT_PCM. Trying to resize samples with       * FormatTag must be DLS_WAVE_FORMAT_PCM. Trying to resize samples with
803       * other formats will fail!       * other formats will fail!
804       *       *
805       * @param iNewSize - new sample wave data size in sample points (must be       * @param NewSize - new sample wave data size in sample points (must be
806       *                   greater than zero)       *                  greater than zero)
807       * @throws DLS::Excecption if FormatTag != DLS_WAVE_FORMAT_PCM       * @throws DLS::Excecption if FormatTag != DLS_WAVE_FORMAT_PCM
808       *                         or if \a iNewSize is less than 1       * @throws DLS::Exception if \a NewSize is less than 1 or unrealistic large
809       * @throws gig::Exception if existing sample is compressed       * @throws gig::Exception if existing sample is compressed
810       * @see DLS::Sample::GetSize(), DLS::Sample::FrameSize,       * @see DLS::Sample::GetSize(), DLS::Sample::FrameSize,
811       *      DLS::Sample::FormatTag, File::Save()       *      DLS::Sample::FormatTag, File::Save()
812       */       */
813      void Sample::Resize(int iNewSize) {      void Sample::Resize(file_offset_t NewSize) {
814          if (Compressed) throw gig::Exception("There is no support for modifying compressed samples (yet)");          if (Compressed) throw gig::Exception("There is no support for modifying compressed samples (yet)");
815          DLS::Sample::Resize(iNewSize);          DLS::Sample::Resize(NewSize);
816      }      }
817    
818      /**      /**
# Line 1341  namespace { Line 1354  namespace {
1354          return pGroup;          return pGroup;
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
1375         * 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
1377         * is calculated and stored/updated for this sample, along to the sample's
1378         * meta informations.
1379         *
1380         * Now by calling this method the current raw audio wave data is checked
1381         * against the already stored CRC32 check sum in order to check whether the
1382         * sample data had been damaged unintentionally for some reason. Since by
1383         * calling this method always the entire raw audio wave data has to be
1384         * read, verifying all samples this way may take a long time accordingly.
1385         * And that's also the reason why the sample integrity is not checked by
1386         * default whenever a gig file is loaded. So this method must be called
1387         * 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
1394         * @throws Exception if no checksum had been stored to disk for this
1395         *         sample yet, or on I/O issues
1396         * @see GetWaveDataCRC32Checksum()
1397         */
1398        bool Sample::VerifyWaveData(uint32_t* pActually) {
1399            File* pFile = static_cast<File*>(GetParent());
1400            uint32_t crc = CalculateWaveDataChecksum();
1401            if (pActually) *pActually = crc;
1402            return crc == this->crc;
1403        }
1404    
1405        uint32_t Sample::CalculateWaveDataChecksum() {
1406            const size_t sz = 20*1024; // 20kB buffer size
1407            std::vector<uint8_t> buffer(sz);
1408            buffer.resize(sz);
1409    
1410            const size_t n = sz / FrameSize;
1411            SetPos(0);
1412            uint32_t crc = 0;
1413            __resetCRC(crc);
1414            while (true) {
1415                file_offset_t nRead = Read(&buffer[0], n);
1416                if (nRead <= 0) break;
1417                __calculateCRC(&buffer[0], nRead * FrameSize, crc);
1418            }
1419            __encodeCRC(crc);
1420            return crc;
1421        }
1422    
1423      Sample::~Sample() {      Sample::~Sample() {
1424          Instances--;          Instances--;
1425          if (!Instances && InternalDecompressionBuffer.Size) {          if (!Instances && InternalDecompressionBuffer.Size) {
# Line 1357  namespace { Line 1436  namespace {
1436  // *************** DimensionRegion ***************  // *************** DimensionRegion ***************
1437  // *  // *
1438    
1439      uint                               DimensionRegion::Instances       = 0;      size_t                             DimensionRegion::Instances       = 0;
1440      DimensionRegion::VelocityTableMap* DimensionRegion::pVelocityTables = NULL;      DimensionRegion::VelocityTableMap* DimensionRegion::pVelocityTables = NULL;
1441    
1442      DimensionRegion::DimensionRegion(Region* pParent, RIFF::List* _3ewl) : DLS::Sampler(_3ewl) {      DimensionRegion::DimensionRegion(Region* pParent, RIFF::List* _3ewl) : DLS::Sampler(_3ewl) {
# Line 3106  namespace { Line 3185  namespace {
3185          int step = 1;          int step = 1;
3186          for (int i = 0 ; i < veldim ; i++) step <<= pDimensionDefinitions[i].bits;          for (int i = 0 ; i < veldim ; i++) step <<= pDimensionDefinitions[i].bits;
3187          int skipveldim = (step << pDimensionDefinitions[veldim].bits) - step;          int skipveldim = (step << pDimensionDefinitions[veldim].bits) - step;
         int end = step * pDimensionDefinitions[veldim].zones;  
3188    
3189          // loop through all dimension regions for all dimensions except the velocity dimension          // loop through all dimension regions for all dimensions except the velocity dimension
3190          int dim[8] = { 0 };          int dim[8] = { 0 };
3191          for (int i = 0 ; i < DimensionRegions ; i++) {          for (int i = 0 ; i < DimensionRegions ; i++) {
3192                const int end = i + step * pDimensionDefinitions[veldim].zones;
3193    
3194                // create a velocity table for all cases where the velocity zone is zero
3195              if (pDimensionRegions[i]->DimensionUpperLimits[veldim] ||              if (pDimensionRegions[i]->DimensionUpperLimits[veldim] ||
3196                  pDimensionRegions[i]->VelocityUpperLimit) {                  pDimensionRegions[i]->VelocityUpperLimit) {
3197                  // create the velocity table                  // create the velocity table
# Line 3142  namespace { Line 3222  namespace {
3222                  }                  }
3223              }              }
3224    
3225                // jump to the next case where the velocity zone is zero
3226              int j;              int j;
3227              int shift = 0;              int shift = 0;
3228              for (j = 0 ; j < Dimensions ; j++) {              for (j = 0 ; j < Dimensions ; j++) {
# Line 3850  namespace { Line 3931  namespace {
3931          if ((int32_t)WavePoolTableIndex == -1) return NULL;          if ((int32_t)WavePoolTableIndex == -1) return NULL;
3932          File* file = (File*) GetParent()->GetParent();          File* file = (File*) GetParent()->GetParent();
3933          if (!file->pWavePoolTable) return NULL;          if (!file->pWavePoolTable) return NULL;
3934          if (file->HasMonolithicLargeFilePolicy()) {          // for new files or files >= 2 GB use 64 bit wave pool offsets
3935            if (file->pRIFF->IsNew() || (file->pRIFF->GetCurrentFileSize() >> 31)) {
3936                // use 64 bit wave pool offsets (treating this as large file)
3937              uint64_t soughtoffset =              uint64_t soughtoffset =
3938                  uint64_t(file->pWavePoolTable[WavePoolTableIndex]) |                  uint64_t(file->pWavePoolTable[WavePoolTableIndex]) |
3939                  uint64_t(file->pWavePoolTableHi[WavePoolTableIndex]) << 32;                  uint64_t(file->pWavePoolTableHi[WavePoolTableIndex]) << 32;
# Line 3861  namespace { Line 3944  namespace {
3944                  sample = file->GetNextSample();                  sample = file->GetNextSample();
3945              }              }
3946          } else {          } else {
3947                // use extension files and 32 bit wave pool offsets
3948              file_offset_t soughtoffset = file->pWavePoolTable[WavePoolTableIndex];              file_offset_t soughtoffset = file->pWavePoolTable[WavePoolTableIndex];
3949              file_offset_t soughtfileno = file->pWavePoolTableHi[WavePoolTableIndex];              file_offset_t soughtfileno = file->pWavePoolTableHi[WavePoolTableIndex];
3950              Sample* sample = file->GetFirstSample(pProgress);              Sample* sample = file->GetFirstSample(pProgress);
# Line 5348  namespace { Line 5432  namespace {
5432          int iTotalSamples = WavePoolCount;          int iTotalSamples = WavePoolCount;
5433    
5434          // check if samples should be loaded from extension files          // check if samples should be loaded from extension files
5435            // (only for old gig files < 2 GB)
5436          int lastFileNo = 0;          int lastFileNo = 0;
5437          if (!HasMonolithicLargeFilePolicy()) {          if (!file->IsNew() && !(file->GetCurrentFileSize() >> 31)) {
5438              for (int i = 0 ; i < WavePoolCount ; i++) {              for (int i = 0 ; i < WavePoolCount ; i++) {
5439                  if (pWavePoolTableHi[i] > lastFileNo) lastFileNo = pWavePoolTableHi[i];                  if (pWavePoolTableHi[i] > lastFileNo) lastFileNo = pWavePoolTableHi[i];
5440              }              }
# Line 5371  namespace { Line 5456  namespace {
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                      }                      }
# Line 5609  namespace { Line 5694  namespace {
5694          if (!_3crc) return;          if (!_3crc) return;
5695    
5696          // get the index of the sample          // get the index of the sample
5697          int iWaveIndex = -1;          int iWaveIndex = GetWaveTableIndexOf(pSample);
         File::SampleList::iterator iter = pSamples->begin();  
         File::SampleList::iterator end  = pSamples->end();  
         for (int index = 0; iter != end; ++iter, ++index) {  
             if (*iter == pSample) {  
                 iWaveIndex = index;  
                 break;  
             }  
         }  
5698          if (iWaveIndex < 0) throw gig::Exception("Could not update crc, could not find sample");          if (iWaveIndex < 0) throw gig::Exception("Could not update crc, could not find sample");
5699    
5700          // write the CRC-32 checksum to disk          // write the CRC-32 checksum to disk
5701          _3crc->SetPos(iWaveIndex * 8);          _3crc->SetPos(iWaveIndex * 8);
5702          uint32_t tmp = 1;          uint32_t one = 1;
5703          _3crc->WriteUint32(&tmp); // unknown, always 1?          _3crc->WriteUint32(&one); // always 1
5704          _3crc->WriteUint32(&crc);          _3crc->WriteUint32(&crc);
5705      }      }
5706    
5707        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);
5719            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();
5721            if (!pData) throw gig::Exception("Could not retrieve reference crc of sample, no checksums stored for this file yet");
5722    
5723            // read the CRC-32 checksum directly from disk
5724            size_t pos = index * 8;
5725            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");
5727    
5728            uint32_t one = load32(&pData[pos]); // always 1
5729            if (one != 1)
5730                throw gig::Exception("Could not retrieve reference crc of sample, because reference checksum table is damaged");
5731    
5732            return load32(&pData[pos+4]);
5733        }
5734    
5735        int File::GetWaveTableIndexOf(gig::Sample* pSample) {
5736            if (!pSamples) GetFirstSample(); // make sure sample chunks were scanned
5737            File::SampleList::iterator iter = pSamples->begin();
5738            File::SampleList::iterator end  = pSamples->end();
5739            for (int index = 0; iter != end; ++iter, ++index)
5740                if (*iter == pSample)
5741                    return index;
5742            return -1;
5743        }
5744    
5745        /**
5746         * Checks whether the file's "3CRC" chunk was damaged. This chunk contains
5747         * the CRC32 check sums of all samples' raw wave data.
5748         *
5749         * @return true if 3CRC chunk is OK, or false if 3CRC chunk is damaged
5750         */
5751        bool File::VerifySampleChecksumTable() {
5752            RIFF::Chunk* _3crc = pRIFF->GetSubChunk(CHUNK_ID_3CRC);
5753            if (!_3crc) return false;
5754            if (_3crc->GetNewSize() <= 0) return false;
5755            if (_3crc->GetNewSize() % 8) return false;
5756            if (!pSamples) GetFirstSample(); // make sure sample chunks were scanned
5757            if (_3crc->GetNewSize() != pSamples->size() * 8) return false;
5758    
5759            const int n = _3crc->GetNewSize() / 8;
5760    
5761            uint32_t* pData = (uint32_t*) _3crc->LoadChunkData();
5762            if (!pData) return false;
5763    
5764            for (int i = 0; i < n; ++i) {
5765                uint32_t one = pData[i*2];
5766                if (one != 1) return false;
5767            }
5768    
5769            return true;
5770        }
5771    
5772        /**
5773         * Recalculates CRC32 checksums for all samples and rebuilds this gig
5774         * file's checksum table with those new checksums. This might usually
5775         * just be necessary if the checksum table was damaged.
5776         *
5777         * @e IMPORTANT: The current implementation of this method only works
5778         * with files that have not been modified since it was loaded, because
5779         * it expects that no externally caused file structure changes are
5780         * required!
5781         *
5782         * Due to the expectation above, this method is currently protected
5783         * and actually only used by the command line tool "gigdump" yet.
5784         *
5785         * @returns true if Save() is required to be called after this call,
5786         *          false if no further action is required
5787         */
5788        bool File::RebuildSampleChecksumTable() {
5789            // make sure sample chunks were scanned
5790            if (!pSamples) GetFirstSample();
5791    
5792            bool bRequiresSave = false;
5793    
5794            // make sure "3CRC" chunk exists with required size
5795            RIFF::Chunk* _3crc = pRIFF->GetSubChunk(CHUNK_ID_3CRC);
5796            if (!_3crc) {
5797                _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;
5802            } else if (_3crc->GetNewSize() != pSamples->size() * 8) {
5803                _3crc->Resize(pSamples->size() * 8);
5804                bRequiresSave = true;
5805            }
5806    
5807            if (bRequiresSave) { // refill CRC table for all samples in RAM ...
5808                uint32_t* pData = (uint32_t*) _3crc->LoadChunkData();
5809                {
5810                    File::SampleList::iterator iter = pSamples->begin();
5811                    File::SampleList::iterator end  = pSamples->end();
5812                    for (; iter != end; ++iter) {
5813                        gig::Sample* pSample = (gig::Sample*) *iter;
5814                        int index = GetWaveTableIndexOf(pSample);
5815                        if (index < 0) throw gig::Exception("Could not rebuild crc table for samples, wave table index of a sample could not be resolved");
5816                        pData[index*2]   = 1; // always 1
5817                        pData[index*2+1] = pSample->CalculateWaveDataChecksum();
5818                    }
5819                }
5820            } else { // no file structure changes necessary, so directly write to disk and we are done ...
5821                // make sure file is in write mode
5822                pRIFF->SetMode(RIFF::stream_mode_read_write);
5823                {
5824                    File::SampleList::iterator iter = pSamples->begin();
5825                    File::SampleList::iterator end  = pSamples->end();
5826                    for (; iter != end; ++iter) {
5827                        gig::Sample* pSample = (gig::Sample*) *iter;
5828                        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");
5830                        pSample->crc  = pSample->CalculateWaveDataChecksum();
5831                        SetSampleChecksum(pSample, pSample->crc);
5832                    }
5833                }
5834            }
5835    
5836            return bRequiresSave;
5837        }
5838    
5839      Group* File::GetFirstGroup() {      Group* File::GetFirstGroup() {
5840          if (!pGroups) LoadGroups();          if (!pGroups) LoadGroups();
5841          // there must always be at least one group          // there must always be at least one group
# Line 5841  namespace { Line 6050  namespace {
6050          }          }
6051      }      }
6052    
     /** @brief Returns the version number of libgig's Giga file format extension.  
      *  
      * libgig added several new features which were not available with the  
      * original GigaStudio software. For those purposes libgig's own custom RIFF  
      * chunks were added to the Giga file format.  
      *  
      * This method returns the version number of the Giga file format extension  
      * used in this Giga file. Currently there are 3 possible values that might  
      * be returned by this method:  
      *  
      * - @c 0: This gig file is not using any libgig specific file format  
      *         extension at all.  
      * - @c 1: This gig file uses the RT instrument script format extension.  
      * - @c 2: This gig file additionally provides support for monolithic  
      *         large gig files (larger than 2 GB).  
      *  
      * @note This method is currently protected and shall not be used as public  
      * API method, since its method signature might change in future.  
      */  
     uint File::GetFormatExtensionVersion() const {  
         RIFF::List* lst3LS = pRIFF->GetSubList(LIST_TYPE_3LS);  
         if (!lst3LS) return 0; // is not using custom Giga format extensions at all  
         RIFF::Chunk* ckFFmt = lst3LS->GetSubChunk(CHUNK_ID_FFMT);  
         if (!ckFFmt) return 1; // uses custom Giga format extension(s) but had no format version saved  
         uint8_t* pData = (uint8_t*) ckFFmt->LoadChunkData();  
         return load32(pData);  
     }  
   
     /** @brief Returns true in case this file is stored as one, single monolithic gig file.  
      *  
      * To avoid issues with operating systems which did not support large files  
      * (larger than 2 GB) the original Giga file format avoided to ever save gig  
      * files larger than 2 GB, instead such large Giga files were splitted into  
      * several files, each one not being larger than 2 GB. It used a predefined  
      * file name scheme for them like this:  
      * @code  
      * foo.gig  
      * foo.gx01  
      * foo.gx02  
      * foo.gx03  
      * ...  
      * @endcode  
      * So when like in this example foo.gig was loaded, all other files  
      * (foo.gx01, ...) were automatically loaded as well to make up the overall  
      * large gig file (provided they were located at the same directory). Such  
      * additional .gxYY files were called "extension files".  
      *  
      * Since nowadays all modern systems support large files, libgig always  
      * saves large gig files as one single monolithic gig file instead, that  
      * is libgig won't split such a large gig file into separate files like the  
      * original GigaStudio software did. It uses a custom Giga file format  
      * extension for this feature.  
      *  
      * For still being able though to load old splitted gig files and the new  
      * large monolithic ones, this method is used to determine which loading  
      * policy must be used for this gig file.  
      *  
      * @note This method is currently protected and shall not be used as public  
      * API method, since its method signature might change in future and since  
      * this method should not be directly relevant for applications based on  
      * libgig.  
      */  
     bool File::HasMonolithicLargeFilePolicy() const {  
         RIFF::List* lst3LS = pRIFF->GetSubList(LIST_TYPE_3LS);  
         if (!lst3LS) return false;  
         RIFF::Chunk* ckFFmt = lst3LS->GetSubChunk(CHUNK_ID_FFMT);  
         if (!ckFFmt) return false;  
         uint8_t* pData = (uint8_t*) ckFFmt->LoadChunkData();  
         uint32_t formatBitField = load32(&pData[4]);  
         return formatBitField & 1;  
     }  
   
6053      /**      /**
6054       * Apply all the gig file's current instruments, samples, groups and settings       * Apply all the gig file's current instruments, samples, groups and settings
6055       * to the respective RIFF chunks. You have to call Save() to make changes       * to the respective RIFF chunks. You have to call Save() to make changes
# Line 5927  namespace { Line 6064  namespace {
6064      void File::UpdateChunks(progress_t* pProgress) {      void File::UpdateChunks(progress_t* pProgress) {
6065          bool newFile = pRIFF->GetSubList(LIST_TYPE_INFO) == NULL;          bool newFile = pRIFF->GetSubList(LIST_TYPE_INFO) == NULL;
6066    
         b64BitWavePoolOffsets = pVersion && pVersion->major == 3;  
   
6067          // update own gig format extension chunks          // update own gig format extension chunks
6068          // (not part of the GigaStudio 4 format)          // (not part of the GigaStudio 4 format)
6069          RIFF::List* lst3LS = pRIFF->GetSubList(LIST_TYPE_3LS);          RIFF::List* lst3LS = pRIFF->GetSubList(LIST_TYPE_3LS);
# Line 5936  namespace { Line 6071  namespace {
6071              lst3LS = pRIFF->AddSubList(LIST_TYPE_3LS);              lst3LS = pRIFF->AddSubList(LIST_TYPE_3LS);
6072          }          }
6073          // Make sure <3LS > chunk is placed before <ptbl> chunk. The precise          // Make sure <3LS > chunk is placed before <ptbl> chunk. The precise
6074          // location of <3LS > is irrelevant, however it MUST BE located BEFORE          // location of <3LS > is irrelevant, however it should be located
6075          // the actual wave data, otherwise the <3LS > chunk becomes          // before  the actual wave data
         // inaccessible on gig files larger than 4GB !  
6076          RIFF::Chunk* ckPTBL = pRIFF->GetSubChunk(CHUNK_ID_PTBL);          RIFF::Chunk* ckPTBL = pRIFF->GetSubChunk(CHUNK_ID_PTBL);
6077          pRIFF->MoveSubChunk(lst3LS, ckPTBL);          pRIFF->MoveSubChunk(lst3LS, ckPTBL);
6078    
         // Update <FFmt> chunk with informations about our file format  
         // extensions. Currently this <FFmt> chunk has the following  
         // layout:  
         //  
         // <uint32> -> (libgig's) File Format Extension version  
         // <uint32> -> Format bit field:  
         //             bit 0: If flag is not set use separate .gx01  
         //                    extension files if file is larger than 2 GB  
         //                    like with the original Giga format, if flag  
         //                    is set use 64 bit sample references and keep  
         //                    everything as one single monolithic gig file.  
         RIFF::Chunk* ckFFmt = lst3LS->GetSubChunk(CHUNK_ID_FFMT);  
         if (!ckFFmt) {  
             const int iChunkSize = 2 * sizeof(uint32_t);  
             ckFFmt = lst3LS->AddSubChunk(CHUNK_ID_FFMT, iChunkSize);  
         }  
         {  
             uint8_t* pData = (uint8_t*) ckFFmt->LoadChunkData();  
             store32(&pData[0], GIG_FILE_EXT_VERSION);  
             // for now we always save gig files larger than 2 GB as one  
             // single monolithic file (saving those with extension files is  
             // currently not supported and probably also not desired anymore  
             // nowadays).  
             uint32_t formatBitfield = 1;  
             store32(&pData[4], formatBitfield);  
         }  
6079          // This must be performed before writing the chunks for instruments,          // This must be performed before writing the chunks for instruments,
6080          // because the instruments' script slots will write the file offsets          // because the instruments' script slots will write the file offsets
6081          // of the respective instrument script chunk as reference.          // of the respective instrument script chunk as reference.
# Line 5980  namespace { Line 6088  namespace {
6088              }              }
6089          }          }
6090    
6091            // in case no libgig custom format data was added, then remove the
6092            // custom "3LS " chunk again
6093            if (!lst3LS->CountSubChunks()) {
6094                pRIFF->DeleteSubChunk(lst3LS);
6095                lst3LS = NULL;
6096            }
6097    
6098          // first update base class's chunks          // first update base class's chunks
6099          DLS::File::UpdateChunks(pProgress);          DLS::File::UpdateChunks(pProgress);
6100    
# Line 6136  namespace { Line 6251  namespace {
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);          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() {

Legend:
Removed from v.2912  
changed lines
  Added in v.2989

  ViewVC Help
Powered by ViewVC