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-2013 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 * |
23 |
|
|
24 |
#include "gig.h" |
#include "gig.h" |
25 |
|
|
26 |
|
#include "helper.h" |
27 |
|
|
28 |
|
#include <algorithm> |
29 |
|
#include <math.h> |
30 |
#include <iostream> |
#include <iostream> |
31 |
|
|
32 |
namespace gig { namespace { |
/// Initial size of the sample buffer which is used for decompression of |
33 |
|
/// compressed sample wave streams - this value should always be bigger than |
34 |
|
/// the biggest sample piece expected to be read by the sampler engine, |
35 |
|
/// otherwise the buffer size will be raised at runtime and thus the buffer |
36 |
|
/// reallocated which is time consuming and unefficient. |
37 |
|
#define INITIAL_SAMPLE_BUFFER_SIZE 512000 // 512 kB |
38 |
|
|
39 |
|
/** (so far) every exponential paramater in the gig format has a basis of 1.000000008813822 */ |
40 |
|
#define GIG_EXP_DECODE(x) (pow(1.000000008813822, x)) |
41 |
|
#define GIG_EXP_ENCODE(x) (log(x) / log(1.000000008813822)) |
42 |
|
#define GIG_PITCH_TRACK_EXTRACT(x) (!(x & 0x01)) |
43 |
|
#define GIG_PITCH_TRACK_ENCODE(x) ((x) ? 0x00 : 0x01) |
44 |
|
#define GIG_VCF_RESONANCE_CTRL_EXTRACT(x) ((x >> 4) & 0x03) |
45 |
|
#define GIG_VCF_RESONANCE_CTRL_ENCODE(x) ((x & 0x03) << 4) |
46 |
|
#define GIG_EG_CTR_ATTACK_INFLUENCE_EXTRACT(x) ((x >> 1) & 0x03) |
47 |
|
#define GIG_EG_CTR_DECAY_INFLUENCE_EXTRACT(x) ((x >> 3) & 0x03) |
48 |
|
#define GIG_EG_CTR_RELEASE_INFLUENCE_EXTRACT(x) ((x >> 5) & 0x03) |
49 |
|
#define GIG_EG_CTR_ATTACK_INFLUENCE_ENCODE(x) ((x & 0x03) << 1) |
50 |
|
#define GIG_EG_CTR_DECAY_INFLUENCE_ENCODE(x) ((x & 0x03) << 3) |
51 |
|
#define GIG_EG_CTR_RELEASE_INFLUENCE_ENCODE(x) ((x & 0x03) << 5) |
52 |
|
|
53 |
|
namespace gig { |
54 |
|
|
55 |
|
// *************** progress_t *************** |
56 |
|
// * |
57 |
|
|
58 |
|
progress_t::progress_t() { |
59 |
|
callback = NULL; |
60 |
|
custom = NULL; |
61 |
|
__range_min = 0.0f; |
62 |
|
__range_max = 1.0f; |
63 |
|
} |
64 |
|
|
65 |
|
// private helper function to convert progress of a subprocess into the global progress |
66 |
|
static void __notify_progress(progress_t* pProgress, float subprogress) { |
67 |
|
if (pProgress && pProgress->callback) { |
68 |
|
const float totalrange = pProgress->__range_max - pProgress->__range_min; |
69 |
|
const float totalprogress = pProgress->__range_min + subprogress * totalrange; |
70 |
|
pProgress->factor = totalprogress; |
71 |
|
pProgress->callback(pProgress); // now actually notify about the progress |
72 |
|
} |
73 |
|
} |
74 |
|
|
75 |
|
// private helper function to divide a progress into subprogresses |
76 |
|
static void __divide_progress(progress_t* pParentProgress, progress_t* pSubProgress, float totalTasks, float currentTask) { |
77 |
|
if (pParentProgress && pParentProgress->callback) { |
78 |
|
const float totalrange = pParentProgress->__range_max - pParentProgress->__range_min; |
79 |
|
pSubProgress->callback = pParentProgress->callback; |
80 |
|
pSubProgress->custom = pParentProgress->custom; |
81 |
|
pSubProgress->__range_min = pParentProgress->__range_min + totalrange * currentTask / totalTasks; |
82 |
|
pSubProgress->__range_max = pSubProgress->__range_min + totalrange / totalTasks; |
83 |
|
} |
84 |
|
} |
85 |
|
|
86 |
|
|
87 |
// *************** Internal functions for sample decopmression *************** |
// *************** Internal functions for sample decompression *************** |
88 |
// * |
// * |
89 |
|
|
90 |
|
namespace { |
91 |
|
|
92 |
inline int get12lo(const unsigned char* pSrc) |
inline int get12lo(const unsigned char* pSrc) |
93 |
{ |
{ |
94 |
const int x = pSrc[0] | (pSrc[1] & 0x0f) << 8; |
const int x = pSrc[0] | (pSrc[1] & 0x0f) << 8; |
112 |
return x & 0x800000 ? x - 0x1000000 : x; |
return x & 0x800000 ? x - 0x1000000 : x; |
113 |
} |
} |
114 |
|
|
115 |
|
inline void store24(unsigned char* pDst, int x) |
116 |
|
{ |
117 |
|
pDst[0] = x; |
118 |
|
pDst[1] = x >> 8; |
119 |
|
pDst[2] = x >> 16; |
120 |
|
} |
121 |
|
|
122 |
void Decompress16(int compressionmode, const unsigned char* params, |
void Decompress16(int compressionmode, const unsigned char* params, |
123 |
int srcStep, int dstStep, |
int srcStep, int dstStep, |
124 |
const unsigned char* pSrc, int16_t* pDst, |
const unsigned char* pSrc, int16_t* pDst, |
158 |
} |
} |
159 |
|
|
160 |
void Decompress24(int compressionmode, const unsigned char* params, |
void Decompress24(int compressionmode, const unsigned char* params, |
161 |
int dstStep, const unsigned char* pSrc, int16_t* pDst, |
int dstStep, const unsigned char* pSrc, uint8_t* pDst, |
162 |
unsigned long currentframeoffset, |
unsigned long currentframeoffset, |
163 |
unsigned long copysamples, int truncatedBits) |
unsigned long copysamples, int truncatedBits) |
164 |
{ |
{ |
165 |
// Note: The 24 bits are truncated to 16 bits for now. |
int y, dy, ddy, dddy; |
166 |
|
|
167 |
// Note: The calculation of the initial value of y is strange |
#define GET_PARAMS(params) \ |
168 |
// and not 100% correct. What should the first two parameters |
y = get24(params); \ |
169 |
// really be used for? Why are they two? The correct value for |
dy = y - get24((params) + 3); \ |
170 |
// y seems to lie somewhere between the values of the first |
ddy = get24((params) + 6); \ |
171 |
// two parameters. |
dddy = get24((params) + 9) |
|
// |
|
|
// Strange thing #2: The formula in SKIP_ONE gives values for |
|
|
// y that are twice as high as they should be. That's why |
|
|
// COPY_ONE shifts an extra step, and also why y is |
|
|
// initialized with a sum instead of a mean value. |
|
|
|
|
|
int y, dy, ddy; |
|
|
|
|
|
const int shift = 8 - truncatedBits; |
|
|
const int shift1 = shift + 1; |
|
|
|
|
|
#define GET_PARAMS(params) \ |
|
|
y = (get24(params) + get24((params) + 3)); \ |
|
|
dy = get24((params) + 6); \ |
|
|
ddy = get24((params) + 9) |
|
172 |
|
|
173 |
#define SKIP_ONE(x) \ |
#define SKIP_ONE(x) \ |
174 |
ddy -= (x); \ |
dddy -= (x); \ |
175 |
dy -= ddy; \ |
ddy -= dddy; \ |
176 |
y -= dy |
dy = -dy - ddy; \ |
177 |
|
y += dy |
178 |
|
|
179 |
#define COPY_ONE(x) \ |
#define COPY_ONE(x) \ |
180 |
SKIP_ONE(x); \ |
SKIP_ONE(x); \ |
181 |
*pDst = y >> shift1; \ |
store24(pDst, y << truncatedBits); \ |
182 |
pDst += dstStep |
pDst += dstStep |
183 |
|
|
184 |
switch (compressionmode) { |
switch (compressionmode) { |
185 |
case 2: // 24 bit uncompressed |
case 2: // 24 bit uncompressed |
186 |
pSrc += currentframeoffset * 3; |
pSrc += currentframeoffset * 3; |
187 |
while (copysamples) { |
while (copysamples) { |
188 |
*pDst = get24(pSrc) >> shift; |
store24(pDst, get24(pSrc) << truncatedBits); |
189 |
pDst += dstStep; |
pDst += dstStep; |
190 |
pSrc += 3; |
pSrc += 3; |
191 |
copysamples--; |
copysamples--; |
255 |
} |
} |
256 |
|
|
257 |
|
|
258 |
|
|
259 |
|
// *************** Internal CRC-32 (Cyclic Redundancy Check) functions *************** |
260 |
|
// * |
261 |
|
|
262 |
|
static uint32_t* __initCRCTable() { |
263 |
|
static uint32_t res[256]; |
264 |
|
|
265 |
|
for (int i = 0 ; i < 256 ; i++) { |
266 |
|
uint32_t c = i; |
267 |
|
for (int j = 0 ; j < 8 ; j++) { |
268 |
|
c = (c & 1) ? 0xedb88320 ^ (c >> 1) : c >> 1; |
269 |
|
} |
270 |
|
res[i] = c; |
271 |
|
} |
272 |
|
return res; |
273 |
|
} |
274 |
|
|
275 |
|
static const uint32_t* __CRCTable = __initCRCTable(); |
276 |
|
|
277 |
|
/** |
278 |
|
* Initialize a CRC variable. |
279 |
|
* |
280 |
|
* @param crc - variable to be initialized |
281 |
|
*/ |
282 |
|
inline static void __resetCRC(uint32_t& crc) { |
283 |
|
crc = 0xffffffff; |
284 |
|
} |
285 |
|
|
286 |
|
/** |
287 |
|
* Used to calculate checksums of the sample data in a gig file. The |
288 |
|
* checksums are stored in the 3crc chunk of the gig file and |
289 |
|
* automatically updated when a sample is written with Sample::Write(). |
290 |
|
* |
291 |
|
* One should call __resetCRC() to initialize the CRC variable to be |
292 |
|
* used before calling this function the first time. |
293 |
|
* |
294 |
|
* After initializing the CRC variable one can call this function |
295 |
|
* arbitrary times, i.e. to split the overall CRC calculation into |
296 |
|
* steps. |
297 |
|
* |
298 |
|
* Once the whole data was processed by __calculateCRC(), one should |
299 |
|
* call __encodeCRC() to get the final CRC result. |
300 |
|
* |
301 |
|
* @param buf - pointer to data the CRC shall be calculated of |
302 |
|
* @param bufSize - size of the data to be processed |
303 |
|
* @param crc - variable the CRC sum shall be stored to |
304 |
|
*/ |
305 |
|
static void __calculateCRC(unsigned char* buf, int bufSize, uint32_t& crc) { |
306 |
|
for (int i = 0 ; i < bufSize ; i++) { |
307 |
|
crc = __CRCTable[(crc ^ buf[i]) & 0xff] ^ (crc >> 8); |
308 |
|
} |
309 |
|
} |
310 |
|
|
311 |
|
/** |
312 |
|
* Returns the final CRC result. |
313 |
|
* |
314 |
|
* @param crc - variable previously passed to __calculateCRC() |
315 |
|
*/ |
316 |
|
inline static uint32_t __encodeCRC(const uint32_t& crc) { |
317 |
|
return crc ^ 0xffffffff; |
318 |
|
} |
319 |
|
|
320 |
|
|
321 |
|
|
322 |
|
// *************** Other Internal functions *************** |
323 |
|
// * |
324 |
|
|
325 |
|
static split_type_t __resolveSplitType(dimension_t dimension) { |
326 |
|
return ( |
327 |
|
dimension == dimension_layer || |
328 |
|
dimension == dimension_samplechannel || |
329 |
|
dimension == dimension_releasetrigger || |
330 |
|
dimension == dimension_keyboard || |
331 |
|
dimension == dimension_roundrobin || |
332 |
|
dimension == dimension_random || |
333 |
|
dimension == dimension_smartmidi || |
334 |
|
dimension == dimension_roundrobinkeyboard |
335 |
|
) ? split_type_bit : split_type_normal; |
336 |
|
} |
337 |
|
|
338 |
|
static int __resolveZoneSize(dimension_def_t& dimension_definition) { |
339 |
|
return (dimension_definition.split_type == split_type_normal) |
340 |
|
? int(128.0 / dimension_definition.zones) : 0; |
341 |
|
} |
342 |
|
|
343 |
|
|
344 |
|
|
345 |
// *************** Sample *************** |
// *************** Sample *************** |
346 |
// * |
// * |
347 |
|
|
348 |
unsigned int Sample::Instances = 0; |
unsigned int Sample::Instances = 0; |
349 |
buffer_t Sample::InternalDecompressionBuffer; |
buffer_t Sample::InternalDecompressionBuffer; |
350 |
|
|
351 |
Sample::Sample(File* pFile, RIFF::List* waveList, unsigned long WavePoolOffset) : DLS::Sample((DLS::File*) pFile, waveList, WavePoolOffset) { |
/** @brief Constructor. |
352 |
|
* |
353 |
|
* Load an existing sample or create a new one. A 'wave' list chunk must |
354 |
|
* be given to this constructor. In case the given 'wave' list chunk |
355 |
|
* contains a 'fmt', 'data' (and optionally a '3gix', 'smpl') chunk, the |
356 |
|
* format and sample data will be loaded from there, otherwise default |
357 |
|
* values will be used and those chunks will be created when |
358 |
|
* File::Save() will be called later on. |
359 |
|
* |
360 |
|
* @param pFile - pointer to gig::File where this sample is |
361 |
|
* located (or will be located) |
362 |
|
* @param waveList - pointer to 'wave' list chunk which is (or |
363 |
|
* will be) associated with this sample |
364 |
|
* @param WavePoolOffset - offset of this sample data from wave pool |
365 |
|
* ('wvpl') list chunk |
366 |
|
* @param fileNo - number of an extension file where this sample |
367 |
|
* is located, 0 otherwise |
368 |
|
*/ |
369 |
|
Sample::Sample(File* pFile, RIFF::List* waveList, unsigned long WavePoolOffset, unsigned long fileNo) : DLS::Sample((DLS::File*) pFile, waveList, WavePoolOffset) { |
370 |
|
static const DLS::Info::string_length_t fixedStringLengths[] = { |
371 |
|
{ CHUNK_ID_INAM, 64 }, |
372 |
|
{ 0, 0 } |
373 |
|
}; |
374 |
|
pInfo->SetFixedStringLengths(fixedStringLengths); |
375 |
Instances++; |
Instances++; |
376 |
|
FileNo = fileNo; |
377 |
|
|
378 |
RIFF::Chunk* _3gix = waveList->GetSubChunk(CHUNK_ID_3GIX); |
__resetCRC(crc); |
379 |
if (!_3gix) throw gig::Exception("Mandatory chunks in <wave> list chunk not found."); |
|
380 |
SampleGroup = _3gix->ReadInt16(); |
pCk3gix = waveList->GetSubChunk(CHUNK_ID_3GIX); |
381 |
|
if (pCk3gix) { |
382 |
RIFF::Chunk* smpl = waveList->GetSubChunk(CHUNK_ID_SMPL); |
uint16_t iSampleGroup = pCk3gix->ReadInt16(); |
383 |
if (!smpl) throw gig::Exception("Mandatory chunks in <wave> list chunk not found."); |
pGroup = pFile->GetGroup(iSampleGroup); |
384 |
Manufacturer = smpl->ReadInt32(); |
} else { // '3gix' chunk missing |
385 |
Product = smpl->ReadInt32(); |
// by default assigned to that mandatory "Default Group" |
386 |
SamplePeriod = smpl->ReadInt32(); |
pGroup = pFile->GetGroup(0); |
387 |
MIDIUnityNote = smpl->ReadInt32(); |
} |
388 |
FineTune = smpl->ReadInt32(); |
|
389 |
smpl->Read(&SMPTEFormat, 1, 4); |
pCkSmpl = waveList->GetSubChunk(CHUNK_ID_SMPL); |
390 |
SMPTEOffset = smpl->ReadInt32(); |
if (pCkSmpl) { |
391 |
Loops = smpl->ReadInt32(); |
Manufacturer = pCkSmpl->ReadInt32(); |
392 |
smpl->ReadInt32(); // manufByt |
Product = pCkSmpl->ReadInt32(); |
393 |
LoopID = smpl->ReadInt32(); |
SamplePeriod = pCkSmpl->ReadInt32(); |
394 |
smpl->Read(&LoopType, 1, 4); |
MIDIUnityNote = pCkSmpl->ReadInt32(); |
395 |
LoopStart = smpl->ReadInt32(); |
FineTune = pCkSmpl->ReadInt32(); |
396 |
LoopEnd = smpl->ReadInt32(); |
pCkSmpl->Read(&SMPTEFormat, 1, 4); |
397 |
LoopFraction = smpl->ReadInt32(); |
SMPTEOffset = pCkSmpl->ReadInt32(); |
398 |
LoopPlayCount = smpl->ReadInt32(); |
Loops = pCkSmpl->ReadInt32(); |
399 |
|
pCkSmpl->ReadInt32(); // manufByt |
400 |
|
LoopID = pCkSmpl->ReadInt32(); |
401 |
|
pCkSmpl->Read(&LoopType, 1, 4); |
402 |
|
LoopStart = pCkSmpl->ReadInt32(); |
403 |
|
LoopEnd = pCkSmpl->ReadInt32(); |
404 |
|
LoopFraction = pCkSmpl->ReadInt32(); |
405 |
|
LoopPlayCount = pCkSmpl->ReadInt32(); |
406 |
|
} else { // 'smpl' chunk missing |
407 |
|
// use default values |
408 |
|
Manufacturer = 0; |
409 |
|
Product = 0; |
410 |
|
SamplePeriod = uint32_t(1000000000.0 / SamplesPerSecond + 0.5); |
411 |
|
MIDIUnityNote = 60; |
412 |
|
FineTune = 0; |
413 |
|
SMPTEFormat = smpte_format_no_offset; |
414 |
|
SMPTEOffset = 0; |
415 |
|
Loops = 0; |
416 |
|
LoopID = 0; |
417 |
|
LoopType = loop_type_normal; |
418 |
|
LoopStart = 0; |
419 |
|
LoopEnd = 0; |
420 |
|
LoopFraction = 0; |
421 |
|
LoopPlayCount = 0; |
422 |
|
} |
423 |
|
|
424 |
FrameTable = NULL; |
FrameTable = NULL; |
425 |
SamplePos = 0; |
SamplePos = 0; |
450 |
} |
} |
451 |
FrameOffset = 0; // just for streaming compressed samples |
FrameOffset = 0; // just for streaming compressed samples |
452 |
|
|
453 |
LoopSize = LoopEnd - LoopStart; |
LoopSize = LoopEnd - LoopStart + 1; |
454 |
|
} |
455 |
|
|
456 |
|
/** |
457 |
|
* Apply sample and its settings to the respective RIFF chunks. You have |
458 |
|
* to call File::Save() to make changes persistent. |
459 |
|
* |
460 |
|
* Usually there is absolutely no need to call this method explicitly. |
461 |
|
* It will be called automatically when File::Save() was called. |
462 |
|
* |
463 |
|
* @throws DLS::Exception if FormatTag != DLS_WAVE_FORMAT_PCM or no sample data |
464 |
|
* was provided yet |
465 |
|
* @throws gig::Exception if there is any invalid sample setting |
466 |
|
*/ |
467 |
|
void Sample::UpdateChunks() { |
468 |
|
// first update base class's chunks |
469 |
|
DLS::Sample::UpdateChunks(); |
470 |
|
|
471 |
|
// make sure 'smpl' chunk exists |
472 |
|
pCkSmpl = pWaveList->GetSubChunk(CHUNK_ID_SMPL); |
473 |
|
if (!pCkSmpl) { |
474 |
|
pCkSmpl = pWaveList->AddSubChunk(CHUNK_ID_SMPL, 60); |
475 |
|
memset(pCkSmpl->LoadChunkData(), 0, 60); |
476 |
|
} |
477 |
|
// update 'smpl' chunk |
478 |
|
uint8_t* pData = (uint8_t*) pCkSmpl->LoadChunkData(); |
479 |
|
SamplePeriod = uint32_t(1000000000.0 / SamplesPerSecond + 0.5); |
480 |
|
store32(&pData[0], Manufacturer); |
481 |
|
store32(&pData[4], Product); |
482 |
|
store32(&pData[8], SamplePeriod); |
483 |
|
store32(&pData[12], MIDIUnityNote); |
484 |
|
store32(&pData[16], FineTune); |
485 |
|
store32(&pData[20], SMPTEFormat); |
486 |
|
store32(&pData[24], SMPTEOffset); |
487 |
|
store32(&pData[28], Loops); |
488 |
|
|
489 |
|
// we skip 'manufByt' for now (4 bytes) |
490 |
|
|
491 |
|
store32(&pData[36], LoopID); |
492 |
|
store32(&pData[40], LoopType); |
493 |
|
store32(&pData[44], LoopStart); |
494 |
|
store32(&pData[48], LoopEnd); |
495 |
|
store32(&pData[52], LoopFraction); |
496 |
|
store32(&pData[56], LoopPlayCount); |
497 |
|
|
498 |
|
// make sure '3gix' chunk exists |
499 |
|
pCk3gix = pWaveList->GetSubChunk(CHUNK_ID_3GIX); |
500 |
|
if (!pCk3gix) pCk3gix = pWaveList->AddSubChunk(CHUNK_ID_3GIX, 4); |
501 |
|
// determine appropriate sample group index (to be stored in chunk) |
502 |
|
uint16_t iSampleGroup = 0; // 0 refers to default sample group |
503 |
|
File* pFile = static_cast<File*>(pParent); |
504 |
|
if (pFile->pGroups) { |
505 |
|
std::list<Group*>::iterator iter = pFile->pGroups->begin(); |
506 |
|
std::list<Group*>::iterator end = pFile->pGroups->end(); |
507 |
|
for (int i = 0; iter != end; i++, iter++) { |
508 |
|
if (*iter == pGroup) { |
509 |
|
iSampleGroup = i; |
510 |
|
break; // found |
511 |
|
} |
512 |
|
} |
513 |
|
} |
514 |
|
// update '3gix' chunk |
515 |
|
pData = (uint8_t*) pCk3gix->LoadChunkData(); |
516 |
|
store16(&pData[0], iSampleGroup); |
517 |
} |
} |
518 |
|
|
519 |
/// Scans compressed samples for mandatory informations (e.g. actual number of total sample points). |
/// Scans compressed samples for mandatory informations (e.g. actual number of total sample points). |
677 |
if (SampleCount > this->SamplesTotal) SampleCount = this->SamplesTotal; |
if (SampleCount > this->SamplesTotal) SampleCount = this->SamplesTotal; |
678 |
if (RAMCache.pStart) delete[] (int8_t*) RAMCache.pStart; |
if (RAMCache.pStart) delete[] (int8_t*) RAMCache.pStart; |
679 |
unsigned long allocationsize = (SampleCount + NullSamplesCount) * this->FrameSize; |
unsigned long allocationsize = (SampleCount + NullSamplesCount) * this->FrameSize; |
680 |
|
SetPos(0); // reset read position to begin of sample |
681 |
RAMCache.pStart = new int8_t[allocationsize]; |
RAMCache.pStart = new int8_t[allocationsize]; |
682 |
RAMCache.Size = Read(RAMCache.pStart, SampleCount) * this->FrameSize; |
RAMCache.Size = Read(RAMCache.pStart, SampleCount) * this->FrameSize; |
683 |
RAMCache.NullExtensionSize = allocationsize - RAMCache.Size; |
RAMCache.NullExtensionSize = allocationsize - RAMCache.Size; |
715 |
if (RAMCache.pStart) delete[] (int8_t*) RAMCache.pStart; |
if (RAMCache.pStart) delete[] (int8_t*) RAMCache.pStart; |
716 |
RAMCache.pStart = NULL; |
RAMCache.pStart = NULL; |
717 |
RAMCache.Size = 0; |
RAMCache.Size = 0; |
718 |
|
RAMCache.NullExtensionSize = 0; |
719 |
|
} |
720 |
|
|
721 |
|
/** @brief Resize sample. |
722 |
|
* |
723 |
|
* Resizes the sample's wave form data, that is the actual size of |
724 |
|
* sample wave data possible to be written for this sample. This call |
725 |
|
* will return immediately and just schedule the resize operation. You |
726 |
|
* should call File::Save() to actually perform the resize operation(s) |
727 |
|
* "physically" to the file. As this can take a while on large files, it |
728 |
|
* is recommended to call Resize() first on all samples which have to be |
729 |
|
* resized and finally to call File::Save() to perform all those resize |
730 |
|
* operations in one rush. |
731 |
|
* |
732 |
|
* The actual size (in bytes) is dependant to the current FrameSize |
733 |
|
* value. You may want to set FrameSize before calling Resize(). |
734 |
|
* |
735 |
|
* <b>Caution:</b> You cannot directly write (i.e. with Write()) to |
736 |
|
* enlarged samples before calling File::Save() as this might exceed the |
737 |
|
* current sample's boundary! |
738 |
|
* |
739 |
|
* Also note: only DLS_WAVE_FORMAT_PCM is currently supported, that is |
740 |
|
* FormatTag must be DLS_WAVE_FORMAT_PCM. Trying to resize samples with |
741 |
|
* other formats will fail! |
742 |
|
* |
743 |
|
* @param iNewSize - new sample wave data size in sample points (must be |
744 |
|
* greater than zero) |
745 |
|
* @throws DLS::Excecption if FormatTag != DLS_WAVE_FORMAT_PCM |
746 |
|
* or if \a iNewSize is less than 1 |
747 |
|
* @throws gig::Exception if existing sample is compressed |
748 |
|
* @see DLS::Sample::GetSize(), DLS::Sample::FrameSize, |
749 |
|
* DLS::Sample::FormatTag, File::Save() |
750 |
|
*/ |
751 |
|
void Sample::Resize(int iNewSize) { |
752 |
|
if (Compressed) throw gig::Exception("There is no support for modifying compressed samples (yet)"); |
753 |
|
DLS::Sample::Resize(iNewSize); |
754 |
} |
} |
755 |
|
|
756 |
/** |
/** |
842 |
* @param SampleCount number of sample points to read |
* @param SampleCount number of sample points to read |
843 |
* @param pPlaybackState will be used to store and reload the playback |
* @param pPlaybackState will be used to store and reload the playback |
844 |
* state for the next ReadAndLoop() call |
* state for the next ReadAndLoop() call |
845 |
|
* @param pDimRgn dimension region with looping information |
846 |
* @param pExternalDecompressionBuffer (optional) external buffer to use for decompression |
* @param pExternalDecompressionBuffer (optional) external buffer to use for decompression |
847 |
* @returns number of successfully read sample points |
* @returns number of successfully read sample points |
848 |
* @see CreateDecompressionBuffer() |
* @see CreateDecompressionBuffer() |
849 |
*/ |
*/ |
850 |
unsigned long Sample::ReadAndLoop(void* pBuffer, unsigned long SampleCount, playback_state_t* pPlaybackState, buffer_t* pExternalDecompressionBuffer) { |
unsigned long Sample::ReadAndLoop(void* pBuffer, unsigned long SampleCount, playback_state_t* pPlaybackState, |
851 |
|
DimensionRegion* pDimRgn, buffer_t* pExternalDecompressionBuffer) { |
852 |
unsigned long samplestoread = SampleCount, totalreadsamples = 0, readsamples, samplestoloopend; |
unsigned long samplestoread = SampleCount, totalreadsamples = 0, readsamples, samplestoloopend; |
853 |
uint8_t* pDst = (uint8_t*) pBuffer; |
uint8_t* pDst = (uint8_t*) pBuffer; |
854 |
|
|
855 |
SetPos(pPlaybackState->position); // recover position from the last time |
SetPos(pPlaybackState->position); // recover position from the last time |
856 |
|
|
857 |
if (this->Loops && GetPos() <= this->LoopEnd) { // honor looping if there are loop points defined |
if (pDimRgn->SampleLoops) { // honor looping if there are loop points defined |
858 |
|
|
859 |
switch (this->LoopType) { |
const DLS::sample_loop_t& loop = pDimRgn->pSampleLoops[0]; |
860 |
|
const uint32_t loopEnd = loop.LoopStart + loop.LoopLength; |
861 |
|
|
862 |
case loop_type_bidirectional: { //TODO: not tested yet! |
if (GetPos() <= loopEnd) { |
863 |
do { |
switch (loop.LoopType) { |
|
// if not endless loop check if max. number of loop cycles have been passed |
|
|
if (this->LoopPlayCount && !pPlaybackState->loop_cycles_left) break; |
|
|
|
|
|
if (!pPlaybackState->reverse) { // forward playback |
|
|
do { |
|
|
samplestoloopend = this->LoopEnd - GetPos(); |
|
|
readsamples = Read(&pDst[totalreadsamples * this->FrameSize], Min(samplestoread, samplestoloopend), pExternalDecompressionBuffer); |
|
|
samplestoread -= readsamples; |
|
|
totalreadsamples += readsamples; |
|
|
if (readsamples == samplestoloopend) { |
|
|
pPlaybackState->reverse = true; |
|
|
break; |
|
|
} |
|
|
} while (samplestoread && readsamples); |
|
|
} |
|
|
else { // backward playback |
|
864 |
|
|
865 |
// as we can only read forward from disk, we have to |
case loop_type_bidirectional: { //TODO: not tested yet! |
866 |
// determine the end position within the loop first, |
do { |
867 |
// read forward from that 'end' and finally after |
// if not endless loop check if max. number of loop cycles have been passed |
868 |
// reading, swap all sample frames so it reflects |
if (this->LoopPlayCount && !pPlaybackState->loop_cycles_left) break; |
869 |
// backward playback |
|
870 |
|
if (!pPlaybackState->reverse) { // forward playback |
871 |
unsigned long swapareastart = totalreadsamples; |
do { |
872 |
unsigned long loopoffset = GetPos() - this->LoopStart; |
samplestoloopend = loopEnd - GetPos(); |
873 |
unsigned long samplestoreadinloop = Min(samplestoread, loopoffset); |
readsamples = Read(&pDst[totalreadsamples * this->FrameSize], Min(samplestoread, samplestoloopend), pExternalDecompressionBuffer); |
874 |
unsigned long reverseplaybackend = GetPos() - samplestoreadinloop; |
samplestoread -= readsamples; |
875 |
|
totalreadsamples += readsamples; |
876 |
SetPos(reverseplaybackend); |
if (readsamples == samplestoloopend) { |
877 |
|
pPlaybackState->reverse = true; |
878 |
// read samples for backward playback |
break; |
879 |
do { |
} |
880 |
readsamples = Read(&pDst[totalreadsamples * this->FrameSize], samplestoreadinloop, pExternalDecompressionBuffer); |
} while (samplestoread && readsamples); |
881 |
samplestoreadinloop -= readsamples; |
} |
882 |
samplestoread -= readsamples; |
else { // backward playback |
|
totalreadsamples += readsamples; |
|
|
} while (samplestoreadinloop && readsamples); |
|
883 |
|
|
884 |
SetPos(reverseplaybackend); // pretend we really read backwards |
// as we can only read forward from disk, we have to |
885 |
|
// determine the end position within the loop first, |
886 |
|
// read forward from that 'end' and finally after |
887 |
|
// reading, swap all sample frames so it reflects |
888 |
|
// backward playback |
889 |
|
|
890 |
|
unsigned long swapareastart = totalreadsamples; |
891 |
|
unsigned long loopoffset = GetPos() - loop.LoopStart; |
892 |
|
unsigned long samplestoreadinloop = Min(samplestoread, loopoffset); |
893 |
|
unsigned long reverseplaybackend = GetPos() - samplestoreadinloop; |
894 |
|
|
895 |
|
SetPos(reverseplaybackend); |
896 |
|
|
897 |
|
// read samples for backward playback |
898 |
|
do { |
899 |
|
readsamples = Read(&pDst[totalreadsamples * this->FrameSize], samplestoreadinloop, pExternalDecompressionBuffer); |
900 |
|
samplestoreadinloop -= readsamples; |
901 |
|
samplestoread -= readsamples; |
902 |
|
totalreadsamples += readsamples; |
903 |
|
} while (samplestoreadinloop && readsamples); |
904 |
|
|
905 |
|
SetPos(reverseplaybackend); // pretend we really read backwards |
906 |
|
|
907 |
|
if (reverseplaybackend == loop.LoopStart) { |
908 |
|
pPlaybackState->loop_cycles_left--; |
909 |
|
pPlaybackState->reverse = false; |
910 |
|
} |
911 |
|
|
912 |
if (reverseplaybackend == this->LoopStart) { |
// reverse the sample frames for backward playback |
913 |
pPlaybackState->loop_cycles_left--; |
if (totalreadsamples > swapareastart) //FIXME: this if() is just a crash workaround for now (#102), but totalreadsamples <= swapareastart should never be the case, so there's probably still a bug above! |
914 |
pPlaybackState->reverse = false; |
SwapMemoryArea(&pDst[swapareastart * this->FrameSize], (totalreadsamples - swapareastart) * this->FrameSize, this->FrameSize); |
915 |
} |
} |
916 |
|
} while (samplestoread && readsamples); |
917 |
|
break; |
918 |
|
} |
919 |
|
|
920 |
// reverse the sample frames for backward playback |
case loop_type_backward: { // TODO: not tested yet! |
921 |
SwapMemoryArea(&pDst[swapareastart * this->FrameSize], (totalreadsamples - swapareastart) * this->FrameSize, this->FrameSize); |
// forward playback (not entered the loop yet) |
922 |
} |
if (!pPlaybackState->reverse) do { |
923 |
} while (samplestoread && readsamples); |
samplestoloopend = loopEnd - GetPos(); |
924 |
break; |
readsamples = Read(&pDst[totalreadsamples * this->FrameSize], Min(samplestoread, samplestoloopend), pExternalDecompressionBuffer); |
925 |
} |
samplestoread -= readsamples; |
926 |
|
totalreadsamples += readsamples; |
927 |
case loop_type_backward: { // TODO: not tested yet! |
if (readsamples == samplestoloopend) { |
928 |
// forward playback (not entered the loop yet) |
pPlaybackState->reverse = true; |
929 |
if (!pPlaybackState->reverse) do { |
break; |
930 |
samplestoloopend = this->LoopEnd - GetPos(); |
} |
931 |
readsamples = Read(&pDst[totalreadsamples * this->FrameSize], Min(samplestoread, samplestoloopend), pExternalDecompressionBuffer); |
} while (samplestoread && readsamples); |
|
samplestoread -= readsamples; |
|
|
totalreadsamples += readsamples; |
|
|
if (readsamples == samplestoloopend) { |
|
|
pPlaybackState->reverse = true; |
|
|
break; |
|
|
} |
|
|
} while (samplestoread && readsamples); |
|
932 |
|
|
933 |
if (!samplestoread) break; |
if (!samplestoread) break; |
934 |
|
|
935 |
// as we can only read forward from disk, we have to |
// as we can only read forward from disk, we have to |
936 |
// determine the end position within the loop first, |
// determine the end position within the loop first, |
937 |
// read forward from that 'end' and finally after |
// read forward from that 'end' and finally after |
938 |
// reading, swap all sample frames so it reflects |
// reading, swap all sample frames so it reflects |
939 |
// backward playback |
// backward playback |
940 |
|
|
941 |
unsigned long swapareastart = totalreadsamples; |
unsigned long swapareastart = totalreadsamples; |
942 |
unsigned long loopoffset = GetPos() - this->LoopStart; |
unsigned long loopoffset = GetPos() - loop.LoopStart; |
943 |
unsigned long samplestoreadinloop = (this->LoopPlayCount) ? Min(samplestoread, pPlaybackState->loop_cycles_left * LoopSize - loopoffset) |
unsigned long samplestoreadinloop = (this->LoopPlayCount) ? Min(samplestoread, pPlaybackState->loop_cycles_left * loop.LoopLength - loopoffset) |
944 |
: samplestoread; |
: samplestoread; |
945 |
unsigned long reverseplaybackend = this->LoopStart + Abs((loopoffset - samplestoreadinloop) % this->LoopSize); |
unsigned long reverseplaybackend = loop.LoopStart + Abs((loopoffset - samplestoreadinloop) % loop.LoopLength); |
946 |
|
|
947 |
SetPos(reverseplaybackend); |
SetPos(reverseplaybackend); |
948 |
|
|
949 |
// read samples for backward playback |
// read samples for backward playback |
950 |
do { |
do { |
951 |
// if not endless loop check if max. number of loop cycles have been passed |
// if not endless loop check if max. number of loop cycles have been passed |
952 |
if (this->LoopPlayCount && !pPlaybackState->loop_cycles_left) break; |
if (this->LoopPlayCount && !pPlaybackState->loop_cycles_left) break; |
953 |
samplestoloopend = this->LoopEnd - GetPos(); |
samplestoloopend = loopEnd - GetPos(); |
954 |
readsamples = Read(&pDst[totalreadsamples * this->FrameSize], Min(samplestoreadinloop, samplestoloopend), pExternalDecompressionBuffer); |
readsamples = Read(&pDst[totalreadsamples * this->FrameSize], Min(samplestoreadinloop, samplestoloopend), pExternalDecompressionBuffer); |
955 |
samplestoreadinloop -= readsamples; |
samplestoreadinloop -= readsamples; |
956 |
samplestoread -= readsamples; |
samplestoread -= readsamples; |
957 |
totalreadsamples += readsamples; |
totalreadsamples += readsamples; |
958 |
if (readsamples == samplestoloopend) { |
if (readsamples == samplestoloopend) { |
959 |
pPlaybackState->loop_cycles_left--; |
pPlaybackState->loop_cycles_left--; |
960 |
SetPos(this->LoopStart); |
SetPos(loop.LoopStart); |
961 |
} |
} |
962 |
} while (samplestoreadinloop && readsamples); |
} while (samplestoreadinloop && readsamples); |
963 |
|
|
964 |
SetPos(reverseplaybackend); // pretend we really read backwards |
SetPos(reverseplaybackend); // pretend we really read backwards |
965 |
|
|
966 |
// reverse the sample frames for backward playback |
// reverse the sample frames for backward playback |
967 |
SwapMemoryArea(&pDst[swapareastart * this->FrameSize], (totalreadsamples - swapareastart) * this->FrameSize, this->FrameSize); |
SwapMemoryArea(&pDst[swapareastart * this->FrameSize], (totalreadsamples - swapareastart) * this->FrameSize, this->FrameSize); |
968 |
break; |
break; |
969 |
} |
} |
970 |
|
|
971 |
default: case loop_type_normal: { |
default: case loop_type_normal: { |
972 |
do { |
do { |
973 |
// if not endless loop check if max. number of loop cycles have been passed |
// if not endless loop check if max. number of loop cycles have been passed |
974 |
if (this->LoopPlayCount && !pPlaybackState->loop_cycles_left) break; |
if (this->LoopPlayCount && !pPlaybackState->loop_cycles_left) break; |
975 |
samplestoloopend = this->LoopEnd - GetPos(); |
samplestoloopend = loopEnd - GetPos(); |
976 |
readsamples = Read(&pDst[totalreadsamples * this->FrameSize], Min(samplestoread, samplestoloopend), pExternalDecompressionBuffer); |
readsamples = Read(&pDst[totalreadsamples * this->FrameSize], Min(samplestoread, samplestoloopend), pExternalDecompressionBuffer); |
977 |
samplestoread -= readsamples; |
samplestoread -= readsamples; |
978 |
totalreadsamples += readsamples; |
totalreadsamples += readsamples; |
979 |
if (readsamples == samplestoloopend) { |
if (readsamples == samplestoloopend) { |
980 |
pPlaybackState->loop_cycles_left--; |
pPlaybackState->loop_cycles_left--; |
981 |
SetPos(this->LoopStart); |
SetPos(loop.LoopStart); |
982 |
} |
} |
983 |
} while (samplestoread && readsamples); |
} while (samplestoread && readsamples); |
984 |
break; |
break; |
985 |
|
} |
986 |
} |
} |
987 |
} |
} |
988 |
} |
} |
1012 |
* have to use an external decompression buffer for <b>EACH</b> |
* have to use an external decompression buffer for <b>EACH</b> |
1013 |
* streaming thread to avoid race conditions and crashes! |
* streaming thread to avoid race conditions and crashes! |
1014 |
* |
* |
1015 |
|
* For 16 bit samples, the data in the buffer will be int16_t |
1016 |
|
* (using native endianness). For 24 bit, the buffer will |
1017 |
|
* contain three bytes per sample, little-endian. |
1018 |
|
* |
1019 |
* @param pBuffer destination buffer |
* @param pBuffer destination buffer |
1020 |
* @param SampleCount number of sample points to read |
* @param SampleCount number of sample points to read |
1021 |
* @param pExternalDecompressionBuffer (optional) external buffer to use for decompression |
* @param pExternalDecompressionBuffer (optional) external buffer to use for decompression |
1026 |
if (SampleCount == 0) return 0; |
if (SampleCount == 0) return 0; |
1027 |
if (!Compressed) { |
if (!Compressed) { |
1028 |
if (BitDepth == 24) { |
if (BitDepth == 24) { |
1029 |
// 24 bit sample. For now just truncate to 16 bit. |
return pCkData->Read(pBuffer, SampleCount * FrameSize, 1) / FrameSize; |
|
unsigned char* pSrc = (unsigned char*) ((pExternalDecompressionBuffer) ? pExternalDecompressionBuffer->pStart : this->InternalDecompressionBuffer.pStart); |
|
|
int16_t* pDst = static_cast<int16_t*>(pBuffer); |
|
|
if (Channels == 2) { // Stereo |
|
|
unsigned long readBytes = pCkData->Read(pSrc, SampleCount * 6, 1); |
|
|
pSrc++; |
|
|
for (unsigned long i = readBytes ; i > 0 ; i -= 3) { |
|
|
*pDst++ = get16(pSrc); |
|
|
pSrc += 3; |
|
|
} |
|
|
return (pDst - static_cast<int16_t*>(pBuffer)) >> 1; |
|
|
} |
|
|
else { // Mono |
|
|
unsigned long readBytes = pCkData->Read(pSrc, SampleCount * 3, 1); |
|
|
pSrc++; |
|
|
for (unsigned long i = readBytes ; i > 0 ; i -= 3) { |
|
|
*pDst++ = get16(pSrc); |
|
|
pSrc += 3; |
|
|
} |
|
|
return pDst - static_cast<int16_t*>(pBuffer); |
|
|
} |
|
1030 |
} |
} |
1031 |
else { // 16 bit |
else { // 16 bit |
1032 |
// (pCkData->Read does endian correction) |
// (pCkData->Read does endian correction) |
1056 |
|
|
1057 |
unsigned char* pSrc = (unsigned char*) pDecompressionBuffer->pStart; |
unsigned char* pSrc = (unsigned char*) pDecompressionBuffer->pStart; |
1058 |
int16_t* pDst = static_cast<int16_t*>(pBuffer); |
int16_t* pDst = static_cast<int16_t*>(pBuffer); |
1059 |
|
uint8_t* pDst24 = static_cast<uint8_t*>(pBuffer); |
1060 |
remainingbytes = pCkData->Read(pSrc, assumedsize, 1); |
remainingbytes = pCkData->Read(pSrc, assumedsize, 1); |
1061 |
|
|
1062 |
while (remainingsamples && remainingbytes) { |
while (remainingsamples && remainingbytes) { |
1138 |
const unsigned char* const param_r = pSrc; |
const unsigned char* const param_r = pSrc; |
1139 |
if (mode_r != 2) pSrc += 12; |
if (mode_r != 2) pSrc += 12; |
1140 |
|
|
1141 |
Decompress24(mode_l, param_l, 2, pSrc, pDst, |
Decompress24(mode_l, param_l, 6, pSrc, pDst24, |
1142 |
skipsamples, copysamples, TruncatedBits); |
skipsamples, copysamples, TruncatedBits); |
1143 |
Decompress24(mode_r, param_r, 2, pSrc + rightChannelOffset, pDst + 1, |
Decompress24(mode_r, param_r, 6, pSrc + rightChannelOffset, pDst24 + 3, |
1144 |
skipsamples, copysamples, TruncatedBits); |
skipsamples, copysamples, TruncatedBits); |
1145 |
pDst += copysamples << 1; |
pDst24 += copysamples * 6; |
1146 |
} |
} |
1147 |
else { // Mono |
else { // Mono |
1148 |
Decompress24(mode_l, param_l, 1, pSrc, pDst, |
Decompress24(mode_l, param_l, 3, pSrc, pDst24, |
1149 |
skipsamples, copysamples, TruncatedBits); |
skipsamples, copysamples, TruncatedBits); |
1150 |
pDst += copysamples; |
pDst24 += copysamples * 3; |
1151 |
} |
} |
1152 |
} |
} |
1153 |
else { // 16 bit |
else { // 16 bit |
1189 |
} |
} |
1190 |
} |
} |
1191 |
|
|
1192 |
|
/** @brief Write sample wave data. |
1193 |
|
* |
1194 |
|
* Writes \a SampleCount number of sample points from the buffer pointed |
1195 |
|
* by \a pBuffer and increments the position within the sample. Use this |
1196 |
|
* method to directly write the sample data to disk, i.e. if you don't |
1197 |
|
* want or cannot load the whole sample data into RAM. |
1198 |
|
* |
1199 |
|
* You have to Resize() the sample to the desired size and call |
1200 |
|
* File::Save() <b>before</b> using Write(). |
1201 |
|
* |
1202 |
|
* Note: there is currently no support for writing compressed samples. |
1203 |
|
* |
1204 |
|
* For 16 bit samples, the data in the source buffer should be |
1205 |
|
* int16_t (using native endianness). For 24 bit, the buffer |
1206 |
|
* should contain three bytes per sample, little-endian. |
1207 |
|
* |
1208 |
|
* @param pBuffer - source buffer |
1209 |
|
* @param SampleCount - number of sample points to write |
1210 |
|
* @throws DLS::Exception if current sample size is too small |
1211 |
|
* @throws gig::Exception if sample is compressed |
1212 |
|
* @see DLS::LoadSampleData() |
1213 |
|
*/ |
1214 |
|
unsigned long Sample::Write(void* pBuffer, unsigned long SampleCount) { |
1215 |
|
if (Compressed) throw gig::Exception("There is no support for writing compressed gig samples (yet)"); |
1216 |
|
|
1217 |
|
// if this is the first write in this sample, reset the |
1218 |
|
// checksum calculator |
1219 |
|
if (pCkData->GetPos() == 0) { |
1220 |
|
__resetCRC(crc); |
1221 |
|
} |
1222 |
|
if (GetSize() < SampleCount) throw Exception("Could not write sample data, current sample size to small"); |
1223 |
|
unsigned long res; |
1224 |
|
if (BitDepth == 24) { |
1225 |
|
res = pCkData->Write(pBuffer, SampleCount * FrameSize, 1) / FrameSize; |
1226 |
|
} else { // 16 bit |
1227 |
|
res = Channels == 2 ? pCkData->Write(pBuffer, SampleCount << 1, 2) >> 1 |
1228 |
|
: pCkData->Write(pBuffer, SampleCount, 2); |
1229 |
|
} |
1230 |
|
__calculateCRC((unsigned char *)pBuffer, SampleCount * FrameSize, crc); |
1231 |
|
|
1232 |
|
// if this is the last write, update the checksum chunk in the |
1233 |
|
// file |
1234 |
|
if (pCkData->GetPos() == pCkData->GetSize()) { |
1235 |
|
File* pFile = static_cast<File*>(GetParent()); |
1236 |
|
pFile->SetSampleChecksum(this, __encodeCRC(crc)); |
1237 |
|
} |
1238 |
|
return res; |
1239 |
|
} |
1240 |
|
|
1241 |
/** |
/** |
1242 |
* Allocates a decompression buffer for streaming (compressed) samples |
* Allocates a decompression buffer for streaming (compressed) samples |
1243 |
* with Sample::Read(). If you are using more than one streaming thread |
* with Sample::Read(). If you are using more than one streaming thread |
1280 |
} |
} |
1281 |
} |
} |
1282 |
|
|
1283 |
|
/** |
1284 |
|
* Returns pointer to the Group this Sample belongs to. In the .gig |
1285 |
|
* format a sample always belongs to one group. If it wasn't explicitly |
1286 |
|
* assigned to a certain group, it will be automatically assigned to a |
1287 |
|
* default group. |
1288 |
|
* |
1289 |
|
* @returns Sample's Group (never NULL) |
1290 |
|
*/ |
1291 |
|
Group* Sample::GetGroup() const { |
1292 |
|
return pGroup; |
1293 |
|
} |
1294 |
|
|
1295 |
Sample::~Sample() { |
Sample::~Sample() { |
1296 |
Instances--; |
Instances--; |
1297 |
if (!Instances && InternalDecompressionBuffer.Size) { |
if (!Instances && InternalDecompressionBuffer.Size) { |
1311 |
uint DimensionRegion::Instances = 0; |
uint DimensionRegion::Instances = 0; |
1312 |
DimensionRegion::VelocityTableMap* DimensionRegion::pVelocityTables = NULL; |
DimensionRegion::VelocityTableMap* DimensionRegion::pVelocityTables = NULL; |
1313 |
|
|
1314 |
DimensionRegion::DimensionRegion(RIFF::List* _3ewl) : DLS::Sampler(_3ewl) { |
DimensionRegion::DimensionRegion(Region* pParent, RIFF::List* _3ewl) : DLS::Sampler(_3ewl) { |
1315 |
Instances++; |
Instances++; |
1316 |
|
|
1317 |
memcpy(&Crossfade, &SamplerOptions, 4); |
pSample = NULL; |
1318 |
|
pRegion = pParent; |
1319 |
|
|
1320 |
|
if (_3ewl->GetSubChunk(CHUNK_ID_WSMP)) memcpy(&Crossfade, &SamplerOptions, 4); |
1321 |
|
else memset(&Crossfade, 0, 4); |
1322 |
|
|
1323 |
if (!pVelocityTables) pVelocityTables = new VelocityTableMap; |
if (!pVelocityTables) pVelocityTables = new VelocityTableMap; |
1324 |
|
|
1325 |
RIFF::Chunk* _3ewa = _3ewl->GetSubChunk(CHUNK_ID_3EWA); |
RIFF::Chunk* _3ewa = _3ewl->GetSubChunk(CHUNK_ID_3EWA); |
1326 |
_3ewa->ReadInt32(); // unknown, always 0x0000008C ? |
if (_3ewa) { // if '3ewa' chunk exists |
1327 |
LFO3Frequency = (double) GIG_EXP_DECODE(_3ewa->ReadInt32()); |
_3ewa->ReadInt32(); // unknown, always == chunk size ? |
1328 |
EG3Attack = (double) GIG_EXP_DECODE(_3ewa->ReadInt32()); |
LFO3Frequency = (double) GIG_EXP_DECODE(_3ewa->ReadInt32()); |
1329 |
_3ewa->ReadInt16(); // unknown |
EG3Attack = (double) GIG_EXP_DECODE(_3ewa->ReadInt32()); |
1330 |
LFO1InternalDepth = _3ewa->ReadUint16(); |
_3ewa->ReadInt16(); // unknown |
1331 |
_3ewa->ReadInt16(); // unknown |
LFO1InternalDepth = _3ewa->ReadUint16(); |
1332 |
LFO3InternalDepth = _3ewa->ReadInt16(); |
_3ewa->ReadInt16(); // unknown |
1333 |
_3ewa->ReadInt16(); // unknown |
LFO3InternalDepth = _3ewa->ReadInt16(); |
1334 |
LFO1ControlDepth = _3ewa->ReadUint16(); |
_3ewa->ReadInt16(); // unknown |
1335 |
_3ewa->ReadInt16(); // unknown |
LFO1ControlDepth = _3ewa->ReadUint16(); |
1336 |
LFO3ControlDepth = _3ewa->ReadInt16(); |
_3ewa->ReadInt16(); // unknown |
1337 |
EG1Attack = (double) GIG_EXP_DECODE(_3ewa->ReadInt32()); |
LFO3ControlDepth = _3ewa->ReadInt16(); |
1338 |
EG1Decay1 = (double) GIG_EXP_DECODE(_3ewa->ReadInt32()); |
EG1Attack = (double) GIG_EXP_DECODE(_3ewa->ReadInt32()); |
1339 |
_3ewa->ReadInt16(); // unknown |
EG1Decay1 = (double) GIG_EXP_DECODE(_3ewa->ReadInt32()); |
1340 |
EG1Sustain = _3ewa->ReadUint16(); |
_3ewa->ReadInt16(); // unknown |
1341 |
EG1Release = (double) GIG_EXP_DECODE(_3ewa->ReadInt32()); |
EG1Sustain = _3ewa->ReadUint16(); |
1342 |
EG1Controller = DecodeLeverageController(static_cast<_lev_ctrl_t>(_3ewa->ReadUint8())); |
EG1Release = (double) GIG_EXP_DECODE(_3ewa->ReadInt32()); |
1343 |
uint8_t eg1ctrloptions = _3ewa->ReadUint8(); |
EG1Controller = DecodeLeverageController(static_cast<_lev_ctrl_t>(_3ewa->ReadUint8())); |
1344 |
EG1ControllerInvert = eg1ctrloptions & 0x01; |
uint8_t eg1ctrloptions = _3ewa->ReadUint8(); |
1345 |
EG1ControllerAttackInfluence = GIG_EG_CTR_ATTACK_INFLUENCE_EXTRACT(eg1ctrloptions); |
EG1ControllerInvert = eg1ctrloptions & 0x01; |
1346 |
EG1ControllerDecayInfluence = GIG_EG_CTR_DECAY_INFLUENCE_EXTRACT(eg1ctrloptions); |
EG1ControllerAttackInfluence = GIG_EG_CTR_ATTACK_INFLUENCE_EXTRACT(eg1ctrloptions); |
1347 |
EG1ControllerReleaseInfluence = GIG_EG_CTR_RELEASE_INFLUENCE_EXTRACT(eg1ctrloptions); |
EG1ControllerDecayInfluence = GIG_EG_CTR_DECAY_INFLUENCE_EXTRACT(eg1ctrloptions); |
1348 |
EG2Controller = DecodeLeverageController(static_cast<_lev_ctrl_t>(_3ewa->ReadUint8())); |
EG1ControllerReleaseInfluence = GIG_EG_CTR_RELEASE_INFLUENCE_EXTRACT(eg1ctrloptions); |
1349 |
uint8_t eg2ctrloptions = _3ewa->ReadUint8(); |
EG2Controller = DecodeLeverageController(static_cast<_lev_ctrl_t>(_3ewa->ReadUint8())); |
1350 |
EG2ControllerInvert = eg2ctrloptions & 0x01; |
uint8_t eg2ctrloptions = _3ewa->ReadUint8(); |
1351 |
EG2ControllerAttackInfluence = GIG_EG_CTR_ATTACK_INFLUENCE_EXTRACT(eg2ctrloptions); |
EG2ControllerInvert = eg2ctrloptions & 0x01; |
1352 |
EG2ControllerDecayInfluence = GIG_EG_CTR_DECAY_INFLUENCE_EXTRACT(eg2ctrloptions); |
EG2ControllerAttackInfluence = GIG_EG_CTR_ATTACK_INFLUENCE_EXTRACT(eg2ctrloptions); |
1353 |
EG2ControllerReleaseInfluence = GIG_EG_CTR_RELEASE_INFLUENCE_EXTRACT(eg2ctrloptions); |
EG2ControllerDecayInfluence = GIG_EG_CTR_DECAY_INFLUENCE_EXTRACT(eg2ctrloptions); |
1354 |
LFO1Frequency = (double) GIG_EXP_DECODE(_3ewa->ReadInt32()); |
EG2ControllerReleaseInfluence = GIG_EG_CTR_RELEASE_INFLUENCE_EXTRACT(eg2ctrloptions); |
1355 |
EG2Attack = (double) GIG_EXP_DECODE(_3ewa->ReadInt32()); |
LFO1Frequency = (double) GIG_EXP_DECODE(_3ewa->ReadInt32()); |
1356 |
EG2Decay1 = (double) GIG_EXP_DECODE(_3ewa->ReadInt32()); |
EG2Attack = (double) GIG_EXP_DECODE(_3ewa->ReadInt32()); |
1357 |
_3ewa->ReadInt16(); // unknown |
EG2Decay1 = (double) GIG_EXP_DECODE(_3ewa->ReadInt32()); |
1358 |
EG2Sustain = _3ewa->ReadUint16(); |
_3ewa->ReadInt16(); // unknown |
1359 |
EG2Release = (double) GIG_EXP_DECODE(_3ewa->ReadInt32()); |
EG2Sustain = _3ewa->ReadUint16(); |
1360 |
_3ewa->ReadInt16(); // unknown |
EG2Release = (double) GIG_EXP_DECODE(_3ewa->ReadInt32()); |
1361 |
LFO2ControlDepth = _3ewa->ReadUint16(); |
_3ewa->ReadInt16(); // unknown |
1362 |
LFO2Frequency = (double) GIG_EXP_DECODE(_3ewa->ReadInt32()); |
LFO2ControlDepth = _3ewa->ReadUint16(); |
1363 |
_3ewa->ReadInt16(); // unknown |
LFO2Frequency = (double) GIG_EXP_DECODE(_3ewa->ReadInt32()); |
1364 |
LFO2InternalDepth = _3ewa->ReadUint16(); |
_3ewa->ReadInt16(); // unknown |
1365 |
int32_t eg1decay2 = _3ewa->ReadInt32(); |
LFO2InternalDepth = _3ewa->ReadUint16(); |
1366 |
EG1Decay2 = (double) GIG_EXP_DECODE(eg1decay2); |
int32_t eg1decay2 = _3ewa->ReadInt32(); |
1367 |
EG1InfiniteSustain = (eg1decay2 == 0x7fffffff); |
EG1Decay2 = (double) GIG_EXP_DECODE(eg1decay2); |
1368 |
_3ewa->ReadInt16(); // unknown |
EG1InfiniteSustain = (eg1decay2 == 0x7fffffff); |
1369 |
EG1PreAttack = _3ewa->ReadUint16(); |
_3ewa->ReadInt16(); // unknown |
1370 |
int32_t eg2decay2 = _3ewa->ReadInt32(); |
EG1PreAttack = _3ewa->ReadUint16(); |
1371 |
EG2Decay2 = (double) GIG_EXP_DECODE(eg2decay2); |
int32_t eg2decay2 = _3ewa->ReadInt32(); |
1372 |
EG2InfiniteSustain = (eg2decay2 == 0x7fffffff); |
EG2Decay2 = (double) GIG_EXP_DECODE(eg2decay2); |
1373 |
_3ewa->ReadInt16(); // unknown |
EG2InfiniteSustain = (eg2decay2 == 0x7fffffff); |
1374 |
EG2PreAttack = _3ewa->ReadUint16(); |
_3ewa->ReadInt16(); // unknown |
1375 |
uint8_t velocityresponse = _3ewa->ReadUint8(); |
EG2PreAttack = _3ewa->ReadUint16(); |
1376 |
if (velocityresponse < 5) { |
uint8_t velocityresponse = _3ewa->ReadUint8(); |
1377 |
VelocityResponseCurve = curve_type_nonlinear; |
if (velocityresponse < 5) { |
1378 |
VelocityResponseDepth = velocityresponse; |
VelocityResponseCurve = curve_type_nonlinear; |
1379 |
} |
VelocityResponseDepth = velocityresponse; |
1380 |
else if (velocityresponse < 10) { |
} else if (velocityresponse < 10) { |
1381 |
VelocityResponseCurve = curve_type_linear; |
VelocityResponseCurve = curve_type_linear; |
1382 |
VelocityResponseDepth = velocityresponse - 5; |
VelocityResponseDepth = velocityresponse - 5; |
1383 |
} |
} else if (velocityresponse < 15) { |
1384 |
else if (velocityresponse < 15) { |
VelocityResponseCurve = curve_type_special; |
1385 |
VelocityResponseCurve = curve_type_special; |
VelocityResponseDepth = velocityresponse - 10; |
1386 |
VelocityResponseDepth = velocityresponse - 10; |
} else { |
1387 |
|
VelocityResponseCurve = curve_type_unknown; |
1388 |
|
VelocityResponseDepth = 0; |
1389 |
|
} |
1390 |
|
uint8_t releasevelocityresponse = _3ewa->ReadUint8(); |
1391 |
|
if (releasevelocityresponse < 5) { |
1392 |
|
ReleaseVelocityResponseCurve = curve_type_nonlinear; |
1393 |
|
ReleaseVelocityResponseDepth = releasevelocityresponse; |
1394 |
|
} else if (releasevelocityresponse < 10) { |
1395 |
|
ReleaseVelocityResponseCurve = curve_type_linear; |
1396 |
|
ReleaseVelocityResponseDepth = releasevelocityresponse - 5; |
1397 |
|
} else if (releasevelocityresponse < 15) { |
1398 |
|
ReleaseVelocityResponseCurve = curve_type_special; |
1399 |
|
ReleaseVelocityResponseDepth = releasevelocityresponse - 10; |
1400 |
|
} else { |
1401 |
|
ReleaseVelocityResponseCurve = curve_type_unknown; |
1402 |
|
ReleaseVelocityResponseDepth = 0; |
1403 |
|
} |
1404 |
|
VelocityResponseCurveScaling = _3ewa->ReadUint8(); |
1405 |
|
AttenuationControllerThreshold = _3ewa->ReadInt8(); |
1406 |
|
_3ewa->ReadInt32(); // unknown |
1407 |
|
SampleStartOffset = (uint16_t) _3ewa->ReadInt16(); |
1408 |
|
_3ewa->ReadInt16(); // unknown |
1409 |
|
uint8_t pitchTrackDimensionBypass = _3ewa->ReadInt8(); |
1410 |
|
PitchTrack = GIG_PITCH_TRACK_EXTRACT(pitchTrackDimensionBypass); |
1411 |
|
if (pitchTrackDimensionBypass & 0x10) DimensionBypass = dim_bypass_ctrl_94; |
1412 |
|
else if (pitchTrackDimensionBypass & 0x20) DimensionBypass = dim_bypass_ctrl_95; |
1413 |
|
else DimensionBypass = dim_bypass_ctrl_none; |
1414 |
|
uint8_t pan = _3ewa->ReadUint8(); |
1415 |
|
Pan = (pan < 64) ? pan : -((int)pan - 63); // signed 7 bit -> signed 8 bit |
1416 |
|
SelfMask = _3ewa->ReadInt8() & 0x01; |
1417 |
|
_3ewa->ReadInt8(); // unknown |
1418 |
|
uint8_t lfo3ctrl = _3ewa->ReadUint8(); |
1419 |
|
LFO3Controller = static_cast<lfo3_ctrl_t>(lfo3ctrl & 0x07); // lower 3 bits |
1420 |
|
LFO3Sync = lfo3ctrl & 0x20; // bit 5 |
1421 |
|
InvertAttenuationController = lfo3ctrl & 0x80; // bit 7 |
1422 |
|
AttenuationController = DecodeLeverageController(static_cast<_lev_ctrl_t>(_3ewa->ReadUint8())); |
1423 |
|
uint8_t lfo2ctrl = _3ewa->ReadUint8(); |
1424 |
|
LFO2Controller = static_cast<lfo2_ctrl_t>(lfo2ctrl & 0x07); // lower 3 bits |
1425 |
|
LFO2FlipPhase = lfo2ctrl & 0x80; // bit 7 |
1426 |
|
LFO2Sync = lfo2ctrl & 0x20; // bit 5 |
1427 |
|
bool extResonanceCtrl = lfo2ctrl & 0x40; // bit 6 |
1428 |
|
uint8_t lfo1ctrl = _3ewa->ReadUint8(); |
1429 |
|
LFO1Controller = static_cast<lfo1_ctrl_t>(lfo1ctrl & 0x07); // lower 3 bits |
1430 |
|
LFO1FlipPhase = lfo1ctrl & 0x80; // bit 7 |
1431 |
|
LFO1Sync = lfo1ctrl & 0x40; // bit 6 |
1432 |
|
VCFResonanceController = (extResonanceCtrl) ? static_cast<vcf_res_ctrl_t>(GIG_VCF_RESONANCE_CTRL_EXTRACT(lfo1ctrl)) |
1433 |
|
: vcf_res_ctrl_none; |
1434 |
|
uint16_t eg3depth = _3ewa->ReadUint16(); |
1435 |
|
EG3Depth = (eg3depth <= 1200) ? eg3depth /* positives */ |
1436 |
|
: (-1) * (int16_t) ((eg3depth ^ 0xfff) + 1); /* binary complementary for negatives */ |
1437 |
|
_3ewa->ReadInt16(); // unknown |
1438 |
|
ChannelOffset = _3ewa->ReadUint8() / 4; |
1439 |
|
uint8_t regoptions = _3ewa->ReadUint8(); |
1440 |
|
MSDecode = regoptions & 0x01; // bit 0 |
1441 |
|
SustainDefeat = regoptions & 0x02; // bit 1 |
1442 |
|
_3ewa->ReadInt16(); // unknown |
1443 |
|
VelocityUpperLimit = _3ewa->ReadInt8(); |
1444 |
|
_3ewa->ReadInt8(); // unknown |
1445 |
|
_3ewa->ReadInt16(); // unknown |
1446 |
|
ReleaseTriggerDecay = _3ewa->ReadUint8(); // release trigger decay |
1447 |
|
_3ewa->ReadInt8(); // unknown |
1448 |
|
_3ewa->ReadInt8(); // unknown |
1449 |
|
EG1Hold = _3ewa->ReadUint8() & 0x80; // bit 7 |
1450 |
|
uint8_t vcfcutoff = _3ewa->ReadUint8(); |
1451 |
|
VCFEnabled = vcfcutoff & 0x80; // bit 7 |
1452 |
|
VCFCutoff = vcfcutoff & 0x7f; // lower 7 bits |
1453 |
|
VCFCutoffController = static_cast<vcf_cutoff_ctrl_t>(_3ewa->ReadUint8()); |
1454 |
|
uint8_t vcfvelscale = _3ewa->ReadUint8(); |
1455 |
|
VCFCutoffControllerInvert = vcfvelscale & 0x80; // bit 7 |
1456 |
|
VCFVelocityScale = vcfvelscale & 0x7f; // lower 7 bits |
1457 |
|
_3ewa->ReadInt8(); // unknown |
1458 |
|
uint8_t vcfresonance = _3ewa->ReadUint8(); |
1459 |
|
VCFResonance = vcfresonance & 0x7f; // lower 7 bits |
1460 |
|
VCFResonanceDynamic = !(vcfresonance & 0x80); // bit 7 |
1461 |
|
uint8_t vcfbreakpoint = _3ewa->ReadUint8(); |
1462 |
|
VCFKeyboardTracking = vcfbreakpoint & 0x80; // bit 7 |
1463 |
|
VCFKeyboardTrackingBreakpoint = vcfbreakpoint & 0x7f; // lower 7 bits |
1464 |
|
uint8_t vcfvelocity = _3ewa->ReadUint8(); |
1465 |
|
VCFVelocityDynamicRange = vcfvelocity % 5; |
1466 |
|
VCFVelocityCurve = static_cast<curve_type_t>(vcfvelocity / 5); |
1467 |
|
VCFType = static_cast<vcf_type_t>(_3ewa->ReadUint8()); |
1468 |
|
if (VCFType == vcf_type_lowpass) { |
1469 |
|
if (lfo3ctrl & 0x40) // bit 6 |
1470 |
|
VCFType = vcf_type_lowpassturbo; |
1471 |
|
} |
1472 |
|
if (_3ewa->RemainingBytes() >= 8) { |
1473 |
|
_3ewa->Read(DimensionUpperLimits, 1, 8); |
1474 |
|
} else { |
1475 |
|
memset(DimensionUpperLimits, 0, 8); |
1476 |
|
} |
1477 |
|
} else { // '3ewa' chunk does not exist yet |
1478 |
|
// use default values |
1479 |
|
LFO3Frequency = 1.0; |
1480 |
|
EG3Attack = 0.0; |
1481 |
|
LFO1InternalDepth = 0; |
1482 |
|
LFO3InternalDepth = 0; |
1483 |
|
LFO1ControlDepth = 0; |
1484 |
|
LFO3ControlDepth = 0; |
1485 |
|
EG1Attack = 0.0; |
1486 |
|
EG1Decay1 = 0.005; |
1487 |
|
EG1Sustain = 1000; |
1488 |
|
EG1Release = 0.3; |
1489 |
|
EG1Controller.type = eg1_ctrl_t::type_none; |
1490 |
|
EG1Controller.controller_number = 0; |
1491 |
|
EG1ControllerInvert = false; |
1492 |
|
EG1ControllerAttackInfluence = 0; |
1493 |
|
EG1ControllerDecayInfluence = 0; |
1494 |
|
EG1ControllerReleaseInfluence = 0; |
1495 |
|
EG2Controller.type = eg2_ctrl_t::type_none; |
1496 |
|
EG2Controller.controller_number = 0; |
1497 |
|
EG2ControllerInvert = false; |
1498 |
|
EG2ControllerAttackInfluence = 0; |
1499 |
|
EG2ControllerDecayInfluence = 0; |
1500 |
|
EG2ControllerReleaseInfluence = 0; |
1501 |
|
LFO1Frequency = 1.0; |
1502 |
|
EG2Attack = 0.0; |
1503 |
|
EG2Decay1 = 0.005; |
1504 |
|
EG2Sustain = 1000; |
1505 |
|
EG2Release = 0.3; |
1506 |
|
LFO2ControlDepth = 0; |
1507 |
|
LFO2Frequency = 1.0; |
1508 |
|
LFO2InternalDepth = 0; |
1509 |
|
EG1Decay2 = 0.0; |
1510 |
|
EG1InfiniteSustain = true; |
1511 |
|
EG1PreAttack = 0; |
1512 |
|
EG2Decay2 = 0.0; |
1513 |
|
EG2InfiniteSustain = true; |
1514 |
|
EG2PreAttack = 0; |
1515 |
|
VelocityResponseCurve = curve_type_nonlinear; |
1516 |
|
VelocityResponseDepth = 3; |
1517 |
|
ReleaseVelocityResponseCurve = curve_type_nonlinear; |
1518 |
|
ReleaseVelocityResponseDepth = 3; |
1519 |
|
VelocityResponseCurveScaling = 32; |
1520 |
|
AttenuationControllerThreshold = 0; |
1521 |
|
SampleStartOffset = 0; |
1522 |
|
PitchTrack = true; |
1523 |
|
DimensionBypass = dim_bypass_ctrl_none; |
1524 |
|
Pan = 0; |
1525 |
|
SelfMask = true; |
1526 |
|
LFO3Controller = lfo3_ctrl_modwheel; |
1527 |
|
LFO3Sync = false; |
1528 |
|
InvertAttenuationController = false; |
1529 |
|
AttenuationController.type = attenuation_ctrl_t::type_none; |
1530 |
|
AttenuationController.controller_number = 0; |
1531 |
|
LFO2Controller = lfo2_ctrl_internal; |
1532 |
|
LFO2FlipPhase = false; |
1533 |
|
LFO2Sync = false; |
1534 |
|
LFO1Controller = lfo1_ctrl_internal; |
1535 |
|
LFO1FlipPhase = false; |
1536 |
|
LFO1Sync = false; |
1537 |
|
VCFResonanceController = vcf_res_ctrl_none; |
1538 |
|
EG3Depth = 0; |
1539 |
|
ChannelOffset = 0; |
1540 |
|
MSDecode = false; |
1541 |
|
SustainDefeat = false; |
1542 |
|
VelocityUpperLimit = 0; |
1543 |
|
ReleaseTriggerDecay = 0; |
1544 |
|
EG1Hold = false; |
1545 |
|
VCFEnabled = false; |
1546 |
|
VCFCutoff = 0; |
1547 |
|
VCFCutoffController = vcf_cutoff_ctrl_none; |
1548 |
|
VCFCutoffControllerInvert = false; |
1549 |
|
VCFVelocityScale = 0; |
1550 |
|
VCFResonance = 0; |
1551 |
|
VCFResonanceDynamic = false; |
1552 |
|
VCFKeyboardTracking = false; |
1553 |
|
VCFKeyboardTrackingBreakpoint = 0; |
1554 |
|
VCFVelocityDynamicRange = 0x04; |
1555 |
|
VCFVelocityCurve = curve_type_linear; |
1556 |
|
VCFType = vcf_type_lowpass; |
1557 |
|
memset(DimensionUpperLimits, 127, 8); |
1558 |
|
} |
1559 |
|
|
1560 |
|
pVelocityAttenuationTable = GetVelocityTable(VelocityResponseCurve, |
1561 |
|
VelocityResponseDepth, |
1562 |
|
VelocityResponseCurveScaling); |
1563 |
|
|
1564 |
|
pVelocityReleaseTable = GetReleaseVelocityTable( |
1565 |
|
ReleaseVelocityResponseCurve, |
1566 |
|
ReleaseVelocityResponseDepth |
1567 |
|
); |
1568 |
|
|
1569 |
|
pVelocityCutoffTable = GetCutoffVelocityTable(VCFVelocityCurve, |
1570 |
|
VCFVelocityDynamicRange, |
1571 |
|
VCFVelocityScale, |
1572 |
|
VCFCutoffController); |
1573 |
|
|
1574 |
|
SampleAttenuation = pow(10.0, -Gain / (20.0 * 655360)); |
1575 |
|
VelocityTable = 0; |
1576 |
|
} |
1577 |
|
|
1578 |
|
/* |
1579 |
|
* Constructs a DimensionRegion by copying all parameters from |
1580 |
|
* another DimensionRegion |
1581 |
|
*/ |
1582 |
|
DimensionRegion::DimensionRegion(RIFF::List* _3ewl, const DimensionRegion& src) : DLS::Sampler(_3ewl) { |
1583 |
|
Instances++; |
1584 |
|
//NOTE: I think we cannot call CopyAssign() here (in a constructor) as long as its a virtual method |
1585 |
|
*this = src; // default memberwise shallow copy of all parameters |
1586 |
|
pParentList = _3ewl; // restore the chunk pointer |
1587 |
|
|
1588 |
|
// deep copy of owned structures |
1589 |
|
if (src.VelocityTable) { |
1590 |
|
VelocityTable = new uint8_t[128]; |
1591 |
|
for (int k = 0 ; k < 128 ; k++) |
1592 |
|
VelocityTable[k] = src.VelocityTable[k]; |
1593 |
|
} |
1594 |
|
if (src.pSampleLoops) { |
1595 |
|
pSampleLoops = new DLS::sample_loop_t[src.SampleLoops]; |
1596 |
|
for (int k = 0 ; k < src.SampleLoops ; k++) |
1597 |
|
pSampleLoops[k] = src.pSampleLoops[k]; |
1598 |
} |
} |
1599 |
else { |
} |
1600 |
VelocityResponseCurve = curve_type_unknown; |
|
1601 |
VelocityResponseDepth = 0; |
/** |
1602 |
|
* Make a (semi) deep copy of the DimensionRegion object given by @a orig |
1603 |
|
* and assign it to this object. |
1604 |
|
* |
1605 |
|
* Note that all sample pointers referenced by @a orig are simply copied as |
1606 |
|
* memory address. Thus the respective samples are shared, not duplicated! |
1607 |
|
* |
1608 |
|
* @param orig - original DimensionRegion object to be copied from |
1609 |
|
*/ |
1610 |
|
void DimensionRegion::CopyAssign(const DimensionRegion* orig) { |
1611 |
|
// delete all allocated data first |
1612 |
|
if (VelocityTable) delete [] VelocityTable; |
1613 |
|
if (pSampleLoops) delete [] pSampleLoops; |
1614 |
|
|
1615 |
|
// backup parent list pointer |
1616 |
|
RIFF::List* p = pParentList; |
1617 |
|
|
1618 |
|
//NOTE: copy code copied from assignment constructor above, see comment there as well |
1619 |
|
|
1620 |
|
*this = *orig; // default memberwise shallow copy of all parameters |
1621 |
|
pParentList = p; // restore the chunk pointer |
1622 |
|
|
1623 |
|
// deep copy of owned structures |
1624 |
|
if (orig->VelocityTable) { |
1625 |
|
VelocityTable = new uint8_t[128]; |
1626 |
|
for (int k = 0 ; k < 128 ; k++) |
1627 |
|
VelocityTable[k] = orig->VelocityTable[k]; |
1628 |
|
} |
1629 |
|
if (orig->pSampleLoops) { |
1630 |
|
pSampleLoops = new DLS::sample_loop_t[orig->SampleLoops]; |
1631 |
|
for (int k = 0 ; k < orig->SampleLoops ; k++) |
1632 |
|
pSampleLoops[k] = orig->pSampleLoops[k]; |
1633 |
} |
} |
1634 |
uint8_t releasevelocityresponse = _3ewa->ReadUint8(); |
} |
1635 |
if (releasevelocityresponse < 5) { |
|
1636 |
ReleaseVelocityResponseCurve = curve_type_nonlinear; |
/** |
1637 |
ReleaseVelocityResponseDepth = releasevelocityresponse; |
* Updates the respective member variable and updates @c SampleAttenuation |
1638 |
} |
* which depends on this value. |
1639 |
else if (releasevelocityresponse < 10) { |
*/ |
1640 |
ReleaseVelocityResponseCurve = curve_type_linear; |
void DimensionRegion::SetGain(int32_t gain) { |
1641 |
ReleaseVelocityResponseDepth = releasevelocityresponse - 5; |
DLS::Sampler::SetGain(gain); |
1642 |
} |
SampleAttenuation = pow(10.0, -Gain / (20.0 * 655360)); |
1643 |
else if (releasevelocityresponse < 15) { |
} |
1644 |
ReleaseVelocityResponseCurve = curve_type_special; |
|
1645 |
ReleaseVelocityResponseDepth = releasevelocityresponse - 10; |
/** |
1646 |
|
* Apply dimension region settings to the respective RIFF chunks. You |
1647 |
|
* have to call File::Save() to make changes persistent. |
1648 |
|
* |
1649 |
|
* Usually there is absolutely no need to call this method explicitly. |
1650 |
|
* It will be called automatically when File::Save() was called. |
1651 |
|
*/ |
1652 |
|
void DimensionRegion::UpdateChunks() { |
1653 |
|
// first update base class's chunk |
1654 |
|
DLS::Sampler::UpdateChunks(); |
1655 |
|
|
1656 |
|
RIFF::Chunk* wsmp = pParentList->GetSubChunk(CHUNK_ID_WSMP); |
1657 |
|
uint8_t* pData = (uint8_t*) wsmp->LoadChunkData(); |
1658 |
|
pData[12] = Crossfade.in_start; |
1659 |
|
pData[13] = Crossfade.in_end; |
1660 |
|
pData[14] = Crossfade.out_start; |
1661 |
|
pData[15] = Crossfade.out_end; |
1662 |
|
|
1663 |
|
// make sure '3ewa' chunk exists |
1664 |
|
RIFF::Chunk* _3ewa = pParentList->GetSubChunk(CHUNK_ID_3EWA); |
1665 |
|
if (!_3ewa) { |
1666 |
|
File* pFile = (File*) GetParent()->GetParent()->GetParent(); |
1667 |
|
bool version3 = pFile->pVersion && pFile->pVersion->major == 3; |
1668 |
|
_3ewa = pParentList->AddSubChunk(CHUNK_ID_3EWA, version3 ? 148 : 140); |
1669 |
} |
} |
1670 |
else { |
pData = (uint8_t*) _3ewa->LoadChunkData(); |
1671 |
ReleaseVelocityResponseCurve = curve_type_unknown; |
|
1672 |
ReleaseVelocityResponseDepth = 0; |
// update '3ewa' chunk with DimensionRegion's current settings |
1673 |
|
|
1674 |
|
const uint32_t chunksize = _3ewa->GetNewSize(); |
1675 |
|
store32(&pData[0], chunksize); // unknown, always chunk size? |
1676 |
|
|
1677 |
|
const int32_t lfo3freq = (int32_t) GIG_EXP_ENCODE(LFO3Frequency); |
1678 |
|
store32(&pData[4], lfo3freq); |
1679 |
|
|
1680 |
|
const int32_t eg3attack = (int32_t) GIG_EXP_ENCODE(EG3Attack); |
1681 |
|
store32(&pData[8], eg3attack); |
1682 |
|
|
1683 |
|
// next 2 bytes unknown |
1684 |
|
|
1685 |
|
store16(&pData[14], LFO1InternalDepth); |
1686 |
|
|
1687 |
|
// next 2 bytes unknown |
1688 |
|
|
1689 |
|
store16(&pData[18], LFO3InternalDepth); |
1690 |
|
|
1691 |
|
// next 2 bytes unknown |
1692 |
|
|
1693 |
|
store16(&pData[22], LFO1ControlDepth); |
1694 |
|
|
1695 |
|
// next 2 bytes unknown |
1696 |
|
|
1697 |
|
store16(&pData[26], LFO3ControlDepth); |
1698 |
|
|
1699 |
|
const int32_t eg1attack = (int32_t) GIG_EXP_ENCODE(EG1Attack); |
1700 |
|
store32(&pData[28], eg1attack); |
1701 |
|
|
1702 |
|
const int32_t eg1decay1 = (int32_t) GIG_EXP_ENCODE(EG1Decay1); |
1703 |
|
store32(&pData[32], eg1decay1); |
1704 |
|
|
1705 |
|
// next 2 bytes unknown |
1706 |
|
|
1707 |
|
store16(&pData[38], EG1Sustain); |
1708 |
|
|
1709 |
|
const int32_t eg1release = (int32_t) GIG_EXP_ENCODE(EG1Release); |
1710 |
|
store32(&pData[40], eg1release); |
1711 |
|
|
1712 |
|
const uint8_t eg1ctl = (uint8_t) EncodeLeverageController(EG1Controller); |
1713 |
|
pData[44] = eg1ctl; |
1714 |
|
|
1715 |
|
const uint8_t eg1ctrloptions = |
1716 |
|
(EG1ControllerInvert ? 0x01 : 0x00) | |
1717 |
|
GIG_EG_CTR_ATTACK_INFLUENCE_ENCODE(EG1ControllerAttackInfluence) | |
1718 |
|
GIG_EG_CTR_DECAY_INFLUENCE_ENCODE(EG1ControllerDecayInfluence) | |
1719 |
|
GIG_EG_CTR_RELEASE_INFLUENCE_ENCODE(EG1ControllerReleaseInfluence); |
1720 |
|
pData[45] = eg1ctrloptions; |
1721 |
|
|
1722 |
|
const uint8_t eg2ctl = (uint8_t) EncodeLeverageController(EG2Controller); |
1723 |
|
pData[46] = eg2ctl; |
1724 |
|
|
1725 |
|
const uint8_t eg2ctrloptions = |
1726 |
|
(EG2ControllerInvert ? 0x01 : 0x00) | |
1727 |
|
GIG_EG_CTR_ATTACK_INFLUENCE_ENCODE(EG2ControllerAttackInfluence) | |
1728 |
|
GIG_EG_CTR_DECAY_INFLUENCE_ENCODE(EG2ControllerDecayInfluence) | |
1729 |
|
GIG_EG_CTR_RELEASE_INFLUENCE_ENCODE(EG2ControllerReleaseInfluence); |
1730 |
|
pData[47] = eg2ctrloptions; |
1731 |
|
|
1732 |
|
const int32_t lfo1freq = (int32_t) GIG_EXP_ENCODE(LFO1Frequency); |
1733 |
|
store32(&pData[48], lfo1freq); |
1734 |
|
|
1735 |
|
const int32_t eg2attack = (int32_t) GIG_EXP_ENCODE(EG2Attack); |
1736 |
|
store32(&pData[52], eg2attack); |
1737 |
|
|
1738 |
|
const int32_t eg2decay1 = (int32_t) GIG_EXP_ENCODE(EG2Decay1); |
1739 |
|
store32(&pData[56], eg2decay1); |
1740 |
|
|
1741 |
|
// next 2 bytes unknown |
1742 |
|
|
1743 |
|
store16(&pData[62], EG2Sustain); |
1744 |
|
|
1745 |
|
const int32_t eg2release = (int32_t) GIG_EXP_ENCODE(EG2Release); |
1746 |
|
store32(&pData[64], eg2release); |
1747 |
|
|
1748 |
|
// next 2 bytes unknown |
1749 |
|
|
1750 |
|
store16(&pData[70], LFO2ControlDepth); |
1751 |
|
|
1752 |
|
const int32_t lfo2freq = (int32_t) GIG_EXP_ENCODE(LFO2Frequency); |
1753 |
|
store32(&pData[72], lfo2freq); |
1754 |
|
|
1755 |
|
// next 2 bytes unknown |
1756 |
|
|
1757 |
|
store16(&pData[78], LFO2InternalDepth); |
1758 |
|
|
1759 |
|
const int32_t eg1decay2 = (int32_t) (EG1InfiniteSustain) ? 0x7fffffff : (int32_t) GIG_EXP_ENCODE(EG1Decay2); |
1760 |
|
store32(&pData[80], eg1decay2); |
1761 |
|
|
1762 |
|
// next 2 bytes unknown |
1763 |
|
|
1764 |
|
store16(&pData[86], EG1PreAttack); |
1765 |
|
|
1766 |
|
const int32_t eg2decay2 = (int32_t) (EG2InfiniteSustain) ? 0x7fffffff : (int32_t) GIG_EXP_ENCODE(EG2Decay2); |
1767 |
|
store32(&pData[88], eg2decay2); |
1768 |
|
|
1769 |
|
// next 2 bytes unknown |
1770 |
|
|
1771 |
|
store16(&pData[94], EG2PreAttack); |
1772 |
|
|
1773 |
|
{ |
1774 |
|
if (VelocityResponseDepth > 4) throw Exception("VelocityResponseDepth must be between 0 and 4"); |
1775 |
|
uint8_t velocityresponse = VelocityResponseDepth; |
1776 |
|
switch (VelocityResponseCurve) { |
1777 |
|
case curve_type_nonlinear: |
1778 |
|
break; |
1779 |
|
case curve_type_linear: |
1780 |
|
velocityresponse += 5; |
1781 |
|
break; |
1782 |
|
case curve_type_special: |
1783 |
|
velocityresponse += 10; |
1784 |
|
break; |
1785 |
|
case curve_type_unknown: |
1786 |
|
default: |
1787 |
|
throw Exception("Could not update DimensionRegion's chunk, unknown VelocityResponseCurve selected"); |
1788 |
|
} |
1789 |
|
pData[96] = velocityresponse; |
1790 |
} |
} |
1791 |
VelocityResponseCurveScaling = _3ewa->ReadUint8(); |
|
1792 |
AttenuationControllerThreshold = _3ewa->ReadInt8(); |
{ |
1793 |
_3ewa->ReadInt32(); // unknown |
if (ReleaseVelocityResponseDepth > 4) throw Exception("ReleaseVelocityResponseDepth must be between 0 and 4"); |
1794 |
SampleStartOffset = (uint16_t) _3ewa->ReadInt16(); |
uint8_t releasevelocityresponse = ReleaseVelocityResponseDepth; |
1795 |
_3ewa->ReadInt16(); // unknown |
switch (ReleaseVelocityResponseCurve) { |
1796 |
uint8_t pitchTrackDimensionBypass = _3ewa->ReadInt8(); |
case curve_type_nonlinear: |
1797 |
PitchTrack = GIG_PITCH_TRACK_EXTRACT(pitchTrackDimensionBypass); |
break; |
1798 |
if (pitchTrackDimensionBypass & 0x10) DimensionBypass = dim_bypass_ctrl_94; |
case curve_type_linear: |
1799 |
else if (pitchTrackDimensionBypass & 0x20) DimensionBypass = dim_bypass_ctrl_95; |
releasevelocityresponse += 5; |
1800 |
else DimensionBypass = dim_bypass_ctrl_none; |
break; |
1801 |
uint8_t pan = _3ewa->ReadUint8(); |
case curve_type_special: |
1802 |
Pan = (pan < 64) ? pan : -((int)pan - 63); // signed 7 bit -> signed 8 bit |
releasevelocityresponse += 10; |
1803 |
SelfMask = _3ewa->ReadInt8() & 0x01; |
break; |
1804 |
_3ewa->ReadInt8(); // unknown |
case curve_type_unknown: |
1805 |
uint8_t lfo3ctrl = _3ewa->ReadUint8(); |
default: |
1806 |
LFO3Controller = static_cast<lfo3_ctrl_t>(lfo3ctrl & 0x07); // lower 3 bits |
throw Exception("Could not update DimensionRegion's chunk, unknown ReleaseVelocityResponseCurve selected"); |
1807 |
LFO3Sync = lfo3ctrl & 0x20; // bit 5 |
} |
1808 |
InvertAttenuationController = lfo3ctrl & 0x80; // bit 7 |
pData[97] = releasevelocityresponse; |
|
AttenuationController = DecodeLeverageController(static_cast<_lev_ctrl_t>(_3ewa->ReadUint8())); |
|
|
uint8_t lfo2ctrl = _3ewa->ReadUint8(); |
|
|
LFO2Controller = static_cast<lfo2_ctrl_t>(lfo2ctrl & 0x07); // lower 3 bits |
|
|
LFO2FlipPhase = lfo2ctrl & 0x80; // bit 7 |
|
|
LFO2Sync = lfo2ctrl & 0x20; // bit 5 |
|
|
bool extResonanceCtrl = lfo2ctrl & 0x40; // bit 6 |
|
|
uint8_t lfo1ctrl = _3ewa->ReadUint8(); |
|
|
LFO1Controller = static_cast<lfo1_ctrl_t>(lfo1ctrl & 0x07); // lower 3 bits |
|
|
LFO1FlipPhase = lfo1ctrl & 0x80; // bit 7 |
|
|
LFO1Sync = lfo1ctrl & 0x40; // bit 6 |
|
|
VCFResonanceController = (extResonanceCtrl) ? static_cast<vcf_res_ctrl_t>(GIG_VCF_RESONANCE_CTRL_EXTRACT(lfo1ctrl)) |
|
|
: vcf_res_ctrl_none; |
|
|
uint16_t eg3depth = _3ewa->ReadUint16(); |
|
|
EG3Depth = (eg3depth <= 1200) ? eg3depth /* positives */ |
|
|
: (-1) * (int16_t) ((eg3depth ^ 0xffff) + 1); /* binary complementary for negatives */ |
|
|
_3ewa->ReadInt16(); // unknown |
|
|
ChannelOffset = _3ewa->ReadUint8() / 4; |
|
|
uint8_t regoptions = _3ewa->ReadUint8(); |
|
|
MSDecode = regoptions & 0x01; // bit 0 |
|
|
SustainDefeat = regoptions & 0x02; // bit 1 |
|
|
_3ewa->ReadInt16(); // unknown |
|
|
VelocityUpperLimit = _3ewa->ReadInt8(); |
|
|
_3ewa->ReadInt8(); // unknown |
|
|
_3ewa->ReadInt16(); // unknown |
|
|
ReleaseTriggerDecay = _3ewa->ReadUint8(); // release trigger decay |
|
|
_3ewa->ReadInt8(); // unknown |
|
|
_3ewa->ReadInt8(); // unknown |
|
|
EG1Hold = _3ewa->ReadUint8() & 0x80; // bit 7 |
|
|
uint8_t vcfcutoff = _3ewa->ReadUint8(); |
|
|
VCFEnabled = vcfcutoff & 0x80; // bit 7 |
|
|
VCFCutoff = vcfcutoff & 0x7f; // lower 7 bits |
|
|
VCFCutoffController = static_cast<vcf_cutoff_ctrl_t>(_3ewa->ReadUint8()); |
|
|
VCFVelocityScale = _3ewa->ReadUint8(); |
|
|
_3ewa->ReadInt8(); // unknown |
|
|
uint8_t vcfresonance = _3ewa->ReadUint8(); |
|
|
VCFResonance = vcfresonance & 0x7f; // lower 7 bits |
|
|
VCFResonanceDynamic = !(vcfresonance & 0x80); // bit 7 |
|
|
uint8_t vcfbreakpoint = _3ewa->ReadUint8(); |
|
|
VCFKeyboardTracking = vcfbreakpoint & 0x80; // bit 7 |
|
|
VCFKeyboardTrackingBreakpoint = vcfbreakpoint & 0x7f; // lower 7 bits |
|
|
uint8_t vcfvelocity = _3ewa->ReadUint8(); |
|
|
VCFVelocityDynamicRange = vcfvelocity % 5; |
|
|
VCFVelocityCurve = static_cast<curve_type_t>(vcfvelocity / 5); |
|
|
VCFType = static_cast<vcf_type_t>(_3ewa->ReadUint8()); |
|
|
if (VCFType == vcf_type_lowpass) { |
|
|
if (lfo3ctrl & 0x40) // bit 6 |
|
|
VCFType = vcf_type_lowpassturbo; |
|
1809 |
} |
} |
1810 |
|
|
1811 |
// get the corresponding velocity->volume table from the table map or create & calculate that table if it doesn't exist yet |
pData[98] = VelocityResponseCurveScaling; |
1812 |
uint32_t tableKey = (VelocityResponseCurve<<16) | (VelocityResponseDepth<<8) | VelocityResponseCurveScaling; |
|
1813 |
|
pData[99] = AttenuationControllerThreshold; |
1814 |
|
|
1815 |
|
// next 4 bytes unknown |
1816 |
|
|
1817 |
|
store16(&pData[104], SampleStartOffset); |
1818 |
|
|
1819 |
|
// next 2 bytes unknown |
1820 |
|
|
1821 |
|
{ |
1822 |
|
uint8_t pitchTrackDimensionBypass = GIG_PITCH_TRACK_ENCODE(PitchTrack); |
1823 |
|
switch (DimensionBypass) { |
1824 |
|
case dim_bypass_ctrl_94: |
1825 |
|
pitchTrackDimensionBypass |= 0x10; |
1826 |
|
break; |
1827 |
|
case dim_bypass_ctrl_95: |
1828 |
|
pitchTrackDimensionBypass |= 0x20; |
1829 |
|
break; |
1830 |
|
case dim_bypass_ctrl_none: |
1831 |
|
//FIXME: should we set anything here? |
1832 |
|
break; |
1833 |
|
default: |
1834 |
|
throw Exception("Could not update DimensionRegion's chunk, unknown DimensionBypass selected"); |
1835 |
|
} |
1836 |
|
pData[108] = pitchTrackDimensionBypass; |
1837 |
|
} |
1838 |
|
|
1839 |
|
const uint8_t pan = (Pan >= 0) ? Pan : ((-Pan) + 63); // signed 8 bit -> signed 7 bit |
1840 |
|
pData[109] = pan; |
1841 |
|
|
1842 |
|
const uint8_t selfmask = (SelfMask) ? 0x01 : 0x00; |
1843 |
|
pData[110] = selfmask; |
1844 |
|
|
1845 |
|
// next byte unknown |
1846 |
|
|
1847 |
|
{ |
1848 |
|
uint8_t lfo3ctrl = LFO3Controller & 0x07; // lower 3 bits |
1849 |
|
if (LFO3Sync) lfo3ctrl |= 0x20; // bit 5 |
1850 |
|
if (InvertAttenuationController) lfo3ctrl |= 0x80; // bit 7 |
1851 |
|
if (VCFType == vcf_type_lowpassturbo) lfo3ctrl |= 0x40; // bit 6 |
1852 |
|
pData[112] = lfo3ctrl; |
1853 |
|
} |
1854 |
|
|
1855 |
|
const uint8_t attenctl = EncodeLeverageController(AttenuationController); |
1856 |
|
pData[113] = attenctl; |
1857 |
|
|
1858 |
|
{ |
1859 |
|
uint8_t lfo2ctrl = LFO2Controller & 0x07; // lower 3 bits |
1860 |
|
if (LFO2FlipPhase) lfo2ctrl |= 0x80; // bit 7 |
1861 |
|
if (LFO2Sync) lfo2ctrl |= 0x20; // bit 5 |
1862 |
|
if (VCFResonanceController != vcf_res_ctrl_none) lfo2ctrl |= 0x40; // bit 6 |
1863 |
|
pData[114] = lfo2ctrl; |
1864 |
|
} |
1865 |
|
|
1866 |
|
{ |
1867 |
|
uint8_t lfo1ctrl = LFO1Controller & 0x07; // lower 3 bits |
1868 |
|
if (LFO1FlipPhase) lfo1ctrl |= 0x80; // bit 7 |
1869 |
|
if (LFO1Sync) lfo1ctrl |= 0x40; // bit 6 |
1870 |
|
if (VCFResonanceController != vcf_res_ctrl_none) |
1871 |
|
lfo1ctrl |= GIG_VCF_RESONANCE_CTRL_ENCODE(VCFResonanceController); |
1872 |
|
pData[115] = lfo1ctrl; |
1873 |
|
} |
1874 |
|
|
1875 |
|
const uint16_t eg3depth = (EG3Depth >= 0) ? EG3Depth |
1876 |
|
: uint16_t(((-EG3Depth) - 1) ^ 0xfff); /* binary complementary for negatives */ |
1877 |
|
store16(&pData[116], eg3depth); |
1878 |
|
|
1879 |
|
// next 2 bytes unknown |
1880 |
|
|
1881 |
|
const uint8_t channeloffset = ChannelOffset * 4; |
1882 |
|
pData[120] = channeloffset; |
1883 |
|
|
1884 |
|
{ |
1885 |
|
uint8_t regoptions = 0; |
1886 |
|
if (MSDecode) regoptions |= 0x01; // bit 0 |
1887 |
|
if (SustainDefeat) regoptions |= 0x02; // bit 1 |
1888 |
|
pData[121] = regoptions; |
1889 |
|
} |
1890 |
|
|
1891 |
|
// next 2 bytes unknown |
1892 |
|
|
1893 |
|
pData[124] = VelocityUpperLimit; |
1894 |
|
|
1895 |
|
// next 3 bytes unknown |
1896 |
|
|
1897 |
|
pData[128] = ReleaseTriggerDecay; |
1898 |
|
|
1899 |
|
// next 2 bytes unknown |
1900 |
|
|
1901 |
|
const uint8_t eg1hold = (EG1Hold) ? 0x80 : 0x00; // bit 7 |
1902 |
|
pData[131] = eg1hold; |
1903 |
|
|
1904 |
|
const uint8_t vcfcutoff = (VCFEnabled ? 0x80 : 0x00) | /* bit 7 */ |
1905 |
|
(VCFCutoff & 0x7f); /* lower 7 bits */ |
1906 |
|
pData[132] = vcfcutoff; |
1907 |
|
|
1908 |
|
pData[133] = VCFCutoffController; |
1909 |
|
|
1910 |
|
const uint8_t vcfvelscale = (VCFCutoffControllerInvert ? 0x80 : 0x00) | /* bit 7 */ |
1911 |
|
(VCFVelocityScale & 0x7f); /* lower 7 bits */ |
1912 |
|
pData[134] = vcfvelscale; |
1913 |
|
|
1914 |
|
// next byte unknown |
1915 |
|
|
1916 |
|
const uint8_t vcfresonance = (VCFResonanceDynamic ? 0x00 : 0x80) | /* bit 7 */ |
1917 |
|
(VCFResonance & 0x7f); /* lower 7 bits */ |
1918 |
|
pData[136] = vcfresonance; |
1919 |
|
|
1920 |
|
const uint8_t vcfbreakpoint = (VCFKeyboardTracking ? 0x80 : 0x00) | /* bit 7 */ |
1921 |
|
(VCFKeyboardTrackingBreakpoint & 0x7f); /* lower 7 bits */ |
1922 |
|
pData[137] = vcfbreakpoint; |
1923 |
|
|
1924 |
|
const uint8_t vcfvelocity = VCFVelocityDynamicRange % 5 + |
1925 |
|
VCFVelocityCurve * 5; |
1926 |
|
pData[138] = vcfvelocity; |
1927 |
|
|
1928 |
|
const uint8_t vcftype = (VCFType == vcf_type_lowpassturbo) ? vcf_type_lowpass : VCFType; |
1929 |
|
pData[139] = vcftype; |
1930 |
|
|
1931 |
|
if (chunksize >= 148) { |
1932 |
|
memcpy(&pData[140], DimensionUpperLimits, 8); |
1933 |
|
} |
1934 |
|
} |
1935 |
|
|
1936 |
|
double* DimensionRegion::GetReleaseVelocityTable(curve_type_t releaseVelocityResponseCurve, uint8_t releaseVelocityResponseDepth) { |
1937 |
|
curve_type_t curveType = releaseVelocityResponseCurve; |
1938 |
|
uint8_t depth = releaseVelocityResponseDepth; |
1939 |
|
// this models a strange behaviour or bug in GSt: two of the |
1940 |
|
// velocity response curves for release time are not used even |
1941 |
|
// if specified, instead another curve is chosen. |
1942 |
|
if ((curveType == curve_type_nonlinear && depth == 0) || |
1943 |
|
(curveType == curve_type_special && depth == 4)) { |
1944 |
|
curveType = curve_type_nonlinear; |
1945 |
|
depth = 3; |
1946 |
|
} |
1947 |
|
return GetVelocityTable(curveType, depth, 0); |
1948 |
|
} |
1949 |
|
|
1950 |
|
double* DimensionRegion::GetCutoffVelocityTable(curve_type_t vcfVelocityCurve, |
1951 |
|
uint8_t vcfVelocityDynamicRange, |
1952 |
|
uint8_t vcfVelocityScale, |
1953 |
|
vcf_cutoff_ctrl_t vcfCutoffController) |
1954 |
|
{ |
1955 |
|
curve_type_t curveType = vcfVelocityCurve; |
1956 |
|
uint8_t depth = vcfVelocityDynamicRange; |
1957 |
|
// even stranger GSt: two of the velocity response curves for |
1958 |
|
// filter cutoff are not used, instead another special curve |
1959 |
|
// is chosen. This curve is not used anywhere else. |
1960 |
|
if ((curveType == curve_type_nonlinear && depth == 0) || |
1961 |
|
(curveType == curve_type_special && depth == 4)) { |
1962 |
|
curveType = curve_type_special; |
1963 |
|
depth = 5; |
1964 |
|
} |
1965 |
|
return GetVelocityTable(curveType, depth, |
1966 |
|
(vcfCutoffController <= vcf_cutoff_ctrl_none2) |
1967 |
|
? vcfVelocityScale : 0); |
1968 |
|
} |
1969 |
|
|
1970 |
|
// get the corresponding velocity table from the table map or create & calculate that table if it doesn't exist yet |
1971 |
|
double* DimensionRegion::GetVelocityTable(curve_type_t curveType, uint8_t depth, uint8_t scaling) |
1972 |
|
{ |
1973 |
|
double* table; |
1974 |
|
uint32_t tableKey = (curveType<<16) | (depth<<8) | scaling; |
1975 |
if (pVelocityTables->count(tableKey)) { // if key exists |
if (pVelocityTables->count(tableKey)) { // if key exists |
1976 |
pVelocityAttenuationTable = (*pVelocityTables)[tableKey]; |
table = (*pVelocityTables)[tableKey]; |
1977 |
} |
} |
1978 |
else { |
else { |
1979 |
pVelocityAttenuationTable = |
table = CreateVelocityTable(curveType, depth, scaling); |
1980 |
CreateVelocityTable(VelocityResponseCurve, |
(*pVelocityTables)[tableKey] = table; // put the new table into the tables map |
|
VelocityResponseDepth, |
|
|
VelocityResponseCurveScaling); |
|
|
(*pVelocityTables)[tableKey] = pVelocityAttenuationTable; // put the new table into the tables map |
|
1981 |
} |
} |
1982 |
|
return table; |
1983 |
|
} |
1984 |
|
|
1985 |
SampleAttenuation = pow(10.0, -Gain / (20.0 * 655360)); |
Region* DimensionRegion::GetParent() const { |
1986 |
|
return pRegion; |
1987 |
} |
} |
1988 |
|
|
1989 |
leverage_ctrl_t DimensionRegion::DecodeLeverageController(_lev_ctrl_t EncodedController) { |
leverage_ctrl_t DimensionRegion::DecodeLeverageController(_lev_ctrl_t EncodedController) { |
2104 |
return decodedcontroller; |
return decodedcontroller; |
2105 |
} |
} |
2106 |
|
|
2107 |
|
DimensionRegion::_lev_ctrl_t DimensionRegion::EncodeLeverageController(leverage_ctrl_t DecodedController) { |
2108 |
|
_lev_ctrl_t encodedcontroller; |
2109 |
|
switch (DecodedController.type) { |
2110 |
|
// special controller |
2111 |
|
case leverage_ctrl_t::type_none: |
2112 |
|
encodedcontroller = _lev_ctrl_none; |
2113 |
|
break; |
2114 |
|
case leverage_ctrl_t::type_velocity: |
2115 |
|
encodedcontroller = _lev_ctrl_velocity; |
2116 |
|
break; |
2117 |
|
case leverage_ctrl_t::type_channelaftertouch: |
2118 |
|
encodedcontroller = _lev_ctrl_channelaftertouch; |
2119 |
|
break; |
2120 |
|
|
2121 |
|
// ordinary MIDI control change controller |
2122 |
|
case leverage_ctrl_t::type_controlchange: |
2123 |
|
switch (DecodedController.controller_number) { |
2124 |
|
case 1: |
2125 |
|
encodedcontroller = _lev_ctrl_modwheel; |
2126 |
|
break; |
2127 |
|
case 2: |
2128 |
|
encodedcontroller = _lev_ctrl_breath; |
2129 |
|
break; |
2130 |
|
case 4: |
2131 |
|
encodedcontroller = _lev_ctrl_foot; |
2132 |
|
break; |
2133 |
|
case 12: |
2134 |
|
encodedcontroller = _lev_ctrl_effect1; |
2135 |
|
break; |
2136 |
|
case 13: |
2137 |
|
encodedcontroller = _lev_ctrl_effect2; |
2138 |
|
break; |
2139 |
|
case 16: |
2140 |
|
encodedcontroller = _lev_ctrl_genpurpose1; |
2141 |
|
break; |
2142 |
|
case 17: |
2143 |
|
encodedcontroller = _lev_ctrl_genpurpose2; |
2144 |
|
break; |
2145 |
|
case 18: |
2146 |
|
encodedcontroller = _lev_ctrl_genpurpose3; |
2147 |
|
break; |
2148 |
|
case 19: |
2149 |
|
encodedcontroller = _lev_ctrl_genpurpose4; |
2150 |
|
break; |
2151 |
|
case 5: |
2152 |
|
encodedcontroller = _lev_ctrl_portamentotime; |
2153 |
|
break; |
2154 |
|
case 64: |
2155 |
|
encodedcontroller = _lev_ctrl_sustainpedal; |
2156 |
|
break; |
2157 |
|
case 65: |
2158 |
|
encodedcontroller = _lev_ctrl_portamento; |
2159 |
|
break; |
2160 |
|
case 66: |
2161 |
|
encodedcontroller = _lev_ctrl_sostenutopedal; |
2162 |
|
break; |
2163 |
|
case 67: |
2164 |
|
encodedcontroller = _lev_ctrl_softpedal; |
2165 |
|
break; |
2166 |
|
case 80: |
2167 |
|
encodedcontroller = _lev_ctrl_genpurpose5; |
2168 |
|
break; |
2169 |
|
case 81: |
2170 |
|
encodedcontroller = _lev_ctrl_genpurpose6; |
2171 |
|
break; |
2172 |
|
case 82: |
2173 |
|
encodedcontroller = _lev_ctrl_genpurpose7; |
2174 |
|
break; |
2175 |
|
case 83: |
2176 |
|
encodedcontroller = _lev_ctrl_genpurpose8; |
2177 |
|
break; |
2178 |
|
case 91: |
2179 |
|
encodedcontroller = _lev_ctrl_effect1depth; |
2180 |
|
break; |
2181 |
|
case 92: |
2182 |
|
encodedcontroller = _lev_ctrl_effect2depth; |
2183 |
|
break; |
2184 |
|
case 93: |
2185 |
|
encodedcontroller = _lev_ctrl_effect3depth; |
2186 |
|
break; |
2187 |
|
case 94: |
2188 |
|
encodedcontroller = _lev_ctrl_effect4depth; |
2189 |
|
break; |
2190 |
|
case 95: |
2191 |
|
encodedcontroller = _lev_ctrl_effect5depth; |
2192 |
|
break; |
2193 |
|
default: |
2194 |
|
throw gig::Exception("leverage controller number is not supported by the gig format"); |
2195 |
|
} |
2196 |
|
break; |
2197 |
|
default: |
2198 |
|
throw gig::Exception("Unknown leverage controller type."); |
2199 |
|
} |
2200 |
|
return encodedcontroller; |
2201 |
|
} |
2202 |
|
|
2203 |
DimensionRegion::~DimensionRegion() { |
DimensionRegion::~DimensionRegion() { |
2204 |
Instances--; |
Instances--; |
2205 |
if (!Instances) { |
if (!Instances) { |
2213 |
delete pVelocityTables; |
delete pVelocityTables; |
2214 |
pVelocityTables = NULL; |
pVelocityTables = NULL; |
2215 |
} |
} |
2216 |
|
if (VelocityTable) delete[] VelocityTable; |
2217 |
} |
} |
2218 |
|
|
2219 |
/** |
/** |
2231 |
return pVelocityAttenuationTable[MIDIKeyVelocity]; |
return pVelocityAttenuationTable[MIDIKeyVelocity]; |
2232 |
} |
} |
2233 |
|
|
2234 |
|
double DimensionRegion::GetVelocityRelease(uint8_t MIDIKeyVelocity) { |
2235 |
|
return pVelocityReleaseTable[MIDIKeyVelocity]; |
2236 |
|
} |
2237 |
|
|
2238 |
|
double DimensionRegion::GetVelocityCutoff(uint8_t MIDIKeyVelocity) { |
2239 |
|
return pVelocityCutoffTable[MIDIKeyVelocity]; |
2240 |
|
} |
2241 |
|
|
2242 |
|
/** |
2243 |
|
* Updates the respective member variable and the lookup table / cache |
2244 |
|
* that depends on this value. |
2245 |
|
*/ |
2246 |
|
void DimensionRegion::SetVelocityResponseCurve(curve_type_t curve) { |
2247 |
|
pVelocityAttenuationTable = |
2248 |
|
GetVelocityTable( |
2249 |
|
curve, VelocityResponseDepth, VelocityResponseCurveScaling |
2250 |
|
); |
2251 |
|
VelocityResponseCurve = curve; |
2252 |
|
} |
2253 |
|
|
2254 |
|
/** |
2255 |
|
* Updates the respective member variable and the lookup table / cache |
2256 |
|
* that depends on this value. |
2257 |
|
*/ |
2258 |
|
void DimensionRegion::SetVelocityResponseDepth(uint8_t depth) { |
2259 |
|
pVelocityAttenuationTable = |
2260 |
|
GetVelocityTable( |
2261 |
|
VelocityResponseCurve, depth, VelocityResponseCurveScaling |
2262 |
|
); |
2263 |
|
VelocityResponseDepth = depth; |
2264 |
|
} |
2265 |
|
|
2266 |
|
/** |
2267 |
|
* Updates the respective member variable and the lookup table / cache |
2268 |
|
* that depends on this value. |
2269 |
|
*/ |
2270 |
|
void DimensionRegion::SetVelocityResponseCurveScaling(uint8_t scaling) { |
2271 |
|
pVelocityAttenuationTable = |
2272 |
|
GetVelocityTable( |
2273 |
|
VelocityResponseCurve, VelocityResponseDepth, scaling |
2274 |
|
); |
2275 |
|
VelocityResponseCurveScaling = scaling; |
2276 |
|
} |
2277 |
|
|
2278 |
|
/** |
2279 |
|
* Updates the respective member variable and the lookup table / cache |
2280 |
|
* that depends on this value. |
2281 |
|
*/ |
2282 |
|
void DimensionRegion::SetReleaseVelocityResponseCurve(curve_type_t curve) { |
2283 |
|
pVelocityReleaseTable = GetReleaseVelocityTable(curve, ReleaseVelocityResponseDepth); |
2284 |
|
ReleaseVelocityResponseCurve = curve; |
2285 |
|
} |
2286 |
|
|
2287 |
|
/** |
2288 |
|
* Updates the respective member variable and the lookup table / cache |
2289 |
|
* that depends on this value. |
2290 |
|
*/ |
2291 |
|
void DimensionRegion::SetReleaseVelocityResponseDepth(uint8_t depth) { |
2292 |
|
pVelocityReleaseTable = GetReleaseVelocityTable(ReleaseVelocityResponseCurve, depth); |
2293 |
|
ReleaseVelocityResponseDepth = depth; |
2294 |
|
} |
2295 |
|
|
2296 |
|
/** |
2297 |
|
* Updates the respective member variable and the lookup table / cache |
2298 |
|
* that depends on this value. |
2299 |
|
*/ |
2300 |
|
void DimensionRegion::SetVCFCutoffController(vcf_cutoff_ctrl_t controller) { |
2301 |
|
pVelocityCutoffTable = GetCutoffVelocityTable(VCFVelocityCurve, VCFVelocityDynamicRange, VCFVelocityScale, controller); |
2302 |
|
VCFCutoffController = controller; |
2303 |
|
} |
2304 |
|
|
2305 |
|
/** |
2306 |
|
* Updates the respective member variable and the lookup table / cache |
2307 |
|
* that depends on this value. |
2308 |
|
*/ |
2309 |
|
void DimensionRegion::SetVCFVelocityCurve(curve_type_t curve) { |
2310 |
|
pVelocityCutoffTable = GetCutoffVelocityTable(curve, VCFVelocityDynamicRange, VCFVelocityScale, VCFCutoffController); |
2311 |
|
VCFVelocityCurve = curve; |
2312 |
|
} |
2313 |
|
|
2314 |
|
/** |
2315 |
|
* Updates the respective member variable and the lookup table / cache |
2316 |
|
* that depends on this value. |
2317 |
|
*/ |
2318 |
|
void DimensionRegion::SetVCFVelocityDynamicRange(uint8_t range) { |
2319 |
|
pVelocityCutoffTable = GetCutoffVelocityTable(VCFVelocityCurve, range, VCFVelocityScale, VCFCutoffController); |
2320 |
|
VCFVelocityDynamicRange = range; |
2321 |
|
} |
2322 |
|
|
2323 |
|
/** |
2324 |
|
* Updates the respective member variable and the lookup table / cache |
2325 |
|
* that depends on this value. |
2326 |
|
*/ |
2327 |
|
void DimensionRegion::SetVCFVelocityScale(uint8_t scaling) { |
2328 |
|
pVelocityCutoffTable = GetCutoffVelocityTable(VCFVelocityCurve, VCFVelocityDynamicRange, scaling, VCFCutoffController); |
2329 |
|
VCFVelocityScale = scaling; |
2330 |
|
} |
2331 |
|
|
2332 |
double* DimensionRegion::CreateVelocityTable(curve_type_t curveType, uint8_t depth, uint8_t scaling) { |
double* DimensionRegion::CreateVelocityTable(curve_type_t curveType, uint8_t depth, uint8_t scaling) { |
2333 |
|
|
2334 |
// line-segment approximations of the 15 velocity curves |
// line-segment approximations of the 15 velocity curves |
2362 |
const int spe4[] = { 1, 4, 23, 5, 49, 13, 57, 17, 92, 57, 122, 127, |
const int spe4[] = { 1, 4, 23, 5, 49, 13, 57, 17, 92, 57, 122, 127, |
2363 |
127, 127 }; |
127, 127 }; |
2364 |
|
|
2365 |
|
// this is only used by the VCF velocity curve |
2366 |
|
const int spe5[] = { 1, 2, 30, 5, 60, 19, 77, 70, 83, 85, 88, 106, |
2367 |
|
91, 127, 127, 127 }; |
2368 |
|
|
2369 |
const int* const curves[] = { non0, non1, non2, non3, non4, |
const int* const curves[] = { non0, non1, non2, non3, non4, |
2370 |
lin0, lin1, lin2, lin3, lin4, |
lin0, lin1, lin2, lin3, lin4, |
2371 |
spe0, spe1, spe2, spe3, spe4 }; |
spe0, spe1, spe2, spe3, spe4, spe5 }; |
2372 |
|
|
2373 |
double* const table = new double[128]; |
double* const table = new double[128]; |
2374 |
|
|
2412 |
|
|
2413 |
// Actual Loading |
// Actual Loading |
2414 |
|
|
2415 |
|
if (!file->GetAutoLoad()) return; |
2416 |
|
|
2417 |
LoadDimensionRegions(rgnList); |
LoadDimensionRegions(rgnList); |
2418 |
|
|
2419 |
RIFF::Chunk* _3lnk = rgnList->GetSubChunk(CHUNK_ID_3LNK); |
RIFF::Chunk* _3lnk = rgnList->GetSubChunk(CHUNK_ID_3LNK); |
2422 |
for (int i = 0; i < dimensionBits; i++) { |
for (int i = 0; i < dimensionBits; i++) { |
2423 |
dimension_t dimension = static_cast<dimension_t>(_3lnk->ReadUint8()); |
dimension_t dimension = static_cast<dimension_t>(_3lnk->ReadUint8()); |
2424 |
uint8_t bits = _3lnk->ReadUint8(); |
uint8_t bits = _3lnk->ReadUint8(); |
2425 |
|
_3lnk->ReadUint8(); // bit position of the dimension (bits[0] + bits[1] + ... + bits[i-1]) |
2426 |
|
_3lnk->ReadUint8(); // (1 << bit position of next dimension) - (1 << bit position of this dimension) |
2427 |
|
uint8_t zones = _3lnk->ReadUint8(); // new for v3: number of zones doesn't have to be == pow(2,bits) |
2428 |
if (dimension == dimension_none) { // inactive dimension |
if (dimension == dimension_none) { // inactive dimension |
2429 |
pDimensionDefinitions[i].dimension = dimension_none; |
pDimensionDefinitions[i].dimension = dimension_none; |
2430 |
pDimensionDefinitions[i].bits = 0; |
pDimensionDefinitions[i].bits = 0; |
2431 |
pDimensionDefinitions[i].zones = 0; |
pDimensionDefinitions[i].zones = 0; |
2432 |
pDimensionDefinitions[i].split_type = split_type_bit; |
pDimensionDefinitions[i].split_type = split_type_bit; |
|
pDimensionDefinitions[i].ranges = NULL; |
|
2433 |
pDimensionDefinitions[i].zone_size = 0; |
pDimensionDefinitions[i].zone_size = 0; |
2434 |
} |
} |
2435 |
else { // active dimension |
else { // active dimension |
2436 |
pDimensionDefinitions[i].dimension = dimension; |
pDimensionDefinitions[i].dimension = dimension; |
2437 |
pDimensionDefinitions[i].bits = bits; |
pDimensionDefinitions[i].bits = bits; |
2438 |
pDimensionDefinitions[i].zones = 0x01 << bits; // = pow(2,bits) |
pDimensionDefinitions[i].zones = zones ? zones : 0x01 << bits; // = pow(2,bits) |
2439 |
pDimensionDefinitions[i].split_type = (dimension == dimension_layer || |
pDimensionDefinitions[i].split_type = __resolveSplitType(dimension); |
2440 |
dimension == dimension_samplechannel || |
pDimensionDefinitions[i].zone_size = __resolveZoneSize(pDimensionDefinitions[i]); |
|
dimension == dimension_releasetrigger || |
|
|
dimension == dimension_roundrobin || |
|
|
dimension == dimension_random) ? split_type_bit |
|
|
: split_type_normal; |
|
|
pDimensionDefinitions[i].ranges = NULL; // it's not possible to check velocity dimensions for custom defined ranges at this point |
|
|
pDimensionDefinitions[i].zone_size = |
|
|
(pDimensionDefinitions[i].split_type == split_type_normal) ? 128 / pDimensionDefinitions[i].zones |
|
|
: 0; |
|
2441 |
Dimensions++; |
Dimensions++; |
2442 |
|
|
2443 |
// if this is a layer dimension, remember the amount of layers |
// if this is a layer dimension, remember the amount of layers |
2444 |
if (dimension == dimension_layer) Layers = pDimensionDefinitions[i].zones; |
if (dimension == dimension_layer) Layers = pDimensionDefinitions[i].zones; |
2445 |
} |
} |
2446 |
_3lnk->SetPos(6, RIFF::stream_curpos); // jump forward to next dimension definition |
_3lnk->SetPos(3, RIFF::stream_curpos); // jump forward to next dimension definition |
2447 |
} |
} |
2448 |
|
for (int i = dimensionBits ; i < 8 ; i++) pDimensionDefinitions[i].bits = 0; |
2449 |
|
|
2450 |
// check velocity dimension (if there is one) for custom defined zone ranges |
// if there's a velocity dimension and custom velocity zone splits are used, |
2451 |
for (uint i = 0; i < Dimensions; i++) { |
// update the VelocityTables in the dimension regions |
2452 |
dimension_def_t* pDimDef = pDimensionDefinitions + i; |
UpdateVelocityTable(); |
|
if (pDimDef->dimension == dimension_velocity) { |
|
|
if (pDimensionRegions[0]->VelocityUpperLimit == 0) { |
|
|
// no custom defined ranges |
|
|
pDimDef->split_type = split_type_normal; |
|
|
pDimDef->ranges = NULL; |
|
|
} |
|
|
else { // custom defined ranges |
|
|
pDimDef->split_type = split_type_customvelocity; |
|
|
pDimDef->ranges = new range_t[pDimDef->zones]; |
|
|
uint8_t bits[8] = { 0 }; |
|
|
int previousUpperLimit = -1; |
|
|
for (int velocityZone = 0; velocityZone < pDimDef->zones; velocityZone++) { |
|
|
bits[i] = velocityZone; |
|
|
DimensionRegion* pDimRegion = GetDimensionRegionByBit(bits); |
|
|
|
|
|
pDimDef->ranges[velocityZone].low = previousUpperLimit + 1; |
|
|
pDimDef->ranges[velocityZone].high = pDimRegion->VelocityUpperLimit; |
|
|
previousUpperLimit = pDimDef->ranges[velocityZone].high; |
|
|
// fill velocity table |
|
|
for (int i = pDimDef->ranges[velocityZone].low; i <= pDimDef->ranges[velocityZone].high; i++) { |
|
|
VelocityTable[i] = velocityZone; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
2453 |
|
|
2454 |
// jump to start of the wave pool indices (if not already there) |
// jump to start of the wave pool indices (if not already there) |
|
File* file = (File*) GetParent()->GetParent(); |
|
2455 |
if (file->pVersion && file->pVersion->major == 3) |
if (file->pVersion && file->pVersion->major == 3) |
2456 |
_3lnk->SetPos(68); // version 3 has a different 3lnk structure |
_3lnk->SetPos(68); // version 3 has a different 3lnk structure |
2457 |
else |
else |
2458 |
_3lnk->SetPos(44); |
_3lnk->SetPos(44); |
2459 |
|
|
2460 |
// load sample references |
// load sample references (if auto loading is enabled) |
2461 |
for (uint i = 0; i < DimensionRegions; i++) { |
if (file->GetAutoLoad()) { |
2462 |
uint32_t wavepoolindex = _3lnk->ReadUint32(); |
for (uint i = 0; i < DimensionRegions; i++) { |
2463 |
pDimensionRegions[i]->pSample = GetSampleFromWavePool(wavepoolindex); |
uint32_t wavepoolindex = _3lnk->ReadUint32(); |
2464 |
|
if (file->pWavePoolTable) pDimensionRegions[i]->pSample = GetSampleFromWavePool(wavepoolindex); |
2465 |
|
} |
2466 |
|
GetSample(); // load global region sample reference |
2467 |
|
} |
2468 |
|
} else { |
2469 |
|
DimensionRegions = 0; |
2470 |
|
for (int i = 0 ; i < 8 ; i++) { |
2471 |
|
pDimensionDefinitions[i].dimension = dimension_none; |
2472 |
|
pDimensionDefinitions[i].bits = 0; |
2473 |
|
pDimensionDefinitions[i].zones = 0; |
2474 |
|
} |
2475 |
|
} |
2476 |
|
|
2477 |
|
// make sure there is at least one dimension region |
2478 |
|
if (!DimensionRegions) { |
2479 |
|
RIFF::List* _3prg = rgnList->GetSubList(LIST_TYPE_3PRG); |
2480 |
|
if (!_3prg) _3prg = rgnList->AddSubList(LIST_TYPE_3PRG); |
2481 |
|
RIFF::List* _3ewl = _3prg->AddSubList(LIST_TYPE_3EWL); |
2482 |
|
pDimensionRegions[0] = new DimensionRegion(this, _3ewl); |
2483 |
|
DimensionRegions = 1; |
2484 |
|
} |
2485 |
|
} |
2486 |
|
|
2487 |
|
/** |
2488 |
|
* Apply Region settings and all its DimensionRegions to the respective |
2489 |
|
* RIFF chunks. You have to call File::Save() to make changes persistent. |
2490 |
|
* |
2491 |
|
* Usually there is absolutely no need to call this method explicitly. |
2492 |
|
* It will be called automatically when File::Save() was called. |
2493 |
|
* |
2494 |
|
* @throws gig::Exception if samples cannot be dereferenced |
2495 |
|
*/ |
2496 |
|
void Region::UpdateChunks() { |
2497 |
|
// in the gig format we don't care about the Region's sample reference |
2498 |
|
// but we still have to provide some existing one to not corrupt the |
2499 |
|
// file, so to avoid the latter we simply always assign the sample of |
2500 |
|
// the first dimension region of this region |
2501 |
|
pSample = pDimensionRegions[0]->pSample; |
2502 |
|
|
2503 |
|
// first update base class's chunks |
2504 |
|
DLS::Region::UpdateChunks(); |
2505 |
|
|
2506 |
|
// update dimension region's chunks |
2507 |
|
for (int i = 0; i < DimensionRegions; i++) { |
2508 |
|
pDimensionRegions[i]->UpdateChunks(); |
2509 |
|
} |
2510 |
|
|
2511 |
|
File* pFile = (File*) GetParent()->GetParent(); |
2512 |
|
bool version3 = pFile->pVersion && pFile->pVersion->major == 3; |
2513 |
|
const int iMaxDimensions = version3 ? 8 : 5; |
2514 |
|
const int iMaxDimensionRegions = version3 ? 256 : 32; |
2515 |
|
|
2516 |
|
// make sure '3lnk' chunk exists |
2517 |
|
RIFF::Chunk* _3lnk = pCkRegion->GetSubChunk(CHUNK_ID_3LNK); |
2518 |
|
if (!_3lnk) { |
2519 |
|
const int _3lnkChunkSize = version3 ? 1092 : 172; |
2520 |
|
_3lnk = pCkRegion->AddSubChunk(CHUNK_ID_3LNK, _3lnkChunkSize); |
2521 |
|
memset(_3lnk->LoadChunkData(), 0, _3lnkChunkSize); |
2522 |
|
|
2523 |
|
// move 3prg to last position |
2524 |
|
pCkRegion->MoveSubChunk(pCkRegion->GetSubList(LIST_TYPE_3PRG), 0); |
2525 |
|
} |
2526 |
|
|
2527 |
|
// update dimension definitions in '3lnk' chunk |
2528 |
|
uint8_t* pData = (uint8_t*) _3lnk->LoadChunkData(); |
2529 |
|
store32(&pData[0], DimensionRegions); |
2530 |
|
int shift = 0; |
2531 |
|
for (int i = 0; i < iMaxDimensions; i++) { |
2532 |
|
pData[4 + i * 8] = (uint8_t) pDimensionDefinitions[i].dimension; |
2533 |
|
pData[5 + i * 8] = pDimensionDefinitions[i].bits; |
2534 |
|
pData[6 + i * 8] = pDimensionDefinitions[i].dimension == dimension_none ? 0 : shift; |
2535 |
|
pData[7 + i * 8] = (1 << (shift + pDimensionDefinitions[i].bits)) - (1 << shift); |
2536 |
|
pData[8 + i * 8] = pDimensionDefinitions[i].zones; |
2537 |
|
// next 3 bytes unknown, always zero? |
2538 |
|
|
2539 |
|
shift += pDimensionDefinitions[i].bits; |
2540 |
|
} |
2541 |
|
|
2542 |
|
// update wave pool table in '3lnk' chunk |
2543 |
|
const int iWavePoolOffset = version3 ? 68 : 44; |
2544 |
|
for (uint i = 0; i < iMaxDimensionRegions; i++) { |
2545 |
|
int iWaveIndex = -1; |
2546 |
|
if (i < DimensionRegions) { |
2547 |
|
if (!pFile->pSamples || !pFile->pSamples->size()) throw gig::Exception("Could not update gig::Region, there are no samples"); |
2548 |
|
File::SampleList::iterator iter = pFile->pSamples->begin(); |
2549 |
|
File::SampleList::iterator end = pFile->pSamples->end(); |
2550 |
|
for (int index = 0; iter != end; ++iter, ++index) { |
2551 |
|
if (*iter == pDimensionRegions[i]->pSample) { |
2552 |
|
iWaveIndex = index; |
2553 |
|
break; |
2554 |
|
} |
2555 |
|
} |
2556 |
} |
} |
2557 |
|
store32(&pData[iWavePoolOffset + i * 4], iWaveIndex); |
2558 |
} |
} |
|
else throw gig::Exception("Mandatory <3lnk> chunk not found."); |
|
2559 |
} |
} |
2560 |
|
|
2561 |
void Region::LoadDimensionRegions(RIFF::List* rgn) { |
void Region::LoadDimensionRegions(RIFF::List* rgn) { |
2565 |
RIFF::List* _3ewl = _3prg->GetFirstSubList(); |
RIFF::List* _3ewl = _3prg->GetFirstSubList(); |
2566 |
while (_3ewl) { |
while (_3ewl) { |
2567 |
if (_3ewl->GetListType() == LIST_TYPE_3EWL) { |
if (_3ewl->GetListType() == LIST_TYPE_3EWL) { |
2568 |
pDimensionRegions[dimensionRegionNr] = new DimensionRegion(_3ewl); |
pDimensionRegions[dimensionRegionNr] = new DimensionRegion(this, _3ewl); |
2569 |
dimensionRegionNr++; |
dimensionRegionNr++; |
2570 |
} |
} |
2571 |
_3ewl = _3prg->GetNextSubList(); |
_3ewl = _3prg->GetNextSubList(); |
2574 |
} |
} |
2575 |
} |
} |
2576 |
|
|
2577 |
Region::~Region() { |
void Region::SetKeyRange(uint16_t Low, uint16_t High) { |
2578 |
for (uint i = 0; i < Dimensions; i++) { |
// update KeyRange struct and make sure regions are in correct order |
2579 |
if (pDimensionDefinitions[i].ranges) delete[] pDimensionDefinitions[i].ranges; |
DLS::Region::SetKeyRange(Low, High); |
2580 |
|
// update Region key table for fast lookup |
2581 |
|
((gig::Instrument*)GetParent())->UpdateRegionKeyTable(); |
2582 |
|
} |
2583 |
|
|
2584 |
|
void Region::UpdateVelocityTable() { |
2585 |
|
// get velocity dimension's index |
2586 |
|
int veldim = -1; |
2587 |
|
for (int i = 0 ; i < Dimensions ; i++) { |
2588 |
|
if (pDimensionDefinitions[i].dimension == gig::dimension_velocity) { |
2589 |
|
veldim = i; |
2590 |
|
break; |
2591 |
|
} |
2592 |
} |
} |
2593 |
|
if (veldim == -1) return; |
2594 |
|
|
2595 |
|
int step = 1; |
2596 |
|
for (int i = 0 ; i < veldim ; i++) step <<= pDimensionDefinitions[i].bits; |
2597 |
|
int skipveldim = (step << pDimensionDefinitions[veldim].bits) - step; |
2598 |
|
int end = step * pDimensionDefinitions[veldim].zones; |
2599 |
|
|
2600 |
|
// loop through all dimension regions for all dimensions except the velocity dimension |
2601 |
|
int dim[8] = { 0 }; |
2602 |
|
for (int i = 0 ; i < DimensionRegions ; i++) { |
2603 |
|
|
2604 |
|
if (pDimensionRegions[i]->DimensionUpperLimits[veldim] || |
2605 |
|
pDimensionRegions[i]->VelocityUpperLimit) { |
2606 |
|
// create the velocity table |
2607 |
|
uint8_t* table = pDimensionRegions[i]->VelocityTable; |
2608 |
|
if (!table) { |
2609 |
|
table = new uint8_t[128]; |
2610 |
|
pDimensionRegions[i]->VelocityTable = table; |
2611 |
|
} |
2612 |
|
int tableidx = 0; |
2613 |
|
int velocityZone = 0; |
2614 |
|
if (pDimensionRegions[i]->DimensionUpperLimits[veldim]) { // gig3 |
2615 |
|
for (int k = i ; k < end ; k += step) { |
2616 |
|
DimensionRegion *d = pDimensionRegions[k]; |
2617 |
|
for (; tableidx <= d->DimensionUpperLimits[veldim] ; tableidx++) table[tableidx] = velocityZone; |
2618 |
|
velocityZone++; |
2619 |
|
} |
2620 |
|
} else { // gig2 |
2621 |
|
for (int k = i ; k < end ; k += step) { |
2622 |
|
DimensionRegion *d = pDimensionRegions[k]; |
2623 |
|
for (; tableidx <= d->VelocityUpperLimit ; tableidx++) table[tableidx] = velocityZone; |
2624 |
|
velocityZone++; |
2625 |
|
} |
2626 |
|
} |
2627 |
|
} else { |
2628 |
|
if (pDimensionRegions[i]->VelocityTable) { |
2629 |
|
delete[] pDimensionRegions[i]->VelocityTable; |
2630 |
|
pDimensionRegions[i]->VelocityTable = 0; |
2631 |
|
} |
2632 |
|
} |
2633 |
|
|
2634 |
|
int j; |
2635 |
|
int shift = 0; |
2636 |
|
for (j = 0 ; j < Dimensions ; j++) { |
2637 |
|
if (j == veldim) i += skipveldim; // skip velocity dimension |
2638 |
|
else { |
2639 |
|
dim[j]++; |
2640 |
|
if (dim[j] < pDimensionDefinitions[j].zones) break; |
2641 |
|
else { |
2642 |
|
// skip unused dimension regions |
2643 |
|
dim[j] = 0; |
2644 |
|
i += ((1 << pDimensionDefinitions[j].bits) - |
2645 |
|
pDimensionDefinitions[j].zones) << shift; |
2646 |
|
} |
2647 |
|
} |
2648 |
|
shift += pDimensionDefinitions[j].bits; |
2649 |
|
} |
2650 |
|
if (j == Dimensions) break; |
2651 |
|
} |
2652 |
|
} |
2653 |
|
|
2654 |
|
/** @brief Einstein would have dreamed of it - create a new dimension. |
2655 |
|
* |
2656 |
|
* Creates a new dimension with the dimension definition given by |
2657 |
|
* \a pDimDef. The appropriate amount of DimensionRegions will be created. |
2658 |
|
* There is a hard limit of dimensions and total amount of "bits" all |
2659 |
|
* dimensions can have. This limit is dependant to what gig file format |
2660 |
|
* version this file refers to. The gig v2 (and lower) format has a |
2661 |
|
* dimension limit and total amount of bits limit of 5, whereas the gig v3 |
2662 |
|
* format has a limit of 8. |
2663 |
|
* |
2664 |
|
* @param pDimDef - defintion of the new dimension |
2665 |
|
* @throws gig::Exception if dimension of the same type exists already |
2666 |
|
* @throws gig::Exception if amount of dimensions or total amount of |
2667 |
|
* dimension bits limit is violated |
2668 |
|
*/ |
2669 |
|
void Region::AddDimension(dimension_def_t* pDimDef) { |
2670 |
|
// check if max. amount of dimensions reached |
2671 |
|
File* file = (File*) GetParent()->GetParent(); |
2672 |
|
const int iMaxDimensions = (file->pVersion && file->pVersion->major == 3) ? 8 : 5; |
2673 |
|
if (Dimensions >= iMaxDimensions) |
2674 |
|
throw gig::Exception("Could not add new dimension, max. amount of " + ToString(iMaxDimensions) + " dimensions already reached"); |
2675 |
|
// check if max. amount of dimension bits reached |
2676 |
|
int iCurrentBits = 0; |
2677 |
|
for (int i = 0; i < Dimensions; i++) |
2678 |
|
iCurrentBits += pDimensionDefinitions[i].bits; |
2679 |
|
if (iCurrentBits >= iMaxDimensions) |
2680 |
|
throw gig::Exception("Could not add new dimension, max. amount of " + ToString(iMaxDimensions) + " dimension bits already reached"); |
2681 |
|
const int iNewBits = iCurrentBits + pDimDef->bits; |
2682 |
|
if (iNewBits > iMaxDimensions) |
2683 |
|
throw gig::Exception("Could not add new dimension, new dimension would exceed max. amount of " + ToString(iMaxDimensions) + " dimension bits"); |
2684 |
|
// check if there's already a dimensions of the same type |
2685 |
|
for (int i = 0; i < Dimensions; i++) |
2686 |
|
if (pDimensionDefinitions[i].dimension == pDimDef->dimension) |
2687 |
|
throw gig::Exception("Could not add new dimension, there is already a dimension of the same type"); |
2688 |
|
|
2689 |
|
// pos is where the new dimension should be placed, normally |
2690 |
|
// last in list, except for the samplechannel dimension which |
2691 |
|
// has to be first in list |
2692 |
|
int pos = pDimDef->dimension == dimension_samplechannel ? 0 : Dimensions; |
2693 |
|
int bitpos = 0; |
2694 |
|
for (int i = 0 ; i < pos ; i++) |
2695 |
|
bitpos += pDimensionDefinitions[i].bits; |
2696 |
|
|
2697 |
|
// make room for the new dimension |
2698 |
|
for (int i = Dimensions ; i > pos ; i--) pDimensionDefinitions[i] = pDimensionDefinitions[i - 1]; |
2699 |
|
for (int i = 0 ; i < (1 << iCurrentBits) ; i++) { |
2700 |
|
for (int j = Dimensions ; j > pos ; j--) { |
2701 |
|
pDimensionRegions[i]->DimensionUpperLimits[j] = |
2702 |
|
pDimensionRegions[i]->DimensionUpperLimits[j - 1]; |
2703 |
|
} |
2704 |
|
} |
2705 |
|
|
2706 |
|
// assign definition of new dimension |
2707 |
|
pDimensionDefinitions[pos] = *pDimDef; |
2708 |
|
|
2709 |
|
// auto correct certain dimension definition fields (where possible) |
2710 |
|
pDimensionDefinitions[pos].split_type = |
2711 |
|
__resolveSplitType(pDimensionDefinitions[pos].dimension); |
2712 |
|
pDimensionDefinitions[pos].zone_size = |
2713 |
|
__resolveZoneSize(pDimensionDefinitions[pos]); |
2714 |
|
|
2715 |
|
// create new dimension region(s) for this new dimension, and make |
2716 |
|
// sure that the dimension regions are placed correctly in both the |
2717 |
|
// RIFF list and the pDimensionRegions array |
2718 |
|
RIFF::Chunk* moveTo = NULL; |
2719 |
|
RIFF::List* _3prg = pCkRegion->GetSubList(LIST_TYPE_3PRG); |
2720 |
|
for (int i = (1 << iCurrentBits) - (1 << bitpos) ; i >= 0 ; i -= (1 << bitpos)) { |
2721 |
|
for (int k = 0 ; k < (1 << bitpos) ; k++) { |
2722 |
|
pDimensionRegions[(i << pDimDef->bits) + k] = pDimensionRegions[i + k]; |
2723 |
|
} |
2724 |
|
for (int j = 1 ; j < (1 << pDimDef->bits) ; j++) { |
2725 |
|
for (int k = 0 ; k < (1 << bitpos) ; k++) { |
2726 |
|
RIFF::List* pNewDimRgnListChunk = _3prg->AddSubList(LIST_TYPE_3EWL); |
2727 |
|
if (moveTo) _3prg->MoveSubChunk(pNewDimRgnListChunk, moveTo); |
2728 |
|
// create a new dimension region and copy all parameter values from |
2729 |
|
// an existing dimension region |
2730 |
|
pDimensionRegions[(i << pDimDef->bits) + (j << bitpos) + k] = |
2731 |
|
new DimensionRegion(pNewDimRgnListChunk, *pDimensionRegions[i + k]); |
2732 |
|
|
2733 |
|
DimensionRegions++; |
2734 |
|
} |
2735 |
|
} |
2736 |
|
moveTo = pDimensionRegions[i]->pParentList; |
2737 |
|
} |
2738 |
|
|
2739 |
|
// initialize the upper limits for this dimension |
2740 |
|
int mask = (1 << bitpos) - 1; |
2741 |
|
for (int z = 0 ; z < pDimDef->zones ; z++) { |
2742 |
|
uint8_t upperLimit = uint8_t((z + 1) * 128.0 / pDimDef->zones - 1); |
2743 |
|
for (int i = 0 ; i < 1 << iCurrentBits ; i++) { |
2744 |
|
pDimensionRegions[((i & ~mask) << pDimDef->bits) | |
2745 |
|
(z << bitpos) | |
2746 |
|
(i & mask)]->DimensionUpperLimits[pos] = upperLimit; |
2747 |
|
} |
2748 |
|
} |
2749 |
|
|
2750 |
|
Dimensions++; |
2751 |
|
|
2752 |
|
// if this is a layer dimension, update 'Layers' attribute |
2753 |
|
if (pDimDef->dimension == dimension_layer) Layers = pDimDef->zones; |
2754 |
|
|
2755 |
|
UpdateVelocityTable(); |
2756 |
|
} |
2757 |
|
|
2758 |
|
/** @brief Delete an existing dimension. |
2759 |
|
* |
2760 |
|
* Deletes the dimension given by \a pDimDef and deletes all respective |
2761 |
|
* dimension regions, that is all dimension regions where the dimension's |
2762 |
|
* bit(s) part is greater than 0. In case of a 'sustain pedal' dimension |
2763 |
|
* for example this would delete all dimension regions for the case(s) |
2764 |
|
* where the sustain pedal is pressed down. |
2765 |
|
* |
2766 |
|
* @param pDimDef - dimension to delete |
2767 |
|
* @throws gig::Exception if given dimension cannot be found |
2768 |
|
*/ |
2769 |
|
void Region::DeleteDimension(dimension_def_t* pDimDef) { |
2770 |
|
// get dimension's index |
2771 |
|
int iDimensionNr = -1; |
2772 |
|
for (int i = 0; i < Dimensions; i++) { |
2773 |
|
if (&pDimensionDefinitions[i] == pDimDef) { |
2774 |
|
iDimensionNr = i; |
2775 |
|
break; |
2776 |
|
} |
2777 |
|
} |
2778 |
|
if (iDimensionNr < 0) throw gig::Exception("Invalid dimension_def_t pointer"); |
2779 |
|
|
2780 |
|
// get amount of bits below the dimension to delete |
2781 |
|
int iLowerBits = 0; |
2782 |
|
for (int i = 0; i < iDimensionNr; i++) |
2783 |
|
iLowerBits += pDimensionDefinitions[i].bits; |
2784 |
|
|
2785 |
|
// get amount ot bits above the dimension to delete |
2786 |
|
int iUpperBits = 0; |
2787 |
|
for (int i = iDimensionNr + 1; i < Dimensions; i++) |
2788 |
|
iUpperBits += pDimensionDefinitions[i].bits; |
2789 |
|
|
2790 |
|
RIFF::List* _3prg = pCkRegion->GetSubList(LIST_TYPE_3PRG); |
2791 |
|
|
2792 |
|
// delete dimension regions which belong to the given dimension |
2793 |
|
// (that is where the dimension's bit > 0) |
2794 |
|
for (int iUpperBit = 0; iUpperBit < 1 << iUpperBits; iUpperBit++) { |
2795 |
|
for (int iObsoleteBit = 1; iObsoleteBit < 1 << pDimensionDefinitions[iDimensionNr].bits; iObsoleteBit++) { |
2796 |
|
for (int iLowerBit = 0; iLowerBit < 1 << iLowerBits; iLowerBit++) { |
2797 |
|
int iToDelete = iUpperBit << (pDimensionDefinitions[iDimensionNr].bits + iLowerBits) | |
2798 |
|
iObsoleteBit << iLowerBits | |
2799 |
|
iLowerBit; |
2800 |
|
|
2801 |
|
_3prg->DeleteSubChunk(pDimensionRegions[iToDelete]->pParentList); |
2802 |
|
delete pDimensionRegions[iToDelete]; |
2803 |
|
pDimensionRegions[iToDelete] = NULL; |
2804 |
|
DimensionRegions--; |
2805 |
|
} |
2806 |
|
} |
2807 |
|
} |
2808 |
|
|
2809 |
|
// defrag pDimensionRegions array |
2810 |
|
// (that is remove the NULL spaces within the pDimensionRegions array) |
2811 |
|
for (int iFrom = 2, iTo = 1; iFrom < 256 && iTo < 256 - 1; iTo++) { |
2812 |
|
if (!pDimensionRegions[iTo]) { |
2813 |
|
if (iFrom <= iTo) iFrom = iTo + 1; |
2814 |
|
while (!pDimensionRegions[iFrom] && iFrom < 256) iFrom++; |
2815 |
|
if (iFrom < 256 && pDimensionRegions[iFrom]) { |
2816 |
|
pDimensionRegions[iTo] = pDimensionRegions[iFrom]; |
2817 |
|
pDimensionRegions[iFrom] = NULL; |
2818 |
|
} |
2819 |
|
} |
2820 |
|
} |
2821 |
|
|
2822 |
|
// remove the this dimension from the upper limits arrays |
2823 |
|
for (int j = 0 ; j < 256 && pDimensionRegions[j] ; j++) { |
2824 |
|
DimensionRegion* d = pDimensionRegions[j]; |
2825 |
|
for (int i = iDimensionNr + 1; i < Dimensions; i++) { |
2826 |
|
d->DimensionUpperLimits[i - 1] = d->DimensionUpperLimits[i]; |
2827 |
|
} |
2828 |
|
d->DimensionUpperLimits[Dimensions - 1] = 127; |
2829 |
|
} |
2830 |
|
|
2831 |
|
// 'remove' dimension definition |
2832 |
|
for (int i = iDimensionNr + 1; i < Dimensions; i++) { |
2833 |
|
pDimensionDefinitions[i - 1] = pDimensionDefinitions[i]; |
2834 |
|
} |
2835 |
|
pDimensionDefinitions[Dimensions - 1].dimension = dimension_none; |
2836 |
|
pDimensionDefinitions[Dimensions - 1].bits = 0; |
2837 |
|
pDimensionDefinitions[Dimensions - 1].zones = 0; |
2838 |
|
|
2839 |
|
Dimensions--; |
2840 |
|
|
2841 |
|
// if this was a layer dimension, update 'Layers' attribute |
2842 |
|
if (pDimDef->dimension == dimension_layer) Layers = 1; |
2843 |
|
} |
2844 |
|
|
2845 |
|
Region::~Region() { |
2846 |
for (int i = 0; i < 256; i++) { |
for (int i = 0; i < 256; i++) { |
2847 |
if (pDimensionRegions[i]) delete pDimensionRegions[i]; |
if (pDimensionRegions[i]) delete pDimensionRegions[i]; |
2848 |
} |
} |
2867 |
* @see Dimensions |
* @see Dimensions |
2868 |
*/ |
*/ |
2869 |
DimensionRegion* Region::GetDimensionRegionByValue(const uint DimValues[8]) { |
DimensionRegion* Region::GetDimensionRegionByValue(const uint DimValues[8]) { |
2870 |
uint8_t bits[8] = { 0 }; |
uint8_t bits; |
2871 |
|
int veldim = -1; |
2872 |
|
int velbitpos; |
2873 |
|
int bitpos = 0; |
2874 |
|
int dimregidx = 0; |
2875 |
for (uint i = 0; i < Dimensions; i++) { |
for (uint i = 0; i < Dimensions; i++) { |
2876 |
bits[i] = DimValues[i]; |
if (pDimensionDefinitions[i].dimension == dimension_velocity) { |
2877 |
switch (pDimensionDefinitions[i].split_type) { |
// the velocity dimension must be handled after the other dimensions |
2878 |
case split_type_normal: |
veldim = i; |
2879 |
bits[i] /= pDimensionDefinitions[i].zone_size; |
velbitpos = bitpos; |
2880 |
break; |
} else { |
2881 |
case split_type_customvelocity: |
switch (pDimensionDefinitions[i].split_type) { |
2882 |
bits[i] = VelocityTable[bits[i]]; |
case split_type_normal: |
2883 |
break; |
if (pDimensionRegions[0]->DimensionUpperLimits[i]) { |
2884 |
case split_type_bit: // the value is already the sought dimension bit number |
// gig3: all normal dimensions (not just the velocity dimension) have custom zone ranges |
2885 |
const uint8_t limiter_mask = (0xff << pDimensionDefinitions[i].bits) ^ 0xff; |
for (bits = 0 ; bits < pDimensionDefinitions[i].zones ; bits++) { |
2886 |
bits[i] = bits[i] & limiter_mask; // just make sure the value don't uses more bits than allowed |
if (DimValues[i] <= pDimensionRegions[bits << bitpos]->DimensionUpperLimits[i]) break; |
2887 |
break; |
} |
2888 |
|
} else { |
2889 |
|
// gig2: evenly sized zones |
2890 |
|
bits = uint8_t(DimValues[i] / pDimensionDefinitions[i].zone_size); |
2891 |
|
} |
2892 |
|
break; |
2893 |
|
case split_type_bit: // the value is already the sought dimension bit number |
2894 |
|
const uint8_t limiter_mask = (0xff << pDimensionDefinitions[i].bits) ^ 0xff; |
2895 |
|
bits = DimValues[i] & limiter_mask; // just make sure the value doesn't use more bits than allowed |
2896 |
|
break; |
2897 |
|
} |
2898 |
|
dimregidx |= bits << bitpos; |
2899 |
} |
} |
2900 |
|
bitpos += pDimensionDefinitions[i].bits; |
2901 |
|
} |
2902 |
|
DimensionRegion* dimreg = pDimensionRegions[dimregidx]; |
2903 |
|
if (veldim != -1) { |
2904 |
|
// (dimreg is now the dimension region for the lowest velocity) |
2905 |
|
if (dimreg->VelocityTable) // custom defined zone ranges |
2906 |
|
bits = dimreg->VelocityTable[DimValues[veldim]]; |
2907 |
|
else // normal split type |
2908 |
|
bits = uint8_t(DimValues[veldim] / pDimensionDefinitions[veldim].zone_size); |
2909 |
|
|
2910 |
|
dimregidx |= bits << velbitpos; |
2911 |
|
dimreg = pDimensionRegions[dimregidx]; |
2912 |
} |
} |
2913 |
return GetDimensionRegionByBit(bits); |
return dimreg; |
2914 |
} |
} |
2915 |
|
|
2916 |
/** |
/** |
2947 |
else return static_cast<gig::Sample*>(pSample = GetSampleFromWavePool(WavePoolTableIndex)); |
else return static_cast<gig::Sample*>(pSample = GetSampleFromWavePool(WavePoolTableIndex)); |
2948 |
} |
} |
2949 |
|
|
2950 |
Sample* Region::GetSampleFromWavePool(unsigned int WavePoolTableIndex) { |
Sample* Region::GetSampleFromWavePool(unsigned int WavePoolTableIndex, progress_t* pProgress) { |
2951 |
if ((int32_t)WavePoolTableIndex == -1) return NULL; |
if ((int32_t)WavePoolTableIndex == -1) return NULL; |
2952 |
File* file = (File*) GetParent()->GetParent(); |
File* file = (File*) GetParent()->GetParent(); |
2953 |
|
if (!file->pWavePoolTable) return NULL; |
2954 |
unsigned long soughtoffset = file->pWavePoolTable[WavePoolTableIndex]; |
unsigned long soughtoffset = file->pWavePoolTable[WavePoolTableIndex]; |
2955 |
Sample* sample = file->GetFirstSample(); |
unsigned long soughtfileno = file->pWavePoolTableHi[WavePoolTableIndex]; |
2956 |
|
Sample* sample = file->GetFirstSample(pProgress); |
2957 |
while (sample) { |
while (sample) { |
2958 |
if (sample->ulWavePoolOffset == soughtoffset) return static_cast<gig::Sample*>(pSample = sample); |
if (sample->ulWavePoolOffset == soughtoffset && |
2959 |
|
sample->FileNo == soughtfileno) return static_cast<gig::Sample*>(sample); |
2960 |
sample = file->GetNextSample(); |
sample = file->GetNextSample(); |
2961 |
} |
} |
2962 |
return NULL; |
return NULL; |
2963 |
} |
} |
2964 |
|
|
2965 |
|
/** |
2966 |
|
* Make a (semi) deep copy of the Region object given by @a orig |
2967 |
|
* and assign it to this object. |
2968 |
|
* |
2969 |
|
* Note that all sample pointers referenced by @a orig are simply copied as |
2970 |
|
* memory address. Thus the respective samples are shared, not duplicated! |
2971 |
|
* |
2972 |
|
* @param orig - original Region object to be copied from |
2973 |
|
*/ |
2974 |
|
void Region::CopyAssign(const Region* orig) { |
2975 |
|
// handle base classes |
2976 |
|
DLS::Region::CopyAssign(orig); |
2977 |
|
|
2978 |
|
// handle own member variables |
2979 |
|
for (int i = Dimensions - 1; i >= 0; --i) { |
2980 |
|
DeleteDimension(&pDimensionDefinitions[i]); |
2981 |
|
} |
2982 |
|
Layers = 0; // just to be sure |
2983 |
|
for (int i = 0; i < orig->Dimensions; i++) { |
2984 |
|
// we need to copy the dim definition here, to avoid the compiler |
2985 |
|
// complaining about const-ness issue |
2986 |
|
dimension_def_t def = orig->pDimensionDefinitions[i]; |
2987 |
|
AddDimension(&def); |
2988 |
|
} |
2989 |
|
for (int i = 0; i < 256; i++) { |
2990 |
|
if (pDimensionRegions[i] && orig->pDimensionRegions[i]) { |
2991 |
|
pDimensionRegions[i]->CopyAssign( |
2992 |
|
orig->pDimensionRegions[i] |
2993 |
|
); |
2994 |
|
} |
2995 |
|
} |
2996 |
|
Layers = orig->Layers; |
2997 |
|
} |
2998 |
|
|
2999 |
|
|
3000 |
|
// *************** MidiRule *************** |
3001 |
|
// * |
3002 |
|
|
3003 |
|
MidiRuleCtrlTrigger::MidiRuleCtrlTrigger(RIFF::Chunk* _3ewg) { |
3004 |
|
_3ewg->SetPos(36); |
3005 |
|
Triggers = _3ewg->ReadUint8(); |
3006 |
|
_3ewg->SetPos(40); |
3007 |
|
ControllerNumber = _3ewg->ReadUint8(); |
3008 |
|
_3ewg->SetPos(46); |
3009 |
|
for (int i = 0 ; i < Triggers ; i++) { |
3010 |
|
pTriggers[i].TriggerPoint = _3ewg->ReadUint8(); |
3011 |
|
pTriggers[i].Descending = _3ewg->ReadUint8(); |
3012 |
|
pTriggers[i].VelSensitivity = _3ewg->ReadUint8(); |
3013 |
|
pTriggers[i].Key = _3ewg->ReadUint8(); |
3014 |
|
pTriggers[i].NoteOff = _3ewg->ReadUint8(); |
3015 |
|
pTriggers[i].Velocity = _3ewg->ReadUint8(); |
3016 |
|
pTriggers[i].OverridePedal = _3ewg->ReadUint8(); |
3017 |
|
_3ewg->ReadUint8(); |
3018 |
|
} |
3019 |
|
} |
3020 |
|
|
3021 |
|
MidiRuleCtrlTrigger::MidiRuleCtrlTrigger() : |
3022 |
|
ControllerNumber(0), |
3023 |
|
Triggers(0) { |
3024 |
|
} |
3025 |
|
|
3026 |
|
void MidiRuleCtrlTrigger::UpdateChunks(uint8_t* pData) const { |
3027 |
|
pData[32] = 4; |
3028 |
|
pData[33] = 16; |
3029 |
|
pData[36] = Triggers; |
3030 |
|
pData[40] = ControllerNumber; |
3031 |
|
for (int i = 0 ; i < Triggers ; i++) { |
3032 |
|
pData[46 + i * 8] = pTriggers[i].TriggerPoint; |
3033 |
|
pData[47 + i * 8] = pTriggers[i].Descending; |
3034 |
|
pData[48 + i * 8] = pTriggers[i].VelSensitivity; |
3035 |
|
pData[49 + i * 8] = pTriggers[i].Key; |
3036 |
|
pData[50 + i * 8] = pTriggers[i].NoteOff; |
3037 |
|
pData[51 + i * 8] = pTriggers[i].Velocity; |
3038 |
|
pData[52 + i * 8] = pTriggers[i].OverridePedal; |
3039 |
|
} |
3040 |
|
} |
3041 |
|
|
3042 |
|
MidiRuleLegato::MidiRuleLegato(RIFF::Chunk* _3ewg) { |
3043 |
|
_3ewg->SetPos(36); |
3044 |
|
LegatoSamples = _3ewg->ReadUint8(); // always 12 |
3045 |
|
_3ewg->SetPos(40); |
3046 |
|
BypassUseController = _3ewg->ReadUint8(); |
3047 |
|
BypassKey = _3ewg->ReadUint8(); |
3048 |
|
BypassController = _3ewg->ReadUint8(); |
3049 |
|
ThresholdTime = _3ewg->ReadUint16(); |
3050 |
|
_3ewg->ReadInt16(); |
3051 |
|
ReleaseTime = _3ewg->ReadUint16(); |
3052 |
|
_3ewg->ReadInt16(); |
3053 |
|
KeyRange.low = _3ewg->ReadUint8(); |
3054 |
|
KeyRange.high = _3ewg->ReadUint8(); |
3055 |
|
_3ewg->SetPos(64); |
3056 |
|
ReleaseTriggerKey = _3ewg->ReadUint8(); |
3057 |
|
AltSustain1Key = _3ewg->ReadUint8(); |
3058 |
|
AltSustain2Key = _3ewg->ReadUint8(); |
3059 |
|
} |
3060 |
|
|
3061 |
|
MidiRuleLegato::MidiRuleLegato() : |
3062 |
|
LegatoSamples(12), |
3063 |
|
BypassUseController(false), |
3064 |
|
BypassKey(0), |
3065 |
|
BypassController(1), |
3066 |
|
ThresholdTime(20), |
3067 |
|
ReleaseTime(20), |
3068 |
|
ReleaseTriggerKey(0), |
3069 |
|
AltSustain1Key(0), |
3070 |
|
AltSustain2Key(0) |
3071 |
|
{ |
3072 |
|
KeyRange.low = KeyRange.high = 0; |
3073 |
|
} |
3074 |
|
|
3075 |
|
void MidiRuleLegato::UpdateChunks(uint8_t* pData) const { |
3076 |
|
pData[32] = 0; |
3077 |
|
pData[33] = 16; |
3078 |
|
pData[36] = LegatoSamples; |
3079 |
|
pData[40] = BypassUseController; |
3080 |
|
pData[41] = BypassKey; |
3081 |
|
pData[42] = BypassController; |
3082 |
|
store16(&pData[43], ThresholdTime); |
3083 |
|
store16(&pData[47], ReleaseTime); |
3084 |
|
pData[51] = KeyRange.low; |
3085 |
|
pData[52] = KeyRange.high; |
3086 |
|
pData[64] = ReleaseTriggerKey; |
3087 |
|
pData[65] = AltSustain1Key; |
3088 |
|
pData[66] = AltSustain2Key; |
3089 |
|
} |
3090 |
|
|
3091 |
|
MidiRuleAlternator::MidiRuleAlternator(RIFF::Chunk* _3ewg) { |
3092 |
|
_3ewg->SetPos(36); |
3093 |
|
Articulations = _3ewg->ReadUint8(); |
3094 |
|
int flags = _3ewg->ReadUint8(); |
3095 |
|
Polyphonic = flags & 8; |
3096 |
|
Chained = flags & 4; |
3097 |
|
Selector = (flags & 2) ? selector_controller : |
3098 |
|
(flags & 1) ? selector_key_switch : selector_none; |
3099 |
|
Patterns = _3ewg->ReadUint8(); |
3100 |
|
_3ewg->ReadUint8(); // chosen row |
3101 |
|
_3ewg->ReadUint8(); // unknown |
3102 |
|
_3ewg->ReadUint8(); // unknown |
3103 |
|
_3ewg->ReadUint8(); // unknown |
3104 |
|
KeySwitchRange.low = _3ewg->ReadUint8(); |
3105 |
|
KeySwitchRange.high = _3ewg->ReadUint8(); |
3106 |
|
Controller = _3ewg->ReadUint8(); |
3107 |
|
PlayRange.low = _3ewg->ReadUint8(); |
3108 |
|
PlayRange.high = _3ewg->ReadUint8(); |
3109 |
|
|
3110 |
|
int n = std::min(int(Articulations), 32); |
3111 |
|
for (int i = 0 ; i < n ; i++) { |
3112 |
|
_3ewg->ReadString(pArticulations[i], 32); |
3113 |
|
} |
3114 |
|
_3ewg->SetPos(1072); |
3115 |
|
n = std::min(int(Patterns), 32); |
3116 |
|
for (int i = 0 ; i < n ; i++) { |
3117 |
|
_3ewg->ReadString(pPatterns[i].Name, 16); |
3118 |
|
pPatterns[i].Size = _3ewg->ReadUint8(); |
3119 |
|
_3ewg->Read(&pPatterns[i][0], 1, 32); |
3120 |
|
} |
3121 |
|
} |
3122 |
|
|
3123 |
|
MidiRuleAlternator::MidiRuleAlternator() : |
3124 |
|
Articulations(0), |
3125 |
|
Patterns(0), |
3126 |
|
Selector(selector_none), |
3127 |
|
Controller(0), |
3128 |
|
Polyphonic(false), |
3129 |
|
Chained(false) |
3130 |
|
{ |
3131 |
|
PlayRange.low = PlayRange.high = 0; |
3132 |
|
KeySwitchRange.low = KeySwitchRange.high = 0; |
3133 |
|
} |
3134 |
|
|
3135 |
|
void MidiRuleAlternator::UpdateChunks(uint8_t* pData) const { |
3136 |
|
pData[32] = 3; |
3137 |
|
pData[33] = 16; |
3138 |
|
pData[36] = Articulations; |
3139 |
|
pData[37] = (Polyphonic ? 8 : 0) | (Chained ? 4 : 0) | |
3140 |
|
(Selector == selector_controller ? 2 : |
3141 |
|
(Selector == selector_key_switch ? 1 : 0)); |
3142 |
|
pData[38] = Patterns; |
3143 |
|
|
3144 |
|
pData[43] = KeySwitchRange.low; |
3145 |
|
pData[44] = KeySwitchRange.high; |
3146 |
|
pData[45] = Controller; |
3147 |
|
pData[46] = PlayRange.low; |
3148 |
|
pData[47] = PlayRange.high; |
3149 |
|
|
3150 |
|
char* str = reinterpret_cast<char*>(pData); |
3151 |
|
int pos = 48; |
3152 |
|
int n = std::min(int(Articulations), 32); |
3153 |
|
for (int i = 0 ; i < n ; i++, pos += 32) { |
3154 |
|
strncpy(&str[pos], pArticulations[i].c_str(), 32); |
3155 |
|
} |
3156 |
|
|
3157 |
|
pos = 1072; |
3158 |
|
n = std::min(int(Patterns), 32); |
3159 |
|
for (int i = 0 ; i < n ; i++, pos += 49) { |
3160 |
|
strncpy(&str[pos], pPatterns[i].Name.c_str(), 16); |
3161 |
|
pData[pos + 16] = pPatterns[i].Size; |
3162 |
|
memcpy(&pData[pos + 16], &(pPatterns[i][0]), 32); |
3163 |
|
} |
3164 |
|
} |
3165 |
|
|
3166 |
// *************** Instrument *************** |
// *************** Instrument *************** |
3167 |
// * |
// * |
3168 |
|
|
3169 |
Instrument::Instrument(File* pFile, RIFF::List* insList) : DLS::Instrument((DLS::File*)pFile, insList) { |
Instrument::Instrument(File* pFile, RIFF::List* insList, progress_t* pProgress) : DLS::Instrument((DLS::File*)pFile, insList) { |
3170 |
|
static const DLS::Info::string_length_t fixedStringLengths[] = { |
3171 |
|
{ CHUNK_ID_INAM, 64 }, |
3172 |
|
{ CHUNK_ID_ISFT, 12 }, |
3173 |
|
{ 0, 0 } |
3174 |
|
}; |
3175 |
|
pInfo->SetFixedStringLengths(fixedStringLengths); |
3176 |
|
|
3177 |
// Initialization |
// Initialization |
3178 |
for (int i = 0; i < 128; i++) RegionKeyTable[i] = NULL; |
for (int i = 0; i < 128; i++) RegionKeyTable[i] = NULL; |
3179 |
RegionIndex = -1; |
EffectSend = 0; |
3180 |
|
Attenuation = 0; |
3181 |
|
FineTune = 0; |
3182 |
|
PitchbendRange = 0; |
3183 |
|
PianoReleaseMode = false; |
3184 |
|
DimensionKeyRange.low = 0; |
3185 |
|
DimensionKeyRange.high = 0; |
3186 |
|
pMidiRules = new MidiRule*[3]; |
3187 |
|
pMidiRules[0] = NULL; |
3188 |
|
|
3189 |
// Loading |
// Loading |
3190 |
RIFF::List* lart = insList->GetSubList(LIST_TYPE_LART); |
RIFF::List* lart = insList->GetSubList(LIST_TYPE_LART); |
3199 |
PianoReleaseMode = dimkeystart & 0x01; |
PianoReleaseMode = dimkeystart & 0x01; |
3200 |
DimensionKeyRange.low = dimkeystart >> 1; |
DimensionKeyRange.low = dimkeystart >> 1; |
3201 |
DimensionKeyRange.high = _3ewg->ReadUint8(); |
DimensionKeyRange.high = _3ewg->ReadUint8(); |
3202 |
|
|
3203 |
|
if (_3ewg->GetSize() > 32) { |
3204 |
|
// read MIDI rules |
3205 |
|
int i = 0; |
3206 |
|
_3ewg->SetPos(32); |
3207 |
|
uint8_t id1 = _3ewg->ReadUint8(); |
3208 |
|
uint8_t id2 = _3ewg->ReadUint8(); |
3209 |
|
|
3210 |
|
if (id2 == 16) { |
3211 |
|
if (id1 == 4) { |
3212 |
|
pMidiRules[i++] = new MidiRuleCtrlTrigger(_3ewg); |
3213 |
|
} else if (id1 == 0) { |
3214 |
|
pMidiRules[i++] = new MidiRuleLegato(_3ewg); |
3215 |
|
} else if (id1 == 3) { |
3216 |
|
pMidiRules[i++] = new MidiRuleAlternator(_3ewg); |
3217 |
|
} else { |
3218 |
|
pMidiRules[i++] = new MidiRuleUnknown; |
3219 |
|
} |
3220 |
|
} |
3221 |
|
else if (id1 != 0 || id2 != 0) { |
3222 |
|
pMidiRules[i++] = new MidiRuleUnknown; |
3223 |
|
} |
3224 |
|
//TODO: all the other types of rules |
3225 |
|
|
3226 |
|
pMidiRules[i] = NULL; |
3227 |
|
} |
3228 |
} |
} |
|
else throw gig::Exception("Mandatory <3ewg> chunk not found."); |
|
3229 |
} |
} |
|
else throw gig::Exception("Mandatory <lart> list chunk not found."); |
|
3230 |
|
|
3231 |
RIFF::List* lrgn = insList->GetSubList(LIST_TYPE_LRGN); |
if (pFile->GetAutoLoad()) { |
3232 |
if (!lrgn) throw gig::Exception("Mandatory chunks in <ins > chunk not found."); |
if (!pRegions) pRegions = new RegionList; |
3233 |
pRegions = new Region*[Regions]; |
RIFF::List* lrgn = insList->GetSubList(LIST_TYPE_LRGN); |
3234 |
for (uint i = 0; i < Regions; i++) pRegions[i] = NULL; |
if (lrgn) { |
3235 |
RIFF::List* rgn = lrgn->GetFirstSubList(); |
RIFF::List* rgn = lrgn->GetFirstSubList(); |
3236 |
unsigned int iRegion = 0; |
while (rgn) { |
3237 |
while (rgn) { |
if (rgn->GetListType() == LIST_TYPE_RGN) { |
3238 |
if (rgn->GetListType() == LIST_TYPE_RGN) { |
__notify_progress(pProgress, (float) pRegions->size() / (float) Regions); |
3239 |
pRegions[iRegion] = new Region(this, rgn); |
pRegions->push_back(new Region(this, rgn)); |
3240 |
iRegion++; |
} |
3241 |
} |
rgn = lrgn->GetNextSubList(); |
3242 |
rgn = lrgn->GetNextSubList(); |
} |
3243 |
} |
// Creating Region Key Table for fast lookup |
3244 |
|
UpdateRegionKeyTable(); |
3245 |
// Creating Region Key Table for fast lookup |
} |
3246 |
for (uint iReg = 0; iReg < Regions; iReg++) { |
} |
3247 |
for (int iKey = pRegions[iReg]->KeyRange.low; iKey <= pRegions[iReg]->KeyRange.high; iKey++) { |
|
3248 |
RegionKeyTable[iKey] = pRegions[iReg]; |
__notify_progress(pProgress, 1.0f); // notify done |
3249 |
|
} |
3250 |
|
|
3251 |
|
void Instrument::UpdateRegionKeyTable() { |
3252 |
|
for (int i = 0; i < 128; i++) RegionKeyTable[i] = NULL; |
3253 |
|
RegionList::iterator iter = pRegions->begin(); |
3254 |
|
RegionList::iterator end = pRegions->end(); |
3255 |
|
for (; iter != end; ++iter) { |
3256 |
|
gig::Region* pRegion = static_cast<gig::Region*>(*iter); |
3257 |
|
for (int iKey = pRegion->KeyRange.low; iKey <= pRegion->KeyRange.high; iKey++) { |
3258 |
|
RegionKeyTable[iKey] = pRegion; |
3259 |
} |
} |
3260 |
} |
} |
3261 |
} |
} |
3262 |
|
|
3263 |
Instrument::~Instrument() { |
Instrument::~Instrument() { |
3264 |
for (uint i = 0; i < Regions; i++) { |
for (int i = 0 ; pMidiRules[i] ; i++) { |
3265 |
if (pRegions) { |
delete pMidiRules[i]; |
3266 |
if (pRegions[i]) delete (pRegions[i]); |
} |
3267 |
|
delete[] pMidiRules; |
3268 |
|
} |
3269 |
|
|
3270 |
|
/** |
3271 |
|
* Apply Instrument with all its Regions to the respective RIFF chunks. |
3272 |
|
* You have to call File::Save() to make changes persistent. |
3273 |
|
* |
3274 |
|
* Usually there is absolutely no need to call this method explicitly. |
3275 |
|
* It will be called automatically when File::Save() was called. |
3276 |
|
* |
3277 |
|
* @throws gig::Exception if samples cannot be dereferenced |
3278 |
|
*/ |
3279 |
|
void Instrument::UpdateChunks() { |
3280 |
|
// first update base classes' chunks |
3281 |
|
DLS::Instrument::UpdateChunks(); |
3282 |
|
|
3283 |
|
// update Regions' chunks |
3284 |
|
{ |
3285 |
|
RegionList::iterator iter = pRegions->begin(); |
3286 |
|
RegionList::iterator end = pRegions->end(); |
3287 |
|
for (; iter != end; ++iter) |
3288 |
|
(*iter)->UpdateChunks(); |
3289 |
|
} |
3290 |
|
|
3291 |
|
// make sure 'lart' RIFF list chunk exists |
3292 |
|
RIFF::List* lart = pCkInstrument->GetSubList(LIST_TYPE_LART); |
3293 |
|
if (!lart) lart = pCkInstrument->AddSubList(LIST_TYPE_LART); |
3294 |
|
// make sure '3ewg' RIFF chunk exists |
3295 |
|
RIFF::Chunk* _3ewg = lart->GetSubChunk(CHUNK_ID_3EWG); |
3296 |
|
if (!_3ewg) { |
3297 |
|
File* pFile = (File*) GetParent(); |
3298 |
|
|
3299 |
|
// 3ewg is bigger in gig3, as it includes the iMIDI rules |
3300 |
|
int size = (pFile->pVersion && pFile->pVersion->major == 3) ? 16416 : 12; |
3301 |
|
_3ewg = lart->AddSubChunk(CHUNK_ID_3EWG, size); |
3302 |
|
memset(_3ewg->LoadChunkData(), 0, size); |
3303 |
|
} |
3304 |
|
// update '3ewg' RIFF chunk |
3305 |
|
uint8_t* pData = (uint8_t*) _3ewg->LoadChunkData(); |
3306 |
|
store16(&pData[0], EffectSend); |
3307 |
|
store32(&pData[2], Attenuation); |
3308 |
|
store16(&pData[6], FineTune); |
3309 |
|
store16(&pData[8], PitchbendRange); |
3310 |
|
const uint8_t dimkeystart = (PianoReleaseMode ? 0x01 : 0x00) | |
3311 |
|
DimensionKeyRange.low << 1; |
3312 |
|
pData[10] = dimkeystart; |
3313 |
|
pData[11] = DimensionKeyRange.high; |
3314 |
|
|
3315 |
|
if (pMidiRules[0] == 0 && _3ewg->GetSize() >= 34) { |
3316 |
|
pData[32] = 0; |
3317 |
|
pData[33] = 0; |
3318 |
|
} else { |
3319 |
|
for (int i = 0 ; pMidiRules[i] ; i++) { |
3320 |
|
pMidiRules[i]->UpdateChunks(pData); |
3321 |
} |
} |
3322 |
} |
} |
|
if (pRegions) delete[] pRegions; |
|
3323 |
} |
} |
3324 |
|
|
3325 |
/** |
/** |
3330 |
* there is no Region defined for the given \a Key |
* there is no Region defined for the given \a Key |
3331 |
*/ |
*/ |
3332 |
Region* Instrument::GetRegion(unsigned int Key) { |
Region* Instrument::GetRegion(unsigned int Key) { |
3333 |
if (!pRegions || Key > 127) return NULL; |
if (!pRegions || pRegions->empty() || Key > 127) return NULL; |
3334 |
return RegionKeyTable[Key]; |
return RegionKeyTable[Key]; |
3335 |
|
|
3336 |
/*for (int i = 0; i < Regions; i++) { |
/*for (int i = 0; i < Regions; i++) { |
3337 |
if (Key <= pRegions[i]->KeyRange.high && |
if (Key <= pRegions[i]->KeyRange.high && |
3338 |
Key >= pRegions[i]->KeyRange.low) return pRegions[i]; |
Key >= pRegions[i]->KeyRange.low) return pRegions[i]; |
3348 |
* @see GetNextRegion() |
* @see GetNextRegion() |
3349 |
*/ |
*/ |
3350 |
Region* Instrument::GetFirstRegion() { |
Region* Instrument::GetFirstRegion() { |
3351 |
if (!Regions) return NULL; |
if (!pRegions) return NULL; |
3352 |
RegionIndex = 1; |
RegionsIterator = pRegions->begin(); |
3353 |
return pRegions[0]; |
return static_cast<gig::Region*>( (RegionsIterator != pRegions->end()) ? *RegionsIterator : NULL ); |
3354 |
} |
} |
3355 |
|
|
3356 |
/** |
/** |
3362 |
* @see GetFirstRegion() |
* @see GetFirstRegion() |
3363 |
*/ |
*/ |
3364 |
Region* Instrument::GetNextRegion() { |
Region* Instrument::GetNextRegion() { |
3365 |
if (RegionIndex < 0 || uint32_t(RegionIndex) >= Regions) return NULL; |
if (!pRegions) return NULL; |
3366 |
return pRegions[RegionIndex++]; |
RegionsIterator++; |
3367 |
|
return static_cast<gig::Region*>( (RegionsIterator != pRegions->end()) ? *RegionsIterator : NULL ); |
3368 |
|
} |
3369 |
|
|
3370 |
|
Region* Instrument::AddRegion() { |
3371 |
|
// create new Region object (and its RIFF chunks) |
3372 |
|
RIFF::List* lrgn = pCkInstrument->GetSubList(LIST_TYPE_LRGN); |
3373 |
|
if (!lrgn) lrgn = pCkInstrument->AddSubList(LIST_TYPE_LRGN); |
3374 |
|
RIFF::List* rgn = lrgn->AddSubList(LIST_TYPE_RGN); |
3375 |
|
Region* pNewRegion = new Region(this, rgn); |
3376 |
|
pRegions->push_back(pNewRegion); |
3377 |
|
Regions = pRegions->size(); |
3378 |
|
// update Region key table for fast lookup |
3379 |
|
UpdateRegionKeyTable(); |
3380 |
|
// done |
3381 |
|
return pNewRegion; |
3382 |
|
} |
3383 |
|
|
3384 |
|
void Instrument::DeleteRegion(Region* pRegion) { |
3385 |
|
if (!pRegions) return; |
3386 |
|
DLS::Instrument::DeleteRegion((DLS::Region*) pRegion); |
3387 |
|
// update Region key table for fast lookup |
3388 |
|
UpdateRegionKeyTable(); |
3389 |
} |
} |
3390 |
|
|
3391 |
|
/** |
3392 |
|
* Returns a MIDI rule of the instrument. |
3393 |
|
* |
3394 |
|
* The list of MIDI rules, at least in gig v3, always contains at |
3395 |
|
* most two rules. The second rule can only be the DEF filter |
3396 |
|
* (which currently isn't supported by libgig). |
3397 |
|
* |
3398 |
|
* @param i - MIDI rule number |
3399 |
|
* @returns pointer address to MIDI rule number i or NULL if there is none |
3400 |
|
*/ |
3401 |
|
MidiRule* Instrument::GetMidiRule(int i) { |
3402 |
|
return pMidiRules[i]; |
3403 |
|
} |
3404 |
|
|
3405 |
|
/** |
3406 |
|
* Adds the "controller trigger" MIDI rule to the instrument. |
3407 |
|
* |
3408 |
|
* @returns the new MIDI rule |
3409 |
|
*/ |
3410 |
|
MidiRuleCtrlTrigger* Instrument::AddMidiRuleCtrlTrigger() { |
3411 |
|
delete pMidiRules[0]; |
3412 |
|
MidiRuleCtrlTrigger* r = new MidiRuleCtrlTrigger; |
3413 |
|
pMidiRules[0] = r; |
3414 |
|
pMidiRules[1] = 0; |
3415 |
|
return r; |
3416 |
|
} |
3417 |
|
|
3418 |
// *************** File *************** |
/** |
3419 |
|
* Adds the legato MIDI rule to the instrument. |
3420 |
|
* |
3421 |
|
* @returns the new MIDI rule |
3422 |
|
*/ |
3423 |
|
MidiRuleLegato* Instrument::AddMidiRuleLegato() { |
3424 |
|
delete pMidiRules[0]; |
3425 |
|
MidiRuleLegato* r = new MidiRuleLegato; |
3426 |
|
pMidiRules[0] = r; |
3427 |
|
pMidiRules[1] = 0; |
3428 |
|
return r; |
3429 |
|
} |
3430 |
|
|
3431 |
|
/** |
3432 |
|
* Adds the alternator MIDI rule to the instrument. |
3433 |
|
* |
3434 |
|
* @returns the new MIDI rule |
3435 |
|
*/ |
3436 |
|
MidiRuleAlternator* Instrument::AddMidiRuleAlternator() { |
3437 |
|
delete pMidiRules[0]; |
3438 |
|
MidiRuleAlternator* r = new MidiRuleAlternator; |
3439 |
|
pMidiRules[0] = r; |
3440 |
|
pMidiRules[1] = 0; |
3441 |
|
return r; |
3442 |
|
} |
3443 |
|
|
3444 |
|
/** |
3445 |
|
* Deletes a MIDI rule from the instrument. |
3446 |
|
* |
3447 |
|
* @param i - MIDI rule number |
3448 |
|
*/ |
3449 |
|
void Instrument::DeleteMidiRule(int i) { |
3450 |
|
delete pMidiRules[i]; |
3451 |
|
pMidiRules[i] = 0; |
3452 |
|
} |
3453 |
|
|
3454 |
|
/** |
3455 |
|
* Make a (semi) deep copy of the Instrument object given by @a orig |
3456 |
|
* and assign it to this object. |
3457 |
|
* |
3458 |
|
* Note that all sample pointers referenced by @a orig are simply copied as |
3459 |
|
* memory address. Thus the respective samples are shared, not duplicated! |
3460 |
|
* |
3461 |
|
* @param orig - original Instrument object to be copied from |
3462 |
|
*/ |
3463 |
|
void Instrument::CopyAssign(const Instrument* orig) { |
3464 |
|
// handle base class |
3465 |
|
// (without copying DLS region stuff) |
3466 |
|
DLS::Instrument::CopyAssignCore(orig); |
3467 |
|
|
3468 |
|
// handle own member variables |
3469 |
|
Attenuation = orig->Attenuation; |
3470 |
|
EffectSend = orig->EffectSend; |
3471 |
|
FineTune = orig->FineTune; |
3472 |
|
PitchbendRange = orig->PitchbendRange; |
3473 |
|
PianoReleaseMode = orig->PianoReleaseMode; |
3474 |
|
DimensionKeyRange = orig->DimensionKeyRange; |
3475 |
|
|
3476 |
|
// free old midi rules |
3477 |
|
for (int i = 0 ; pMidiRules[i] ; i++) { |
3478 |
|
delete pMidiRules[i]; |
3479 |
|
} |
3480 |
|
//TODO: MIDI rule copying |
3481 |
|
pMidiRules[0] = NULL; |
3482 |
|
|
3483 |
|
// delete all old regions |
3484 |
|
while (Regions) DeleteRegion(GetFirstRegion()); |
3485 |
|
// create new regions and copy them from original |
3486 |
|
{ |
3487 |
|
RegionList::const_iterator it = orig->pRegions->begin(); |
3488 |
|
for (int i = 0; i < orig->Regions; ++i, ++it) { |
3489 |
|
Region* dstRgn = AddRegion(); |
3490 |
|
//NOTE: Region does semi-deep copy ! |
3491 |
|
dstRgn->CopyAssign( |
3492 |
|
static_cast<gig::Region*>(*it) |
3493 |
|
); |
3494 |
|
} |
3495 |
|
} |
3496 |
|
|
3497 |
|
UpdateRegionKeyTable(); |
3498 |
|
} |
3499 |
|
|
3500 |
|
|
3501 |
|
// *************** Group *************** |
3502 |
// * |
// * |
3503 |
|
|
3504 |
File::File(RIFF::File* pRIFF) : DLS::File(pRIFF) { |
/** @brief Constructor. |
3505 |
pSamples = NULL; |
* |
3506 |
pInstruments = NULL; |
* @param file - pointer to the gig::File object |
3507 |
|
* @param ck3gnm - pointer to 3gnm chunk associated with this group or |
3508 |
|
* NULL if this is a new Group |
3509 |
|
*/ |
3510 |
|
Group::Group(File* file, RIFF::Chunk* ck3gnm) { |
3511 |
|
pFile = file; |
3512 |
|
pNameChunk = ck3gnm; |
3513 |
|
::LoadString(pNameChunk, Name); |
3514 |
} |
} |
3515 |
|
|
3516 |
File::~File() { |
Group::~Group() { |
3517 |
// free samples |
// remove the chunk associated with this group (if any) |
3518 |
if (pSamples) { |
if (pNameChunk) pNameChunk->GetParent()->DeleteSubChunk(pNameChunk); |
3519 |
SamplesIterator = pSamples->begin(); |
} |
3520 |
while (SamplesIterator != pSamples->end() ) { |
|
3521 |
delete (*SamplesIterator); |
/** @brief Update chunks with current group settings. |
3522 |
SamplesIterator++; |
* |
3523 |
|
* Apply current Group field values to the respective chunks. You have |
3524 |
|
* to call File::Save() to make changes persistent. |
3525 |
|
* |
3526 |
|
* Usually there is absolutely no need to call this method explicitly. |
3527 |
|
* It will be called automatically when File::Save() was called. |
3528 |
|
*/ |
3529 |
|
void Group::UpdateChunks() { |
3530 |
|
// make sure <3gri> and <3gnl> list chunks exist |
3531 |
|
RIFF::List* _3gri = pFile->pRIFF->GetSubList(LIST_TYPE_3GRI); |
3532 |
|
if (!_3gri) { |
3533 |
|
_3gri = pFile->pRIFF->AddSubList(LIST_TYPE_3GRI); |
3534 |
|
pFile->pRIFF->MoveSubChunk(_3gri, pFile->pRIFF->GetSubChunk(CHUNK_ID_PTBL)); |
3535 |
|
} |
3536 |
|
RIFF::List* _3gnl = _3gri->GetSubList(LIST_TYPE_3GNL); |
3537 |
|
if (!_3gnl) _3gnl = _3gri->AddSubList(LIST_TYPE_3GNL); |
3538 |
|
|
3539 |
|
if (!pNameChunk && pFile->pVersion && pFile->pVersion->major == 3) { |
3540 |
|
// v3 has a fixed list of 128 strings, find a free one |
3541 |
|
for (RIFF::Chunk* ck = _3gnl->GetFirstSubChunk() ; ck ; ck = _3gnl->GetNextSubChunk()) { |
3542 |
|
if (strcmp(static_cast<char*>(ck->LoadChunkData()), "") == 0) { |
3543 |
|
pNameChunk = ck; |
3544 |
|
break; |
3545 |
|
} |
3546 |
} |
} |
3547 |
pSamples->clear(); |
} |
|
delete pSamples; |
|
3548 |
|
|
3549 |
|
// now store the name of this group as <3gnm> chunk as subchunk of the <3gnl> list chunk |
3550 |
|
::SaveString(CHUNK_ID_3GNM, pNameChunk, _3gnl, Name, String("Unnamed Group"), true, 64); |
3551 |
|
} |
3552 |
|
|
3553 |
|
/** |
3554 |
|
* Returns the first Sample of this Group. You have to call this method |
3555 |
|
* once before you use GetNextSample(). |
3556 |
|
* |
3557 |
|
* <b>Notice:</b> this method might block for a long time, in case the |
3558 |
|
* samples of this .gig file were not scanned yet |
3559 |
|
* |
3560 |
|
* @returns pointer address to first Sample or NULL if there is none |
3561 |
|
* applied to this Group |
3562 |
|
* @see GetNextSample() |
3563 |
|
*/ |
3564 |
|
Sample* Group::GetFirstSample() { |
3565 |
|
// FIXME: lazy und unsafe implementation, should be an autonomous iterator |
3566 |
|
for (Sample* pSample = pFile->GetFirstSample(); pSample; pSample = pFile->GetNextSample()) { |
3567 |
|
if (pSample->GetGroup() == this) return pSample; |
3568 |
} |
} |
3569 |
// free instruments |
return NULL; |
3570 |
if (pInstruments) { |
} |
3571 |
InstrumentsIterator = pInstruments->begin(); |
|
3572 |
while (InstrumentsIterator != pInstruments->end() ) { |
/** |
3573 |
delete (*InstrumentsIterator); |
* Returns the next Sample of the Group. You have to call |
3574 |
InstrumentsIterator++; |
* GetFirstSample() once before you can use this method. By calling this |
3575 |
|
* method multiple times it iterates through the Samples assigned to |
3576 |
|
* this Group. |
3577 |
|
* |
3578 |
|
* @returns pointer address to the next Sample of this Group or NULL if |
3579 |
|
* end reached |
3580 |
|
* @see GetFirstSample() |
3581 |
|
*/ |
3582 |
|
Sample* Group::GetNextSample() { |
3583 |
|
// FIXME: lazy und unsafe implementation, should be an autonomous iterator |
3584 |
|
for (Sample* pSample = pFile->GetNextSample(); pSample; pSample = pFile->GetNextSample()) { |
3585 |
|
if (pSample->GetGroup() == this) return pSample; |
3586 |
|
} |
3587 |
|
return NULL; |
3588 |
|
} |
3589 |
|
|
3590 |
|
/** |
3591 |
|
* Move Sample given by \a pSample from another Group to this Group. |
3592 |
|
*/ |
3593 |
|
void Group::AddSample(Sample* pSample) { |
3594 |
|
pSample->pGroup = this; |
3595 |
|
} |
3596 |
|
|
3597 |
|
/** |
3598 |
|
* Move all members of this group to another group (preferably the 1st |
3599 |
|
* one except this). This method is called explicitly by |
3600 |
|
* File::DeleteGroup() thus when a Group was deleted. This code was |
3601 |
|
* intentionally not placed in the destructor! |
3602 |
|
*/ |
3603 |
|
void Group::MoveAll() { |
3604 |
|
// get "that" other group first |
3605 |
|
Group* pOtherGroup = NULL; |
3606 |
|
for (pOtherGroup = pFile->GetFirstGroup(); pOtherGroup; pOtherGroup = pFile->GetNextGroup()) { |
3607 |
|
if (pOtherGroup != this) break; |
3608 |
|
} |
3609 |
|
if (!pOtherGroup) throw Exception( |
3610 |
|
"Could not move samples to another group, since there is no " |
3611 |
|
"other Group. This is a bug, report it!" |
3612 |
|
); |
3613 |
|
// now move all samples of this group to the other group |
3614 |
|
for (Sample* pSample = GetFirstSample(); pSample; pSample = GetNextSample()) { |
3615 |
|
pOtherGroup->AddSample(pSample); |
3616 |
|
} |
3617 |
|
} |
3618 |
|
|
3619 |
|
|
3620 |
|
|
3621 |
|
// *************** File *************** |
3622 |
|
// * |
3623 |
|
|
3624 |
|
/// Reflects Gigasampler file format version 2.0 (1998-06-28). |
3625 |
|
const DLS::version_t File::VERSION_2 = { |
3626 |
|
0, 2, 19980628 & 0xffff, 19980628 >> 16 |
3627 |
|
}; |
3628 |
|
|
3629 |
|
/// Reflects Gigasampler file format version 3.0 (2003-03-31). |
3630 |
|
const DLS::version_t File::VERSION_3 = { |
3631 |
|
0, 3, 20030331 & 0xffff, 20030331 >> 16 |
3632 |
|
}; |
3633 |
|
|
3634 |
|
static const DLS::Info::string_length_t _FileFixedStringLengths[] = { |
3635 |
|
{ CHUNK_ID_IARL, 256 }, |
3636 |
|
{ CHUNK_ID_IART, 128 }, |
3637 |
|
{ CHUNK_ID_ICMS, 128 }, |
3638 |
|
{ CHUNK_ID_ICMT, 1024 }, |
3639 |
|
{ CHUNK_ID_ICOP, 128 }, |
3640 |
|
{ CHUNK_ID_ICRD, 128 }, |
3641 |
|
{ CHUNK_ID_IENG, 128 }, |
3642 |
|
{ CHUNK_ID_IGNR, 128 }, |
3643 |
|
{ CHUNK_ID_IKEY, 128 }, |
3644 |
|
{ CHUNK_ID_IMED, 128 }, |
3645 |
|
{ CHUNK_ID_INAM, 128 }, |
3646 |
|
{ CHUNK_ID_IPRD, 128 }, |
3647 |
|
{ CHUNK_ID_ISBJ, 128 }, |
3648 |
|
{ CHUNK_ID_ISFT, 128 }, |
3649 |
|
{ CHUNK_ID_ISRC, 128 }, |
3650 |
|
{ CHUNK_ID_ISRF, 128 }, |
3651 |
|
{ CHUNK_ID_ITCH, 128 }, |
3652 |
|
{ 0, 0 } |
3653 |
|
}; |
3654 |
|
|
3655 |
|
File::File() : DLS::File() { |
3656 |
|
bAutoLoad = true; |
3657 |
|
*pVersion = VERSION_3; |
3658 |
|
pGroups = NULL; |
3659 |
|
pInfo->SetFixedStringLengths(_FileFixedStringLengths); |
3660 |
|
pInfo->ArchivalLocation = String(256, ' '); |
3661 |
|
|
3662 |
|
// add some mandatory chunks to get the file chunks in right |
3663 |
|
// order (INFO chunk will be moved to first position later) |
3664 |
|
pRIFF->AddSubChunk(CHUNK_ID_VERS, 8); |
3665 |
|
pRIFF->AddSubChunk(CHUNK_ID_COLH, 4); |
3666 |
|
pRIFF->AddSubChunk(CHUNK_ID_DLID, 16); |
3667 |
|
|
3668 |
|
GenerateDLSID(); |
3669 |
|
} |
3670 |
|
|
3671 |
|
File::File(RIFF::File* pRIFF) : DLS::File(pRIFF) { |
3672 |
|
bAutoLoad = true; |
3673 |
|
pGroups = NULL; |
3674 |
|
pInfo->SetFixedStringLengths(_FileFixedStringLengths); |
3675 |
|
} |
3676 |
|
|
3677 |
|
File::~File() { |
3678 |
|
if (pGroups) { |
3679 |
|
std::list<Group*>::iterator iter = pGroups->begin(); |
3680 |
|
std::list<Group*>::iterator end = pGroups->end(); |
3681 |
|
while (iter != end) { |
3682 |
|
delete *iter; |
3683 |
|
++iter; |
3684 |
} |
} |
3685 |
pInstruments->clear(); |
delete pGroups; |
|
delete pInstruments; |
|
3686 |
} |
} |
3687 |
} |
} |
3688 |
|
|
3689 |
Sample* File::GetFirstSample() { |
Sample* File::GetFirstSample(progress_t* pProgress) { |
3690 |
if (!pSamples) LoadSamples(); |
if (!pSamples) LoadSamples(pProgress); |
3691 |
if (!pSamples) return NULL; |
if (!pSamples) return NULL; |
3692 |
SamplesIterator = pSamples->begin(); |
SamplesIterator = pSamples->begin(); |
3693 |
return static_cast<gig::Sample*>( (SamplesIterator != pSamples->end()) ? *SamplesIterator : NULL ); |
return static_cast<gig::Sample*>( (SamplesIterator != pSamples->end()) ? *SamplesIterator : NULL ); |
3699 |
return static_cast<gig::Sample*>( (SamplesIterator != pSamples->end()) ? *SamplesIterator : NULL ); |
return static_cast<gig::Sample*>( (SamplesIterator != pSamples->end()) ? *SamplesIterator : NULL ); |
3700 |
} |
} |
3701 |
|
|
3702 |
void File::LoadSamples() { |
/** @brief Add a new sample. |
3703 |
RIFF::List* wvpl = pRIFF->GetSubList(LIST_TYPE_WVPL); |
* |
3704 |
if (wvpl) { |
* This will create a new Sample object for the gig file. You have to |
3705 |
unsigned long wvplFileOffset = wvpl->GetFilePos(); |
* call Save() to make this persistent to the file. |
3706 |
RIFF::List* wave = wvpl->GetFirstSubList(); |
* |
3707 |
while (wave) { |
* @returns pointer to new Sample object |
3708 |
if (wave->GetListType() == LIST_TYPE_WAVE) { |
*/ |
3709 |
if (!pSamples) pSamples = new SampleList; |
Sample* File::AddSample() { |
3710 |
unsigned long waveFileOffset = wave->GetFilePos(); |
if (!pSamples) LoadSamples(); |
3711 |
pSamples->push_back(new Sample(this, wave, waveFileOffset - wvplFileOffset)); |
__ensureMandatoryChunksExist(); |
3712 |
|
RIFF::List* wvpl = pRIFF->GetSubList(LIST_TYPE_WVPL); |
3713 |
|
// create new Sample object and its respective 'wave' list chunk |
3714 |
|
RIFF::List* wave = wvpl->AddSubList(LIST_TYPE_WAVE); |
3715 |
|
Sample* pSample = new Sample(this, wave, 0 /*arbitrary value, we update offsets when we save*/); |
3716 |
|
|
3717 |
|
// add mandatory chunks to get the chunks in right order |
3718 |
|
wave->AddSubChunk(CHUNK_ID_FMT, 16); |
3719 |
|
wave->AddSubList(LIST_TYPE_INFO); |
3720 |
|
|
3721 |
|
pSamples->push_back(pSample); |
3722 |
|
return pSample; |
3723 |
|
} |
3724 |
|
|
3725 |
|
/** @brief Delete a sample. |
3726 |
|
* |
3727 |
|
* This will delete the given Sample object from the gig file. Any |
3728 |
|
* references to this sample from Regions and DimensionRegions will be |
3729 |
|
* removed. You have to call Save() to make this persistent to the file. |
3730 |
|
* |
3731 |
|
* @param pSample - sample to delete |
3732 |
|
* @throws gig::Exception if given sample could not be found |
3733 |
|
*/ |
3734 |
|
void File::DeleteSample(Sample* pSample) { |
3735 |
|
if (!pSamples || !pSamples->size()) throw gig::Exception("Could not delete sample as there are no samples"); |
3736 |
|
SampleList::iterator iter = find(pSamples->begin(), pSamples->end(), (DLS::Sample*) pSample); |
3737 |
|
if (iter == pSamples->end()) throw gig::Exception("Could not delete sample, could not find given sample"); |
3738 |
|
if (SamplesIterator != pSamples->end() && *SamplesIterator == pSample) ++SamplesIterator; // avoid iterator invalidation |
3739 |
|
pSamples->erase(iter); |
3740 |
|
delete pSample; |
3741 |
|
|
3742 |
|
SampleList::iterator tmp = SamplesIterator; |
3743 |
|
// remove all references to the sample |
3744 |
|
for (Instrument* instrument = GetFirstInstrument() ; instrument ; |
3745 |
|
instrument = GetNextInstrument()) { |
3746 |
|
for (Region* region = instrument->GetFirstRegion() ; region ; |
3747 |
|
region = instrument->GetNextRegion()) { |
3748 |
|
|
3749 |
|
if (region->GetSample() == pSample) region->SetSample(NULL); |
3750 |
|
|
3751 |
|
for (int i = 0 ; i < region->DimensionRegions ; i++) { |
3752 |
|
gig::DimensionRegion *d = region->pDimensionRegions[i]; |
3753 |
|
if (d->pSample == pSample) d->pSample = NULL; |
3754 |
} |
} |
|
wave = wvpl->GetNextSubList(); |
|
3755 |
} |
} |
3756 |
} |
} |
3757 |
else throw gig::Exception("Mandatory <wvpl> chunk not found."); |
SamplesIterator = tmp; // restore iterator |
3758 |
|
} |
3759 |
|
|
3760 |
|
void File::LoadSamples() { |
3761 |
|
LoadSamples(NULL); |
3762 |
|
} |
3763 |
|
|
3764 |
|
void File::LoadSamples(progress_t* pProgress) { |
3765 |
|
// Groups must be loaded before samples, because samples will try |
3766 |
|
// to resolve the group they belong to |
3767 |
|
if (!pGroups) LoadGroups(); |
3768 |
|
|
3769 |
|
if (!pSamples) pSamples = new SampleList; |
3770 |
|
|
3771 |
|
RIFF::File* file = pRIFF; |
3772 |
|
|
3773 |
|
// just for progress calculation |
3774 |
|
int iSampleIndex = 0; |
3775 |
|
int iTotalSamples = WavePoolCount; |
3776 |
|
|
3777 |
|
// check if samples should be loaded from extension files |
3778 |
|
int lastFileNo = 0; |
3779 |
|
for (int i = 0 ; i < WavePoolCount ; i++) { |
3780 |
|
if (pWavePoolTableHi[i] > lastFileNo) lastFileNo = pWavePoolTableHi[i]; |
3781 |
|
} |
3782 |
|
String name(pRIFF->GetFileName()); |
3783 |
|
int nameLen = name.length(); |
3784 |
|
char suffix[6]; |
3785 |
|
if (nameLen > 4 && name.substr(nameLen - 4) == ".gig") nameLen -= 4; |
3786 |
|
|
3787 |
|
for (int fileNo = 0 ; ; ) { |
3788 |
|
RIFF::List* wvpl = file->GetSubList(LIST_TYPE_WVPL); |
3789 |
|
if (wvpl) { |
3790 |
|
unsigned long wvplFileOffset = wvpl->GetFilePos(); |
3791 |
|
RIFF::List* wave = wvpl->GetFirstSubList(); |
3792 |
|
while (wave) { |
3793 |
|
if (wave->GetListType() == LIST_TYPE_WAVE) { |
3794 |
|
// notify current progress |
3795 |
|
const float subprogress = (float) iSampleIndex / (float) iTotalSamples; |
3796 |
|
__notify_progress(pProgress, subprogress); |
3797 |
|
|
3798 |
|
unsigned long waveFileOffset = wave->GetFilePos(); |
3799 |
|
pSamples->push_back(new Sample(this, wave, waveFileOffset - wvplFileOffset, fileNo)); |
3800 |
|
|
3801 |
|
iSampleIndex++; |
3802 |
|
} |
3803 |
|
wave = wvpl->GetNextSubList(); |
3804 |
|
} |
3805 |
|
|
3806 |
|
if (fileNo == lastFileNo) break; |
3807 |
|
|
3808 |
|
// open extension file (*.gx01, *.gx02, ...) |
3809 |
|
fileNo++; |
3810 |
|
sprintf(suffix, ".gx%02d", fileNo); |
3811 |
|
name.replace(nameLen, 5, suffix); |
3812 |
|
file = new RIFF::File(name); |
3813 |
|
ExtensionFiles.push_back(file); |
3814 |
|
} else break; |
3815 |
|
} |
3816 |
|
|
3817 |
|
__notify_progress(pProgress, 1.0); // notify done |
3818 |
} |
} |
3819 |
|
|
3820 |
Instrument* File::GetFirstInstrument() { |
Instrument* File::GetFirstInstrument() { |
3821 |
if (!pInstruments) LoadInstruments(); |
if (!pInstruments) LoadInstruments(); |
3822 |
if (!pInstruments) return NULL; |
if (!pInstruments) return NULL; |
3823 |
InstrumentsIterator = pInstruments->begin(); |
InstrumentsIterator = pInstruments->begin(); |
3824 |
return (InstrumentsIterator != pInstruments->end()) ? *InstrumentsIterator : NULL; |
return static_cast<gig::Instrument*>( (InstrumentsIterator != pInstruments->end()) ? *InstrumentsIterator : NULL ); |
3825 |
} |
} |
3826 |
|
|
3827 |
Instrument* File::GetNextInstrument() { |
Instrument* File::GetNextInstrument() { |
3828 |
if (!pInstruments) return NULL; |
if (!pInstruments) return NULL; |
3829 |
InstrumentsIterator++; |
InstrumentsIterator++; |
3830 |
return (InstrumentsIterator != pInstruments->end()) ? *InstrumentsIterator : NULL; |
return static_cast<gig::Instrument*>( (InstrumentsIterator != pInstruments->end()) ? *InstrumentsIterator : NULL ); |
3831 |
} |
} |
3832 |
|
|
3833 |
/** |
/** |
3834 |
* Returns the instrument with the given index. |
* Returns the instrument with the given index. |
3835 |
* |
* |
3836 |
|
* @param index - number of the sought instrument (0..n) |
3837 |
|
* @param pProgress - optional: callback function for progress notification |
3838 |
* @returns sought instrument or NULL if there's no such instrument |
* @returns sought instrument or NULL if there's no such instrument |
3839 |
*/ |
*/ |
3840 |
Instrument* File::GetInstrument(uint index) { |
Instrument* File::GetInstrument(uint index, progress_t* pProgress) { |
3841 |
if (!pInstruments) LoadInstruments(); |
if (!pInstruments) { |
3842 |
|
// TODO: hack - we simply load ALL samples here, it would have been done in the Region constructor anyway (ATM) |
3843 |
|
|
3844 |
|
// sample loading subtask |
3845 |
|
progress_t subprogress; |
3846 |
|
__divide_progress(pProgress, &subprogress, 3.0f, 0.0f); // randomly schedule 33% for this subtask |
3847 |
|
__notify_progress(&subprogress, 0.0f); |
3848 |
|
if (GetAutoLoad()) |
3849 |
|
GetFirstSample(&subprogress); // now force all samples to be loaded |
3850 |
|
__notify_progress(&subprogress, 1.0f); |
3851 |
|
|
3852 |
|
// instrument loading subtask |
3853 |
|
if (pProgress && pProgress->callback) { |
3854 |
|
subprogress.__range_min = subprogress.__range_max; |
3855 |
|
subprogress.__range_max = pProgress->__range_max; // schedule remaining percentage for this subtask |
3856 |
|
} |
3857 |
|
__notify_progress(&subprogress, 0.0f); |
3858 |
|
LoadInstruments(&subprogress); |
3859 |
|
__notify_progress(&subprogress, 1.0f); |
3860 |
|
} |
3861 |
if (!pInstruments) return NULL; |
if (!pInstruments) return NULL; |
3862 |
InstrumentsIterator = pInstruments->begin(); |
InstrumentsIterator = pInstruments->begin(); |
3863 |
for (uint i = 0; InstrumentsIterator != pInstruments->end(); i++) { |
for (uint i = 0; InstrumentsIterator != pInstruments->end(); i++) { |
3864 |
if (i == index) return *InstrumentsIterator; |
if (i == index) return static_cast<gig::Instrument*>( *InstrumentsIterator ); |
3865 |
InstrumentsIterator++; |
InstrumentsIterator++; |
3866 |
} |
} |
3867 |
return NULL; |
return NULL; |
3868 |
} |
} |
3869 |
|
|
3870 |
|
/** @brief Add a new instrument definition. |
3871 |
|
* |
3872 |
|
* This will create a new Instrument object for the gig file. You have |
3873 |
|
* to call Save() to make this persistent to the file. |
3874 |
|
* |
3875 |
|
* @returns pointer to new Instrument object |
3876 |
|
*/ |
3877 |
|
Instrument* File::AddInstrument() { |
3878 |
|
if (!pInstruments) LoadInstruments(); |
3879 |
|
__ensureMandatoryChunksExist(); |
3880 |
|
RIFF::List* lstInstruments = pRIFF->GetSubList(LIST_TYPE_LINS); |
3881 |
|
RIFF::List* lstInstr = lstInstruments->AddSubList(LIST_TYPE_INS); |
3882 |
|
|
3883 |
|
// add mandatory chunks to get the chunks in right order |
3884 |
|
lstInstr->AddSubList(LIST_TYPE_INFO); |
3885 |
|
lstInstr->AddSubChunk(CHUNK_ID_DLID, 16); |
3886 |
|
|
3887 |
|
Instrument* pInstrument = new Instrument(this, lstInstr); |
3888 |
|
pInstrument->GenerateDLSID(); |
3889 |
|
|
3890 |
|
lstInstr->AddSubChunk(CHUNK_ID_INSH, 12); |
3891 |
|
|
3892 |
|
// this string is needed for the gig to be loadable in GSt: |
3893 |
|
pInstrument->pInfo->Software = "Endless Wave"; |
3894 |
|
|
3895 |
|
pInstruments->push_back(pInstrument); |
3896 |
|
return pInstrument; |
3897 |
|
} |
3898 |
|
|
3899 |
|
/** @brief Add a duplicate of an existing instrument. |
3900 |
|
* |
3901 |
|
* Duplicates the instrument definition given by @a orig and adds it |
3902 |
|
* to this file. This allows in an instrument editor application to |
3903 |
|
* easily create variations of an instrument, which will be stored in |
3904 |
|
* the same .gig file, sharing i.e. the same samples. |
3905 |
|
* |
3906 |
|
* Note that all sample pointers referenced by @a orig are simply copied as |
3907 |
|
* memory address. Thus the respective samples are shared, not duplicated! |
3908 |
|
* |
3909 |
|
* You have to call Save() to make this persistent to the file. |
3910 |
|
* |
3911 |
|
* @param orig - original instrument to be copied |
3912 |
|
* @returns duplicated copy of the given instrument |
3913 |
|
*/ |
3914 |
|
Instrument* File::AddDuplicateInstrument(const Instrument* orig) { |
3915 |
|
Instrument* instr = AddInstrument(); |
3916 |
|
instr->CopyAssign(orig); |
3917 |
|
return instr; |
3918 |
|
} |
3919 |
|
|
3920 |
|
/** @brief Delete an instrument. |
3921 |
|
* |
3922 |
|
* This will delete the given Instrument object from the gig file. You |
3923 |
|
* have to call Save() to make this persistent to the file. |
3924 |
|
* |
3925 |
|
* @param pInstrument - instrument to delete |
3926 |
|
* @throws gig::Exception if given instrument could not be found |
3927 |
|
*/ |
3928 |
|
void File::DeleteInstrument(Instrument* pInstrument) { |
3929 |
|
if (!pInstruments) throw gig::Exception("Could not delete instrument as there are no instruments"); |
3930 |
|
InstrumentList::iterator iter = find(pInstruments->begin(), pInstruments->end(), (DLS::Instrument*) pInstrument); |
3931 |
|
if (iter == pInstruments->end()) throw gig::Exception("Could not delete instrument, could not find given instrument"); |
3932 |
|
pInstruments->erase(iter); |
3933 |
|
delete pInstrument; |
3934 |
|
} |
3935 |
|
|
3936 |
void File::LoadInstruments() { |
void File::LoadInstruments() { |
3937 |
|
LoadInstruments(NULL); |
3938 |
|
} |
3939 |
|
|
3940 |
|
void File::LoadInstruments(progress_t* pProgress) { |
3941 |
|
if (!pInstruments) pInstruments = new InstrumentList; |
3942 |
RIFF::List* lstInstruments = pRIFF->GetSubList(LIST_TYPE_LINS); |
RIFF::List* lstInstruments = pRIFF->GetSubList(LIST_TYPE_LINS); |
3943 |
if (lstInstruments) { |
if (lstInstruments) { |
3944 |
|
int iInstrumentIndex = 0; |
3945 |
RIFF::List* lstInstr = lstInstruments->GetFirstSubList(); |
RIFF::List* lstInstr = lstInstruments->GetFirstSubList(); |
3946 |
while (lstInstr) { |
while (lstInstr) { |
3947 |
if (lstInstr->GetListType() == LIST_TYPE_INS) { |
if (lstInstr->GetListType() == LIST_TYPE_INS) { |
3948 |
if (!pInstruments) pInstruments = new InstrumentList; |
// notify current progress |
3949 |
pInstruments->push_back(new Instrument(this, lstInstr)); |
const float localProgress = (float) iInstrumentIndex / (float) Instruments; |
3950 |
|
__notify_progress(pProgress, localProgress); |
3951 |
|
|
3952 |
|
// divide local progress into subprogress for loading current Instrument |
3953 |
|
progress_t subprogress; |
3954 |
|
__divide_progress(pProgress, &subprogress, Instruments, iInstrumentIndex); |
3955 |
|
|
3956 |
|
pInstruments->push_back(new Instrument(this, lstInstr, &subprogress)); |
3957 |
|
|
3958 |
|
iInstrumentIndex++; |
3959 |
} |
} |
3960 |
lstInstr = lstInstruments->GetNextSubList(); |
lstInstr = lstInstruments->GetNextSubList(); |
3961 |
} |
} |
3962 |
|
__notify_progress(pProgress, 1.0); // notify done |
3963 |
|
} |
3964 |
|
} |
3965 |
|
|
3966 |
|
/// Updates the 3crc chunk with the checksum of a sample. The |
3967 |
|
/// update is done directly to disk, as this method is called |
3968 |
|
/// after File::Save() |
3969 |
|
void File::SetSampleChecksum(Sample* pSample, uint32_t crc) { |
3970 |
|
RIFF::Chunk* _3crc = pRIFF->GetSubChunk(CHUNK_ID_3CRC); |
3971 |
|
if (!_3crc) return; |
3972 |
|
|
3973 |
|
// get the index of the sample |
3974 |
|
int iWaveIndex = -1; |
3975 |
|
File::SampleList::iterator iter = pSamples->begin(); |
3976 |
|
File::SampleList::iterator end = pSamples->end(); |
3977 |
|
for (int index = 0; iter != end; ++iter, ++index) { |
3978 |
|
if (*iter == pSample) { |
3979 |
|
iWaveIndex = index; |
3980 |
|
break; |
3981 |
|
} |
3982 |
|
} |
3983 |
|
if (iWaveIndex < 0) throw gig::Exception("Could not update crc, could not find sample"); |
3984 |
|
|
3985 |
|
// write the CRC-32 checksum to disk |
3986 |
|
_3crc->SetPos(iWaveIndex * 8); |
3987 |
|
uint32_t tmp = 1; |
3988 |
|
_3crc->WriteUint32(&tmp); // unknown, always 1? |
3989 |
|
_3crc->WriteUint32(&crc); |
3990 |
|
} |
3991 |
|
|
3992 |
|
Group* File::GetFirstGroup() { |
3993 |
|
if (!pGroups) LoadGroups(); |
3994 |
|
// there must always be at least one group |
3995 |
|
GroupsIterator = pGroups->begin(); |
3996 |
|
return *GroupsIterator; |
3997 |
|
} |
3998 |
|
|
3999 |
|
Group* File::GetNextGroup() { |
4000 |
|
if (!pGroups) return NULL; |
4001 |
|
++GroupsIterator; |
4002 |
|
return (GroupsIterator == pGroups->end()) ? NULL : *GroupsIterator; |
4003 |
|
} |
4004 |
|
|
4005 |
|
/** |
4006 |
|
* Returns the group with the given index. |
4007 |
|
* |
4008 |
|
* @param index - number of the sought group (0..n) |
4009 |
|
* @returns sought group or NULL if there's no such group |
4010 |
|
*/ |
4011 |
|
Group* File::GetGroup(uint index) { |
4012 |
|
if (!pGroups) LoadGroups(); |
4013 |
|
GroupsIterator = pGroups->begin(); |
4014 |
|
for (uint i = 0; GroupsIterator != pGroups->end(); i++) { |
4015 |
|
if (i == index) return *GroupsIterator; |
4016 |
|
++GroupsIterator; |
4017 |
|
} |
4018 |
|
return NULL; |
4019 |
|
} |
4020 |
|
|
4021 |
|
Group* File::AddGroup() { |
4022 |
|
if (!pGroups) LoadGroups(); |
4023 |
|
// there must always be at least one group |
4024 |
|
__ensureMandatoryChunksExist(); |
4025 |
|
Group* pGroup = new Group(this, NULL); |
4026 |
|
pGroups->push_back(pGroup); |
4027 |
|
return pGroup; |
4028 |
|
} |
4029 |
|
|
4030 |
|
/** @brief Delete a group and its samples. |
4031 |
|
* |
4032 |
|
* This will delete the given Group object and all the samples that |
4033 |
|
* belong to this group from the gig file. You have to call Save() to |
4034 |
|
* make this persistent to the file. |
4035 |
|
* |
4036 |
|
* @param pGroup - group to delete |
4037 |
|
* @throws gig::Exception if given group could not be found |
4038 |
|
*/ |
4039 |
|
void File::DeleteGroup(Group* pGroup) { |
4040 |
|
if (!pGroups) LoadGroups(); |
4041 |
|
std::list<Group*>::iterator iter = find(pGroups->begin(), pGroups->end(), pGroup); |
4042 |
|
if (iter == pGroups->end()) throw gig::Exception("Could not delete group, could not find given group"); |
4043 |
|
if (pGroups->size() == 1) throw gig::Exception("Cannot delete group, there must be at least one default group!"); |
4044 |
|
// delete all members of this group |
4045 |
|
for (Sample* pSample = pGroup->GetFirstSample(); pSample; pSample = pGroup->GetNextSample()) { |
4046 |
|
DeleteSample(pSample); |
4047 |
|
} |
4048 |
|
// now delete this group object |
4049 |
|
pGroups->erase(iter); |
4050 |
|
delete pGroup; |
4051 |
|
} |
4052 |
|
|
4053 |
|
/** @brief Delete a group. |
4054 |
|
* |
4055 |
|
* This will delete the given Group object from the gig file. All the |
4056 |
|
* samples that belong to this group will not be deleted, but instead |
4057 |
|
* be moved to another group. You have to call Save() to make this |
4058 |
|
* persistent to the file. |
4059 |
|
* |
4060 |
|
* @param pGroup - group to delete |
4061 |
|
* @throws gig::Exception if given group could not be found |
4062 |
|
*/ |
4063 |
|
void File::DeleteGroupOnly(Group* pGroup) { |
4064 |
|
if (!pGroups) LoadGroups(); |
4065 |
|
std::list<Group*>::iterator iter = find(pGroups->begin(), pGroups->end(), pGroup); |
4066 |
|
if (iter == pGroups->end()) throw gig::Exception("Could not delete group, could not find given group"); |
4067 |
|
if (pGroups->size() == 1) throw gig::Exception("Cannot delete group, there must be at least one default group!"); |
4068 |
|
// move all members of this group to another group |
4069 |
|
pGroup->MoveAll(); |
4070 |
|
pGroups->erase(iter); |
4071 |
|
delete pGroup; |
4072 |
|
} |
4073 |
|
|
4074 |
|
void File::LoadGroups() { |
4075 |
|
if (!pGroups) pGroups = new std::list<Group*>; |
4076 |
|
// try to read defined groups from file |
4077 |
|
RIFF::List* lst3gri = pRIFF->GetSubList(LIST_TYPE_3GRI); |
4078 |
|
if (lst3gri) { |
4079 |
|
RIFF::List* lst3gnl = lst3gri->GetSubList(LIST_TYPE_3GNL); |
4080 |
|
if (lst3gnl) { |
4081 |
|
RIFF::Chunk* ck = lst3gnl->GetFirstSubChunk(); |
4082 |
|
while (ck) { |
4083 |
|
if (ck->GetChunkID() == CHUNK_ID_3GNM) { |
4084 |
|
if (pVersion && pVersion->major == 3 && |
4085 |
|
strcmp(static_cast<char*>(ck->LoadChunkData()), "") == 0) break; |
4086 |
|
|
4087 |
|
pGroups->push_back(new Group(this, ck)); |
4088 |
|
} |
4089 |
|
ck = lst3gnl->GetNextSubChunk(); |
4090 |
|
} |
4091 |
|
} |
4092 |
|
} |
4093 |
|
// if there were no group(s), create at least the mandatory default group |
4094 |
|
if (!pGroups->size()) { |
4095 |
|
Group* pGroup = new Group(this, NULL); |
4096 |
|
pGroup->Name = "Default Group"; |
4097 |
|
pGroups->push_back(pGroup); |
4098 |
|
} |
4099 |
|
} |
4100 |
|
|
4101 |
|
/** |
4102 |
|
* Apply all the gig file's current instruments, samples, groups and settings |
4103 |
|
* to the respective RIFF chunks. You have to call Save() to make changes |
4104 |
|
* persistent. |
4105 |
|
* |
4106 |
|
* Usually there is absolutely no need to call this method explicitly. |
4107 |
|
* It will be called automatically when File::Save() was called. |
4108 |
|
* |
4109 |
|
* @throws Exception - on errors |
4110 |
|
*/ |
4111 |
|
void File::UpdateChunks() { |
4112 |
|
bool newFile = pRIFF->GetSubList(LIST_TYPE_INFO) == NULL; |
4113 |
|
|
4114 |
|
b64BitWavePoolOffsets = pVersion && pVersion->major == 3; |
4115 |
|
|
4116 |
|
// first update base class's chunks |
4117 |
|
DLS::File::UpdateChunks(); |
4118 |
|
|
4119 |
|
if (newFile) { |
4120 |
|
// INFO was added by Resource::UpdateChunks - make sure it |
4121 |
|
// is placed first in file |
4122 |
|
RIFF::Chunk* info = pRIFF->GetSubList(LIST_TYPE_INFO); |
4123 |
|
RIFF::Chunk* first = pRIFF->GetFirstSubChunk(); |
4124 |
|
if (first != info) { |
4125 |
|
pRIFF->MoveSubChunk(info, first); |
4126 |
|
} |
4127 |
|
} |
4128 |
|
|
4129 |
|
// update group's chunks |
4130 |
|
if (pGroups) { |
4131 |
|
// make sure '3gri' and '3gnl' list chunks exist |
4132 |
|
// (before updating the Group chunks) |
4133 |
|
RIFF::List* _3gri = pRIFF->GetSubList(LIST_TYPE_3GRI); |
4134 |
|
if (!_3gri) { |
4135 |
|
_3gri = pRIFF->AddSubList(LIST_TYPE_3GRI); |
4136 |
|
pRIFF->MoveSubChunk(_3gri, pRIFF->GetSubChunk(CHUNK_ID_PTBL)); |
4137 |
|
} |
4138 |
|
RIFF::List* _3gnl = _3gri->GetSubList(LIST_TYPE_3GNL); |
4139 |
|
if (!_3gnl) _3gnl = _3gri->AddSubList(LIST_TYPE_3GNL); |
4140 |
|
|
4141 |
|
// v3: make sure the file has 128 3gnm chunks |
4142 |
|
// (before updating the Group chunks) |
4143 |
|
if (pVersion && pVersion->major == 3) { |
4144 |
|
RIFF::Chunk* _3gnm = _3gnl->GetFirstSubChunk(); |
4145 |
|
for (int i = 0 ; i < 128 ; i++) { |
4146 |
|
if (i >= pGroups->size()) ::SaveString(CHUNK_ID_3GNM, _3gnm, _3gnl, "", "", true, 64); |
4147 |
|
if (_3gnm) _3gnm = _3gnl->GetNextSubChunk(); |
4148 |
|
} |
4149 |
|
} |
4150 |
|
|
4151 |
|
std::list<Group*>::iterator iter = pGroups->begin(); |
4152 |
|
std::list<Group*>::iterator end = pGroups->end(); |
4153 |
|
for (; iter != end; ++iter) { |
4154 |
|
(*iter)->UpdateChunks(); |
4155 |
|
} |
4156 |
|
} |
4157 |
|
|
4158 |
|
// update einf chunk |
4159 |
|
|
4160 |
|
// The einf chunk contains statistics about the gig file, such |
4161 |
|
// as the number of regions and samples used by each |
4162 |
|
// instrument. It is divided in equally sized parts, where the |
4163 |
|
// first part contains information about the whole gig file, |
4164 |
|
// and the rest of the parts map to each instrument in the |
4165 |
|
// file. |
4166 |
|
// |
4167 |
|
// At the end of each part there is a bit map of each sample |
4168 |
|
// in the file, where a set bit means that the sample is used |
4169 |
|
// by the file/instrument. |
4170 |
|
// |
4171 |
|
// Note that there are several fields with unknown use. These |
4172 |
|
// are set to zero. |
4173 |
|
|
4174 |
|
int sublen = pSamples->size() / 8 + 49; |
4175 |
|
int einfSize = (Instruments + 1) * sublen; |
4176 |
|
|
4177 |
|
RIFF::Chunk* einf = pRIFF->GetSubChunk(CHUNK_ID_EINF); |
4178 |
|
if (einf) { |
4179 |
|
if (einf->GetSize() != einfSize) { |
4180 |
|
einf->Resize(einfSize); |
4181 |
|
memset(einf->LoadChunkData(), 0, einfSize); |
4182 |
|
} |
4183 |
|
} else if (newFile) { |
4184 |
|
einf = pRIFF->AddSubChunk(CHUNK_ID_EINF, einfSize); |
4185 |
|
} |
4186 |
|
if (einf) { |
4187 |
|
uint8_t* pData = (uint8_t*) einf->LoadChunkData(); |
4188 |
|
|
4189 |
|
std::map<gig::Sample*,int> sampleMap; |
4190 |
|
int sampleIdx = 0; |
4191 |
|
for (Sample* pSample = GetFirstSample(); pSample; pSample = GetNextSample()) { |
4192 |
|
sampleMap[pSample] = sampleIdx++; |
4193 |
|
} |
4194 |
|
|
4195 |
|
int totnbusedsamples = 0; |
4196 |
|
int totnbusedchannels = 0; |
4197 |
|
int totnbregions = 0; |
4198 |
|
int totnbdimregions = 0; |
4199 |
|
int totnbloops = 0; |
4200 |
|
int instrumentIdx = 0; |
4201 |
|
|
4202 |
|
memset(&pData[48], 0, sublen - 48); |
4203 |
|
|
4204 |
|
for (Instrument* instrument = GetFirstInstrument() ; instrument ; |
4205 |
|
instrument = GetNextInstrument()) { |
4206 |
|
int nbusedsamples = 0; |
4207 |
|
int nbusedchannels = 0; |
4208 |
|
int nbdimregions = 0; |
4209 |
|
int nbloops = 0; |
4210 |
|
|
4211 |
|
memset(&pData[(instrumentIdx + 1) * sublen + 48], 0, sublen - 48); |
4212 |
|
|
4213 |
|
for (Region* region = instrument->GetFirstRegion() ; region ; |
4214 |
|
region = instrument->GetNextRegion()) { |
4215 |
|
for (int i = 0 ; i < region->DimensionRegions ; i++) { |
4216 |
|
gig::DimensionRegion *d = region->pDimensionRegions[i]; |
4217 |
|
if (d->pSample) { |
4218 |
|
int sampleIdx = sampleMap[d->pSample]; |
4219 |
|
int byte = 48 + sampleIdx / 8; |
4220 |
|
int bit = 1 << (sampleIdx & 7); |
4221 |
|
if ((pData[(instrumentIdx + 1) * sublen + byte] & bit) == 0) { |
4222 |
|
pData[(instrumentIdx + 1) * sublen + byte] |= bit; |
4223 |
|
nbusedsamples++; |
4224 |
|
nbusedchannels += d->pSample->Channels; |
4225 |
|
|
4226 |
|
if ((pData[byte] & bit) == 0) { |
4227 |
|
pData[byte] |= bit; |
4228 |
|
totnbusedsamples++; |
4229 |
|
totnbusedchannels += d->pSample->Channels; |
4230 |
|
} |
4231 |
|
} |
4232 |
|
} |
4233 |
|
if (d->SampleLoops) nbloops++; |
4234 |
|
} |
4235 |
|
nbdimregions += region->DimensionRegions; |
4236 |
|
} |
4237 |
|
// first 4 bytes unknown - sometimes 0, sometimes length of einf part |
4238 |
|
// store32(&pData[(instrumentIdx + 1) * sublen], sublen); |
4239 |
|
store32(&pData[(instrumentIdx + 1) * sublen + 4], nbusedchannels); |
4240 |
|
store32(&pData[(instrumentIdx + 1) * sublen + 8], nbusedsamples); |
4241 |
|
store32(&pData[(instrumentIdx + 1) * sublen + 12], 1); |
4242 |
|
store32(&pData[(instrumentIdx + 1) * sublen + 16], instrument->Regions); |
4243 |
|
store32(&pData[(instrumentIdx + 1) * sublen + 20], nbdimregions); |
4244 |
|
store32(&pData[(instrumentIdx + 1) * sublen + 24], nbloops); |
4245 |
|
// next 8 bytes unknown |
4246 |
|
store32(&pData[(instrumentIdx + 1) * sublen + 36], instrumentIdx); |
4247 |
|
store32(&pData[(instrumentIdx + 1) * sublen + 40], pSamples->size()); |
4248 |
|
// next 4 bytes unknown |
4249 |
|
|
4250 |
|
totnbregions += instrument->Regions; |
4251 |
|
totnbdimregions += nbdimregions; |
4252 |
|
totnbloops += nbloops; |
4253 |
|
instrumentIdx++; |
4254 |
|
} |
4255 |
|
// first 4 bytes unknown - sometimes 0, sometimes length of einf part |
4256 |
|
// store32(&pData[0], sublen); |
4257 |
|
store32(&pData[4], totnbusedchannels); |
4258 |
|
store32(&pData[8], totnbusedsamples); |
4259 |
|
store32(&pData[12], Instruments); |
4260 |
|
store32(&pData[16], totnbregions); |
4261 |
|
store32(&pData[20], totnbdimregions); |
4262 |
|
store32(&pData[24], totnbloops); |
4263 |
|
// next 8 bytes unknown |
4264 |
|
// next 4 bytes unknown, not always 0 |
4265 |
|
store32(&pData[40], pSamples->size()); |
4266 |
|
// next 4 bytes unknown |
4267 |
|
} |
4268 |
|
|
4269 |
|
// update 3crc chunk |
4270 |
|
|
4271 |
|
// The 3crc chunk contains CRC-32 checksums for the |
4272 |
|
// samples. The actual checksum values will be filled in |
4273 |
|
// later, by Sample::Write. |
4274 |
|
|
4275 |
|
RIFF::Chunk* _3crc = pRIFF->GetSubChunk(CHUNK_ID_3CRC); |
4276 |
|
if (_3crc) { |
4277 |
|
_3crc->Resize(pSamples->size() * 8); |
4278 |
|
} else if (newFile) { |
4279 |
|
_3crc = pRIFF->AddSubChunk(CHUNK_ID_3CRC, pSamples->size() * 8); |
4280 |
|
_3crc->LoadChunkData(); |
4281 |
|
|
4282 |
|
// the order of einf and 3crc is not the same in v2 and v3 |
4283 |
|
if (einf && pVersion && pVersion->major == 3) pRIFF->MoveSubChunk(_3crc, einf); |
4284 |
} |
} |
4285 |
else throw gig::Exception("Mandatory <lins> list chunk not found."); |
} |
4286 |
|
|
4287 |
|
/** |
4288 |
|
* Enable / disable automatic loading. By default this properyt is |
4289 |
|
* enabled and all informations are loaded automatically. However |
4290 |
|
* loading all Regions, DimensionRegions and especially samples might |
4291 |
|
* take a long time for large .gig files, and sometimes one might only |
4292 |
|
* be interested in retrieving very superficial informations like the |
4293 |
|
* amount of instruments and their names. In this case one might disable |
4294 |
|
* automatic loading to avoid very slow response times. |
4295 |
|
* |
4296 |
|
* @e CAUTION: by disabling this property many pointers (i.e. sample |
4297 |
|
* references) and informations will have invalid or even undefined |
4298 |
|
* data! This feature is currently only intended for retrieving very |
4299 |
|
* superficial informations in a very fast way. Don't use it to retrieve |
4300 |
|
* details like synthesis informations or even to modify .gig files! |
4301 |
|
*/ |
4302 |
|
void File::SetAutoLoad(bool b) { |
4303 |
|
bAutoLoad = b; |
4304 |
|
} |
4305 |
|
|
4306 |
|
/** |
4307 |
|
* Returns whether automatic loading is enabled. |
4308 |
|
* @see SetAutoLoad() |
4309 |
|
*/ |
4310 |
|
bool File::GetAutoLoad() { |
4311 |
|
return bAutoLoad; |
4312 |
} |
} |
4313 |
|
|
4314 |
|
|
4323 |
std::cout << "gig::Exception: " << Message << std::endl; |
std::cout << "gig::Exception: " << Message << std::endl; |
4324 |
} |
} |
4325 |
|
|
4326 |
|
|
4327 |
|
// *************** functions *************** |
4328 |
|
// * |
4329 |
|
|
4330 |
|
/** |
4331 |
|
* Returns the name of this C++ library. This is usually "libgig" of |
4332 |
|
* course. This call is equivalent to RIFF::libraryName() and |
4333 |
|
* DLS::libraryName(). |
4334 |
|
*/ |
4335 |
|
String libraryName() { |
4336 |
|
return PACKAGE; |
4337 |
|
} |
4338 |
|
|
4339 |
|
/** |
4340 |
|
* Returns version of this C++ library. This call is equivalent to |
4341 |
|
* RIFF::libraryVersion() and DLS::libraryVersion(). |
4342 |
|
*/ |
4343 |
|
String libraryVersion() { |
4344 |
|
return VERSION; |
4345 |
|
} |
4346 |
|
|
4347 |
} // namespace gig |
} // namespace gig |