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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1266 - (show annotations) (download)
Thu Aug 2 14:12:55 2007 UTC (16 years, 8 months ago) by persson
File size: 64988 byte(s)
* write support fixes: allow regions/dimension regions without mapped
  samples, 3gnl in v3 files now always has 128 entries, several
  parameters where incorrectly saved due to an operator precedence
  mistake
* DeleteSample now removes all references to the deleted sample

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

  ViewVC Help
Powered by ViewVC