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, 2004 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, int truncatedBits) |
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 an extra step, and also why y is |
110 |
|
// initialized with a sum instead of a mean value. |
111 |
|
|
112 |
|
int y, dy, ddy; |
113 |
|
|
114 |
|
const int shift = 8 - truncatedBits; |
115 |
|
const int shift1 = shift + 1; |
116 |
|
|
117 |
|
#define GET_PARAMS(params) \ |
118 |
|
y = (get24(params) + get24((params) + 3)); \ |
119 |
|
dy = get24((params) + 6); \ |
120 |
|
ddy = get24((params) + 9) |
121 |
|
|
122 |
|
#define SKIP_ONE(x) \ |
123 |
|
ddy -= (x); \ |
124 |
|
dy -= ddy; \ |
125 |
|
y -= dy |
126 |
|
|
127 |
|
#define COPY_ONE(x) \ |
128 |
|
SKIP_ONE(x); \ |
129 |
|
*pDst = y >> shift1; \ |
130 |
|
pDst += dstStep |
131 |
|
|
132 |
|
switch (compressionmode) { |
133 |
|
case 2: // 24 bit uncompressed |
134 |
|
pSrc += currentframeoffset * 3; |
135 |
|
while (copysamples) { |
136 |
|
*pDst = get24(pSrc) >> shift; |
137 |
|
pDst += dstStep; |
138 |
|
pSrc += 3; |
139 |
|
copysamples--; |
140 |
|
} |
141 |
|
break; |
142 |
|
|
143 |
|
case 3: // 24 bit compressed to 16 bit |
144 |
|
GET_PARAMS(params); |
145 |
|
while (currentframeoffset) { |
146 |
|
SKIP_ONE(get16(pSrc)); |
147 |
|
pSrc += 2; |
148 |
|
currentframeoffset--; |
149 |
|
} |
150 |
|
while (copysamples) { |
151 |
|
COPY_ONE(get16(pSrc)); |
152 |
|
pSrc += 2; |
153 |
|
copysamples--; |
154 |
|
} |
155 |
|
break; |
156 |
|
|
157 |
|
case 4: // 24 bit compressed to 12 bit |
158 |
|
GET_PARAMS(params); |
159 |
|
while (currentframeoffset > 1) { |
160 |
|
SKIP_ONE(get12lo(pSrc)); |
161 |
|
SKIP_ONE(get12hi(pSrc)); |
162 |
|
pSrc += 3; |
163 |
|
currentframeoffset -= 2; |
164 |
|
} |
165 |
|
if (currentframeoffset) { |
166 |
|
SKIP_ONE(get12lo(pSrc)); |
167 |
|
currentframeoffset--; |
168 |
|
if (copysamples) { |
169 |
|
COPY_ONE(get12hi(pSrc)); |
170 |
|
pSrc += 3; |
171 |
|
copysamples--; |
172 |
|
} |
173 |
|
} |
174 |
|
while (copysamples > 1) { |
175 |
|
COPY_ONE(get12lo(pSrc)); |
176 |
|
COPY_ONE(get12hi(pSrc)); |
177 |
|
pSrc += 3; |
178 |
|
copysamples -= 2; |
179 |
|
} |
180 |
|
if (copysamples) { |
181 |
|
COPY_ONE(get12lo(pSrc)); |
182 |
|
} |
183 |
|
break; |
184 |
|
|
185 |
|
case 5: // 24 bit compressed to 8 bit |
186 |
|
GET_PARAMS(params); |
187 |
|
while (currentframeoffset) { |
188 |
|
SKIP_ONE(int8_t(*pSrc++)); |
189 |
|
currentframeoffset--; |
190 |
|
} |
191 |
|
while (copysamples) { |
192 |
|
COPY_ONE(int8_t(*pSrc++)); |
193 |
|
copysamples--; |
194 |
|
} |
195 |
|
break; |
196 |
|
} |
197 |
|
} |
198 |
|
|
199 |
|
const int bytesPerFrame[] = { 4096, 2052, 768, 524, 396, 268 }; |
200 |
|
const int bytesPerFrameNoHdr[] = { 4096, 2048, 768, 512, 384, 256 }; |
201 |
|
const int headerSize[] = { 0, 4, 0, 12, 12, 12 }; |
202 |
|
const int bitsPerSample[] = { 16, 8, 24, 16, 12, 8 }; |
203 |
|
} |
204 |
|
|
205 |
|
|
206 |
// *************** Sample *************** |
// *************** Sample *************** |
207 |
// * |
// * |
208 |
|
|
209 |
unsigned int Sample::Instances = 0; |
unsigned int Sample::Instances = 0; |
210 |
void* Sample::pDecompressionBuffer = NULL; |
buffer_t Sample::InternalDecompressionBuffer; |
|
unsigned long Sample::DecompressionBufferSize = 0; |
|
211 |
|
|
212 |
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) { |
213 |
Instances++; |
Instances++; |
226 |
smpl->Read(&SMPTEFormat, 1, 4); |
smpl->Read(&SMPTEFormat, 1, 4); |
227 |
SMPTEOffset = smpl->ReadInt32(); |
SMPTEOffset = smpl->ReadInt32(); |
228 |
Loops = smpl->ReadInt32(); |
Loops = smpl->ReadInt32(); |
229 |
uint32_t manufByt = smpl->ReadInt32(); |
smpl->ReadInt32(); // manufByt |
230 |
LoopID = smpl->ReadInt32(); |
LoopID = smpl->ReadInt32(); |
231 |
smpl->Read(&LoopType, 1, 4); |
smpl->Read(&LoopType, 1, 4); |
232 |
LoopStart = smpl->ReadInt32(); |
LoopStart = smpl->ReadInt32(); |
240 |
RAMCache.pStart = NULL; |
RAMCache.pStart = NULL; |
241 |
RAMCache.NullExtensionSize = 0; |
RAMCache.NullExtensionSize = 0; |
242 |
|
|
243 |
Compressed = (waveList->GetSubChunk(CHUNK_ID_EWAV)); |
if (BitDepth > 24) throw gig::Exception("Only samples up to 24 bit supported"); |
244 |
|
|
245 |
|
RIFF::Chunk* ewav = waveList->GetSubChunk(CHUNK_ID_EWAV); |
246 |
|
Compressed = ewav; |
247 |
|
Dithered = false; |
248 |
|
TruncatedBits = 0; |
249 |
if (Compressed) { |
if (Compressed) { |
250 |
ScanCompressedSample(); |
uint32_t version = ewav->ReadInt32(); |
251 |
if (!pDecompressionBuffer) { |
if (version == 3 && BitDepth == 24) { |
252 |
pDecompressionBuffer = new int8_t[INITIAL_SAMPLE_BUFFER_SIZE]; |
Dithered = ewav->ReadInt32(); |
253 |
DecompressionBufferSize = INITIAL_SAMPLE_BUFFER_SIZE; |
ewav->SetPos(Channels == 2 ? 84 : 64); |
254 |
|
TruncatedBits = ewav->ReadInt32(); |
255 |
} |
} |
256 |
|
ScanCompressedSample(); |
257 |
|
} |
258 |
|
|
259 |
|
// we use a buffer for decompression and for truncating 24 bit samples to 16 bit |
260 |
|
if ((Compressed || BitDepth == 24) && !InternalDecompressionBuffer.Size) { |
261 |
|
InternalDecompressionBuffer.pStart = new unsigned char[INITIAL_SAMPLE_BUFFER_SIZE]; |
262 |
|
InternalDecompressionBuffer.Size = INITIAL_SAMPLE_BUFFER_SIZE; |
263 |
} |
} |
264 |
FrameOffset = 0; // just for streaming compressed samples |
FrameOffset = 0; // just for streaming compressed samples |
265 |
|
|
266 |
LoopSize = LoopEnd - LoopStart; |
LoopSize = LoopEnd - LoopStart; |
267 |
} |
} |
272 |
this->SamplesTotal = 0; |
this->SamplesTotal = 0; |
273 |
std::list<unsigned long> frameOffsets; |
std::list<unsigned long> frameOffsets; |
274 |
|
|
275 |
|
SamplesPerFrame = BitDepth == 24 ? 256 : 2048; |
276 |
|
WorstCaseFrameSize = SamplesPerFrame * FrameSize + Channels; // +Channels for compression flag |
277 |
|
|
278 |
// Scanning |
// Scanning |
279 |
pCkData->SetPos(0); |
pCkData->SetPos(0); |
280 |
while (pCkData->GetState() == RIFF::stream_ready) { |
if (Channels == 2) { // Stereo |
281 |
frameOffsets.push_back(pCkData->GetPos()); |
for (int i = 0 ; ; i++) { |
282 |
int16_t compressionmode = pCkData->ReadInt16(); |
// for 24 bit samples every 8:th frame offset is |
283 |
this->SamplesTotal += 2048; |
// stored, to save some memory |
284 |
switch (compressionmode) { |
if (BitDepth != 24 || (i & 7) == 0) frameOffsets.push_back(pCkData->GetPos()); |
285 |
case 1: // left channel compressed |
|
286 |
case 256: // right channel compressed |
const int mode_l = pCkData->ReadUint8(); |
287 |
pCkData->SetPos(6148, RIFF::stream_curpos); |
const int mode_r = pCkData->ReadUint8(); |
288 |
|
if (mode_l > 5 || mode_r > 5) throw gig::Exception("Unknown compression mode"); |
289 |
|
const unsigned long frameSize = bytesPerFrame[mode_l] + bytesPerFrame[mode_r]; |
290 |
|
|
291 |
|
if (pCkData->RemainingBytes() <= frameSize) { |
292 |
|
SamplesInLastFrame = |
293 |
|
((pCkData->RemainingBytes() - headerSize[mode_l] - headerSize[mode_r]) << 3) / |
294 |
|
(bitsPerSample[mode_l] + bitsPerSample[mode_r]); |
295 |
|
SamplesTotal += SamplesInLastFrame; |
296 |
break; |
break; |
297 |
case 257: // both channels compressed |
} |
298 |
pCkData->SetPos(4104, RIFF::stream_curpos); |
SamplesTotal += SamplesPerFrame; |
299 |
|
pCkData->SetPos(frameSize, RIFF::stream_curpos); |
300 |
|
} |
301 |
|
} |
302 |
|
else { // Mono |
303 |
|
for (int i = 0 ; ; i++) { |
304 |
|
if (BitDepth != 24 || (i & 7) == 0) frameOffsets.push_back(pCkData->GetPos()); |
305 |
|
|
306 |
|
const int mode = pCkData->ReadUint8(); |
307 |
|
if (mode > 5) throw gig::Exception("Unknown compression mode"); |
308 |
|
const unsigned long frameSize = bytesPerFrame[mode]; |
309 |
|
|
310 |
|
if (pCkData->RemainingBytes() <= frameSize) { |
311 |
|
SamplesInLastFrame = |
312 |
|
((pCkData->RemainingBytes() - headerSize[mode]) << 3) / bitsPerSample[mode]; |
313 |
|
SamplesTotal += SamplesInLastFrame; |
314 |
break; |
break; |
315 |
default: // both channels uncompressed |
} |
316 |
pCkData->SetPos(8192, RIFF::stream_curpos); |
SamplesTotal += SamplesPerFrame; |
317 |
|
pCkData->SetPos(frameSize, RIFF::stream_curpos); |
318 |
} |
} |
319 |
} |
} |
320 |
pCkData->SetPos(0); |
pCkData->SetPos(0); |
321 |
|
|
|
//FIXME: only seen compressed samples with 16 bit stereo so far |
|
|
this->FrameSize = 4; |
|
|
this->BitDepth = 16; |
|
|
|
|
322 |
// 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) |
323 |
if (FrameTable) delete[] FrameTable; |
if (FrameTable) delete[] FrameTable; |
324 |
FrameTable = new unsigned long[frameOffsets.size()]; |
FrameTable = new unsigned long[frameOffsets.size()]; |
354 |
* that will be returned to determine the actual cached samples, but note |
* that will be returned to determine the actual cached samples, but note |
355 |
* 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 |
356 |
* samples by dividing it by the frame size of the sample: |
* samples by dividing it by the frame size of the sample: |
357 |
* |
* @code |
358 |
* buffer_t buf = pSample->LoadSampleData(acquired_samples); |
* buffer_t buf = pSample->LoadSampleData(acquired_samples); |
359 |
* long cachedsamples = buf.Size / pSample->FrameSize; |
* long cachedsamples = buf.Size / pSample->FrameSize; |
360 |
|
* @endcode |
361 |
* |
* |
362 |
* @param SampleCount - number of sample points to load into RAM |
* @param SampleCount - number of sample points to load into RAM |
363 |
* @returns buffer_t structure with start address and size of |
* @returns buffer_t structure with start address and size of |
403 |
* that will be returned to determine the actual cached samples, but note |
* that will be returned to determine the actual cached samples, but note |
404 |
* 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 |
405 |
* samples by dividing it by the frame size of the sample: |
* samples by dividing it by the frame size of the sample: |
406 |
* |
* @code |
407 |
* buffer_t buf = pSample->LoadSampleDataWithNullSamplesExtension(acquired_samples, null_samples); |
* buffer_t buf = pSample->LoadSampleDataWithNullSamplesExtension(acquired_samples, null_samples); |
408 |
* long cachedsamples = buf.Size / pSample->FrameSize; |
* long cachedsamples = buf.Size / pSample->FrameSize; |
409 |
* |
* @endcode |
410 |
* The method will add \a NullSamplesCount silence samples past the |
* The method will add \a NullSamplesCount silence samples past the |
411 |
* official buffer end (this won't affect the 'Size' member of the |
* official buffer end (this won't affect the 'Size' member of the |
412 |
* buffer_t structure, that means 'Size' always reflects the size of the |
* buffer_t structure, that means 'Size' always reflects the size of the |
537 |
* 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. |
538 |
* You have to allocate and initialize the playback_state_t structure by |
* You have to allocate and initialize the playback_state_t structure by |
539 |
* yourself before you use it to stream a sample: |
* yourself before you use it to stream a sample: |
540 |
* |
* @code |
541 |
* <i> |
* gig::playback_state_t playbackstate; |
542 |
* gig::playback_state_t playbackstate; <br> |
* playbackstate.position = 0; |
543 |
* playbackstate.position = 0; <br> |
* playbackstate.reverse = false; |
544 |
* playbackstate.reverse = false; <br> |
* playbackstate.loop_cycles_left = pSample->LoopPlayCount; |
545 |
* playbackstate.loop_cycles_left = pSample->LoopPlayCount; <br> |
* @endcode |
|
* </i> |
|
|
* |
|
546 |
* 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 |
547 |
* 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. |
548 |
* The method already handles such cases by itself. |
* The method already handles such cases by itself. |
549 |
* |
* |
550 |
|
* <b>Caution:</b> If you are using more than one streaming thread, you |
551 |
|
* have to use an external decompression buffer for <b>EACH</b> |
552 |
|
* streaming thread to avoid race conditions and crashes! |
553 |
|
* |
554 |
* @param pBuffer destination buffer |
* @param pBuffer destination buffer |
555 |
* @param SampleCount number of sample points to read |
* @param SampleCount number of sample points to read |
556 |
* @param pPlaybackState will be used to store and reload the playback |
* @param pPlaybackState will be used to store and reload the playback |
557 |
* state for the next ReadAndLoop() call |
* state for the next ReadAndLoop() call |
558 |
|
* @param pExternalDecompressionBuffer (optional) external buffer to use for decompression |
559 |
* @returns number of successfully read sample points |
* @returns number of successfully read sample points |
560 |
|
* @see CreateDecompressionBuffer() |
561 |
*/ |
*/ |
562 |
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) { |
563 |
unsigned long samplestoread = SampleCount, totalreadsamples = 0, readsamples, samplestoloopend; |
unsigned long samplestoread = SampleCount, totalreadsamples = 0, readsamples, samplestoloopend; |
564 |
uint8_t* pDst = (uint8_t*) pBuffer; |
uint8_t* pDst = (uint8_t*) pBuffer; |
565 |
|
|
577 |
if (!pPlaybackState->reverse) { // forward playback |
if (!pPlaybackState->reverse) { // forward playback |
578 |
do { |
do { |
579 |
samplestoloopend = this->LoopEnd - GetPos(); |
samplestoloopend = this->LoopEnd - GetPos(); |
580 |
readsamples = Read(&pDst[totalreadsamples * this->FrameSize], Min(samplestoread, samplestoloopend)); |
readsamples = Read(&pDst[totalreadsamples * this->FrameSize], Min(samplestoread, samplestoloopend), pExternalDecompressionBuffer); |
581 |
samplestoread -= readsamples; |
samplestoread -= readsamples; |
582 |
totalreadsamples += readsamples; |
totalreadsamples += readsamples; |
583 |
if (readsamples == samplestoloopend) { |
if (readsamples == samplestoloopend) { |
603 |
|
|
604 |
// read samples for backward playback |
// read samples for backward playback |
605 |
do { |
do { |
606 |
readsamples = Read(&pDst[totalreadsamples * this->FrameSize], samplestoreadinloop); |
readsamples = Read(&pDst[totalreadsamples * this->FrameSize], samplestoreadinloop, pExternalDecompressionBuffer); |
607 |
samplestoreadinloop -= readsamples; |
samplestoreadinloop -= readsamples; |
608 |
samplestoread -= readsamples; |
samplestoread -= readsamples; |
609 |
totalreadsamples += readsamples; |
totalreadsamples += readsamples; |
627 |
// forward playback (not entered the loop yet) |
// forward playback (not entered the loop yet) |
628 |
if (!pPlaybackState->reverse) do { |
if (!pPlaybackState->reverse) do { |
629 |
samplestoloopend = this->LoopEnd - GetPos(); |
samplestoloopend = this->LoopEnd - GetPos(); |
630 |
readsamples = Read(&pDst[totalreadsamples * this->FrameSize], Min(samplestoread, samplestoloopend)); |
readsamples = Read(&pDst[totalreadsamples * this->FrameSize], Min(samplestoread, samplestoloopend), pExternalDecompressionBuffer); |
631 |
samplestoread -= readsamples; |
samplestoread -= readsamples; |
632 |
totalreadsamples += readsamples; |
totalreadsamples += readsamples; |
633 |
if (readsamples == samplestoloopend) { |
if (readsamples == samplestoloopend) { |
657 |
// 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 |
658 |
if (this->LoopPlayCount && !pPlaybackState->loop_cycles_left) break; |
if (this->LoopPlayCount && !pPlaybackState->loop_cycles_left) break; |
659 |
samplestoloopend = this->LoopEnd - GetPos(); |
samplestoloopend = this->LoopEnd - GetPos(); |
660 |
readsamples = Read(&pDst[totalreadsamples * this->FrameSize], Min(samplestoreadinloop, samplestoloopend)); |
readsamples = Read(&pDst[totalreadsamples * this->FrameSize], Min(samplestoreadinloop, samplestoloopend), pExternalDecompressionBuffer); |
661 |
samplestoreadinloop -= readsamples; |
samplestoreadinloop -= readsamples; |
662 |
samplestoread -= readsamples; |
samplestoread -= readsamples; |
663 |
totalreadsamples += readsamples; |
totalreadsamples += readsamples; |
679 |
// 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 |
680 |
if (this->LoopPlayCount && !pPlaybackState->loop_cycles_left) break; |
if (this->LoopPlayCount && !pPlaybackState->loop_cycles_left) break; |
681 |
samplestoloopend = this->LoopEnd - GetPos(); |
samplestoloopend = this->LoopEnd - GetPos(); |
682 |
readsamples = Read(&pDst[totalreadsamples * this->FrameSize], Min(samplestoread, samplestoloopend)); |
readsamples = Read(&pDst[totalreadsamples * this->FrameSize], Min(samplestoread, samplestoloopend), pExternalDecompressionBuffer); |
683 |
samplestoread -= readsamples; |
samplestoread -= readsamples; |
684 |
totalreadsamples += readsamples; |
totalreadsamples += readsamples; |
685 |
if (readsamples == samplestoloopend) { |
if (readsamples == samplestoloopend) { |
694 |
|
|
695 |
// read on without looping |
// read on without looping |
696 |
if (samplestoread) do { |
if (samplestoread) do { |
697 |
readsamples = Read(&pDst[totalreadsamples * this->FrameSize], samplestoread); |
readsamples = Read(&pDst[totalreadsamples * this->FrameSize], samplestoread, pExternalDecompressionBuffer); |
698 |
samplestoread -= readsamples; |
samplestoread -= readsamples; |
699 |
totalreadsamples += readsamples; |
totalreadsamples += readsamples; |
700 |
} while (readsamples && samplestoread); |
} while (readsamples && samplestoread); |
713 |
* 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, |
714 |
* thus for disk streaming. |
* thus for disk streaming. |
715 |
* |
* |
716 |
|
* <b>Caution:</b> If you are using more than one streaming thread, you |
717 |
|
* have to use an external decompression buffer for <b>EACH</b> |
718 |
|
* streaming thread to avoid race conditions and crashes! |
719 |
|
* |
720 |
* @param pBuffer destination buffer |
* @param pBuffer destination buffer |
721 |
* @param SampleCount number of sample points to read |
* @param SampleCount number of sample points to read |
722 |
|
* @param pExternalDecompressionBuffer (optional) external buffer to use for decompression |
723 |
* @returns number of successfully read sample points |
* @returns number of successfully read sample points |
724 |
* @see SetPos() |
* @see SetPos(), CreateDecompressionBuffer() |
725 |
*/ |
*/ |
726 |
unsigned long Sample::Read(void* pBuffer, unsigned long SampleCount) { |
unsigned long Sample::Read(void* pBuffer, unsigned long SampleCount, buffer_t* pExternalDecompressionBuffer) { |
727 |
if (SampleCount == 0) return 0; |
if (SampleCount == 0) return 0; |
728 |
if (!Compressed) return pCkData->Read(pBuffer, SampleCount, FrameSize); //FIXME: channel inversion due to endian correction? |
if (!Compressed) { |
729 |
else { //FIXME: no support for mono compressed samples yet, are there any? |
if (BitDepth == 24) { |
730 |
|
// 24 bit sample. For now just truncate to 16 bit. |
731 |
|
unsigned char* pSrc = (unsigned char*) ((pExternalDecompressionBuffer) ? pExternalDecompressionBuffer->pStart : this->InternalDecompressionBuffer.pStart); |
732 |
|
int16_t* pDst = static_cast<int16_t*>(pBuffer); |
733 |
|
if (Channels == 2) { // Stereo |
734 |
|
unsigned long readBytes = pCkData->Read(pSrc, SampleCount * 6, 1); |
735 |
|
pSrc++; |
736 |
|
for (unsigned long i = readBytes ; i > 0 ; i -= 3) { |
737 |
|
*pDst++ = get16(pSrc); |
738 |
|
pSrc += 3; |
739 |
|
} |
740 |
|
return (pDst - static_cast<int16_t*>(pBuffer)) >> 1; |
741 |
|
} |
742 |
|
else { // Mono |
743 |
|
unsigned long readBytes = pCkData->Read(pSrc, SampleCount * 3, 1); |
744 |
|
pSrc++; |
745 |
|
for (unsigned long i = readBytes ; i > 0 ; i -= 3) { |
746 |
|
*pDst++ = get16(pSrc); |
747 |
|
pSrc += 3; |
748 |
|
} |
749 |
|
return pDst - static_cast<int16_t*>(pBuffer); |
750 |
|
} |
751 |
|
} |
752 |
|
else { // 16 bit |
753 |
|
// (pCkData->Read does endian correction) |
754 |
|
return Channels == 2 ? pCkData->Read(pBuffer, SampleCount << 1, 2) >> 1 |
755 |
|
: pCkData->Read(pBuffer, SampleCount, 2); |
756 |
|
} |
757 |
|
} |
758 |
|
else { |
759 |
if (this->SamplePos >= this->SamplesTotal) return 0; |
if (this->SamplePos >= this->SamplesTotal) return 0; |
760 |
//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 |
761 |
// 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 |
|
762 |
remainingbytes = 0, // remaining bytes in the local buffer |
remainingbytes = 0, // remaining bytes in the local buffer |
763 |
remainingsamples = SampleCount, |
remainingsamples = SampleCount, |
764 |
copysamples; |
copysamples, skipsamples, |
765 |
int currentframeoffset = this->FrameOffset; // offset in current sample frame since last Read() |
currentframeoffset = this->FrameOffset; // offset in current sample frame since last Read() |
766 |
this->FrameOffset = 0; |
this->FrameOffset = 0; |
767 |
|
|
768 |
if (assumedsize > this->DecompressionBufferSize) { |
buffer_t* pDecompressionBuffer = (pExternalDecompressionBuffer) ? pExternalDecompressionBuffer : &InternalDecompressionBuffer; |
769 |
// local buffer reallocation - hope this won't happen |
|
770 |
if (this->pDecompressionBuffer) delete[] (int8_t*) this->pDecompressionBuffer; |
// if decompression buffer too small, then reduce amount of samples to read |
771 |
this->pDecompressionBuffer = new int8_t[assumedsize << 1]; // double of current needed size |
if (pDecompressionBuffer->Size < assumedsize) { |
772 |
this->DecompressionBufferSize = assumedsize; |
std::cerr << "gig::Read(): WARNING - decompression buffer size too small!" << std::endl; |
773 |
|
SampleCount = WorstCaseMaxSamples(pDecompressionBuffer); |
774 |
|
remainingsamples = SampleCount; |
775 |
|
assumedsize = GuessSize(SampleCount); |
776 |
} |
} |
777 |
|
|
778 |
int16_t compressionmode, left, dleft, right, dright; |
unsigned char* pSrc = (unsigned char*) pDecompressionBuffer->pStart; |
779 |
int8_t* pSrc = (int8_t*) this->pDecompressionBuffer; |
int16_t* pDst = static_cast<int16_t*>(pBuffer); |
|
int16_t* pDst = (int16_t*) pBuffer; |
|
780 |
remainingbytes = pCkData->Read(pSrc, assumedsize, 1); |
remainingbytes = pCkData->Read(pSrc, assumedsize, 1); |
781 |
|
|
782 |
while (remainingsamples) { |
while (remainingsamples && remainingbytes) { |
783 |
|
unsigned long framesamples = SamplesPerFrame; |
784 |
// reload from disk to local buffer if needed |
unsigned long framebytes, rightChannelOffset = 0, nextFrameOffset; |
785 |
if (remainingbytes < 8194) { |
|
786 |
if (pCkData->GetState() != RIFF::stream_ready) { |
int mode_l = *pSrc++, mode_r = 0; |
787 |
this->SamplePos = this->SamplesTotal; |
|
788 |
return (SampleCount - remainingsamples); |
if (Channels == 2) { |
789 |
|
mode_r = *pSrc++; |
790 |
|
framebytes = bytesPerFrame[mode_l] + bytesPerFrame[mode_r] + 2; |
791 |
|
rightChannelOffset = bytesPerFrameNoHdr[mode_l]; |
792 |
|
nextFrameOffset = rightChannelOffset + bytesPerFrameNoHdr[mode_r]; |
793 |
|
if (remainingbytes < framebytes) { // last frame in sample |
794 |
|
framesamples = SamplesInLastFrame; |
795 |
|
if (mode_l == 4 && (framesamples & 1)) { |
796 |
|
rightChannelOffset = ((framesamples + 1) * bitsPerSample[mode_l]) >> 3; |
797 |
|
} |
798 |
|
else { |
799 |
|
rightChannelOffset = (framesamples * bitsPerSample[mode_l]) >> 3; |
800 |
|
} |
801 |
|
} |
802 |
|
} |
803 |
|
else { |
804 |
|
framebytes = bytesPerFrame[mode_l] + 1; |
805 |
|
nextFrameOffset = bytesPerFrameNoHdr[mode_l]; |
806 |
|
if (remainingbytes < framebytes) { |
807 |
|
framesamples = SamplesInLastFrame; |
808 |
} |
} |
|
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; |
|
809 |
} |
} |
810 |
|
|
811 |
// determine how many samples in this frame to skip and read |
// determine how many samples in this frame to skip and read |
812 |
if (remainingsamples >= 2048) { |
if (currentframeoffset + remainingsamples >= framesamples) { |
813 |
copysamples = 2048 - currentframeoffset; |
if (currentframeoffset <= framesamples) { |
814 |
remainingsamples -= copysamples; |
copysamples = framesamples - currentframeoffset; |
815 |
|
skipsamples = currentframeoffset; |
816 |
|
} |
817 |
|
else { |
818 |
|
copysamples = 0; |
819 |
|
skipsamples = framesamples; |
820 |
|
} |
821 |
} |
} |
822 |
else { |
else { |
823 |
|
// This frame has enough data for pBuffer, but not |
824 |
|
// all of the frame is needed. Set file position |
825 |
|
// to start of this frame for next call to Read. |
826 |
copysamples = remainingsamples; |
copysamples = remainingsamples; |
827 |
if (currentframeoffset + copysamples > 2048) { |
skipsamples = currentframeoffset; |
828 |
copysamples = 2048 - currentframeoffset; |
pCkData->SetPos(remainingbytes, RIFF::stream_backward); |
829 |
remainingsamples -= copysamples; |
this->FrameOffset = currentframeoffset + copysamples; |
830 |
} |
} |
831 |
else { |
remainingsamples -= copysamples; |
832 |
|
|
833 |
|
if (remainingbytes > framebytes) { |
834 |
|
remainingbytes -= framebytes; |
835 |
|
if (remainingsamples == 0 && |
836 |
|
currentframeoffset + copysamples == framesamples) { |
837 |
|
// This frame has enough data for pBuffer, and |
838 |
|
// all of the frame is needed. Set file |
839 |
|
// position to start of next frame for next |
840 |
|
// call to Read. FrameOffset is 0. |
841 |
pCkData->SetPos(remainingbytes, RIFF::stream_backward); |
pCkData->SetPos(remainingbytes, RIFF::stream_backward); |
|
remainingsamples = 0; |
|
|
this->FrameOffset = currentframeoffset + copysamples; |
|
842 |
} |
} |
843 |
} |
} |
844 |
|
else remainingbytes = 0; |
845 |
|
|
846 |
// decompress and copy current frame from local buffer to destination buffer |
currentframeoffset -= skipsamples; |
847 |
compressionmode = *(int16_t*)pSrc; pSrc+=2; |
|
848 |
switch (compressionmode) { |
if (copysamples == 0) { |
849 |
case 1: // left channel compressed |
// skip this frame |
850 |
remainingbytes -= 6150; // (left 8 bit, right 16 bit, +6 byte header) |
pSrc += framebytes - Channels; |
851 |
if (!remainingsamples && copysamples == 2048) |
} |
852 |
pCkData->SetPos(remainingbytes, RIFF::stream_backward); |
else { |
853 |
|
const unsigned char* const param_l = pSrc; |
854 |
left = *(int16_t*)pSrc; pSrc+=2; |
if (BitDepth == 24) { |
855 |
dleft = *(int16_t*)pSrc; pSrc+=2; |
if (mode_l != 2) pSrc += 12; |
856 |
while (currentframeoffset) { |
|
857 |
dleft -= *pSrc; |
if (Channels == 2) { // Stereo |
858 |
left -= dleft; |
const unsigned char* const param_r = pSrc; |
859 |
pSrc+=3; // 8 bit left channel, skip uncompressed right channel (16 bit) |
if (mode_r != 2) pSrc += 12; |
860 |
currentframeoffset--; |
|
861 |
} |
Decompress24(mode_l, param_l, 2, pSrc, pDst, |
862 |
while (copysamples) { |
skipsamples, copysamples, TruncatedBits); |
863 |
dleft -= *pSrc; pSrc++; |
Decompress24(mode_r, param_r, 2, pSrc + rightChannelOffset, pDst + 1, |
864 |
left -= dleft; |
skipsamples, copysamples, TruncatedBits); |
865 |
*pDst = left; pDst++; |
pDst += copysamples << 1; |
|
*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 |
|
866 |
} |
} |
867 |
while (copysamples) { |
else { // Mono |
868 |
*pDst = *(int16_t*)pSrc; pDst++; pSrc+=2; |
Decompress24(mode_l, param_l, 1, pSrc, pDst, |
869 |
dright -= *pSrc; pSrc++; |
skipsamples, copysamples, TruncatedBits); |
870 |
right -= dright; |
pDst += copysamples; |
|
*pDst = right; pDst++; |
|
|
copysamples--; |
|
871 |
} |
} |
872 |
break; |
} |
873 |
case 257: // both channels compressed |
else { // 16 bit |
874 |
remainingbytes -= 4106; // (left 8 bit, right 8 bit, +10 byte header) |
if (mode_l) pSrc += 4; |
875 |
if (!remainingsamples && copysamples == 2048) |
|
876 |
pCkData->SetPos(remainingbytes, RIFF::stream_backward); |
int step; |
877 |
|
if (Channels == 2) { // Stereo |
878 |
left = *(int16_t*)pSrc; pSrc+=2; |
const unsigned char* const param_r = pSrc; |
879 |
dleft = *(int16_t*)pSrc; pSrc+=2; |
if (mode_r) pSrc += 4; |
880 |
right = *(int16_t*)pSrc; pSrc+=2; |
|
881 |
dright = *(int16_t*)pSrc; pSrc+=2; |
step = (2 - mode_l) + (2 - mode_r); |
882 |
while (currentframeoffset) { |
Decompress16(mode_l, param_l, step, 2, pSrc, pDst, skipsamples, copysamples); |
883 |
dleft -= *pSrc; pSrc++; |
Decompress16(mode_r, param_r, step, 2, pSrc + (2 - mode_l), pDst + 1, |
884 |
left -= dleft; |
skipsamples, copysamples); |
885 |
dright -= *pSrc; pSrc++; |
pDst += copysamples << 1; |
|
right -= dright; |
|
|
currentframeoffset--; |
|
886 |
} |
} |
887 |
while (copysamples) { |
else { // Mono |
888 |
dleft -= *pSrc; pSrc++; |
step = 2 - mode_l; |
889 |
left -= dleft; |
Decompress16(mode_l, param_l, step, 1, pSrc, pDst, skipsamples, copysamples); |
890 |
dright -= *pSrc; pSrc++; |
pDst += copysamples; |
|
right -= dright; |
|
|
*pDst = left; pDst++; |
|
|
*pDst = right; pDst++; |
|
|
copysamples--; |
|
891 |
} |
} |
892 |
break; |
} |
893 |
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; |
|
894 |
} |
} |
895 |
} |
|
896 |
|
// reload from disk to local buffer if needed |
897 |
|
if (remainingsamples && remainingbytes < WorstCaseFrameSize && pCkData->GetState() == RIFF::stream_ready) { |
898 |
|
assumedsize = GuessSize(remainingsamples); |
899 |
|
pCkData->SetPos(remainingbytes, RIFF::stream_backward); |
900 |
|
if (pCkData->RemainingBytes() < assumedsize) assumedsize = pCkData->RemainingBytes(); |
901 |
|
remainingbytes = pCkData->Read(pDecompressionBuffer->pStart, assumedsize, 1); |
902 |
|
pSrc = (unsigned char*) pDecompressionBuffer->pStart; |
903 |
|
} |
904 |
|
} // while |
905 |
|
|
906 |
this->SamplePos += (SampleCount - remainingsamples); |
this->SamplePos += (SampleCount - remainingsamples); |
907 |
if (this->SamplePos > this->SamplesTotal) this->SamplePos = this->SamplesTotal; |
if (this->SamplePos > this->SamplesTotal) this->SamplePos = this->SamplesTotal; |
908 |
return (SampleCount - remainingsamples); |
return (SampleCount - remainingsamples); |
909 |
} |
} |
910 |
} |
} |
911 |
|
|
912 |
|
/** |
913 |
|
* Allocates a decompression buffer for streaming (compressed) samples |
914 |
|
* with Sample::Read(). If you are using more than one streaming thread |
915 |
|
* in your application you <b>HAVE</b> to create a decompression buffer |
916 |
|
* for <b>EACH</b> of your streaming threads and provide it with the |
917 |
|
* Sample::Read() call in order to avoid race conditions and crashes. |
918 |
|
* |
919 |
|
* You should free the memory occupied by the allocated buffer(s) once |
920 |
|
* you don't need one of your streaming threads anymore by calling |
921 |
|
* DestroyDecompressionBuffer(). |
922 |
|
* |
923 |
|
* @param MaxReadSize - the maximum size (in sample points) you ever |
924 |
|
* expect to read with one Read() call |
925 |
|
* @returns allocated decompression buffer |
926 |
|
* @see DestroyDecompressionBuffer() |
927 |
|
*/ |
928 |
|
buffer_t Sample::CreateDecompressionBuffer(unsigned long MaxReadSize) { |
929 |
|
buffer_t result; |
930 |
|
const double worstCaseHeaderOverhead = |
931 |
|
(256.0 /*frame size*/ + 12.0 /*header*/ + 2.0 /*compression type flag (stereo)*/) / 256.0; |
932 |
|
result.Size = (unsigned long) (double(MaxReadSize) * 3.0 /*(24 Bit)*/ * 2.0 /*stereo*/ * worstCaseHeaderOverhead); |
933 |
|
result.pStart = new int8_t[result.Size]; |
934 |
|
result.NullExtensionSize = 0; |
935 |
|
return result; |
936 |
|
} |
937 |
|
|
938 |
|
/** |
939 |
|
* Free decompression buffer, previously created with |
940 |
|
* CreateDecompressionBuffer(). |
941 |
|
* |
942 |
|
* @param DecompressionBuffer - previously allocated decompression |
943 |
|
* buffer to free |
944 |
|
*/ |
945 |
|
void Sample::DestroyDecompressionBuffer(buffer_t& DecompressionBuffer) { |
946 |
|
if (DecompressionBuffer.Size && DecompressionBuffer.pStart) { |
947 |
|
delete[] (int8_t*) DecompressionBuffer.pStart; |
948 |
|
DecompressionBuffer.pStart = NULL; |
949 |
|
DecompressionBuffer.Size = 0; |
950 |
|
DecompressionBuffer.NullExtensionSize = 0; |
951 |
|
} |
952 |
|
} |
953 |
|
|
954 |
Sample::~Sample() { |
Sample::~Sample() { |
955 |
Instances--; |
Instances--; |
956 |
if (!Instances && pDecompressionBuffer) delete[] (int8_t*) pDecompressionBuffer; |
if (!Instances && InternalDecompressionBuffer.Size) { |
957 |
|
delete[] (unsigned char*) InternalDecompressionBuffer.pStart; |
958 |
|
InternalDecompressionBuffer.pStart = NULL; |
959 |
|
InternalDecompressionBuffer.Size = 0; |
960 |
|
} |
961 |
if (FrameTable) delete[] FrameTable; |
if (FrameTable) delete[] FrameTable; |
962 |
if (RAMCache.pStart) delete[] (int8_t*) RAMCache.pStart; |
if (RAMCache.pStart) delete[] (int8_t*) RAMCache.pStart; |
963 |
} |
} |
1078 |
LFO3Controller = static_cast<lfo3_ctrl_t>(lfo3ctrl & 0x07); // lower 3 bits |
LFO3Controller = static_cast<lfo3_ctrl_t>(lfo3ctrl & 0x07); // lower 3 bits |
1079 |
LFO3Sync = lfo3ctrl & 0x20; // bit 5 |
LFO3Sync = lfo3ctrl & 0x20; // bit 5 |
1080 |
InvertAttenuationController = lfo3ctrl & 0x80; // bit 7 |
InvertAttenuationController = lfo3ctrl & 0x80; // bit 7 |
|
if (VCFType == vcf_type_lowpass) { |
|
|
if (lfo3ctrl & 0x40) // bit 6 |
|
|
VCFType = vcf_type_lowpassturbo; |
|
|
} |
|
1081 |
AttenuationController = DecodeLeverageController(static_cast<_lev_ctrl_t>(_3ewa->ReadUint8())); |
AttenuationController = DecodeLeverageController(static_cast<_lev_ctrl_t>(_3ewa->ReadUint8())); |
1082 |
uint8_t lfo2ctrl = _3ewa->ReadUint8(); |
uint8_t lfo2ctrl = _3ewa->ReadUint8(); |
1083 |
LFO2Controller = static_cast<lfo2_ctrl_t>(lfo2ctrl & 0x07); // lower 3 bits |
LFO2Controller = static_cast<lfo2_ctrl_t>(lfo2ctrl & 0x07); // lower 3 bits |
1122 |
VCFVelocityDynamicRange = vcfvelocity % 5; |
VCFVelocityDynamicRange = vcfvelocity % 5; |
1123 |
VCFVelocityCurve = static_cast<curve_type_t>(vcfvelocity / 5); |
VCFVelocityCurve = static_cast<curve_type_t>(vcfvelocity / 5); |
1124 |
VCFType = static_cast<vcf_type_t>(_3ewa->ReadUint8()); |
VCFType = static_cast<vcf_type_t>(_3ewa->ReadUint8()); |
1125 |
|
if (VCFType == vcf_type_lowpass) { |
1126 |
|
if (lfo3ctrl & 0x40) // bit 6 |
1127 |
|
VCFType = vcf_type_lowpassturbo; |
1128 |
|
} |
1129 |
|
|
1130 |
// 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 |
1131 |
uint32_t tableKey = (VelocityResponseCurve<<16) | (VelocityResponseDepth<<8) | VelocityResponseCurveScaling; |
uint32_t tableKey = (VelocityResponseCurve<<16) | (VelocityResponseDepth<<8) | VelocityResponseCurveScaling; |
1133 |
pVelocityAttenuationTable = (*pVelocityTables)[tableKey]; |
pVelocityAttenuationTable = (*pVelocityTables)[tableKey]; |
1134 |
} |
} |
1135 |
else { |
else { |
1136 |
pVelocityAttenuationTable = new double[128]; |
pVelocityAttenuationTable = |
1137 |
switch (VelocityResponseCurve) { // calculate the new table |
CreateVelocityTable(VelocityResponseCurve, |
1138 |
case curve_type_nonlinear: |
VelocityResponseDepth, |
1139 |
for (int velocity = 0; velocity < 128; velocity++) { |
VelocityResponseCurveScaling); |
|
pVelocityAttenuationTable[velocity] = |
|
|
GIG_VELOCITY_TRANSFORM_NONLINEAR(((double)velocity),((double)VelocityResponseDepth),((double)VelocityResponseCurveScaling)); |
|
|
if (pVelocityAttenuationTable[velocity] > 1.0) pVelocityAttenuationTable[velocity] = 1.0; |
|
|
else if (pVelocityAttenuationTable[velocity] < 1e-15) 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),((double)VelocityResponseCurveScaling)); |
|
|
if (pVelocityAttenuationTable[velocity] > 1.0) pVelocityAttenuationTable[velocity] = 1.0; |
|
|
else if (pVelocityAttenuationTable[velocity] < 1e-15) pVelocityAttenuationTable[velocity] = 0.0; |
|
|
} |
|
|
break; |
|
|
case curve_type_special: |
|
|
for (int velocity = 0; velocity < 128; velocity++) { |
|
|
pVelocityAttenuationTable[velocity] = |
|
|
GIG_VELOCITY_TRANSFORM_SPECIAL(((double)velocity),((double)VelocityResponseDepth),((double)VelocityResponseCurveScaling)); |
|
|
if (pVelocityAttenuationTable[velocity] > 1.0) pVelocityAttenuationTable[velocity] = 1.0; |
|
|
else if (pVelocityAttenuationTable[velocity] < 1e-15) pVelocityAttenuationTable[velocity] = 0.0; |
|
|
} |
|
|
break; |
|
|
case curve_type_unknown: |
|
|
default: |
|
|
throw gig::Exception("Unknown transform curve type."); |
|
|
} |
|
1140 |
(*pVelocityTables)[tableKey] = pVelocityAttenuationTable; // put the new table into the tables map |
(*pVelocityTables)[tableKey] = pVelocityAttenuationTable; // put the new table into the tables map |
1141 |
} |
} |
1142 |
|
|
1143 |
|
SampleAttenuation = pow(10.0, -Gain / (20.0 * 655360)); |
1144 |
} |
} |
1145 |
|
|
1146 |
leverage_ctrl_t DimensionRegion::DecodeLeverageController(_lev_ctrl_t EncodedController) { |
leverage_ctrl_t DimensionRegion::DecodeLeverageController(_lev_ctrl_t EncodedController) { |
1291 |
return pVelocityAttenuationTable[MIDIKeyVelocity]; |
return pVelocityAttenuationTable[MIDIKeyVelocity]; |
1292 |
} |
} |
1293 |
|
|
1294 |
|
double* DimensionRegion::CreateVelocityTable(curve_type_t curveType, uint8_t depth, uint8_t scaling) { |
1295 |
|
|
1296 |
|
// line-segment approximations of the 15 velocity curves |
1297 |
|
|
1298 |
|
// linear |
1299 |
|
const int lin0[] = { 1, 1, 127, 127 }; |
1300 |
|
const int lin1[] = { 1, 21, 127, 127 }; |
1301 |
|
const int lin2[] = { 1, 45, 127, 127 }; |
1302 |
|
const int lin3[] = { 1, 74, 127, 127 }; |
1303 |
|
const int lin4[] = { 1, 127, 127, 127 }; |
1304 |
|
|
1305 |
|
// non-linear |
1306 |
|
const int non0[] = { 1, 4, 24, 5, 57, 17, 92, 57, 122, 127, 127, 127 }; |
1307 |
|
const int non1[] = { 1, 4, 46, 9, 93, 56, 118, 106, 123, 127, |
1308 |
|
127, 127 }; |
1309 |
|
const int non2[] = { 1, 4, 46, 9, 57, 20, 102, 107, 107, 127, |
1310 |
|
127, 127 }; |
1311 |
|
const int non3[] = { 1, 15, 10, 19, 67, 73, 80, 80, 90, 98, 98, 127, |
1312 |
|
127, 127 }; |
1313 |
|
const int non4[] = { 1, 25, 33, 57, 82, 81, 92, 127, 127, 127 }; |
1314 |
|
|
1315 |
|
// special |
1316 |
|
const int spe0[] = { 1, 2, 76, 10, 90, 15, 95, 20, 99, 28, 103, 44, |
1317 |
|
113, 127, 127, 127 }; |
1318 |
|
const int spe1[] = { 1, 2, 27, 5, 67, 18, 89, 29, 95, 35, 107, 67, |
1319 |
|
118, 127, 127, 127 }; |
1320 |
|
const int spe2[] = { 1, 1, 33, 1, 53, 5, 61, 13, 69, 32, 79, 74, |
1321 |
|
85, 90, 91, 127, 127, 127 }; |
1322 |
|
const int spe3[] = { 1, 32, 28, 35, 66, 48, 89, 59, 95, 65, 99, 73, |
1323 |
|
117, 127, 127, 127 }; |
1324 |
|
const int spe4[] = { 1, 4, 23, 5, 49, 13, 57, 17, 92, 57, 122, 127, |
1325 |
|
127, 127 }; |
1326 |
|
|
1327 |
|
const int* const curves[] = { non0, non1, non2, non3, non4, |
1328 |
|
lin0, lin1, lin2, lin3, lin4, |
1329 |
|
spe0, spe1, spe2, spe3, spe4 }; |
1330 |
|
|
1331 |
|
double* const table = new double[128]; |
1332 |
|
|
1333 |
|
const int* curve = curves[curveType * 5 + depth]; |
1334 |
|
const int s = scaling == 0 ? 20 : scaling; // 0 or 20 means no scaling |
1335 |
|
|
1336 |
|
table[0] = 0; |
1337 |
|
for (int x = 1 ; x < 128 ; x++) { |
1338 |
|
|
1339 |
|
if (x > curve[2]) curve += 2; |
1340 |
|
double y = curve[1] + (x - curve[0]) * |
1341 |
|
(double(curve[3] - curve[1]) / (curve[2] - curve[0])); |
1342 |
|
y = y / 127; |
1343 |
|
|
1344 |
|
// Scale up for s > 20, down for s < 20. When |
1345 |
|
// down-scaling, the curve still ends at 1.0. |
1346 |
|
if (s < 20 && y >= 0.5) |
1347 |
|
y = y / ((2 - 40.0 / s) * y + 40.0 / s - 1); |
1348 |
|
else |
1349 |
|
y = y * (s / 20.0); |
1350 |
|
if (y > 1) y = 1; |
1351 |
|
|
1352 |
|
table[x] = y; |
1353 |
|
} |
1354 |
|
return table; |
1355 |
|
} |
1356 |
|
|
1357 |
|
|
1358 |
// *************** Region *************** |
// *************** Region *************** |
1361 |
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) { |
1362 |
// Initialization |
// Initialization |
1363 |
Dimensions = 0; |
Dimensions = 0; |
1364 |
for (int i = 0; i < 32; i++) { |
for (int i = 0; i < 256; i++) { |
1365 |
pDimensionRegions[i] = NULL; |
pDimensionRegions[i] = NULL; |
1366 |
} |
} |
1367 |
|
Layers = 1; |
1368 |
|
File* file = (File*) GetParent()->GetParent(); |
1369 |
|
int dimensionBits = (file->pVersion && file->pVersion->major == 3) ? 8 : 5; |
1370 |
|
|
1371 |
// Actual Loading |
// Actual Loading |
1372 |
|
|
1375 |
RIFF::Chunk* _3lnk = rgnList->GetSubChunk(CHUNK_ID_3LNK); |
RIFF::Chunk* _3lnk = rgnList->GetSubChunk(CHUNK_ID_3LNK); |
1376 |
if (_3lnk) { |
if (_3lnk) { |
1377 |
DimensionRegions = _3lnk->ReadUint32(); |
DimensionRegions = _3lnk->ReadUint32(); |
1378 |
for (int i = 0; i < 5; i++) { |
for (int i = 0; i < dimensionBits; i++) { |
1379 |
dimension_t dimension = static_cast<dimension_t>(_3lnk->ReadUint8()); |
dimension_t dimension = static_cast<dimension_t>(_3lnk->ReadUint8()); |
1380 |
uint8_t bits = _3lnk->ReadUint8(); |
uint8_t bits = _3lnk->ReadUint8(); |
1381 |
if (dimension == dimension_none) { // inactive dimension |
if (dimension == dimension_none) { // inactive dimension |
1392 |
pDimensionDefinitions[i].zones = 0x01 << bits; // = pow(2,bits) |
pDimensionDefinitions[i].zones = 0x01 << bits; // = pow(2,bits) |
1393 |
pDimensionDefinitions[i].split_type = (dimension == dimension_layer || |
pDimensionDefinitions[i].split_type = (dimension == dimension_layer || |
1394 |
dimension == dimension_samplechannel || |
dimension == dimension_samplechannel || |
1395 |
dimension == dimension_releasetrigger) ? split_type_bit |
dimension == dimension_releasetrigger || |
1396 |
: split_type_normal; |
dimension == dimension_roundrobin || |
1397 |
|
dimension == dimension_random) ? split_type_bit |
1398 |
|
: split_type_normal; |
1399 |
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 |
1400 |
pDimensionDefinitions[i].zone_size = |
pDimensionDefinitions[i].zone_size = |
1401 |
(pDimensionDefinitions[i].split_type == split_type_normal) ? 128 / pDimensionDefinitions[i].zones |
(pDimensionDefinitions[i].split_type == split_type_normal) ? 128 / pDimensionDefinitions[i].zones |
1402 |
: 0; |
: 0; |
1403 |
Dimensions++; |
Dimensions++; |
1404 |
|
|
1405 |
|
// if this is a layer dimension, remember the amount of layers |
1406 |
|
if (dimension == dimension_layer) Layers = pDimensionDefinitions[i].zones; |
1407 |
} |
} |
1408 |
_3lnk->SetPos(6, RIFF::stream_curpos); // jump forward to next dimension definition |
_3lnk->SetPos(6, RIFF::stream_curpos); // jump forward to next dimension definition |
1409 |
} |
} |
1420 |
else { // custom defined ranges |
else { // custom defined ranges |
1421 |
pDimDef->split_type = split_type_customvelocity; |
pDimDef->split_type = split_type_customvelocity; |
1422 |
pDimDef->ranges = new range_t[pDimDef->zones]; |
pDimDef->ranges = new range_t[pDimDef->zones]; |
1423 |
unsigned int bits[5] = {0,0,0,0,0}; |
uint8_t bits[8] = { 0 }; |
1424 |
int previousUpperLimit = -1; |
int previousUpperLimit = -1; |
1425 |
for (int velocityZone = 0; velocityZone < pDimDef->zones; velocityZone++) { |
for (int velocityZone = 0; velocityZone < pDimDef->zones; velocityZone++) { |
1426 |
bits[i] = velocityZone; |
bits[i] = velocityZone; |
1427 |
DimensionRegion* pDimRegion = GetDimensionRegionByBit(bits[4],bits[3],bits[2],bits[1],bits[0]); |
DimensionRegion* pDimRegion = GetDimensionRegionByBit(bits); |
1428 |
|
|
1429 |
pDimDef->ranges[velocityZone].low = previousUpperLimit + 1; |
pDimDef->ranges[velocityZone].low = previousUpperLimit + 1; |
1430 |
pDimDef->ranges[velocityZone].high = pDimRegion->VelocityUpperLimit; |
pDimDef->ranges[velocityZone].high = pDimRegion->VelocityUpperLimit; |
1438 |
} |
} |
1439 |
} |
} |
1440 |
|
|
1441 |
|
// jump to start of the wave pool indices (if not already there) |
1442 |
|
File* file = (File*) GetParent()->GetParent(); |
1443 |
|
if (file->pVersion && file->pVersion->major == 3) |
1444 |
|
_3lnk->SetPos(68); // version 3 has a different 3lnk structure |
1445 |
|
else |
1446 |
|
_3lnk->SetPos(44); |
1447 |
|
|
1448 |
// load sample references |
// load sample references |
|
_3lnk->SetPos(44); // jump to start of the wave pool indices (if not already there) |
|
1449 |
for (uint i = 0; i < DimensionRegions; i++) { |
for (uint i = 0; i < DimensionRegions; i++) { |
1450 |
uint32_t wavepoolindex = _3lnk->ReadUint32(); |
uint32_t wavepoolindex = _3lnk->ReadUint32(); |
1451 |
pDimensionRegions[i]->pSample = GetSampleFromWavePool(wavepoolindex); |
pDimensionRegions[i]->pSample = GetSampleFromWavePool(wavepoolindex); |
1474 |
for (uint i = 0; i < Dimensions; i++) { |
for (uint i = 0; i < Dimensions; i++) { |
1475 |
if (pDimensionDefinitions[i].ranges) delete[] pDimensionDefinitions[i].ranges; |
if (pDimensionDefinitions[i].ranges) delete[] pDimensionDefinitions[i].ranges; |
1476 |
} |
} |
1477 |
for (int i = 0; i < 32; i++) { |
for (int i = 0; i < 256; i++) { |
1478 |
if (pDimensionRegions[i]) delete pDimensionRegions[i]; |
if (pDimensionRegions[i]) delete pDimensionRegions[i]; |
1479 |
} |
} |
1480 |
} |
} |
1492 |
* 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, |
1493 |
* etc.). |
* etc.). |
1494 |
* |
* |
1495 |
* @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 |
|
1496 |
* @returns adress to the DimensionRegion for the given situation |
* @returns adress to the DimensionRegion for the given situation |
1497 |
* @see pDimensionDefinitions |
* @see pDimensionDefinitions |
1498 |
* @see Dimensions |
* @see Dimensions |
1499 |
*/ |
*/ |
1500 |
DimensionRegion* Region::GetDimensionRegionByValue(uint Dim4Val, uint Dim3Val, uint Dim2Val, uint Dim1Val, uint Dim0Val) { |
DimensionRegion* Region::GetDimensionRegionByValue(const uint DimValues[8]) { |
1501 |
uint8_t bits[5] = {Dim0Val,Dim1Val,Dim2Val,Dim3Val,Dim4Val}; |
uint8_t bits[8] = { 0 }; |
1502 |
for (uint i = 0; i < Dimensions; i++) { |
for (uint i = 0; i < Dimensions; i++) { |
1503 |
|
bits[i] = DimValues[i]; |
1504 |
switch (pDimensionDefinitions[i].split_type) { |
switch (pDimensionDefinitions[i].split_type) { |
1505 |
case split_type_normal: |
case split_type_normal: |
1506 |
bits[i] /= pDimensionDefinitions[i].zone_size; |
bits[i] /= pDimensionDefinitions[i].zone_size; |
1514 |
break; |
break; |
1515 |
} |
} |
1516 |
} |
} |
1517 |
return GetDimensionRegionByBit(bits[4],bits[3],bits[2],bits[1],bits[0]); |
return GetDimensionRegionByBit(bits); |
1518 |
} |
} |
1519 |
|
|
1520 |
/** |
/** |
1522 |
* numbers (zone index). You usually use <i>GetDimensionRegionByValue</i> |
* numbers (zone index). You usually use <i>GetDimensionRegionByValue</i> |
1523 |
* instead of calling this method directly! |
* instead of calling this method directly! |
1524 |
* |
* |
1525 |
* @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 |
|
1526 |
* @returns adress to the DimensionRegion for the given dimension |
* @returns adress to the DimensionRegion for the given dimension |
1527 |
* bit numbers |
* bit numbers |
1528 |
* @see GetDimensionRegionByValue() |
* @see GetDimensionRegionByValue() |
1529 |
*/ |
*/ |
1530 |
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]) { |
1531 |
return *(pDimensionRegions + ((((((((Dim4Bit << pDimensionDefinitions[3].bits) | Dim3Bit) |
return pDimensionRegions[((((((DimBits[7] << pDimensionDefinitions[6].bits | DimBits[6]) |
1532 |
<< pDimensionDefinitions[2].bits) | Dim2Bit) |
<< pDimensionDefinitions[5].bits | DimBits[5]) |
1533 |
<< pDimensionDefinitions[1].bits) | Dim1Bit) |
<< pDimensionDefinitions[4].bits | DimBits[4]) |
1534 |
<< pDimensionDefinitions[0].bits) | Dim0Bit) ); |
<< pDimensionDefinitions[3].bits | DimBits[3]) |
1535 |
|
<< pDimensionDefinitions[2].bits | DimBits[2]) |
1536 |
|
<< pDimensionDefinitions[1].bits | DimBits[1]) |
1537 |
|
<< pDimensionDefinitions[0].bits | DimBits[0]]; |
1538 |
} |
} |
1539 |
|
|
1540 |
/** |
/** |
1552 |
} |
} |
1553 |
|
|
1554 |
Sample* Region::GetSampleFromWavePool(unsigned int WavePoolTableIndex) { |
Sample* Region::GetSampleFromWavePool(unsigned int WavePoolTableIndex) { |
1555 |
|
if ((int32_t)WavePoolTableIndex == -1) return NULL; |
1556 |
File* file = (File*) GetParent()->GetParent(); |
File* file = (File*) GetParent()->GetParent(); |
1557 |
unsigned long soughtoffset = file->pWavePoolTable[WavePoolTableIndex]; |
unsigned long soughtoffset = file->pWavePoolTable[WavePoolTableIndex]; |
1558 |
Sample* sample = file->GetFirstSample(); |
Sample* sample = file->GetFirstSample(); |
1594 |
RIFF::List* lrgn = insList->GetSubList(LIST_TYPE_LRGN); |
RIFF::List* lrgn = insList->GetSubList(LIST_TYPE_LRGN); |
1595 |
if (!lrgn) throw gig::Exception("Mandatory chunks in <ins > chunk not found."); |
if (!lrgn) throw gig::Exception("Mandatory chunks in <ins > chunk not found."); |
1596 |
pRegions = new Region*[Regions]; |
pRegions = new Region*[Regions]; |
1597 |
|
for (uint i = 0; i < Regions; i++) pRegions[i] = NULL; |
1598 |
RIFF::List* rgn = lrgn->GetFirstSubList(); |
RIFF::List* rgn = lrgn->GetFirstSubList(); |
1599 |
unsigned int iRegion = 0; |
unsigned int iRegion = 0; |
1600 |
while (rgn) { |
while (rgn) { |
1618 |
if (pRegions) { |
if (pRegions) { |
1619 |
if (pRegions[i]) delete (pRegions[i]); |
if (pRegions[i]) delete (pRegions[i]); |
1620 |
} |
} |
|
delete[] pRegions; |
|
1621 |
} |
} |
1622 |
|
if (pRegions) delete[] pRegions; |
1623 |
} |
} |
1624 |
|
|
1625 |
/** |
/** |
1661 |
* @see GetFirstRegion() |
* @see GetFirstRegion() |
1662 |
*/ |
*/ |
1663 |
Region* Instrument::GetNextRegion() { |
Region* Instrument::GetNextRegion() { |
1664 |
if (RegionIndex < 0 || RegionIndex >= Regions) return NULL; |
if (RegionIndex < 0 || uint32_t(RegionIndex) >= Regions) return NULL; |
1665 |
return pRegions[RegionIndex++]; |
return pRegions[RegionIndex++]; |
1666 |
} |
} |
1667 |
|
|
1675 |
pInstruments = NULL; |
pInstruments = NULL; |
1676 |
} |
} |
1677 |
|
|
1678 |
|
File::~File() { |
1679 |
|
// free samples |
1680 |
|
if (pSamples) { |
1681 |
|
SamplesIterator = pSamples->begin(); |
1682 |
|
while (SamplesIterator != pSamples->end() ) { |
1683 |
|
delete (*SamplesIterator); |
1684 |
|
SamplesIterator++; |
1685 |
|
} |
1686 |
|
pSamples->clear(); |
1687 |
|
delete pSamples; |
1688 |
|
|
1689 |
|
} |
1690 |
|
// free instruments |
1691 |
|
if (pInstruments) { |
1692 |
|
InstrumentsIterator = pInstruments->begin(); |
1693 |
|
while (InstrumentsIterator != pInstruments->end() ) { |
1694 |
|
delete (*InstrumentsIterator); |
1695 |
|
InstrumentsIterator++; |
1696 |
|
} |
1697 |
|
pInstruments->clear(); |
1698 |
|
delete pInstruments; |
1699 |
|
} |
1700 |
|
} |
1701 |
|
|
1702 |
Sample* File::GetFirstSample() { |
Sample* File::GetFirstSample() { |
1703 |
if (!pSamples) LoadSamples(); |
if (!pSamples) LoadSamples(); |
1704 |
if (!pSamples) return NULL; |
if (!pSamples) return NULL; |