2 |
* * |
* * |
3 |
* libgig - C++ cross-platform Gigasampler format file loader library * |
* libgig - C++ cross-platform Gigasampler format file loader library * |
4 |
* * |
* * |
5 |
* Copyright (C) 2003 by Christian Schoenebeck * |
* Copyright (C) 2003-2005 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 * |
9 |
* it under the terms of the GNU General Public License as published by * |
* it under the terms of the GNU General Public License as published by * |
23 |
|
|
24 |
#include "gig.h" |
#include "gig.h" |
25 |
|
|
26 |
namespace gig { |
#include <iostream> |
27 |
|
|
28 |
|
namespace gig { namespace { |
29 |
|
|
30 |
|
// *************** Internal functions for sample decopmression *************** |
31 |
|
// * |
32 |
|
|
33 |
|
inline int get12lo(const unsigned char* pSrc) |
34 |
|
{ |
35 |
|
const int x = pSrc[0] | (pSrc[1] & 0x0f) << 8; |
36 |
|
return x & 0x800 ? x - 0x1000 : x; |
37 |
|
} |
38 |
|
|
39 |
|
inline int get12hi(const unsigned char* pSrc) |
40 |
|
{ |
41 |
|
const int x = pSrc[1] >> 4 | pSrc[2] << 4; |
42 |
|
return x & 0x800 ? x - 0x1000 : x; |
43 |
|
} |
44 |
|
|
45 |
|
inline int16_t get16(const unsigned char* pSrc) |
46 |
|
{ |
47 |
|
return int16_t(pSrc[0] | pSrc[1] << 8); |
48 |
|
} |
49 |
|
|
50 |
|
inline int get24(const unsigned char* pSrc) |
51 |
|
{ |
52 |
|
const int x = pSrc[0] | pSrc[1] << 8 | pSrc[2] << 16; |
53 |
|
return x & 0x800000 ? x - 0x1000000 : x; |
54 |
|
} |
55 |
|
|
56 |
|
void Decompress16(int compressionmode, const unsigned char* params, |
57 |
|
int srcStep, int dstStep, |
58 |
|
const unsigned char* pSrc, int16_t* pDst, |
59 |
|
unsigned long currentframeoffset, |
60 |
|
unsigned long copysamples) |
61 |
|
{ |
62 |
|
switch (compressionmode) { |
63 |
|
case 0: // 16 bit uncompressed |
64 |
|
pSrc += currentframeoffset * srcStep; |
65 |
|
while (copysamples) { |
66 |
|
*pDst = get16(pSrc); |
67 |
|
pDst += dstStep; |
68 |
|
pSrc += srcStep; |
69 |
|
copysamples--; |
70 |
|
} |
71 |
|
break; |
72 |
|
|
73 |
|
case 1: // 16 bit compressed to 8 bit |
74 |
|
int y = get16(params); |
75 |
|
int dy = get16(params + 2); |
76 |
|
while (currentframeoffset) { |
77 |
|
dy -= int8_t(*pSrc); |
78 |
|
y -= dy; |
79 |
|
pSrc += srcStep; |
80 |
|
currentframeoffset--; |
81 |
|
} |
82 |
|
while (copysamples) { |
83 |
|
dy -= int8_t(*pSrc); |
84 |
|
y -= dy; |
85 |
|
*pDst = y; |
86 |
|
pDst += dstStep; |
87 |
|
pSrc += srcStep; |
88 |
|
copysamples--; |
89 |
|
} |
90 |
|
break; |
91 |
|
} |
92 |
|
} |
93 |
|
|
94 |
|
void Decompress24(int compressionmode, const unsigned char* params, |
95 |
|
int dstStep, const unsigned char* pSrc, int16_t* pDst, |
96 |
|
unsigned long currentframeoffset, |
97 |
|
unsigned long copysamples) |
98 |
|
{ |
99 |
|
// Note: The 24 bits are truncated to 16 bits for now. |
100 |
|
|
101 |
|
// Note: The calculation of the initial value of y is strange |
102 |
|
// and not 100% correct. What should the first two parameters |
103 |
|
// really be used for? Why are they two? The correct value for |
104 |
|
// y seems to lie somewhere between the values of the first |
105 |
|
// two parameters. |
106 |
|
// |
107 |
|
// Strange thing #2: The formula in SKIP_ONE gives values for |
108 |
|
// y that are twice as high as they should be. That's why |
109 |
|
// COPY_ONE shifts 9 steps instead of 8, and also why y is |
110 |
|
// initialized with a sum instead of a mean value. |
111 |
|
|
112 |
|
int y, dy, ddy; |
113 |
|
|
114 |
|
#define GET_PARAMS(params) \ |
115 |
|
y = (get24(params) + get24((params) + 3)); \ |
116 |
|
dy = get24((params) + 6); \ |
117 |
|
ddy = get24((params) + 9) |
118 |
|
|
119 |
|
#define SKIP_ONE(x) \ |
120 |
|
ddy -= (x); \ |
121 |
|
dy -= ddy; \ |
122 |
|
y -= dy |
123 |
|
|
124 |
|
#define COPY_ONE(x) \ |
125 |
|
SKIP_ONE(x); \ |
126 |
|
*pDst = y >> 9; \ |
127 |
|
pDst += dstStep |
128 |
|
|
129 |
|
switch (compressionmode) { |
130 |
|
case 2: // 24 bit uncompressed |
131 |
|
pSrc += currentframeoffset * 3; |
132 |
|
while (copysamples) { |
133 |
|
*pDst = get24(pSrc) >> 8; |
134 |
|
pDst += dstStep; |
135 |
|
pSrc += 3; |
136 |
|
copysamples--; |
137 |
|
} |
138 |
|
break; |
139 |
|
|
140 |
|
case 3: // 24 bit compressed to 16 bit |
141 |
|
GET_PARAMS(params); |
142 |
|
while (currentframeoffset) { |
143 |
|
SKIP_ONE(get16(pSrc)); |
144 |
|
pSrc += 2; |
145 |
|
currentframeoffset--; |
146 |
|
} |
147 |
|
while (copysamples) { |
148 |
|
COPY_ONE(get16(pSrc)); |
149 |
|
pSrc += 2; |
150 |
|
copysamples--; |
151 |
|
} |
152 |
|
break; |
153 |
|
|
154 |
|
case 4: // 24 bit compressed to 12 bit |
155 |
|
GET_PARAMS(params); |
156 |
|
while (currentframeoffset > 1) { |
157 |
|
SKIP_ONE(get12lo(pSrc)); |
158 |
|
SKIP_ONE(get12hi(pSrc)); |
159 |
|
pSrc += 3; |
160 |
|
currentframeoffset -= 2; |
161 |
|
} |
162 |
|
if (currentframeoffset) { |
163 |
|
SKIP_ONE(get12lo(pSrc)); |
164 |
|
currentframeoffset--; |
165 |
|
if (copysamples) { |
166 |
|
COPY_ONE(get12hi(pSrc)); |
167 |
|
pSrc += 3; |
168 |
|
copysamples--; |
169 |
|
} |
170 |
|
} |
171 |
|
while (copysamples > 1) { |
172 |
|
COPY_ONE(get12lo(pSrc)); |
173 |
|
COPY_ONE(get12hi(pSrc)); |
174 |
|
pSrc += 3; |
175 |
|
copysamples -= 2; |
176 |
|
} |
177 |
|
if (copysamples) { |
178 |
|
COPY_ONE(get12lo(pSrc)); |
179 |
|
} |
180 |
|
break; |
181 |
|
|
182 |
|
case 5: // 24 bit compressed to 8 bit |
183 |
|
GET_PARAMS(params); |
184 |
|
while (currentframeoffset) { |
185 |
|
SKIP_ONE(int8_t(*pSrc++)); |
186 |
|
currentframeoffset--; |
187 |
|
} |
188 |
|
while (copysamples) { |
189 |
|
COPY_ONE(int8_t(*pSrc++)); |
190 |
|
copysamples--; |
191 |
|
} |
192 |
|
break; |
193 |
|
} |
194 |
|
} |
195 |
|
|
196 |
|
const int bytesPerFrame[] = { 4096, 2052, 768, 524, 396, 268 }; |
197 |
|
const int bytesPerFrameNoHdr[] = { 4096, 2048, 768, 512, 384, 256 }; |
198 |
|
const int headerSize[] = { 0, 4, 0, 12, 12, 12 }; |
199 |
|
const int bitsPerSample[] = { 16, 8, 24, 16, 12, 8 }; |
200 |
|
} |
201 |
|
|
202 |
|
|
203 |
// *************** Sample *************** |
// *************** Sample *************** |
204 |
// * |
// * |
205 |
|
|
206 |
unsigned int Sample::Instances = 0; |
unsigned int Sample::Instances = 0; |
207 |
void* Sample::pDecompressionBuffer = NULL; |
buffer_t Sample::InternalDecompressionBuffer; |
|
unsigned long Sample::DecompressionBufferSize = 0; |
|
208 |
|
|
209 |
Sample::Sample(File* pFile, RIFF::List* waveList, unsigned long WavePoolOffset) : DLS::Sample((DLS::File*) pFile, waveList, WavePoolOffset) { |
Sample::Sample(File* pFile, RIFF::List* waveList, unsigned long WavePoolOffset) : DLS::Sample((DLS::File*) pFile, waveList, WavePoolOffset) { |
210 |
Instances++; |
Instances++; |
223 |
smpl->Read(&SMPTEFormat, 1, 4); |
smpl->Read(&SMPTEFormat, 1, 4); |
224 |
SMPTEOffset = smpl->ReadInt32(); |
SMPTEOffset = smpl->ReadInt32(); |
225 |
Loops = smpl->ReadInt32(); |
Loops = smpl->ReadInt32(); |
226 |
uint32_t manufByt = smpl->ReadInt32(); |
smpl->ReadInt32(); // manufByt |
227 |
LoopID = smpl->ReadInt32(); |
LoopID = smpl->ReadInt32(); |
228 |
smpl->Read(&LoopType, 1, 4); |
smpl->Read(&LoopType, 1, 4); |
229 |
LoopStart = smpl->ReadInt32(); |
LoopStart = smpl->ReadInt32(); |
237 |
RAMCache.pStart = NULL; |
RAMCache.pStart = NULL; |
238 |
RAMCache.NullExtensionSize = 0; |
RAMCache.NullExtensionSize = 0; |
239 |
|
|
240 |
|
if (BitDepth > 24) throw gig::Exception("Only samples up to 24 bit supported"); |
241 |
|
|
242 |
Compressed = (waveList->GetSubChunk(CHUNK_ID_EWAV)); |
Compressed = (waveList->GetSubChunk(CHUNK_ID_EWAV)); |
243 |
if (Compressed) { |
if (Compressed) { |
244 |
ScanCompressedSample(); |
ScanCompressedSample(); |
245 |
if (!pDecompressionBuffer) { |
} |
246 |
pDecompressionBuffer = new int8_t[INITIAL_SAMPLE_BUFFER_SIZE]; |
|
247 |
DecompressionBufferSize = INITIAL_SAMPLE_BUFFER_SIZE; |
// we use a buffer for decompression and for truncating 24 bit samples to 16 bit |
248 |
} |
if ((Compressed || BitDepth == 24) && !InternalDecompressionBuffer.Size) { |
249 |
|
InternalDecompressionBuffer.pStart = new unsigned char[INITIAL_SAMPLE_BUFFER_SIZE]; |
250 |
|
InternalDecompressionBuffer.Size = INITIAL_SAMPLE_BUFFER_SIZE; |
251 |
} |
} |
252 |
FrameOffset = 0; // just for streaming compressed samples |
FrameOffset = 0; // just for streaming compressed samples |
253 |
|
|
260 |
this->SamplesTotal = 0; |
this->SamplesTotal = 0; |
261 |
std::list<unsigned long> frameOffsets; |
std::list<unsigned long> frameOffsets; |
262 |
|
|
263 |
|
SamplesPerFrame = BitDepth == 24 ? 256 : 2048; |
264 |
|
WorstCaseFrameSize = SamplesPerFrame * FrameSize + Channels; // +Channels for compression flag |
265 |
|
|
266 |
// Scanning |
// Scanning |
267 |
pCkData->SetPos(0); |
pCkData->SetPos(0); |
268 |
while (pCkData->GetState() == RIFF::stream_ready) { |
if (Channels == 2) { // Stereo |
269 |
frameOffsets.push_back(pCkData->GetPos()); |
for (int i = 0 ; ; i++) { |
270 |
int16_t compressionmode = pCkData->ReadInt16(); |
// for 24 bit samples every 8:th frame offset is |
271 |
this->SamplesTotal += 2048; |
// stored, to save some memory |
272 |
switch (compressionmode) { |
if (BitDepth != 24 || (i & 7) == 0) frameOffsets.push_back(pCkData->GetPos()); |
273 |
case 1: // left channel compressed |
|
274 |
case 256: // right channel compressed |
const int mode_l = pCkData->ReadUint8(); |
275 |
pCkData->SetPos(6148, RIFF::stream_curpos); |
const int mode_r = pCkData->ReadUint8(); |
276 |
|
if (mode_l > 5 || mode_r > 5) throw gig::Exception("Unknown compression mode"); |
277 |
|
const unsigned long frameSize = bytesPerFrame[mode_l] + bytesPerFrame[mode_r]; |
278 |
|
|
279 |
|
if (pCkData->RemainingBytes() <= frameSize) { |
280 |
|
SamplesInLastFrame = |
281 |
|
((pCkData->RemainingBytes() - headerSize[mode_l] - headerSize[mode_r]) << 3) / |
282 |
|
(bitsPerSample[mode_l] + bitsPerSample[mode_r]); |
283 |
|
SamplesTotal += SamplesInLastFrame; |
284 |
break; |
break; |
285 |
case 257: // both channels compressed |
} |
286 |
pCkData->SetPos(4104, RIFF::stream_curpos); |
SamplesTotal += SamplesPerFrame; |
287 |
|
pCkData->SetPos(frameSize, RIFF::stream_curpos); |
288 |
|
} |
289 |
|
} |
290 |
|
else { // Mono |
291 |
|
for (int i = 0 ; ; i++) { |
292 |
|
if (BitDepth != 24 || (i & 7) == 0) frameOffsets.push_back(pCkData->GetPos()); |
293 |
|
|
294 |
|
const int mode = pCkData->ReadUint8(); |
295 |
|
if (mode > 5) throw gig::Exception("Unknown compression mode"); |
296 |
|
const unsigned long frameSize = bytesPerFrame[mode]; |
297 |
|
|
298 |
|
if (pCkData->RemainingBytes() <= frameSize) { |
299 |
|
SamplesInLastFrame = |
300 |
|
((pCkData->RemainingBytes() - headerSize[mode]) << 3) / bitsPerSample[mode]; |
301 |
|
SamplesTotal += SamplesInLastFrame; |
302 |
break; |
break; |
303 |
default: // both channels uncompressed |
} |
304 |
pCkData->SetPos(8192, RIFF::stream_curpos); |
SamplesTotal += SamplesPerFrame; |
305 |
|
pCkData->SetPos(frameSize, RIFF::stream_curpos); |
306 |
} |
} |
307 |
} |
} |
308 |
pCkData->SetPos(0); |
pCkData->SetPos(0); |
309 |
|
|
|
//FIXME: only seen compressed samples with 16 bit stereo so far |
|
|
this->FrameSize = 4; |
|
|
this->BitDepth = 16; |
|
|
|
|
310 |
// Build the frames table (which is used for fast resolving of a frame's chunk offset) |
// Build the frames table (which is used for fast resolving of a frame's chunk offset) |
311 |
if (FrameTable) delete[] FrameTable; |
if (FrameTable) delete[] FrameTable; |
312 |
FrameTable = new unsigned long[frameOffsets.size()]; |
FrameTable = new unsigned long[frameOffsets.size()]; |
342 |
* that will be returned to determine the actual cached samples, but note |
* that will be returned to determine the actual cached samples, but note |
343 |
* that the size is given in bytes! You get the number of actually cached |
* that the size is given in bytes! You get the number of actually cached |
344 |
* samples by dividing it by the frame size of the sample: |
* samples by dividing it by the frame size of the sample: |
345 |
* |
* @code |
346 |
* buffer_t buf = pSample->LoadSampleData(acquired_samples); |
* buffer_t buf = pSample->LoadSampleData(acquired_samples); |
347 |
* long cachedsamples = buf.Size / pSample->FrameSize; |
* long cachedsamples = buf.Size / pSample->FrameSize; |
348 |
|
* @endcode |
349 |
* |
* |
350 |
* @param SampleCount - number of sample points to load into RAM |
* @param SampleCount - number of sample points to load into RAM |
351 |
* @returns buffer_t structure with start address and size of |
* @returns buffer_t structure with start address and size of |
391 |
* that will be returned to determine the actual cached samples, but note |
* that will be returned to determine the actual cached samples, but note |
392 |
* that the size is given in bytes! You get the number of actually cached |
* that the size is given in bytes! You get the number of actually cached |
393 |
* samples by dividing it by the frame size of the sample: |
* samples by dividing it by the frame size of the sample: |
394 |
* |
* @code |
395 |
* buffer_t buf = pSample->LoadSampleDataWithNullSamplesExtension(acquired_samples, null_samples); |
* buffer_t buf = pSample->LoadSampleDataWithNullSamplesExtension(acquired_samples, null_samples); |
396 |
* long cachedsamples = buf.Size / pSample->FrameSize; |
* long cachedsamples = buf.Size / pSample->FrameSize; |
397 |
* |
* @endcode |
398 |
* The method will add \a NullSamplesCount silence samples past the |
* The method will add \a NullSamplesCount silence samples past the |
399 |
* official buffer end (this won't affect the 'Size' member of the |
* official buffer end (this won't affect the 'Size' member of the |
400 |
* buffer_t structure, that means 'Size' always reflects the size of the |
* buffer_t structure, that means 'Size' always reflects the size of the |
525 |
* for the next time you call this method is stored in \a pPlaybackState. |
* for the next time you call this method is stored in \a pPlaybackState. |
526 |
* You have to allocate and initialize the playback_state_t structure by |
* You have to allocate and initialize the playback_state_t structure by |
527 |
* yourself before you use it to stream a sample: |
* yourself before you use it to stream a sample: |
528 |
* |
* @code |
529 |
* <i> |
* gig::playback_state_t playbackstate; |
530 |
* gig::playback_state_t playbackstate; <br> |
* playbackstate.position = 0; |
531 |
* playbackstate.position = 0; <br> |
* playbackstate.reverse = false; |
532 |
* playbackstate.reverse = false; <br> |
* playbackstate.loop_cycles_left = pSample->LoopPlayCount; |
533 |
* playbackstate.loop_cycles_left = pSample->LoopPlayCount; <br> |
* @endcode |
|
* </i> |
|
|
* |
|
534 |
* You don't have to take care of things like if there is actually a loop |
* You don't have to take care of things like if there is actually a loop |
535 |
* defined or if the current read position is located within a loop area. |
* defined or if the current read position is located within a loop area. |
536 |
* The method already handles such cases by itself. |
* The method already handles such cases by itself. |
537 |
* |
* |
538 |
|
* <b>Caution:</b> If you are using more than one streaming thread, you |
539 |
|
* have to use an external decompression buffer for <b>EACH</b> |
540 |
|
* streaming thread to avoid race conditions and crashes! |
541 |
|
* |
542 |
* @param pBuffer destination buffer |
* @param pBuffer destination buffer |
543 |
* @param SampleCount number of sample points to read |
* @param SampleCount number of sample points to read |
544 |
* @param pPlaybackState will be used to store and reload the playback |
* @param pPlaybackState will be used to store and reload the playback |
545 |
* state for the next ReadAndLoop() call |
* state for the next ReadAndLoop() call |
546 |
|
* @param pExternalDecompressionBuffer (optional) external buffer to use for decompression |
547 |
* @returns number of successfully read sample points |
* @returns number of successfully read sample points |
548 |
|
* @see CreateDecompressionBuffer() |
549 |
*/ |
*/ |
550 |
unsigned long Sample::ReadAndLoop(void* pBuffer, unsigned long SampleCount, playback_state_t* pPlaybackState) { |
unsigned long Sample::ReadAndLoop(void* pBuffer, unsigned long SampleCount, playback_state_t* pPlaybackState, buffer_t* pExternalDecompressionBuffer) { |
551 |
unsigned long samplestoread = SampleCount, totalreadsamples = 0, readsamples, samplestoloopend; |
unsigned long samplestoread = SampleCount, totalreadsamples = 0, readsamples, samplestoloopend; |
552 |
uint8_t* pDst = (uint8_t*) pBuffer; |
uint8_t* pDst = (uint8_t*) pBuffer; |
553 |
|
|
565 |
if (!pPlaybackState->reverse) { // forward playback |
if (!pPlaybackState->reverse) { // forward playback |
566 |
do { |
do { |
567 |
samplestoloopend = this->LoopEnd - GetPos(); |
samplestoloopend = this->LoopEnd - GetPos(); |
568 |
readsamples = Read(&pDst[totalreadsamples * this->FrameSize], Min(samplestoread, samplestoloopend)); |
readsamples = Read(&pDst[totalreadsamples * this->FrameSize], Min(samplestoread, samplestoloopend), pExternalDecompressionBuffer); |
569 |
samplestoread -= readsamples; |
samplestoread -= readsamples; |
570 |
totalreadsamples += readsamples; |
totalreadsamples += readsamples; |
571 |
if (readsamples == samplestoloopend) { |
if (readsamples == samplestoloopend) { |
591 |
|
|
592 |
// read samples for backward playback |
// read samples for backward playback |
593 |
do { |
do { |
594 |
readsamples = Read(&pDst[totalreadsamples * this->FrameSize], samplestoreadinloop); |
readsamples = Read(&pDst[totalreadsamples * this->FrameSize], samplestoreadinloop, pExternalDecompressionBuffer); |
595 |
samplestoreadinloop -= readsamples; |
samplestoreadinloop -= readsamples; |
596 |
samplestoread -= readsamples; |
samplestoread -= readsamples; |
597 |
totalreadsamples += readsamples; |
totalreadsamples += readsamples; |
615 |
// forward playback (not entered the loop yet) |
// forward playback (not entered the loop yet) |
616 |
if (!pPlaybackState->reverse) do { |
if (!pPlaybackState->reverse) do { |
617 |
samplestoloopend = this->LoopEnd - GetPos(); |
samplestoloopend = this->LoopEnd - GetPos(); |
618 |
readsamples = Read(&pDst[totalreadsamples * this->FrameSize], Min(samplestoread, samplestoloopend)); |
readsamples = Read(&pDst[totalreadsamples * this->FrameSize], Min(samplestoread, samplestoloopend), pExternalDecompressionBuffer); |
619 |
samplestoread -= readsamples; |
samplestoread -= readsamples; |
620 |
totalreadsamples += readsamples; |
totalreadsamples += readsamples; |
621 |
if (readsamples == samplestoloopend) { |
if (readsamples == samplestoloopend) { |
645 |
// 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 |
646 |
if (this->LoopPlayCount && !pPlaybackState->loop_cycles_left) break; |
if (this->LoopPlayCount && !pPlaybackState->loop_cycles_left) break; |
647 |
samplestoloopend = this->LoopEnd - GetPos(); |
samplestoloopend = this->LoopEnd - GetPos(); |
648 |
readsamples = Read(&pDst[totalreadsamples * this->FrameSize], Min(samplestoreadinloop, samplestoloopend)); |
readsamples = Read(&pDst[totalreadsamples * this->FrameSize], Min(samplestoreadinloop, samplestoloopend), pExternalDecompressionBuffer); |
649 |
samplestoreadinloop -= readsamples; |
samplestoreadinloop -= readsamples; |
650 |
samplestoread -= readsamples; |
samplestoread -= readsamples; |
651 |
totalreadsamples += readsamples; |
totalreadsamples += readsamples; |
667 |
// 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 |
668 |
if (this->LoopPlayCount && !pPlaybackState->loop_cycles_left) break; |
if (this->LoopPlayCount && !pPlaybackState->loop_cycles_left) break; |
669 |
samplestoloopend = this->LoopEnd - GetPos(); |
samplestoloopend = this->LoopEnd - GetPos(); |
670 |
readsamples = Read(&pDst[totalreadsamples * this->FrameSize], Min(samplestoread, samplestoloopend)); |
readsamples = Read(&pDst[totalreadsamples * this->FrameSize], Min(samplestoread, samplestoloopend), pExternalDecompressionBuffer); |
671 |
samplestoread -= readsamples; |
samplestoread -= readsamples; |
672 |
totalreadsamples += readsamples; |
totalreadsamples += readsamples; |
673 |
if (readsamples == samplestoloopend) { |
if (readsamples == samplestoloopend) { |
682 |
|
|
683 |
// read on without looping |
// read on without looping |
684 |
if (samplestoread) do { |
if (samplestoread) do { |
685 |
readsamples = Read(&pDst[totalreadsamples * this->FrameSize], samplestoread); |
readsamples = Read(&pDst[totalreadsamples * this->FrameSize], samplestoread, pExternalDecompressionBuffer); |
686 |
samplestoread -= readsamples; |
samplestoread -= readsamples; |
687 |
totalreadsamples += readsamples; |
totalreadsamples += readsamples; |
688 |
} while (readsamples && samplestoread); |
} while (readsamples && samplestoread); |
701 |
* and <i>SetPos()</i> if you don't want to load the sample into RAM, |
* and <i>SetPos()</i> if you don't want to load the sample into RAM, |
702 |
* thus for disk streaming. |
* thus for disk streaming. |
703 |
* |
* |
704 |
|
* <b>Caution:</b> If you are using more than one streaming thread, you |
705 |
|
* have to use an external decompression buffer for <b>EACH</b> |
706 |
|
* streaming thread to avoid race conditions and crashes! |
707 |
|
* |
708 |
* @param pBuffer destination buffer |
* @param pBuffer destination buffer |
709 |
* @param SampleCount number of sample points to read |
* @param SampleCount number of sample points to read |
710 |
|
* @param pExternalDecompressionBuffer (optional) external buffer to use for decompression |
711 |
* @returns number of successfully read sample points |
* @returns number of successfully read sample points |
712 |
* @see SetPos() |
* @see SetPos(), CreateDecompressionBuffer() |
713 |
*/ |
*/ |
714 |
unsigned long Sample::Read(void* pBuffer, unsigned long SampleCount) { |
unsigned long Sample::Read(void* pBuffer, unsigned long SampleCount, buffer_t* pExternalDecompressionBuffer) { |
715 |
if (SampleCount == 0) return 0; |
if (SampleCount == 0) return 0; |
716 |
if (!Compressed) return pCkData->Read(pBuffer, SampleCount, FrameSize); //FIXME: channel inversion due to endian correction? |
if (!Compressed) { |
717 |
else { //FIXME: no support for mono compressed samples yet, are there any? |
if (BitDepth == 24) { |
718 |
|
// 24 bit sample. For now just truncate to 16 bit. |
719 |
|
unsigned char* pSrc = (unsigned char*) ((pExternalDecompressionBuffer) ? pExternalDecompressionBuffer->pStart : this->InternalDecompressionBuffer.pStart); |
720 |
|
int16_t* pDst = static_cast<int16_t*>(pBuffer); |
721 |
|
if (Channels == 2) { // Stereo |
722 |
|
unsigned long readBytes = pCkData->Read(pSrc, SampleCount * 6, 1); |
723 |
|
pSrc++; |
724 |
|
for (unsigned long i = readBytes ; i > 0 ; i -= 3) { |
725 |
|
*pDst++ = get16(pSrc); |
726 |
|
pSrc += 3; |
727 |
|
} |
728 |
|
return (pDst - static_cast<int16_t*>(pBuffer)) >> 1; |
729 |
|
} |
730 |
|
else { // Mono |
731 |
|
unsigned long readBytes = pCkData->Read(pSrc, SampleCount * 3, 1); |
732 |
|
pSrc++; |
733 |
|
for (unsigned long i = readBytes ; i > 0 ; i -= 3) { |
734 |
|
*pDst++ = get16(pSrc); |
735 |
|
pSrc += 3; |
736 |
|
} |
737 |
|
return pDst - static_cast<int16_t*>(pBuffer); |
738 |
|
} |
739 |
|
} |
740 |
|
else { // 16 bit |
741 |
|
// (pCkData->Read does endian correction) |
742 |
|
return Channels == 2 ? pCkData->Read(pBuffer, SampleCount << 1, 2) >> 1 |
743 |
|
: pCkData->Read(pBuffer, SampleCount, 2); |
744 |
|
} |
745 |
|
} |
746 |
|
else { |
747 |
if (this->SamplePos >= this->SamplesTotal) return 0; |
if (this->SamplePos >= this->SamplesTotal) return 0; |
748 |
//TODO: efficiency: we simply assume here that all frames are compressed, maybe we should test for an average compression rate |
//TODO: efficiency: maybe we should test for an average compression rate |
749 |
// best case needed buffer size (all frames compressed) |
unsigned long assumedsize = GuessSize(SampleCount), |
|
unsigned long assumedsize = (SampleCount << 1) + // *2 (16 Bit, stereo, but assume all frames compressed) |
|
|
(SampleCount >> 10) + // 10 bytes header per 2048 sample points |
|
|
8194, // at least one worst case sample frame |
|
750 |
remainingbytes = 0, // remaining bytes in the local buffer |
remainingbytes = 0, // remaining bytes in the local buffer |
751 |
remainingsamples = SampleCount, |
remainingsamples = SampleCount, |
752 |
copysamples; |
copysamples, skipsamples, |
753 |
int currentframeoffset = this->FrameOffset; // offset in current sample frame since last Read() |
currentframeoffset = this->FrameOffset; // offset in current sample frame since last Read() |
754 |
this->FrameOffset = 0; |
this->FrameOffset = 0; |
755 |
|
|
756 |
if (assumedsize > this->DecompressionBufferSize) { |
buffer_t* pDecompressionBuffer = (pExternalDecompressionBuffer) ? pExternalDecompressionBuffer : &InternalDecompressionBuffer; |
757 |
// local buffer reallocation - hope this won't happen |
|
758 |
if (this->pDecompressionBuffer) delete[] (int8_t*) this->pDecompressionBuffer; |
// if decompression buffer too small, then reduce amount of samples to read |
759 |
this->pDecompressionBuffer = new int8_t[assumedsize << 1]; // double of current needed size |
if (pDecompressionBuffer->Size < assumedsize) { |
760 |
this->DecompressionBufferSize = assumedsize; |
std::cerr << "gig::Read(): WARNING - decompression buffer size too small!" << std::endl; |
761 |
|
SampleCount = WorstCaseMaxSamples(pDecompressionBuffer); |
762 |
|
remainingsamples = SampleCount; |
763 |
|
assumedsize = GuessSize(SampleCount); |
764 |
} |
} |
765 |
|
|
766 |
int16_t compressionmode, left, dleft, right, dright; |
unsigned char* pSrc = (unsigned char*) pDecompressionBuffer->pStart; |
767 |
int8_t* pSrc = (int8_t*) this->pDecompressionBuffer; |
int16_t* pDst = static_cast<int16_t*>(pBuffer); |
|
int16_t* pDst = (int16_t*) pBuffer; |
|
768 |
remainingbytes = pCkData->Read(pSrc, assumedsize, 1); |
remainingbytes = pCkData->Read(pSrc, assumedsize, 1); |
769 |
|
|
770 |
while (remainingsamples) { |
while (remainingsamples && remainingbytes) { |
771 |
|
unsigned long framesamples = SamplesPerFrame; |
772 |
// reload from disk to local buffer if needed |
unsigned long framebytes, rightChannelOffset = 0, nextFrameOffset; |
773 |
if (remainingbytes < 8194) { |
|
774 |
if (pCkData->GetState() != RIFF::stream_ready) { |
int mode_l = *pSrc++, mode_r = 0; |
775 |
this->SamplePos = this->SamplesTotal; |
|
776 |
return (SampleCount - remainingsamples); |
if (Channels == 2) { |
777 |
|
mode_r = *pSrc++; |
778 |
|
framebytes = bytesPerFrame[mode_l] + bytesPerFrame[mode_r] + 2; |
779 |
|
rightChannelOffset = bytesPerFrameNoHdr[mode_l]; |
780 |
|
nextFrameOffset = rightChannelOffset + bytesPerFrameNoHdr[mode_r]; |
781 |
|
if (remainingbytes < framebytes) { // last frame in sample |
782 |
|
framesamples = SamplesInLastFrame; |
783 |
|
if (mode_l == 4 && (framesamples & 1)) { |
784 |
|
rightChannelOffset = ((framesamples + 1) * bitsPerSample[mode_l]) >> 3; |
785 |
|
} |
786 |
|
else { |
787 |
|
rightChannelOffset = (framesamples * bitsPerSample[mode_l]) >> 3; |
788 |
|
} |
789 |
|
} |
790 |
|
} |
791 |
|
else { |
792 |
|
framebytes = bytesPerFrame[mode_l] + 1; |
793 |
|
nextFrameOffset = bytesPerFrameNoHdr[mode_l]; |
794 |
|
if (remainingbytes < framebytes) { |
795 |
|
framesamples = SamplesInLastFrame; |
796 |
} |
} |
|
assumedsize = remainingsamples; |
|
|
assumedsize = (assumedsize << 1) + // *2 (16 Bit, stereo, but assume all frames compressed) |
|
|
(assumedsize >> 10) + // 10 bytes header per 2048 sample points |
|
|
8194; // at least one worst case sample frame |
|
|
pCkData->SetPos(remainingbytes, RIFF::stream_backward); |
|
|
if (pCkData->RemainingBytes() < assumedsize) assumedsize = pCkData->RemainingBytes(); |
|
|
remainingbytes = pCkData->Read(this->pDecompressionBuffer, assumedsize, 1); |
|
|
pSrc = (int8_t*) this->pDecompressionBuffer; |
|
797 |
} |
} |
798 |
|
|
799 |
// determine how many samples in this frame to skip and read |
// determine how many samples in this frame to skip and read |
800 |
if (remainingsamples >= 2048) { |
if (currentframeoffset + remainingsamples >= framesamples) { |
801 |
copysamples = 2048 - currentframeoffset; |
if (currentframeoffset <= framesamples) { |
802 |
remainingsamples -= copysamples; |
copysamples = framesamples - currentframeoffset; |
803 |
|
skipsamples = currentframeoffset; |
804 |
|
} |
805 |
|
else { |
806 |
|
copysamples = 0; |
807 |
|
skipsamples = framesamples; |
808 |
|
} |
809 |
} |
} |
810 |
else { |
else { |
811 |
|
// This frame has enough data for pBuffer, but not |
812 |
|
// all of the frame is needed. Set file position |
813 |
|
// to start of this frame for next call to Read. |
814 |
copysamples = remainingsamples; |
copysamples = remainingsamples; |
815 |
if (currentframeoffset + copysamples > 2048) { |
skipsamples = currentframeoffset; |
816 |
copysamples = 2048 - currentframeoffset; |
pCkData->SetPos(remainingbytes, RIFF::stream_backward); |
817 |
remainingsamples -= copysamples; |
this->FrameOffset = currentframeoffset + copysamples; |
818 |
} |
} |
819 |
else { |
remainingsamples -= copysamples; |
820 |
|
|
821 |
|
if (remainingbytes > framebytes) { |
822 |
|
remainingbytes -= framebytes; |
823 |
|
if (remainingsamples == 0 && |
824 |
|
currentframeoffset + copysamples == framesamples) { |
825 |
|
// This frame has enough data for pBuffer, and |
826 |
|
// all of the frame is needed. Set file |
827 |
|
// position to start of next frame for next |
828 |
|
// call to Read. FrameOffset is 0. |
829 |
pCkData->SetPos(remainingbytes, RIFF::stream_backward); |
pCkData->SetPos(remainingbytes, RIFF::stream_backward); |
|
remainingsamples = 0; |
|
|
this->FrameOffset = currentframeoffset + copysamples; |
|
830 |
} |
} |
831 |
} |
} |
832 |
|
else remainingbytes = 0; |
833 |
|
|
834 |
// decompress and copy current frame from local buffer to destination buffer |
currentframeoffset -= skipsamples; |
835 |
compressionmode = *(int16_t*)pSrc; pSrc+=2; |
|
836 |
switch (compressionmode) { |
if (copysamples == 0) { |
837 |
case 1: // left channel compressed |
// skip this frame |
838 |
remainingbytes -= 6150; // (left 8 bit, right 16 bit, +6 byte header) |
pSrc += framebytes - Channels; |
839 |
if (!remainingsamples && copysamples == 2048) |
} |
840 |
pCkData->SetPos(remainingbytes, RIFF::stream_backward); |
else { |
841 |
|
const unsigned char* const param_l = pSrc; |
842 |
left = *(int16_t*)pSrc; pSrc+=2; |
if (BitDepth == 24) { |
843 |
dleft = *(int16_t*)pSrc; pSrc+=2; |
if (mode_l != 2) pSrc += 12; |
844 |
while (currentframeoffset) { |
|
845 |
dleft -= *pSrc; |
if (Channels == 2) { // Stereo |
846 |
left -= dleft; |
const unsigned char* const param_r = pSrc; |
847 |
pSrc+=3; // 8 bit left channel, skip uncompressed right channel (16 bit) |
if (mode_r != 2) pSrc += 12; |
848 |
currentframeoffset--; |
|
849 |
} |
Decompress24(mode_l, param_l, 2, pSrc, pDst, skipsamples, copysamples); |
850 |
while (copysamples) { |
Decompress24(mode_r, param_r, 2, pSrc + rightChannelOffset, pDst + 1, |
851 |
dleft -= *pSrc; pSrc++; |
skipsamples, copysamples); |
852 |
left -= dleft; |
pDst += copysamples << 1; |
|
*pDst = left; pDst++; |
|
|
*pDst = *(int16_t*)pSrc; pDst++; pSrc+=2; |
|
|
copysamples--; |
|
|
} |
|
|
break; |
|
|
case 256: // right channel compressed |
|
|
remainingbytes -= 6150; // (left 16 bit, right 8 bit, +6 byte header) |
|
|
if (!remainingsamples && copysamples == 2048) |
|
|
pCkData->SetPos(remainingbytes, RIFF::stream_backward); |
|
|
|
|
|
right = *(int16_t*)pSrc; pSrc+=2; |
|
|
dright = *(int16_t*)pSrc; pSrc+=2; |
|
|
if (currentframeoffset) { |
|
|
pSrc+=2; // skip uncompressed left channel, now we can increment by 3 |
|
|
while (currentframeoffset) { |
|
|
dright -= *pSrc; |
|
|
right -= dright; |
|
|
pSrc+=3; // 8 bit right channel, skip uncompressed left channel (16 bit) |
|
|
currentframeoffset--; |
|
|
} |
|
|
pSrc-=2; // back aligned to left channel |
|
853 |
} |
} |
854 |
while (copysamples) { |
else { // Mono |
855 |
*pDst = *(int16_t*)pSrc; pDst++; pSrc+=2; |
Decompress24(mode_l, param_l, 1, pSrc, pDst, skipsamples, copysamples); |
856 |
dright -= *pSrc; pSrc++; |
pDst += copysamples; |
|
right -= dright; |
|
|
*pDst = right; pDst++; |
|
|
copysamples--; |
|
857 |
} |
} |
858 |
break; |
} |
859 |
case 257: // both channels compressed |
else { // 16 bit |
860 |
remainingbytes -= 4106; // (left 8 bit, right 8 bit, +10 byte header) |
if (mode_l) pSrc += 4; |
861 |
if (!remainingsamples && copysamples == 2048) |
|
862 |
pCkData->SetPos(remainingbytes, RIFF::stream_backward); |
int step; |
863 |
|
if (Channels == 2) { // Stereo |
864 |
left = *(int16_t*)pSrc; pSrc+=2; |
const unsigned char* const param_r = pSrc; |
865 |
dleft = *(int16_t*)pSrc; pSrc+=2; |
if (mode_r) pSrc += 4; |
866 |
right = *(int16_t*)pSrc; pSrc+=2; |
|
867 |
dright = *(int16_t*)pSrc; pSrc+=2; |
step = (2 - mode_l) + (2 - mode_r); |
868 |
while (currentframeoffset) { |
Decompress16(mode_l, param_l, step, 2, pSrc, pDst, skipsamples, copysamples); |
869 |
dleft -= *pSrc; pSrc++; |
Decompress16(mode_r, param_r, step, 2, pSrc + (2 - mode_l), pDst + 1, |
870 |
left -= dleft; |
skipsamples, copysamples); |
871 |
dright -= *pSrc; pSrc++; |
pDst += copysamples << 1; |
|
right -= dright; |
|
|
currentframeoffset--; |
|
872 |
} |
} |
873 |
while (copysamples) { |
else { // Mono |
874 |
dleft -= *pSrc; pSrc++; |
step = 2 - mode_l; |
875 |
left -= dleft; |
Decompress16(mode_l, param_l, step, 1, pSrc, pDst, skipsamples, copysamples); |
876 |
dright -= *pSrc; pSrc++; |
pDst += copysamples; |
|
right -= dright; |
|
|
*pDst = left; pDst++; |
|
|
*pDst = right; pDst++; |
|
|
copysamples--; |
|
877 |
} |
} |
878 |
break; |
} |
879 |
default: // both channels uncompressed |
pSrc += nextFrameOffset; |
|
remainingbytes -= 8194; // (left 16 bit, right 16 bit, +2 byte header) |
|
|
if (!remainingsamples && copysamples == 2048) |
|
|
pCkData->SetPos(remainingbytes, RIFF::stream_backward); |
|
|
|
|
|
pSrc += currentframeoffset << 2; |
|
|
currentframeoffset = 0; |
|
|
memcpy(pDst, pSrc, copysamples << 2); |
|
|
pDst += copysamples << 1; |
|
|
pSrc += copysamples << 2; |
|
|
break; |
|
880 |
} |
} |
881 |
} |
|
882 |
|
// reload from disk to local buffer if needed |
883 |
|
if (remainingsamples && remainingbytes < WorstCaseFrameSize && pCkData->GetState() == RIFF::stream_ready) { |
884 |
|
assumedsize = GuessSize(remainingsamples); |
885 |
|
pCkData->SetPos(remainingbytes, RIFF::stream_backward); |
886 |
|
if (pCkData->RemainingBytes() < assumedsize) assumedsize = pCkData->RemainingBytes(); |
887 |
|
remainingbytes = pCkData->Read(pDecompressionBuffer->pStart, assumedsize, 1); |
888 |
|
pSrc = (unsigned char*) pDecompressionBuffer->pStart; |
889 |
|
} |
890 |
|
} // while |
891 |
|
|
892 |
this->SamplePos += (SampleCount - remainingsamples); |
this->SamplePos += (SampleCount - remainingsamples); |
893 |
if (this->SamplePos > this->SamplesTotal) this->SamplePos = this->SamplesTotal; |
if (this->SamplePos > this->SamplesTotal) this->SamplePos = this->SamplesTotal; |
894 |
return (SampleCount - remainingsamples); |
return (SampleCount - remainingsamples); |
895 |
} |
} |
896 |
} |
} |
897 |
|
|
898 |
|
/** |
899 |
|
* Allocates a decompression buffer for streaming (compressed) samples |
900 |
|
* with Sample::Read(). If you are using more than one streaming thread |
901 |
|
* in your application you <b>HAVE</b> to create a decompression buffer |
902 |
|
* for <b>EACH</b> of your streaming threads and provide it with the |
903 |
|
* Sample::Read() call in order to avoid race conditions and crashes. |
904 |
|
* |
905 |
|
* You should free the memory occupied by the allocated buffer(s) once |
906 |
|
* you don't need one of your streaming threads anymore by calling |
907 |
|
* DestroyDecompressionBuffer(). |
908 |
|
* |
909 |
|
* @param MaxReadSize - the maximum size (in sample points) you ever |
910 |
|
* expect to read with one Read() call |
911 |
|
* @returns allocated decompression buffer |
912 |
|
* @see DestroyDecompressionBuffer() |
913 |
|
*/ |
914 |
|
buffer_t Sample::CreateDecompressionBuffer(unsigned long MaxReadSize) { |
915 |
|
buffer_t result; |
916 |
|
const double worstCaseHeaderOverhead = |
917 |
|
(256.0 /*frame size*/ + 12.0 /*header*/ + 2.0 /*compression type flag (stereo)*/) / 256.0; |
918 |
|
result.Size = (unsigned long) (double(MaxReadSize) * 3.0 /*(24 Bit)*/ * 2.0 /*stereo*/ * worstCaseHeaderOverhead); |
919 |
|
result.pStart = new int8_t[result.Size]; |
920 |
|
result.NullExtensionSize = 0; |
921 |
|
return result; |
922 |
|
} |
923 |
|
|
924 |
|
/** |
925 |
|
* Free decompression buffer, previously created with |
926 |
|
* CreateDecompressionBuffer(). |
927 |
|
* |
928 |
|
* @param DecompressionBuffer - previously allocated decompression |
929 |
|
* buffer to free |
930 |
|
*/ |
931 |
|
void Sample::DestroyDecompressionBuffer(buffer_t& DecompressionBuffer) { |
932 |
|
if (DecompressionBuffer.Size && DecompressionBuffer.pStart) { |
933 |
|
delete[] (int8_t*) DecompressionBuffer.pStart; |
934 |
|
DecompressionBuffer.pStart = NULL; |
935 |
|
DecompressionBuffer.Size = 0; |
936 |
|
DecompressionBuffer.NullExtensionSize = 0; |
937 |
|
} |
938 |
|
} |
939 |
|
|
940 |
Sample::~Sample() { |
Sample::~Sample() { |
941 |
Instances--; |
Instances--; |
942 |
if (!Instances && pDecompressionBuffer) delete[] (int8_t*) pDecompressionBuffer; |
if (!Instances && InternalDecompressionBuffer.Size) { |
943 |
|
delete[] (unsigned char*) InternalDecompressionBuffer.pStart; |
944 |
|
InternalDecompressionBuffer.pStart = NULL; |
945 |
|
InternalDecompressionBuffer.Size = 0; |
946 |
|
} |
947 |
if (FrameTable) delete[] FrameTable; |
if (FrameTable) delete[] FrameTable; |
948 |
if (RAMCache.pStart) delete[] (int8_t*) RAMCache.pStart; |
if (RAMCache.pStart) delete[] (int8_t*) RAMCache.pStart; |
949 |
} |
} |
963 |
if (!pVelocityTables) pVelocityTables = new VelocityTableMap; |
if (!pVelocityTables) pVelocityTables = new VelocityTableMap; |
964 |
|
|
965 |
RIFF::Chunk* _3ewa = _3ewl->GetSubChunk(CHUNK_ID_3EWA); |
RIFF::Chunk* _3ewa = _3ewl->GetSubChunk(CHUNK_ID_3EWA); |
966 |
_3ewa->ReadInt32(); // unknown, allways 0x0000008C ? |
_3ewa->ReadInt32(); // unknown, always 0x0000008C ? |
967 |
LFO3Frequency = (double) GIG_EXP_DECODE(_3ewa->ReadInt32()); |
LFO3Frequency = (double) GIG_EXP_DECODE(_3ewa->ReadInt32()); |
968 |
EG3Attack = (double) GIG_EXP_DECODE(_3ewa->ReadInt32()); |
EG3Attack = (double) GIG_EXP_DECODE(_3ewa->ReadInt32()); |
969 |
_3ewa->ReadInt16(); // unknown |
_3ewa->ReadInt16(); // unknown |
979 |
_3ewa->ReadInt16(); // unknown |
_3ewa->ReadInt16(); // unknown |
980 |
EG1Sustain = _3ewa->ReadUint16(); |
EG1Sustain = _3ewa->ReadUint16(); |
981 |
EG1Release = (double) GIG_EXP_DECODE(_3ewa->ReadInt32()); |
EG1Release = (double) GIG_EXP_DECODE(_3ewa->ReadInt32()); |
982 |
EG1Controller = static_cast<eg1_ctrl_t>(_3ewa->ReadUint8()); |
EG1Controller = DecodeLeverageController(static_cast<_lev_ctrl_t>(_3ewa->ReadUint8())); |
983 |
uint8_t eg1ctrloptions = _3ewa->ReadUint8(); |
uint8_t eg1ctrloptions = _3ewa->ReadUint8(); |
984 |
EG1ControllerInvert = eg1ctrloptions & 0x01; |
EG1ControllerInvert = eg1ctrloptions & 0x01; |
985 |
EG1ControllerAttackInfluence = GIG_EG_CTR_ATTACK_INFLUENCE_EXTRACT(eg1ctrloptions); |
EG1ControllerAttackInfluence = GIG_EG_CTR_ATTACK_INFLUENCE_EXTRACT(eg1ctrloptions); |
986 |
EG1ControllerDecayInfluence = GIG_EG_CTR_DECAY_INFLUENCE_EXTRACT(eg1ctrloptions); |
EG1ControllerDecayInfluence = GIG_EG_CTR_DECAY_INFLUENCE_EXTRACT(eg1ctrloptions); |
987 |
EG1ControllerReleaseInfluence = GIG_EG_CTR_RELEASE_INFLUENCE_EXTRACT(eg1ctrloptions); |
EG1ControllerReleaseInfluence = GIG_EG_CTR_RELEASE_INFLUENCE_EXTRACT(eg1ctrloptions); |
988 |
EG2Controller = static_cast<eg2_ctrl_t>(_3ewa->ReadUint8()); |
EG2Controller = DecodeLeverageController(static_cast<_lev_ctrl_t>(_3ewa->ReadUint8())); |
989 |
uint8_t eg2ctrloptions = _3ewa->ReadUint8(); |
uint8_t eg2ctrloptions = _3ewa->ReadUint8(); |
990 |
EG2ControllerInvert = eg2ctrloptions & 0x01; |
EG2ControllerInvert = eg2ctrloptions & 0x01; |
991 |
EG2ControllerAttackInfluence = GIG_EG_CTR_ATTACK_INFLUENCE_EXTRACT(eg2ctrloptions); |
EG2ControllerAttackInfluence = GIG_EG_CTR_ATTACK_INFLUENCE_EXTRACT(eg2ctrloptions); |
1047 |
ReleaseVelocityResponseDepth = 0; |
ReleaseVelocityResponseDepth = 0; |
1048 |
} |
} |
1049 |
VelocityResponseCurveScaling = _3ewa->ReadUint8(); |
VelocityResponseCurveScaling = _3ewa->ReadUint8(); |
1050 |
AttenuationControlTreshold = _3ewa->ReadInt8(); |
AttenuationControllerThreshold = _3ewa->ReadInt8(); |
1051 |
_3ewa->ReadInt32(); // unknown |
_3ewa->ReadInt32(); // unknown |
1052 |
SampleStartOffset = (uint16_t) _3ewa->ReadInt16(); |
SampleStartOffset = (uint16_t) _3ewa->ReadInt16(); |
1053 |
_3ewa->ReadInt16(); // unknown |
_3ewa->ReadInt16(); // unknown |
1057 |
else if (pitchTrackDimensionBypass & 0x20) DimensionBypass = dim_bypass_ctrl_95; |
else if (pitchTrackDimensionBypass & 0x20) DimensionBypass = dim_bypass_ctrl_95; |
1058 |
else DimensionBypass = dim_bypass_ctrl_none; |
else DimensionBypass = dim_bypass_ctrl_none; |
1059 |
uint8_t pan = _3ewa->ReadUint8(); |
uint8_t pan = _3ewa->ReadUint8(); |
1060 |
Pan = (pan < 64) ? pan : (-1) * (int8_t)pan - 63; |
Pan = (pan < 64) ? pan : -((int)pan - 63); // signed 7 bit -> signed 8 bit |
1061 |
SelfMask = _3ewa->ReadInt8() & 0x01; |
SelfMask = _3ewa->ReadInt8() & 0x01; |
1062 |
_3ewa->ReadInt8(); // unknown |
_3ewa->ReadInt8(); // unknown |
1063 |
uint8_t lfo3ctrl = _3ewa->ReadUint8(); |
uint8_t lfo3ctrl = _3ewa->ReadUint8(); |
1064 |
LFO3Controller = static_cast<lfo3_ctrl_t>(lfo3ctrl & 0x07); // lower 3 bits |
LFO3Controller = static_cast<lfo3_ctrl_t>(lfo3ctrl & 0x07); // lower 3 bits |
1065 |
LFO3Sync = lfo3ctrl & 0x20; // bit 5 |
LFO3Sync = lfo3ctrl & 0x20; // bit 5 |
1066 |
InvertAttenuationControl = lfo3ctrl & 0x80; // bit 7 |
InvertAttenuationController = lfo3ctrl & 0x80; // bit 7 |
1067 |
if (VCFType == vcf_type_lowpass) { |
AttenuationController = DecodeLeverageController(static_cast<_lev_ctrl_t>(_3ewa->ReadUint8())); |
|
if (lfo3ctrl & 0x40) // bit 6 |
|
|
VCFType = vcf_type_lowpassturbo; |
|
|
} |
|
|
AttenuationControl = static_cast<attenuation_ctrl_t>(_3ewa->ReadUint8()); |
|
1068 |
uint8_t lfo2ctrl = _3ewa->ReadUint8(); |
uint8_t lfo2ctrl = _3ewa->ReadUint8(); |
1069 |
LFO2Controller = static_cast<lfo2_ctrl_t>(lfo2ctrl & 0x07); // lower 3 bits |
LFO2Controller = static_cast<lfo2_ctrl_t>(lfo2ctrl & 0x07); // lower 3 bits |
1070 |
LFO2FlipPhase = lfo2ctrl & 0x80; // bit 7 |
LFO2FlipPhase = lfo2ctrl & 0x80; // bit 7 |
1108 |
VCFVelocityDynamicRange = vcfvelocity % 5; |
VCFVelocityDynamicRange = vcfvelocity % 5; |
1109 |
VCFVelocityCurve = static_cast<curve_type_t>(vcfvelocity / 5); |
VCFVelocityCurve = static_cast<curve_type_t>(vcfvelocity / 5); |
1110 |
VCFType = static_cast<vcf_type_t>(_3ewa->ReadUint8()); |
VCFType = static_cast<vcf_type_t>(_3ewa->ReadUint8()); |
1111 |
|
if (VCFType == vcf_type_lowpass) { |
1112 |
|
if (lfo3ctrl & 0x40) // bit 6 |
1113 |
|
VCFType = vcf_type_lowpassturbo; |
1114 |
|
} |
1115 |
|
|
1116 |
// get the corresponding velocity->volume table from the table map or create & calculate that table if it doesn't exist yet |
// get the corresponding velocity->volume table from the table map or create & calculate that table if it doesn't exist yet |
1117 |
uint32_t tableKey = (VelocityResponseCurve<<16) | (VelocityResponseDepth<<8) | VelocityResponseCurveScaling; |
uint32_t tableKey = (VelocityResponseCurve<<16) | (VelocityResponseDepth<<8) | VelocityResponseCurveScaling; |
1119 |
pVelocityAttenuationTable = (*pVelocityTables)[tableKey]; |
pVelocityAttenuationTable = (*pVelocityTables)[tableKey]; |
1120 |
} |
} |
1121 |
else { |
else { |
1122 |
pVelocityAttenuationTable = new double[128]; |
pVelocityAttenuationTable = |
1123 |
switch (VelocityResponseCurve) { // calculate the new table |
CreateVelocityTable(VelocityResponseCurve, |
1124 |
case curve_type_nonlinear: |
VelocityResponseDepth, |
1125 |
for (int velocity = 0; velocity < 128; velocity++) { |
VelocityResponseCurveScaling); |
|
pVelocityAttenuationTable[velocity] = |
|
|
GIG_VELOCITY_TRANSFORM_NONLINEAR((double)(velocity+1),(double)(VelocityResponseDepth+1),(double)VelocityResponseCurveScaling); |
|
|
if (pVelocityAttenuationTable[velocity] > 1.0) pVelocityAttenuationTable[velocity] = 1.0; |
|
|
else if (pVelocityAttenuationTable[velocity] < 0.0) pVelocityAttenuationTable[velocity] = 0.0; |
|
|
} |
|
|
break; |
|
|
case curve_type_linear: |
|
|
for (int velocity = 0; velocity < 128; velocity++) { |
|
|
pVelocityAttenuationTable[velocity] = |
|
|
GIG_VELOCITY_TRANSFORM_LINEAR((double)velocity,(double)(VelocityResponseDepth+1),(double)VelocityResponseCurveScaling); |
|
|
if (pVelocityAttenuationTable[velocity] > 1.0) pVelocityAttenuationTable[velocity] = 1.0; |
|
|
else if (pVelocityAttenuationTable[velocity] < 0.0) pVelocityAttenuationTable[velocity] = 0.0; |
|
|
} |
|
|
break; |
|
|
case curve_type_special: |
|
|
for (int velocity = 0; velocity < 128; velocity++) { |
|
|
pVelocityAttenuationTable[velocity] = |
|
|
GIG_VELOCITY_TRANSFORM_SPECIAL((double)(velocity+1),(double)(VelocityResponseDepth+1),(double)VelocityResponseCurveScaling); |
|
|
if (pVelocityAttenuationTable[velocity] > 1.0) pVelocityAttenuationTable[velocity] = 1.0; |
|
|
else if (pVelocityAttenuationTable[velocity] < 0.0) pVelocityAttenuationTable[velocity] = 0.0; |
|
|
} |
|
|
break; |
|
|
case curve_type_unknown: |
|
|
default: |
|
|
throw gig::Exception("Unknown transform curve type."); |
|
|
} |
|
1126 |
(*pVelocityTables)[tableKey] = pVelocityAttenuationTable; // put the new table into the tables map |
(*pVelocityTables)[tableKey] = pVelocityAttenuationTable; // put the new table into the tables map |
1127 |
} |
} |
1128 |
|
|
1129 |
|
SampleAttenuation = pow(10.0, -Gain / (20.0 * 655360)); |
1130 |
|
} |
1131 |
|
|
1132 |
|
leverage_ctrl_t DimensionRegion::DecodeLeverageController(_lev_ctrl_t EncodedController) { |
1133 |
|
leverage_ctrl_t decodedcontroller; |
1134 |
|
switch (EncodedController) { |
1135 |
|
// special controller |
1136 |
|
case _lev_ctrl_none: |
1137 |
|
decodedcontroller.type = leverage_ctrl_t::type_none; |
1138 |
|
decodedcontroller.controller_number = 0; |
1139 |
|
break; |
1140 |
|
case _lev_ctrl_velocity: |
1141 |
|
decodedcontroller.type = leverage_ctrl_t::type_velocity; |
1142 |
|
decodedcontroller.controller_number = 0; |
1143 |
|
break; |
1144 |
|
case _lev_ctrl_channelaftertouch: |
1145 |
|
decodedcontroller.type = leverage_ctrl_t::type_channelaftertouch; |
1146 |
|
decodedcontroller.controller_number = 0; |
1147 |
|
break; |
1148 |
|
|
1149 |
|
// ordinary MIDI control change controller |
1150 |
|
case _lev_ctrl_modwheel: |
1151 |
|
decodedcontroller.type = leverage_ctrl_t::type_controlchange; |
1152 |
|
decodedcontroller.controller_number = 1; |
1153 |
|
break; |
1154 |
|
case _lev_ctrl_breath: |
1155 |
|
decodedcontroller.type = leverage_ctrl_t::type_controlchange; |
1156 |
|
decodedcontroller.controller_number = 2; |
1157 |
|
break; |
1158 |
|
case _lev_ctrl_foot: |
1159 |
|
decodedcontroller.type = leverage_ctrl_t::type_controlchange; |
1160 |
|
decodedcontroller.controller_number = 4; |
1161 |
|
break; |
1162 |
|
case _lev_ctrl_effect1: |
1163 |
|
decodedcontroller.type = leverage_ctrl_t::type_controlchange; |
1164 |
|
decodedcontroller.controller_number = 12; |
1165 |
|
break; |
1166 |
|
case _lev_ctrl_effect2: |
1167 |
|
decodedcontroller.type = leverage_ctrl_t::type_controlchange; |
1168 |
|
decodedcontroller.controller_number = 13; |
1169 |
|
break; |
1170 |
|
case _lev_ctrl_genpurpose1: |
1171 |
|
decodedcontroller.type = leverage_ctrl_t::type_controlchange; |
1172 |
|
decodedcontroller.controller_number = 16; |
1173 |
|
break; |
1174 |
|
case _lev_ctrl_genpurpose2: |
1175 |
|
decodedcontroller.type = leverage_ctrl_t::type_controlchange; |
1176 |
|
decodedcontroller.controller_number = 17; |
1177 |
|
break; |
1178 |
|
case _lev_ctrl_genpurpose3: |
1179 |
|
decodedcontroller.type = leverage_ctrl_t::type_controlchange; |
1180 |
|
decodedcontroller.controller_number = 18; |
1181 |
|
break; |
1182 |
|
case _lev_ctrl_genpurpose4: |
1183 |
|
decodedcontroller.type = leverage_ctrl_t::type_controlchange; |
1184 |
|
decodedcontroller.controller_number = 19; |
1185 |
|
break; |
1186 |
|
case _lev_ctrl_portamentotime: |
1187 |
|
decodedcontroller.type = leverage_ctrl_t::type_controlchange; |
1188 |
|
decodedcontroller.controller_number = 5; |
1189 |
|
break; |
1190 |
|
case _lev_ctrl_sustainpedal: |
1191 |
|
decodedcontroller.type = leverage_ctrl_t::type_controlchange; |
1192 |
|
decodedcontroller.controller_number = 64; |
1193 |
|
break; |
1194 |
|
case _lev_ctrl_portamento: |
1195 |
|
decodedcontroller.type = leverage_ctrl_t::type_controlchange; |
1196 |
|
decodedcontroller.controller_number = 65; |
1197 |
|
break; |
1198 |
|
case _lev_ctrl_sostenutopedal: |
1199 |
|
decodedcontroller.type = leverage_ctrl_t::type_controlchange; |
1200 |
|
decodedcontroller.controller_number = 66; |
1201 |
|
break; |
1202 |
|
case _lev_ctrl_softpedal: |
1203 |
|
decodedcontroller.type = leverage_ctrl_t::type_controlchange; |
1204 |
|
decodedcontroller.controller_number = 67; |
1205 |
|
break; |
1206 |
|
case _lev_ctrl_genpurpose5: |
1207 |
|
decodedcontroller.type = leverage_ctrl_t::type_controlchange; |
1208 |
|
decodedcontroller.controller_number = 80; |
1209 |
|
break; |
1210 |
|
case _lev_ctrl_genpurpose6: |
1211 |
|
decodedcontroller.type = leverage_ctrl_t::type_controlchange; |
1212 |
|
decodedcontroller.controller_number = 81; |
1213 |
|
break; |
1214 |
|
case _lev_ctrl_genpurpose7: |
1215 |
|
decodedcontroller.type = leverage_ctrl_t::type_controlchange; |
1216 |
|
decodedcontroller.controller_number = 82; |
1217 |
|
break; |
1218 |
|
case _lev_ctrl_genpurpose8: |
1219 |
|
decodedcontroller.type = leverage_ctrl_t::type_controlchange; |
1220 |
|
decodedcontroller.controller_number = 83; |
1221 |
|
break; |
1222 |
|
case _lev_ctrl_effect1depth: |
1223 |
|
decodedcontroller.type = leverage_ctrl_t::type_controlchange; |
1224 |
|
decodedcontroller.controller_number = 91; |
1225 |
|
break; |
1226 |
|
case _lev_ctrl_effect2depth: |
1227 |
|
decodedcontroller.type = leverage_ctrl_t::type_controlchange; |
1228 |
|
decodedcontroller.controller_number = 92; |
1229 |
|
break; |
1230 |
|
case _lev_ctrl_effect3depth: |
1231 |
|
decodedcontroller.type = leverage_ctrl_t::type_controlchange; |
1232 |
|
decodedcontroller.controller_number = 93; |
1233 |
|
break; |
1234 |
|
case _lev_ctrl_effect4depth: |
1235 |
|
decodedcontroller.type = leverage_ctrl_t::type_controlchange; |
1236 |
|
decodedcontroller.controller_number = 94; |
1237 |
|
break; |
1238 |
|
case _lev_ctrl_effect5depth: |
1239 |
|
decodedcontroller.type = leverage_ctrl_t::type_controlchange; |
1240 |
|
decodedcontroller.controller_number = 95; |
1241 |
|
break; |
1242 |
|
|
1243 |
|
// unknown controller type |
1244 |
|
default: |
1245 |
|
throw gig::Exception("Unknown leverage controller type."); |
1246 |
|
} |
1247 |
|
return decodedcontroller; |
1248 |
} |
} |
1249 |
|
|
1250 |
DimensionRegion::~DimensionRegion() { |
DimensionRegion::~DimensionRegion() { |
1270 |
* triggered to get the volume with which the sample should be played |
* triggered to get the volume with which the sample should be played |
1271 |
* back. |
* back. |
1272 |
* |
* |
1273 |
* @param MIDI velocity value of the triggered key (between 0 and 127) |
* @param MIDIKeyVelocity MIDI velocity value of the triggered key (between 0 and 127) |
1274 |
* @returns amplitude factor (between 0.0 and 1.0) |
* @returns amplitude factor (between 0.0 and 1.0) |
1275 |
*/ |
*/ |
1276 |
double DimensionRegion::GetVelocityAttenuation(uint8_t MIDIKeyVelocity) { |
double DimensionRegion::GetVelocityAttenuation(uint8_t MIDIKeyVelocity) { |
1277 |
return pVelocityAttenuationTable[MIDIKeyVelocity]; |
return pVelocityAttenuationTable[MIDIKeyVelocity]; |
1278 |
} |
} |
1279 |
|
|
1280 |
|
double* DimensionRegion::CreateVelocityTable(curve_type_t curveType, uint8_t depth, uint8_t scaling) { |
1281 |
|
|
1282 |
|
// line-segment approximations of the 15 velocity curves |
1283 |
|
|
1284 |
|
// linear |
1285 |
|
const int lin0[] = { 1, 1, 127, 127 }; |
1286 |
|
const int lin1[] = { 1, 21, 127, 127 }; |
1287 |
|
const int lin2[] = { 1, 45, 127, 127 }; |
1288 |
|
const int lin3[] = { 1, 74, 127, 127 }; |
1289 |
|
const int lin4[] = { 1, 127, 127, 127 }; |
1290 |
|
|
1291 |
|
// non-linear |
1292 |
|
const int non0[] = { 1, 4, 24, 5, 57, 17, 92, 57, 122, 127, 127, 127 }; |
1293 |
|
const int non1[] = { 1, 4, 46, 9, 93, 56, 118, 106, 123, 127, |
1294 |
|
127, 127 }; |
1295 |
|
const int non2[] = { 1, 4, 46, 9, 57, 20, 102, 107, 107, 127, |
1296 |
|
127, 127 }; |
1297 |
|
const int non3[] = { 1, 15, 10, 19, 67, 73, 80, 80, 90, 98, 98, 127, |
1298 |
|
127, 127 }; |
1299 |
|
const int non4[] = { 1, 25, 33, 57, 82, 81, 92, 127, 127, 127 }; |
1300 |
|
|
1301 |
|
// special |
1302 |
|
const int spe0[] = { 1, 2, 76, 10, 90, 15, 95, 20, 99, 28, 103, 44, |
1303 |
|
113, 127, 127, 127 }; |
1304 |
|
const int spe1[] = { 1, 2, 27, 5, 67, 18, 89, 29, 95, 35, 107, 67, |
1305 |
|
118, 127, 127, 127 }; |
1306 |
|
const int spe2[] = { 1, 1, 33, 1, 53, 5, 61, 13, 69, 32, 79, 74, |
1307 |
|
85, 90, 91, 127, 127, 127 }; |
1308 |
|
const int spe3[] = { 1, 32, 28, 35, 66, 48, 89, 59, 95, 65, 99, 73, |
1309 |
|
117, 127, 127, 127 }; |
1310 |
|
const int spe4[] = { 1, 4, 23, 5, 49, 13, 57, 17, 92, 57, 122, 127, |
1311 |
|
127, 127 }; |
1312 |
|
|
1313 |
|
const int* const curves[] = { non0, non1, non2, non3, non4, |
1314 |
|
lin0, lin1, lin2, lin3, lin4, |
1315 |
|
spe0, spe1, spe2, spe3, spe4 }; |
1316 |
|
|
1317 |
|
double* const table = new double[128]; |
1318 |
|
|
1319 |
|
const int* curve = curves[curveType * 5 + depth]; |
1320 |
|
const int s = scaling == 0 ? 20 : scaling; // 0 or 20 means no scaling |
1321 |
|
|
1322 |
|
table[0] = 0; |
1323 |
|
for (int x = 1 ; x < 128 ; x++) { |
1324 |
|
|
1325 |
|
if (x > curve[2]) curve += 2; |
1326 |
|
double y = curve[1] + (x - curve[0]) * |
1327 |
|
(double(curve[3] - curve[1]) / (curve[2] - curve[0])); |
1328 |
|
y = y / 127; |
1329 |
|
|
1330 |
|
// Scale up for s > 20, down for s < 20. When |
1331 |
|
// down-scaling, the curve still ends at 1.0. |
1332 |
|
if (s < 20 && y >= 0.5) |
1333 |
|
y = y / ((2 - 40.0 / s) * y + 40.0 / s - 1); |
1334 |
|
else |
1335 |
|
y = y * (s / 20.0); |
1336 |
|
if (y > 1) y = 1; |
1337 |
|
|
1338 |
|
table[x] = y; |
1339 |
|
} |
1340 |
|
return table; |
1341 |
|
} |
1342 |
|
|
1343 |
|
|
1344 |
// *************** Region *************** |
// *************** Region *************** |
1347 |
Region::Region(Instrument* pInstrument, RIFF::List* rgnList) : DLS::Region((DLS::Instrument*) pInstrument, rgnList) { |
Region::Region(Instrument* pInstrument, RIFF::List* rgnList) : DLS::Region((DLS::Instrument*) pInstrument, rgnList) { |
1348 |
// Initialization |
// Initialization |
1349 |
Dimensions = 0; |
Dimensions = 0; |
1350 |
for (int i = 0; i < 32; i++) { |
for (int i = 0; i < 256; i++) { |
1351 |
pDimensionRegions[i] = NULL; |
pDimensionRegions[i] = NULL; |
1352 |
} |
} |
1353 |
|
Layers = 1; |
1354 |
|
File* file = (File*) GetParent()->GetParent(); |
1355 |
|
int dimensionBits = (file->pVersion && file->pVersion->major == 3) ? 8 : 5; |
1356 |
|
|
1357 |
// Actual Loading |
// Actual Loading |
1358 |
|
|
1361 |
RIFF::Chunk* _3lnk = rgnList->GetSubChunk(CHUNK_ID_3LNK); |
RIFF::Chunk* _3lnk = rgnList->GetSubChunk(CHUNK_ID_3LNK); |
1362 |
if (_3lnk) { |
if (_3lnk) { |
1363 |
DimensionRegions = _3lnk->ReadUint32(); |
DimensionRegions = _3lnk->ReadUint32(); |
1364 |
for (int i = 0; i < 5; i++) { |
for (int i = 0; i < dimensionBits; i++) { |
1365 |
dimension_t dimension = static_cast<dimension_t>(_3lnk->ReadUint8()); |
dimension_t dimension = static_cast<dimension_t>(_3lnk->ReadUint8()); |
1366 |
uint8_t bits = _3lnk->ReadUint8(); |
uint8_t bits = _3lnk->ReadUint8(); |
1367 |
if (dimension == dimension_none) { // inactive dimension |
if (dimension == dimension_none) { // inactive dimension |
1377 |
pDimensionDefinitions[i].bits = bits; |
pDimensionDefinitions[i].bits = bits; |
1378 |
pDimensionDefinitions[i].zones = 0x01 << bits; // = pow(2,bits) |
pDimensionDefinitions[i].zones = 0x01 << bits; // = pow(2,bits) |
1379 |
pDimensionDefinitions[i].split_type = (dimension == dimension_layer || |
pDimensionDefinitions[i].split_type = (dimension == dimension_layer || |
1380 |
dimension == dimension_samplechannel) ? split_type_bit |
dimension == dimension_samplechannel || |
1381 |
: split_type_normal; |
dimension == dimension_releasetrigger) ? split_type_bit |
1382 |
|
: split_type_normal; |
1383 |
pDimensionDefinitions[i].ranges = NULL; // it's not possible to check velocity dimensions for custom defined ranges at this point |
pDimensionDefinitions[i].ranges = NULL; // it's not possible to check velocity dimensions for custom defined ranges at this point |
1384 |
pDimensionDefinitions[i].zone_size = |
pDimensionDefinitions[i].zone_size = |
1385 |
(pDimensionDefinitions[i].split_type == split_type_normal) ? 128 / pDimensionDefinitions[i].zones |
(pDimensionDefinitions[i].split_type == split_type_normal) ? 128 / pDimensionDefinitions[i].zones |
1386 |
: 0; |
: 0; |
1387 |
Dimensions++; |
Dimensions++; |
1388 |
|
|
1389 |
|
// if this is a layer dimension, remember the amount of layers |
1390 |
|
if (dimension == dimension_layer) Layers = pDimensionDefinitions[i].zones; |
1391 |
} |
} |
1392 |
_3lnk->SetPos(6, RIFF::stream_curpos); // jump forward to next dimension definition |
_3lnk->SetPos(6, RIFF::stream_curpos); // jump forward to next dimension definition |
1393 |
} |
} |
1404 |
else { // custom defined ranges |
else { // custom defined ranges |
1405 |
pDimDef->split_type = split_type_customvelocity; |
pDimDef->split_type = split_type_customvelocity; |
1406 |
pDimDef->ranges = new range_t[pDimDef->zones]; |
pDimDef->ranges = new range_t[pDimDef->zones]; |
1407 |
unsigned int bits[5] = {0,0,0,0,0}; |
uint8_t bits[8] = { 0 }; |
1408 |
int previousUpperLimit = -1; |
int previousUpperLimit = -1; |
1409 |
for (int velocityZone = 0; velocityZone < pDimDef->zones; velocityZone++) { |
for (int velocityZone = 0; velocityZone < pDimDef->zones; velocityZone++) { |
1410 |
bits[i] = velocityZone; |
bits[i] = velocityZone; |
1411 |
DimensionRegion* pDimRegion = GetDimensionRegionByBit(bits[4],bits[3],bits[2],bits[1],bits[0]); |
DimensionRegion* pDimRegion = GetDimensionRegionByBit(bits); |
1412 |
|
|
1413 |
pDimDef->ranges[velocityZone].low = previousUpperLimit + 1; |
pDimDef->ranges[velocityZone].low = previousUpperLimit + 1; |
1414 |
pDimDef->ranges[velocityZone].high = pDimRegion->VelocityUpperLimit; |
pDimDef->ranges[velocityZone].high = pDimRegion->VelocityUpperLimit; |
1422 |
} |
} |
1423 |
} |
} |
1424 |
|
|
1425 |
|
// jump to start of the wave pool indices (if not already there) |
1426 |
|
File* file = (File*) GetParent()->GetParent(); |
1427 |
|
if (file->pVersion && file->pVersion->major == 3) |
1428 |
|
_3lnk->SetPos(68); // version 3 has a different 3lnk structure |
1429 |
|
else |
1430 |
|
_3lnk->SetPos(44); |
1431 |
|
|
1432 |
// load sample references |
// load sample references |
|
_3lnk->SetPos(44); // jump to start of the wave pool indices (if not already there) |
|
1433 |
for (uint i = 0; i < DimensionRegions; i++) { |
for (uint i = 0; i < DimensionRegions; i++) { |
1434 |
uint32_t wavepoolindex = _3lnk->ReadUint32(); |
uint32_t wavepoolindex = _3lnk->ReadUint32(); |
1435 |
pDimensionRegions[i]->pSample = GetSampleFromWavePool(wavepoolindex); |
pDimensionRegions[i]->pSample = GetSampleFromWavePool(wavepoolindex); |
1458 |
for (uint i = 0; i < Dimensions; i++) { |
for (uint i = 0; i < Dimensions; i++) { |
1459 |
if (pDimensionDefinitions[i].ranges) delete[] pDimensionDefinitions[i].ranges; |
if (pDimensionDefinitions[i].ranges) delete[] pDimensionDefinitions[i].ranges; |
1460 |
} |
} |
1461 |
for (int i = 0; i < 32; i++) { |
for (int i = 0; i < 256; i++) { |
1462 |
if (pDimensionRegions[i]) delete pDimensionRegions[i]; |
if (pDimensionRegions[i]) delete pDimensionRegions[i]; |
1463 |
} |
} |
1464 |
} |
} |
1476 |
* left channel, 1 for right channel or 0 for layer 0, 1 for layer 1, |
* left channel, 1 for right channel or 0 for layer 0, 1 for layer 1, |
1477 |
* etc.). |
* etc.). |
1478 |
* |
* |
1479 |
* @param Dim4Val MIDI controller value (0-127) for dimension 4 |
* @param DimValues MIDI controller values (0-127) for dimension 0 to 7 |
|
* @param Dim3Val MIDI controller value (0-127) for dimension 3 |
|
|
* @param Dim2Val MIDI controller value (0-127) for dimension 2 |
|
|
* @param Dim1Val MIDI controller value (0-127) for dimension 1 |
|
|
* @param Dim0Val MIDI controller value (0-127) for dimension 0 |
|
1480 |
* @returns adress to the DimensionRegion for the given situation |
* @returns adress to the DimensionRegion for the given situation |
1481 |
* @see pDimensionDefinitions |
* @see pDimensionDefinitions |
1482 |
* @see Dimensions |
* @see Dimensions |
1483 |
*/ |
*/ |
1484 |
DimensionRegion* Region::GetDimensionRegionByValue(uint Dim4Val, uint Dim3Val, uint Dim2Val, uint Dim1Val, uint Dim0Val) { |
DimensionRegion* Region::GetDimensionRegionByValue(const uint DimValues[8]) { |
1485 |
unsigned int bits[5] = {Dim0Val,Dim1Val,Dim2Val,Dim3Val,Dim4Val}; |
uint8_t bits[8] = { 0 }; |
1486 |
for (uint i = 0; i < Dimensions; i++) { |
for (uint i = 0; i < Dimensions; i++) { |
1487 |
|
bits[i] = DimValues[i]; |
1488 |
switch (pDimensionDefinitions[i].split_type) { |
switch (pDimensionDefinitions[i].split_type) { |
1489 |
case split_type_normal: |
case split_type_normal: |
1490 |
bits[i] /= pDimensionDefinitions[i].zone_size; |
bits[i] /= pDimensionDefinitions[i].zone_size; |
1492 |
case split_type_customvelocity: |
case split_type_customvelocity: |
1493 |
bits[i] = VelocityTable[bits[i]]; |
bits[i] = VelocityTable[bits[i]]; |
1494 |
break; |
break; |
1495 |
// else the value is already the sought dimension bit number |
case split_type_bit: // the value is already the sought dimension bit number |
1496 |
|
const uint8_t limiter_mask = (0xff << pDimensionDefinitions[i].bits) ^ 0xff; |
1497 |
|
bits[i] = bits[i] & limiter_mask; // just make sure the value don't uses more bits than allowed |
1498 |
|
break; |
1499 |
} |
} |
1500 |
} |
} |
1501 |
return GetDimensionRegionByBit(bits[4],bits[3],bits[2],bits[1],bits[0]); |
return GetDimensionRegionByBit(bits); |
1502 |
} |
} |
1503 |
|
|
1504 |
/** |
/** |
1506 |
* numbers (zone index). You usually use <i>GetDimensionRegionByValue</i> |
* numbers (zone index). You usually use <i>GetDimensionRegionByValue</i> |
1507 |
* instead of calling this method directly! |
* instead of calling this method directly! |
1508 |
* |
* |
1509 |
* @param Dim4Bit Bit number for dimension 4 |
* @param DimBits Bit numbers for dimension 0 to 7 |
|
* @param Dim3Bit Bit number for dimension 3 |
|
|
* @param Dim2Bit Bit number for dimension 2 |
|
|
* @param Dim1Bit Bit number for dimension 1 |
|
|
* @param Dim0Bit Bit number for dimension 0 |
|
1510 |
* @returns adress to the DimensionRegion for the given dimension |
* @returns adress to the DimensionRegion for the given dimension |
1511 |
* bit numbers |
* bit numbers |
1512 |
* @see GetDimensionRegionByValue() |
* @see GetDimensionRegionByValue() |
1513 |
*/ |
*/ |
1514 |
DimensionRegion* Region::GetDimensionRegionByBit(uint8_t Dim4Bit, uint8_t Dim3Bit, uint8_t Dim2Bit, uint8_t Dim1Bit, uint8_t Dim0Bit) { |
DimensionRegion* Region::GetDimensionRegionByBit(const uint8_t DimBits[8]) { |
1515 |
return *(pDimensionRegions + ((((((((Dim4Bit << pDimensionDefinitions[3].bits) | Dim3Bit) |
return pDimensionRegions[((((((DimBits[7] << pDimensionDefinitions[6].bits | DimBits[6]) |
1516 |
<< pDimensionDefinitions[2].bits) | Dim2Bit) |
<< pDimensionDefinitions[5].bits | DimBits[5]) |
1517 |
<< pDimensionDefinitions[1].bits) | Dim1Bit) |
<< pDimensionDefinitions[4].bits | DimBits[4]) |
1518 |
<< pDimensionDefinitions[0].bits) | Dim0Bit) ); |
<< pDimensionDefinitions[3].bits | DimBits[3]) |
1519 |
|
<< pDimensionDefinitions[2].bits | DimBits[2]) |
1520 |
|
<< pDimensionDefinitions[1].bits | DimBits[1]) |
1521 |
|
<< pDimensionDefinitions[0].bits | DimBits[0]]; |
1522 |
} |
} |
1523 |
|
|
1524 |
/** |
/** |
1536 |
} |
} |
1537 |
|
|
1538 |
Sample* Region::GetSampleFromWavePool(unsigned int WavePoolTableIndex) { |
Sample* Region::GetSampleFromWavePool(unsigned int WavePoolTableIndex) { |
1539 |
|
if ((int32_t)WavePoolTableIndex == -1) return NULL; |
1540 |
File* file = (File*) GetParent()->GetParent(); |
File* file = (File*) GetParent()->GetParent(); |
1541 |
unsigned long soughtoffset = file->pWavePoolTable[WavePoolTableIndex]; |
unsigned long soughtoffset = file->pWavePoolTable[WavePoolTableIndex]; |
1542 |
Sample* sample = file->GetFirstSample(); |
Sample* sample = file->GetFirstSample(); |
1578 |
RIFF::List* lrgn = insList->GetSubList(LIST_TYPE_LRGN); |
RIFF::List* lrgn = insList->GetSubList(LIST_TYPE_LRGN); |
1579 |
if (!lrgn) throw gig::Exception("Mandatory chunks in <ins > chunk not found."); |
if (!lrgn) throw gig::Exception("Mandatory chunks in <ins > chunk not found."); |
1580 |
pRegions = new Region*[Regions]; |
pRegions = new Region*[Regions]; |
1581 |
|
for (uint i = 0; i < Regions; i++) pRegions[i] = NULL; |
1582 |
RIFF::List* rgn = lrgn->GetFirstSubList(); |
RIFF::List* rgn = lrgn->GetFirstSubList(); |
1583 |
unsigned int iRegion = 0; |
unsigned int iRegion = 0; |
1584 |
while (rgn) { |
while (rgn) { |
1602 |
if (pRegions) { |
if (pRegions) { |
1603 |
if (pRegions[i]) delete (pRegions[i]); |
if (pRegions[i]) delete (pRegions[i]); |
1604 |
} |
} |
|
delete[] pRegions; |
|
1605 |
} |
} |
1606 |
|
if (pRegions) delete[] pRegions; |
1607 |
} |
} |
1608 |
|
|
1609 |
/** |
/** |
1645 |
* @see GetFirstRegion() |
* @see GetFirstRegion() |
1646 |
*/ |
*/ |
1647 |
Region* Instrument::GetNextRegion() { |
Region* Instrument::GetNextRegion() { |
1648 |
if (RegionIndex < 0 || RegionIndex >= Regions) return NULL; |
if (RegionIndex < 0 || uint32_t(RegionIndex) >= Regions) return NULL; |
1649 |
return pRegions[RegionIndex++]; |
return pRegions[RegionIndex++]; |
1650 |
} |
} |
1651 |
|
|
1659 |
pInstruments = NULL; |
pInstruments = NULL; |
1660 |
} |
} |
1661 |
|
|
1662 |
|
File::~File() { |
1663 |
|
// free samples |
1664 |
|
if (pSamples) { |
1665 |
|
SamplesIterator = pSamples->begin(); |
1666 |
|
while (SamplesIterator != pSamples->end() ) { |
1667 |
|
delete (*SamplesIterator); |
1668 |
|
SamplesIterator++; |
1669 |
|
} |
1670 |
|
pSamples->clear(); |
1671 |
|
delete pSamples; |
1672 |
|
|
1673 |
|
} |
1674 |
|
// free instruments |
1675 |
|
if (pInstruments) { |
1676 |
|
InstrumentsIterator = pInstruments->begin(); |
1677 |
|
while (InstrumentsIterator != pInstruments->end() ) { |
1678 |
|
delete (*InstrumentsIterator); |
1679 |
|
InstrumentsIterator++; |
1680 |
|
} |
1681 |
|
pInstruments->clear(); |
1682 |
|
delete pInstruments; |
1683 |
|
} |
1684 |
|
} |
1685 |
|
|
1686 |
Sample* File::GetFirstSample() { |
Sample* File::GetFirstSample() { |
1687 |
if (!pSamples) LoadSamples(); |
if (!pSamples) LoadSamples(); |
1688 |
if (!pSamples) return NULL; |
if (!pSamples) return NULL; |