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 * |
45 |
#define CONN_TRANSFORM_INVERT_SRC_ENCODE(x) ((x) ? 0x8000 : 0) |
#define CONN_TRANSFORM_INVERT_SRC_ENCODE(x) ((x) ? 0x8000 : 0) |
46 |
#define CONN_TRANSFORM_INVERT_CTL_ENCODE(x) ((x) ? 0x0200 : 0) |
#define CONN_TRANSFORM_INVERT_CTL_ENCODE(x) ((x) ? 0x0200 : 0) |
47 |
|
|
48 |
#define DRUM_TYPE_MASK 0x00000001 |
#define DRUM_TYPE_MASK 0x80000000 |
49 |
|
|
50 |
#define F_RGN_OPTION_SELFNONEXCLUSIVE 0x0001 |
#define F_RGN_OPTION_SELFNONEXCLUSIVE 0x0001 |
51 |
|
|
142 |
const int iEntrySize = 12; // 12 bytes per connection block |
const int iEntrySize = 12; // 12 bytes per connection block |
143 |
pArticulationCk->Resize(HeaderSize + Connections * iEntrySize); |
pArticulationCk->Resize(HeaderSize + Connections * iEntrySize); |
144 |
uint8_t* pData = (uint8_t*) pArticulationCk->LoadChunkData(); |
uint8_t* pData = (uint8_t*) pArticulationCk->LoadChunkData(); |
145 |
memccpy(&pData[0], &HeaderSize, 1, 2); |
store16(&pData[0], HeaderSize); |
146 |
memccpy(&pData[2], &Connections, 1, 2); |
store16(&pData[2], Connections); |
147 |
for (uint32_t i = 0; i < Connections; i++) { |
for (uint32_t i = 0; i < Connections; i++) { |
148 |
Connection::conn_block_t c = pConnections[i].ToConnBlock(); |
Connection::conn_block_t c = pConnections[i].ToConnBlock(); |
149 |
memccpy(&pData[HeaderSize + i * iEntrySize], &c.source, 1, 2); |
store16(&pData[HeaderSize + i * iEntrySize], c.source); |
150 |
memccpy(&pData[HeaderSize + i * iEntrySize + 2], &c.control, 1, 2); |
store16(&pData[HeaderSize + i * iEntrySize + 2], c.control); |
151 |
memccpy(&pData[HeaderSize + i * iEntrySize + 4], &c.destination, 1, 2); |
store16(&pData[HeaderSize + i * iEntrySize + 4], c.destination); |
152 |
memccpy(&pData[HeaderSize + i * iEntrySize + 6], &c.transform, 1, 2); |
store16(&pData[HeaderSize + i * iEntrySize + 6], c.transform); |
153 |
memccpy(&pData[HeaderSize + i * iEntrySize + 8], &c.scale, 1, 4); |
store32(&pData[HeaderSize + i * iEntrySize + 8], c.scale); |
154 |
} |
} |
155 |
} |
} |
156 |
|
|
212 |
* call File::Save() to make changes persistent. |
* call File::Save() to make changes persistent. |
213 |
*/ |
*/ |
214 |
void Articulator::UpdateChunks() { |
void Articulator::UpdateChunks() { |
215 |
ArticulationList::iterator iter = pArticulations->begin(); |
if (pArticulations) { |
216 |
ArticulationList::iterator end = pArticulations->end(); |
ArticulationList::iterator iter = pArticulations->begin(); |
217 |
for (; iter != end; ++iter) { |
ArticulationList::iterator end = pArticulations->end(); |
218 |
(*iter)->UpdateChunks(); |
for (; iter != end; ++iter) { |
219 |
|
(*iter)->UpdateChunks(); |
220 |
|
} |
221 |
} |
} |
222 |
} |
} |
223 |
|
|
228 |
|
|
229 |
/** @brief Constructor. |
/** @brief Constructor. |
230 |
* |
* |
231 |
* Initializes the info strings with values provided by a INFO list chunk. |
* Initializes the info strings with values provided by an INFO list chunk. |
232 |
* |
* |
233 |
* @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 |
234 |
*/ |
*/ |
235 |
Info::Info(RIFF::List* list) { |
Info::Info(RIFF::List* list) { |
236 |
|
FixedStringLengths = NULL; |
237 |
pResourceListChunk = list; |
pResourceListChunk = list; |
238 |
if (list) { |
if (list) { |
239 |
RIFF::List* lstINFO = list->GetSubList(LIST_TYPE_INFO); |
RIFF::List* lstINFO = list->GetSubList(LIST_TYPE_INFO); |
254 |
LoadString(CHUNK_ID_ISRC, lstINFO, Source); |
LoadString(CHUNK_ID_ISRC, lstINFO, Source); |
255 |
LoadString(CHUNK_ID_ISRF, lstINFO, SourceForm); |
LoadString(CHUNK_ID_ISRF, lstINFO, SourceForm); |
256 |
LoadString(CHUNK_ID_ICMS, lstINFO, Commissioned); |
LoadString(CHUNK_ID_ICMS, lstINFO, Commissioned); |
257 |
|
LoadString(CHUNK_ID_ISBJ, lstINFO, Subject); |
258 |
} |
} |
259 |
} |
} |
260 |
} |
} |
261 |
|
|
262 |
|
Info::~Info() { |
263 |
|
} |
264 |
|
|
265 |
/** @brief Load given INFO field. |
/** @brief Load given INFO field. |
266 |
* |
* |
267 |
* 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 |
269 |
*/ |
*/ |
270 |
void Info::LoadString(uint32_t ChunkID, RIFF::List* lstINFO, String& s) { |
void Info::LoadString(uint32_t ChunkID, RIFF::List* lstINFO, String& s) { |
271 |
RIFF::Chunk* ck = lstINFO->GetSubChunk(ChunkID); |
RIFF::Chunk* ck = lstINFO->GetSubChunk(ChunkID); |
272 |
if (ck) { |
::LoadString(ck, s); // function from helper.h |
|
// TODO: no check for ZSTR terminated strings yet |
|
|
s = (char*) ck->LoadChunkData(); |
|
|
ck->ReleaseChunkData(); |
|
|
} |
|
273 |
} |
} |
274 |
|
|
275 |
/** @brief Apply given INFO field to the respective chunk. |
/** @brief Apply given INFO field to the respective chunk. |
276 |
* |
* |
277 |
* Apply given info value to info chunk with ID \a ChunkID, which is a |
* Apply given info value to info chunk with ID \a ChunkID, which is a |
278 |
* subchunk of INFO list chunk \a lstINFO. If the given chunk already |
* subchunk of INFO list chunk \a lstINFO. If the given chunk already |
279 |
* exists, value \a s will be applied, otherwise if it doesn't exist yet |
* exists, value \a s will be applied. Otherwise if it doesn't exist yet |
280 |
* and \a sDefault is not an empty string, such a chunk will be created |
* and either \a s or \a sDefault is not an empty string, such a chunk |
281 |
* and \a sDefault will be applied. |
* will be created and either \a s or \a sDefault will be applied |
282 |
|
* (depending on which one is not an empty string, if both are not an |
283 |
|
* empty string \a s will be preferred). |
284 |
* |
* |
285 |
* @param ChunkID - 32 bit RIFF chunk ID of INFO subchunk |
* @param ChunkID - 32 bit RIFF chunk ID of INFO subchunk |
286 |
* @param lstINFO - parent (INFO) RIFF list chunk |
* @param lstINFO - parent (INFO) RIFF list chunk |
288 |
* @param sDefault - default value |
* @param sDefault - default value |
289 |
*/ |
*/ |
290 |
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) { |
291 |
RIFF::Chunk* ck = lstINFO->GetSubChunk(ChunkID); |
int size = 0; |
292 |
if (ck) { // if chunk exists already, use 's' as value |
if (FixedStringLengths) { |
293 |
ck->Resize(s.size() + 1); |
for (int i = 0 ; FixedStringLengths[i].length ; i++) { |
294 |
char* pData = (char*) ck->LoadChunkData(); |
if (FixedStringLengths[i].chunkId == ChunkID) { |
295 |
memcpy(pData, s.c_str(), s.size() + 1); |
size = FixedStringLengths[i].length; |
296 |
} else if (sDefault != "") { // create chunk and use default value |
} |
297 |
ck = lstINFO->AddSubChunk(ChunkID, sDefault.size() + 1); |
} |
|
char* pData = (char*) ck->LoadChunkData(); |
|
|
memcpy(pData, sDefault.c_str(), sDefault.size() + 1); |
|
298 |
} |
} |
299 |
|
RIFF::Chunk* ck = lstINFO->GetSubChunk(ChunkID); |
300 |
|
::SaveString(ChunkID, ck, lstINFO, s, sDefault, size != 0, size); // function from helper.h |
301 |
} |
} |
302 |
|
|
303 |
/** @brief Update chunks with current info values. |
/** @brief Update chunks with current info values. |
310 |
|
|
311 |
// make sure INFO list chunk exists |
// make sure INFO list chunk exists |
312 |
RIFF::List* lstINFO = pResourceListChunk->GetSubList(LIST_TYPE_INFO); |
RIFF::List* lstINFO = pResourceListChunk->GetSubList(LIST_TYPE_INFO); |
|
if (!lstINFO) lstINFO = pResourceListChunk->AddSubList(LIST_TYPE_INFO); |
|
313 |
|
|
314 |
// assemble default values in case the respective chunk is missing yet |
String defaultName = ""; |
315 |
String defaultName = "NONAME"; |
String defaultCreationDate = ""; |
316 |
// get current date |
String defaultSoftware = ""; |
317 |
time_t now = time(NULL); |
String defaultComments = ""; |
318 |
tm* pNowBroken = localtime(&now); |
|
319 |
String defaultCreationDate = ToString(pNowBroken->tm_year) + "-" + |
uint32_t resourceType = pResourceListChunk->GetListType(); |
320 |
ToString(pNowBroken->tm_mon) + "-" + |
|
321 |
ToString(pNowBroken->tm_mday); |
if (!lstINFO) { |
322 |
String defaultSoftware = libraryName() + " " + libraryVersion(); |
lstINFO = pResourceListChunk->AddSubList(LIST_TYPE_INFO); |
323 |
String defaultComments = "Created with " + libraryName() + " " + libraryVersion(); |
|
324 |
|
// assemble default values |
325 |
|
defaultName = "NONAME"; |
326 |
|
|
327 |
|
if (resourceType == RIFF_TYPE_DLS) { |
328 |
|
// get current date |
329 |
|
time_t now = time(NULL); |
330 |
|
tm* pNowBroken = localtime(&now); |
331 |
|
char buf[11]; |
332 |
|
strftime(buf, 11, "%F", pNowBroken); |
333 |
|
defaultCreationDate = buf; |
334 |
|
|
335 |
|
defaultComments = "Created with " + libraryName() + " " + libraryVersion(); |
336 |
|
} |
337 |
|
if (resourceType == RIFF_TYPE_DLS || resourceType == LIST_TYPE_INS) |
338 |
|
{ |
339 |
|
defaultSoftware = libraryName() + " " + libraryVersion(); |
340 |
|
} |
341 |
|
} |
342 |
|
|
343 |
// save values |
// save values |
344 |
SaveString(CHUNK_ID_INAM, lstINFO, Name, defaultName); |
|
345 |
SaveString(CHUNK_ID_IARL, lstINFO, ArchivalLocation, String("")); |
SaveString(CHUNK_ID_IARL, lstINFO, ArchivalLocation, String("")); |
346 |
SaveString(CHUNK_ID_ICRD, lstINFO, CreationDate, defaultCreationDate); |
SaveString(CHUNK_ID_IART, lstINFO, Artists, String("")); |
347 |
|
SaveString(CHUNK_ID_ICMS, lstINFO, Commissioned, String("")); |
348 |
SaveString(CHUNK_ID_ICMT, lstINFO, Comments, defaultComments); |
SaveString(CHUNK_ID_ICMT, lstINFO, Comments, defaultComments); |
|
SaveString(CHUNK_ID_IPRD, lstINFO, Product, String("")); |
|
349 |
SaveString(CHUNK_ID_ICOP, lstINFO, Copyright, String("")); |
SaveString(CHUNK_ID_ICOP, lstINFO, Copyright, String("")); |
350 |
SaveString(CHUNK_ID_IART, lstINFO, Artists, String("")); |
SaveString(CHUNK_ID_ICRD, lstINFO, CreationDate, defaultCreationDate); |
351 |
|
SaveString(CHUNK_ID_IENG, lstINFO, Engineer, String("")); |
352 |
SaveString(CHUNK_ID_IGNR, lstINFO, Genre, String("")); |
SaveString(CHUNK_ID_IGNR, lstINFO, Genre, String("")); |
353 |
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); |
|
354 |
SaveString(CHUNK_ID_IMED, lstINFO, Medium, String("")); |
SaveString(CHUNK_ID_IMED, lstINFO, Medium, String("")); |
355 |
|
SaveString(CHUNK_ID_INAM, lstINFO, Name, defaultName); |
356 |
|
SaveString(CHUNK_ID_IPRD, lstINFO, Product, String("")); |
357 |
|
SaveString(CHUNK_ID_ISBJ, lstINFO, Subject, String("")); |
358 |
|
SaveString(CHUNK_ID_ISFT, lstINFO, Software, defaultSoftware); |
359 |
SaveString(CHUNK_ID_ISRC, lstINFO, Source, String("")); |
SaveString(CHUNK_ID_ISRC, lstINFO, Source, String("")); |
360 |
SaveString(CHUNK_ID_ISRF, lstINFO, SourceForm, String("")); |
SaveString(CHUNK_ID_ISRF, lstINFO, SourceForm, String("")); |
361 |
SaveString(CHUNK_ID_ICMS, lstINFO, Commissioned, String("")); |
SaveString(CHUNK_ID_ITCH, lstINFO, Technician, String("")); |
362 |
} |
} |
363 |
|
|
364 |
|
|
464 |
} |
} |
465 |
uint8_t* pData = (uint8_t*) wsmp->LoadChunkData(); |
uint8_t* pData = (uint8_t*) wsmp->LoadChunkData(); |
466 |
// update headers size |
// update headers size |
467 |
memccpy(&pData[0], &uiHeaderSize, 1, 4); |
store32(&pData[0], uiHeaderSize); |
468 |
// update respective sampler options bits |
// update respective sampler options bits |
469 |
SamplerOptions = (NoSampleDepthTruncation) ? SamplerOptions | F_WSMP_NO_TRUNCATION |
SamplerOptions = (NoSampleDepthTruncation) ? SamplerOptions | F_WSMP_NO_TRUNCATION |
470 |
: SamplerOptions & (~F_WSMP_NO_TRUNCATION); |
: SamplerOptions & (~F_WSMP_NO_TRUNCATION); |
471 |
SamplerOptions = (NoSampleCompression) ? SamplerOptions | F_WSMP_NO_COMPRESSION |
SamplerOptions = (NoSampleCompression) ? SamplerOptions | F_WSMP_NO_COMPRESSION |
472 |
: SamplerOptions & (~F_WSMP_NO_COMPRESSION); |
: SamplerOptions & (~F_WSMP_NO_COMPRESSION); |
473 |
|
store16(&pData[4], UnityNote); |
474 |
|
store16(&pData[6], FineTune); |
475 |
|
store32(&pData[8], Gain); |
476 |
|
store32(&pData[12], SamplerOptions); |
477 |
|
store32(&pData[16], SampleLoops); |
478 |
// update loop definitions |
// update loop definitions |
479 |
for (uint32_t i = 0; i < SampleLoops; i++) { |
for (uint32_t i = 0; i < SampleLoops; i++) { |
480 |
//FIXME: this does not handle extended loop structs correctly |
//FIXME: this does not handle extended loop structs correctly |
481 |
memccpy(&pData[uiHeaderSize + i * 16], pSampleLoops + i, 4, 4); |
store32(&pData[uiHeaderSize + i * 16], pSampleLoops[i].Size); |
482 |
|
store32(&pData[uiHeaderSize + i * 16 + 4], pSampleLoops[i].LoopType); |
483 |
|
store32(&pData[uiHeaderSize + i * 16 + 8], pSampleLoops[i].LoopStart); |
484 |
|
store32(&pData[uiHeaderSize + i * 16 + 12], pSampleLoops[i].LoopLength); |
485 |
} |
} |
486 |
} |
} |
487 |
|
|
488 |
|
/** |
489 |
|
* Adds a new sample loop with the provided loop definition. |
490 |
|
* |
491 |
|
* @param pLoopDef - points to a loop definition that is to be copied |
492 |
|
*/ |
493 |
|
void Sampler::AddSampleLoop(sample_loop_t* pLoopDef) { |
494 |
|
sample_loop_t* pNewLoops = new sample_loop_t[SampleLoops + 1]; |
495 |
|
// copy old loops array |
496 |
|
for (int i = 0; i < SampleLoops; i++) { |
497 |
|
pNewLoops[i] = pSampleLoops[i]; |
498 |
|
} |
499 |
|
// add the new loop |
500 |
|
pNewLoops[SampleLoops] = *pLoopDef; |
501 |
|
// auto correct size field |
502 |
|
pNewLoops[SampleLoops].Size = sizeof(DLS::sample_loop_t); |
503 |
|
// free the old array and update the member variables |
504 |
|
if (SampleLoops) delete[] pSampleLoops; |
505 |
|
pSampleLoops = pNewLoops; |
506 |
|
SampleLoops++; |
507 |
|
} |
508 |
|
|
509 |
|
/** |
510 |
|
* Deletes an existing sample loop. |
511 |
|
* |
512 |
|
* @param pLoopDef - pointer to existing loop definition |
513 |
|
* @throws Exception - if given loop definition does not exist |
514 |
|
*/ |
515 |
|
void Sampler::DeleteSampleLoop(sample_loop_t* pLoopDef) { |
516 |
|
sample_loop_t* pNewLoops = new sample_loop_t[SampleLoops - 1]; |
517 |
|
// copy old loops array (skipping given loop) |
518 |
|
for (int i = 0, o = 0; i < SampleLoops; i++) { |
519 |
|
if (&pSampleLoops[i] == pLoopDef) continue; |
520 |
|
if (o == SampleLoops - 1) |
521 |
|
throw Exception("Could not delete Sample Loop, because it does not exist"); |
522 |
|
pNewLoops[o] = pSampleLoops[i]; |
523 |
|
o++; |
524 |
|
} |
525 |
|
// free the old array and update the member variables |
526 |
|
if (SampleLoops) delete[] pSampleLoops; |
527 |
|
pSampleLoops = pNewLoops; |
528 |
|
SampleLoops--; |
529 |
|
} |
530 |
|
|
531 |
|
|
532 |
|
|
533 |
// *************** Sample *************** |
// *************** Sample *************** |
561 |
AverageBytesPerSecond = pCkFormat->ReadUint32(); |
AverageBytesPerSecond = pCkFormat->ReadUint32(); |
562 |
BlockAlign = pCkFormat->ReadUint16(); |
BlockAlign = pCkFormat->ReadUint16(); |
563 |
// PCM format specific |
// PCM format specific |
564 |
if (FormatTag == WAVE_FORMAT_PCM) { |
if (FormatTag == DLS_WAVE_FORMAT_PCM) { |
565 |
BitDepth = pCkFormat->ReadUint16(); |
BitDepth = pCkFormat->ReadUint16(); |
566 |
FrameSize = (FormatTag == WAVE_FORMAT_PCM) ? (BitDepth / 8) * Channels |
FrameSize = (BitDepth / 8) * Channels; |
|
: 0; |
|
567 |
} else { // unsupported sample data format |
} else { // unsupported sample data format |
568 |
BitDepth = 0; |
BitDepth = 0; |
569 |
FrameSize = 0; |
FrameSize = 0; |
570 |
} |
} |
571 |
} else { // 'fmt' chunk missing |
} else { // 'fmt' chunk missing |
572 |
FormatTag = WAVE_FORMAT_PCM; |
FormatTag = DLS_WAVE_FORMAT_PCM; |
573 |
BitDepth = 16; |
BitDepth = 16; |
574 |
Channels = 1; |
Channels = 1; |
575 |
SamplesPerSecond = 44100; |
SamplesPerSecond = 44100; |
577 |
FrameSize = (BitDepth / 8) * Channels; |
FrameSize = (BitDepth / 8) * Channels; |
578 |
BlockAlign = FrameSize; |
BlockAlign = FrameSize; |
579 |
} |
} |
580 |
SamplesTotal = (pCkData) ? (FormatTag == WAVE_FORMAT_PCM) ? pCkData->GetSize() / FrameSize |
SamplesTotal = (pCkData) ? (FormatTag == DLS_WAVE_FORMAT_PCM) ? pCkData->GetSize() / FrameSize |
581 |
: 0 |
: 0 |
582 |
: 0; |
: 0; |
583 |
} |
} |
584 |
|
|
638 |
* the RIFF chunk which encapsulates the sample's wave data. The |
* the RIFF chunk which encapsulates the sample's wave data. The |
639 |
* returned value is dependant to the current FrameSize value. |
* returned value is dependant to the current FrameSize value. |
640 |
* |
* |
641 |
* @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 |
642 |
* @see FrameSize, FormatTag |
* @see FrameSize, FormatTag |
643 |
*/ |
*/ |
644 |
unsigned long Sample::GetSize() { |
unsigned long Sample::GetSize() { |
645 |
if (FormatTag != WAVE_FORMAT_PCM) return 0; |
if (FormatTag != DLS_WAVE_FORMAT_PCM) return 0; |
646 |
return (pCkData) ? pCkData->GetSize() / FrameSize : 0; |
return (pCkData) ? pCkData->GetSize() / FrameSize : 0; |
647 |
} |
} |
648 |
|
|
664 |
* calling File::Save() as this might exceed the current sample's |
* calling File::Save() as this might exceed the current sample's |
665 |
* boundary! |
* boundary! |
666 |
* |
* |
667 |
* Also note: only WAVE_FORMAT_PCM is currently supported, that is |
* Also note: only DLS_WAVE_FORMAT_PCM is currently supported, that is |
668 |
* FormatTag must be WAVE_FORMAT_PCM. Trying to resize samples with |
* FormatTag must be DLS_WAVE_FORMAT_PCM. Trying to resize samples with |
669 |
* other formats will fail! |
* other formats will fail! |
670 |
* |
* |
671 |
* @param iNewSize - new sample wave data size in sample points (must be |
* @param iNewSize - new sample wave data size in sample points (must be |
672 |
* greater than zero) |
* greater than zero) |
673 |
* @throws Excecption if FormatTag != WAVE_FORMAT_PCM |
* @throws Excecption if FormatTag != DLS_WAVE_FORMAT_PCM |
674 |
* @throws Exception if \a iNewSize is less than 1 |
* @throws Exception if \a iNewSize is less than 1 |
675 |
* @see File::Save(), FrameSize, FormatTag |
* @see File::Save(), FrameSize, FormatTag |
676 |
*/ |
*/ |
677 |
void Sample::Resize(int iNewSize) { |
void Sample::Resize(int iNewSize) { |
678 |
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"); |
679 |
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"); |
680 |
const int iSizeInBytes = iNewSize * FrameSize; |
const int iSizeInBytes = iNewSize * FrameSize; |
681 |
pCkData = pWaveList->GetSubChunk(CHUNK_ID_DATA); |
pCkData = pWaveList->GetSubChunk(CHUNK_ID_DATA); |
688 |
* 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 |
689 |
* the sample into RAM, thus for disk streaming. |
* the sample into RAM, thus for disk streaming. |
690 |
* |
* |
691 |
* Also note: only WAVE_FORMAT_PCM is currently supported, that is |
* Also note: only DLS_WAVE_FORMAT_PCM is currently supported, that is |
692 |
* FormatTag must be WAVE_FORMAT_PCM. Trying to reposition the sample |
* FormatTag must be DLS_WAVE_FORMAT_PCM. Trying to reposition the sample |
693 |
* with other formats will fail! |
* with other formats will fail! |
694 |
* |
* |
695 |
* @param SampleCount number of sample points |
* @param SampleCount number of sample points |
696 |
* @param Whence to which relation \a SampleCount refers to |
* @param Whence to which relation \a SampleCount refers to |
697 |
* @returns new position within the sample, 0 if |
* @returns new position within the sample, 0 if |
698 |
* FormatTag != WAVE_FORMAT_PCM |
* FormatTag != DLS_WAVE_FORMAT_PCM |
699 |
* @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 |
700 |
* @see FrameSize, FormatTag |
* @see FrameSize, FormatTag |
701 |
*/ |
*/ |
702 |
unsigned long Sample::SetPos(unsigned long SampleCount, RIFF::stream_whence_t Whence) { |
unsigned long Sample::SetPos(unsigned long SampleCount, RIFF::stream_whence_t Whence) { |
703 |
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 |
704 |
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"); |
705 |
unsigned long orderedBytes = SampleCount * FrameSize; |
unsigned long orderedBytes = SampleCount * FrameSize; |
706 |
unsigned long result = pCkData->SetPos(orderedBytes, Whence); |
unsigned long result = pCkData->SetPos(orderedBytes, Whence); |
718 |
* @param SampleCount number of sample points to read |
* @param SampleCount number of sample points to read |
719 |
*/ |
*/ |
720 |
unsigned long Sample::Read(void* pBuffer, unsigned long SampleCount) { |
unsigned long Sample::Read(void* pBuffer, unsigned long SampleCount) { |
721 |
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 |
722 |
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? |
723 |
} |
} |
724 |
|
|
738 |
* @see LoadSampleData() |
* @see LoadSampleData() |
739 |
*/ |
*/ |
740 |
unsigned long Sample::Write(void* pBuffer, unsigned long SampleCount) { |
unsigned long Sample::Write(void* pBuffer, unsigned long SampleCount) { |
741 |
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 |
742 |
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"); |
743 |
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? |
744 |
} |
} |
747 |
* Apply sample and its settings to the respective RIFF chunks. You have |
* Apply sample and its settings to the respective RIFF chunks. You have |
748 |
* to call File::Save() to make changes persistent. |
* to call File::Save() to make changes persistent. |
749 |
* |
* |
750 |
* @throws Exception if FormatTag != WAVE_FORMAT_PCM or no sample data |
* @throws Exception if FormatTag != DLS_WAVE_FORMAT_PCM or no sample data |
751 |
* was provided yet |
* was provided yet |
752 |
*/ |
*/ |
753 |
void Sample::UpdateChunks() { |
void Sample::UpdateChunks() { |
754 |
if (FormatTag != WAVE_FORMAT_PCM) |
if (FormatTag != DLS_WAVE_FORMAT_PCM) |
755 |
throw Exception("Could not save sample, only PCM format is supported"); |
throw Exception("Could not save sample, only PCM format is supported"); |
756 |
// 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 |
757 |
if (!pCkData) |
if (!pCkData) |
763 |
if (!pCkFormat) pCkFormat = pWaveList->AddSubChunk(CHUNK_ID_FMT, 16); // assumes PCM format |
if (!pCkFormat) pCkFormat = pWaveList->AddSubChunk(CHUNK_ID_FMT, 16); // assumes PCM format |
764 |
uint8_t* pData = (uint8_t*) pCkFormat->LoadChunkData(); |
uint8_t* pData = (uint8_t*) pCkFormat->LoadChunkData(); |
765 |
// update 'fmt' chunk |
// update 'fmt' chunk |
766 |
memccpy(&pData[0], &FormatTag, 1, 2); |
store16(&pData[0], FormatTag); |
767 |
memccpy(&pData[2], &Channels, 1, 2); |
store16(&pData[2], Channels); |
768 |
memccpy(&pData[4], &SamplesPerSecond, 1, 4); |
store32(&pData[4], SamplesPerSecond); |
769 |
memccpy(&pData[8], &AverageBytesPerSecond, 1, 4); |
store32(&pData[8], AverageBytesPerSecond); |
770 |
memccpy(&pData[12], &BlockAlign, 1, 2); |
store16(&pData[12], BlockAlign); |
771 |
memccpy(&pData[14], &BitDepth, 1, 2); // assuming PCM format |
store16(&pData[14], BitDepth); // assuming PCM format |
772 |
} |
} |
773 |
|
|
774 |
|
|
860 |
void Region::UpdateChunks() { |
void Region::UpdateChunks() { |
861 |
// make sure 'rgnh' chunk exists |
// make sure 'rgnh' chunk exists |
862 |
RIFF::Chunk* rgnh = pCkRegion->GetSubChunk(CHUNK_ID_RGNH); |
RIFF::Chunk* rgnh = pCkRegion->GetSubChunk(CHUNK_ID_RGNH); |
863 |
if (!rgnh) rgnh = pCkRegion->AddSubChunk(CHUNK_ID_RGNH, 14); |
if (!rgnh) rgnh = pCkRegion->AddSubChunk(CHUNK_ID_RGNH, Layer ? 14 : 12); |
864 |
uint8_t* pData = (uint8_t*) rgnh->LoadChunkData(); |
uint8_t* pData = (uint8_t*) rgnh->LoadChunkData(); |
865 |
FormatOptionFlags = (SelfNonExclusive) |
FormatOptionFlags = (SelfNonExclusive) |
866 |
? FormatOptionFlags | F_RGN_OPTION_SELFNONEXCLUSIVE |
? FormatOptionFlags | F_RGN_OPTION_SELFNONEXCLUSIVE |
867 |
: FormatOptionFlags & (~F_RGN_OPTION_SELFNONEXCLUSIVE); |
: FormatOptionFlags & (~F_RGN_OPTION_SELFNONEXCLUSIVE); |
868 |
// update 'rgnh' chunk |
// update 'rgnh' chunk |
869 |
memccpy(&pData[0], &KeyRange, 2, 2); |
store16(&pData[0], KeyRange.low); |
870 |
memccpy(&pData[4], &VelocityRange, 2, 2); |
store16(&pData[2], KeyRange.high); |
871 |
memccpy(&pData[8], &FormatOptionFlags, 1, 2); |
store16(&pData[4], VelocityRange.low); |
872 |
memccpy(&pData[10], &KeyGroup, 1, 2); |
store16(&pData[6], VelocityRange.high); |
873 |
memccpy(&pData[12], &Layer, 1, 2); |
store16(&pData[8], FormatOptionFlags); |
874 |
|
store16(&pData[10], KeyGroup); |
875 |
|
if (rgnh->GetSize() >= 14) store16(&pData[12], Layer); |
876 |
|
|
877 |
// update chunks of base classes as well |
// update chunks of base classes as well (but skip Resource, |
878 |
Resource::UpdateChunks(); |
// as a rgn doesn't seem to have dlid and INFO chunks) |
879 |
Articulator::UpdateChunks(); |
Articulator::UpdateChunks(); |
880 |
Sampler::UpdateChunks(); |
Sampler::UpdateChunks(); |
881 |
|
|
892 |
// get sample's wave pool table index |
// get sample's wave pool table index |
893 |
int index = -1; |
int index = -1; |
894 |
File* pFile = (File*) GetParent()->GetParent(); |
File* pFile = (File*) GetParent()->GetParent(); |
895 |
File::SampleList::iterator iter = pFile->pSamples->begin(); |
if (pFile->pSamples) { |
896 |
File::SampleList::iterator end = pFile->pSamples->end(); |
File::SampleList::iterator iter = pFile->pSamples->begin(); |
897 |
for (int i = 0; iter != end; ++iter, i++) { |
File::SampleList::iterator end = pFile->pSamples->end(); |
898 |
if (*iter == pSample) { |
for (int i = 0; iter != end; ++iter, i++) { |
899 |
index = i; |
if (*iter == pSample) { |
900 |
break; |
index = i; |
901 |
|
break; |
902 |
|
} |
903 |
} |
} |
904 |
} |
} |
905 |
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"); |
906 |
WavePoolTableIndex = index; |
WavePoolTableIndex = index; |
907 |
// update 'wlnk' chunk |
// update 'wlnk' chunk |
908 |
memccpy(&pData[0], &WaveLinkOptionFlags, 1, 2); |
store16(&pData[0], WaveLinkOptionFlags); |
909 |
memccpy(&pData[2], &PhaseGroup, 1, 2); |
store16(&pData[2], PhaseGroup); |
910 |
memccpy(&pData[4], &Channel, 1, 4); |
store32(&pData[4], Channel); |
911 |
memccpy(&pData[8], &WavePoolTableIndex, 1, 4); |
store32(&pData[8], WavePoolTableIndex); |
912 |
} |
} |
913 |
|
|
914 |
|
|
966 |
} |
} |
967 |
|
|
968 |
void Instrument::LoadRegions() { |
void Instrument::LoadRegions() { |
969 |
|
if (!pRegions) pRegions = new RegionList; |
970 |
RIFF::List* lrgn = pCkInstrument->GetSubList(LIST_TYPE_LRGN); |
RIFF::List* lrgn = pCkInstrument->GetSubList(LIST_TYPE_LRGN); |
971 |
if (!lrgn) throw DLS::Exception("Mandatory chunks in <ins > chunk not found."); |
if (lrgn) { |
972 |
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 |
973 |
RIFF::List* rgn = lrgn->GetFirstSubList(); |
RIFF::List* rgn = lrgn->GetFirstSubList(); |
974 |
while (rgn) { |
while (rgn) { |
975 |
if (rgn->GetListType() == regionCkType) { |
if (rgn->GetListType() == regionCkType) { |
976 |
if (!pRegions) pRegions = new RegionList; |
pRegions->push_back(new Region(this, rgn)); |
977 |
pRegions->push_back(new Region(this, rgn)); |
} |
978 |
|
rgn = lrgn->GetNextSubList(); |
979 |
} |
} |
|
rgn = lrgn->GetNextSubList(); |
|
980 |
} |
} |
981 |
} |
} |
982 |
|
|
983 |
Region* Instrument::AddRegion() { |
Region* Instrument::AddRegion() { |
984 |
if (!pRegions) pRegions = new RegionList; |
if (!pRegions) LoadRegions(); |
985 |
RIFF::List* lrgn = pCkInstrument->GetSubList(LIST_TYPE_LRGN); |
RIFF::List* lrgn = pCkInstrument->GetSubList(LIST_TYPE_LRGN); |
986 |
if (!lrgn) lrgn = pCkInstrument->AddSubList(LIST_TYPE_LRGN); |
if (!lrgn) lrgn = pCkInstrument->AddSubList(LIST_TYPE_LRGN); |
987 |
RIFF::List* rgn = lrgn->AddSubList(LIST_TYPE_RGN); |
RIFF::List* rgn = lrgn->AddSubList(LIST_TYPE_RGN); |
988 |
Region* pNewRegion = new Region(this, rgn); |
Region* pNewRegion = new Region(this, rgn); |
989 |
pRegions->push_back(pNewRegion); |
pRegions->push_back(pNewRegion); |
990 |
|
Regions = pRegions->size(); |
991 |
return pNewRegion; |
return pNewRegion; |
992 |
} |
} |
993 |
|
|
994 |
|
void Instrument::MoveRegion(Region* pSrc, Region* pDst) { |
995 |
|
RIFF::List* lrgn = pCkInstrument->GetSubList(LIST_TYPE_LRGN); |
996 |
|
lrgn->MoveSubChunk(pSrc->pCkRegion, pDst ? pDst->pCkRegion : 0); |
997 |
|
|
998 |
|
pRegions->remove(pSrc); |
999 |
|
RegionList::iterator iter = find(pRegions->begin(), pRegions->end(), pDst); |
1000 |
|
pRegions->insert(iter, pSrc); |
1001 |
|
} |
1002 |
|
|
1003 |
void Instrument::DeleteRegion(Region* pRegion) { |
void Instrument::DeleteRegion(Region* pRegion) { |
1004 |
|
if (!pRegions) return; |
1005 |
RegionList::iterator iter = find(pRegions->begin(), pRegions->end(), pRegion); |
RegionList::iterator iter = find(pRegions->begin(), pRegions->end(), pRegion); |
1006 |
if (iter == pRegions->end()) return; |
if (iter == pRegions->end()) return; |
1007 |
pRegions->erase(iter); |
pRegions->erase(iter); |
1008 |
|
Regions = pRegions->size(); |
1009 |
delete pRegion; |
delete pRegion; |
1010 |
} |
} |
1011 |
|
|
1024 |
if (!insh) insh = pCkInstrument->AddSubChunk(CHUNK_ID_INSH, 12); |
if (!insh) insh = pCkInstrument->AddSubChunk(CHUNK_ID_INSH, 12); |
1025 |
uint8_t* pData = (uint8_t*) insh->LoadChunkData(); |
uint8_t* pData = (uint8_t*) insh->LoadChunkData(); |
1026 |
// update 'insh' chunk |
// update 'insh' chunk |
1027 |
Regions = pRegions->size(); |
Regions = (pRegions) ? pRegions->size() : 0; |
1028 |
midi_locale_t locale; |
midi_locale_t locale; |
1029 |
locale.instrument = MIDIProgram; |
locale.instrument = MIDIProgram; |
1030 |
locale.bank = MIDI_BANK_ENCODE(MIDIBankCoarse, MIDIBankFine); |
locale.bank = MIDI_BANK_ENCODE(MIDIBankCoarse, MIDIBankFine); |
1031 |
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); |
1032 |
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 |
1033 |
memccpy(&pData[0], &Regions, 1, 4); |
store32(&pData[0], Regions); |
1034 |
memccpy(&pData[4], &locale, 2, 4); |
store32(&pData[4], locale.bank); |
1035 |
|
store32(&pData[8], locale.instrument); |
1036 |
// update Region's chunks |
// update Region's chunks |
1037 |
|
if (!pRegions) return; |
1038 |
RegionList::iterator iter = pRegions->begin(); |
RegionList::iterator iter = pRegions->begin(); |
1039 |
RegionList::iterator end = pRegions->end(); |
RegionList::iterator end = pRegions->end(); |
1040 |
for (; iter != end; ++iter) { |
for (; iter != end; ++iter) { |
1073 |
* to add samples, instruments and finally call Save() to actually write |
* to add samples, instruments and finally call Save() to actually write |
1074 |
* a DLS file. |
* a DLS file. |
1075 |
*/ |
*/ |
1076 |
File::File() : pRIFF(new RIFF::File(RIFF_TYPE_DLS)), Resource(NULL, pRIFF) { |
File::File() : Resource(NULL, pRIFF = new RIFF::File(RIFF_TYPE_DLS)) { |
1077 |
|
pRIFF->SetByteOrder(RIFF::endian_little); |
1078 |
pVersion = new version_t; |
pVersion = new version_t; |
1079 |
pVersion->major = 0; |
pVersion->major = 0; |
1080 |
pVersion->minor = 0; |
pVersion->minor = 0; |
1118 |
Instruments = colh->ReadUint32(); |
Instruments = colh->ReadUint32(); |
1119 |
|
|
1120 |
RIFF::Chunk* ptbl = pRIFF->GetSubChunk(CHUNK_ID_PTBL); |
RIFF::Chunk* ptbl = pRIFF->GetSubChunk(CHUNK_ID_PTBL); |
1121 |
if (!ptbl) throw DLS::Exception("Mandatory <ptbl> chunk not found."); |
if (!ptbl) { // pool table is missing - this is probably an ".art" file |
1122 |
WavePoolHeaderSize = ptbl->ReadUint32(); |
WavePoolCount = 0; |
1123 |
WavePoolCount = ptbl->ReadUint32(); |
pWavePoolTable = NULL; |
1124 |
pWavePoolTable = new uint32_t[WavePoolCount]; |
pWavePoolTableHi = NULL; |
1125 |
pWavePoolTableHi = new uint32_t[WavePoolCount]; |
WavePoolHeaderSize = 8; |
1126 |
ptbl->SetPos(WavePoolHeaderSize); |
b64BitWavePoolOffsets = false; |
1127 |
|
} else { |
1128 |
// Check for 64 bit offsets (used in gig v3 files) |
WavePoolHeaderSize = ptbl->ReadUint32(); |
1129 |
b64BitWavePoolOffsets = (ptbl->GetSize() - WavePoolHeaderSize == WavePoolCount * 8); |
WavePoolCount = ptbl->ReadUint32(); |
1130 |
if (b64BitWavePoolOffsets) { |
pWavePoolTable = new uint32_t[WavePoolCount]; |
1131 |
for (int i = 0 ; i < WavePoolCount ; i++) { |
pWavePoolTableHi = new uint32_t[WavePoolCount]; |
1132 |
pWavePoolTableHi[i] = ptbl->ReadUint32(); |
ptbl->SetPos(WavePoolHeaderSize); |
1133 |
pWavePoolTable[i] = ptbl->ReadUint32(); |
|
1134 |
if (pWavePoolTable[i] & 0x80000000) |
// Check for 64 bit offsets (used in gig v3 files) |
1135 |
throw DLS::Exception("Files larger than 2 GB not yet supported"); |
b64BitWavePoolOffsets = (ptbl->GetSize() - WavePoolHeaderSize == WavePoolCount * 8); |
1136 |
|
if (b64BitWavePoolOffsets) { |
1137 |
|
for (int i = 0 ; i < WavePoolCount ; i++) { |
1138 |
|
pWavePoolTableHi[i] = ptbl->ReadUint32(); |
1139 |
|
pWavePoolTable[i] = ptbl->ReadUint32(); |
1140 |
|
if (pWavePoolTable[i] & 0x80000000) |
1141 |
|
throw DLS::Exception("Files larger than 2 GB not yet supported"); |
1142 |
|
} |
1143 |
|
} else { // conventional 32 bit offsets |
1144 |
|
ptbl->Read(pWavePoolTable, WavePoolCount, sizeof(uint32_t)); |
1145 |
|
for (int i = 0 ; i < WavePoolCount ; i++) pWavePoolTableHi[i] = 0; |
1146 |
} |
} |
|
} else { // conventional 32 bit offsets |
|
|
ptbl->Read(pWavePoolTable, WavePoolCount, sizeof(uint32_t)); |
|
|
for (int i = 0 ; i < WavePoolCount ; i++) pWavePoolTableHi[i] = 0; |
|
1147 |
} |
} |
1148 |
|
|
1149 |
pSamples = NULL; |
pSamples = NULL; |
1174 |
if (pWavePoolTable) delete[] pWavePoolTable; |
if (pWavePoolTable) delete[] pWavePoolTable; |
1175 |
if (pWavePoolTableHi) delete[] pWavePoolTableHi; |
if (pWavePoolTableHi) delete[] pWavePoolTableHi; |
1176 |
if (pVersion) delete pVersion; |
if (pVersion) delete pVersion; |
1177 |
|
for (std::list<RIFF::File*>::iterator i = ExtensionFiles.begin() ; i != ExtensionFiles.end() ; i++) |
1178 |
|
delete *i; |
1179 |
} |
} |
1180 |
|
|
1181 |
Sample* File::GetFirstSample() { |
Sample* File::GetFirstSample() { |
1192 |
} |
} |
1193 |
|
|
1194 |
void File::LoadSamples() { |
void File::LoadSamples() { |
1195 |
|
if (!pSamples) pSamples = new SampleList; |
1196 |
RIFF::List* wvpl = pRIFF->GetSubList(LIST_TYPE_WVPL); |
RIFF::List* wvpl = pRIFF->GetSubList(LIST_TYPE_WVPL); |
1197 |
if (wvpl) { |
if (wvpl) { |
1198 |
unsigned long wvplFileOffset = wvpl->GetFilePos(); |
unsigned long wvplFileOffset = wvpl->GetFilePos(); |
1199 |
RIFF::List* wave = wvpl->GetFirstSubList(); |
RIFF::List* wave = wvpl->GetFirstSubList(); |
1200 |
while (wave) { |
while (wave) { |
1201 |
if (wave->GetListType() == LIST_TYPE_WAVE) { |
if (wave->GetListType() == LIST_TYPE_WAVE) { |
|
if (!pSamples) pSamples = new SampleList; |
|
1202 |
unsigned long waveFileOffset = wave->GetFilePos(); |
unsigned long waveFileOffset = wave->GetFilePos(); |
1203 |
pSamples->push_back(new Sample(this, wave, waveFileOffset - wvplFileOffset)); |
pSamples->push_back(new Sample(this, wave, waveFileOffset - wvplFileOffset)); |
1204 |
} |
} |
1212 |
RIFF::List* wave = dwpl->GetFirstSubList(); |
RIFF::List* wave = dwpl->GetFirstSubList(); |
1213 |
while (wave) { |
while (wave) { |
1214 |
if (wave->GetListType() == LIST_TYPE_WAVE) { |
if (wave->GetListType() == LIST_TYPE_WAVE) { |
|
if (!pSamples) pSamples = new SampleList; |
|
1215 |
unsigned long waveFileOffset = wave->GetFilePos(); |
unsigned long waveFileOffset = wave->GetFilePos(); |
1216 |
pSamples->push_back(new Sample(this, wave, waveFileOffset - dwplFileOffset)); |
pSamples->push_back(new Sample(this, wave, waveFileOffset - dwplFileOffset)); |
1217 |
} |
} |
1229 |
* @returns pointer to new Sample object |
* @returns pointer to new Sample object |
1230 |
*/ |
*/ |
1231 |
Sample* File::AddSample() { |
Sample* File::AddSample() { |
1232 |
|
if (!pSamples) LoadSamples(); |
1233 |
__ensureMandatoryChunksExist(); |
__ensureMandatoryChunksExist(); |
1234 |
RIFF::List* wvpl = pRIFF->GetSubList(LIST_TYPE_WVPL); |
RIFF::List* wvpl = pRIFF->GetSubList(LIST_TYPE_WVPL); |
1235 |
// 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; |
|
1236 |
RIFF::List* wave = wvpl->AddSubList(LIST_TYPE_WAVE); |
RIFF::List* wave = wvpl->AddSubList(LIST_TYPE_WAVE); |
1237 |
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*/); |
1238 |
pSamples->push_back(pSample); |
pSamples->push_back(pSample); |
1247 |
* @param pSample - sample to delete |
* @param pSample - sample to delete |
1248 |
*/ |
*/ |
1249 |
void File::DeleteSample(Sample* pSample) { |
void File::DeleteSample(Sample* pSample) { |
1250 |
|
if (!pSamples) return; |
1251 |
SampleList::iterator iter = find(pSamples->begin(), pSamples->end(), pSample); |
SampleList::iterator iter = find(pSamples->begin(), pSamples->end(), pSample); |
1252 |
if (iter == pSamples->end()) return; |
if (iter == pSamples->end()) return; |
1253 |
pSamples->erase(iter); |
pSamples->erase(iter); |
1268 |
} |
} |
1269 |
|
|
1270 |
void File::LoadInstruments() { |
void File::LoadInstruments() { |
1271 |
|
if (!pInstruments) pInstruments = new InstrumentList; |
1272 |
RIFF::List* lstInstruments = pRIFF->GetSubList(LIST_TYPE_LINS); |
RIFF::List* lstInstruments = pRIFF->GetSubList(LIST_TYPE_LINS); |
1273 |
if (lstInstruments) { |
if (lstInstruments) { |
1274 |
RIFF::List* lstInstr = lstInstruments->GetFirstSubList(); |
RIFF::List* lstInstr = lstInstruments->GetFirstSubList(); |
1275 |
while (lstInstr) { |
while (lstInstr) { |
1276 |
if (lstInstr->GetListType() == LIST_TYPE_INS) { |
if (lstInstr->GetListType() == LIST_TYPE_INS) { |
|
if (!pInstruments) pInstruments = new InstrumentList; |
|
1277 |
pInstruments->push_back(new Instrument(this, lstInstr)); |
pInstruments->push_back(new Instrument(this, lstInstr)); |
1278 |
} |
} |
1279 |
lstInstr = lstInstruments->GetNextSubList(); |
lstInstr = lstInstruments->GetNextSubList(); |
1289 |
* @returns pointer to new Instrument object |
* @returns pointer to new Instrument object |
1290 |
*/ |
*/ |
1291 |
Instrument* File::AddInstrument() { |
Instrument* File::AddInstrument() { |
1292 |
|
if (!pInstruments) LoadInstruments(); |
1293 |
__ensureMandatoryChunksExist(); |
__ensureMandatoryChunksExist(); |
|
if (!pInstruments) pInstruments = new InstrumentList; |
|
1294 |
RIFF::List* lstInstruments = pRIFF->GetSubList(LIST_TYPE_LINS); |
RIFF::List* lstInstruments = pRIFF->GetSubList(LIST_TYPE_LINS); |
1295 |
RIFF::List* lstInstr = lstInstruments->AddSubList(LIST_TYPE_INS); |
RIFF::List* lstInstr = lstInstruments->AddSubList(LIST_TYPE_INS); |
1296 |
Instrument* pInstrument = new Instrument(this, lstInstr); |
Instrument* pInstrument = new Instrument(this, lstInstr); |
1298 |
return pInstrument; |
return pInstrument; |
1299 |
} |
} |
1300 |
|
|
1301 |
/** @brief Delete a instrument. |
/** @brief Delete an instrument. |
1302 |
* |
* |
1303 |
* This will delete the given Instrument object from the DLS file. You |
* This will delete the given Instrument object from the DLS file. You |
1304 |
* have to call Save() to make this persistent to the file. |
* have to call Save() to make this persistent to the file. |
1306 |
* @param pInstrument - instrument to delete |
* @param pInstrument - instrument to delete |
1307 |
*/ |
*/ |
1308 |
void File::DeleteInstrument(Instrument* pInstrument) { |
void File::DeleteInstrument(Instrument* pInstrument) { |
1309 |
|
if (!pInstruments) return; |
1310 |
InstrumentList::iterator iter = find(pInstruments->begin(), pInstruments->end(), pInstrument); |
InstrumentList::iterator iter = find(pInstruments->begin(), pInstruments->end(), pInstrument); |
1311 |
if (iter == pInstruments->end()) return; |
if (iter == pInstruments->end()) return; |
1312 |
pInstruments->erase(iter); |
pInstruments->erase(iter); |
1329 |
RIFF::Chunk* ckVersion = pRIFF->GetSubChunk(CHUNK_ID_VERS); |
RIFF::Chunk* ckVersion = pRIFF->GetSubChunk(CHUNK_ID_VERS); |
1330 |
if (!ckVersion) ckVersion = pRIFF->AddSubChunk(CHUNK_ID_VERS, 8); |
if (!ckVersion) ckVersion = pRIFF->AddSubChunk(CHUNK_ID_VERS, 8); |
1331 |
uint8_t* pData = (uint8_t*) ckVersion->LoadChunkData(); |
uint8_t* pData = (uint8_t*) ckVersion->LoadChunkData(); |
1332 |
memccpy(pData, pVersion, 2, 4); |
store16(&pData[0], pVersion->minor); |
1333 |
|
store16(&pData[2], pVersion->major); |
1334 |
|
store16(&pData[4], pVersion->build); |
1335 |
|
store16(&pData[6], pVersion->release); |
1336 |
} |
} |
1337 |
|
|
1338 |
// update 'colh' chunk |
// update 'colh' chunk |
1340 |
RIFF::Chunk* colh = pRIFF->GetSubChunk(CHUNK_ID_COLH); |
RIFF::Chunk* colh = pRIFF->GetSubChunk(CHUNK_ID_COLH); |
1341 |
if (!colh) colh = pRIFF->AddSubChunk(CHUNK_ID_COLH, 4); |
if (!colh) colh = pRIFF->AddSubChunk(CHUNK_ID_COLH, 4); |
1342 |
uint8_t* pData = (uint8_t*) colh->LoadChunkData(); |
uint8_t* pData = (uint8_t*) colh->LoadChunkData(); |
1343 |
memccpy(pData, &Instruments, 1, 4); |
store32(pData, Instruments); |
1344 |
|
|
1345 |
// update instrument's chunks |
// update instrument's chunks |
1346 |
if (pInstruments) { |
if (pInstruments) { |
1360 |
ptbl->Resize(iPtblSize); |
ptbl->Resize(iPtblSize); |
1361 |
pData = (uint8_t*) ptbl->LoadChunkData(); |
pData = (uint8_t*) ptbl->LoadChunkData(); |
1362 |
WavePoolCount = iSamples; |
WavePoolCount = iSamples; |
1363 |
memccpy(&pData[4], &WavePoolCount, 1, 4); |
store32(&pData[4], WavePoolCount); |
1364 |
// 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() |
1365 |
memset(&pData[WavePoolHeaderSize], 0, iPtblSize - WavePoolHeaderSize); |
memset(&pData[WavePoolHeaderSize], 0, iPtblSize - WavePoolHeaderSize); |
1366 |
|
|
1431 |
/** |
/** |
1432 |
* Updates (persistently) the wave pool table with offsets to all |
* Updates (persistently) the wave pool table with offsets to all |
1433 |
* currently available samples. <b>Caution:</b> this method assumes the |
* currently available samples. <b>Caution:</b> this method assumes the |
1434 |
* 'ptbl' chunk to be already of the correct size, so usually this |
* 'ptbl' chunk to be already of the correct size and the file to be |
1435 |
* method is only called after a Save() call. |
* writable, so usually this method is only called after a Save() call. |
1436 |
* |
* |
1437 |
* @throws Exception - if 'ptbl' chunk is too small (should only occur |
* @throws Exception - if 'ptbl' chunk is too small (should only occur |
1438 |
* if there's a bug) |
* if there's a bug) |
1442 |
RIFF::Chunk* ptbl = pRIFF->GetSubChunk(CHUNK_ID_PTBL); |
RIFF::Chunk* ptbl = pRIFF->GetSubChunk(CHUNK_ID_PTBL); |
1443 |
const int iOffsetSize = (b64BitWavePoolOffsets) ? 8 : 4; |
const int iOffsetSize = (b64BitWavePoolOffsets) ? 8 : 4; |
1444 |
// check if 'ptbl' chunk is large enough |
// check if 'ptbl' chunk is large enough |
1445 |
WavePoolCount = pSamples->size(); |
WavePoolCount = (pSamples) ? pSamples->size() : 0; |
1446 |
const unsigned long ulRequiredSize = WavePoolHeaderSize + iOffsetSize * WavePoolCount; |
const unsigned long ulRequiredSize = WavePoolHeaderSize + iOffsetSize * WavePoolCount; |
1447 |
if (ptbl->GetSize() < ulRequiredSize) throw Exception("Fatal error, 'ptbl' chunk too small"); |
if (ptbl->GetSize() < ulRequiredSize) throw Exception("Fatal error, 'ptbl' chunk too small"); |
1448 |
uint8_t* pData = (uint8_t*) ptbl->LoadChunkData(); |
// save the 'ptbl' chunk's current read/write position |
1449 |
|
unsigned long ulOriginalPos = ptbl->GetPos(); |
1450 |
// update headers |
// update headers |
1451 |
memccpy(&pData[0], &WavePoolHeaderSize, 1, 4); |
ptbl->SetPos(0); |
1452 |
memccpy(&pData[4], &WavePoolCount, 1, 4); |
uint32_t tmp = WavePoolHeaderSize; |
1453 |
|
ptbl->WriteUint32(&tmp); |
1454 |
|
tmp = WavePoolCount; |
1455 |
|
ptbl->WriteUint32(&tmp); |
1456 |
// update offsets |
// update offsets |
1457 |
|
ptbl->SetPos(WavePoolHeaderSize); |
1458 |
if (b64BitWavePoolOffsets) { |
if (b64BitWavePoolOffsets) { |
1459 |
for (int i = 0 ; i < WavePoolCount ; i++) { |
for (int i = 0 ; i < WavePoolCount ; i++) { |
1460 |
memccpy(&pData[WavePoolHeaderSize + i*iOffsetSize], &pWavePoolTableHi[i], 1, 4); |
tmp = pWavePoolTableHi[i]; |
1461 |
memccpy(&pData[WavePoolHeaderSize + i*iOffsetSize], &pWavePoolTable[i], 1, 4); |
ptbl->WriteUint32(&tmp); |
1462 |
|
tmp = pWavePoolTable[i]; |
1463 |
|
ptbl->WriteUint32(&tmp); |
1464 |
} |
} |
1465 |
} else { // conventional 32 bit offsets |
} else { // conventional 32 bit offsets |
1466 |
for (int i = 0 ; i < WavePoolCount ; i++) |
for (int i = 0 ; i < WavePoolCount ; i++) { |
1467 |
memccpy(&pData[WavePoolHeaderSize + i*iOffsetSize], &pWavePoolTable[i], 1, 4); |
tmp = pWavePoolTable[i]; |
1468 |
|
ptbl->WriteUint32(&tmp); |
1469 |
|
} |
1470 |
} |
} |
1471 |
|
// restore 'ptbl' chunk's original read/write position |
1472 |
|
ptbl->SetPos(ulOriginalPos); |
1473 |
} |
} |
1474 |
|
|
1475 |
/** |
/** |
1478 |
* exists already. |
* exists already. |
1479 |
*/ |
*/ |
1480 |
void File::__UpdateWavePoolTable() { |
void File::__UpdateWavePoolTable() { |
1481 |
WavePoolCount = pSamples->size(); |
WavePoolCount = (pSamples) ? pSamples->size() : 0; |
1482 |
// resize wave pool table arrays |
// resize wave pool table arrays |
1483 |
if (pWavePoolTable) delete[] pWavePoolTable; |
if (pWavePoolTable) delete[] pWavePoolTable; |
1484 |
if (pWavePoolTableHi) delete[] pWavePoolTableHi; |
if (pWavePoolTableHi) delete[] pWavePoolTableHi; |
1485 |
pWavePoolTable = new uint32_t[WavePoolCount]; |
pWavePoolTable = new uint32_t[WavePoolCount]; |
1486 |
pWavePoolTableHi = new uint32_t[WavePoolCount]; |
pWavePoolTableHi = new uint32_t[WavePoolCount]; |
1487 |
|
if (!pSamples) return; |
1488 |
// update offsets int wave pool table |
// update offsets int wave pool table |
1489 |
RIFF::List* wvpl = pRIFF->GetSubList(LIST_TYPE_WVPL); |
RIFF::List* wvpl = pRIFF->GetSubList(LIST_TYPE_WVPL); |
1490 |
uint64_t wvplFileOffset = wvpl->GetFilePos(); |
uint64_t wvplFileOffset = wvpl->GetFilePos(); |
1492 |
SampleList::iterator iter = pSamples->begin(); |
SampleList::iterator iter = pSamples->begin(); |
1493 |
SampleList::iterator end = pSamples->end(); |
SampleList::iterator end = pSamples->end(); |
1494 |
for (int i = 0 ; iter != end ; ++iter, i++) { |
for (int i = 0 ; iter != end ; ++iter, i++) { |
1495 |
uint64_t _64BitOffset = wvplFileOffset - (*iter)->pWaveList->GetFilePos() - LIST_HEADER_SIZE; |
uint64_t _64BitOffset = (*iter)->pWaveList->GetFilePos() - wvplFileOffset - LIST_HEADER_SIZE; |
1496 |
(*iter)->ulWavePoolOffset = _64BitOffset; |
(*iter)->ulWavePoolOffset = _64BitOffset; |
1497 |
pWavePoolTableHi[i] = (uint32_t) (_64BitOffset >> 32); |
pWavePoolTableHi[i] = (uint32_t) (_64BitOffset >> 32); |
1498 |
pWavePoolTable[i] = (uint32_t) _64BitOffset; |
pWavePoolTable[i] = (uint32_t) _64BitOffset; |
1501 |
SampleList::iterator iter = pSamples->begin(); |
SampleList::iterator iter = pSamples->begin(); |
1502 |
SampleList::iterator end = pSamples->end(); |
SampleList::iterator end = pSamples->end(); |
1503 |
for (int i = 0 ; iter != end ; ++iter, i++) { |
for (int i = 0 ; iter != end ; ++iter, i++) { |
1504 |
uint64_t _64BitOffset = wvplFileOffset - (*iter)->pWaveList->GetFilePos() - LIST_HEADER_SIZE; |
uint64_t _64BitOffset = (*iter)->pWaveList->GetFilePos() - wvplFileOffset - LIST_HEADER_SIZE; |
1505 |
(*iter)->ulWavePoolOffset = _64BitOffset; |
(*iter)->ulWavePoolOffset = _64BitOffset; |
1506 |
pWavePoolTable[i] = (uint32_t) _64BitOffset; |
pWavePoolTable[i] = (uint32_t) _64BitOffset; |
1507 |
} |
} |