--- libgig/trunk/src/DLS.cpp 2006/09/02 08:45:37 918 +++ libgig/trunk/src/DLS.cpp 2007/03/18 19:38:47 1106 @@ -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 * @@ -228,9 +228,9 @@ /** @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; @@ -254,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); } } } @@ -268,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. @@ -293,22 +286,12 @@ * @param lstINFO - parent (INFO) RIFF list chunk * @param s - current value of info field * @param sDefault - default value - * @param size - wanted size of the INFO chunk. This is ignored if UseFixedLengthStrings is false. + * @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, int size) { + 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 - if (!UseFixedLengthStrings) size = s.size() + 1; - ck->Resize(size); - char* pData = (char*) ck->LoadChunkData(); - strncpy(pData, s.c_str(), size); - } else if (s != "" || sDefault != "") { // create chunk - const String& sToSave = (s != "") ? s : sDefault; - if (!UseFixedLengthStrings) size = sToSave.size() + 1; - ck = lstINFO->AddSubChunk(ChunkID, size); - char* pData = (char*) ck->LoadChunkData(); - strncpy(pData, sToSave.c_str(), size); - } + ::SaveString(ChunkID, ck, lstINFO, s, sDefault, bUseFixedLengthStrings, size); // function from helper.h } /** @brief Update chunks with current info values. @@ -355,25 +338,26 @@ // (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, + SaveString(CHUNK_ID_INAM, lstINFO, Name, defaultName, UseFixedLengthStrings, resourceType == RIFF_TYPE_DLS ? 128 : 64); - SaveString(CHUNK_ID_IARL, lstINFO, ArchivalLocation, String(""), 256); - SaveString(CHUNK_ID_ICRD, lstINFO, CreationDate, defaultCreationDate, 128); - SaveString(CHUNK_ID_ICMT, lstINFO, Comments, defaultComments, 1024); - SaveString(CHUNK_ID_IPRD, lstINFO, Product, String(""), 128); - SaveString(CHUNK_ID_ICOP, lstINFO, Copyright, String(""), 128); - SaveString(CHUNK_ID_IART, lstINFO, Artists, String(""), 128); - SaveString(CHUNK_ID_IGNR, lstINFO, Genre, String(""), 128); - SaveString(CHUNK_ID_IKEY, lstINFO, Keywords, String(""), 128); - SaveString(CHUNK_ID_IENG, lstINFO, Engineer, String(""), 128); - SaveString(CHUNK_ID_ITCH, lstINFO, Technician, String(""), 128); - SaveString(CHUNK_ID_ISFT, lstINFO, Software, defaultSoftware, + 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() : Software.length()) : 128); - SaveString(CHUNK_ID_IMED, lstINFO, Medium, String(""), 128); - SaveString(CHUNK_ID_ISRC, lstINFO, Source, String(""), 128); - SaveString(CHUNK_ID_ISRF, lstINFO, SourceForm, String(""), 128); - SaveString(CHUNK_ID_ICMS, lstINFO, Commissioned, String(""), 128); + (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); } @@ -485,6 +469,11 @@ : 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 @@ -525,16 +514,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; @@ -542,8 +530,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; } @@ -603,11 +591,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; } @@ -629,18 +617,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); @@ -653,19 +641,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); @@ -683,7 +671,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? } @@ -703,7 +691,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? } @@ -712,11 +700,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) @@ -954,6 +942,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);