--- libgig/trunk/src/gig.cpp 2018/01/30 20:17:12 3414 +++ libgig/trunk/src/gig.cpp 2019/02/20 17:06:11 3475 @@ -2,7 +2,7 @@ * * * libgig - C++ cross-platform Gigasampler format file access library * * * - * Copyright (C) 2003-2018 by Christian Schoenebeck * + * Copyright (C) 2003-2019 by Christian Schoenebeck * * * * * * This library is free software; you can redistribute it and/or modify * @@ -468,7 +468,7 @@ TruncatedBits = 0; if (Compressed) { uint32_t version = ewav->ReadInt32(); - if (version == 3 && BitDepth == 24) { + if (version > 2 && BitDepth == 24) { Dithered = ewav->ReadInt32(); ewav->SetPos(Channels == 2 ? 84 : 64); TruncatedBits = ewav->ReadInt32(); @@ -1732,12 +1732,11 @@ VCFType = vcf_type_lowpass; memset(DimensionUpperLimits, 127, 8); } - // format extension for EG behavior options, these will *NOT* work with - // Gigasampler/GigaStudio ! + // chunk for own format extensions, these will *NOT* work with Gigasampler/GigaStudio ! RIFF::Chunk* lsde = _3ewl->GetSubChunk(CHUNK_ID_LSDE); - if (lsde) { + if (lsde) { // format extension for EG behavior options eg_opt_t* pEGOpts[2] = { &EG1Options, &EG2Options }; - for (int i = 0; i < 2; ++i) { + for (int i = 0; i < 2; ++i) { // NOTE: we reserved a 3rd byte for a potential future EG3 option unsigned char byte = lsde->ReadUint8(); pEGOpts[i]->AttackCancel = byte & 1; pEGOpts[i]->AttackHoldCancel = byte & (1 << 1); @@ -1746,6 +1745,16 @@ pEGOpts[i]->ReleaseCancel = byte & (1 << 4); } } + // format extension for sustain pedal up effect on release trigger samples + if (lsde && lsde->GetSize() > 3) { // NOTE: we reserved the 3rd byte for a potential future EG3 option + lsde->SetPos(3); + uint8_t byte = lsde->ReadUint8(); + SustainReleaseTrigger = static_cast(byte & 0x03); + NoNoteOffReleaseTrigger = byte >> 7; + } else { + SustainReleaseTrigger = sust_rel_trg_none; + NoNoteOffReleaseTrigger = false; + } pVelocityAttenuationTable = GetVelocityTable(VelocityResponseCurve, VelocityResponseDepth, @@ -1936,6 +1945,8 @@ SRLZ(SampleAttenuation); SRLZ(EG1Options); SRLZ(EG2Options); + SRLZ(SustainReleaseTrigger); + SRLZ(NoNoteOffReleaseTrigger); // derived attributes from DLS::Sampler SRLZ(FineTune); @@ -1975,8 +1986,8 @@ RIFF::Chunk* _3ewa = pParentList->GetSubChunk(CHUNK_ID_3EWA); if (!_3ewa) { File* pFile = (File*) GetParent()->GetParent()->GetParent(); - bool version3 = pFile->pVersion && pFile->pVersion->major == 3; - _3ewa = pParentList->AddSubChunk(CHUNK_ID_3EWA, version3 ? 148 : 140); + bool versiongt2 = pFile->pVersion && pFile->pVersion->major > 2; + _3ewa = pParentList->AddSubChunk(CHUNK_ID_3EWA, versiongt2 ? 148 : 140); } pData = (uint8_t*) _3ewa->LoadChunkData(); @@ -2243,25 +2254,30 @@ memcpy(&pData[140], DimensionUpperLimits, 8); } - // format extension for EG behavior options, these will *NOT* work with + // chunk for own format extensions, these will *NOT* work with // Gigasampler/GigaStudio ! RIFF::Chunk* lsde = pParentList->GetSubChunk(CHUNK_ID_LSDE); + const int lsdeSize = 4; // NOTE: we reserved the 3rd byte for a potential future EG3 option if (!lsde) { - // only add this "LSDE" chunk if the EG options do not match the - // default EG behavior + // only add this "LSDE" chunk if either EG options or release + // trigger options deviate from their default behaviour eg_opt_t defaultOpt; if (memcmp(&EG1Options, &defaultOpt, sizeof(eg_opt_t)) || - memcmp(&EG2Options, &defaultOpt, sizeof(eg_opt_t))) + memcmp(&EG2Options, &defaultOpt, sizeof(eg_opt_t)) || + SustainReleaseTrigger || NoNoteOffReleaseTrigger) { - lsde = pParentList->AddSubChunk(CHUNK_ID_LSDE, 2); + lsde = pParentList->AddSubChunk(CHUNK_ID_LSDE, lsdeSize); // move LSDE chunk to the end of parent list pParentList->MoveSubChunk(lsde, (RIFF::Chunk*)NULL); } } if (lsde) { + if (lsde->GetNewSize() < lsdeSize) + lsde->Resize(lsdeSize); + // format extension for EG behavior options unsigned char* pData = (unsigned char*) lsde->LoadChunkData(); eg_opt_t* pEGOpts[2] = { &EG1Options, &EG2Options }; - for (int i = 0; i < 2; ++i) { + for (int i = 0; i < 2; ++i) { // NOTE: we reserved the 3rd byte for a potential future EG3 option pData[i] = (pEGOpts[i]->AttackCancel ? 1 : 0) | (pEGOpts[i]->AttackHoldCancel ? (1<<1) : 0) | @@ -2269,6 +2285,8 @@ (pEGOpts[i]->Decay2Cancel ? (1<<3) : 0) | (pEGOpts[i]->ReleaseCancel ? (1<<4) : 0); } + // format extension for release trigger options + pData[3] = static_cast(SustainReleaseTrigger) | (NoNoteOffReleaseTrigger ? (1<<7) : 0); } } @@ -3208,7 +3226,7 @@ } Layers = 1; File* file = (File*) GetParent()->GetParent(); - int dimensionBits = (file->pVersion && file->pVersion->major == 3) ? 8 : 5; + int dimensionBits = (file->pVersion && file->pVersion->major > 2) ? 8 : 5; // Actual Loading @@ -3252,7 +3270,7 @@ UpdateVelocityTable(); // jump to start of the wave pool indices (if not already there) - if (file->pVersion && file->pVersion->major == 3) + if (file->pVersion && file->pVersion->major > 2) _3lnk->SetPos(68); // version 3 has a different 3lnk structure else _3lnk->SetPos(44); @@ -3311,14 +3329,14 @@ } File* pFile = (File*) GetParent()->GetParent(); - bool version3 = pFile->pVersion && pFile->pVersion->major == 3; - const int iMaxDimensions = version3 ? 8 : 5; - const int iMaxDimensionRegions = version3 ? 256 : 32; + bool versiongt2 = pFile->pVersion && pFile->pVersion->major > 2; + const int iMaxDimensions = versiongt2 ? 8 : 5; + const int iMaxDimensionRegions = versiongt2 ? 256 : 32; // make sure '3lnk' chunk exists RIFF::Chunk* _3lnk = pCkRegion->GetSubChunk(CHUNK_ID_3LNK); if (!_3lnk) { - const int _3lnkChunkSize = version3 ? 1092 : 172; + const int _3lnkChunkSize = versiongt2 ? 1092 : 172; _3lnk = pCkRegion->AddSubChunk(CHUNK_ID_3LNK, _3lnkChunkSize); memset(_3lnk->LoadChunkData(), 0, _3lnkChunkSize); @@ -3342,7 +3360,7 @@ } // update wave pool table in '3lnk' chunk - const int iWavePoolOffset = version3 ? 68 : 44; + const int iWavePoolOffset = versiongt2 ? 68 : 44; for (uint i = 0; i < iMaxDimensionRegions; i++) { int iWaveIndex = -1; if (i < DimensionRegions) { @@ -3485,7 +3503,7 @@ // check if max. amount of dimensions reached File* file = (File*) GetParent()->GetParent(); - const int iMaxDimensions = (file->pVersion && file->pVersion->major == 3) ? 8 : 5; + const int iMaxDimensions = (file->pVersion && file->pVersion->major > 2) ? 8 : 5; if (Dimensions >= iMaxDimensions) throw gig::Exception("Could not add new dimension, max. amount of " + ToString(iMaxDimensions) + " dimensions already reached"); // check if max. amount of dimension bits reached @@ -4817,7 +4835,7 @@ File* pFile = (File*) GetParent(); // 3ewg is bigger in gig3, as it includes the iMIDI rules - int size = (pFile->pVersion && pFile->pVersion->major == 3) ? 16416 : 12; + int size = (pFile->pVersion && pFile->pVersion->major > 2) ? 16416 : 12; _3ewg = lart->AddSubChunk(CHUNK_ID_3EWG, size); memset(_3ewg->LoadChunkData(), 0, size); } @@ -5399,7 +5417,7 @@ RIFF::List* _3gnl = _3gri->GetSubList(LIST_TYPE_3GNL); if (!_3gnl) _3gnl = _3gri->AddSubList(LIST_TYPE_3GNL); - if (!pNameChunk && pFile->pVersion && pFile->pVersion->major == 3) { + if (!pNameChunk && pFile->pVersion && pFile->pVersion->major > 2) { // v3 has a fixed list of 128 strings, find a free one for (RIFF::Chunk* ck = _3gnl->GetFirstSubChunk() ; ck ; ck = _3gnl->GetNextSubChunk()) { if (strcmp(static_cast(ck->LoadChunkData()), "") == 0) { @@ -5494,6 +5512,11 @@ 0, 3, 20030331 & 0xffff, 20030331 >> 16 }; + /// Reflects Gigasampler file format version 4.0 (2007-10-12). + const DLS::version_t File::VERSION_4 = { + 0, 4, 20071012 & 0xffff, 20071012 >> 16 + }; + static const DLS::Info::string_length_t _FileFixedStringLengths[] = { { CHUNK_ID_IARL, 256 }, { CHUNK_ID_IART, 128 }, @@ -5679,20 +5702,78 @@ int iSampleIndex = 0; int iTotalSamples = WavePoolCount; - // check if samples should be loaded from extension files - // (only for old gig files < 2 GB) - int lastFileNo = 0; - if (!file->IsNew() && !(file->GetCurrentFileSize() >> 31)) { - for (int i = 0 ; i < WavePoolCount ; i++) { - if (pWavePoolTableHi[i] > lastFileNo) lastFileNo = pWavePoolTableHi[i]; - } - } - String name(pRIFF->GetFileName()); - int nameLen = (int) name.length(); - char suffix[6]; - if (nameLen > 4 && name.substr(nameLen - 4) == ".gig") nameLen -= 4; - - for (int fileNo = 0 ; ; ) { + // just for assembling path of optional extension files to be read + const std::string folder = parentPath(pRIFF->GetFileName()); + const std::string baseName = pathWithoutExtension(pRIFF->GetFileName()); + + // the main gig file and the extension files (.gx01, ... , .gx98) may + // contain wave data (wave pool) + std::vector poolFiles; + poolFiles.push_back(pRIFF); + + // get info about all extension files + RIFF::Chunk* ckXfil = pRIFF->GetSubChunk(CHUNK_ID_XFIL); + if (ckXfil) { // there are extension files (.gx01, ... , .gx98) ... + const uint32_t n = ckXfil->ReadInt32(); + for (int i = 0; i < n; i++) { + // read the filename and load the extension file + std::string name; + ckXfil->ReadString(name, 128); + std::string path = concatPath(folder, name); + RIFF::File* pExtFile = new RIFF::File(path); + // check that the dlsids match + RIFF::Chunk* ckDLSID = pExtFile->GetSubChunk(CHUNK_ID_DLID); + if (ckDLSID) { + ::DLS::dlsid_t idExpected; + idExpected.ulData1 = ckXfil->ReadInt32(); + idExpected.usData2 = ckXfil->ReadInt16(); + idExpected.usData3 = ckXfil->ReadInt16(); + ckXfil->Read(idExpected.abData, 8, 1); + ::DLS::dlsid_t idFound; + ckDLSID->Read(&idFound.ulData1, 1, 4); + ckDLSID->Read(&idFound.usData2, 1, 2); + ckDLSID->Read(&idFound.usData3, 1, 2); + ckDLSID->Read(idFound.abData, 8, 1); + if (memcmp(&idExpected, &idFound, 16) != 0) + throw gig::Exception("dlsid mismatch for extension file: %s", path.c_str()); + } + poolFiles.push_back(pExtFile); + ExtensionFiles.push_back(pExtFile); + } + } + + // check if a .gx99 (GigaPulse) file exists + RIFF::Chunk* ckDoxf = pRIFF->GetSubChunk(CHUNK_ID_DOXF); + if (ckDoxf) { // there is a .gx99 (GigaPulse) file ... + std::string path = baseName + ".gx99"; + RIFF::File* pExtFile = new RIFF::File(path); + + // skip unused int and filename + ckDoxf->SetPos(132, RIFF::stream_curpos); + + // check that the dlsids match + RIFF::Chunk* ckDLSID = pExtFile->GetSubChunk(CHUNK_ID_DLID); + if (ckDLSID) { + ::DLS::dlsid_t idExpected; + idExpected.ulData1 = ckDoxf->ReadInt32(); + idExpected.usData2 = ckDoxf->ReadInt16(); + idExpected.usData3 = ckDoxf->ReadInt16(); + ckDoxf->Read(idExpected.abData, 8, 1); + ::DLS::dlsid_t idFound; + ckDLSID->Read(&idFound.ulData1, 1, 4); + ckDLSID->Read(&idFound.usData2, 1, 2); + ckDLSID->Read(&idFound.usData3, 1, 2); + ckDLSID->Read(idFound.abData, 8, 1); + if (memcmp(&idExpected, &idFound, 16) != 0) + throw gig::Exception("dlsid mismatch for GigaPulse file: %s", path.c_str()); + } + poolFiles.push_back(pExtFile); + ExtensionFiles.push_back(pExtFile); + } + + // load samples from extension files (if required) + for (int i = 0; i < poolFiles.size(); i++) { + RIFF::File* file = poolFiles[i]; RIFF::List* wvpl = file->GetSubList(LIST_TYPE_WVPL); if (wvpl) { file_offset_t wvplFileOffset = wvpl->GetFilePos(); @@ -5704,22 +5785,13 @@ __notify_progress(pProgress, subprogress); file_offset_t waveFileOffset = wave->GetFilePos(); - pSamples->push_back(new Sample(this, wave, waveFileOffset - wvplFileOffset, fileNo, iSampleIndex)); + pSamples->push_back(new Sample(this, wave, waveFileOffset - wvplFileOffset, i, iSampleIndex)); iSampleIndex++; } wave = wvpl->GetNextSubList(); } - - if (fileNo == lastFileNo) break; - - // open extension file (*.gx01, *.gx02, ...) - fileNo++; - sprintf(suffix, ".gx%02d", fileNo); - name.replace(nameLen, 5, suffix); - file = new RIFF::File(name); - ExtensionFiles.push_back(file); - } else break; + } } __notify_progress(pProgress, 1.0); // notify done @@ -6071,7 +6143,7 @@ _3crc = pRIFF->AddSubChunk(CHUNK_ID_3CRC, pSamples->size() * 8); // the order of einf and 3crc is not the same in v2 and v3 RIFF::Chunk* einf = pRIFF->GetSubChunk(CHUNK_ID_EINF); - if (einf && pVersion && pVersion->major == 3) pRIFF->MoveSubChunk(_3crc, einf); + if (einf && pVersion && pVersion->major > 2) pRIFF->MoveSubChunk(_3crc, einf); bRequiresSave = true; } else if (_3crc->GetNewSize() != pSamples->size() * 8) { _3crc->Resize(pSamples->size() * 8); @@ -6220,7 +6292,7 @@ RIFF::Chunk* ck = lst3gnl->GetFirstSubChunk(); while (ck) { if (ck->GetChunkID() == CHUNK_ID_3GNM) { - if (pVersion && pVersion->major == 3 && + if (pVersion && pVersion->major > 2 && strcmp(static_cast(ck->LoadChunkData()), "") == 0) break; pGroups->push_back(new Group(this, ck)); @@ -6396,7 +6468,7 @@ // v3: make sure the file has 128 3gnm chunks // (before updating the Group chunks) - if (pVersion && pVersion->major == 3) { + if (pVersion && pVersion->major > 2) { RIFF::Chunk* _3gnm = _3gnl->GetFirstSubChunk(); for (int i = 0 ; i < 128 ; i++) { if (i >= pGroups->size()) ::SaveString(CHUNK_ID_3GNM, _3gnm, _3gnl, "", "", true, 64); @@ -6542,7 +6614,7 @@ } else /*if (newFile)*/ { _3crc = pRIFF->AddSubChunk(CHUNK_ID_3CRC, pSamples->size() * 8); // the order of einf and 3crc is not the same in v2 and v3 - if (einf && pVersion && pVersion->major == 3) pRIFF->MoveSubChunk(_3crc, einf); + if (einf && pVersion && pVersion->major > 2) pRIFF->MoveSubChunk(_3crc, einf); } { // must be performed in RAM here ... uint32_t* pData = (uint32_t*) _3crc->LoadChunkData();