DLS.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                                                         *
00003  *   libgig - C++ cross-platform Gigasampler format file access library    *
00004  *                                                                         *
00005  *   Copyright (C) 2003-2007 by Christian Schoenebeck                      *
00006  *                              <cuse@users.sourceforge.net>               *
00007  *                                                                         *
00008  *   This library is free software; you can redistribute it and/or modify  *
00009  *   it under the terms of the GNU General Public License as published by  *
00010  *   the Free Software Foundation; either version 2 of the License, or     *
00011  *   (at your option) any later version.                                   *
00012  *                                                                         *
00013  *   This library is distributed in the hope that it will be useful,       *
00014  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00015  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00016  *   GNU General Public License for more details.                          *
00017  *                                                                         *
00018  *   You should have received a copy of the GNU General Public License     *
00019  *   along with this library; if not, write to the Free Software           *
00020  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston,                 *
00021  *   MA  02111-1307  USA                                                   *
00022  ***************************************************************************/
00023 
00024 #include "DLS.h"
00025 
00026 #include <algorithm>
00027 #include <time.h>
00028 
00029 #ifdef __APPLE__
00030 #include <CoreFoundation/CFUUID.h>
00031 #elif defined(HAVE_UUID_UUID_H)
00032 #include <uuid/uuid.h>
00033 #endif
00034 
00035 #include "helper.h"
00036 
00037 // macros to decode connection transforms
00038 #define CONN_TRANSFORM_SRC(x)                   ((x >> 10) & 0x000F)
00039 #define CONN_TRANSFORM_CTL(x)                   ((x >> 4) & 0x000F)
00040 #define CONN_TRANSFORM_DST(x)                   (x & 0x000F)
00041 #define CONN_TRANSFORM_BIPOLAR_SRC(x)   (x & 0x4000)
00042 #define CONN_TRANSFORM_BIPOLAR_CTL(x)   (x & 0x0100)
00043 #define CONN_TRANSFORM_INVERT_SRC(x)    (x & 0x8000)
00044 #define CONN_TRANSFORM_INVERT_CTL(x)    (x & 0x0200)
00045 
00046 // macros to encode connection transforms
00047 #define CONN_TRANSFORM_SRC_ENCODE(x)                    ((x & 0x000F) << 10)
00048 #define CONN_TRANSFORM_CTL_ENCODE(x)                    ((x & 0x000F) << 4)
00049 #define CONN_TRANSFORM_DST_ENCODE(x)                    (x & 0x000F)
00050 #define CONN_TRANSFORM_BIPOLAR_SRC_ENCODE(x)    ((x) ? 0x4000 : 0)
00051 #define CONN_TRANSFORM_BIPOLAR_CTL_ENCODE(x)    ((x) ? 0x0100 : 0)
00052 #define CONN_TRANSFORM_INVERT_SRC_ENCODE(x)             ((x) ? 0x8000 : 0)
00053 #define CONN_TRANSFORM_INVERT_CTL_ENCODE(x)             ((x) ? 0x0200 : 0)
00054 
00055 #define DRUM_TYPE_MASK                  0x80000000
00056 
00057 #define F_RGN_OPTION_SELFNONEXCLUSIVE   0x0001
00058 
00059 #define F_WAVELINK_PHASE_MASTER         0x0001
00060 #define F_WAVELINK_MULTICHANNEL         0x0002
00061 
00062 #define F_WSMP_NO_TRUNCATION            0x0001
00063 #define F_WSMP_NO_COMPRESSION           0x0002
00064 
00065 #define MIDI_BANK_COARSE(x)             ((x & 0x00007F00) >> 8)                 // CC0
00066 #define MIDI_BANK_FINE(x)               (x & 0x0000007F)                        // CC32
00067 #define MIDI_BANK_MERGE(coarse, fine)   ((((uint16_t) coarse) << 7) | fine)     // CC0 + CC32
00068 #define MIDI_BANK_ENCODE(coarse, fine)  (((coarse & 0x0000007F) << 8) | (fine & 0x0000007F))
00069 
00070 namespace DLS {
00071 
00072 // *************** Connection  ***************
00073 // *
00074 
00075     void Connection::Init(conn_block_t* Header) {
00076         Source               = (conn_src_t) Header->source;
00077         Control              = (conn_src_t) Header->control;
00078         Destination          = (conn_dst_t) Header->destination;
00079         Scale                = Header->scale;
00080         SourceTransform      = (conn_trn_t) CONN_TRANSFORM_SRC(Header->transform);
00081         ControlTransform     = (conn_trn_t) CONN_TRANSFORM_CTL(Header->transform);
00082         DestinationTransform = (conn_trn_t) CONN_TRANSFORM_DST(Header->transform);
00083         SourceInvert         = CONN_TRANSFORM_INVERT_SRC(Header->transform);
00084         SourceBipolar        = CONN_TRANSFORM_BIPOLAR_SRC(Header->transform);
00085         ControlInvert        = CONN_TRANSFORM_INVERT_CTL(Header->transform);
00086         ControlBipolar       = CONN_TRANSFORM_BIPOLAR_CTL(Header->transform);
00087     }
00088 
00089     Connection::conn_block_t Connection::ToConnBlock() {
00090         conn_block_t c;
00091         c.source = Source;
00092         c.control = Control;
00093         c.destination = Destination;
00094         c.scale = Scale;
00095         c.transform = CONN_TRANSFORM_SRC_ENCODE(SourceTransform) |
00096                       CONN_TRANSFORM_CTL_ENCODE(ControlTransform) |
00097                       CONN_TRANSFORM_DST_ENCODE(DestinationTransform) |
00098                       CONN_TRANSFORM_INVERT_SRC_ENCODE(SourceInvert) |
00099                       CONN_TRANSFORM_BIPOLAR_SRC_ENCODE(SourceBipolar) |
00100                       CONN_TRANSFORM_INVERT_CTL_ENCODE(ControlInvert) |
00101                       CONN_TRANSFORM_BIPOLAR_CTL_ENCODE(ControlBipolar);
00102         return c;
00103     }
00104 
00105 
00106 
00107 // *************** Articulation  ***************
00108 // *
00109 
00118     Articulation::Articulation(RIFF::Chunk* artl) {
00119         pArticulationCk = artl;
00120         if (artl->GetChunkID() != CHUNK_ID_ART2 &&
00121             artl->GetChunkID() != CHUNK_ID_ARTL) {
00122               throw DLS::Exception("<artl-ck> or <art2-ck> chunk expected");
00123         }
00124         HeaderSize  = artl->ReadUint32();
00125         Connections = artl->ReadUint32();
00126         artl->SetPos(HeaderSize);
00127 
00128         pConnections = new Connection[Connections];
00129         Connection::conn_block_t connblock;
00130         for (uint32_t i = 0; i < Connections; i++) {
00131             artl->Read(&connblock.source, 1, 2);
00132             artl->Read(&connblock.control, 1, 2);
00133             artl->Read(&connblock.destination, 1, 2);
00134             artl->Read(&connblock.transform, 1, 2);
00135             artl->Read(&connblock.scale, 1, 4);
00136             pConnections[i].Init(&connblock);
00137         }
00138     }
00139 
00140     Articulation::~Articulation() {
00141        if (pConnections) delete[] pConnections;
00142     }
00143 
00148     void Articulation::UpdateChunks() {
00149         const int iEntrySize = 12; // 12 bytes per connection block
00150         pArticulationCk->Resize(HeaderSize + Connections * iEntrySize);
00151         uint8_t* pData = (uint8_t*) pArticulationCk->LoadChunkData();
00152         store16(&pData[0], HeaderSize);
00153         store16(&pData[2], Connections);
00154         for (uint32_t i = 0; i < Connections; i++) {
00155             Connection::conn_block_t c = pConnections[i].ToConnBlock();
00156             store16(&pData[HeaderSize + i * iEntrySize],     c.source);
00157             store16(&pData[HeaderSize + i * iEntrySize + 2], c.control);
00158             store16(&pData[HeaderSize + i * iEntrySize + 4], c.destination);
00159             store16(&pData[HeaderSize + i * iEntrySize + 6], c.transform);
00160             store32(&pData[HeaderSize + i * iEntrySize + 8], c.scale);
00161         }
00162     }
00163 
00164 
00165 
00166 // *************** Articulator  ***************
00167 // *
00168 
00169     Articulator::Articulator(RIFF::List* ParentList) {
00170         pParentList    = ParentList;
00171         pArticulations = NULL;
00172     }
00173 
00174     Articulation* Articulator::GetFirstArticulation() {
00175         if (!pArticulations) LoadArticulations();
00176         if (!pArticulations) return NULL;
00177         ArticulationsIterator = pArticulations->begin();
00178         return (ArticulationsIterator != pArticulations->end()) ? *ArticulationsIterator : NULL;
00179     }
00180 
00181     Articulation* Articulator::GetNextArticulation() {
00182         if (!pArticulations) return NULL;
00183         ArticulationsIterator++;
00184         return (ArticulationsIterator != pArticulations->end()) ? *ArticulationsIterator : NULL;
00185     }
00186 
00187     void Articulator::LoadArticulations() {
00188         // prefer articulation level 2
00189         RIFF::List* lart = pParentList->GetSubList(LIST_TYPE_LAR2);
00190         if (!lart)  lart = pParentList->GetSubList(LIST_TYPE_LART);
00191         if (lart) {
00192             uint32_t artCkType = (lart->GetListType() == LIST_TYPE_LAR2) ? CHUNK_ID_ART2
00193                                                                          : CHUNK_ID_ARTL;
00194             RIFF::Chunk* art = lart->GetFirstSubChunk();
00195             while (art) {
00196                 if (art->GetChunkID() == artCkType) {
00197                     if (!pArticulations) pArticulations = new ArticulationList;
00198                     pArticulations->push_back(new Articulation(art));
00199                 }
00200                 art = lart->GetNextSubChunk();
00201             }
00202         }
00203     }
00204 
00205     Articulator::~Articulator() {
00206         if (pArticulations) {
00207             ArticulationList::iterator iter = pArticulations->begin();
00208             ArticulationList::iterator end  = pArticulations->end();
00209             while (iter != end) {
00210                 delete *iter;
00211                 iter++;
00212             }
00213             delete pArticulations;
00214         }
00215     }
00216 
00221     void Articulator::UpdateChunks() {
00222         if (pArticulations) {
00223             ArticulationList::iterator iter = pArticulations->begin();
00224             ArticulationList::iterator end  = pArticulations->end();
00225             for (; iter != end; ++iter) {
00226                 (*iter)->UpdateChunks();
00227             }
00228         }
00229     }
00230 
00231 
00232 
00233 // *************** Info  ***************
00234 // *
00235 
00242     Info::Info(RIFF::List* list) {
00243         pFixedStringLengths = NULL;
00244         pResourceListChunk = list;
00245         if (list) {
00246             RIFF::List* lstINFO = list->GetSubList(LIST_TYPE_INFO);
00247             if (lstINFO) {
00248                 LoadString(CHUNK_ID_INAM, lstINFO, Name);
00249                 LoadString(CHUNK_ID_IARL, lstINFO, ArchivalLocation);
00250                 LoadString(CHUNK_ID_ICRD, lstINFO, CreationDate);
00251                 LoadString(CHUNK_ID_ICMT, lstINFO, Comments);
00252                 LoadString(CHUNK_ID_IPRD, lstINFO, Product);
00253                 LoadString(CHUNK_ID_ICOP, lstINFO, Copyright);
00254                 LoadString(CHUNK_ID_IART, lstINFO, Artists);
00255                 LoadString(CHUNK_ID_IGNR, lstINFO, Genre);
00256                 LoadString(CHUNK_ID_IKEY, lstINFO, Keywords);
00257                 LoadString(CHUNK_ID_IENG, lstINFO, Engineer);
00258                 LoadString(CHUNK_ID_ITCH, lstINFO, Technician);
00259                 LoadString(CHUNK_ID_ISFT, lstINFO, Software);
00260                 LoadString(CHUNK_ID_IMED, lstINFO, Medium);
00261                 LoadString(CHUNK_ID_ISRC, lstINFO, Source);
00262                 LoadString(CHUNK_ID_ISRF, lstINFO, SourceForm);
00263                 LoadString(CHUNK_ID_ICMS, lstINFO, Commissioned);
00264                 LoadString(CHUNK_ID_ISBJ, lstINFO, Subject);
00265             }
00266         }
00267     }
00268 
00269     Info::~Info() {
00270     }
00271 
00283     void Info::SetFixedStringLengths(const string_length_t* lengths) {
00284         pFixedStringLengths = lengths;
00285     }
00286 
00292     void Info::LoadString(uint32_t ChunkID, RIFF::List* lstINFO, String& s) {
00293         RIFF::Chunk* ck = lstINFO->GetSubChunk(ChunkID);
00294         ::LoadString(ck, s); // function from helper.h
00295     }
00296 
00312     void Info::SaveString(uint32_t ChunkID, RIFF::List* lstINFO, const String& s, const String& sDefault) {
00313         int size = 0;
00314         if (pFixedStringLengths) {
00315             for (int i = 0 ; pFixedStringLengths[i].length ; i++) {
00316                 if (pFixedStringLengths[i].chunkId == ChunkID) {
00317                     size = pFixedStringLengths[i].length;
00318                     break;
00319                 }
00320             }
00321         }
00322         RIFF::Chunk* ck = lstINFO->GetSubChunk(ChunkID);
00323         ::SaveString(ChunkID, ck, lstINFO, s, sDefault, size != 0, size); // function from helper.h
00324     }
00325 
00331     void Info::UpdateChunks() {
00332         if (!pResourceListChunk) return;
00333 
00334         // make sure INFO list chunk exists
00335         RIFF::List* lstINFO   = pResourceListChunk->GetSubList(LIST_TYPE_INFO);
00336 
00337         String defaultName = "";
00338         String defaultCreationDate = "";
00339         String defaultSoftware = "";
00340         String defaultComments = "";
00341 
00342         uint32_t resourceType = pResourceListChunk->GetListType();
00343 
00344         if (!lstINFO) {
00345             lstINFO = pResourceListChunk->AddSubList(LIST_TYPE_INFO);
00346 
00347             // assemble default values
00348             defaultName = "NONAME";
00349 
00350             if (resourceType == RIFF_TYPE_DLS) {
00351                 // get current date
00352                 time_t now = time(NULL);
00353                 tm* pNowBroken = localtime(&now);
00354                 char buf[11];
00355                 strftime(buf, 11, "%F", pNowBroken);
00356                 defaultCreationDate = buf;
00357 
00358                 defaultComments = "Created with " + libraryName() + " " + libraryVersion();
00359             }
00360             if (resourceType == RIFF_TYPE_DLS || resourceType == LIST_TYPE_INS)
00361             {
00362                 defaultSoftware = libraryName() + " " + libraryVersion();
00363             }
00364         }
00365 
00366         // save values
00367 
00368         SaveString(CHUNK_ID_IARL, lstINFO, ArchivalLocation, String(""));
00369         SaveString(CHUNK_ID_IART, lstINFO, Artists, String(""));
00370         SaveString(CHUNK_ID_ICMS, lstINFO, Commissioned, String(""));
00371         SaveString(CHUNK_ID_ICMT, lstINFO, Comments, defaultComments);
00372         SaveString(CHUNK_ID_ICOP, lstINFO, Copyright, String(""));
00373         SaveString(CHUNK_ID_ICRD, lstINFO, CreationDate, defaultCreationDate);
00374         SaveString(CHUNK_ID_IENG, lstINFO, Engineer, String(""));
00375         SaveString(CHUNK_ID_IGNR, lstINFO, Genre, String(""));
00376         SaveString(CHUNK_ID_IKEY, lstINFO, Keywords, String(""));
00377         SaveString(CHUNK_ID_IMED, lstINFO, Medium, String(""));
00378         SaveString(CHUNK_ID_INAM, lstINFO, Name, defaultName);
00379         SaveString(CHUNK_ID_IPRD, lstINFO, Product, String(""));
00380         SaveString(CHUNK_ID_ISBJ, lstINFO, Subject, String(""));
00381         SaveString(CHUNK_ID_ISFT, lstINFO, Software, defaultSoftware);
00382         SaveString(CHUNK_ID_ISRC, lstINFO, Source, String(""));
00383         SaveString(CHUNK_ID_ISRF, lstINFO, SourceForm, String(""));
00384         SaveString(CHUNK_ID_ITCH, lstINFO, Technician, String(""));
00385     }
00386 
00387 
00388 
00389 // *************** Resource ***************
00390 // *
00391 
00401     Resource::Resource(Resource* Parent, RIFF::List* lstResource) {
00402         pParent = Parent;
00403         pResourceList = lstResource;
00404 
00405         pInfo = new Info(lstResource);
00406 
00407         RIFF::Chunk* ckDLSID = lstResource->GetSubChunk(CHUNK_ID_DLID);
00408         if (ckDLSID) {
00409             pDLSID = new dlsid_t;
00410             ckDLSID->Read(&pDLSID->ulData1, 1, 4);
00411             ckDLSID->Read(&pDLSID->usData2, 1, 2);
00412             ckDLSID->Read(&pDLSID->usData3, 1, 2);
00413             ckDLSID->Read(pDLSID->abData, 8, 1);
00414         }
00415         else pDLSID = NULL;
00416     }
00417 
00418     Resource::~Resource() {
00419         if (pDLSID) delete pDLSID;
00420         if (pInfo)  delete pInfo;
00421     }
00422 
00431     void Resource::UpdateChunks() {
00432         pInfo->UpdateChunks();
00433 
00434         if (pDLSID) {
00435             // make sure 'dlid' chunk exists
00436             RIFF::Chunk* ckDLSID = pResourceList->GetSubChunk(CHUNK_ID_DLID);
00437             if (!ckDLSID) ckDLSID = pResourceList->AddSubChunk(CHUNK_ID_DLID, 16);
00438             uint8_t* pData = (uint8_t*)ckDLSID->LoadChunkData();
00439             // update 'dlid' chunk
00440             store32(&pData[0], pDLSID->ulData1);
00441             store16(&pData[4], pDLSID->usData2);
00442             store16(&pData[6], pDLSID->usData3);
00443             memcpy(&pData[8], pDLSID->abData, 8);
00444         }
00445     }
00446 
00450     void Resource::GenerateDLSID() {
00451 #if defined(WIN32) || defined(__APPLE__) || defined(HAVE_UUID_GENERATE)
00452 
00453         if (!pDLSID) pDLSID = new dlsid_t;
00454 
00455 #ifdef WIN32
00456 
00457         UUID uuid;
00458         UuidCreate(&uuid);
00459         pDLSID->ulData1 = uuid.Data1;
00460         pDLSID->usData2 = uuid.Data2;
00461         pDLSID->usData3 = uuid.Data3;
00462         memcpy(pDLSID->abData, uuid.Data4, 8);
00463 
00464 #elif defined(__APPLE__)
00465 
00466         CFUUIDRef uuidRef = CFUUIDCreate(NULL);
00467         CFUUIDBytes uuid = CFUUIDGetUUIDBytes(uuidRef);
00468         CFRelease(uuidRef);
00469         pDLSID->ulData1 = uuid.byte0 | uuid.byte1 << 8 | uuid.byte2 << 16 | uuid.byte3 << 24;
00470         pDLSID->usData2 = uuid.byte4 | uuid.byte5 << 8;
00471         pDLSID->usData3 = uuid.byte6 | uuid.byte7 << 8;
00472         pDLSID->abData[0] = uuid.byte8;
00473         pDLSID->abData[1] = uuid.byte9;
00474         pDLSID->abData[2] = uuid.byte10;
00475         pDLSID->abData[3] = uuid.byte11;
00476         pDLSID->abData[4] = uuid.byte12;
00477         pDLSID->abData[5] = uuid.byte13;
00478         pDLSID->abData[6] = uuid.byte14;
00479         pDLSID->abData[7] = uuid.byte15;
00480 #else
00481         uuid_t uuid;
00482         uuid_generate(uuid);
00483         pDLSID->ulData1 = uuid[0] | uuid[1] << 8 | uuid[2] << 16 | uuid[3] << 24;
00484         pDLSID->usData2 = uuid[4] | uuid[5] << 8;
00485         pDLSID->usData3 = uuid[6] | uuid[7] << 8;
00486         memcpy(pDLSID->abData, &uuid[8], 8);
00487 #endif
00488 #endif
00489     }
00490 
00491 
00492 // *************** Sampler ***************
00493 // *
00494 
00495     Sampler::Sampler(RIFF::List* ParentList) {
00496         pParentList       = ParentList;
00497         RIFF::Chunk* wsmp = ParentList->GetSubChunk(CHUNK_ID_WSMP);
00498         if (wsmp) {
00499             uiHeaderSize   = wsmp->ReadUint32();
00500             UnityNote      = wsmp->ReadUint16();
00501             FineTune       = wsmp->ReadInt16();
00502             Gain           = wsmp->ReadInt32();
00503             SamplerOptions = wsmp->ReadUint32();
00504             SampleLoops    = wsmp->ReadUint32();
00505         } else { // 'wsmp' chunk missing
00506             uiHeaderSize   = 20;
00507             UnityNote      = 60;
00508             FineTune       = 0; // +- 0 cents
00509             Gain           = 0; // 0 dB
00510             SamplerOptions = F_WSMP_NO_COMPRESSION;
00511             SampleLoops    = 0;
00512         }
00513         NoSampleDepthTruncation = SamplerOptions & F_WSMP_NO_TRUNCATION;
00514         NoSampleCompression     = SamplerOptions & F_WSMP_NO_COMPRESSION;
00515         pSampleLoops            = (SampleLoops) ? new sample_loop_t[SampleLoops] : NULL;
00516         if (SampleLoops) {
00517             wsmp->SetPos(uiHeaderSize);
00518             for (uint32_t i = 0; i < SampleLoops; i++) {
00519                 wsmp->Read(pSampleLoops + i, 4, 4);
00520                 if (pSampleLoops[i].Size > sizeof(sample_loop_t)) { // if loop struct was extended
00521                     wsmp->SetPos(pSampleLoops[i].Size - sizeof(sample_loop_t), RIFF::stream_curpos);
00522                 }
00523             }
00524         }
00525     }
00526 
00527     Sampler::~Sampler() {
00528         if (pSampleLoops) delete[] pSampleLoops;
00529     }
00530 
00531     void Sampler::SetGain(int32_t gain) {
00532         Gain = gain;
00533     }
00534 
00539     void Sampler::UpdateChunks() {
00540         // make sure 'wsmp' chunk exists
00541         RIFF::Chunk* wsmp = pParentList->GetSubChunk(CHUNK_ID_WSMP);
00542         int wsmpSize = uiHeaderSize + SampleLoops * 16;
00543         if (!wsmp) {
00544             wsmp = pParentList->AddSubChunk(CHUNK_ID_WSMP, wsmpSize);
00545         } else if (wsmp->GetSize() != wsmpSize) {
00546             wsmp->Resize(wsmpSize);
00547         }
00548         uint8_t* pData = (uint8_t*) wsmp->LoadChunkData();
00549         // update headers size
00550         store32(&pData[0], uiHeaderSize);
00551         // update respective sampler options bits
00552         SamplerOptions = (NoSampleDepthTruncation) ? SamplerOptions | F_WSMP_NO_TRUNCATION
00553                                                    : SamplerOptions & (~F_WSMP_NO_TRUNCATION);
00554         SamplerOptions = (NoSampleCompression) ? SamplerOptions | F_WSMP_NO_COMPRESSION
00555                                                : SamplerOptions & (~F_WSMP_NO_COMPRESSION);
00556         store16(&pData[4], UnityNote);
00557         store16(&pData[6], FineTune);
00558         store32(&pData[8], Gain);
00559         store32(&pData[12], SamplerOptions);
00560         store32(&pData[16], SampleLoops);
00561         // update loop definitions
00562         for (uint32_t i = 0; i < SampleLoops; i++) {
00563             //FIXME: this does not handle extended loop structs correctly
00564             store32(&pData[uiHeaderSize + i * 16], pSampleLoops[i].Size);
00565             store32(&pData[uiHeaderSize + i * 16 + 4], pSampleLoops[i].LoopType);
00566             store32(&pData[uiHeaderSize + i * 16 + 8], pSampleLoops[i].LoopStart);
00567             store32(&pData[uiHeaderSize + i * 16 + 12], pSampleLoops[i].LoopLength);
00568         }
00569     }
00570 
00576     void Sampler::AddSampleLoop(sample_loop_t* pLoopDef) {
00577         sample_loop_t* pNewLoops = new sample_loop_t[SampleLoops + 1];
00578         // copy old loops array
00579         for (int i = 0; i < SampleLoops; i++) {
00580             pNewLoops[i] = pSampleLoops[i];
00581         }
00582         // add the new loop
00583         pNewLoops[SampleLoops] = *pLoopDef;
00584         // auto correct size field
00585         pNewLoops[SampleLoops].Size = sizeof(DLS::sample_loop_t);
00586         // free the old array and update the member variables
00587         if (SampleLoops) delete[] pSampleLoops;
00588         pSampleLoops = pNewLoops;
00589         SampleLoops++;
00590     }
00591 
00598     void Sampler::DeleteSampleLoop(sample_loop_t* pLoopDef) {
00599         sample_loop_t* pNewLoops = new sample_loop_t[SampleLoops - 1];
00600         // copy old loops array (skipping given loop)
00601         for (int i = 0, o = 0; i < SampleLoops; i++) {
00602             if (&pSampleLoops[i] == pLoopDef) continue;
00603             if (o == SampleLoops - 1)
00604                 throw Exception("Could not delete Sample Loop, because it does not exist");
00605             pNewLoops[o] = pSampleLoops[i];
00606             o++;
00607         }
00608         // free the old array and update the member variables
00609         if (SampleLoops) delete[] pSampleLoops;
00610         pSampleLoops = pNewLoops;
00611         SampleLoops--;
00612     }
00613 
00614 
00615 
00616 // *************** Sample ***************
00617 // *
00618 
00634     Sample::Sample(File* pFile, RIFF::List* waveList, unsigned long WavePoolOffset) : Resource(pFile, waveList) {
00635         pWaveList = waveList;
00636         ulWavePoolOffset = WavePoolOffset - LIST_HEADER_SIZE;
00637         pCkFormat = waveList->GetSubChunk(CHUNK_ID_FMT);
00638         pCkData   = waveList->GetSubChunk(CHUNK_ID_DATA);
00639         if (pCkFormat) {
00640             // common fields
00641             FormatTag              = pCkFormat->ReadUint16();
00642             Channels               = pCkFormat->ReadUint16();
00643             SamplesPerSecond       = pCkFormat->ReadUint32();
00644             AverageBytesPerSecond  = pCkFormat->ReadUint32();
00645             BlockAlign             = pCkFormat->ReadUint16();
00646             // PCM format specific
00647             if (FormatTag == DLS_WAVE_FORMAT_PCM) {
00648                 BitDepth     = pCkFormat->ReadUint16();
00649                 FrameSize    = (BitDepth / 8) * Channels;
00650             } else { // unsupported sample data format
00651                 BitDepth     = 0;
00652                 FrameSize    = 0;
00653             }
00654         } else { // 'fmt' chunk missing
00655             FormatTag              = DLS_WAVE_FORMAT_PCM;
00656             BitDepth               = 16;
00657             Channels               = 1;
00658             SamplesPerSecond       = 44100;
00659             AverageBytesPerSecond  = (BitDepth / 8) * SamplesPerSecond * Channels;
00660             FrameSize              = (BitDepth / 8) * Channels;
00661             BlockAlign             = FrameSize;
00662         }
00663         SamplesTotal = (pCkData) ? (FormatTag == DLS_WAVE_FORMAT_PCM) ? pCkData->GetSize() / FrameSize
00664                                                                       : 0
00665                                  : 0;
00666     }
00667 
00673     Sample::~Sample() {
00674         RIFF::List* pParent = pWaveList->GetParent();
00675         pParent->DeleteSubChunk(pWaveList);
00676     }
00677 
00704     void* Sample::LoadSampleData() {
00705         return (pCkData) ? pCkData->LoadChunkData() : NULL;
00706     }
00707 
00713     void Sample::ReleaseSampleData() {
00714         if (pCkData) pCkData->ReleaseChunkData();
00715     }
00716 
00727     unsigned long Sample::GetSize() {
00728         if (FormatTag != DLS_WAVE_FORMAT_PCM) return 0;
00729         return (pCkData) ? pCkData->GetSize() / FrameSize : 0;
00730     }
00731 
00760     void Sample::Resize(int iNewSize) {
00761         if (FormatTag != DLS_WAVE_FORMAT_PCM) throw Exception("Sample's format is not DLS_WAVE_FORMAT_PCM");
00762         if (iNewSize < 1) throw Exception("Sample size must be at least one sample point");
00763         const int iSizeInBytes = iNewSize * FrameSize;
00764         pCkData = pWaveList->GetSubChunk(CHUNK_ID_DATA);
00765         if (pCkData) pCkData->Resize(iSizeInBytes);
00766         else pCkData = pWaveList->AddSubChunk(CHUNK_ID_DATA, iSizeInBytes);
00767     }
00768 
00785     unsigned long Sample::SetPos(unsigned long SampleCount, RIFF::stream_whence_t Whence) {
00786         if (FormatTag != DLS_WAVE_FORMAT_PCM) return 0; // failed: wave data not PCM format
00787         if (!pCkData) throw Exception("No data chunk created for sample yet, call Sample::Resize() to create one");
00788         unsigned long orderedBytes = SampleCount * FrameSize;
00789         unsigned long result = pCkData->SetPos(orderedBytes, Whence);
00790         return (result == orderedBytes) ? SampleCount
00791                                         : result / FrameSize;
00792     }
00793 
00803     unsigned long Sample::Read(void* pBuffer, unsigned long SampleCount) {
00804         if (FormatTag != DLS_WAVE_FORMAT_PCM) return 0; // failed: wave data not PCM format
00805         return pCkData->Read(pBuffer, SampleCount, FrameSize); // FIXME: channel inversion due to endian correction?
00806     }
00807 
00823     unsigned long Sample::Write(void* pBuffer, unsigned long SampleCount) {
00824         if (FormatTag != DLS_WAVE_FORMAT_PCM) return 0; // failed: wave data not PCM format
00825         if (GetSize() < SampleCount) throw Exception("Could not write sample data, current sample size to small");
00826         return pCkData->Write(pBuffer, SampleCount, FrameSize); // FIXME: channel inversion due to endian correction?
00827     }
00828 
00836     void Sample::UpdateChunks() {
00837         if (FormatTag != DLS_WAVE_FORMAT_PCM)
00838             throw Exception("Could not save sample, only PCM format is supported");
00839         // we refuse to do anything if not sample wave form was provided yet
00840         if (!pCkData)
00841             throw Exception("Could not save sample, there is no sample data to save");
00842         // update chunks of base class as well
00843