--- libgig/trunk/src/gig.cpp 2017/05/03 16:19:53 3140 +++ libgig/trunk/src/gig.cpp 2019/02/20 16:04:19 3474 @@ -2,7 +2,7 @@ * * * libgig - C++ cross-platform Gigasampler format file access library * * * - * Copyright (C) 2003-2017 by Christian Schoenebeck * + * Copyright (C) 2003-2019 by Christian Schoenebeck * * * * * * This library is free software; you can redistribute it and/or modify * @@ -341,6 +341,27 @@ +// *************** eg_opt_t *************** +// * + + eg_opt_t::eg_opt_t() { + AttackCancel = true; + AttackHoldCancel = true; + Decay1Cancel = true; + Decay2Cancel = true; + ReleaseCancel = true; + } + + void eg_opt_t::serialize(Serialization::Archive* archive) { + SRLZ(AttackCancel); + SRLZ(AttackHoldCancel); + SRLZ(Decay1Cancel); + SRLZ(Decay2Cancel); + SRLZ(ReleaseCancel); + } + + + // *************** Sample *************** // * @@ -447,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(); @@ -1711,6 +1732,29 @@ VCFType = vcf_type_lowpass; memset(DimensionUpperLimits, 127, 8); } + // chunk for own format extensions, these will *NOT* work with Gigasampler/GigaStudio ! + RIFF::Chunk* lsde = _3ewl->GetSubChunk(CHUNK_ID_LSDE); + if (lsde) { // format extension for EG behavior options + eg_opt_t* pEGOpts[2] = { &EG1Options, &EG2Options }; + 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); + pEGOpts[i]->Decay1Cancel = byte & (1 << 2); + pEGOpts[i]->Decay2Cancel = byte & (1 << 3); + 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, @@ -1817,6 +1861,11 @@ } void DimensionRegion::serialize(Serialization::Archive* archive) { + // in case this class will become backward incompatible one day, + // then set a version and minimum version for this class like: + //archive->setVersion(*this, 2); + //archive->setMinVersion(*this, 1); + SRLZ(VelocityUpperLimit); SRLZ(EG1PreAttack); SRLZ(EG1Attack); @@ -1894,6 +1943,10 @@ SRLZ(MSDecode); //SRLZ(SampleStartOffset); SRLZ(SampleAttenuation); + SRLZ(EG1Options); + SRLZ(EG2Options); + SRLZ(SustainReleaseTrigger); + SRLZ(NoNoteOffReleaseTrigger); // derived attributes from DLS::Sampler SRLZ(FineTune); @@ -1933,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(); @@ -2200,6 +2253,41 @@ if (chunksize >= 148) { memcpy(&pData[140], DimensionUpperLimits, 8); } + + // 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 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)) || + SustainReleaseTrigger || NoNoteOffReleaseTrigger) + { + 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) { // 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) | + (pEGOpts[i]->Decay1Cancel ? (1<<2) : 0) | + (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); + } } double* DimensionRegion::GetReleaseVelocityTable(curve_type_t releaseVelocityResponseCurve, uint8_t releaseVelocityResponseDepth) { @@ -2239,6 +2327,33 @@ // get the corresponding velocity table from the table map or create & calculate that table if it doesn't exist yet double* DimensionRegion::GetVelocityTable(curve_type_t curveType, uint8_t depth, uint8_t scaling) { + // sanity check input parameters + // (fallback to some default parameters on ill input) + switch (curveType) { + case curve_type_nonlinear: + case curve_type_linear: + if (depth > 4) { + printf("Warning: Invalid depth (0x%x) for velocity curve type (0x%x).\n", depth, curveType); + depth = 0; + scaling = 0; + } + break; + case curve_type_special: + if (depth > 5) { + printf("Warning: Invalid depth (0x%x) for velocity curve type 'special'.\n", depth); + depth = 0; + scaling = 0; + } + break; + case curve_type_unknown: + default: + printf("Warning: Unknown velocity curve type (0x%x).\n", curveType); + curveType = curve_type_linear; + depth = 0; + scaling = 0; + break; + } + double* table; uint32_t tableKey = (curveType<<16) | (depth<<8) | scaling; if (pVelocityTables->count(tableKey)) { // if key exists @@ -2614,7 +2729,10 @@ // unknown controller type default: - throw gig::Exception("Unknown leverage controller type."); + decodedcontroller.type = leverage_ctrl_t::type_none; + decodedcontroller.controller_number = 0; + printf("Warning: Unknown leverage controller type (0x%x).\n", EncodedController); + break; } return decodedcontroller; } @@ -3108,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 @@ -3152,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); @@ -3161,7 +3279,8 @@ if (file->GetAutoLoad()) { for (uint i = 0; i < DimensionRegions; i++) { uint32_t wavepoolindex = _3lnk->ReadUint32(); - if (file->pWavePoolTable) pDimensionRegions[i]->pSample = GetSampleFromWavePool(wavepoolindex); + if (file->pWavePoolTable && pDimensionRegions[i]) + pDimensionRegions[i]->pSample = GetSampleFromWavePool(wavepoolindex); } GetSample(); // load global region sample reference } @@ -3210,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); @@ -3241,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) { @@ -3384,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 @@ -4042,6 +4161,7 @@ if ((int32_t)WavePoolTableIndex == -1) return NULL; File* file = (File*) GetParent()->GetParent(); if (!file->pWavePoolTable) return NULL; + if (WavePoolTableIndex + 1 > file->WavePoolCount) return NULL; // for new files or files >= 2 GB use 64 bit wave pool offsets if (file->pRIFF->IsNew() || (file->pRIFF->GetCurrentFileSize() >> 31)) { // use 64 bit wave pool offsets (treating this as large file) @@ -4668,7 +4788,9 @@ RegionList::iterator end = pRegions->end(); for (; iter != end; ++iter) { gig::Region* pRegion = static_cast(*iter); - for (int iKey = pRegion->KeyRange.low; iKey <= pRegion->KeyRange.high; iKey++) { + const int low = std::max(int(pRegion->KeyRange.low), 0); + const int high = std::min(int(pRegion->KeyRange.high), 127); + for (int iKey = low; iKey <= high; iKey++) { RegionKeyTable[iKey] = pRegion; } } @@ -4713,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); } @@ -5295,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) { @@ -5390,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 }, @@ -5486,6 +5613,20 @@ return static_cast( *it ); } + /** + * Returns the total amount of samples of this gig file. + * + * Note that this method might block for a long time in case it is required + * to load the sample info for the first time. + * + * @returns total amount of samples + */ + size_t File::CountSamples() { + if (!pSamples) LoadSamples(); + if (!pSamples) return 0; + return pSamples->size(); + } + /** @brief Add a new sample. * * This will create a new Sample object for the gig file. You have to @@ -5561,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_whence_t::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(); @@ -5586,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 @@ -5621,6 +5811,20 @@ } /** + * Returns the total amount of instruments of this gig file. + * + * Note that this method might block for a long time in case it is required + * to load the instruments info for the first time. + * + * @returns total amount of instruments + */ + size_t File::CountInstruments() { + if (!pInstruments) LoadInstruments(); + if (!pInstruments) return 0; + return pInstruments->size(); + } + + /** * Returns the instrument with the given index. * * @param index - number of the sought instrument (0..n) @@ -5939,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); @@ -6088,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)); @@ -6264,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); @@ -6410,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(); @@ -6468,7 +6672,18 @@ // *************** Exception *************** // * - Exception::Exception(String Message) : DLS::Exception(Message) { + Exception::Exception() : DLS::Exception() { + } + + Exception::Exception(String format, ...) : DLS::Exception() { + va_list arg; + va_start(arg, format); + Message = assemble(format, arg); + va_end(arg); + } + + Exception::Exception(String format, va_list arg) : DLS::Exception() { + Message = assemble(format, arg); } void Exception::PrintMessage() {