1 |
/*************************************************************************** |
/*************************************************************************** |
2 |
* * |
* * |
3 |
* libgig - C++ cross-platform Gigasampler format file loader library * |
* libgig - C++ cross-platform Gigasampler format file access library * |
4 |
* * |
* * |
5 |
* Copyright (C) 2003-2005 by Christian Schoenebeck * |
* Copyright (C) 2003-2007 by Christian Schoenebeck * |
6 |
* <cuse@users.sourceforge.net> * |
* <cuse@users.sourceforge.net> * |
7 |
* * |
* * |
8 |
* This library is free software; you can redistribute it and/or modify * |
* This library is free software; you can redistribute it and/or modify * |
25 |
|
|
26 |
#include <time.h> |
#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" |
#include "helper.h" |
35 |
|
|
36 |
// macros to decode connection transforms |
// macros to decode connection transforms |
51 |
#define CONN_TRANSFORM_INVERT_SRC_ENCODE(x) ((x) ? 0x8000 : 0) |
#define CONN_TRANSFORM_INVERT_SRC_ENCODE(x) ((x) ? 0x8000 : 0) |
52 |
#define CONN_TRANSFORM_INVERT_CTL_ENCODE(x) ((x) ? 0x0200 : 0) |
#define CONN_TRANSFORM_INVERT_CTL_ENCODE(x) ((x) ? 0x0200 : 0) |
53 |
|
|
54 |
#define DRUM_TYPE_MASK 0x00000001 |
#define DRUM_TYPE_MASK 0x80000000 |
55 |
|
|
56 |
#define F_RGN_OPTION_SELFNONEXCLUSIVE 0x0001 |
#define F_RGN_OPTION_SELFNONEXCLUSIVE 0x0001 |
57 |
|
|
148 |
const int iEntrySize = 12; // 12 bytes per connection block |
const int iEntrySize = 12; // 12 bytes per connection block |
149 |
pArticulationCk->Resize(HeaderSize + Connections * iEntrySize); |
pArticulationCk->Resize(HeaderSize + Connections * iEntrySize); |
150 |
uint8_t* pData = (uint8_t*) pArticulationCk->LoadChunkData(); |
uint8_t* pData = (uint8_t*) pArticulationCk->LoadChunkData(); |
151 |
memccpy(&pData[0], &HeaderSize, 1, 2); |
store16(&pData[0], HeaderSize); |
152 |
memccpy(&pData[2], &Connections, 1, 2); |
store16(&pData[2], Connections); |
153 |
for (uint32_t i = 0; i < Connections; i++) { |
for (uint32_t i = 0; i < Connections; i++) { |
154 |
Connection::conn_block_t c = pConnections[i].ToConnBlock(); |
Connection::conn_block_t c = pConnections[i].ToConnBlock(); |
155 |
memccpy(&pData[HeaderSize + i * iEntrySize], &c.source, 1, 2); |
store16(&pData[HeaderSize + i * iEntrySize], c.source); |
156 |
memccpy(&pData[HeaderSize + i * iEntrySize + 2], &c.control, 1, 2); |
store16(&pData[HeaderSize + i * iEntrySize + 2], c.control); |
157 |
memccpy(&pData[HeaderSize + i * iEntrySize + 4], &c.destination, 1, 2); |
store16(&pData[HeaderSize + i * iEntrySize + 4], c.destination); |
158 |
memccpy(&pData[HeaderSize + i * iEntrySize + 6], &c.transform, 1, 2); |
store16(&pData[HeaderSize + i * iEntrySize + 6], c.transform); |
159 |
memccpy(&pData[HeaderSize + i * iEntrySize + 8], &c.scale, 1, 4); |
store32(&pData[HeaderSize + i * iEntrySize + 8], c.scale); |
160 |
} |
} |
161 |
} |
} |
162 |
|
|
234 |
|
|
235 |
/** @brief Constructor. |
/** @brief Constructor. |
236 |
* |
* |
237 |
* Initializes the info strings with values provided by a INFO list chunk. |
* Initializes the info strings with values provided by an INFO list chunk. |
238 |
* |
* |
239 |
* @param list - pointer to a list chunk which contains a INFO list chunk |
* @param list - pointer to a list chunk which contains an INFO list chunk |
240 |
*/ |
*/ |
241 |
Info::Info(RIFF::List* list) { |
Info::Info(RIFF::List* list) { |
242 |
|
FixedStringLengths = NULL; |
243 |
pResourceListChunk = list; |
pResourceListChunk = list; |
244 |
if (list) { |
if (list) { |
245 |
RIFF::List* lstINFO = list->GetSubList(LIST_TYPE_INFO); |
RIFF::List* lstINFO = list->GetSubList(LIST_TYPE_INFO); |
260 |
LoadString(CHUNK_ID_ISRC, lstINFO, Source); |
LoadString(CHUNK_ID_ISRC, lstINFO, Source); |
261 |
LoadString(CHUNK_ID_ISRF, lstINFO, SourceForm); |
LoadString(CHUNK_ID_ISRF, lstINFO, SourceForm); |
262 |
LoadString(CHUNK_ID_ICMS, lstINFO, Commissioned); |
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. |
/** @brief Load given INFO field. |
272 |
* |
* |
273 |
* Load INFO field from INFO chunk with chunk ID \a ChunkID from INFO |
* Load INFO field from INFO chunk with chunk ID \a ChunkID from INFO |
275 |
*/ |
*/ |
276 |
void Info::LoadString(uint32_t ChunkID, RIFF::List* lstINFO, String& s) { |
void Info::LoadString(uint32_t ChunkID, RIFF::List* lstINFO, String& s) { |
277 |
RIFF::Chunk* ck = lstINFO->GetSubChunk(ChunkID); |
RIFF::Chunk* ck = lstINFO->GetSubChunk(ChunkID); |
278 |
if (ck) { |
::LoadString(ck, s); // function from helper.h |
|
// TODO: no check for ZSTR terminated strings yet |
|
|
s = (char*) ck->LoadChunkData(); |
|
|
ck->ReleaseChunkData(); |
|
|
} |
|
279 |
} |
} |
280 |
|
|
281 |
/** @brief Apply given INFO field to the respective chunk. |
/** @brief Apply given INFO field to the respective chunk. |
294 |
* @param sDefault - default value |
* @param sDefault - default value |
295 |
*/ |
*/ |
296 |
void Info::SaveString(uint32_t ChunkID, RIFF::List* lstINFO, const String& s, const String& sDefault) { |
void Info::SaveString(uint32_t ChunkID, RIFF::List* lstINFO, const String& s, const String& sDefault) { |
297 |
RIFF::Chunk* ck = lstINFO->GetSubChunk(ChunkID); |
int size = 0; |
298 |
if (ck) { // if chunk exists already, use 's' as value |
if (FixedStringLengths) { |
299 |
ck->Resize(s.size() + 1); |
for (int i = 0 ; FixedStringLengths[i].length ; i++) { |
300 |
char* pData = (char*) ck->LoadChunkData(); |
if (FixedStringLengths[i].chunkId == ChunkID) { |
301 |
memcpy(pData, s.c_str(), s.size() + 1); |
size = FixedStringLengths[i].length; |
302 |
} else if (s != "" || sDefault != "") { // create chunk |
break; |
303 |
const String& sToSave = (s != "") ? s : sDefault; |
} |
304 |
ck = lstINFO->AddSubChunk(ChunkID, sToSave.size() + 1); |
} |
|
char* pData = (char*) ck->LoadChunkData(); |
|
|
memcpy(pData, sToSave.c_str(), sToSave.size() + 1); |
|
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. |
/** @brief Update chunks with current info values. |
317 |
|
|
318 |
// make sure INFO list chunk exists |
// make sure INFO list chunk exists |
319 |
RIFF::List* lstINFO = pResourceListChunk->GetSubList(LIST_TYPE_INFO); |
RIFF::List* lstINFO = pResourceListChunk->GetSubList(LIST_TYPE_INFO); |
|
if (!lstINFO) lstINFO = pResourceListChunk->AddSubList(LIST_TYPE_INFO); |
|
320 |
|
|
321 |
// assemble default values in case the respective chunk is missing yet |
String defaultName = ""; |
322 |
String defaultName = "NONAME"; |
String defaultCreationDate = ""; |
323 |
// get current date |
String defaultSoftware = ""; |
324 |
time_t now = time(NULL); |
String defaultComments = ""; |
325 |
tm* pNowBroken = localtime(&now); |
|
326 |
String defaultCreationDate = ToString(1900 + pNowBroken->tm_year) + "-" + |
uint32_t resourceType = pResourceListChunk->GetListType(); |
327 |
ToString(pNowBroken->tm_mon + 1) + "-" + |
|
328 |
ToString(pNowBroken->tm_mday); |
if (!lstINFO) { |
329 |
String defaultSoftware = libraryName() + " " + libraryVersion(); |
lstINFO = pResourceListChunk->AddSubList(LIST_TYPE_INFO); |
330 |
String defaultComments = "Created with " + libraryName() + " " + libraryVersion(); |
|
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 |
// save values |
351 |
SaveString(CHUNK_ID_INAM, lstINFO, Name, defaultName); |
|
352 |
SaveString(CHUNK_ID_IARL, lstINFO, ArchivalLocation, String("")); |
SaveString(CHUNK_ID_IARL, lstINFO, ArchivalLocation, String("")); |
353 |
SaveString(CHUNK_ID_ICRD, lstINFO, CreationDate, defaultCreationDate); |
SaveString(CHUNK_ID_IART, lstINFO, Artists, String("")); |
354 |
|
SaveString(CHUNK_ID_ICMS, lstINFO, Commissioned, String("")); |
355 |
SaveString(CHUNK_ID_ICMT, lstINFO, Comments, defaultComments); |
SaveString(CHUNK_ID_ICMT, lstINFO, Comments, defaultComments); |
|
SaveString(CHUNK_ID_IPRD, lstINFO, Product, String("")); |
|
356 |
SaveString(CHUNK_ID_ICOP, lstINFO, Copyright, String("")); |
SaveString(CHUNK_ID_ICOP, lstINFO, Copyright, String("")); |
357 |
SaveString(CHUNK_ID_IART, lstINFO, Artists, String("")); |
SaveString(CHUNK_ID_ICRD, lstINFO, CreationDate, defaultCreationDate); |
358 |
|
SaveString(CHUNK_ID_IENG, lstINFO, Engineer, String("")); |
359 |
SaveString(CHUNK_ID_IGNR, lstINFO, Genre, String("")); |
SaveString(CHUNK_ID_IGNR, lstINFO, Genre, String("")); |
360 |
SaveString(CHUNK_ID_IKEY, lstINFO, Keywords, String("")); |
SaveString(CHUNK_ID_IKEY, lstINFO, Keywords, String("")); |
|
SaveString(CHUNK_ID_IENG, lstINFO, Engineer, String("")); |
|
|
SaveString(CHUNK_ID_ITCH, lstINFO, Technician, String("")); |
|
|
SaveString(CHUNK_ID_ISFT, lstINFO, Software, defaultSoftware); |
|
361 |
SaveString(CHUNK_ID_IMED, lstINFO, Medium, String("")); |
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("")); |
SaveString(CHUNK_ID_ISRC, lstINFO, Source, String("")); |
367 |
SaveString(CHUNK_ID_ISRF, lstINFO, SourceForm, String("")); |
SaveString(CHUNK_ID_ISRF, lstINFO, SourceForm, String("")); |
368 |
SaveString(CHUNK_ID_ICMS, lstINFO, Commissioned, String("")); |
SaveString(CHUNK_ID_ITCH, lstINFO, Technician, String("")); |
369 |
} |
} |
370 |
|
|
371 |
|
|
414 |
*/ |
*/ |
415 |
void Resource::UpdateChunks() { |
void Resource::UpdateChunks() { |
416 |
pInfo->UpdateChunks(); |
pInfo->UpdateChunks(); |
417 |
//TODO: save DLSID |
|
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 *************** |
// *************** Sampler *************** |
488 |
SampleLoops = wsmp->ReadUint32(); |
SampleLoops = wsmp->ReadUint32(); |
489 |
} else { // 'wsmp' chunk missing |
} else { // 'wsmp' chunk missing |
490 |
uiHeaderSize = 0; |
uiHeaderSize = 0; |
491 |
UnityNote = 64; |
UnityNote = 60; |
492 |
FineTune = 0; // +- 0 cents |
FineTune = 0; // +- 0 cents |
493 |
Gain = 0; // 0 dB |
Gain = 0; // 0 dB |
494 |
SamplerOptions = F_WSMP_NO_COMPRESSION; |
SamplerOptions = F_WSMP_NO_COMPRESSION; |
525 |
} |
} |
526 |
uint8_t* pData = (uint8_t*) wsmp->LoadChunkData(); |
uint8_t* pData = (uint8_t*) wsmp->LoadChunkData(); |
527 |
// update headers size |
// update headers size |
528 |
memccpy(&pData[0], &uiHeaderSize, 1, 4); |
store32(&pData[0], uiHeaderSize); |
529 |
// update respective sampler options bits |
// update respective sampler options bits |
530 |
SamplerOptions = (NoSampleDepthTruncation) ? SamplerOptions | F_WSMP_NO_TRUNCATION |
SamplerOptions = (NoSampleDepthTruncation) ? SamplerOptions | F_WSMP_NO_TRUNCATION |
531 |
: SamplerOptions & (~F_WSMP_NO_TRUNCATION); |
: SamplerOptions & (~F_WSMP_NO_TRUNCATION); |
532 |
SamplerOptions = (NoSampleCompression) ? SamplerOptions | F_WSMP_NO_COMPRESSION |
SamplerOptions = (NoSampleCompression) ? SamplerOptions | F_WSMP_NO_COMPRESSION |
533 |
: SamplerOptions & (~F_WSMP_NO_COMPRESSION); |
: 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 |
// update loop definitions |
540 |
for (uint32_t i = 0; i < SampleLoops; i++) { |
for (uint32_t i = 0; i < SampleLoops; i++) { |
541 |
//FIXME: this does not handle extended loop structs correctly |
//FIXME: this does not handle extended loop structs correctly |
542 |
memccpy(&pData[uiHeaderSize + i * 16], pSampleLoops + i, 4, 4); |
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 *************** |
// *************** Sample *************** |
622 |
AverageBytesPerSecond = pCkFormat->ReadUint32(); |
AverageBytesPerSecond = pCkFormat->ReadUint32(); |
623 |
BlockAlign = pCkFormat->ReadUint16(); |
BlockAlign = pCkFormat->ReadUint16(); |
624 |
// PCM format specific |
// PCM format specific |
625 |
if (FormatTag == WAVE_FORMAT_PCM) { |
if (FormatTag == DLS_WAVE_FORMAT_PCM) { |
626 |
BitDepth = pCkFormat->ReadUint16(); |
BitDepth = pCkFormat->ReadUint16(); |
627 |
FrameSize = (FormatTag == WAVE_FORMAT_PCM) ? (BitDepth / 8) * Channels |
FrameSize = (BitDepth / 8) * Channels; |
|
: 0; |
|
628 |
} else { // unsupported sample data format |
} else { // unsupported sample data format |
629 |
BitDepth = 0; |
BitDepth = 0; |
630 |
FrameSize = 0; |
FrameSize = 0; |
631 |
} |
} |
632 |
} else { // 'fmt' chunk missing |
} else { // 'fmt' chunk missing |
633 |
FormatTag = WAVE_FORMAT_PCM; |
FormatTag = DLS_WAVE_FORMAT_PCM; |
634 |
BitDepth = 16; |
BitDepth = 16; |
635 |
Channels = 1; |
Channels = 1; |
636 |
SamplesPerSecond = 44100; |
SamplesPerSecond = 44100; |
638 |
FrameSize = (BitDepth / 8) * Channels; |
FrameSize = (BitDepth / 8) * Channels; |
639 |
BlockAlign = FrameSize; |
BlockAlign = FrameSize; |
640 |
} |
} |
641 |
SamplesTotal = (pCkData) ? (FormatTag == WAVE_FORMAT_PCM) ? pCkData->GetSize() / FrameSize |
SamplesTotal = (pCkData) ? (FormatTag == DLS_WAVE_FORMAT_PCM) ? pCkData->GetSize() / FrameSize |
642 |
: 0 |
: 0 |
643 |
: 0; |
: 0; |
644 |
} |
} |
645 |
|
|
699 |
* the RIFF chunk which encapsulates the sample's wave data. The |
* the RIFF chunk which encapsulates the sample's wave data. The |
700 |
* returned value is dependant to the current FrameSize value. |
* returned value is dependant to the current FrameSize value. |
701 |
* |
* |
702 |
* @returns number of sample points or 0 if FormatTag != WAVE_FORMAT_PCM |
* @returns number of sample points or 0 if FormatTag != DLS_WAVE_FORMAT_PCM |
703 |
* @see FrameSize, FormatTag |
* @see FrameSize, FormatTag |
704 |
*/ |
*/ |
705 |
unsigned long Sample::GetSize() { |
unsigned long Sample::GetSize() { |
706 |
if (FormatTag != WAVE_FORMAT_PCM) return 0; |
if (FormatTag != DLS_WAVE_FORMAT_PCM) return 0; |
707 |
return (pCkData) ? pCkData->GetSize() / FrameSize : 0; |
return (pCkData) ? pCkData->GetSize() / FrameSize : 0; |
708 |
} |
} |
709 |
|
|
725 |
* calling File::Save() as this might exceed the current sample's |
* calling File::Save() as this might exceed the current sample's |
726 |
* boundary! |
* boundary! |
727 |
* |
* |
728 |
* Also note: only WAVE_FORMAT_PCM is currently supported, that is |
* Also note: only DLS_WAVE_FORMAT_PCM is currently supported, that is |
729 |
* FormatTag must be WAVE_FORMAT_PCM. Trying to resize samples with |
* FormatTag must be DLS_WAVE_FORMAT_PCM. Trying to resize samples with |
730 |
* other formats will fail! |
* other formats will fail! |
731 |
* |
* |
732 |
* @param iNewSize - new sample wave data size in sample points (must be |
* @param iNewSize - new sample wave data size in sample points (must be |
733 |
* greater than zero) |
* greater than zero) |
734 |
* @throws Excecption if FormatTag != WAVE_FORMAT_PCM |
* @throws Excecption if FormatTag != DLS_WAVE_FORMAT_PCM |
735 |
* @throws Exception if \a iNewSize is less than 1 |
* @throws Exception if \a iNewSize is less than 1 |
736 |
* @see File::Save(), FrameSize, FormatTag |
* @see File::Save(), FrameSize, FormatTag |
737 |
*/ |
*/ |
738 |
void Sample::Resize(int iNewSize) { |
void Sample::Resize(int iNewSize) { |
739 |
if (FormatTag != WAVE_FORMAT_PCM) throw Exception("Sample's format is not WAVE_FORMAT_PCM"); |
if (FormatTag != DLS_WAVE_FORMAT_PCM) throw Exception("Sample's format is not DLS_WAVE_FORMAT_PCM"); |
740 |
if (iNewSize < 1) throw Exception("Sample size must be at least one sample point"); |
if (iNewSize < 1) throw Exception("Sample size must be at least one sample point"); |
741 |
const int iSizeInBytes = iNewSize * FrameSize; |
const int iSizeInBytes = iNewSize * FrameSize; |
742 |
pCkData = pWaveList->GetSubChunk(CHUNK_ID_DATA); |
pCkData = pWaveList->GetSubChunk(CHUNK_ID_DATA); |
749 |
* bytes). Use this method and <i>Read()</i> if you don't want to load |
* bytes). Use this method and <i>Read()</i> if you don't want to load |
750 |
* the sample into RAM, thus for disk streaming. |
* the sample into RAM, thus for disk streaming. |
751 |
* |
* |
752 |
* Also note: only WAVE_FORMAT_PCM is currently supported, that is |
* Also note: only DLS_WAVE_FORMAT_PCM is currently supported, that is |
753 |
* FormatTag must be WAVE_FORMAT_PCM. Trying to reposition the sample |
* FormatTag must be DLS_WAVE_FORMAT_PCM. Trying to reposition the sample |
754 |
* with other formats will fail! |
* with other formats will fail! |
755 |
* |
* |
756 |
* @param SampleCount number of sample points |
* @param SampleCount number of sample points |
757 |
* @param Whence to which relation \a SampleCount refers to |
* @param Whence to which relation \a SampleCount refers to |
758 |
* @returns new position within the sample, 0 if |
* @returns new position within the sample, 0 if |
759 |
* FormatTag != WAVE_FORMAT_PCM |
* FormatTag != DLS_WAVE_FORMAT_PCM |
760 |
* @throws Exception if no data RIFF chunk was created for the sample yet |
* @throws Exception if no data RIFF chunk was created for the sample yet |
761 |
* @see FrameSize, FormatTag |
* @see FrameSize, FormatTag |
762 |
*/ |
*/ |
763 |
unsigned long Sample::SetPos(unsigned long SampleCount, RIFF::stream_whence_t Whence) { |
unsigned long Sample::SetPos(unsigned long SampleCount, RIFF::stream_whence_t Whence) { |
764 |
if (FormatTag != WAVE_FORMAT_PCM) return 0; // failed: wave data not PCM format |
if (FormatTag != DLS_WAVE_FORMAT_PCM) return 0; // failed: wave data not PCM format |
765 |
if (!pCkData) throw Exception("No data chunk created for sample yet, call Sample::Resize() to create one"); |
if (!pCkData) throw Exception("No data chunk created for sample yet, call Sample::Resize() to create one"); |
766 |
unsigned long orderedBytes = SampleCount * FrameSize; |
unsigned long orderedBytes = SampleCount * FrameSize; |
767 |
unsigned long result = pCkData->SetPos(orderedBytes, Whence); |
unsigned long result = pCkData->SetPos(orderedBytes, Whence); |
779 |
* @param SampleCount number of sample points to read |
* @param SampleCount number of sample points to read |
780 |
*/ |
*/ |
781 |
unsigned long Sample::Read(void* pBuffer, unsigned long SampleCount) { |
unsigned long Sample::Read(void* pBuffer, unsigned long SampleCount) { |
782 |
if (FormatTag != WAVE_FORMAT_PCM) return 0; // failed: wave data not PCM format |
if (FormatTag != DLS_WAVE_FORMAT_PCM) return 0; // failed: wave data not PCM format |
783 |
return pCkData->Read(pBuffer, SampleCount, FrameSize); // FIXME: channel inversion due to endian correction? |
return pCkData->Read(pBuffer, SampleCount, FrameSize); // FIXME: channel inversion due to endian correction? |
784 |
} |
} |
785 |
|
|
799 |
* @see LoadSampleData() |
* @see LoadSampleData() |
800 |
*/ |
*/ |
801 |
unsigned long Sample::Write(void* pBuffer, unsigned long SampleCount) { |
unsigned long Sample::Write(void* pBuffer, unsigned long SampleCount) { |
802 |
if (FormatTag != WAVE_FORMAT_PCM) return 0; // failed: wave data not PCM format |
if (FormatTag != DLS_WAVE_FORMAT_PCM) return 0; // failed: wave data not PCM format |
803 |
if (GetSize() < SampleCount) throw Exception("Could not write sample data, current sample size to small"); |
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? |
return pCkData->Write(pBuffer, SampleCount, FrameSize); // FIXME: channel inversion due to endian correction? |
805 |
} |
} |
808 |
* Apply sample and its settings to the respective RIFF chunks. You have |
* Apply sample and its settings to the respective RIFF chunks. You have |
809 |
* to call File::Save() to make changes persistent. |
* to call File::Save() to make changes persistent. |
810 |
* |
* |
811 |
* @throws Exception if FormatTag != WAVE_FORMAT_PCM or no sample data |
* @throws Exception if FormatTag != DLS_WAVE_FORMAT_PCM or no sample data |
812 |
* was provided yet |
* was provided yet |
813 |
*/ |
*/ |
814 |
void Sample::UpdateChunks() { |
void Sample::UpdateChunks() { |
815 |
if (FormatTag != WAVE_FORMAT_PCM) |
if (FormatTag != DLS_WAVE_FORMAT_PCM) |
816 |
throw Exception("Could not save sample, only PCM format is supported"); |
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 |
// we refuse to do anything if not sample wave form was provided yet |
818 |
if (!pCkData) |
if (!pCkData) |
824 |
if (!pCkFormat) pCkFormat = pWaveList->AddSubChunk(CHUNK_ID_FMT, 16); // assumes PCM format |
if (!pCkFormat) pCkFormat = pWaveList->AddSubChunk(CHUNK_ID_FMT, 16); // assumes PCM format |
825 |
uint8_t* pData = (uint8_t*) pCkFormat->LoadChunkData(); |
uint8_t* pData = (uint8_t*) pCkFormat->LoadChunkData(); |
826 |
// update 'fmt' chunk |
// update 'fmt' chunk |
827 |
memccpy(&pData[0], &FormatTag, 1, 2); |
store16(&pData[0], FormatTag); |
828 |
memccpy(&pData[2], &Channels, 1, 2); |
store16(&pData[2], Channels); |
829 |
memccpy(&pData[4], &SamplesPerSecond, 1, 4); |
store32(&pData[4], SamplesPerSecond); |
830 |
memccpy(&pData[8], &AverageBytesPerSecond, 1, 4); |
store32(&pData[8], AverageBytesPerSecond); |
831 |
memccpy(&pData[12], &BlockAlign, 1, 2); |
store16(&pData[12], BlockAlign); |
832 |
memccpy(&pData[14], &BitDepth, 1, 2); // assuming PCM format |
store16(&pData[14], BitDepth); // assuming PCM format |
833 |
} |
} |
834 |
|
|
835 |
|
|
921 |
void Region::UpdateChunks() { |
void Region::UpdateChunks() { |
922 |
// make sure 'rgnh' chunk exists |
// make sure 'rgnh' chunk exists |
923 |
RIFF::Chunk* rgnh = pCkRegion->GetSubChunk(CHUNK_ID_RGNH); |
RIFF::Chunk* rgnh = pCkRegion->GetSubChunk(CHUNK_ID_RGNH); |
924 |
if (!rgnh) rgnh = pCkRegion->AddSubChunk(CHUNK_ID_RGNH, 14); |
if (!rgnh) rgnh = pCkRegion->AddSubChunk(CHUNK_ID_RGNH, Layer ? 14 : 12); |
925 |
uint8_t* pData = (uint8_t*) rgnh->LoadChunkData(); |
uint8_t* pData = (uint8_t*) rgnh->LoadChunkData(); |
926 |
FormatOptionFlags = (SelfNonExclusive) |
FormatOptionFlags = (SelfNonExclusive) |
927 |
? FormatOptionFlags | F_RGN_OPTION_SELFNONEXCLUSIVE |
? FormatOptionFlags | F_RGN_OPTION_SELFNONEXCLUSIVE |
928 |
: FormatOptionFlags & (~F_RGN_OPTION_SELFNONEXCLUSIVE); |
: FormatOptionFlags & (~F_RGN_OPTION_SELFNONEXCLUSIVE); |
929 |
// update 'rgnh' chunk |
// update 'rgnh' chunk |
930 |
memccpy(&pData[0], &KeyRange, 2, 2); |
store16(&pData[0], KeyRange.low); |
931 |
memccpy(&pData[4], &VelocityRange, 2, 2); |
store16(&pData[2], KeyRange.high); |
932 |
memccpy(&pData[8], &FormatOptionFlags, 1, 2); |
store16(&pData[4], VelocityRange.low); |
933 |
memccpy(&pData[10], &KeyGroup, 1, 2); |
store16(&pData[6], VelocityRange.high); |
934 |
memccpy(&pData[12], &Layer, 1, 2); |
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 |
// update chunks of base classes as well (but skip Resource, |
939 |
Resource::UpdateChunks(); |
// as a rgn doesn't seem to have dlid and INFO chunks) |
940 |
Articulator::UpdateChunks(); |
Articulator::UpdateChunks(); |
941 |
Sampler::UpdateChunks(); |
Sampler::UpdateChunks(); |
942 |
|
|
966 |
if (index < 0) throw Exception("Could not save Region, could not find Region's sample"); |
if (index < 0) throw Exception("Could not save Region, could not find Region's sample"); |
967 |
WavePoolTableIndex = index; |
WavePoolTableIndex = index; |
968 |
// update 'wlnk' chunk |
// update 'wlnk' chunk |
969 |
memccpy(&pData[0], &WaveLinkOptionFlags, 1, 2); |
store16(&pData[0], WaveLinkOptionFlags); |
970 |
memccpy(&pData[2], &PhaseGroup, 1, 2); |
store16(&pData[2], PhaseGroup); |
971 |
memccpy(&pData[4], &Channel, 1, 4); |
store32(&pData[4], Channel); |
972 |
memccpy(&pData[8], &WavePoolTableIndex, 1, 4); |
store32(&pData[8], WavePoolTableIndex); |
973 |
} |
} |
974 |
|
|
975 |
|
|
1027 |
} |
} |
1028 |
|
|
1029 |
void Instrument::LoadRegions() { |
void Instrument::LoadRegions() { |
1030 |
|
if (!pRegions) pRegions = new RegionList; |
1031 |
RIFF::List* lrgn = pCkInstrument->GetSubList(LIST_TYPE_LRGN); |
RIFF::List* lrgn = pCkInstrument->GetSubList(LIST_TYPE_LRGN); |
1032 |
if (!lrgn) throw DLS::Exception("DLS::Instrument doesn't seem to have any Region (mandatory chunks in <ins > chunk not found)"); |
if (lrgn) { |
1033 |
uint32_t regionCkType = (lrgn->GetSubList(LIST_TYPE_RGN2)) ? LIST_TYPE_RGN2 : LIST_TYPE_RGN; // prefer regions level 2 |
uint32_t regionCkType = (lrgn->GetSubList(LIST_TYPE_RGN2)) ? LIST_TYPE_RGN2 : LIST_TYPE_RGN; // prefer regions level 2 |
1034 |
RIFF::List* rgn = lrgn->GetFirstSubList(); |
RIFF::List* rgn = lrgn->GetFirstSubList(); |
1035 |
while (rgn) { |
while (rgn) { |
1036 |
if (rgn->GetListType() == regionCkType) { |
if (rgn->GetListType() == regionCkType) { |
1037 |
if (!pRegions) pRegions = new RegionList; |
pRegions->push_back(new Region(this, rgn)); |
1038 |
pRegions->push_back(new Region(this, rgn)); |
} |
1039 |
|
rgn = lrgn->GetNextSubList(); |
1040 |
} |
} |
|
rgn = lrgn->GetNextSubList(); |
|
1041 |
} |
} |
1042 |
} |
} |
1043 |
|
|
1044 |
Region* Instrument::AddRegion() { |
Region* Instrument::AddRegion() { |
1045 |
if (!pRegions) pRegions = new RegionList; |
if (!pRegions) LoadRegions(); |
1046 |
RIFF::List* lrgn = pCkInstrument->GetSubList(LIST_TYPE_LRGN); |
RIFF::List* lrgn = pCkInstrument->GetSubList(LIST_TYPE_LRGN); |
1047 |
if (!lrgn) lrgn = pCkInstrument->AddSubList(LIST_TYPE_LRGN); |
if (!lrgn) lrgn = pCkInstrument->AddSubList(LIST_TYPE_LRGN); |
1048 |
RIFF::List* rgn = lrgn->AddSubList(LIST_TYPE_RGN); |
RIFF::List* rgn = lrgn->AddSubList(LIST_TYPE_RGN); |
1049 |
Region* pNewRegion = new Region(this, rgn); |
Region* pNewRegion = new Region(this, rgn); |
1050 |
pRegions->push_back(pNewRegion); |
pRegions->push_back(pNewRegion); |
1051 |
|
Regions = pRegions->size(); |
1052 |
return pNewRegion; |
return pNewRegion; |
1053 |
} |
} |
1054 |
|
|
1055 |
|
void Instrument::MoveRegion(Region* pSrc, Region* pDst) { |
1056 |
|
RIFF::List* lrgn = pCkInstrument->GetSubList(LIST_TYPE_LRGN); |
1057 |
|
lrgn->MoveSubChunk(pSrc->pCkRegion, pDst ? pDst->pCkRegion : 0); |
1058 |
|
|
1059 |
|
pRegions->remove(pSrc); |
1060 |
|
RegionList::iterator iter = find(pRegions->begin(), pRegions->end(), pDst); |
1061 |
|
pRegions->insert(iter, pSrc); |
1062 |
|
} |
1063 |
|
|
1064 |
void Instrument::DeleteRegion(Region* pRegion) { |
void Instrument::DeleteRegion(Region* pRegion) { |
1065 |
if (!pRegions) return; |
if (!pRegions) return; |
1066 |
RegionList::iterator iter = find(pRegions->begin(), pRegions->end(), pRegion); |
RegionList::iterator iter = find(pRegions->begin(), pRegions->end(), pRegion); |
1067 |
if (iter == pRegions->end()) return; |
if (iter == pRegions->end()) return; |
1068 |
pRegions->erase(iter); |
pRegions->erase(iter); |
1069 |
|
Regions = pRegions->size(); |
1070 |
delete pRegion; |
delete pRegion; |
1071 |
} |
} |
1072 |
|
|
1091 |
locale.bank = MIDI_BANK_ENCODE(MIDIBankCoarse, MIDIBankFine); |
locale.bank = MIDI_BANK_ENCODE(MIDIBankCoarse, MIDIBankFine); |
1092 |
locale.bank = (IsDrum) ? locale.bank | DRUM_TYPE_MASK : locale.bank & (~DRUM_TYPE_MASK); |
locale.bank = (IsDrum) ? locale.bank | DRUM_TYPE_MASK : locale.bank & (~DRUM_TYPE_MASK); |
1093 |
MIDIBank = MIDI_BANK_MERGE(MIDIBankCoarse, MIDIBankFine); // just a sync, when we're at it |
MIDIBank = MIDI_BANK_MERGE(MIDIBankCoarse, MIDIBankFine); // just a sync, when we're at it |
1094 |
memccpy(&pData[0], &Regions, 1, 4); |
store32(&pData[0], Regions); |
1095 |
memccpy(&pData[4], &locale, 2, 4); |
store32(&pData[4], locale.bank); |
1096 |
|
store32(&pData[8], locale.instrument); |
1097 |
// update Region's chunks |
// update Region's chunks |
1098 |
if (!pRegions) return; |
if (!pRegions) return; |
1099 |
RegionList::iterator iter = pRegions->begin(); |
RegionList::iterator iter = pRegions->begin(); |
1135 |
* a DLS file. |
* a DLS file. |
1136 |
*/ |
*/ |
1137 |
File::File() : Resource(NULL, pRIFF = new RIFF::File(RIFF_TYPE_DLS)) { |
File::File() : Resource(NULL, pRIFF = new RIFF::File(RIFF_TYPE_DLS)) { |
1138 |
|
pRIFF->SetByteOrder(RIFF::endian_little); |
1139 |
pVersion = new version_t; |
pVersion = new version_t; |
1140 |
pVersion->major = 0; |
pVersion->major = 0; |
1141 |
pVersion->minor = 0; |
pVersion->minor = 0; |
1179 |
Instruments = colh->ReadUint32(); |
Instruments = colh->ReadUint32(); |
1180 |
|
|
1181 |
RIFF::Chunk* ptbl = pRIFF->GetSubChunk(CHUNK_ID_PTBL); |
RIFF::Chunk* ptbl = pRIFF->GetSubChunk(CHUNK_ID_PTBL); |
1182 |
if (!ptbl) throw DLS::Exception("Mandatory <ptbl> chunk not found."); |
if (!ptbl) { // pool table is missing - this is probably an ".art" file |
1183 |
WavePoolHeaderSize = ptbl->ReadUint32(); |
WavePoolCount = 0; |
1184 |
WavePoolCount = ptbl->ReadUint32(); |
pWavePoolTable = NULL; |
1185 |
pWavePoolTable = new uint32_t[WavePoolCount]; |
pWavePoolTableHi = NULL; |
1186 |
pWavePoolTableHi = new uint32_t[WavePoolCount]; |
WavePoolHeaderSize = 8; |
1187 |
ptbl->SetPos(WavePoolHeaderSize); |
b64BitWavePoolOffsets = false; |
1188 |
|
} else { |
1189 |
// Check for 64 bit offsets (used in gig v3 files) |
WavePoolHeaderSize = ptbl->ReadUint32(); |
1190 |
b64BitWavePoolOffsets = (ptbl->GetSize() - WavePoolHeaderSize == WavePoolCount * 8); |
WavePoolCount = ptbl->ReadUint32(); |
1191 |
if (b64BitWavePoolOffsets) { |
pWavePoolTable = new uint32_t[WavePoolCount]; |
1192 |
for (int i = 0 ; i < WavePoolCount ; i++) { |
pWavePoolTableHi = new uint32_t[WavePoolCount]; |
1193 |
pWavePoolTableHi[i] = ptbl->ReadUint32(); |
ptbl->SetPos(WavePoolHeaderSize); |
1194 |
pWavePoolTable[i] = ptbl->ReadUint32(); |
|
1195 |
if (pWavePoolTable[i] & 0x80000000) |
// Check for 64 bit offsets (used in gig v3 files) |
1196 |
throw DLS::Exception("Files larger than 2 GB not yet supported"); |
b64BitWavePoolOffsets = (ptbl->GetSize() - WavePoolHeaderSize == WavePoolCount * 8); |
1197 |
|
if (b64BitWavePoolOffsets) { |
1198 |
|
for (int i = 0 ; i < WavePoolCount ; i++) { |
1199 |
|
pWavePoolTableHi[i] = ptbl->ReadUint32(); |
1200 |
|
pWavePoolTable[i] = ptbl->ReadUint32(); |
1201 |
|
if (pWavePoolTable[i] & 0x80000000) |
1202 |
|
throw DLS::Exception("Files larger than 2 GB not yet supported"); |
1203 |
|
} |
1204 |
|
} else { // conventional 32 bit offsets |
1205 |
|
ptbl->Read(pWavePoolTable, WavePoolCount, sizeof(uint32_t)); |
1206 |
|
for (int i = 0 ; i < WavePoolCount ; i++) pWavePoolTableHi[i] = 0; |
1207 |
} |
} |
|
} else { // conventional 32 bit offsets |
|
|
ptbl->Read(pWavePoolTable, WavePoolCount, sizeof(uint32_t)); |
|
|
for (int i = 0 ; i < WavePoolCount ; i++) pWavePoolTableHi[i] = 0; |
|
1208 |
} |
} |
1209 |
|
|
1210 |
pSamples = NULL; |
pSamples = NULL; |
1235 |
if (pWavePoolTable) delete[] pWavePoolTable; |
if (pWavePoolTable) delete[] pWavePoolTable; |
1236 |
if (pWavePoolTableHi) delete[] pWavePoolTableHi; |
if (pWavePoolTableHi) delete[] pWavePoolTableHi; |
1237 |
if (pVersion) delete pVersion; |
if (pVersion) delete pVersion; |
1238 |
|
for (std::list<RIFF::File*>::iterator i = ExtensionFiles.begin() ; i != ExtensionFiles.end() ; i++) |
1239 |
|
delete *i; |
1240 |
} |
} |
1241 |
|
|
1242 |
Sample* File::GetFirstSample() { |
Sample* File::GetFirstSample() { |
1253 |
} |
} |
1254 |
|
|
1255 |
void File::LoadSamples() { |
void File::LoadSamples() { |
1256 |
|
if (!pSamples) pSamples = new SampleList; |
1257 |
RIFF::List* wvpl = pRIFF->GetSubList(LIST_TYPE_WVPL); |
RIFF::List* wvpl = pRIFF->GetSubList(LIST_TYPE_WVPL); |
1258 |
if (wvpl) { |
if (wvpl) { |
1259 |
unsigned long wvplFileOffset = wvpl->GetFilePos(); |
unsigned long wvplFileOffset = wvpl->GetFilePos(); |
1260 |
RIFF::List* wave = wvpl->GetFirstSubList(); |
RIFF::List* wave = wvpl->GetFirstSubList(); |
1261 |
while (wave) { |
while (wave) { |
1262 |
if (wave->GetListType() == LIST_TYPE_WAVE) { |
if (wave->GetListType() == LIST_TYPE_WAVE) { |
|
if (!pSamples) pSamples = new SampleList; |
|
1263 |
unsigned long waveFileOffset = wave->GetFilePos(); |
unsigned long waveFileOffset = wave->GetFilePos(); |
1264 |
pSamples->push_back(new Sample(this, wave, waveFileOffset - wvplFileOffset)); |
pSamples->push_back(new Sample(this, wave, waveFileOffset - wvplFileOffset)); |
1265 |
} |
} |
1273 |
RIFF::List* wave = dwpl->GetFirstSubList(); |
RIFF::List* wave = dwpl->GetFirstSubList(); |
1274 |
while (wave) { |
while (wave) { |
1275 |
if (wave->GetListType() == LIST_TYPE_WAVE) { |
if (wave->GetListType() == LIST_TYPE_WAVE) { |
|
if (!pSamples) pSamples = new SampleList; |
|
1276 |
unsigned long waveFileOffset = wave->GetFilePos(); |
unsigned long waveFileOffset = wave->GetFilePos(); |
1277 |
pSamples->push_back(new Sample(this, wave, waveFileOffset - dwplFileOffset)); |
pSamples->push_back(new Sample(this, wave, waveFileOffset - dwplFileOffset)); |
1278 |
} |
} |
1290 |
* @returns pointer to new Sample object |
* @returns pointer to new Sample object |
1291 |
*/ |
*/ |
1292 |
Sample* File::AddSample() { |
Sample* File::AddSample() { |
1293 |
|
if (!pSamples) LoadSamples(); |
1294 |
__ensureMandatoryChunksExist(); |
__ensureMandatoryChunksExist(); |
1295 |
RIFF::List* wvpl = pRIFF->GetSubList(LIST_TYPE_WVPL); |
RIFF::List* wvpl = pRIFF->GetSubList(LIST_TYPE_WVPL); |
1296 |
// create new Sample object and its respective 'wave' list chunk |
// create new Sample object and its respective 'wave' list chunk |
|
if (!pSamples) pSamples = new SampleList; |
|
1297 |
RIFF::List* wave = wvpl->AddSubList(LIST_TYPE_WAVE); |
RIFF::List* wave = wvpl->AddSubList(LIST_TYPE_WAVE); |
1298 |
Sample* pSample = new Sample(this, wave, 0 /*arbitrary value, we update offsets when we save*/); |
Sample* pSample = new Sample(this, wave, 0 /*arbitrary value, we update offsets when we save*/); |
1299 |
pSamples->push_back(pSample); |
pSamples->push_back(pSample); |
1329 |
} |
} |
1330 |
|
|
1331 |
void File::LoadInstruments() { |
void File::LoadInstruments() { |
1332 |
|
if (!pInstruments) pInstruments = new InstrumentList; |
1333 |
RIFF::List* lstInstruments = pRIFF->GetSubList(LIST_TYPE_LINS); |
RIFF::List* lstInstruments = pRIFF->GetSubList(LIST_TYPE_LINS); |
1334 |
if (lstInstruments) { |
if (lstInstruments) { |
1335 |
RIFF::List* lstInstr = lstInstruments->GetFirstSubList(); |
RIFF::List* lstInstr = lstInstruments->GetFirstSubList(); |
1336 |
while (lstInstr) { |
while (lstInstr) { |
1337 |
if (lstInstr->GetListType() == LIST_TYPE_INS) { |
if (lstInstr->GetListType() == LIST_TYPE_INS) { |
|
if (!pInstruments) pInstruments = new InstrumentList; |
|
1338 |
pInstruments->push_back(new Instrument(this, lstInstr)); |
pInstruments->push_back(new Instrument(this, lstInstr)); |
1339 |
} |
} |
1340 |
lstInstr = lstInstruments->GetNextSubList(); |
lstInstr = lstInstruments->GetNextSubList(); |
1350 |
* @returns pointer to new Instrument object |
* @returns pointer to new Instrument object |
1351 |
*/ |
*/ |
1352 |
Instrument* File::AddInstrument() { |
Instrument* File::AddInstrument() { |
1353 |
|
if (!pInstruments) LoadInstruments(); |
1354 |
__ensureMandatoryChunksExist(); |
__ensureMandatoryChunksExist(); |
|
if (!pInstruments) pInstruments = new InstrumentList; |
|
1355 |
RIFF::List* lstInstruments = pRIFF->GetSubList(LIST_TYPE_LINS); |
RIFF::List* lstInstruments = pRIFF->GetSubList(LIST_TYPE_LINS); |
1356 |
RIFF::List* lstInstr = lstInstruments->AddSubList(LIST_TYPE_INS); |
RIFF::List* lstInstr = lstInstruments->AddSubList(LIST_TYPE_INS); |
1357 |
Instrument* pInstrument = new Instrument(this, lstInstr); |
Instrument* pInstrument = new Instrument(this, lstInstr); |
1359 |
return pInstrument; |
return pInstrument; |
1360 |
} |
} |
1361 |
|
|
1362 |
/** @brief Delete a instrument. |
/** @brief Delete an instrument. |
1363 |
* |
* |
1364 |
* This will delete the given Instrument object from the DLS file. You |
* This will delete the given Instrument object from the DLS file. You |
1365 |
* have to call Save() to make this persistent to the file. |
* have to call Save() to make this persistent to the file. |
1390 |
RIFF::Chunk* ckVersion = pRIFF->GetSubChunk(CHUNK_ID_VERS); |
RIFF::Chunk* ckVersion = pRIFF->GetSubChunk(CHUNK_ID_VERS); |
1391 |
if (!ckVersion) ckVersion = pRIFF->AddSubChunk(CHUNK_ID_VERS, 8); |
if (!ckVersion) ckVersion = pRIFF->AddSubChunk(CHUNK_ID_VERS, 8); |
1392 |
uint8_t* pData = (uint8_t*) ckVersion->LoadChunkData(); |
uint8_t* pData = (uint8_t*) ckVersion->LoadChunkData(); |
1393 |
memccpy(pData, pVersion, 2, 4); |
store16(&pData[0], pVersion->minor); |
1394 |
|
store16(&pData[2], pVersion->major); |
1395 |
|
store16(&pData[4], pVersion->build); |
1396 |
|
store16(&pData[6], pVersion->release); |
1397 |
} |
} |
1398 |
|
|
1399 |
// update 'colh' chunk |
// update 'colh' chunk |
1401 |
RIFF::Chunk* colh = pRIFF->GetSubChunk(CHUNK_ID_COLH); |
RIFF::Chunk* colh = pRIFF->GetSubChunk(CHUNK_ID_COLH); |
1402 |
if (!colh) colh = pRIFF->AddSubChunk(CHUNK_ID_COLH, 4); |
if (!colh) colh = pRIFF->AddSubChunk(CHUNK_ID_COLH, 4); |
1403 |
uint8_t* pData = (uint8_t*) colh->LoadChunkData(); |
uint8_t* pData = (uint8_t*) colh->LoadChunkData(); |
1404 |
memccpy(pData, &Instruments, 1, 4); |
store32(pData, Instruments); |
1405 |
|
|
1406 |
// update instrument's chunks |
// update instrument's chunks |
1407 |
if (pInstruments) { |
if (pInstruments) { |
1421 |
ptbl->Resize(iPtblSize); |
ptbl->Resize(iPtblSize); |
1422 |
pData = (uint8_t*) ptbl->LoadChunkData(); |
pData = (uint8_t*) ptbl->LoadChunkData(); |
1423 |
WavePoolCount = iSamples; |
WavePoolCount = iSamples; |
1424 |
memccpy(&pData[4], &WavePoolCount, 1, 4); |
store32(&pData[4], WavePoolCount); |
1425 |
// we actually update the sample offsets in the pool table when we Save() |
// we actually update the sample offsets in the pool table when we Save() |
1426 |
memset(&pData[WavePoolHeaderSize], 0, iPtblSize - WavePoolHeaderSize); |
memset(&pData[WavePoolHeaderSize], 0, iPtblSize - WavePoolHeaderSize); |
1427 |
|
|
1510 |
unsigned long ulOriginalPos = ptbl->GetPos(); |
unsigned long ulOriginalPos = ptbl->GetPos(); |
1511 |
// update headers |
// update headers |
1512 |
ptbl->SetPos(0); |
ptbl->SetPos(0); |
1513 |
ptbl->WriteUint32(&WavePoolHeaderSize); |
uint32_t tmp = WavePoolHeaderSize; |
1514 |
ptbl->WriteUint32(&WavePoolCount); |
ptbl->WriteUint32(&tmp); |
1515 |
|
tmp = WavePoolCount; |
1516 |
|
ptbl->WriteUint32(&tmp); |
1517 |
// update offsets |
// update offsets |
1518 |
ptbl->SetPos(WavePoolHeaderSize); |
ptbl->SetPos(WavePoolHeaderSize); |
1519 |
if (b64BitWavePoolOffsets) { |
if (b64BitWavePoolOffsets) { |
1520 |
for (int i = 0 ; i < WavePoolCount ; i++) { |
for (int i = 0 ; i < WavePoolCount ; i++) { |
1521 |
ptbl->WriteUint32(&pWavePoolTableHi[i]); |
tmp = pWavePoolTableHi[i]; |
1522 |
ptbl->WriteUint32(&pWavePoolTable[i]); |
ptbl->WriteUint32(&tmp); |
1523 |
|
tmp = pWavePoolTable[i]; |
1524 |
|
ptbl->WriteUint32(&tmp); |
1525 |
} |
} |
1526 |
} else { // conventional 32 bit offsets |
} else { // conventional 32 bit offsets |
1527 |
for (int i = 0 ; i < WavePoolCount ; i++) |
for (int i = 0 ; i < WavePoolCount ; i++) { |
1528 |
ptbl->WriteUint32(&pWavePoolTable[i]); |
tmp = pWavePoolTable[i]; |
1529 |
|
ptbl->WriteUint32(&tmp); |
1530 |
|
} |
1531 |
} |
} |
1532 |
// restore 'ptbl' chunk's original read/write position |
// restore 'ptbl' chunk's original read/write position |
1533 |
ptbl->SetPos(ulOriginalPos); |
ptbl->SetPos(ulOriginalPos); |