/[svn]/libgig/trunk/src/DLS.cpp
ViewVC logotype

Annotation of /libgig/trunk/src/DLS.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1179 - (hide annotations) (download)
Sat May 12 11:25:04 2007 UTC (16 years, 11 months ago) by persson
File size: 63712 byte(s)
* fixed write support for big-endian systems

1 schoenebeck 2 /***************************************************************************
2     * *
3 schoenebeck 933 * libgig - C++ cross-platform Gigasampler format file access library *
4 schoenebeck 2 * *
5 schoenebeck 1050 * Copyright (C) 2003-2007 by Christian Schoenebeck *
6 schoenebeck 384 * <cuse@users.sourceforge.net> *
7 schoenebeck 2 * *
8     * This library is free software; you can redistribute it and/or modify *
9     * it under the terms of the GNU General Public License as published by *
10     * the Free Software Foundation; either version 2 of the License, or *
11     * (at your option) any later version. *
12     * *
13     * This library is distributed in the hope that it will be useful, *
14     * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16     * GNU General Public License for more details. *
17     * *
18     * You should have received a copy of the GNU General Public License *
19     * along with this library; if not, write to the Free Software *
20     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
21     * MA 02111-1307 USA *
22     ***************************************************************************/
23    
24     #include "DLS.h"
25    
26 schoenebeck 800 #include <time.h>
27    
28     #include "helper.h"
29    
30     // macros to decode connection transforms
31     #define CONN_TRANSFORM_SRC(x) ((x >> 10) & 0x000F)
32     #define CONN_TRANSFORM_CTL(x) ((x >> 4) & 0x000F)
33     #define CONN_TRANSFORM_DST(x) (x & 0x000F)
34     #define CONN_TRANSFORM_BIPOLAR_SRC(x) (x & 0x4000)
35     #define CONN_TRANSFORM_BIPOLAR_CTL(x) (x & 0x0100)
36     #define CONN_TRANSFORM_INVERT_SRC(x) (x & 0x8000)
37     #define CONN_TRANSFORM_INVERT_CTL(x) (x & 0x0200)
38    
39     // macros to encode connection transforms
40     #define CONN_TRANSFORM_SRC_ENCODE(x) ((x & 0x000F) << 10)
41     #define CONN_TRANSFORM_CTL_ENCODE(x) ((x & 0x000F) << 4)
42     #define CONN_TRANSFORM_DST_ENCODE(x) (x & 0x000F)
43     #define CONN_TRANSFORM_BIPOLAR_SRC_ENCODE(x) ((x) ? 0x4000 : 0)
44     #define CONN_TRANSFORM_BIPOLAR_CTL_ENCODE(x) ((x) ? 0x0100 : 0)
45     #define CONN_TRANSFORM_INVERT_SRC_ENCODE(x) ((x) ? 0x8000 : 0)
46     #define CONN_TRANSFORM_INVERT_CTL_ENCODE(x) ((x) ? 0x0200 : 0)
47    
48 persson 918 #define DRUM_TYPE_MASK 0x80000000
49 schoenebeck 800
50     #define F_RGN_OPTION_SELFNONEXCLUSIVE 0x0001
51    
52     #define F_WAVELINK_PHASE_MASTER 0x0001
53     #define F_WAVELINK_MULTICHANNEL 0x0002
54    
55     #define F_WSMP_NO_TRUNCATION 0x0001
56     #define F_WSMP_NO_COMPRESSION 0x0002
57    
58     #define MIDI_BANK_COARSE(x) ((x & 0x00007F00) >> 8) // CC0
59     #define MIDI_BANK_FINE(x) (x & 0x0000007F) // CC32
60     #define MIDI_BANK_MERGE(coarse, fine) ((((uint16_t) coarse) << 7) | fine) // CC0 + CC32
61     #define MIDI_BANK_ENCODE(coarse, fine) (((coarse & 0x0000007F) << 8) | (fine & 0x0000007F))
62    
63 schoenebeck 2 namespace DLS {
64    
65     // *************** Connection ***************
66     // *
67    
68     void Connection::Init(conn_block_t* Header) {
69     Source = (conn_src_t) Header->source;
70     Control = (conn_src_t) Header->control;
71     Destination = (conn_dst_t) Header->destination;
72     Scale = Header->scale;
73     SourceTransform = (conn_trn_t) CONN_TRANSFORM_SRC(Header->transform);
74     ControlTransform = (conn_trn_t) CONN_TRANSFORM_CTL(Header->transform);
75     DestinationTransform = (conn_trn_t) CONN_TRANSFORM_DST(Header->transform);
76     SourceInvert = CONN_TRANSFORM_INVERT_SRC(Header->transform);
77     SourceBipolar = CONN_TRANSFORM_BIPOLAR_SRC(Header->transform);
78     ControlInvert = CONN_TRANSFORM_INVERT_CTL(Header->transform);
79     ControlBipolar = CONN_TRANSFORM_BIPOLAR_CTL(Header->transform);
80     }
81    
82 schoenebeck 800 Connection::conn_block_t Connection::ToConnBlock() {
83     conn_block_t c;
84     c.source = Source;
85     c.control = Control;
86     c.destination = Destination;
87     c.scale = Scale;
88     c.transform = CONN_TRANSFORM_SRC_ENCODE(SourceTransform) |
89     CONN_TRANSFORM_CTL_ENCODE(ControlTransform) |
90     CONN_TRANSFORM_DST_ENCODE(DestinationTransform) |
91     CONN_TRANSFORM_INVERT_SRC_ENCODE(SourceInvert) |
92     CONN_TRANSFORM_BIPOLAR_SRC_ENCODE(SourceBipolar) |
93     CONN_TRANSFORM_INVERT_CTL_ENCODE(ControlInvert) |
94     CONN_TRANSFORM_BIPOLAR_CTL_ENCODE(ControlBipolar);
95     return c;
96     }
97 schoenebeck 2
98    
99 schoenebeck 800
100 schoenebeck 2 // *************** Articulation ***************
101     // *
102    
103 schoenebeck 800 /** @brief Constructor.
104     *
105     * Expects an 'artl' or 'art2' chunk to be given where the articulation
106     * connections will be read from.
107     *
108     * @param artl - pointer to an 'artl' or 'art2' chunk
109     * @throws Exception if no 'artl' or 'art2' chunk was given
110     */
111     Articulation::Articulation(RIFF::Chunk* artl) {
112     pArticulationCk = artl;
113     if (artl->GetChunkID() != CHUNK_ID_ART2 &&
114     artl->GetChunkID() != CHUNK_ID_ARTL) {
115     throw DLS::Exception("<artl-ck> or <art2-ck> chunk expected");
116 schoenebeck 2 }
117 schoenebeck 800 HeaderSize = artl->ReadUint32();
118     Connections = artl->ReadUint32();
119     artl->SetPos(HeaderSize);
120 schoenebeck 2
121     pConnections = new Connection[Connections];
122     Connection::conn_block_t connblock;
123 schoenebeck 800 for (uint32_t i = 0; i < Connections; i++) {
124     artl->Read(&connblock.source, 1, 2);
125     artl->Read(&connblock.control, 1, 2);
126     artl->Read(&connblock.destination, 1, 2);
127     artl->Read(&connblock.transform, 1, 2);
128     artl->Read(&connblock.scale, 1, 4);
129 schoenebeck 2 pConnections[i].Init(&connblock);
130     }
131     }
132    
133     Articulation::~Articulation() {
134     if (pConnections) delete[] pConnections;
135     }
136    
137 schoenebeck 800 /**
138     * Apply articulation connections to the respective RIFF chunks. You
139     * have to call File::Save() to make changes persistent.
140     */
141     void Articulation::UpdateChunks() {
142     const int iEntrySize = 12; // 12 bytes per connection block
143     pArticulationCk->Resize(HeaderSize + Connections * iEntrySize);
144     uint8_t* pData = (uint8_t*) pArticulationCk->LoadChunkData();
145 persson 1179 store16(&pData[0], HeaderSize);
146     store16(&pData[2], Connections);
147 schoenebeck 800 for (uint32_t i = 0; i < Connections; i++) {
148     Connection::conn_block_t c = pConnections[i].ToConnBlock();
149 persson 1179 store16(&pData[HeaderSize + i * iEntrySize], c.source);
150     store16(&pData[HeaderSize + i * iEntrySize + 2], c.control);
151     store16(&pData[HeaderSize + i * iEntrySize + 4], c.destination);
152     store16(&pData[HeaderSize + i * iEntrySize + 6], c.transform);
153     store32(&pData[HeaderSize + i * iEntrySize + 8], c.scale);
154 schoenebeck 800 }
155     }
156 schoenebeck 2
157    
158 schoenebeck 800
159 schoenebeck 2 // *************** Articulator ***************
160     // *
161    
162     Articulator::Articulator(RIFF::List* ParentList) {
163     pParentList = ParentList;
164     pArticulations = NULL;
165     }
166    
167     Articulation* Articulator::GetFirstArticulation() {
168     if (!pArticulations) LoadArticulations();
169     if (!pArticulations) return NULL;
170     ArticulationsIterator = pArticulations->begin();
171     return (ArticulationsIterator != pArticulations->end()) ? *ArticulationsIterator : NULL;
172     }
173    
174     Articulation* Articulator::GetNextArticulation() {
175     if (!pArticulations) return NULL;
176     ArticulationsIterator++;
177     return (ArticulationsIterator != pArticulations->end()) ? *ArticulationsIterator : NULL;
178     }
179    
180     void Articulator::LoadArticulations() {
181     // prefer articulation level 2
182     RIFF::List* lart = pParentList->GetSubList(LIST_TYPE_LAR2);
183     if (!lart) lart = pParentList->GetSubList(LIST_TYPE_LART);
184     if (lart) {
185 schoenebeck 800 uint32_t artCkType = (lart->GetListType() == LIST_TYPE_LAR2) ? CHUNK_ID_ART2
186     : CHUNK_ID_ARTL;
187     RIFF::Chunk* art = lart->GetFirstSubChunk();
188 schoenebeck 2 while (art) {
189 schoenebeck 800 if (art->GetChunkID() == artCkType) {
190 schoenebeck 2 if (!pArticulations) pArticulations = new ArticulationList;
191     pArticulations->push_back(new Articulation(art));
192     }
193 schoenebeck 800 art = lart->GetNextSubChunk();
194 schoenebeck 2 }
195     }
196     }
197    
198     Articulator::~Articulator() {
199     if (pArticulations) {
200     ArticulationList::iterator iter = pArticulations->begin();
201     ArticulationList::iterator end = pArticulations->end();
202     while (iter != end) {
203     delete *iter;
204     iter++;
205     }
206     delete pArticulations;
207     }
208     }
209    
210 schoenebeck 800 /**
211     * Apply all articulations to the respective RIFF chunks. You have to
212     * call File::Save() to make changes persistent.
213     */
214     void Articulator::UpdateChunks() {
215 schoenebeck 804 if (pArticulations) {
216     ArticulationList::iterator iter = pArticulations->begin();
217     ArticulationList::iterator end = pArticulations->end();
218     for (; iter != end; ++iter) {
219     (*iter)->UpdateChunks();
220     }
221 schoenebeck 800 }
222     }
223 schoenebeck 2
224    
225 schoenebeck 800
226 schoenebeck 2 // *************** Info ***************
227     // *
228    
229 schoenebeck 800 /** @brief Constructor.
230     *
231 schoenebeck 929 * Initializes the info strings with values provided by an INFO list chunk.
232 schoenebeck 800 *
233 schoenebeck 929 * @param list - pointer to a list chunk which contains an INFO list chunk
234 schoenebeck 800 */
235 schoenebeck 2 Info::Info(RIFF::List* list) {
236 persson 918 UseFixedLengthStrings = false;
237 schoenebeck 800 pResourceListChunk = list;
238 schoenebeck 2 if (list) {
239     RIFF::List* lstINFO = list->GetSubList(LIST_TYPE_INFO);
240     if (lstINFO) {
241     LoadString(CHUNK_ID_INAM, lstINFO, Name);
242     LoadString(CHUNK_ID_IARL, lstINFO, ArchivalLocation);
243     LoadString(CHUNK_ID_ICRD, lstINFO, CreationDate);
244     LoadString(CHUNK_ID_ICMT, lstINFO, Comments);
245     LoadString(CHUNK_ID_IPRD, lstINFO, Product);
246     LoadString(CHUNK_ID_ICOP, lstINFO, Copyright);
247     LoadString(CHUNK_ID_IART, lstINFO, Artists);
248     LoadString(CHUNK_ID_IGNR, lstINFO, Genre);
249     LoadString(CHUNK_ID_IKEY, lstINFO, Keywords);
250     LoadString(CHUNK_ID_IENG, lstINFO, Engineer);
251     LoadString(CHUNK_ID_ITCH, lstINFO, Technician);
252     LoadString(CHUNK_ID_ISFT, lstINFO, Software);
253     LoadString(CHUNK_ID_IMED, lstINFO, Medium);
254     LoadString(CHUNK_ID_ISRC, lstINFO, Source);
255     LoadString(CHUNK_ID_ISRF, lstINFO, SourceForm);
256     LoadString(CHUNK_ID_ICMS, lstINFO, Commissioned);
257 persson 928 LoadString(CHUNK_ID_ISBJ, lstINFO, Subject);
258 schoenebeck 2 }
259     }
260     }
261    
262 schoenebeck 823 Info::~Info() {
263     }
264    
265 schoenebeck 800 /** @brief Load given INFO field.
266     *
267     * Load INFO field from INFO chunk with chunk ID \a ChunkID from INFO
268     * list chunk \a lstINFO and save value to \a s.
269     */
270     void Info::LoadString(uint32_t ChunkID, RIFF::List* lstINFO, String& s) {
271     RIFF::Chunk* ck = lstINFO->GetSubChunk(ChunkID);
272 schoenebeck 929 ::LoadString(ck, s); // function from helper.h
273 schoenebeck 800 }
274 schoenebeck 2
275 schoenebeck 800 /** @brief Apply given INFO field to the respective chunk.
276     *
277     * Apply given info value to info chunk with ID \a ChunkID, which is a
278     * subchunk of INFO list chunk \a lstINFO. If the given chunk already
279 schoenebeck 804 * exists, value \a s will be applied. Otherwise if it doesn't exist yet
280     * and either \a s or \a sDefault is not an empty string, such a chunk
281     * will be created and either \a s or \a sDefault will be applied
282     * (depending on which one is not an empty string, if both are not an
283     * empty string \a s will be preferred).
284 schoenebeck 800 *
285     * @param ChunkID - 32 bit RIFF chunk ID of INFO subchunk
286     * @param lstINFO - parent (INFO) RIFF list chunk
287     * @param s - current value of info field
288     * @param sDefault - default value
289 schoenebeck 929 * @param bUseFixedLengthStrings - should a specific string size be forced in the chunk?
290     * @param size - wanted size of the INFO chunk. This is ignored if bUseFixedLengthStrings is false.
291 schoenebeck 800 */
292 schoenebeck 929 void Info::SaveString(uint32_t ChunkID, RIFF::List* lstINFO, const String& s, const String& sDefault, bool bUseFixedLengthStrings, int size) {
293 schoenebeck 800 RIFF::Chunk* ck = lstINFO->GetSubChunk(ChunkID);
294 schoenebeck 929 ::SaveString(ChunkID, ck, lstINFO, s, sDefault, bUseFixedLengthStrings, size); // function from helper.h
295 schoenebeck 800 }
296 schoenebeck 2
297 schoenebeck 800 /** @brief Update chunks with current info values.
298     *
299     * Apply current INFO field values to the respective INFO chunks. You
300     * have to call File::Save() to make changes persistent.
301     */
302     void Info::UpdateChunks() {
303     if (!pResourceListChunk) return;
304    
305     // make sure INFO list chunk exists
306     RIFF::List* lstINFO = pResourceListChunk->GetSubList(LIST_TYPE_INFO);
307    
308 persson 918 String defaultName = "";
309     String defaultCreationDate = "";
310     String defaultSoftware = "";
311     String defaultComments = "";
312 schoenebeck 800
313 persson 918 uint32_t resourceType = pResourceListChunk->GetListType();
314    
315     if (!lstINFO) {
316     lstINFO = pResourceListChunk->AddSubList(LIST_TYPE_INFO);
317    
318     // assemble default values
319     defaultName = "NONAME";
320    
321     if (resourceType == RIFF_TYPE_DLS) {
322     // get current date
323     time_t now = time(NULL);
324     tm* pNowBroken = localtime(&now);
325     char buf[11];
326     strftime(buf, 11, "%F", pNowBroken);
327     defaultCreationDate = buf;
328    
329     defaultComments = "Created with " + libraryName() + " " + libraryVersion();
330     }
331     if (resourceType == RIFF_TYPE_DLS || resourceType == LIST_TYPE_INS)
332     {
333     defaultSoftware = libraryName() + " " + libraryVersion();
334     }
335     }
336    
337 schoenebeck 800 // save values
338 persson 918
339     // (the string size values are for gig files; they are only
340     // used if UseFixedLengthStrings is set to true)
341 schoenebeck 929 SaveString(CHUNK_ID_INAM, lstINFO, Name, defaultName, UseFixedLengthStrings,
342 persson 918 resourceType == RIFF_TYPE_DLS ? 128 : 64);
343 schoenebeck 929 SaveString(CHUNK_ID_IARL, lstINFO, ArchivalLocation, String(""), UseFixedLengthStrings, 256);
344     SaveString(CHUNK_ID_ICRD, lstINFO, CreationDate, defaultCreationDate, UseFixedLengthStrings, 128);
345     SaveString(CHUNK_ID_ICMT, lstINFO, Comments, defaultComments, UseFixedLengthStrings, 1024);
346     SaveString(CHUNK_ID_IPRD, lstINFO, Product, String(""), UseFixedLengthStrings, 128);
347     SaveString(CHUNK_ID_ICOP, lstINFO, Copyright, String(""), UseFixedLengthStrings, 128);
348     SaveString(CHUNK_ID_IART, lstINFO, Artists, String(""), UseFixedLengthStrings, 128);
349     SaveString(CHUNK_ID_IGNR, lstINFO, Genre, String(""), UseFixedLengthStrings, 128);
350     SaveString(CHUNK_ID_IKEY, lstINFO, Keywords, String(""), UseFixedLengthStrings, 128);
351     SaveString(CHUNK_ID_IENG, lstINFO, Engineer, String(""), UseFixedLengthStrings, 128);
352     SaveString(CHUNK_ID_ITCH, lstINFO, Technician, String(""), UseFixedLengthStrings, 128);
353     SaveString(CHUNK_ID_ISFT, lstINFO, Software, defaultSoftware, UseFixedLengthStrings,
354 persson 918 resourceType == LIST_TYPE_INS ?
355 schoenebeck 1106 (Software == "" ? defaultSoftware.length()+1 : Software.length()+1) : 128);
356 schoenebeck 929 SaveString(CHUNK_ID_IMED, lstINFO, Medium, String(""), UseFixedLengthStrings, 128);
357     SaveString(CHUNK_ID_ISRC, lstINFO, Source, String(""), UseFixedLengthStrings, 128);
358     SaveString(CHUNK_ID_ISRF, lstINFO, SourceForm, String(""), UseFixedLengthStrings, 128);
359     SaveString(CHUNK_ID_ICMS, lstINFO, Commissioned, String(""), UseFixedLengthStrings, 128);
360     SaveString(CHUNK_ID_ISBJ, lstINFO, Subject, String(""), UseFixedLengthStrings, 128);
361 schoenebeck 800 }
362    
363    
364    
365 schoenebeck 2 // *************** Resource ***************
366     // *
367    
368 schoenebeck 800 /** @brief Constructor.
369     *
370     * Initializes the 'Resource' object with values provided by a given
371     * INFO list chunk and a DLID chunk (the latter optional).
372     *
373     * @param Parent - pointer to parent 'Resource', NULL if this is
374     * the toplevel 'Resource' object
375     * @param lstResource - pointer to an INFO list chunk
376     */
377 schoenebeck 2 Resource::Resource(Resource* Parent, RIFF::List* lstResource) {
378     pParent = Parent;
379 schoenebeck 800 pResourceList = lstResource;
380 schoenebeck 2
381     pInfo = new Info(lstResource);
382    
383     RIFF::Chunk* ckDLSID = lstResource->GetSubChunk(CHUNK_ID_DLID);
384     if (ckDLSID) {
385     pDLSID = new dlsid_t;
386 schoenebeck 11 ckDLSID->Read(&pDLSID->ulData1, 1, 4);
387     ckDLSID->Read(&pDLSID->usData2, 1, 2);
388     ckDLSID->Read(&pDLSID->usData3, 1, 2);
389     ckDLSID->Read(pDLSID->abData, 8, 1);
390 schoenebeck 2 }
391     else pDLSID = NULL;
392     }
393    
394     Resource::~Resource() {
395     if (pDLSID) delete pDLSID;
396     if (pInfo) delete pInfo;
397     }
398    
399 schoenebeck 800 /** @brief Update chunks with current Resource data.
400     *
401     * Apply Resource data persistently below the previously given resource
402     * list chunk. This will currently only include the INFO data. The DLSID
403     * will not be applied at the moment (yet).
404     *
405     * You have to call File::Save() to make changes persistent.
406     */
407     void Resource::UpdateChunks() {
408     pInfo->UpdateChunks();
409     //TODO: save DLSID
410     }
411 schoenebeck 2
412    
413 schoenebeck 800
414 schoenebeck 2 // *************** Sampler ***************
415     // *
416    
417     Sampler::Sampler(RIFF::List* ParentList) {
418 schoenebeck 800 pParentList = ParentList;
419 schoenebeck 2 RIFF::Chunk* wsmp = ParentList->GetSubChunk(CHUNK_ID_WSMP);
420 schoenebeck 800 if (wsmp) {
421     uiHeaderSize = wsmp->ReadUint32();
422     UnityNote = wsmp->ReadUint16();
423     FineTune = wsmp->ReadInt16();
424     Gain = wsmp->ReadInt32();
425     SamplerOptions = wsmp->ReadUint32();
426     SampleLoops = wsmp->ReadUint32();
427     } else { // 'wsmp' chunk missing
428     uiHeaderSize = 0;
429     UnityNote = 64;
430     FineTune = 0; // +- 0 cents
431     Gain = 0; // 0 dB
432     SamplerOptions = F_WSMP_NO_COMPRESSION;
433     SampleLoops = 0;
434     }
435 schoenebeck 2 NoSampleDepthTruncation = SamplerOptions & F_WSMP_NO_TRUNCATION;
436     NoSampleCompression = SamplerOptions & F_WSMP_NO_COMPRESSION;
437     pSampleLoops = (SampleLoops) ? new sample_loop_t[SampleLoops] : NULL;
438 schoenebeck 800 if (SampleLoops) {
439     wsmp->SetPos(uiHeaderSize);
440     for (uint32_t i = 0; i < SampleLoops; i++) {
441     wsmp->Read(pSampleLoops + i, 4, 4);
442     if (pSampleLoops[i].Size > sizeof(sample_loop_t)) { // if loop struct was extended
443     wsmp->SetPos(pSampleLoops[i].Size - sizeof(sample_loop_t), RIFF::stream_curpos);
444     }
445 schoenebeck 2 }
446     }
447     }
448    
449     Sampler::~Sampler() {
450     if (pSampleLoops) delete[] pSampleLoops;
451     }
452    
453 schoenebeck 800 /**
454     * Apply all sample player options to the respective RIFF chunk. You
455     * have to call File::Save() to make changes persistent.
456     */
457     void Sampler::UpdateChunks() {
458     // make sure 'wsmp' chunk exists
459     RIFF::Chunk* wsmp = pParentList->GetSubChunk(CHUNK_ID_WSMP);
460     if (!wsmp) {
461     uiHeaderSize = 20;
462     wsmp = pParentList->AddSubChunk(CHUNK_ID_WSMP, uiHeaderSize + SampleLoops * 16);
463     }
464     uint8_t* pData = (uint8_t*) wsmp->LoadChunkData();
465     // update headers size
466 persson 1179 store32(&pData[0], uiHeaderSize);
467 schoenebeck 800 // update respective sampler options bits
468     SamplerOptions = (NoSampleDepthTruncation) ? SamplerOptions | F_WSMP_NO_TRUNCATION
469     : SamplerOptions & (~F_WSMP_NO_TRUNCATION);
470     SamplerOptions = (NoSampleCompression) ? SamplerOptions | F_WSMP_NO_COMPRESSION
471     : SamplerOptions & (~F_WSMP_NO_COMPRESSION);
472 persson 1179 store16(&pData[4], UnityNote);
473     store16(&pData[6], FineTune);
474     store32(&pData[8], Gain);
475     store32(&pData[12], SamplerOptions);
476     store32(&pData[16], SampleLoops);
477 schoenebeck 800 // update loop definitions
478     for (uint32_t i = 0; i < SampleLoops; i++) {
479     //FIXME: this does not handle extended loop structs correctly
480 persson 1179 store32(&pData[uiHeaderSize + i * 16], pSampleLoops[i].Size);
481     store32(&pData[uiHeaderSize + i * 16 + 4], pSampleLoops[i].LoopType);
482     store32(&pData[uiHeaderSize + i * 16 + 8], pSampleLoops[i].LoopStart);
483     store32(&pData[uiHeaderSize + i * 16 + 12], pSampleLoops[i].LoopLength);
484 schoenebeck 800 }
485     }
486 schoenebeck 2
487 schoenebeck 1154 /**
488     * Adds a new sample loop with the provided loop definition.
489     *
490     * @param - points to a loop definition that is to be copied
491     */
492     void Sampler::AddSampleLoop(sample_loop_t* pLoopDef) {
493     sample_loop_t* pNewLoops = new sample_loop_t[SampleLoops + 1];
494     // copy old loops array
495     for (int i = 0; i < SampleLoops; i++) {
496     pNewLoops[i] = pSampleLoops[i];
497     }
498     // add the new loop
499     pNewLoops[SampleLoops] = *pLoopDef;
500 schoenebeck 1155 // auto correct size field
501     pNewLoops[SampleLoops].Size = sizeof(DLS::sample_loop_t);
502 schoenebeck 1154 // free the old array and update the member variables
503     if (SampleLoops) delete[] pSampleLoops;
504     pSampleLoops = pNewLoops;
505     SampleLoops++;
506     }
507 schoenebeck 2
508 schoenebeck 1154 /**
509     * Deletes an existing sample loop.
510     *
511     * @param pLoopDef - pointer to existing loop definition
512     * @throws Exception - if given loop definition does not exist
513     */
514     void Sampler::DeleteSampleLoop(sample_loop_t* pLoopDef) {
515     sample_loop_t* pNewLoops = new sample_loop_t[SampleLoops - 1];
516     // copy old loops array (skipping given loop)
517     for (int i = 0, o = 0; i < SampleLoops; i++) {
518     if (&pSampleLoops[i] == pLoopDef) continue;
519     if (o == SampleLoops - 1)
520     throw Exception("Could not delete Sample Loop, because it does not exist");
521     pNewLoops[o] = pSampleLoops[i];
522     o++;
523     }
524     // free the old array and update the member variables
525     if (SampleLoops) delete[] pSampleLoops;
526     pSampleLoops = pNewLoops;
527     SampleLoops--;
528     }
529 schoenebeck 800
530 schoenebeck 1154
531    
532 schoenebeck 2 // *************** Sample ***************
533     // *
534    
535 schoenebeck 800 /** @brief Constructor.
536     *
537     * Load an existing sample or create a new one. A 'wave' list chunk must
538     * be given to this constructor. In case the given 'wave' list chunk
539     * contains a 'fmt' and 'data' chunk, the format and sample data will be
540     * loaded from there, otherwise default values will be used and those
541     * chunks will be created when File::Save() will be called later on.
542     *
543     * @param pFile - pointer to DLS::File where this sample is
544     * located (or will be located)
545     * @param waveList - pointer to 'wave' list chunk which is (or
546     * will be) associated with this sample
547     * @param WavePoolOffset - offset of this sample data from wave pool
548     * ('wvpl') list chunk
549     */
550 schoenebeck 2 Sample::Sample(File* pFile, RIFF::List* waveList, unsigned long WavePoolOffset) : Resource(pFile, waveList) {
551 schoenebeck 800 pWaveList = waveList;
552 schoenebeck 2 ulWavePoolOffset = WavePoolOffset - LIST_HEADER_SIZE;
553     pCkFormat = waveList->GetSubChunk(CHUNK_ID_FMT);
554     pCkData = waveList->GetSubChunk(CHUNK_ID_DATA);
555 schoenebeck 800 if (pCkFormat) {
556     // common fields
557     FormatTag = pCkFormat->ReadUint16();
558     Channels = pCkFormat->ReadUint16();
559     SamplesPerSecond = pCkFormat->ReadUint32();
560     AverageBytesPerSecond = pCkFormat->ReadUint32();
561     BlockAlign = pCkFormat->ReadUint16();
562     // PCM format specific
563 schoenebeck 1050 if (FormatTag == DLS_WAVE_FORMAT_PCM) {
564 schoenebeck 800 BitDepth = pCkFormat->ReadUint16();
565 persson 928 FrameSize = (BitDepth / 8) * Channels;
566 schoenebeck 800 } else { // unsupported sample data format
567     BitDepth = 0;
568     FrameSize = 0;
569     }
570     } else { // 'fmt' chunk missing
571 schoenebeck 1050 FormatTag = DLS_WAVE_FORMAT_PCM;
572 schoenebeck 800 BitDepth = 16;
573     Channels = 1;
574     SamplesPerSecond = 44100;
575     AverageBytesPerSecond = (BitDepth / 8) * SamplesPerSecond * Channels;
576     FrameSize = (BitDepth / 8) * Channels;
577     BlockAlign = FrameSize;
578 schoenebeck 2 }
579 schoenebeck 1050 SamplesTotal = (pCkData) ? (FormatTag == DLS_WAVE_FORMAT_PCM) ? pCkData->GetSize() / FrameSize
580     : 0
581 schoenebeck 800 : 0;
582 schoenebeck 2 }
583    
584 schoenebeck 800 /** @brief Destructor.
585     *
586     * Removes RIFF chunks associated with this Sample and frees all
587     * memory occupied by this sample.
588     */
589     Sample::~Sample() {
590     RIFF::List* pParent = pWaveList->GetParent();
591     pParent->DeleteSubChunk(pWaveList);
592     }
593    
594     /** @brief Load sample data into RAM.
595     *
596     * In case the respective 'data' chunk exists, the sample data will be
597     * loaded into RAM (if not done already) and a pointer to the data in
598     * RAM will be returned. If this is a new sample, you have to call
599     * Resize() with the desired sample size to create the mandatory RIFF
600     * chunk for the sample wave data.
601     *
602     * You can call LoadChunkData() again if you previously scheduled to
603     * enlarge the sample data RIFF chunk with a Resize() call. In that case
604     * the buffer will be enlarged to the new, scheduled size and you can
605     * already place the sample wave data to the buffer and finally call
606     * File::Save() to enlarge the sample data's chunk physically and write
607     * the new sample wave data in one rush. This approach is definitely
608     * recommended if you have to enlarge and write new sample data to a lot
609     * of samples.
610     *
611     * <b>Caution:</b> the buffer pointer will be invalidated once
612     * File::Save() was called. You have to call LoadChunkData() again to
613     * get a new, valid pointer whenever File::Save() was called.
614     *
615     * @returns pointer to sample data in RAM, NULL in case respective
616     * 'data' chunk does not exist (yet)
617     * @throws Exception if data buffer could not be enlarged
618     * @see Resize(), File::Save()
619     */
620 schoenebeck 2 void* Sample::LoadSampleData() {
621 schoenebeck 800 return (pCkData) ? pCkData->LoadChunkData() : NULL;
622 schoenebeck 2 }
623    
624 schoenebeck 800 /** @brief Free sample data from RAM.
625     *
626     * In case sample data was previously successfully loaded into RAM with
627     * LoadSampleData(), this method will free the sample data from RAM.
628     */
629 schoenebeck 2 void Sample::ReleaseSampleData() {
630 schoenebeck 800 if (pCkData) pCkData->ReleaseChunkData();
631 schoenebeck 2 }
632    
633 schoenebeck 800 /** @brief Returns sample size.
634     *
635     * Returns the sample wave form's data size (in sample points). This is
636     * actually the current, physical size (converted to sample points) of
637     * the RIFF chunk which encapsulates the sample's wave data. The
638     * returned value is dependant to the current FrameSize value.
639     *
640 schoenebeck 1050 * @returns number of sample points or 0 if FormatTag != DLS_WAVE_FORMAT_PCM
641 schoenebeck 800 * @see FrameSize, FormatTag
642     */
643     unsigned long Sample::GetSize() {
644 schoenebeck 1050 if (FormatTag != DLS_WAVE_FORMAT_PCM) return 0;
645 schoenebeck 800 return (pCkData) ? pCkData->GetSize() / FrameSize : 0;
646     }
647    
648     /** @brief Resize sample.
649     *
650     * Resizes the sample's wave form data, that is the actual size of
651     * sample wave data possible to be written for this sample. This call
652     * will return immediately and just schedule the resize operation. You
653     * should call File::Save() to actually perform the resize operation(s)
654     * "physically" to the file. As this can take a while on large files, it
655     * is recommended to call Resize() first on all samples which have to be
656     * resized and finally to call File::Save() to perform all those resize
657     * operations in one rush.
658     *
659     * The actual size (in bytes) is dependant to the current FrameSize
660     * value. You may want to set FrameSize before calling Resize().
661     *
662     * <b>Caution:</b> You cannot directly write to enlarged samples before
663     * calling File::Save() as this might exceed the current sample's
664     * boundary!
665     *
666 schoenebeck 1050 * Also note: only DLS_WAVE_FORMAT_PCM is currently supported, that is
667     * FormatTag must be DLS_WAVE_FORMAT_PCM. Trying to resize samples with
668 schoenebeck 800 * other formats will fail!
669     *
670     * @param iNewSize - new sample wave data size in sample points (must be
671     * greater than zero)
672 schoenebeck 1050 * @throws Excecption if FormatTag != DLS_WAVE_FORMAT_PCM
673 schoenebeck 800 * @throws Exception if \a iNewSize is less than 1
674     * @see File::Save(), FrameSize, FormatTag
675     */
676     void Sample::Resize(int iNewSize) {
677 schoenebeck 1050 if (FormatTag != DLS_WAVE_FORMAT_PCM) throw Exception("Sample's format is not DLS_WAVE_FORMAT_PCM");
678 schoenebeck 800 if (iNewSize < 1) throw Exception("Sample size must be at least one sample point");
679     const int iSizeInBytes = iNewSize * FrameSize;
680     pCkData = pWaveList->GetSubChunk(CHUNK_ID_DATA);
681     if (pCkData) pCkData->Resize(iSizeInBytes);
682     else pCkData = pWaveList->AddSubChunk(CHUNK_ID_DATA, iSizeInBytes);
683     }
684    
685 schoenebeck 2 /**
686     * Sets the position within the sample (in sample points, not in
687     * bytes). Use this method and <i>Read()</i> if you don't want to load
688     * the sample into RAM, thus for disk streaming.
689     *
690 schoenebeck 1050 * Also note: only DLS_WAVE_FORMAT_PCM is currently supported, that is
691     * FormatTag must be DLS_WAVE_FORMAT_PCM. Trying to reposition the sample
692 schoenebeck 800 * with other formats will fail!
693     *
694 schoenebeck 2 * @param SampleCount number of sample points
695     * @param Whence to which relation \a SampleCount refers to
696 schoenebeck 800 * @returns new position within the sample, 0 if
697 schoenebeck 1050 * FormatTag != DLS_WAVE_FORMAT_PCM
698 schoenebeck 800 * @throws Exception if no data RIFF chunk was created for the sample yet
699     * @see FrameSize, FormatTag
700 schoenebeck 2 */
701     unsigned long Sample::SetPos(unsigned long SampleCount, RIFF::stream_whence_t Whence) {
702 schoenebeck 1050 if (FormatTag != DLS_WAVE_FORMAT_PCM) return 0; // failed: wave data not PCM format
703 schoenebeck 800 if (!pCkData) throw Exception("No data chunk created for sample yet, call Sample::Resize() to create one");
704 schoenebeck 2 unsigned long orderedBytes = SampleCount * FrameSize;
705     unsigned long result = pCkData->SetPos(orderedBytes, Whence);
706     return (result == orderedBytes) ? SampleCount
707     : result / FrameSize;
708     }
709    
710     /**
711     * Reads \a SampleCount number of sample points from the current
712     * position into the buffer pointed by \a pBuffer and increments the
713     * position within the sample. Use this method and <i>SetPos()</i> if you
714     * don't want to load the sample into RAM, thus for disk streaming.
715     *
716     * @param pBuffer destination buffer
717     * @param SampleCount number of sample points to read
718     */
719     unsigned long Sample::Read(void* pBuffer, unsigned long SampleCount) {
720 schoenebeck 1050 if (FormatTag != DLS_WAVE_FORMAT_PCM) return 0; // failed: wave data not PCM format
721 schoenebeck 11 return pCkData->Read(pBuffer, SampleCount, FrameSize); // FIXME: channel inversion due to endian correction?
722 schoenebeck 2 }
723    
724 schoenebeck 800 /** @brief Write sample wave data.
725     *
726     * Writes \a SampleCount number of sample points from the buffer pointed
727     * by \a pBuffer and increments the position within the sample. Use this
728     * method to directly write the sample data to disk, i.e. if you don't
729     * want or cannot load the whole sample data into RAM.
730     *
731     * You have to Resize() the sample to the desired size and call
732     * File::Save() <b>before</b> using Write().
733     *
734     * @param pBuffer - source buffer
735     * @param SampleCount - number of sample points to write
736     * @throws Exception if current sample size is too small
737     * @see LoadSampleData()
738     */
739     unsigned long Sample::Write(void* pBuffer, unsigned long SampleCount) {
740 schoenebeck 1050 if (FormatTag != DLS_WAVE_FORMAT_PCM) return 0; // failed: wave data not PCM format
741 schoenebeck 800 if (GetSize() < SampleCount) throw Exception("Could not write sample data, current sample size to small");
742     return pCkData->Write(pBuffer, SampleCount, FrameSize); // FIXME: channel inversion due to endian correction?
743     }
744 schoenebeck 2
745 schoenebeck 800 /**
746     * Apply sample and its settings to the respective RIFF chunks. You have
747     * to call File::Save() to make changes persistent.
748     *
749 schoenebeck 1050 * @throws Exception if FormatTag != DLS_WAVE_FORMAT_PCM or no sample data
750 schoenebeck 800 * was provided yet
751     */
752     void Sample::UpdateChunks() {
753 schoenebeck 1050 if (FormatTag != DLS_WAVE_FORMAT_PCM)
754 schoenebeck 800 throw Exception("Could not save sample, only PCM format is supported");
755     // we refuse to do anything if not sample wave form was provided yet
756     if (!pCkData)
757     throw Exception("Could not save sample, there is no sample data to save");
758     // update chunks of base class as well
759     Resource::UpdateChunks();
760     // make sure 'fmt' chunk exists
761     RIFF::Chunk* pCkFormat = pWaveList->GetSubChunk(CHUNK_ID_FMT);
762     if (!pCkFormat) pCkFormat = pWaveList->AddSubChunk(CHUNK_ID_FMT, 16); // assumes PCM format
763     uint8_t* pData = (uint8_t*) pCkFormat->LoadChunkData();
764     // update 'fmt' chunk
765 persson 1179 store16(&pData[0], FormatTag);
766     store16(&pData[2], Channels);
767     store32(&pData[4], SamplesPerSecond);
768     store32(&pData[8], AverageBytesPerSecond);
769     store16(&pData[12], BlockAlign);
770     store16(&pData[14], BitDepth); // assuming PCM format
771 schoenebeck 800 }
772 schoenebeck 2
773 schoenebeck 800
774    
775 schoenebeck 2 // *************** Region ***************
776     // *
777    
778     Region::Region(Instrument* pInstrument, RIFF::List* rgnList) : Resource(pInstrument, rgnList), Articulator(rgnList), Sampler(rgnList) {
779     pCkRegion = rgnList;
780    
781 schoenebeck 800 // articulation informations
782 schoenebeck 2 RIFF::Chunk* rgnh = rgnList->GetSubChunk(CHUNK_ID_RGNH);
783 schoenebeck 800 if (rgnh) {
784     rgnh->Read(&KeyRange, 2, 2);
785     rgnh->Read(&VelocityRange, 2, 2);
786     FormatOptionFlags = rgnh->ReadUint16();
787     KeyGroup = rgnh->ReadUint16();
788     // Layer is optional
789     if (rgnh->RemainingBytes() >= sizeof(uint16_t)) {
790     rgnh->Read(&Layer, 1, sizeof(uint16_t));
791     } else Layer = 0;
792     } else { // 'rgnh' chunk is missing
793     KeyRange.low = 0;
794     KeyRange.high = 127;
795     VelocityRange.low = 0;
796     VelocityRange.high = 127;
797     FormatOptionFlags = F_RGN_OPTION_SELFNONEXCLUSIVE;
798     KeyGroup = 0;
799     Layer = 0;
800 schoenebeck 2 }
801 schoenebeck 800 SelfNonExclusive = FormatOptionFlags & F_RGN_OPTION_SELFNONEXCLUSIVE;
802 schoenebeck 2
803 schoenebeck 800 // sample informations
804 schoenebeck 2 RIFF::Chunk* wlnk = rgnList->GetSubChunk(CHUNK_ID_WLNK);
805 schoenebeck 800 if (wlnk) {
806     WaveLinkOptionFlags = wlnk->ReadUint16();
807     PhaseGroup = wlnk->ReadUint16();
808     Channel = wlnk->ReadUint32();
809     WavePoolTableIndex = wlnk->ReadUint32();
810     } else { // 'wlnk' chunk is missing
811     WaveLinkOptionFlags = 0;
812     PhaseGroup = 0;
813     Channel = 0; // mono
814     WavePoolTableIndex = 0; // first entry in wave pool table
815     }
816     PhaseMaster = WaveLinkOptionFlags & F_WAVELINK_PHASE_MASTER;
817     MultiChannel = WaveLinkOptionFlags & F_WAVELINK_MULTICHANNEL;
818 schoenebeck 2
819     pSample = NULL;
820     }
821    
822 schoenebeck 800 /** @brief Destructor.
823     *
824     * Removes RIFF chunks associated with this Region.
825     */
826 schoenebeck 2 Region::~Region() {
827 schoenebeck 800 RIFF::List* pParent = pCkRegion->GetParent();
828     pParent->DeleteSubChunk(pCkRegion);
829 schoenebeck 2 }
830    
831     Sample* Region::GetSample() {
832     if (pSample) return pSample;
833     File* file = (File*) GetParent()->GetParent();
834     unsigned long soughtoffset = file->pWavePoolTable[WavePoolTableIndex];
835     Sample* sample = file->GetFirstSample();
836     while (sample) {
837     if (sample->ulWavePoolOffset == soughtoffset) return (pSample = sample);
838     sample = file->GetNextSample();
839     }
840     return NULL;
841     }
842    
843 schoenebeck 800 /**
844     * Assign another sample to this Region.
845     *
846     * @param pSample - sample to be assigned
847     */
848     void Region::SetSample(Sample* pSample) {
849     this->pSample = pSample;
850     WavePoolTableIndex = 0; // we update this offset when we Save()
851     }
852 schoenebeck 2
853 schoenebeck 800 /**
854     * Apply Region settings to the respective RIFF chunks. You have to
855     * call File::Save() to make changes persistent.
856     *
857     * @throws Exception - if the Region's sample could not be found
858     */
859     void Region::UpdateChunks() {
860     // make sure 'rgnh' chunk exists
861     RIFF::Chunk* rgnh = pCkRegion->GetSubChunk(CHUNK_ID_RGNH);
862 persson 918 if (!rgnh) rgnh = pCkRegion->AddSubChunk(CHUNK_ID_RGNH, Layer ? 14 : 12);
863 schoenebeck 800 uint8_t* pData = (uint8_t*) rgnh->LoadChunkData();
864     FormatOptionFlags = (SelfNonExclusive)
865     ? FormatOptionFlags | F_RGN_OPTION_SELFNONEXCLUSIVE
866     : FormatOptionFlags & (~F_RGN_OPTION_SELFNONEXCLUSIVE);
867     // update 'rgnh' chunk
868 persson 1179 store16(&pData[0], KeyRange.low);
869     store16(&pData[2], KeyRange.high);
870     store16(&pData[4], VelocityRange.low);
871     store16(&pData[6], VelocityRange.high);
872     store16(&pData[8], FormatOptionFlags);
873     store16(&pData[10], KeyGroup);
874     if (rgnh->GetSize() >= 14) store16(&pData[12], Layer);
875 schoenebeck 2
876 persson 918 // update chunks of base classes as well (but skip Resource,
877     // as a rgn doesn't seem to have dlid and INFO chunks)
878 schoenebeck 800 Articulator::UpdateChunks();
879     Sampler::UpdateChunks();
880    
881     // make sure 'wlnk' chunk exists
882     RIFF::Chunk* wlnk = pCkRegion->GetSubChunk(CHUNK_ID_WLNK);
883     if (!wlnk) wlnk = pCkRegion->AddSubChunk(CHUNK_ID_WLNK, 12);
884     pData = (uint8_t*) wlnk->LoadChunkData();
885     WaveLinkOptionFlags = (PhaseMaster)
886     ? WaveLinkOptionFlags | F_WAVELINK_PHASE_MASTER
887     : WaveLinkOptionFlags & (~F_WAVELINK_PHASE_MASTER);
888     WaveLinkOptionFlags = (MultiChannel)
889     ? WaveLinkOptionFlags | F_WAVELINK_MULTICHANNEL
890     : WaveLinkOptionFlags & (~F_WAVELINK_MULTICHANNEL);
891     // get sample's wave pool table index
892     int index = -1;
893     File* pFile = (File*) GetParent()->GetParent();
894 schoenebeck 804 if (pFile->pSamples) {
895     File::SampleList::iterator iter = pFile->pSamples->begin();
896     File::SampleList::iterator end = pFile->pSamples->end();
897     for (int i = 0; iter != end; ++iter, i++) {
898     if (*iter == pSample) {
899     index = i;
900     break;
901     }
902 schoenebeck 800 }
903     }
904     if (index < 0) throw Exception("Could not save Region, could not find Region's sample");
905     WavePoolTableIndex = index;
906     // update 'wlnk' chunk
907 persson 1179 store16(&pData[0], WaveLinkOptionFlags);
908     store16(&pData[2], PhaseGroup);
909     store32(&pData[4], Channel);
910     store32(&pData[8], WavePoolTableIndex);
911 schoenebeck 800 }
912    
913    
914    
915 schoenebeck 2 // *************** Instrument ***************
916     // *
917    
918 schoenebeck 800 /** @brief Constructor.
919     *
920     * Load an existing instrument definition or create a new one. An 'ins'
921     * list chunk must be given to this constructor. In case this 'ins' list
922     * chunk contains a 'insh' chunk, the instrument data fields will be
923     * loaded from there, otherwise default values will be used and the
924     * 'insh' chunk will be created once File::Save() was called.
925     *
926     * @param pFile - pointer to DLS::File where this instrument is
927     * located (or will be located)
928     * @param insList - pointer to 'ins' list chunk which is (or will be)
929     * associated with this instrument
930     */
931 schoenebeck 2 Instrument::Instrument(File* pFile, RIFF::List* insList) : Resource(pFile, insList), Articulator(insList) {
932     pCkInstrument = insList;
933    
934 schoenebeck 800 midi_locale_t locale;
935 schoenebeck 2 RIFF::Chunk* insh = pCkInstrument->GetSubChunk(CHUNK_ID_INSH);
936 schoenebeck 800 if (insh) {
937     Regions = insh->ReadUint32();
938     insh->Read(&locale, 2, 4);
939     } else { // 'insh' chunk missing
940     Regions = 0;
941     locale.bank = 0;
942     locale.instrument = 0;
943     }
944    
945 schoenebeck 2 MIDIProgram = locale.instrument;
946     IsDrum = locale.bank & DRUM_TYPE_MASK;
947     MIDIBankCoarse = (uint8_t) MIDI_BANK_COARSE(locale.bank);
948     MIDIBankFine = (uint8_t) MIDI_BANK_FINE(locale.bank);
949     MIDIBank = MIDI_BANK_MERGE(MIDIBankCoarse, MIDIBankFine);
950    
951 schoenebeck 800 pRegions = NULL;
952 schoenebeck 2 }
953    
954     Region* Instrument::GetFirstRegion() {
955     if (!pRegions) LoadRegions();
956     if (!pRegions) return NULL;
957     RegionsIterator = pRegions->begin();
958     return (RegionsIterator != pRegions->end()) ? *RegionsIterator : NULL;
959     }
960    
961     Region* Instrument::GetNextRegion() {
962     if (!pRegions) return NULL;
963     RegionsIterator++;
964     return (RegionsIterator != pRegions->end()) ? *RegionsIterator : NULL;
965     }
966    
967     void Instrument::LoadRegions() {
968 schoenebeck 823 if (!pRegions) pRegions = new RegionList;
969 schoenebeck 2 RIFF::List* lrgn = pCkInstrument->GetSubList(LIST_TYPE_LRGN);
970 schoenebeck 823 if (lrgn) {
971     uint32_t regionCkType = (lrgn->GetSubList(LIST_TYPE_RGN2)) ? LIST_TYPE_RGN2 : LIST_TYPE_RGN; // prefer regions level 2
972     RIFF::List* rgn = lrgn->GetFirstSubList();
973     while (rgn) {
974     if (rgn->GetListType() == regionCkType) {
975     pRegions->push_back(new Region(this, rgn));
976     }
977     rgn = lrgn->GetNextSubList();
978 schoenebeck 2 }
979     }
980     }
981    
982 schoenebeck 800 Region* Instrument::AddRegion() {
983 schoenebeck 823 if (!pRegions) LoadRegions();
984 schoenebeck 800 RIFF::List* lrgn = pCkInstrument->GetSubList(LIST_TYPE_LRGN);
985     if (!lrgn) lrgn = pCkInstrument->AddSubList(LIST_TYPE_LRGN);
986     RIFF::List* rgn = lrgn->AddSubList(LIST_TYPE_RGN);
987     Region* pNewRegion = new Region(this, rgn);
988     pRegions->push_back(pNewRegion);
989 schoenebeck 823 Regions = pRegions->size();
990 schoenebeck 800 return pNewRegion;
991     }
992    
993 persson 1102 void Instrument::MoveRegion(Region* pSrc, Region* pDst) {
994     RIFF::List* lrgn = pCkInstrument->GetSubList(LIST_TYPE_LRGN);
995     lrgn->MoveSubChunk(pSrc->pCkRegion, pDst ? pDst->pCkRegion : 0);
996    
997     pRegions->remove(pSrc);
998     RegionList::iterator iter = find(pRegions->begin(), pRegions->end(), pDst);
999     pRegions->insert(iter, pSrc);
1000     }
1001    
1002 schoenebeck 800 void Instrument::DeleteRegion(Region* pRegion) {
1003 schoenebeck 802 if (!pRegions) return;
1004 schoenebeck 800 RegionList::iterator iter = find(pRegions->begin(), pRegions->end(), pRegion);
1005     if (iter == pRegions->end()) return;
1006     pRegions->erase(iter);
1007 schoenebeck 823 Regions = pRegions->size();
1008 schoenebeck 800 delete pRegion;
1009     }
1010    
1011     /**
1012     * Apply Instrument with all its Regions to the respective RIFF chunks.
1013     * You have to call File::Save() to make changes persistent.
1014     *
1015     * @throws Exception - on errors
1016     */
1017     void Instrument::UpdateChunks() {
1018     // first update base classes' chunks
1019     Resource::UpdateChunks();
1020     Articulator::UpdateChunks();
1021     // make sure 'insh' chunk exists
1022     RIFF::Chunk* insh = pCkInstrument->GetSubChunk(CHUNK_ID_INSH);
1023     if (!insh) insh = pCkInstrument->AddSubChunk(CHUNK_ID_INSH, 12);
1024     uint8_t* pData = (uint8_t*) insh->LoadChunkData();
1025     // update 'insh' chunk
1026 schoenebeck 804 Regions = (pRegions) ? pRegions->size() : 0;
1027 schoenebeck 800 midi_locale_t locale;
1028     locale.instrument = MIDIProgram;
1029     locale.bank = MIDI_BANK_ENCODE(MIDIBankCoarse, MIDIBankFine);
1030     locale.bank = (IsDrum) ? locale.bank | DRUM_TYPE_MASK : locale.bank & (~DRUM_TYPE_MASK);
1031     MIDIBank = MIDI_BANK_MERGE(MIDIBankCoarse, MIDIBankFine); // just a sync, when we're at it
1032 persson 1179 store32(&pData[0], Regions);
1033     store32(&pData[4], locale.bank);
1034     store32(&pData[8], locale.instrument);
1035 schoenebeck 800 // update Region's chunks
1036 schoenebeck 804 if (!pRegions) return;
1037 schoenebeck 800 RegionList::iterator iter = pRegions->begin();
1038     RegionList::iterator end = pRegions->end();
1039     for (; iter != end; ++iter) {
1040     (*iter)->UpdateChunks();
1041     }
1042     }
1043    
1044     /** @brief Destructor.
1045     *
1046     * Removes RIFF chunks associated with this Instrument and frees all
1047     * memory occupied by this instrument.
1048     */
1049 schoenebeck 2 Instrument::~Instrument() {
1050     if (pRegions) {
1051     RegionList::iterator iter = pRegions->begin();
1052     RegionList::iterator end = pRegions->end();
1053     while (iter != end) {
1054     delete *iter;
1055     iter++;
1056     }
1057     delete pRegions;
1058     }
1059 schoenebeck 800 // remove instrument's chunks
1060     RIFF::List* pParent = pCkInstrument->GetParent();
1061     pParent->DeleteSubChunk(pCkInstrument);
1062 schoenebeck 2 }
1063    
1064    
1065    
1066     // *************** File ***************
1067     // *
1068    
1069 schoenebeck 800 /** @brief Constructor.
1070     *
1071     * Default constructor, use this to create an empty DLS file. You have
1072     * to add samples, instruments and finally call Save() to actually write
1073     * a DLS file.
1074     */
1075 schoenebeck 802 File::File() : Resource(NULL, pRIFF = new RIFF::File(RIFF_TYPE_DLS)) {
1076 schoenebeck 800 pVersion = new version_t;
1077     pVersion->major = 0;
1078     pVersion->minor = 0;
1079     pVersion->release = 0;
1080     pVersion->build = 0;
1081    
1082     Instruments = 0;
1083     WavePoolCount = 0;
1084     pWavePoolTable = NULL;
1085     pWavePoolTableHi = NULL;
1086     WavePoolHeaderSize = 8;
1087    
1088     pSamples = NULL;
1089     pInstruments = NULL;
1090    
1091     b64BitWavePoolOffsets = false;
1092     }
1093    
1094     /** @brief Constructor.
1095     *
1096     * Load an existing DLS file.
1097     *
1098     * @param pRIFF - pointer to a RIFF file which is actually the DLS file
1099     * to load
1100     * @throws Exception if given file is not a DLS file, expected chunks
1101     * are missing
1102     */
1103 schoenebeck 2 File::File(RIFF::File* pRIFF) : Resource(NULL, pRIFF) {
1104     if (!pRIFF) throw DLS::Exception("NULL pointer reference to RIFF::File object.");
1105     this->pRIFF = pRIFF;
1106    
1107     RIFF::Chunk* ckVersion = pRIFF->GetSubChunk(CHUNK_ID_VERS);
1108     if (ckVersion) {
1109     pVersion = new version_t;
1110 schoenebeck 11 ckVersion->Read(pVersion, 4, 2);
1111 schoenebeck 2 }
1112     else pVersion = NULL;
1113    
1114     RIFF::Chunk* colh = pRIFF->GetSubChunk(CHUNK_ID_COLH);
1115     if (!colh) throw DLS::Exception("Mandatory chunks in RIFF list chunk not found.");
1116     Instruments = colh->ReadUint32();
1117    
1118     RIFF::Chunk* ptbl = pRIFF->GetSubChunk(CHUNK_ID_PTBL);
1119 persson 902 if (!ptbl) { // pool table is missing - this is probably an ".art" file
1120     WavePoolCount = 0;
1121     pWavePoolTable = NULL;
1122     pWavePoolTableHi = NULL;
1123     WavePoolHeaderSize = 8;
1124     b64BitWavePoolOffsets = false;
1125     } else {
1126     WavePoolHeaderSize = ptbl->ReadUint32();
1127     WavePoolCount = ptbl->ReadUint32();
1128     pWavePoolTable = new uint32_t[WavePoolCount];
1129     pWavePoolTableHi = new uint32_t[WavePoolCount];
1130     ptbl->SetPos(WavePoolHeaderSize);
1131 schoenebeck 2
1132 persson 902 // Check for 64 bit offsets (used in gig v3 files)
1133     b64BitWavePoolOffsets = (ptbl->GetSize() - WavePoolHeaderSize == WavePoolCount * 8);
1134     if (b64BitWavePoolOffsets) {
1135     for (int i = 0 ; i < WavePoolCount ; i++) {
1136     pWavePoolTableHi[i] = ptbl->ReadUint32();
1137     pWavePoolTable[i] = ptbl->ReadUint32();
1138     if (pWavePoolTable[i] & 0x80000000)
1139     throw DLS::Exception("Files larger than 2 GB not yet supported");
1140     }
1141     } else { // conventional 32 bit offsets
1142     ptbl->Read(pWavePoolTable, WavePoolCount, sizeof(uint32_t));
1143     for (int i = 0 ; i < WavePoolCount ; i++) pWavePoolTableHi[i] = 0;
1144 schoenebeck 317 }
1145 persson 666 }
1146 schoenebeck 317
1147 schoenebeck 2 pSamples = NULL;
1148     pInstruments = NULL;
1149     }
1150    
1151     File::~File() {
1152     if (pInstruments) {
1153     InstrumentList::iterator iter = pInstruments->begin();
1154     InstrumentList::iterator end = pInstruments->end();
1155     while (iter != end) {
1156     delete *iter;
1157     iter++;
1158     }
1159     delete pInstruments;
1160     }
1161    
1162     if (pSamples) {
1163     SampleList::iterator iter = pSamples->begin();
1164     SampleList::iterator end = pSamples->end();
1165     while (iter != end) {
1166     delete *iter;
1167     iter++;
1168     }
1169     delete pSamples;
1170     }
1171    
1172     if (pWavePoolTable) delete[] pWavePoolTable;
1173 persson 666 if (pWavePoolTableHi) delete[] pWavePoolTableHi;
1174 schoenebeck 2 if (pVersion) delete pVersion;
1175 persson 834 for (std::list<RIFF::File*>::iterator i = ExtensionFiles.begin() ; i != ExtensionFiles.end() ; i++)
1176     delete *i;
1177 schoenebeck 2 }
1178    
1179     Sample* File::GetFirstSample() {
1180     if (!pSamples) LoadSamples();
1181     if (!pSamples) return NULL;
1182     SamplesIterator = pSamples->begin();
1183     return (SamplesIterator != pSamples->end()) ? *SamplesIterator : NULL;
1184     }
1185    
1186     Sample* File::GetNextSample() {
1187     if (!pSamples) return NULL;
1188     SamplesIterator++;
1189     return (SamplesIterator != pSamples->end()) ? *SamplesIterator : NULL;
1190     }
1191    
1192     void File::LoadSamples() {
1193 schoenebeck 823 if (!pSamples) pSamples = new SampleList;
1194 schoenebeck 2 RIFF::List* wvpl = pRIFF->GetSubList(LIST_TYPE_WVPL);
1195     if (wvpl) {
1196     unsigned long wvplFileOffset = wvpl->GetFilePos();
1197     RIFF::List* wave = wvpl->GetFirstSubList();
1198     while (wave) {
1199     if (wave->GetListType() == LIST_TYPE_WAVE) {
1200     unsigned long waveFileOffset = wave->GetFilePos();
1201     pSamples->push_back(new Sample(this, wave, waveFileOffset - wvplFileOffset));
1202     }
1203     wave = wvpl->GetNextSubList();
1204     }
1205     }
1206     else { // Seen a dwpl list chunk instead of a wvpl list chunk in some file (officially not DLS compliant)
1207     RIFF::List* dwpl = pRIFF->GetSubList(LIST_TYPE_DWPL);
1208     if (dwpl) {
1209     unsigned long dwplFileOffset = dwpl->GetFilePos();
1210     RIFF::List* wave = dwpl->GetFirstSubList();
1211     while (wave) {
1212     if (wave->GetListType() == LIST_TYPE_WAVE) {
1213     unsigned long waveFileOffset = wave->GetFilePos();
1214     pSamples->push_back(new Sample(this, wave, waveFileOffset - dwplFileOffset));
1215     }
1216     wave = dwpl->GetNextSubList();
1217     }
1218     }
1219     }
1220     }
1221    
1222 schoenebeck 800 /** @brief Add a new sample.
1223     *
1224     * This will create a new Sample object for the DLS file. You have to
1225     * call Save() to make this persistent to the file.
1226     *
1227     * @returns pointer to new Sample object
1228     */
1229     Sample* File::AddSample() {
1230 schoenebeck 809 if (!pSamples) LoadSamples();
1231 schoenebeck 800 __ensureMandatoryChunksExist();
1232     RIFF::List* wvpl = pRIFF->GetSubList(LIST_TYPE_WVPL);
1233     // create new Sample object and its respective 'wave' list chunk
1234     RIFF::List* wave = wvpl->AddSubList(LIST_TYPE_WAVE);
1235     Sample* pSample = new Sample(this, wave, 0 /*arbitrary value, we update offsets when we save*/);
1236     pSamples->push_back(pSample);
1237     return pSample;
1238     }
1239    
1240     /** @brief Delete a sample.
1241     *
1242     * This will delete the given Sample object from the DLS file. You have
1243     * to call Save() to make this persistent to the file.
1244     *
1245     * @param pSample - sample to delete
1246     */
1247     void File::DeleteSample(Sample* pSample) {
1248 schoenebeck 802 if (!pSamples) return;
1249 schoenebeck 800 SampleList::iterator iter = find(pSamples->begin(), pSamples->end(), pSample);
1250     if (iter == pSamples->end()) return;
1251     pSamples->erase(iter);
1252     delete pSample;
1253     }
1254    
1255 schoenebeck 2 Instrument* File::GetFirstInstrument() {
1256     if (!pInstruments) LoadInstruments();
1257     if (!pInstruments) return NULL;
1258     InstrumentsIterator = pInstruments->begin();
1259     return (InstrumentsIterator != pInstruments->end()) ? *InstrumentsIterator : NULL;
1260     }
1261    
1262     Instrument* File::GetNextInstrument() {
1263     if (!pInstruments) return NULL;
1264     InstrumentsIterator++;
1265     return (InstrumentsIterator != pInstruments->end()) ? *InstrumentsIterator : NULL;
1266     }
1267    
1268     void File::LoadInstruments() {
1269 schoenebeck 823 if (!pInstruments) pInstruments = new InstrumentList;
1270 schoenebeck 2 RIFF::List* lstInstruments = pRIFF->GetSubList(LIST_TYPE_LINS);
1271     if (lstInstruments) {
1272     RIFF::List* lstInstr = lstInstruments->GetFirstSubList();
1273     while (lstInstr) {
1274     if (lstInstr->GetListType() == LIST_TYPE_INS) {
1275     pInstruments->push_back(new Instrument(this, lstInstr));
1276     }
1277     lstInstr = lstInstruments->GetNextSubList();
1278     }
1279     }
1280     }
1281    
1282 schoenebeck 800 /** @brief Add a new instrument definition.
1283     *
1284     * This will create a new Instrument object for the DLS file. You have
1285     * to call Save() to make this persistent to the file.
1286     *
1287     * @returns pointer to new Instrument object
1288     */
1289     Instrument* File::AddInstrument() {
1290 schoenebeck 809 if (!pInstruments) LoadInstruments();
1291 schoenebeck 800 __ensureMandatoryChunksExist();
1292     RIFF::List* lstInstruments = pRIFF->GetSubList(LIST_TYPE_LINS);
1293     RIFF::List* lstInstr = lstInstruments->AddSubList(LIST_TYPE_INS);
1294     Instrument* pInstrument = new Instrument(this, lstInstr);
1295     pInstruments->push_back(pInstrument);
1296     return pInstrument;
1297     }
1298 schoenebeck 2
1299 schoenebeck 809 /** @brief Delete an instrument.
1300 schoenebeck 800 *
1301     * This will delete the given Instrument object from the DLS file. You
1302     * have to call Save() to make this persistent to the file.
1303     *
1304     * @param pInstrument - instrument to delete
1305     */
1306     void File::DeleteInstrument(Instrument* pInstrument) {
1307 schoenebeck 802 if (!pInstruments) return;
1308 schoenebeck 800 InstrumentList::iterator iter = find(pInstruments->begin(), pInstruments->end(), pInstrument);
1309     if (iter == pInstruments->end()) return;
1310     pInstruments->erase(iter);
1311     delete pInstrument;
1312     }
1313 schoenebeck 2
1314 schoenebeck 800 /**
1315     * Apply all the DLS file's current instruments, samples and settings to
1316     * the respective RIFF chunks. You have to call Save() to make changes
1317     * persistent.
1318     *
1319     * @throws Exception - on errors
1320     */
1321     void File::UpdateChunks() {
1322     // first update base class's chunks
1323     Resource::UpdateChunks();
1324    
1325     // if version struct exists, update 'vers' chunk
1326     if (pVersion) {
1327     RIFF::Chunk* ckVersion = pRIFF->GetSubChunk(CHUNK_ID_VERS);
1328     if (!ckVersion) ckVersion = pRIFF->AddSubChunk(CHUNK_ID_VERS, 8);
1329     uint8_t* pData = (uint8_t*) ckVersion->LoadChunkData();
1330 persson 1179 store16(&pData[0], pVersion->minor);
1331     store16(&pData[2], pVersion->major);
1332     store16(&pData[4], pVersion->build);
1333     store16(&pData[6], pVersion->release);
1334 schoenebeck 800 }
1335    
1336     // update 'colh' chunk
1337     Instruments = (pInstruments) ? pInstruments->size() : 0;
1338     RIFF::Chunk* colh = pRIFF->GetSubChunk(CHUNK_ID_COLH);
1339     if (!colh) colh = pRIFF->AddSubChunk(CHUNK_ID_COLH, 4);
1340     uint8_t* pData = (uint8_t*) colh->LoadChunkData();
1341 persson 1179 store32(pData, Instruments);
1342 schoenebeck 800
1343     // update instrument's chunks
1344     if (pInstruments) {
1345     InstrumentList::iterator iter = pInstruments->begin();
1346     InstrumentList::iterator end = pInstruments->end();
1347     for (; iter != end; ++iter) {
1348     (*iter)->UpdateChunks();
1349     }
1350     }
1351    
1352     // update 'ptbl' chunk
1353     const int iSamples = (pSamples) ? pSamples->size() : 0;
1354     const int iPtblOffsetSize = (b64BitWavePoolOffsets) ? 8 : 4;
1355     RIFF::Chunk* ptbl = pRIFF->GetSubChunk(CHUNK_ID_PTBL);
1356     if (!ptbl) ptbl = pRIFF->AddSubChunk(CHUNK_ID_PTBL, 1 /*anything, we'll resize*/);
1357     const int iPtblSize = WavePoolHeaderSize + iPtblOffsetSize * iSamples;
1358     ptbl->Resize(iPtblSize);
1359     pData = (uint8_t*) ptbl->LoadChunkData();
1360     WavePoolCount = iSamples;
1361 persson 1179 store32(&pData[4], WavePoolCount);
1362 schoenebeck 800 // we actually update the sample offsets in the pool table when we Save()
1363     memset(&pData[WavePoolHeaderSize], 0, iPtblSize - WavePoolHeaderSize);
1364    
1365     // update sample's chunks
1366     if (pSamples) {
1367     SampleList::iterator iter = pSamples->begin();
1368     SampleList::iterator end = pSamples->end();
1369     for (; iter != end; ++iter) {
1370     (*iter)->UpdateChunks();
1371     }
1372     }
1373     }
1374    
1375     /** @brief Save changes to another file.
1376     *
1377     * Make all changes persistent by writing them to another file.
1378     * <b>Caution:</b> this method is optimized for writing to
1379     * <b>another</b> file, do not use it to save the changes to the same
1380     * file! Use Save() (without path argument) in that case instead!
1381     * Ignoring this might result in a corrupted file!
1382     *
1383     * After calling this method, this File object will be associated with
1384     * the new file (given by \a Path) afterwards.
1385     *
1386     * @param Path - path and file name where everything should be written to
1387     */
1388     void File::Save(const String& Path) {
1389     UpdateChunks();
1390     pRIFF->Save(Path);
1391     __UpdateWavePoolTableChunk();
1392     }
1393    
1394     /** @brief Save changes to same file.
1395     *
1396     * Make all changes persistent by writing them to the actual (same)
1397     * file. The file might temporarily grow to a higher size than it will
1398     * have at the end of the saving process.
1399     *
1400     * @throws RIFF::Exception if any kind of IO error occured
1401     * @throws DLS::Exception if any kind of DLS specific error occured
1402     */
1403     void File::Save() {
1404     UpdateChunks();
1405     pRIFF->Save();
1406     __UpdateWavePoolTableChunk();
1407     }
1408    
1409     /**
1410     * Checks if all (for DLS) mandatory chunks exist, if not they will be
1411     * created. Note that those chunks will not be made persistent until
1412     * Save() was called.
1413     */
1414     void File::__ensureMandatoryChunksExist() {
1415     // enusre 'lins' list chunk exists (mandatory for instrument definitions)
1416     RIFF::List* lstInstruments = pRIFF->GetSubList(LIST_TYPE_LINS);
1417     if (!lstInstruments) pRIFF->AddSubList(LIST_TYPE_LINS);
1418     // ensure 'ptbl' chunk exists (mandatory for samples)
1419     RIFF::Chunk* ptbl = pRIFF->GetSubChunk(CHUNK_ID_PTBL);
1420     if (!ptbl) {
1421     const int iOffsetSize = (b64BitWavePoolOffsets) ? 8 : 4;
1422     ptbl = pRIFF->AddSubChunk(CHUNK_ID_PTBL, WavePoolHeaderSize + iOffsetSize);
1423     }
1424     // enusre 'wvpl' list chunk exists (mandatory for samples)
1425     RIFF::List* wvpl = pRIFF->GetSubList(LIST_TYPE_WVPL);
1426     if (!wvpl) pRIFF->AddSubList(LIST_TYPE_WVPL);
1427     }
1428    
1429     /**
1430     * Updates (persistently) the wave pool table with offsets to all
1431     * currently available samples. <b>Caution:</b> this method assumes the
1432 schoenebeck 804 * 'ptbl' chunk to be already of the correct size and the file to be
1433     * writable, so usually this method is only called after a Save() call.
1434 schoenebeck 800 *
1435     * @throws Exception - if 'ptbl' chunk is too small (should only occur
1436     * if there's a bug)
1437     */
1438     void File::__UpdateWavePoolTableChunk() {
1439     __UpdateWavePoolTable();
1440     RIFF::Chunk* ptbl = pRIFF->GetSubChunk(CHUNK_ID_PTBL);
1441     const int iOffsetSize = (b64BitWavePoolOffsets) ? 8 : 4;
1442     // check if 'ptbl' chunk is large enough
1443 schoenebeck 802 WavePoolCount = (pSamples) ? pSamples->size() : 0;
1444 schoenebeck 800 const unsigned long ulRequiredSize = WavePoolHeaderSize + iOffsetSize * WavePoolCount;
1445     if (ptbl->GetSize() < ulRequiredSize) throw Exception("Fatal error, 'ptbl' chunk too small");
1446 schoenebeck 804 // save the 'ptbl' chunk's current read/write position
1447     unsigned long ulOriginalPos = ptbl->GetPos();
1448 schoenebeck 800 // update headers
1449 schoenebeck 804 ptbl->SetPos(0);
1450 persson 1179 uint32_t tmp = WavePoolHeaderSize;
1451     ptbl->WriteUint32(&tmp);
1452     tmp = WavePoolCount;
1453     ptbl->WriteUint32(&tmp);
1454 schoenebeck 800 // update offsets
1455 schoenebeck 804 ptbl->SetPos(WavePoolHeaderSize);
1456 schoenebeck 800 if (b64BitWavePoolOffsets) {
1457     for (int i = 0 ; i < WavePoolCount ; i++) {
1458 persson 1179 tmp = pWavePoolTableHi[i];
1459     ptbl->WriteUint32(&tmp);
1460     tmp = pWavePoolTable[i];
1461     ptbl->WriteUint32(&tmp);
1462 schoenebeck 800 }
1463     } else { // conventional 32 bit offsets
1464 persson 1179 for (int i = 0 ; i < WavePoolCount ; i++) {
1465     tmp = pWavePoolTable[i];
1466     ptbl->WriteUint32(&tmp);
1467     }
1468 schoenebeck 800 }
1469 schoenebeck 804 // restore 'ptbl' chunk's original read/write position
1470     ptbl->SetPos(ulOriginalPos);
1471 schoenebeck 800 }
1472    
1473     /**
1474     * Updates the wave pool table with offsets to all currently available
1475     * samples. <b>Caution:</b> this method assumes the 'wvpl' list chunk
1476     * exists already.
1477     */
1478     void File::__UpdateWavePoolTable() {
1479 schoenebeck 802 WavePoolCount = (pSamples) ? pSamples->size() : 0;
1480 schoenebeck 800 // resize wave pool table arrays
1481     if (pWavePoolTable) delete[] pWavePoolTable;
1482     if (pWavePoolTableHi) delete[] pWavePoolTableHi;
1483     pWavePoolTable = new uint32_t[WavePoolCount];
1484     pWavePoolTableHi = new uint32_t[WavePoolCount];
1485 schoenebeck 802 if (!pSamples) return;
1486 schoenebeck 800 // update offsets int wave pool table
1487     RIFF::List* wvpl = pRIFF->GetSubList(LIST_TYPE_WVPL);
1488     uint64_t wvplFileOffset = wvpl->GetFilePos();
1489     if (b64BitWavePoolOffsets) {
1490     SampleList::iterator iter = pSamples->begin();
1491     SampleList::iterator end = pSamples->end();
1492     for (int i = 0 ; iter != end ; ++iter, i++) {
1493 schoenebeck 804 uint64_t _64BitOffset = (*iter)->pWaveList->GetFilePos() - wvplFileOffset - LIST_HEADER_SIZE;
1494 schoenebeck 800 (*iter)->ulWavePoolOffset = _64BitOffset;
1495     pWavePoolTableHi[i] = (uint32_t) (_64BitOffset >> 32);
1496     pWavePoolTable[i] = (uint32_t) _64BitOffset;
1497     }
1498     } else { // conventional 32 bit offsets
1499     SampleList::iterator iter = pSamples->begin();
1500     SampleList::iterator end = pSamples->end();
1501     for (int i = 0 ; iter != end ; ++iter, i++) {
1502 schoenebeck 804 uint64_t _64BitOffset = (*iter)->pWaveList->GetFilePos() - wvplFileOffset - LIST_HEADER_SIZE;
1503 schoenebeck 800 (*iter)->ulWavePoolOffset = _64BitOffset;
1504     pWavePoolTable[i] = (uint32_t) _64BitOffset;
1505     }
1506     }
1507     }
1508    
1509    
1510    
1511 schoenebeck 2 // *************** Exception ***************
1512     // *
1513    
1514     Exception::Exception(String Message) : RIFF::Exception(Message) {
1515     }
1516    
1517     void Exception::PrintMessage() {
1518     std::cout << "DLS::Exception: " << Message << std::endl;
1519     }
1520    
1521 schoenebeck 518
1522     // *************** functions ***************
1523     // *
1524    
1525     /**
1526     * Returns the name of this C++ library. This is usually "libgig" of
1527     * course. This call is equivalent to RIFF::libraryName() and
1528     * gig::libraryName().
1529     */
1530     String libraryName() {
1531     return PACKAGE;
1532     }
1533    
1534     /**
1535     * Returns version of this C++ library. This call is equivalent to
1536     * RIFF::libraryVersion() and gig::libraryVersion().
1537     */
1538     String libraryVersion() {
1539     return VERSION;
1540     }
1541    
1542 schoenebeck 2 } // namespace DLS

  ViewVC Help
Powered by ViewVC