--- libgig/trunk/src/DLS.cpp 2006/02/10 19:23:59 839 +++ libgig/trunk/src/DLS.cpp 2007/04/11 16:33:56 1154 @@ -1,8 +1,8 @@ /*************************************************************************** * * - * libgig - C++ cross-platform Gigasampler format file loader library * + * libgig - C++ cross-platform Gigasampler format file access library * * * - * Copyright (C) 2003-2005 by Christian Schoenebeck * + * Copyright (C) 2003-2007 by Christian Schoenebeck * * * * * * This library is free software; you can redistribute it and/or modify * @@ -45,7 +45,7 @@ #define CONN_TRANSFORM_INVERT_SRC_ENCODE(x) ((x) ? 0x8000 : 0) #define CONN_TRANSFORM_INVERT_CTL_ENCODE(x) ((x) ? 0x0200 : 0) -#define DRUM_TYPE_MASK 0x00000001 +#define DRUM_TYPE_MASK 0x80000000 #define F_RGN_OPTION_SELFNONEXCLUSIVE 0x0001 @@ -142,15 +142,15 @@ const int iEntrySize = 12; // 12 bytes per connection block pArticulationCk->Resize(HeaderSize + Connections * iEntrySize); uint8_t* pData = (uint8_t*) pArticulationCk->LoadChunkData(); - memccpy(&pData[0], &HeaderSize, 1, 2); - memccpy(&pData[2], &Connections, 1, 2); + memcpy(&pData[0], &HeaderSize, 2); + memcpy(&pData[2], &Connections, 2); for (uint32_t i = 0; i < Connections; i++) { Connection::conn_block_t c = pConnections[i].ToConnBlock(); - memccpy(&pData[HeaderSize + i * iEntrySize], &c.source, 1, 2); - memccpy(&pData[HeaderSize + i * iEntrySize + 2], &c.control, 1, 2); - memccpy(&pData[HeaderSize + i * iEntrySize + 4], &c.destination, 1, 2); - memccpy(&pData[HeaderSize + i * iEntrySize + 6], &c.transform, 1, 2); - memccpy(&pData[HeaderSize + i * iEntrySize + 8], &c.scale, 1, 4); + memcpy(&pData[HeaderSize + i * iEntrySize], &c.source, 2); + memcpy(&pData[HeaderSize + i * iEntrySize + 2], &c.control, 2); + memcpy(&pData[HeaderSize + i * iEntrySize + 4], &c.destination, 2); + memcpy(&pData[HeaderSize + i * iEntrySize + 6], &c.transform, 2); + memcpy(&pData[HeaderSize + i * iEntrySize + 8], &c.scale, 4); } } @@ -228,11 +228,12 @@ /** @brief Constructor. * - * Initializes the info strings with values provided by a INFO list chunk. + * Initializes the info strings with values provided by an INFO list chunk. * - * @param list - pointer to a list chunk which contains a INFO list chunk + * @param list - pointer to a list chunk which contains an INFO list chunk */ Info::Info(RIFF::List* list) { + UseFixedLengthStrings = false; pResourceListChunk = list; if (list) { RIFF::List* lstINFO = list->GetSubList(LIST_TYPE_INFO); @@ -253,6 +254,7 @@ LoadString(CHUNK_ID_ISRC, lstINFO, Source); LoadString(CHUNK_ID_ISRF, lstINFO, SourceForm); LoadString(CHUNK_ID_ICMS, lstINFO, Commissioned); + LoadString(CHUNK_ID_ISBJ, lstINFO, Subject); } } } @@ -267,15 +269,7 @@ */ void Info::LoadString(uint32_t ChunkID, RIFF::List* lstINFO, String& s) { RIFF::Chunk* ck = lstINFO->GetSubChunk(ChunkID); - if (ck) { - const char* str = (char*)ck->LoadChunkData(); - int size = ck->GetSize(); - int len; - for (len = 0 ; len < size ; len++) - if (str[len] == '\0') break; - s.assign(str, len); - ck->ReleaseChunkData(); - } + ::LoadString(ck, s); // function from helper.h } /** @brief Apply given INFO field to the respective chunk. @@ -292,19 +286,12 @@ * @param lstINFO - parent (INFO) RIFF list chunk * @param s - current value of info field * @param sDefault - default value + * @param bUseFixedLengthStrings - should a specific string size be forced in the chunk? + * @param size - wanted size of the INFO chunk. This is ignored if bUseFixedLengthStrings is false. */ - void Info::SaveString(uint32_t ChunkID, RIFF::List* lstINFO, const String& s, const String& sDefault) { + void Info::SaveString(uint32_t ChunkID, RIFF::List* lstINFO, const String& s, const String& sDefault, bool bUseFixedLengthStrings, int size) { RIFF::Chunk* ck = lstINFO->GetSubChunk(ChunkID); - if (ck) { // if chunk exists already, use 's' as value - ck->Resize(s.size() + 1); - char* pData = (char*) ck->LoadChunkData(); - memcpy(pData, s.c_str(), s.size() + 1); - } else if (s != "" || sDefault != "") { // create chunk - const String& sToSave = (s != "") ? s : sDefault; - ck = lstINFO->AddSubChunk(ChunkID, sToSave.size() + 1); - char* pData = (char*) ck->LoadChunkData(); - memcpy(pData, sToSave.c_str(), sToSave.size() + 1); - } + ::SaveString(ChunkID, ck, lstINFO, s, sDefault, bUseFixedLengthStrings, size); // function from helper.h } /** @brief Update chunks with current info values. @@ -317,36 +304,60 @@ // make sure INFO list chunk exists RIFF::List* lstINFO = pResourceListChunk->GetSubList(LIST_TYPE_INFO); - if (!lstINFO) lstINFO = pResourceListChunk->AddSubList(LIST_TYPE_INFO); - // assemble default values in case the respective chunk is missing yet - String defaultName = "NONAME"; - // get current date - time_t now = time(NULL); - tm* pNowBroken = localtime(&now); - String defaultCreationDate = ToString(1900 + pNowBroken->tm_year) + "-" + - ToString(pNowBroken->tm_mon + 1) + "-" + - ToString(pNowBroken->tm_mday); - String defaultSoftware = libraryName() + " " + libraryVersion(); - String defaultComments = "Created with " + libraryName() + " " + libraryVersion(); + String defaultName = ""; + String defaultCreationDate = ""; + String defaultSoftware = ""; + String defaultComments = ""; + + uint32_t resourceType = pResourceListChunk->GetListType(); + + if (!lstINFO) { + lstINFO = pResourceListChunk->AddSubList(LIST_TYPE_INFO); + + // assemble default values + defaultName = "NONAME"; + + if (resourceType == RIFF_TYPE_DLS) { + // get current date + time_t now = time(NULL); + tm* pNowBroken = localtime(&now); + char buf[11]; + strftime(buf, 11, "%F", pNowBroken); + defaultCreationDate = buf; + + defaultComments = "Created with " + libraryName() + " " + libraryVersion(); + } + if (resourceType == RIFF_TYPE_DLS || resourceType == LIST_TYPE_INS) + { + defaultSoftware = libraryName() + " " + libraryVersion(); + } + } // save values - SaveString(CHUNK_ID_INAM, lstINFO, Name, defaultName); - SaveString(CHUNK_ID_IARL, lstINFO, ArchivalLocation, String("")); - SaveString(CHUNK_ID_ICRD, lstINFO, CreationDate, defaultCreationDate); - SaveString(CHUNK_ID_ICMT, lstINFO, Comments, defaultComments); - SaveString(CHUNK_ID_IPRD, lstINFO, Product, String("")); - SaveString(CHUNK_ID_ICOP, lstINFO, Copyright, String("")); - SaveString(CHUNK_ID_IART, lstINFO, Artists, String("")); - SaveString(CHUNK_ID_IGNR, lstINFO, Genre, String("")); - SaveString(CHUNK_ID_IKEY, lstINFO, Keywords, String("")); - SaveString(CHUNK_ID_IENG, lstINFO, Engineer, String("")); - SaveString(CHUNK_ID_ITCH, lstINFO, Technician, String("")); - SaveString(CHUNK_ID_ISFT, lstINFO, Software, defaultSoftware); - SaveString(CHUNK_ID_IMED, lstINFO, Medium, String("")); - SaveString(CHUNK_ID_ISRC, lstINFO, Source, String("")); - SaveString(CHUNK_ID_ISRF, lstINFO, SourceForm, String("")); - SaveString(CHUNK_ID_ICMS, lstINFO, Commissioned, String("")); + + // (the string size values are for gig files; they are only + // used if UseFixedLengthStrings is set to true) + SaveString(CHUNK_ID_INAM, lstINFO, Name, defaultName, UseFixedLengthStrings, + resourceType == RIFF_TYPE_DLS ? 128 : 64); + SaveString(CHUNK_ID_IARL, lstINFO, ArchivalLocation, String(""), UseFixedLengthStrings, 256); + SaveString(CHUNK_ID_ICRD, lstINFO, CreationDate, defaultCreationDate, UseFixedLengthStrings, 128); + SaveString(CHUNK_ID_ICMT, lstINFO, Comments, defaultComments, UseFixedLengthStrings, 1024); + SaveString(CHUNK_ID_IPRD, lstINFO, Product, String(""), UseFixedLengthStrings, 128); + SaveString(CHUNK_ID_ICOP, lstINFO, Copyright, String(""), UseFixedLengthStrings, 128); + SaveString(CHUNK_ID_IART, lstINFO, Artists, String(""), UseFixedLengthStrings, 128); + SaveString(CHUNK_ID_IGNR, lstINFO, Genre, String(""), UseFixedLengthStrings, 128); + SaveString(CHUNK_ID_IKEY, lstINFO, Keywords, String(""), UseFixedLengthStrings, 128); + SaveString(CHUNK_ID_IENG, lstINFO, Engineer, String(""), UseFixedLengthStrings, 128); + SaveString(CHUNK_ID_ITCH, lstINFO, Technician, String(""), UseFixedLengthStrings, 128); + SaveString(CHUNK_ID_ISFT, lstINFO, Software, defaultSoftware, UseFixedLengthStrings, + resourceType == LIST_TYPE_INS ? + (Software == "" ? defaultSoftware.length()+1 : Software.length()+1) : 128); + SaveString(CHUNK_ID_IMED, lstINFO, Medium, String(""), UseFixedLengthStrings, 128); + SaveString(CHUNK_ID_ISRC, lstINFO, Source, String(""), UseFixedLengthStrings, 128); + SaveString(CHUNK_ID_ISRF, lstINFO, SourceForm, String(""), UseFixedLengthStrings, 128); + SaveString(CHUNK_ID_ICMS, lstINFO, Commissioned, String(""), UseFixedLengthStrings, 128); + SaveString(CHUNK_ID_ISBJ, lstINFO, Subject, String(""), UseFixedLengthStrings, 128); } @@ -452,19 +463,65 @@ } uint8_t* pData = (uint8_t*) wsmp->LoadChunkData(); // update headers size - memccpy(&pData[0], &uiHeaderSize, 1, 4); + memcpy(&pData[0], &uiHeaderSize, 4); // update respective sampler options bits SamplerOptions = (NoSampleDepthTruncation) ? SamplerOptions | F_WSMP_NO_TRUNCATION : SamplerOptions & (~F_WSMP_NO_TRUNCATION); SamplerOptions = (NoSampleCompression) ? SamplerOptions | F_WSMP_NO_COMPRESSION : SamplerOptions & (~F_WSMP_NO_COMPRESSION); + memcpy(&pData[4], &UnityNote, 2); + memcpy(&pData[6], &FineTune, 2); + memcpy(&pData[8], &Gain, 4); + memcpy(&pData[12], &SamplerOptions, 4); + memcpy(&pData[16], &SampleLoops, 4); // update loop definitions for (uint32_t i = 0; i < SampleLoops; i++) { //FIXME: this does not handle extended loop structs correctly - memccpy(&pData[uiHeaderSize + i * 16], pSampleLoops + i, 4, 4); + memcpy(&pData[uiHeaderSize + i * 16], pSampleLoops + i, 4 * 4); } } + /** + * Adds a new sample loop with the provided loop definition. + * + * @param - points to a loop definition that is to be copied + */ + void Sampler::AddSampleLoop(sample_loop_t* pLoopDef) { + sample_loop_t* pNewLoops = new sample_loop_t[SampleLoops + 1]; + // copy old loops array + for (int i = 0; i < SampleLoops; i++) { + pNewLoops[i] = pSampleLoops[i]; + } + // add the new loop + pNewLoops[SampleLoops] = *pLoopDef; + // free the old array and update the member variables + if (SampleLoops) delete[] pSampleLoops; + pSampleLoops = pNewLoops; + SampleLoops++; + } + + /** + * Deletes an existing sample loop. + * + * @param pLoopDef - pointer to existing loop definition + * @throws Exception - if given loop definition does not exist + */ + void Sampler::DeleteSampleLoop(sample_loop_t* pLoopDef) { + sample_loop_t* pNewLoops = new sample_loop_t[SampleLoops - 1]; + // copy old loops array (skipping given loop) + for (int i = 0, o = 0; i < SampleLoops; i++) { + if (&pSampleLoops[i] == pLoopDef) continue; + if (o == SampleLoops - 1) + throw Exception("Could not delete Sample Loop, because it does not exist"); + pNewLoops[o] = pSampleLoops[i]; + o++; + } + // free the old array and update the member variables + if (SampleLoops) delete[] pSampleLoops; + pSampleLoops = pNewLoops; + SampleLoops--; + } + // *************** Sample *************** @@ -498,16 +555,15 @@ AverageBytesPerSecond = pCkFormat->ReadUint32(); BlockAlign = pCkFormat->ReadUint16(); // PCM format specific - if (FormatTag == WAVE_FORMAT_PCM) { + if (FormatTag == DLS_WAVE_FORMAT_PCM) { BitDepth = pCkFormat->ReadUint16(); - FrameSize = (FormatTag == WAVE_FORMAT_PCM) ? (BitDepth / 8) * Channels - : 0; + FrameSize = (BitDepth / 8) * Channels; } else { // unsupported sample data format BitDepth = 0; FrameSize = 0; } } else { // 'fmt' chunk missing - FormatTag = WAVE_FORMAT_PCM; + FormatTag = DLS_WAVE_FORMAT_PCM; BitDepth = 16; Channels = 1; SamplesPerSecond = 44100; @@ -515,8 +571,8 @@ FrameSize = (BitDepth / 8) * Channels; BlockAlign = FrameSize; } - SamplesTotal = (pCkData) ? (FormatTag == WAVE_FORMAT_PCM) ? pCkData->GetSize() / FrameSize - : 0 + SamplesTotal = (pCkData) ? (FormatTag == DLS_WAVE_FORMAT_PCM) ? pCkData->GetSize() / FrameSize + : 0 : 0; } @@ -576,11 +632,11 @@ * the RIFF chunk which encapsulates the sample's wave data. The * returned value is dependant to the current FrameSize value. * - * @returns number of sample points or 0 if FormatTag != WAVE_FORMAT_PCM + * @returns number of sample points or 0 if FormatTag != DLS_WAVE_FORMAT_PCM * @see FrameSize, FormatTag */ unsigned long Sample::GetSize() { - if (FormatTag != WAVE_FORMAT_PCM) return 0; + if (FormatTag != DLS_WAVE_FORMAT_PCM) return 0; return (pCkData) ? pCkData->GetSize() / FrameSize : 0; } @@ -602,18 +658,18 @@ * calling File::Save() as this might exceed the current sample's * boundary! * - * Also note: only WAVE_FORMAT_PCM is currently supported, that is - * FormatTag must be WAVE_FORMAT_PCM. Trying to resize samples with + * Also note: only DLS_WAVE_FORMAT_PCM is currently supported, that is + * FormatTag must be DLS_WAVE_FORMAT_PCM. Trying to resize samples with * other formats will fail! * * @param iNewSize - new sample wave data size in sample points (must be * greater than zero) - * @throws Excecption if FormatTag != WAVE_FORMAT_PCM + * @throws Excecption if FormatTag != DLS_WAVE_FORMAT_PCM * @throws Exception if \a iNewSize is less than 1 * @see File::Save(), FrameSize, FormatTag */ void Sample::Resize(int iNewSize) { - if (FormatTag != WAVE_FORMAT_PCM) throw Exception("Sample's format is not WAVE_FORMAT_PCM"); + if (FormatTag != DLS_WAVE_FORMAT_PCM) throw Exception("Sample's format is not DLS_WAVE_FORMAT_PCM"); if (iNewSize < 1) throw Exception("Sample size must be at least one sample point"); const int iSizeInBytes = iNewSize * FrameSize; pCkData = pWaveList->GetSubChunk(CHUNK_ID_DATA); @@ -626,19 +682,19 @@ * bytes). Use this method and Read() if you don't want to load * the sample into RAM, thus for disk streaming. * - * Also note: only WAVE_FORMAT_PCM is currently supported, that is - * FormatTag must be WAVE_FORMAT_PCM. Trying to reposition the sample + * Also note: only DLS_WAVE_FORMAT_PCM is currently supported, that is + * FormatTag must be DLS_WAVE_FORMAT_PCM. Trying to reposition the sample * with other formats will fail! * * @param SampleCount number of sample points * @param Whence to which relation \a SampleCount refers to * @returns new position within the sample, 0 if - * FormatTag != WAVE_FORMAT_PCM + * FormatTag != DLS_WAVE_FORMAT_PCM * @throws Exception if no data RIFF chunk was created for the sample yet * @see FrameSize, FormatTag */ unsigned long Sample::SetPos(unsigned long SampleCount, RIFF::stream_whence_t Whence) { - if (FormatTag != WAVE_FORMAT_PCM) return 0; // failed: wave data not PCM format + if (FormatTag != DLS_WAVE_FORMAT_PCM) return 0; // failed: wave data not PCM format if (!pCkData) throw Exception("No data chunk created for sample yet, call Sample::Resize() to create one"); unsigned long orderedBytes = SampleCount * FrameSize; unsigned long result = pCkData->SetPos(orderedBytes, Whence); @@ -656,7 +712,7 @@ * @param SampleCount number of sample points to read */ unsigned long Sample::Read(void* pBuffer, unsigned long SampleCount) { - if (FormatTag != WAVE_FORMAT_PCM) return 0; // failed: wave data not PCM format + if (FormatTag != DLS_WAVE_FORMAT_PCM) return 0; // failed: wave data not PCM format return pCkData->Read(pBuffer, SampleCount, FrameSize); // FIXME: channel inversion due to endian correction? } @@ -676,7 +732,7 @@ * @see LoadSampleData() */ unsigned long Sample::Write(void* pBuffer, unsigned long SampleCount) { - if (FormatTag != WAVE_FORMAT_PCM) return 0; // failed: wave data not PCM format + if (FormatTag != DLS_WAVE_FORMAT_PCM) return 0; // failed: wave data not PCM format if (GetSize() < SampleCount) throw Exception("Could not write sample data, current sample size to small"); return pCkData->Write(pBuffer, SampleCount, FrameSize); // FIXME: channel inversion due to endian correction? } @@ -685,11 +741,11 @@ * Apply sample and its settings to the respective RIFF chunks. You have * to call File::Save() to make changes persistent. * - * @throws Exception if FormatTag != WAVE_FORMAT_PCM or no sample data + * @throws Exception if FormatTag != DLS_WAVE_FORMAT_PCM or no sample data * was provided yet */ void Sample::UpdateChunks() { - if (FormatTag != WAVE_FORMAT_PCM) + if (FormatTag != DLS_WAVE_FORMAT_PCM) throw Exception("Could not save sample, only PCM format is supported"); // we refuse to do anything if not sample wave form was provided yet if (!pCkData) @@ -701,12 +757,12 @@ if (!pCkFormat) pCkFormat = pWaveList->AddSubChunk(CHUNK_ID_FMT, 16); // assumes PCM format uint8_t* pData = (uint8_t*) pCkFormat->LoadChunkData(); // update 'fmt' chunk - memccpy(&pData[0], &FormatTag, 1, 2); - memccpy(&pData[2], &Channels, 1, 2); - memccpy(&pData[4], &SamplesPerSecond, 1, 4); - memccpy(&pData[8], &AverageBytesPerSecond, 1, 4); - memccpy(&pData[12], &BlockAlign, 1, 2); - memccpy(&pData[14], &BitDepth, 1, 2); // assuming PCM format + memcpy(&pData[0], &FormatTag, 2); + memcpy(&pData[2], &Channels, 2); + memcpy(&pData[4], &SamplesPerSecond, 4); + memcpy(&pData[8], &AverageBytesPerSecond, 4); + memcpy(&pData[12], &BlockAlign, 2); + memcpy(&pData[14], &BitDepth, 2); // assuming PCM format } @@ -798,20 +854,20 @@ void Region::UpdateChunks() { // make sure 'rgnh' chunk exists RIFF::Chunk* rgnh = pCkRegion->GetSubChunk(CHUNK_ID_RGNH); - if (!rgnh) rgnh = pCkRegion->AddSubChunk(CHUNK_ID_RGNH, 14); + if (!rgnh) rgnh = pCkRegion->AddSubChunk(CHUNK_ID_RGNH, Layer ? 14 : 12); uint8_t* pData = (uint8_t*) rgnh->LoadChunkData(); FormatOptionFlags = (SelfNonExclusive) ? FormatOptionFlags | F_RGN_OPTION_SELFNONEXCLUSIVE : FormatOptionFlags & (~F_RGN_OPTION_SELFNONEXCLUSIVE); // update 'rgnh' chunk - memccpy(&pData[0], &KeyRange, 2, 2); - memccpy(&pData[4], &VelocityRange, 2, 2); - memccpy(&pData[8], &FormatOptionFlags, 1, 2); - memccpy(&pData[10], &KeyGroup, 1, 2); - memccpy(&pData[12], &Layer, 1, 2); + memcpy(&pData[0], &KeyRange, 2 * 2); + memcpy(&pData[4], &VelocityRange, 2 * 2); + memcpy(&pData[8], &FormatOptionFlags, 2); + memcpy(&pData[10], &KeyGroup, 2); + if (rgnh->GetSize() >= 14) memcpy(&pData[12], &Layer, 2); - // update chunks of base classes as well - Resource::UpdateChunks(); + // update chunks of base classes as well (but skip Resource, + // as a rgn doesn't seem to have dlid and INFO chunks) Articulator::UpdateChunks(); Sampler::UpdateChunks(); @@ -841,10 +897,10 @@ if (index < 0) throw Exception("Could not save Region, could not find Region's sample"); WavePoolTableIndex = index; // update 'wlnk' chunk - memccpy(&pData[0], &WaveLinkOptionFlags, 1, 2); - memccpy(&pData[2], &PhaseGroup, 1, 2); - memccpy(&pData[4], &Channel, 1, 4); - memccpy(&pData[8], &WavePoolTableIndex, 1, 4); + memcpy(&pData[0], &WaveLinkOptionFlags, 2); + memcpy(&pData[2], &PhaseGroup, 2); + memcpy(&pData[4], &Channel, 4); + memcpy(&pData[8], &WavePoolTableIndex, 4); } @@ -927,6 +983,15 @@ return pNewRegion; } + void Instrument::MoveRegion(Region* pSrc, Region* pDst) { + RIFF::List* lrgn = pCkInstrument->GetSubList(LIST_TYPE_LRGN); + lrgn->MoveSubChunk(pSrc->pCkRegion, pDst ? pDst->pCkRegion : 0); + + pRegions->remove(pSrc); + RegionList::iterator iter = find(pRegions->begin(), pRegions->end(), pDst); + pRegions->insert(iter, pSrc); + } + void Instrument::DeleteRegion(Region* pRegion) { if (!pRegions) return; RegionList::iterator iter = find(pRegions->begin(), pRegions->end(), pRegion); @@ -957,8 +1022,8 @@ locale.bank = MIDI_BANK_ENCODE(MIDIBankCoarse, MIDIBankFine); locale.bank = (IsDrum) ? locale.bank | DRUM_TYPE_MASK : locale.bank & (~DRUM_TYPE_MASK); MIDIBank = MIDI_BANK_MERGE(MIDIBankCoarse, MIDIBankFine); // just a sync, when we're at it - memccpy(&pData[0], &Regions, 1, 4); - memccpy(&pData[4], &locale, 2, 4); + memcpy(&pData[0], &Regions, 4); + memcpy(&pData[4], &locale, 2 * 4); // update Region's chunks if (!pRegions) return; RegionList::iterator iter = pRegions->begin(); @@ -1043,25 +1108,32 @@ Instruments = colh->ReadUint32(); RIFF::Chunk* ptbl = pRIFF->GetSubChunk(CHUNK_ID_PTBL); - if (!ptbl) throw DLS::Exception("Mandatory chunk not found."); - WavePoolHeaderSize = ptbl->ReadUint32(); - WavePoolCount = ptbl->ReadUint32(); - pWavePoolTable = new uint32_t[WavePoolCount]; - pWavePoolTableHi = new uint32_t[WavePoolCount]; - ptbl->SetPos(WavePoolHeaderSize); - - // Check for 64 bit offsets (used in gig v3 files) - b64BitWavePoolOffsets = (ptbl->GetSize() - WavePoolHeaderSize == WavePoolCount * 8); - if (b64BitWavePoolOffsets) { - for (int i = 0 ; i < WavePoolCount ; i++) { - pWavePoolTableHi[i] = ptbl->ReadUint32(); - pWavePoolTable[i] = ptbl->ReadUint32(); - if (pWavePoolTable[i] & 0x80000000) - throw DLS::Exception("Files larger than 2 GB not yet supported"); + if (!ptbl) { // pool table is missing - this is probably an ".art" file + WavePoolCount = 0; + pWavePoolTable = NULL; + pWavePoolTableHi = NULL; + WavePoolHeaderSize = 8; + b64BitWavePoolOffsets = false; + } else { + WavePoolHeaderSize = ptbl->ReadUint32(); + WavePoolCount = ptbl->ReadUint32(); + pWavePoolTable = new uint32_t[WavePoolCount]; + pWavePoolTableHi = new uint32_t[WavePoolCount]; + ptbl->SetPos(WavePoolHeaderSize); + + // Check for 64 bit offsets (used in gig v3 files) + b64BitWavePoolOffsets = (ptbl->GetSize() - WavePoolHeaderSize == WavePoolCount * 8); + if (b64BitWavePoolOffsets) { + for (int i = 0 ; i < WavePoolCount ; i++) { + pWavePoolTableHi[i] = ptbl->ReadUint32(); + pWavePoolTable[i] = ptbl->ReadUint32(); + if (pWavePoolTable[i] & 0x80000000) + throw DLS::Exception("Files larger than 2 GB not yet supported"); + } + } else { // conventional 32 bit offsets + ptbl->Read(pWavePoolTable, WavePoolCount, sizeof(uint32_t)); + for (int i = 0 ; i < WavePoolCount ; i++) pWavePoolTableHi[i] = 0; } - } else { // conventional 32 bit offsets - ptbl->Read(pWavePoolTable, WavePoolCount, sizeof(uint32_t)); - for (int i = 0 ; i < WavePoolCount ; i++) pWavePoolTableHi[i] = 0; } pSamples = NULL; @@ -1247,7 +1319,7 @@ RIFF::Chunk* ckVersion = pRIFF->GetSubChunk(CHUNK_ID_VERS); if (!ckVersion) ckVersion = pRIFF->AddSubChunk(CHUNK_ID_VERS, 8); uint8_t* pData = (uint8_t*) ckVersion->LoadChunkData(); - memccpy(pData, pVersion, 2, 4); + memcpy(pData, pVersion, 2 * 4); } // update 'colh' chunk @@ -1255,7 +1327,7 @@ RIFF::Chunk* colh = pRIFF->GetSubChunk(CHUNK_ID_COLH); if (!colh) colh = pRIFF->AddSubChunk(CHUNK_ID_COLH, 4); uint8_t* pData = (uint8_t*) colh->LoadChunkData(); - memccpy(pData, &Instruments, 1, 4); + memcpy(pData, &Instruments, 4); // update instrument's chunks if (pInstruments) { @@ -1275,7 +1347,7 @@ ptbl->Resize(iPtblSize); pData = (uint8_t*) ptbl->LoadChunkData(); WavePoolCount = iSamples; - memccpy(&pData[4], &WavePoolCount, 1, 4); + memcpy(&pData[4], &WavePoolCount, 4); // we actually update the sample offsets in the pool table when we Save() memset(&pData[WavePoolHeaderSize], 0, iPtblSize - WavePoolHeaderSize);