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 |
|
#include <iostream> |
27 |
|
|
28 |
namespace gig { |
namespace gig { |
29 |
|
|
30 |
|
// *************** progress_t *************** |
31 |
|
// * |
32 |
|
|
33 |
|
progress_t::progress_t() { |
34 |
|
callback = NULL; |
35 |
|
__range_min = 0.0f; |
36 |
|
__range_max = 1.0f; |
37 |
|
} |
38 |
|
|
39 |
|
// private helper function to convert progress of a subprocess into the global progress |
40 |
|
static void __notify_progress(progress_t* pProgress, float subprogress) { |
41 |
|
if (pProgress && pProgress->callback) { |
42 |
|
const float totalrange = pProgress->__range_max - pProgress->__range_min; |
43 |
|
const float totalprogress = pProgress->__range_min + subprogress * totalrange; |
44 |
|
pProgress->callback(totalprogress); // now actually notify about the progress |
45 |
|
} |
46 |
|
} |
47 |
|
|
48 |
|
// private helper function to divide a progress into subprogresses |
49 |
|
static void __divide_progress(progress_t* pParentProgress, progress_t* pSubProgress, float totalTasks, float currentTask) { |
50 |
|
if (pParentProgress && pParentProgress->callback) { |
51 |
|
const float totalrange = pParentProgress->__range_max - pParentProgress->__range_min; |
52 |
|
pSubProgress->callback = pParentProgress->callback; |
53 |
|
pSubProgress->__range_min = pParentProgress->__range_min + totalrange * currentTask / totalTasks; |
54 |
|
pSubProgress->__range_max = pSubProgress->__range_min + totalrange / totalTasks; |
55 |
|
} |
56 |
|
} |
57 |
|
|
58 |
|
|
59 |
|
// *************** Internal functions for sample decopmression *************** |
60 |
|
// * |
61 |
|
|
62 |
|
namespace { |
63 |
|
|
64 |
|
inline int get12lo(const unsigned char* pSrc) |
65 |
|
{ |
66 |
|
const int x = pSrc[0] | (pSrc[1] & 0x0f) << 8; |
67 |
|
return x & 0x800 ? x - 0x1000 : x; |
68 |
|
} |
69 |
|
|
70 |
|
inline int get12hi(const unsigned char* pSrc) |
71 |
|
{ |
72 |
|
const int x = pSrc[1] >> 4 | pSrc[2] << 4; |
73 |
|
return x & 0x800 ? x - 0x1000 : x; |
74 |
|
} |
75 |
|
|
76 |
|
inline int16_t get16(const unsigned char* pSrc) |
77 |
|
{ |
78 |
|
return int16_t(pSrc[0] | pSrc[1] << 8); |
79 |
|
} |
80 |
|
|
81 |
|
inline int get24(const unsigned char* pSrc) |
82 |
|
{ |
83 |
|
const int x = pSrc[0] | pSrc[1] << 8 | pSrc[2] << 16; |
84 |
|
return x & 0x800000 ? x - 0x1000000 : x; |
85 |
|
} |
86 |
|
|
87 |
|
void Decompress16(int compressionmode, const unsigned char* params, |
88 |
|
int srcStep, int dstStep, |
89 |
|
const unsigned char* pSrc, int16_t* pDst, |
90 |
|
unsigned long currentframeoffset, |
91 |
|
unsigned long copysamples) |
92 |
|
{ |
93 |
|
switch (compressionmode) { |
94 |
|
case 0: // 16 bit uncompressed |
95 |
|
pSrc += currentframeoffset * srcStep; |
96 |
|
while (copysamples) { |
97 |
|
*pDst = get16(pSrc); |
98 |
|
pDst += dstStep; |
99 |
|
pSrc += srcStep; |
100 |
|
copysamples--; |
101 |
|
} |
102 |
|
break; |
103 |
|
|
104 |
|
case 1: // 16 bit compressed to 8 bit |
105 |
|
int y = get16(params); |
106 |
|
int dy = get16(params + 2); |
107 |
|
while (currentframeoffset) { |
108 |
|
dy -= int8_t(*pSrc); |
109 |
|
y -= dy; |
110 |
|
pSrc += srcStep; |
111 |
|
currentframeoffset--; |
112 |
|
} |
113 |
|
while (copysamples) { |
114 |
|
dy -= int8_t(*pSrc); |
115 |
|
y -= dy; |
116 |
|
*pDst = y; |
117 |
|
pDst += dstStep; |
118 |
|
pSrc += srcStep; |
119 |
|
copysamples--; |
120 |
|
} |
121 |
|
break; |
122 |
|
} |
123 |
|
} |
124 |
|
|
125 |
|
void Decompress24(int compressionmode, const unsigned char* params, |
126 |
|
int dstStep, const unsigned char* pSrc, int16_t* pDst, |
127 |
|
unsigned long currentframeoffset, |
128 |
|
unsigned long copysamples, int truncatedBits) |
129 |
|
{ |
130 |
|
// Note: The 24 bits are truncated to 16 bits for now. |
131 |
|
|
132 |
|
// Note: The calculation of the initial value of y is strange |
133 |
|
// and not 100% correct. What should the first two parameters |
134 |
|
// really be used for? Why are they two? The correct value for |
135 |
|
// y seems to lie somewhere between the values of the first |
136 |
|
// two parameters. |
137 |
|
// |
138 |
|
// Strange thing #2: The formula in SKIP_ONE gives values for |
139 |
|
// y that are twice as high as they should be. That's why |
140 |
|
// COPY_ONE shifts an extra step, and also why y is |
141 |
|
// initialized with a sum instead of a mean value. |
142 |
|
|
143 |
|
int y, dy, ddy; |
144 |
|
|
145 |
|
const int shift = 8 - truncatedBits; |
146 |
|
const int shift1 = shift + 1; |
147 |
|
|
148 |
|
#define GET_PARAMS(params) \ |
149 |
|
y = (get24(params) + get24((params) + 3)); \ |
150 |
|
dy = get24((params) + 6); \ |
151 |
|
ddy = get24((params) + 9) |
152 |
|
|
153 |
|
#define SKIP_ONE(x) \ |
154 |
|
ddy -= (x); \ |
155 |
|
dy -= ddy; \ |
156 |
|
y -= dy |
157 |
|
|
158 |
|
#define COPY_ONE(x) \ |
159 |
|
SKIP_ONE(x); \ |
160 |
|
*pDst = y >> shift1; \ |
161 |
|
pDst += dstStep |
162 |
|
|
163 |
|
switch (compressionmode) { |
164 |
|
case 2: // 24 bit uncompressed |
165 |
|
pSrc += currentframeoffset * 3; |
166 |
|
while (copysamples) { |
167 |
|
*pDst = get24(pSrc) >> shift; |
168 |
|
pDst += dstStep; |
169 |
|
pSrc += 3; |
170 |
|
copysamples--; |
171 |
|
} |
172 |
|
break; |
173 |
|
|
174 |
|
case 3: // 24 bit compressed to 16 bit |
175 |
|
GET_PARAMS(params); |
176 |
|
while (currentframeoffset) { |
177 |
|
SKIP_ONE(get16(pSrc)); |
178 |
|
pSrc += 2; |
179 |
|
currentframeoffset--; |
180 |
|
} |
181 |
|
while (copysamples) { |
182 |
|
COPY_ONE(get16(pSrc)); |
183 |
|
pSrc += 2; |
184 |
|
copysamples--; |
185 |
|
} |
186 |
|
break; |
187 |
|
|
188 |
|
case 4: // 24 bit compressed to 12 bit |
189 |
|
GET_PARAMS(params); |
190 |
|
while (currentframeoffset > 1) { |
191 |
|
SKIP_ONE(get12lo(pSrc)); |
192 |
|
SKIP_ONE(get12hi(pSrc)); |
193 |
|
pSrc += 3; |
194 |
|
currentframeoffset -= 2; |
195 |
|
} |
196 |
|
if (currentframeoffset) { |
197 |
|
SKIP_ONE(get12lo(pSrc)); |
198 |
|
currentframeoffset--; |
199 |
|
if (copysamples) { |
200 |
|
COPY_ONE(get12hi(pSrc)); |
201 |
|
pSrc += 3; |
202 |
|
copysamples--; |
203 |
|
} |
204 |
|
} |
205 |
|
while (copysamples > 1) { |
206 |
|
COPY_ONE(get12lo(pSrc)); |
207 |
|
COPY_ONE(get12hi(pSrc)); |
208 |
|
pSrc += 3; |
209 |
|
copysamples -= 2; |
210 |
|
} |
211 |
|
if (copysamples) { |
212 |
|
COPY_ONE(get12lo(pSrc)); |
213 |
|
} |
214 |
|
break; |
215 |
|
|
216 |
|
case 5: // 24 bit compressed to 8 bit |
217 |
|
GET_PARAMS(params); |
218 |
|
while (currentframeoffset) { |
219 |
|
SKIP_ONE(int8_t(*pSrc++)); |
220 |
|
currentframeoffset--; |
221 |
|
} |
222 |
|
while (copysamples) { |
223 |
|
COPY_ONE(int8_t(*pSrc++)); |
224 |
|
copysamples--; |
225 |
|
} |
226 |
|
break; |
227 |
|
} |
228 |
|
} |
229 |
|
|
230 |
|
const int bytesPerFrame[] = { 4096, 2052, 768, 524, 396, 268 }; |
231 |
|
const int bytesPerFrameNoHdr[] = { 4096, 2048, 768, 512, 384, 256 }; |
232 |
|
const int headerSize[] = { 0, 4, 0, 12, 12, 12 }; |
233 |
|
const int bitsPerSample[] = { 16, 8, 24, 16, 12, 8 }; |
234 |
|
} |
235 |
|
|
236 |
|
|
237 |
// *************** Sample *************** |
// *************** Sample *************** |
238 |
// * |
// * |
239 |
|
|
240 |
unsigned int Sample::Instances = 0; |
unsigned int Sample::Instances = 0; |
241 |
void* Sample::pDecompressionBuffer = NULL; |
buffer_t Sample::InternalDecompressionBuffer; |
|
unsigned long Sample::DecompressionBufferSize = 0; |
|
242 |
|
|
243 |
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) { |
244 |
Instances++; |
Instances++; |
257 |
smpl->Read(&SMPTEFormat, 1, 4); |
smpl->Read(&SMPTEFormat, 1, 4); |
258 |
SMPTEOffset = smpl->ReadInt32(); |
SMPTEOffset = smpl->ReadInt32(); |
259 |
Loops = smpl->ReadInt32(); |
Loops = smpl->ReadInt32(); |
260 |
uint32_t manufByt = smpl->ReadInt32(); |
smpl->ReadInt32(); // manufByt |
261 |
LoopID = smpl->ReadInt32(); |
LoopID = smpl->ReadInt32(); |
262 |
smpl->Read(&LoopType, 1, 4); |
smpl->Read(&LoopType, 1, 4); |
263 |
LoopStart = smpl->ReadInt32(); |
LoopStart = smpl->ReadInt32(); |
271 |
RAMCache.pStart = NULL; |
RAMCache.pStart = NULL; |
272 |
RAMCache.NullExtensionSize = 0; |
RAMCache.NullExtensionSize = 0; |
273 |
|
|
274 |
Compressed = (waveList->GetSubChunk(CHUNK_ID_EWAV)); |
if (BitDepth > 24) throw gig::Exception("Only samples up to 24 bit supported"); |
275 |
|
|
276 |
|
RIFF::Chunk* ewav = waveList->GetSubChunk(CHUNK_ID_EWAV); |
277 |
|
Compressed = ewav; |
278 |
|
Dithered = false; |
279 |
|
TruncatedBits = 0; |
280 |
if (Compressed) { |
if (Compressed) { |
281 |
ScanCompressedSample(); |
uint32_t version = ewav->ReadInt32(); |
282 |
if (!pDecompressionBuffer) { |
if (version == 3 && BitDepth == 24) { |
283 |
pDecompressionBuffer = new int8_t[INITIAL_SAMPLE_BUFFER_SIZE]; |
Dithered = ewav->ReadInt32(); |
284 |
DecompressionBufferSize = INITIAL_SAMPLE_BUFFER_SIZE; |
ewav->SetPos(Channels == 2 ? 84 : 64); |
285 |
|
TruncatedBits = ewav->ReadInt32(); |
286 |
} |
} |
287 |
|
ScanCompressedSample(); |
288 |
} |
} |
289 |
FrameOffset = 0; // just for streaming compressed samples |
|
290 |
|
// we use a buffer for decompression and for truncating 24 bit samples to 16 bit |
291 |
|
if ((Compressed || BitDepth == 24) && !InternalDecompressionBuffer.Size) { |
292 |
|
InternalDecompressionBuffer.pStart = new unsigned char[INITIAL_SAMPLE_BUFFER_SIZE]; |
293 |
|
InternalDecompressionBuffer.Size = INITIAL_SAMPLE_BUFFER_SIZE; |
294 |
|
} |
295 |
|
FrameOffset = 0; // just for streaming compressed samples |
296 |
|
|
297 |
LoopSize = LoopEnd - LoopStart; |
LoopSize = LoopEnd - LoopStart; |
298 |
} |
} |
303 |
this->SamplesTotal = 0; |
this->SamplesTotal = 0; |
304 |
std::list<unsigned long> frameOffsets; |
std::list<unsigned long> frameOffsets; |
305 |
|
|
306 |
|
SamplesPerFrame = BitDepth == 24 ? 256 : 2048; |
307 |
|
WorstCaseFrameSize = SamplesPerFrame * FrameSize + Channels; // +Channels for compression flag |
308 |
|
|
309 |
// Scanning |
// Scanning |
310 |
pCkData->SetPos(0); |
pCkData->SetPos(0); |
311 |
while (pCkData->GetState() == RIFF::stream_ready) { |
if (Channels == 2) { // Stereo |
312 |
frameOffsets.push_back(pCkData->GetPos()); |
for (int i = 0 ; ; i++) { |
313 |
int16_t compressionmode = pCkData->ReadInt16(); |
// for 24 bit samples every 8:th frame offset is |
314 |
this->SamplesTotal += 2048; |
// stored, to save some memory |
315 |
switch (compressionmode) { |
if (BitDepth != 24 || (i & 7) == 0) frameOffsets.push_back(pCkData->GetPos()); |
316 |
case 1: // left channel compressed |
|
317 |
case 256: // right channel compressed |
const int mode_l = pCkData->ReadUint8(); |
318 |
pCkData->SetPos(6148, RIFF::stream_curpos); |
const int mode_r = pCkData->ReadUint8(); |
319 |
|
if (mode_l > 5 || mode_r > 5) throw gig::Exception("Unknown compression mode"); |
320 |
|
const unsigned long frameSize = bytesPerFrame[mode_l] + bytesPerFrame[mode_r]; |
321 |
|
|
322 |
|
if (pCkData->RemainingBytes() <= frameSize) { |
323 |
|
SamplesInLastFrame = |
324 |
|
((pCkData->RemainingBytes() - headerSize[mode_l] - headerSize[mode_r]) << 3) / |
325 |
|
(bitsPerSample[mode_l] + bitsPerSample[mode_r]); |
326 |
|
SamplesTotal += SamplesInLastFrame; |
327 |
break; |
break; |
328 |
case 257: // both channels compressed |
} |
329 |
pCkData->SetPos(4104, RIFF::stream_curpos); |
SamplesTotal += SamplesPerFrame; |
330 |
|
pCkData->SetPos(frameSize, RIFF::stream_curpos); |
331 |
|
} |
332 |
|
} |
333 |
|
else { // Mono |
334 |
|
for (int i = 0 ; ; i++) { |
335 |
|
if (BitDepth != 24 || (i & 7) == 0) frameOffsets.push_back(pCkData->GetPos()); |
336 |
|
|
337 |
|
const int mode = pCkData->ReadUint8(); |
338 |
|
if (mode > 5) throw gig::Exception("Unknown compression mode"); |
339 |
|
const unsigned long frameSize = bytesPerFrame[mode]; |
340 |
|
|
341 |
|
if (pCkData->RemainingBytes() <= frameSize) { |
342 |
|
SamplesInLastFrame = |
343 |
|
((pCkData->RemainingBytes() - headerSize[mode]) << 3) / bitsPerSample[mode]; |
344 |
|
SamplesTotal += SamplesInLastFrame; |
345 |
break; |
break; |
346 |
default: // both channels uncompressed |
} |
347 |
pCkData->SetPos(8192, RIFF::stream_curpos); |
SamplesTotal += SamplesPerFrame; |
348 |
|
pCkData->SetPos(frameSize, RIFF::stream_curpos); |
349 |
} |
} |
350 |
} |
} |
351 |
pCkData->SetPos(0); |
pCkData->SetPos(0); |
352 |
|
|
|
//FIXME: only seen compressed samples with 16 bit stereo so far |
|
|
this->FrameSize = 4; |
|
|
this->BitDepth = 16; |
|
|
|
|
353 |
// 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) |
354 |
if (FrameTable) delete[] FrameTable; |
if (FrameTable) delete[] FrameTable; |
355 |
FrameTable = new unsigned long[frameOffsets.size()]; |
FrameTable = new unsigned long[frameOffsets.size()]; |
385 |
* that will be returned to determine the actual cached samples, but note |
* that will be returned to determine the actual cached samples, but note |
386 |
* 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 |
387 |
* samples by dividing it by the frame size of the sample: |
* samples by dividing it by the frame size of the sample: |
388 |
* |
* @code |
389 |
* buffer_t buf = pSample->LoadSampleData(acquired_samples); |
* buffer_t buf = pSample->LoadSampleData(acquired_samples); |
390 |
* long cachedsamples = buf.Size / pSample->FrameSize; |
* long cachedsamples = buf.Size / pSample->FrameSize; |
391 |
|
* @endcode |
392 |
* |
* |
393 |
* @param SampleCount - number of sample points to load into RAM |
* @param SampleCount - number of sample points to load into RAM |
394 |
* @returns buffer_t structure with start address and size of |
* @returns buffer_t structure with start address and size of |
434 |
* that will be returned to determine the actual cached samples, but note |
* that will be returned to determine the actual cached samples, but note |
435 |
* 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 |
436 |
* samples by dividing it by the frame size of the sample: |
* samples by dividing it by the frame size of the sample: |
437 |
* |
* @code |
438 |
* buffer_t buf = pSample->LoadSampleDataWithNullSamplesExtension(acquired_samples, null_samples); |
* buffer_t buf = pSample->LoadSampleDataWithNullSamplesExtension(acquired_samples, null_samples); |
439 |
* long cachedsamples = buf.Size / pSample->FrameSize; |
* long cachedsamples = buf.Size / pSample->FrameSize; |
440 |
* |
* @endcode |
441 |
* The method will add \a NullSamplesCount silence samples past the |
* The method will add \a NullSamplesCount silence samples past the |
442 |
* official buffer end (this won't affect the 'Size' member of the |
* official buffer end (this won't affect the 'Size' member of the |
443 |
* buffer_t structure, that means 'Size' always reflects the size of the |
* buffer_t structure, that means 'Size' always reflects the size of the |
568 |
* 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. |
569 |
* You have to allocate and initialize the playback_state_t structure by |
* You have to allocate and initialize the playback_state_t structure by |
570 |
* yourself before you use it to stream a sample: |
* yourself before you use it to stream a sample: |
571 |
* |
* @code |
572 |
* <i> |
* gig::playback_state_t playbackstate; |
573 |
* gig::playback_state_t playbackstate; <br> |
* playbackstate.position = 0; |
574 |
* playbackstate.position = 0; <br> |
* playbackstate.reverse = false; |
575 |
* playbackstate.reverse = false; <br> |
* playbackstate.loop_cycles_left = pSample->LoopPlayCount; |
576 |
* playbackstate.loop_cycles_left = pSample->LoopPlayCount; <br> |
* @endcode |
|
* </i> |
|
|
* |
|
577 |
* 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 |
578 |
* 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. |
579 |
* The method already handles such cases by itself. |
* The method already handles such cases by itself. |
580 |
* |
* |
581 |
|
* <b>Caution:</b> If you are using more than one streaming thread, you |
582 |
|
* have to use an external decompression buffer for <b>EACH</b> |
583 |
|
* streaming thread to avoid race conditions and crashes! |
584 |
|
* |
585 |
* @param pBuffer destination buffer |
* @param pBuffer destination buffer |
586 |
* @param SampleCount number of sample points to read |
* @param SampleCount number of sample points to read |
587 |
* @param pPlaybackState will be used to store and reload the playback |
* @param pPlaybackState will be used to store and reload the playback |
588 |
* state for the next ReadAndLoop() call |
* state for the next ReadAndLoop() call |
589 |
|
* @param pExternalDecompressionBuffer (optional) external buffer to use for decompression |
590 |
* @returns number of successfully read sample points |
* @returns number of successfully read sample points |
591 |
|
* @see CreateDecompressionBuffer() |
592 |
*/ |
*/ |
593 |
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) { |
594 |
unsigned long samplestoread = SampleCount, totalreadsamples = 0, readsamples, samplestoloopend; |
unsigned long samplestoread = SampleCount, totalreadsamples = 0, readsamples, samplestoloopend; |
595 |
uint8_t* pDst = (uint8_t*) pBuffer; |
uint8_t* pDst = (uint8_t*) pBuffer; |
596 |
|
|
608 |
if (!pPlaybackState->reverse) { // forward playback |
if (!pPlaybackState->reverse) { // forward playback |
609 |
do { |
do { |
610 |
samplestoloopend = this->LoopEnd - GetPos(); |
samplestoloopend = this->LoopEnd - GetPos(); |
611 |
readsamples = Read(&pDst[totalreadsamples * this->FrameSize], Min(samplestoread, samplestoloopend)); |
readsamples = Read(&pDst[totalreadsamples * this->FrameSize], Min(samplestoread, samplestoloopend), pExternalDecompressionBuffer); |
612 |
samplestoread -= readsamples; |
samplestoread -= readsamples; |
613 |
totalreadsamples += readsamples; |
totalreadsamples += readsamples; |
614 |
if (readsamples == samplestoloopend) { |
if (readsamples == samplestoloopend) { |
634 |
|
|
635 |
// read samples for backward playback |
// read samples for backward playback |
636 |
do { |
do { |
637 |
readsamples = Read(&pDst[totalreadsamples * this->FrameSize], samplestoreadinloop); |
readsamples = Read(&pDst[totalreadsamples * this->FrameSize], samplestoreadinloop, pExternalDecompressionBuffer); |
638 |
samplestoreadinloop -= readsamples; |
samplestoreadinloop -= readsamples; |
639 |
samplestoread -= readsamples; |
samplestoread -= readsamples; |
640 |
totalreadsamples += readsamples; |
totalreadsamples += readsamples; |
658 |
// forward playback (not entered the loop yet) |
// forward playback (not entered the loop yet) |
659 |
if (!pPlaybackState->reverse) do { |
if (!pPlaybackState->reverse) do { |
660 |
samplestoloopend = this->LoopEnd - GetPos(); |
samplestoloopend = this->LoopEnd - GetPos(); |
661 |
readsamples = Read(&pDst[totalreadsamples * this->FrameSize], Min(samplestoread, samplestoloopend)); |
readsamples = Read(&pDst[totalreadsamples * this->FrameSize], Min(samplestoread, samplestoloopend), pExternalDecompressionBuffer); |
662 |
samplestoread -= readsamples; |
samplestoread -= readsamples; |
663 |
totalreadsamples += readsamples; |
totalreadsamples += readsamples; |
664 |
if (readsamples == samplestoloopend) { |
if (readsamples == samplestoloopend) { |
688 |
// 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 |
689 |
if (this->LoopPlayCount && !pPlaybackState->loop_cycles_left) break; |
if (this->LoopPlayCount && !pPlaybackState->loop_cycles_left) break; |
690 |
samplestoloopend = this->LoopEnd - GetPos(); |
samplestoloopend = this->LoopEnd - GetPos(); |
691 |
readsamples = Read(&pDst[totalreadsamples * this->FrameSize], Min(samplestoreadinloop, samplestoloopend)); |
readsamples = Read(&pDst[totalreadsamples * this->FrameSize], Min(samplestoreadinloop, samplestoloopend), pExternalDecompressionBuffer); |
692 |
samplestoreadinloop -= readsamples; |
samplestoreadinloop -= readsamples; |
693 |
samplestoread -= readsamples; |
samplestoread -= readsamples; |
694 |
totalreadsamples += readsamples; |
totalreadsamples += readsamples; |
710 |
// 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 |
711 |
if (this->LoopPlayCount && !pPlaybackState->loop_cycles_left) break; |
if (this->LoopPlayCount && !pPlaybackState->loop_cycles_left) break; |
712 |
samplestoloopend = this->LoopEnd - GetPos(); |
samplestoloopend = this->LoopEnd - GetPos(); |
713 |
readsamples = Read(&pDst[totalreadsamples * this->FrameSize], Min(samplestoread, samplestoloopend)); |
readsamples = Read(&pDst[totalreadsamples * this->FrameSize], Min(samplestoread, samplestoloopend), pExternalDecompressionBuffer); |
714 |
samplestoread -= readsamples; |
samplestoread -= readsamples; |
715 |
totalreadsamples += readsamples; |
totalreadsamples += readsamples; |
716 |
if (readsamples == samplestoloopend) { |
if (readsamples == samplestoloopend) { |
725 |
|
|
726 |
// read on without looping |
// read on without looping |
727 |
if (samplestoread) do { |
if (samplestoread) do { |
728 |
readsamples = Read(&pDst[totalreadsamples * this->FrameSize], samplestoread); |
readsamples = Read(&pDst[totalreadsamples * this->FrameSize], samplestoread, pExternalDecompressionBuffer); |
729 |
samplestoread -= readsamples; |
samplestoread -= readsamples; |
730 |
totalreadsamples += readsamples; |
totalreadsamples += readsamples; |
731 |
} while (readsamples && samplestoread); |
} while (readsamples && samplestoread); |
744 |
* 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, |
745 |
* thus for disk streaming. |
* thus for disk streaming. |
746 |
* |
* |
747 |
|
* <b>Caution:</b> If you are using more than one streaming thread, you |
748 |
|
* have to use an external decompression buffer for <b>EACH</b> |
749 |
|
* streaming thread to avoid race conditions and crashes! |
750 |
|
* |
751 |
* @param pBuffer destination buffer |
* @param pBuffer destination buffer |
752 |
* @param SampleCount number of sample points to read |
* @param SampleCount number of sample points to read |
753 |
|
* @param pExternalDecompressionBuffer (optional) external buffer to use for decompression |
754 |
* @returns number of successfully read sample points |
* @returns number of successfully read sample points |
755 |
* @see SetPos() |
* @see SetPos(), CreateDecompressionBuffer() |
756 |
*/ |
*/ |
757 |
unsigned long Sample::Read(void* pBuffer, unsigned long SampleCount) { |
unsigned long Sample::Read(void* pBuffer, unsigned long SampleCount, buffer_t* pExternalDecompressionBuffer) { |
758 |
if (SampleCount == 0) return 0; |
if (SampleCount == 0) return 0; |
759 |
if (!Compressed) return pCkData->Read(pBuffer, SampleCount, FrameSize); //FIXME: channel inversion due to endian correction? |
if (!Compressed) { |
760 |
else { //FIXME: no support for mono compressed samples yet, are there any? |
if (BitDepth == 24) { |
761 |
|
// 24 bit sample. For now just truncate to 16 bit. |
762 |
|
unsigned char* pSrc = (unsigned char*) ((pExternalDecompressionBuffer) ? pExternalDecompressionBuffer->pStart : this->InternalDecompressionBuffer.pStart); |
763 |
|
int16_t* pDst = static_cast<int16_t*>(pBuffer); |
764 |
|
if (Channels == 2) { // Stereo |
765 |
|
unsigned long readBytes = pCkData->Read(pSrc, SampleCount * 6, 1); |
766 |
|
pSrc++; |
767 |
|
for (unsigned long i = readBytes ; i > 0 ; i -= 3) { |
768 |
|
*pDst++ = get16(pSrc); |
769 |
|
pSrc += 3; |
770 |
|
} |
771 |
|
return (pDst - static_cast<int16_t*>(pBuffer)) >> 1; |
772 |
|
} |
773 |
|
else { // Mono |
774 |
|
unsigned long readBytes = pCkData->Read(pSrc, SampleCount * 3, 1); |
775 |
|
pSrc++; |
776 |
|
for (unsigned long i = readBytes ; i > 0 ; i -= 3) { |
777 |
|
*pDst++ = get16(pSrc); |
778 |
|
pSrc += 3; |
779 |
|
} |
780 |
|
return pDst - static_cast<int16_t*>(pBuffer); |
781 |
|
} |
782 |
|
} |
783 |
|
else { // 16 bit |
784 |
|
// (pCkData->Read does endian correction) |
785 |
|
return Channels == 2 ? pCkData->Read(pBuffer, SampleCount << 1, 2) >> 1 |
786 |
|
: pCkData->Read(pBuffer, SampleCount, 2); |
787 |
|
} |
788 |
|
} |
789 |
|
else { |
790 |
if (this->SamplePos >= this->SamplesTotal) return 0; |
if (this->SamplePos >= this->SamplesTotal) return 0; |
791 |
//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 |
792 |
// 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 |
|
793 |
remainingbytes = 0, // remaining bytes in the local buffer |
remainingbytes = 0, // remaining bytes in the local buffer |
794 |
remainingsamples = SampleCount, |
remainingsamples = SampleCount, |
795 |
copysamples; |
copysamples, skipsamples, |
796 |
int currentframeoffset = this->FrameOffset; // offset in current sample frame since last Read() |
currentframeoffset = this->FrameOffset; // offset in current sample frame since last Read() |
797 |
this->FrameOffset = 0; |
this->FrameOffset = 0; |
798 |
|
|
799 |
if (assumedsize > this->DecompressionBufferSize) { |
buffer_t* pDecompressionBuffer = (pExternalDecompressionBuffer) ? pExternalDecompressionBuffer : &InternalDecompressionBuffer; |
800 |
// local buffer reallocation - hope this won't happen |
|
801 |
if (this->pDecompressionBuffer) delete[] (int8_t*) this->pDecompressionBuffer; |
// if decompression buffer too small, then reduce amount of samples to read |
802 |
this->pDecompressionBuffer = new int8_t[assumedsize << 1]; // double of current needed size |
if (pDecompressionBuffer->Size < assumedsize) { |
803 |
this->DecompressionBufferSize = assumedsize; |
std::cerr << "gig::Read(): WARNING - decompression buffer size too small!" << std::endl; |
804 |
|
SampleCount = WorstCaseMaxSamples(pDecompressionBuffer); |
805 |
|
remainingsamples = SampleCount; |
806 |
|
assumedsize = GuessSize(SampleCount); |
807 |
} |
} |
808 |
|
|
809 |
int16_t compressionmode, left, dleft, right, dright; |
unsigned char* pSrc = (unsigned char*) pDecompressionBuffer->pStart; |
810 |
int8_t* pSrc = (int8_t*) this->pDecompressionBuffer; |
int16_t* pDst = static_cast<int16_t*>(pBuffer); |
|
int16_t* pDst = (int16_t*) pBuffer; |
|
811 |
remainingbytes = pCkData->Read(pSrc, assumedsize, 1); |
remainingbytes = pCkData->Read(pSrc, assumedsize, 1); |
812 |
|
|
813 |
while (remainingsamples) { |
while (remainingsamples && remainingbytes) { |
814 |
|
unsigned long framesamples = SamplesPerFrame; |
815 |
// reload from disk to local buffer if needed |
unsigned long framebytes, rightChannelOffset = 0, nextFrameOffset; |
816 |
if (remainingbytes < 8194) { |
|
817 |
if (pCkData->GetState() != RIFF::stream_ready) { |
int mode_l = *pSrc++, mode_r = 0; |
818 |
this->SamplePos = this->SamplesTotal; |
|
819 |
return (SampleCount - remainingsamples); |
if (Channels == 2) { |
820 |
|
mode_r = *pSrc++; |
821 |
|
framebytes = bytesPerFrame[mode_l] + bytesPerFrame[mode_r] + 2; |
822 |
|
rightChannelOffset = bytesPerFrameNoHdr[mode_l]; |
823 |
|
nextFrameOffset = rightChannelOffset + bytesPerFrameNoHdr[mode_r]; |
824 |
|
if (remainingbytes < framebytes) { // last frame in sample |
825 |
|
framesamples = SamplesInLastFrame; |
826 |
|
if (mode_l == 4 && (framesamples & 1)) { |
827 |
|
rightChannelOffset = ((framesamples + 1) * bitsPerSample[mode_l]) >> 3; |
828 |
|
} |
829 |
|
else { |
830 |
|
rightChannelOffset = (framesamples * bitsPerSample[mode_l]) >> 3; |
831 |
|
} |
832 |
|
} |
833 |
|
} |
834 |
|
else { |
835 |
|
framebytes = bytesPerFrame[mode_l] + 1; |
836 |
|
nextFrameOffset = bytesPerFrameNoHdr[mode_l]; |
837 |
|
if (remainingbytes < framebytes) { |
838 |
|
framesamples = SamplesInLastFrame; |
839 |
} |
} |
|
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; |
|
840 |
} |
} |
841 |
|
|
842 |
// determine how many samples in this frame to skip and read |
// determine how many samples in this frame to skip and read |
843 |
if (remainingsamples >= 2048) { |
if (currentframeoffset + remainingsamples >= framesamples) { |
844 |
copysamples = 2048 - currentframeoffset; |
if (currentframeoffset <= framesamples) { |
845 |
remainingsamples -= copysamples; |
copysamples = framesamples - currentframeoffset; |
846 |
|
skipsamples = currentframeoffset; |
847 |
|
} |
848 |
|
else { |
849 |
|
copysamples = 0; |
850 |
|
skipsamples = framesamples; |
851 |
|
} |
852 |
} |
} |
853 |
else { |
else { |
854 |
|
// This frame has enough data for pBuffer, but not |
855 |
|
// all of the frame is needed. Set file position |
856 |
|
// to start of this frame for next call to Read. |
857 |
copysamples = remainingsamples; |
copysamples = remainingsamples; |
858 |
if (currentframeoffset + copysamples > 2048) { |
skipsamples = currentframeoffset; |
859 |
copysamples = 2048 - currentframeoffset; |
pCkData->SetPos(remainingbytes, RIFF::stream_backward); |
860 |
remainingsamples -= copysamples; |
this->FrameOffset = currentframeoffset + copysamples; |
861 |
} |
} |
862 |
else { |
remainingsamples -= copysamples; |
863 |
|
|
864 |
|
if (remainingbytes > framebytes) { |
865 |
|
remainingbytes -= framebytes; |
866 |
|
if (remainingsamples == 0 && |
867 |
|
currentframeoffset + copysamples == framesamples) { |
868 |
|
// This frame has enough data for pBuffer, and |
869 |
|
// all of the frame is needed. Set file |
870 |
|
// position to start of next frame for next |
871 |
|
// call to Read. FrameOffset is 0. |
872 |
pCkData->SetPos(remainingbytes, RIFF::stream_backward); |
pCkData->SetPos(remainingbytes, RIFF::stream_backward); |
|
remainingsamples = 0; |
|
|
this->FrameOffset = currentframeoffset + copysamples; |
|
873 |
} |
} |
874 |
} |
} |
875 |
|
else remainingbytes = 0; |
876 |
|
|
877 |
// decompress and copy current frame from local buffer to destination buffer |
currentframeoffset -= skipsamples; |
878 |
compressionmode = *(int16_t*)pSrc; pSrc+=2; |
|
879 |
switch (compressionmode) { |
if (copysamples == 0) { |
880 |
case 1: // left channel compressed |
// skip this frame |
881 |
remainingbytes -= 6150; // (left 8 bit, right 16 bit, +6 byte header) |
pSrc += framebytes - Channels; |
882 |
if (!remainingsamples && copysamples == 2048) |
} |
883 |
pCkData->SetPos(remainingbytes, RIFF::stream_backward); |
else { |
884 |
|
const unsigned char* const param_l = pSrc; |
885 |
left = *(int16_t*)pSrc; pSrc+=2; |
if (BitDepth == 24) { |
886 |
dleft = *(int16_t*)pSrc; pSrc+=2; |
if (mode_l != 2) pSrc += 12; |
887 |
while (currentframeoffset) { |
|
888 |
dleft -= *pSrc; |
if (Channels == 2) { // Stereo |
889 |
left -= dleft; |
const unsigned char* const param_r = pSrc; |
890 |
pSrc+=3; // 8 bit left channel, skip uncompressed right channel (16 bit) |
if (mode_r != 2) pSrc += 12; |
891 |
currentframeoffset--; |
|
892 |
} |
Decompress24(mode_l, param_l, 2, pSrc, pDst, |
893 |
while (copysamples) { |
skipsamples, copysamples, TruncatedBits); |
894 |
dleft -= *pSrc; pSrc++; |
Decompress24(mode_r, param_r, 2, pSrc + rightChannelOffset, pDst + 1, |
895 |
left -= dleft; |
skipsamples, copysamples, TruncatedBits); |
896 |
*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 |
|
897 |
} |
} |
898 |
while (copysamples) { |
else { // Mono |
899 |
*pDst = *(int16_t*)pSrc; pDst++; pSrc+=2; |
Decompress24(mode_l, param_l, 1, pSrc, pDst, |
900 |
dright -= *pSrc; pSrc++; |
skipsamples, copysamples, TruncatedBits); |
901 |
right -= dright; |
pDst += copysamples; |
|
*pDst = right; pDst++; |
|
|
copysamples--; |
|
902 |
} |
} |
903 |
break; |
} |
904 |
case 257: // both channels compressed |
else { // 16 bit |
905 |
remainingbytes -= 4106; // (left 8 bit, right 8 bit, +10 byte header) |
if (mode_l) pSrc += 4; |
906 |
if (!remainingsamples && copysamples == 2048) |
|
907 |
pCkData->SetPos(remainingbytes, RIFF::stream_backward); |
int step; |
908 |
|
if (Channels == 2) { // Stereo |
909 |
left = *(int16_t*)pSrc; pSrc+=2; |
const unsigned char* const param_r = pSrc; |
910 |
dleft = *(int16_t*)pSrc; pSrc+=2; |
if (mode_r) pSrc += 4; |
911 |
right = *(int16_t*)pSrc; pSrc+=2; |
|
912 |
dright = *(int16_t*)pSrc; pSrc+=2; |
step = (2 - mode_l) + (2 - mode_r); |
913 |
while (currentframeoffset) { |
Decompress16(mode_l, param_l, step, 2, pSrc, pDst, skipsamples, copysamples); |
914 |
dleft -= *pSrc; pSrc++; |
Decompress16(mode_r, param_r, step, 2, pSrc + (2 - mode_l), pDst + 1, |
915 |
left -= dleft; |
skipsamples, copysamples); |
916 |
dright -= *pSrc; pSrc++; |
pDst += copysamples << 1; |
|
right -= dright; |
|
|
currentframeoffset--; |
|
917 |
} |
} |
918 |
while (copysamples) { |
else { // Mono |
919 |
dleft -= *pSrc; pSrc++; |
step = 2 - mode_l; |
920 |
left -= dleft; |
Decompress16(mode_l, param_l, step, 1, pSrc, pDst, skipsamples, copysamples); |
921 |
dright -= *pSrc; pSrc++; |
pDst += copysamples; |
|
right -= dright; |
|
|
*pDst = left; pDst++; |
|
|
*pDst = right; pDst++; |
|
|
copysamples--; |
|
922 |
} |
} |
923 |
break; |
} |
924 |
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; |
|
925 |
} |
} |
926 |
} |
|
927 |
|
// reload from disk to local buffer if needed |
928 |
|
if (remainingsamples && remainingbytes < WorstCaseFrameSize && pCkData->GetState() == RIFF::stream_ready) { |
929 |
|
assumedsize = GuessSize(remainingsamples); |
930 |
|
pCkData->SetPos(remainingbytes, RIFF::stream_backward); |
931 |
|
if (pCkData->RemainingBytes() < assumedsize) assumedsize = pCkData->RemainingBytes(); |
932 |
|
remainingbytes = pCkData->Read(pDecompressionBuffer->pStart, assumedsize, 1); |
933 |
|
pSrc = (unsigned char*) pDecompressionBuffer->pStart; |
934 |
|
} |
935 |
|
} // while |
936 |
|
|
937 |
this->SamplePos += (SampleCount - remainingsamples); |
this->SamplePos += (SampleCount - remainingsamples); |
938 |
if (this->SamplePos > this->SamplesTotal) this->SamplePos = this->SamplesTotal; |
if (this->SamplePos > this->SamplesTotal) this->SamplePos = this->SamplesTotal; |
939 |
return (SampleCount - remainingsamples); |
return (SampleCount - remainingsamples); |
940 |
} |
} |
941 |
} |
} |
942 |
|
|
943 |
|
/** |
944 |
|
* Allocates a decompression buffer for streaming (compressed) samples |
945 |
|
* with Sample::Read(). If you are using more than one streaming thread |
946 |
|
* in your application you <b>HAVE</b> to create a decompression buffer |
947 |
|
* for <b>EACH</b> of your streaming threads and provide it with the |
948 |
|
* Sample::Read() call in order to avoid race conditions and crashes. |
949 |
|
* |
950 |
|
* You should free the memory occupied by the allocated buffer(s) once |
951 |
|
* you don't need one of your streaming threads anymore by calling |
952 |
|
* DestroyDecompressionBuffer(). |
953 |
|
* |
954 |
|
* @param MaxReadSize - the maximum size (in sample points) you ever |
955 |
|
* expect to read with one Read() call |
956 |
|
* @returns allocated decompression buffer |
957 |
|
* @see DestroyDecompressionBuffer() |
958 |
|
*/ |
959 |
|
buffer_t Sample::CreateDecompressionBuffer(unsigned long MaxReadSize) { |
960 |
|
buffer_t result; |
961 |
|
const double worstCaseHeaderOverhead = |
962 |
|
(256.0 /*frame size*/ + 12.0 /*header*/ + 2.0 /*compression type flag (stereo)*/) / 256.0; |
963 |
|
result.Size = (unsigned long) (double(MaxReadSize) * 3.0 /*(24 Bit)*/ * 2.0 /*stereo*/ * worstCaseHeaderOverhead); |
964 |
|
result.pStart = new int8_t[result.Size]; |
965 |
|
result.NullExtensionSize = 0; |
966 |
|
return result; |
967 |
|
} |
968 |
|
|
969 |
|
/** |
970 |
|
* Free decompression buffer, previously created with |
971 |
|
* CreateDecompressionBuffer(). |
972 |
|
* |
973 |
|
* @param DecompressionBuffer - previously allocated decompression |
974 |
|
* buffer to free |
975 |
|
*/ |
976 |
|
void Sample::DestroyDecompressionBuffer(buffer_t& DecompressionBuffer) { |
977 |
|
if (DecompressionBuffer.Size && DecompressionBuffer.pStart) { |
978 |
|
delete[] (int8_t*) DecompressionBuffer.pStart; |
979 |
|
DecompressionBuffer.pStart = NULL; |
980 |
|
DecompressionBuffer.Size = 0; |
981 |
|
DecompressionBuffer.NullExtensionSize = 0; |
982 |
|
} |
983 |
|
} |
984 |
|
|
985 |
Sample::~Sample() { |
Sample::~Sample() { |
986 |
Instances--; |
Instances--; |
987 |
if (!Instances && pDecompressionBuffer) delete[] (int8_t*) pDecompressionBuffer; |
if (!Instances && InternalDecompressionBuffer.Size) { |
988 |
|
delete[] (unsigned char*) InternalDecompressionBuffer.pStart; |
989 |
|
InternalDecompressionBuffer.pStart = NULL; |
990 |
|
InternalDecompressionBuffer.Size = 0; |
991 |
|
} |
992 |
if (FrameTable) delete[] FrameTable; |
if (FrameTable) delete[] FrameTable; |
993 |
if (RAMCache.pStart) delete[] (int8_t*) RAMCache.pStart; |
if (RAMCache.pStart) delete[] (int8_t*) RAMCache.pStart; |
994 |
} |
} |
1109 |
LFO3Controller = static_cast<lfo3_ctrl_t>(lfo3ctrl & 0x07); // lower 3 bits |
LFO3Controller = static_cast<lfo3_ctrl_t>(lfo3ctrl & 0x07); // lower 3 bits |
1110 |
LFO3Sync = lfo3ctrl & 0x20; // bit 5 |
LFO3Sync = lfo3ctrl & 0x20; // bit 5 |
1111 |
InvertAttenuationController = lfo3ctrl & 0x80; // bit 7 |
InvertAttenuationController = lfo3ctrl & 0x80; // bit 7 |
|
if (VCFType == vcf_type_lowpass) { |
|
|
if (lfo3ctrl & 0x40) // bit 6 |
|
|
VCFType = vcf_type_lowpassturbo; |
|
|
} |
|
1112 |
AttenuationController = DecodeLeverageController(static_cast<_lev_ctrl_t>(_3ewa->ReadUint8())); |
AttenuationController = DecodeLeverageController(static_cast<_lev_ctrl_t>(_3ewa->ReadUint8())); |
1113 |
uint8_t lfo2ctrl = _3ewa->ReadUint8(); |
uint8_t lfo2ctrl = _3ewa->ReadUint8(); |
1114 |
LFO2Controller = static_cast<lfo2_ctrl_t>(lfo2ctrl & 0x07); // lower 3 bits |
LFO2Controller = static_cast<lfo2_ctrl_t>(lfo2ctrl & 0x07); // lower 3 bits |
1153 |
VCFVelocityDynamicRange = vcfvelocity % 5; |
VCFVelocityDynamicRange = vcfvelocity % 5; |
1154 |
VCFVelocityCurve = static_cast<curve_type_t>(vcfvelocity / 5); |
VCFVelocityCurve = static_cast<curve_type_t>(vcfvelocity / 5); |
1155 |
VCFType = static_cast<vcf_type_t>(_3ewa->ReadUint8()); |
VCFType = static_cast<vcf_type_t>(_3ewa->ReadUint8()); |
1156 |
|
if (VCFType == vcf_type_lowpass) { |
1157 |
|
if (lfo3ctrl & 0x40) // bit 6 |
1158 |
|
VCFType = vcf_type_lowpassturbo; |
1159 |
|
} |
1160 |
|
|
1161 |
// 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 |
1162 |
uint32_t tableKey = (VelocityResponseCurve<<16) | (VelocityResponseDepth<<8) | VelocityResponseCurveScaling; |
uint32_t tableKey = (VelocityResponseCurve<<16) | (VelocityResponseDepth<<8) | VelocityResponseCurveScaling; |
1164 |
pVelocityAttenuationTable = (*pVelocityTables)[tableKey]; |
pVelocityAttenuationTable = (*pVelocityTables)[tableKey]; |
1165 |
} |
} |
1166 |
else { |
else { |
1167 |
pVelocityAttenuationTable = new double[128]; |
pVelocityAttenuationTable = |
1168 |
switch (VelocityResponseCurve) { // calculate the new table |
CreateVelocityTable(VelocityResponseCurve, |
1169 |
case curve_type_nonlinear: |
VelocityResponseDepth, |
1170 |
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."); |
|
|
} |
|
1171 |
(*pVelocityTables)[tableKey] = pVelocityAttenuationTable; // put the new table into the tables map |
(*pVelocityTables)[tableKey] = pVelocityAttenuationTable; // put the new table into the tables map |
1172 |
} |
} |
1173 |
|
|
1174 |
|
SampleAttenuation = pow(10.0, -Gain / (20.0 * 655360)); |
1175 |
} |
} |
1176 |
|
|
1177 |
leverage_ctrl_t DimensionRegion::DecodeLeverageController(_lev_ctrl_t EncodedController) { |
leverage_ctrl_t DimensionRegion::DecodeLeverageController(_lev_ctrl_t EncodedController) { |
1322 |
return pVelocityAttenuationTable[MIDIKeyVelocity]; |
return pVelocityAttenuationTable[MIDIKeyVelocity]; |
1323 |
} |
} |
1324 |
|
|
1325 |
|
double* DimensionRegion::CreateVelocityTable(curve_type_t curveType, uint8_t depth, uint8_t scaling) { |
1326 |
|
|
1327 |
|
// line-segment approximations of the 15 velocity curves |
1328 |
|
|
1329 |
|
// linear |
1330 |
|
const int lin0[] = { 1, 1, 127, 127 }; |
1331 |
|
const int lin1[] = { 1, 21, 127, 127 }; |
1332 |
|
const int lin2[] = { 1, 45, 127, 127 }; |
1333 |
|
const int lin3[] = { 1, 74, 127, 127 }; |
1334 |
|
const int lin4[] = { 1, 127, 127, 127 }; |
1335 |
|
|
1336 |
|
// non-linear |
1337 |
|
const int non0[] = { 1, 4, 24, 5, 57, 17, 92, 57, 122, 127, 127, 127 }; |
1338 |
|
const int non1[] = { 1, 4, 46, 9, 93, 56, 118, 106, 123, 127, |
1339 |
|
127, 127 }; |
1340 |
|
const int non2[] = { 1, 4, 46, 9, 57, 20, 102, 107, 107, 127, |
1341 |
|
127, 127 }; |
1342 |
|
const int non3[] = { 1, 15, 10, 19, 67, 73, 80, 80, 90, 98, 98, 127, |
1343 |
|
127, 127 }; |
1344 |
|
const int non4[] = { 1, 25, 33, 57, 82, 81, 92, 127, 127, 127 }; |
1345 |
|
|
1346 |
|
// special |
1347 |
|
const int spe0[] = { 1, 2, 76, 10, 90, 15, 95, 20, 99, 28, 103, 44, |
1348 |
|
113, 127, 127, 127 }; |
1349 |
|
const int spe1[] = { 1, 2, 27, 5, 67, 18, 89, 29, 95, 35, 107, 67, |
1350 |
|
118, 127, 127, 127 }; |
1351 |
|
const int spe2[] = { 1, 1, 33, 1, 53, 5, 61, 13, 69, 32, 79, 74, |
1352 |
|
85, 90, 91, 127, 127, 127 }; |
1353 |
|
const int spe3[] = { 1, 32, 28, 35, 66, 48, 89, 59, 95, 65, 99, 73, |
1354 |
|
117, 127, 127, 127 }; |
1355 |
|
const int spe4[] = { 1, 4, 23, 5, 49, 13, 57, 17, 92, 57, 122, 127, |
1356 |
|
127, 127 }; |
1357 |
|
|
1358 |
|
const int* const curves[] = { non0, non1, non2, non3, non4, |
1359 |
|
lin0, lin1, lin2, lin3, lin4, |
1360 |
|
spe0, spe1, spe2, spe3, spe4 }; |
1361 |
|
|
1362 |
|
double* const table = new double[128]; |
1363 |
|
|
1364 |
|
const int* curve = curves[curveType * 5 + depth]; |
1365 |
|
const int s = scaling == 0 ? 20 : scaling; // 0 or 20 means no scaling |
1366 |
|
|
1367 |
|
table[0] = 0; |
1368 |
|
for (int x = 1 ; x < 128 ; x++) { |
1369 |
|
|
1370 |
|
if (x > curve[2]) curve += 2; |
1371 |
|
double y = curve[1] + (x - curve[0]) * |
1372 |
|
(double(curve[3] - curve[1]) / (curve[2] - curve[0])); |
1373 |
|
y = y / 127; |
1374 |
|
|
1375 |
|
// Scale up for s > 20, down for s < 20. When |
1376 |
|
// down-scaling, the curve still ends at 1.0. |
1377 |
|
if (s < 20 && y >= 0.5) |
1378 |
|
y = y / ((2 - 40.0 / s) * y + 40.0 / s - 1); |
1379 |
|
else |
1380 |
|
y = y * (s / 20.0); |
1381 |
|
if (y > 1) y = 1; |
1382 |
|
|
1383 |
|
table[x] = y; |
1384 |
|
} |
1385 |
|
return table; |
1386 |
|
} |
1387 |
|
|
1388 |
|
|
1389 |
// *************** Region *************** |
// *************** Region *************** |
1392 |
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) { |
1393 |
// Initialization |
// Initialization |
1394 |
Dimensions = 0; |
Dimensions = 0; |
1395 |
for (int i = 0; i < 32; i++) { |
for (int i = 0; i < 256; i++) { |
1396 |
pDimensionRegions[i] = NULL; |
pDimensionRegions[i] = NULL; |
1397 |
} |
} |
1398 |
Layers = 1; |
Layers = 1; |
1399 |
|
File* file = (File*) GetParent()->GetParent(); |
1400 |
|
int dimensionBits = (file->pVersion && file->pVersion->major == 3) ? 8 : 5; |
1401 |
|
|
1402 |
// Actual Loading |
// Actual Loading |
1403 |
|
|
1406 |
RIFF::Chunk* _3lnk = rgnList->GetSubChunk(CHUNK_ID_3LNK); |
RIFF::Chunk* _3lnk = rgnList->GetSubChunk(CHUNK_ID_3LNK); |
1407 |
if (_3lnk) { |
if (_3lnk) { |
1408 |
DimensionRegions = _3lnk->ReadUint32(); |
DimensionRegions = _3lnk->ReadUint32(); |
1409 |
for (int i = 0; i < 5; i++) { |
for (int i = 0; i < dimensionBits; i++) { |
1410 |
dimension_t dimension = static_cast<dimension_t>(_3lnk->ReadUint8()); |
dimension_t dimension = static_cast<dimension_t>(_3lnk->ReadUint8()); |
1411 |
uint8_t bits = _3lnk->ReadUint8(); |
uint8_t bits = _3lnk->ReadUint8(); |
1412 |
if (dimension == dimension_none) { // inactive dimension |
if (dimension == dimension_none) { // inactive dimension |
1423 |
pDimensionDefinitions[i].zones = 0x01 << bits; // = pow(2,bits) |
pDimensionDefinitions[i].zones = 0x01 << bits; // = pow(2,bits) |
1424 |
pDimensionDefinitions[i].split_type = (dimension == dimension_layer || |
pDimensionDefinitions[i].split_type = (dimension == dimension_layer || |
1425 |
dimension == dimension_samplechannel || |
dimension == dimension_samplechannel || |
1426 |
dimension == dimension_releasetrigger) ? split_type_bit |
dimension == dimension_releasetrigger || |
1427 |
: split_type_normal; |
dimension == dimension_roundrobin || |
1428 |
|
dimension == dimension_random) ? split_type_bit |
1429 |
|
: split_type_normal; |
1430 |
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 |
1431 |
pDimensionDefinitions[i].zone_size = |
pDimensionDefinitions[i].zone_size = |
1432 |
(pDimensionDefinitions[i].split_type == split_type_normal) ? 128 / pDimensionDefinitions[i].zones |
(pDimensionDefinitions[i].split_type == split_type_normal) ? 128 / pDimensionDefinitions[i].zones |
1451 |
else { // custom defined ranges |
else { // custom defined ranges |
1452 |
pDimDef->split_type = split_type_customvelocity; |
pDimDef->split_type = split_type_customvelocity; |
1453 |
pDimDef->ranges = new range_t[pDimDef->zones]; |
pDimDef->ranges = new range_t[pDimDef->zones]; |
1454 |
unsigned int bits[5] = {0,0,0,0,0}; |
uint8_t bits[8] = { 0 }; |
1455 |
int previousUpperLimit = -1; |
int previousUpperLimit = -1; |
1456 |
for (int velocityZone = 0; velocityZone < pDimDef->zones; velocityZone++) { |
for (int velocityZone = 0; velocityZone < pDimDef->zones; velocityZone++) { |
1457 |
bits[i] = velocityZone; |
bits[i] = velocityZone; |
1458 |
DimensionRegion* pDimRegion = GetDimensionRegionByBit(bits[4],bits[3],bits[2],bits[1],bits[0]); |
DimensionRegion* pDimRegion = GetDimensionRegionByBit(bits); |
1459 |
|
|
1460 |
pDimDef->ranges[velocityZone].low = previousUpperLimit + 1; |
pDimDef->ranges[velocityZone].low = previousUpperLimit + 1; |
1461 |
pDimDef->ranges[velocityZone].high = pDimRegion->VelocityUpperLimit; |
pDimDef->ranges[velocityZone].high = pDimRegion->VelocityUpperLimit; |
1469 |
} |
} |
1470 |
} |
} |
1471 |
|
|
1472 |
|
// jump to start of the wave pool indices (if not already there) |
1473 |
|
File* file = (File*) GetParent()->GetParent(); |
1474 |
|
if (file->pVersion && file->pVersion->major == 3) |
1475 |
|
_3lnk->SetPos(68); // version 3 has a different 3lnk structure |
1476 |
|
else |
1477 |
|
_3lnk->SetPos(44); |
1478 |
|
|
1479 |
// load sample references |
// load sample references |
|
_3lnk->SetPos(44); // jump to start of the wave pool indices (if not already there) |
|
1480 |
for (uint i = 0; i < DimensionRegions; i++) { |
for (uint i = 0; i < DimensionRegions; i++) { |
1481 |
uint32_t wavepoolindex = _3lnk->ReadUint32(); |
uint32_t wavepoolindex = _3lnk->ReadUint32(); |
1482 |
pDimensionRegions[i]->pSample = GetSampleFromWavePool(wavepoolindex); |
pDimensionRegions[i]->pSample = GetSampleFromWavePool(wavepoolindex); |
1505 |
for (uint i = 0; i < Dimensions; i++) { |
for (uint i = 0; i < Dimensions; i++) { |
1506 |
if (pDimensionDefinitions[i].ranges) delete[] pDimensionDefinitions[i].ranges; |
if (pDimensionDefinitions[i].ranges) delete[] pDimensionDefinitions[i].ranges; |
1507 |
} |
} |
1508 |
for (int i = 0; i < 32; i++) { |
for (int i = 0; i < 256; i++) { |
1509 |
if (pDimensionRegions[i]) delete pDimensionRegions[i]; |
if (pDimensionRegions[i]) delete pDimensionRegions[i]; |
1510 |
} |
} |
1511 |
} |
} |
1523 |
* 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, |
1524 |
* etc.). |
* etc.). |
1525 |
* |
* |
1526 |
* @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 |
|
1527 |
* @returns adress to the DimensionRegion for the given situation |
* @returns adress to the DimensionRegion for the given situation |
1528 |
* @see pDimensionDefinitions |
* @see pDimensionDefinitions |
1529 |
* @see Dimensions |
* @see Dimensions |
1530 |
*/ |
*/ |
1531 |
DimensionRegion* Region::GetDimensionRegionByValue(uint Dim4Val, uint Dim3Val, uint Dim2Val, uint Dim1Val, uint Dim0Val) { |
DimensionRegion* Region::GetDimensionRegionByValue(const uint DimValues[8]) { |
1532 |
uint8_t bits[5] = {Dim0Val,Dim1Val,Dim2Val,Dim3Val,Dim4Val}; |
uint8_t bits[8] = { 0 }; |
1533 |
for (uint i = 0; i < Dimensions; i++) { |
for (uint i = 0; i < Dimensions; i++) { |
1534 |
|
bits[i] = DimValues[i]; |
1535 |
switch (pDimensionDefinitions[i].split_type) { |
switch (pDimensionDefinitions[i].split_type) { |
1536 |
case split_type_normal: |
case split_type_normal: |
1537 |
bits[i] /= pDimensionDefinitions[i].zone_size; |
bits[i] /= pDimensionDefinitions[i].zone_size; |
1545 |
break; |
break; |
1546 |
} |
} |
1547 |
} |
} |
1548 |
return GetDimensionRegionByBit(bits[4],bits[3],bits[2],bits[1],bits[0]); |
return GetDimensionRegionByBit(bits); |
1549 |
} |
} |
1550 |
|
|
1551 |
/** |
/** |
1553 |
* numbers (zone index). You usually use <i>GetDimensionRegionByValue</i> |
* numbers (zone index). You usually use <i>GetDimensionRegionByValue</i> |
1554 |
* instead of calling this method directly! |
* instead of calling this method directly! |
1555 |
* |
* |
1556 |
* @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 |
|
1557 |
* @returns adress to the DimensionRegion for the given dimension |
* @returns adress to the DimensionRegion for the given dimension |
1558 |
* bit numbers |
* bit numbers |
1559 |
* @see GetDimensionRegionByValue() |
* @see GetDimensionRegionByValue() |
1560 |
*/ |
*/ |
1561 |
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]) { |
1562 |
return *(pDimensionRegions + ((((((((Dim4Bit << pDimensionDefinitions[3].bits) | Dim3Bit) |
return pDimensionRegions[((((((DimBits[7] << pDimensionDefinitions[6].bits | DimBits[6]) |
1563 |
<< pDimensionDefinitions[2].bits) | Dim2Bit) |
<< pDimensionDefinitions[5].bits | DimBits[5]) |
1564 |
<< pDimensionDefinitions[1].bits) | Dim1Bit) |
<< pDimensionDefinitions[4].bits | DimBits[4]) |
1565 |
<< pDimensionDefinitions[0].bits) | Dim0Bit) ); |
<< pDimensionDefinitions[3].bits | DimBits[3]) |
1566 |
|
<< pDimensionDefinitions[2].bits | DimBits[2]) |
1567 |
|
<< pDimensionDefinitions[1].bits | DimBits[1]) |
1568 |
|
<< pDimensionDefinitions[0].bits | DimBits[0]]; |
1569 |
} |
} |
1570 |
|
|
1571 |
/** |
/** |
1582 |
else return static_cast<gig::Sample*>(pSample = GetSampleFromWavePool(WavePoolTableIndex)); |
else return static_cast<gig::Sample*>(pSample = GetSampleFromWavePool(WavePoolTableIndex)); |
1583 |
} |
} |
1584 |
|
|
1585 |
Sample* Region::GetSampleFromWavePool(unsigned int WavePoolTableIndex) { |
Sample* Region::GetSampleFromWavePool(unsigned int WavePoolTableIndex, progress_t* pProgress) { |
1586 |
|
if ((int32_t)WavePoolTableIndex == -1) return NULL; |
1587 |
File* file = (File*) GetParent()->GetParent(); |
File* file = (File*) GetParent()->GetParent(); |
1588 |
unsigned long soughtoffset = file->pWavePoolTable[WavePoolTableIndex]; |
unsigned long soughtoffset = file->pWavePoolTable[WavePoolTableIndex]; |
1589 |
Sample* sample = file->GetFirstSample(); |
Sample* sample = file->GetFirstSample(pProgress); |
1590 |
while (sample) { |
while (sample) { |
1591 |
if (sample->ulWavePoolOffset == soughtoffset) return static_cast<gig::Sample*>(pSample = sample); |
if (sample->ulWavePoolOffset == soughtoffset) return static_cast<gig::Sample*>(pSample = sample); |
1592 |
sample = file->GetNextSample(); |
sample = file->GetNextSample(); |
1599 |
// *************** Instrument *************** |
// *************** Instrument *************** |
1600 |
// * |
// * |
1601 |
|
|
1602 |
Instrument::Instrument(File* pFile, RIFF::List* insList) : DLS::Instrument((DLS::File*)pFile, insList) { |
Instrument::Instrument(File* pFile, RIFF::List* insList, progress_t* pProgress) : DLS::Instrument((DLS::File*)pFile, insList) { |
1603 |
// Initialization |
// Initialization |
1604 |
for (int i = 0; i < 128; i++) RegionKeyTable[i] = NULL; |
for (int i = 0; i < 128; i++) RegionKeyTable[i] = NULL; |
1605 |
RegionIndex = -1; |
RegionIndex = -1; |
1625 |
RIFF::List* lrgn = insList->GetSubList(LIST_TYPE_LRGN); |
RIFF::List* lrgn = insList->GetSubList(LIST_TYPE_LRGN); |
1626 |
if (!lrgn) throw gig::Exception("Mandatory chunks in <ins > chunk not found."); |
if (!lrgn) throw gig::Exception("Mandatory chunks in <ins > chunk not found."); |
1627 |
pRegions = new Region*[Regions]; |
pRegions = new Region*[Regions]; |
1628 |
|
for (uint i = 0; i < Regions; i++) pRegions[i] = NULL; |
1629 |
RIFF::List* rgn = lrgn->GetFirstSubList(); |
RIFF::List* rgn = lrgn->GetFirstSubList(); |
1630 |
unsigned int iRegion = 0; |
unsigned int iRegion = 0; |
1631 |
while (rgn) { |
while (rgn) { |
1632 |
if (rgn->GetListType() == LIST_TYPE_RGN) { |
if (rgn->GetListType() == LIST_TYPE_RGN) { |
1633 |
|
__notify_progress(pProgress, (float) iRegion / (float) Regions); |
1634 |
pRegions[iRegion] = new Region(this, rgn); |
pRegions[iRegion] = new Region(this, rgn); |
1635 |
iRegion++; |
iRegion++; |
1636 |
} |
} |
1643 |
RegionKeyTable[iKey] = pRegions[iReg]; |
RegionKeyTable[iKey] = pRegions[iReg]; |
1644 |
} |
} |
1645 |
} |
} |
1646 |
|
|
1647 |
|
__notify_progress(pProgress, 1.0f); // notify done |
1648 |
} |
} |
1649 |
|
|
1650 |
Instrument::~Instrument() { |
Instrument::~Instrument() { |
1652 |
if (pRegions) { |
if (pRegions) { |
1653 |
if (pRegions[i]) delete (pRegions[i]); |
if (pRegions[i]) delete (pRegions[i]); |
1654 |
} |
} |
|
delete[] pRegions; |
|
1655 |
} |
} |
1656 |
|
if (pRegions) delete[] pRegions; |
1657 |
} |
} |
1658 |
|
|
1659 |
/** |
/** |
1695 |
* @see GetFirstRegion() |
* @see GetFirstRegion() |
1696 |
*/ |
*/ |
1697 |
Region* Instrument::GetNextRegion() { |
Region* Instrument::GetNextRegion() { |
1698 |
if (RegionIndex < 0 || RegionIndex >= Regions) return NULL; |
if (RegionIndex < 0 || uint32_t(RegionIndex) >= Regions) return NULL; |
1699 |
return pRegions[RegionIndex++]; |
return pRegions[RegionIndex++]; |
1700 |
} |
} |
1701 |
|
|
1709 |
pInstruments = NULL; |
pInstruments = NULL; |
1710 |
} |
} |
1711 |
|
|
1712 |
Sample* File::GetFirstSample() { |
File::~File() { |
1713 |
if (!pSamples) LoadSamples(); |
// free samples |
1714 |
|
if (pSamples) { |
1715 |
|
SamplesIterator = pSamples->begin(); |
1716 |
|
while (SamplesIterator != pSamples->end() ) { |
1717 |
|
delete (*SamplesIterator); |
1718 |
|
SamplesIterator++; |
1719 |
|
} |
1720 |
|
pSamples->clear(); |
1721 |
|
delete pSamples; |
1722 |
|
|
1723 |
|
} |
1724 |
|
// free instruments |
1725 |
|
if (pInstruments) { |
1726 |
|
InstrumentsIterator = pInstruments->begin(); |
1727 |
|
while (InstrumentsIterator != pInstruments->end() ) { |
1728 |
|
delete (*InstrumentsIterator); |
1729 |
|
InstrumentsIterator++; |
1730 |
|
} |
1731 |
|
pInstruments->clear(); |
1732 |
|
delete pInstruments; |
1733 |
|
} |
1734 |
|
} |
1735 |
|
|
1736 |
|
Sample* File::GetFirstSample(progress_t* pProgress) { |
1737 |
|
if (!pSamples) LoadSamples(pProgress); |
1738 |
if (!pSamples) return NULL; |
if (!pSamples) return NULL; |
1739 |
SamplesIterator = pSamples->begin(); |
SamplesIterator = pSamples->begin(); |
1740 |
return static_cast<gig::Sample*>( (SamplesIterator != pSamples->end()) ? *SamplesIterator : NULL ); |
return static_cast<gig::Sample*>( (SamplesIterator != pSamples->end()) ? *SamplesIterator : NULL ); |
1746 |
return static_cast<gig::Sample*>( (SamplesIterator != pSamples->end()) ? *SamplesIterator : NULL ); |
return static_cast<gig::Sample*>( (SamplesIterator != pSamples->end()) ? *SamplesIterator : NULL ); |
1747 |
} |
} |
1748 |
|
|
1749 |
void File::LoadSamples() { |
void File::LoadSamples(progress_t* pProgress) { |
1750 |
RIFF::List* wvpl = pRIFF->GetSubList(LIST_TYPE_WVPL); |
RIFF::List* wvpl = pRIFF->GetSubList(LIST_TYPE_WVPL); |
1751 |
if (wvpl) { |
if (wvpl) { |
1752 |
|
// just for progress calculation |
1753 |
|
int iSampleIndex = 0; |
1754 |
|
int iTotalSamples = wvpl->CountSubLists(LIST_TYPE_WAVE); |
1755 |
|
|
1756 |
unsigned long wvplFileOffset = wvpl->GetFilePos(); |
unsigned long wvplFileOffset = wvpl->GetFilePos(); |
1757 |
RIFF::List* wave = wvpl->GetFirstSubList(); |
RIFF::List* wave = wvpl->GetFirstSubList(); |
1758 |
while (wave) { |
while (wave) { |
1759 |
if (wave->GetListType() == LIST_TYPE_WAVE) { |
if (wave->GetListType() == LIST_TYPE_WAVE) { |
1760 |
|
// notify current progress |
1761 |
|
const float subprogress = (float) iSampleIndex / (float) iTotalSamples; |
1762 |
|
__notify_progress(pProgress, subprogress); |
1763 |
|
|
1764 |
if (!pSamples) pSamples = new SampleList; |
if (!pSamples) pSamples = new SampleList; |
1765 |
unsigned long waveFileOffset = wave->GetFilePos(); |
unsigned long waveFileOffset = wave->GetFilePos(); |
1766 |
pSamples->push_back(new Sample(this, wave, waveFileOffset - wvplFileOffset)); |
pSamples->push_back(new Sample(this, wave, waveFileOffset - wvplFileOffset)); |
1767 |
|
|
1768 |
|
iSampleIndex++; |
1769 |
} |
} |
1770 |
wave = wvpl->GetNextSubList(); |
wave = wvpl->GetNextSubList(); |
1771 |
} |
} |
1772 |
|
__notify_progress(pProgress, 1.0); // notify done |
1773 |
} |
} |
1774 |
else throw gig::Exception("Mandatory <wvpl> chunk not found."); |
else throw gig::Exception("Mandatory <wvpl> chunk not found."); |
1775 |
} |
} |
1790 |
/** |
/** |
1791 |
* Returns the instrument with the given index. |
* Returns the instrument with the given index. |
1792 |
* |
* |
1793 |
|
* @param index - number of the sought instrument (0..n) |
1794 |
|
* @param pProgress - optional: callback function for progress notification |
1795 |
* @returns sought instrument or NULL if there's no such instrument |
* @returns sought instrument or NULL if there's no such instrument |
1796 |
*/ |
*/ |
1797 |
Instrument* File::GetInstrument(uint index) { |
Instrument* File::GetInstrument(uint index, progress_t* pProgress) { |
1798 |
if (!pInstruments) LoadInstruments(); |
if (!pInstruments) { |
1799 |
|
// TODO: hack - we simply load ALL samples here, it would have been done in the Region constructor anyway (ATM) |
1800 |
|
|
1801 |
|
// sample loading subtask |
1802 |
|
progress_t subprogress; |
1803 |
|
__divide_progress(pProgress, &subprogress, 3.0f, 0.0f); // randomly schedule 33% for this subtask |
1804 |
|
__notify_progress(&subprogress, 0.0f); |
1805 |
|
GetFirstSample(&subprogress); // now force all samples to be loaded |
1806 |
|
__notify_progress(&subprogress, 1.0f); |
1807 |
|
|
1808 |
|
// instrument loading subtask |
1809 |
|
if (pProgress && pProgress->callback) { |
1810 |
|
subprogress.__range_min = subprogress.__range_max; |
1811 |
|
subprogress.__range_max = pProgress->__range_max; // schedule remaining percentage for this subtask |
1812 |
|
} |
1813 |
|
__notify_progress(&subprogress, 0.0f); |
1814 |
|
LoadInstruments(&subprogress); |
1815 |
|
__notify_progress(&subprogress, 1.0f); |
1816 |
|
} |
1817 |
if (!pInstruments) return NULL; |
if (!pInstruments) return NULL; |
1818 |
InstrumentsIterator = pInstruments->begin(); |
InstrumentsIterator = pInstruments->begin(); |
1819 |
for (uint i = 0; InstrumentsIterator != pInstruments->end(); i++) { |
for (uint i = 0; InstrumentsIterator != pInstruments->end(); i++) { |
1823 |
return NULL; |
return NULL; |
1824 |
} |
} |
1825 |
|
|
1826 |
void File::LoadInstruments() { |
void File::LoadInstruments(progress_t* pProgress) { |
1827 |
RIFF::List* lstInstruments = pRIFF->GetSubList(LIST_TYPE_LINS); |
RIFF::List* lstInstruments = pRIFF->GetSubList(LIST_TYPE_LINS); |
1828 |
if (lstInstruments) { |
if (lstInstruments) { |
1829 |
|
int iInstrumentIndex = 0; |
1830 |
RIFF::List* lstInstr = lstInstruments->GetFirstSubList(); |
RIFF::List* lstInstr = lstInstruments->GetFirstSubList(); |
1831 |
while (lstInstr) { |
while (lstInstr) { |
1832 |
if (lstInstr->GetListType() == LIST_TYPE_INS) { |
if (lstInstr->GetListType() == LIST_TYPE_INS) { |
1833 |
|
// notify current progress |
1834 |
|
const float localProgress = (float) iInstrumentIndex / (float) Instruments; |
1835 |
|
__notify_progress(pProgress, localProgress); |
1836 |
|
|
1837 |
|
// divide local progress into subprogress for loading current Instrument |
1838 |
|
progress_t subprogress; |
1839 |
|
__divide_progress(pProgress, &subprogress, Instruments, iInstrumentIndex); |
1840 |
|
|
1841 |
if (!pInstruments) pInstruments = new InstrumentList; |
if (!pInstruments) pInstruments = new InstrumentList; |
1842 |
pInstruments->push_back(new Instrument(this, lstInstr)); |
pInstruments->push_back(new Instrument(this, lstInstr, &subprogress)); |
1843 |
|
|
1844 |
|
iInstrumentIndex++; |
1845 |
} |
} |
1846 |
lstInstr = lstInstruments->GetNextSubList(); |
lstInstr = lstInstruments->GetNextSubList(); |
1847 |
} |
} |
1848 |
|
__notify_progress(pProgress, 1.0); // notify done |
1849 |
} |
} |
1850 |
else throw gig::Exception("Mandatory <lins> list chunk not found."); |
else throw gig::Exception("Mandatory <lins> list chunk not found."); |
1851 |
} |
} |