1 |
schoenebeck |
2 |
/*************************************************************************** |
2 |
|
|
* * |
3 |
|
|
* libgig - C++ cross-platform Gigasampler format file loader library * |
4 |
|
|
* * |
5 |
schoenebeck |
384 |
* Copyright (C) 2003-2005 by Christian Schoenebeck * |
6 |
|
|
* <cuse@users.sourceforge.net> * |
7 |
schoenebeck |
2 |
* * |
8 |
|
|
* 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 * |
10 |
|
|
* the Free Software Foundation; either version 2 of the License, or * |
11 |
|
|
* (at your option) any later version. * |
12 |
|
|
* * |
13 |
|
|
* This library is distributed in the hope that it will be useful, * |
14 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
15 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
16 |
|
|
* GNU General Public License for more details. * |
17 |
|
|
* * |
18 |
|
|
* You should have received a copy of the GNU General Public License * |
19 |
|
|
* along with this library; if not, write to the Free Software * |
20 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, * |
21 |
|
|
* MA 02111-1307 USA * |
22 |
|
|
***************************************************************************/ |
23 |
|
|
|
24 |
|
|
#include "gig.h" |
25 |
|
|
|
26 |
schoenebeck |
384 |
#include <iostream> |
27 |
|
|
|
28 |
persson |
365 |
namespace gig { namespace { |
29 |
schoenebeck |
2 |
|
30 |
persson |
365 |
// *************** 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 |
persson |
372 |
int srcStep, int dstStep, |
58 |
|
|
const unsigned char* pSrc, int16_t* pDst, |
59 |
persson |
365 |
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 |
persson |
372 |
pDst += dstStep; |
68 |
persson |
365 |
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 |
persson |
372 |
pDst += dstStep; |
87 |
persson |
365 |
pSrc += srcStep; |
88 |
|
|
copysamples--; |
89 |
|
|
} |
90 |
|
|
break; |
91 |
|
|
} |
92 |
|
|
} |
93 |
|
|
|
94 |
|
|
void Decompress24(int compressionmode, const unsigned char* params, |
95 |
persson |
372 |
int dstStep, const unsigned char* pSrc, int16_t* pDst, |
96 |
persson |
365 |
unsigned long currentframeoffset, |
97 |
persson |
437 |
unsigned long copysamples, int truncatedBits) |
98 |
persson |
365 |
{ |
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 |
persson |
437 |
// COPY_ONE shifts an extra step, and also why y is |
110 |
persson |
365 |
// initialized with a sum instead of a mean value. |
111 |
|
|
|
112 |
|
|
int y, dy, ddy; |
113 |
|
|
|
114 |
persson |
437 |
const int shift = 8 - truncatedBits; |
115 |
|
|
const int shift1 = shift + 1; |
116 |
|
|
|
117 |
persson |
365 |
#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 |
persson |
437 |
*pDst = y >> shift1; \ |
130 |
persson |
372 |
pDst += dstStep |
131 |
persson |
365 |
|
132 |
|
|
switch (compressionmode) { |
133 |
|
|
case 2: // 24 bit uncompressed |
134 |
|
|
pSrc += currentframeoffset * 3; |
135 |
|
|
while (copysamples) { |
136 |
persson |
437 |
*pDst = get24(pSrc) >> shift; |
137 |
persson |
372 |
pDst += dstStep; |
138 |
persson |
365 |
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 |
schoenebeck |
2 |
// *************** Sample *************** |
207 |
|
|
// * |
208 |
|
|
|
209 |
schoenebeck |
384 |
unsigned int Sample::Instances = 0; |
210 |
|
|
buffer_t Sample::InternalDecompressionBuffer; |
211 |
schoenebeck |
2 |
|
212 |
|
|
Sample::Sample(File* pFile, RIFF::List* waveList, unsigned long WavePoolOffset) : DLS::Sample((DLS::File*) pFile, waveList, WavePoolOffset) { |
213 |
|
|
Instances++; |
214 |
|
|
|
215 |
|
|
RIFF::Chunk* _3gix = waveList->GetSubChunk(CHUNK_ID_3GIX); |
216 |
|
|
if (!_3gix) throw gig::Exception("Mandatory chunks in <wave> list chunk not found."); |
217 |
|
|
SampleGroup = _3gix->ReadInt16(); |
218 |
|
|
|
219 |
|
|
RIFF::Chunk* smpl = waveList->GetSubChunk(CHUNK_ID_SMPL); |
220 |
|
|
if (!smpl) throw gig::Exception("Mandatory chunks in <wave> list chunk not found."); |
221 |
|
|
Manufacturer = smpl->ReadInt32(); |
222 |
|
|
Product = smpl->ReadInt32(); |
223 |
|
|
SamplePeriod = smpl->ReadInt32(); |
224 |
|
|
MIDIUnityNote = smpl->ReadInt32(); |
225 |
schoenebeck |
21 |
FineTune = smpl->ReadInt32(); |
226 |
schoenebeck |
2 |
smpl->Read(&SMPTEFormat, 1, 4); |
227 |
|
|
SMPTEOffset = smpl->ReadInt32(); |
228 |
|
|
Loops = smpl->ReadInt32(); |
229 |
persson |
365 |
smpl->ReadInt32(); // manufByt |
230 |
schoenebeck |
2 |
LoopID = smpl->ReadInt32(); |
231 |
|
|
smpl->Read(&LoopType, 1, 4); |
232 |
|
|
LoopStart = smpl->ReadInt32(); |
233 |
|
|
LoopEnd = smpl->ReadInt32(); |
234 |
|
|
LoopFraction = smpl->ReadInt32(); |
235 |
|
|
LoopPlayCount = smpl->ReadInt32(); |
236 |
|
|
|
237 |
|
|
FrameTable = NULL; |
238 |
|
|
SamplePos = 0; |
239 |
|
|
RAMCache.Size = 0; |
240 |
|
|
RAMCache.pStart = NULL; |
241 |
|
|
RAMCache.NullExtensionSize = 0; |
242 |
|
|
|
243 |
persson |
365 |
if (BitDepth > 24) throw gig::Exception("Only samples up to 24 bit supported"); |
244 |
|
|
|
245 |
persson |
437 |
RIFF::Chunk* ewav = waveList->GetSubChunk(CHUNK_ID_EWAV); |
246 |
|
|
Compressed = ewav; |
247 |
|
|
Dithered = false; |
248 |
|
|
TruncatedBits = 0; |
249 |
schoenebeck |
2 |
if (Compressed) { |
250 |
persson |
437 |
uint32_t version = ewav->ReadInt32(); |
251 |
|
|
if (version == 3 && BitDepth == 24) { |
252 |
|
|
Dithered = ewav->ReadInt32(); |
253 |
|
|
ewav->SetPos(Channels == 2 ? 84 : 64); |
254 |
|
|
TruncatedBits = ewav->ReadInt32(); |
255 |
|
|
} |
256 |
schoenebeck |
2 |
ScanCompressedSample(); |
257 |
|
|
} |
258 |
schoenebeck |
317 |
|
259 |
|
|
// we use a buffer for decompression and for truncating 24 bit samples to 16 bit |
260 |
schoenebeck |
384 |
if ((Compressed || BitDepth == 24) && !InternalDecompressionBuffer.Size) { |
261 |
|
|
InternalDecompressionBuffer.pStart = new unsigned char[INITIAL_SAMPLE_BUFFER_SIZE]; |
262 |
|
|
InternalDecompressionBuffer.Size = INITIAL_SAMPLE_BUFFER_SIZE; |
263 |
schoenebeck |
317 |
} |
264 |
persson |
437 |
FrameOffset = 0; // just for streaming compressed samples |
265 |
schoenebeck |
21 |
|
266 |
schoenebeck |
27 |
LoopSize = LoopEnd - LoopStart; |
267 |
schoenebeck |
2 |
} |
268 |
|
|
|
269 |
|
|
/// Scans compressed samples for mandatory informations (e.g. actual number of total sample points). |
270 |
|
|
void Sample::ScanCompressedSample() { |
271 |
|
|
//TODO: we have to add some more scans here (e.g. determine compression rate) |
272 |
|
|
this->SamplesTotal = 0; |
273 |
|
|
std::list<unsigned long> frameOffsets; |
274 |
|
|
|
275 |
persson |
365 |
SamplesPerFrame = BitDepth == 24 ? 256 : 2048; |
276 |
schoenebeck |
384 |
WorstCaseFrameSize = SamplesPerFrame * FrameSize + Channels; // +Channels for compression flag |
277 |
persson |
365 |
|
278 |
schoenebeck |
2 |
// Scanning |
279 |
|
|
pCkData->SetPos(0); |
280 |
persson |
365 |
if (Channels == 2) { // Stereo |
281 |
|
|
for (int i = 0 ; ; i++) { |
282 |
|
|
// for 24 bit samples every 8:th frame offset is |
283 |
|
|
// stored, to save some memory |
284 |
|
|
if (BitDepth != 24 || (i & 7) == 0) frameOffsets.push_back(pCkData->GetPos()); |
285 |
|
|
|
286 |
|
|
const int mode_l = pCkData->ReadUint8(); |
287 |
|
|
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 |
schoenebeck |
2 |
break; |
297 |
persson |
365 |
} |
298 |
|
|
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 |
schoenebeck |
2 |
break; |
315 |
persson |
365 |
} |
316 |
|
|
SamplesTotal += SamplesPerFrame; |
317 |
|
|
pCkData->SetPos(frameSize, RIFF::stream_curpos); |
318 |
schoenebeck |
2 |
} |
319 |
|
|
} |
320 |
|
|
pCkData->SetPos(0); |
321 |
|
|
|
322 |
|
|
// Build the frames table (which is used for fast resolving of a frame's chunk offset) |
323 |
|
|
if (FrameTable) delete[] FrameTable; |
324 |
|
|
FrameTable = new unsigned long[frameOffsets.size()]; |
325 |
|
|
std::list<unsigned long>::iterator end = frameOffsets.end(); |
326 |
|
|
std::list<unsigned long>::iterator iter = frameOffsets.begin(); |
327 |
|
|
for (int i = 0; iter != end; i++, iter++) { |
328 |
|
|
FrameTable[i] = *iter; |
329 |
|
|
} |
330 |
|
|
} |
331 |
|
|
|
332 |
|
|
/** |
333 |
|
|
* Loads (and uncompresses if needed) the whole sample wave into RAM. Use |
334 |
|
|
* ReleaseSampleData() to free the memory if you don't need the cached |
335 |
|
|
* sample data anymore. |
336 |
|
|
* |
337 |
|
|
* @returns buffer_t structure with start address and size of the buffer |
338 |
|
|
* in bytes |
339 |
|
|
* @see ReleaseSampleData(), Read(), SetPos() |
340 |
|
|
*/ |
341 |
|
|
buffer_t Sample::LoadSampleData() { |
342 |
|
|
return LoadSampleDataWithNullSamplesExtension(this->SamplesTotal, 0); // 0 amount of NullSamples |
343 |
|
|
} |
344 |
|
|
|
345 |
|
|
/** |
346 |
|
|
* Reads (uncompresses if needed) and caches the first \a SampleCount |
347 |
|
|
* numbers of SamplePoints in RAM. Use ReleaseSampleData() to free the |
348 |
|
|
* memory space if you don't need the cached samples anymore. There is no |
349 |
|
|
* guarantee that exactly \a SampleCount samples will be cached; this is |
350 |
|
|
* not an error. The size will be eventually truncated e.g. to the |
351 |
|
|
* beginning of a frame of a compressed sample. This is done for |
352 |
|
|
* efficiency reasons while streaming the wave by your sampler engine |
353 |
|
|
* later. Read the <i>Size</i> member of the <i>buffer_t</i> structure |
354 |
|
|
* 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 |
356 |
|
|
* samples by dividing it by the frame size of the sample: |
357 |
schoenebeck |
384 |
* @code |
358 |
schoenebeck |
2 |
* buffer_t buf = pSample->LoadSampleData(acquired_samples); |
359 |
|
|
* long cachedsamples = buf.Size / pSample->FrameSize; |
360 |
schoenebeck |
384 |
* @endcode |
361 |
schoenebeck |
2 |
* |
362 |
|
|
* @param SampleCount - number of sample points to load into RAM |
363 |
|
|
* @returns buffer_t structure with start address and size of |
364 |
|
|
* the cached sample data in bytes |
365 |
|
|
* @see ReleaseSampleData(), Read(), SetPos() |
366 |
|
|
*/ |
367 |
|
|
buffer_t Sample::LoadSampleData(unsigned long SampleCount) { |
368 |
|
|
return LoadSampleDataWithNullSamplesExtension(SampleCount, 0); // 0 amount of NullSamples |
369 |
|
|
} |
370 |
|
|
|
371 |
|
|
/** |
372 |
|
|
* Loads (and uncompresses if needed) the whole sample wave into RAM. Use |
373 |
|
|
* ReleaseSampleData() to free the memory if you don't need the cached |
374 |
|
|
* sample data anymore. |
375 |
|
|
* The method will add \a NullSamplesCount silence samples past the |
376 |
|
|
* official buffer end (this won't affect the 'Size' member of the |
377 |
|
|
* buffer_t structure, that means 'Size' always reflects the size of the |
378 |
|
|
* actual sample data, the buffer might be bigger though). Silence |
379 |
|
|
* samples past the official buffer are needed for differential |
380 |
|
|
* algorithms that always have to take subsequent samples into account |
381 |
|
|
* (resampling/interpolation would be an important example) and avoids |
382 |
|
|
* memory access faults in such cases. |
383 |
|
|
* |
384 |
|
|
* @param NullSamplesCount - number of silence samples the buffer should |
385 |
|
|
* be extended past it's data end |
386 |
|
|
* @returns buffer_t structure with start address and |
387 |
|
|
* size of the buffer in bytes |
388 |
|
|
* @see ReleaseSampleData(), Read(), SetPos() |
389 |
|
|
*/ |
390 |
|
|
buffer_t Sample::LoadSampleDataWithNullSamplesExtension(uint NullSamplesCount) { |
391 |
|
|
return LoadSampleDataWithNullSamplesExtension(this->SamplesTotal, NullSamplesCount); |
392 |
|
|
} |
393 |
|
|
|
394 |
|
|
/** |
395 |
|
|
* Reads (uncompresses if needed) and caches the first \a SampleCount |
396 |
|
|
* numbers of SamplePoints in RAM. Use ReleaseSampleData() to free the |
397 |
|
|
* memory space if you don't need the cached samples anymore. There is no |
398 |
|
|
* guarantee that exactly \a SampleCount samples will be cached; this is |
399 |
|
|
* not an error. The size will be eventually truncated e.g. to the |
400 |
|
|
* beginning of a frame of a compressed sample. This is done for |
401 |
|
|
* efficiency reasons while streaming the wave by your sampler engine |
402 |
|
|
* later. Read the <i>Size</i> member of the <i>buffer_t</i> structure |
403 |
|
|
* 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 |
405 |
|
|
* samples by dividing it by the frame size of the sample: |
406 |
schoenebeck |
384 |
* @code |
407 |
schoenebeck |
2 |
* buffer_t buf = pSample->LoadSampleDataWithNullSamplesExtension(acquired_samples, null_samples); |
408 |
|
|
* long cachedsamples = buf.Size / pSample->FrameSize; |
409 |
schoenebeck |
384 |
* @endcode |
410 |
schoenebeck |
2 |
* The method will add \a NullSamplesCount silence samples past the |
411 |
|
|
* 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 |
413 |
|
|
* actual sample data, the buffer might be bigger though). Silence |
414 |
|
|
* samples past the official buffer are needed for differential |
415 |
|
|
* algorithms that always have to take subsequent samples into account |
416 |
|
|
* (resampling/interpolation would be an important example) and avoids |
417 |
|
|
* memory access faults in such cases. |
418 |
|
|
* |
419 |
|
|
* @param SampleCount - number of sample points to load into RAM |
420 |
|
|
* @param NullSamplesCount - number of silence samples the buffer should |
421 |
|
|
* be extended past it's data end |
422 |
|
|
* @returns buffer_t structure with start address and |
423 |
|
|
* size of the cached sample data in bytes |
424 |
|
|
* @see ReleaseSampleData(), Read(), SetPos() |
425 |
|
|
*/ |
426 |
|
|
buffer_t Sample::LoadSampleDataWithNullSamplesExtension(unsigned long SampleCount, uint NullSamplesCount) { |
427 |
|
|
if (SampleCount > this->SamplesTotal) SampleCount = this->SamplesTotal; |
428 |
|
|
if (RAMCache.pStart) delete[] (int8_t*) RAMCache.pStart; |
429 |
|
|
unsigned long allocationsize = (SampleCount + NullSamplesCount) * this->FrameSize; |
430 |
|
|
RAMCache.pStart = new int8_t[allocationsize]; |
431 |
|
|
RAMCache.Size = Read(RAMCache.pStart, SampleCount) * this->FrameSize; |
432 |
|
|
RAMCache.NullExtensionSize = allocationsize - RAMCache.Size; |
433 |
|
|
// fill the remaining buffer space with silence samples |
434 |
|
|
memset((int8_t*)RAMCache.pStart + RAMCache.Size, 0, RAMCache.NullExtensionSize); |
435 |
|
|
return GetCache(); |
436 |
|
|
} |
437 |
|
|
|
438 |
|
|
/** |
439 |
|
|
* Returns current cached sample points. A buffer_t structure will be |
440 |
|
|
* returned which contains address pointer to the begin of the cache and |
441 |
|
|
* the size of the cached sample data in bytes. Use |
442 |
|
|
* <i>LoadSampleData()</i> to cache a specific amount of sample points in |
443 |
|
|
* RAM. |
444 |
|
|
* |
445 |
|
|
* @returns buffer_t structure with current cached sample points |
446 |
|
|
* @see LoadSampleData(); |
447 |
|
|
*/ |
448 |
|
|
buffer_t Sample::GetCache() { |
449 |
|
|
// return a copy of the buffer_t structure |
450 |
|
|
buffer_t result; |
451 |
|
|
result.Size = this->RAMCache.Size; |
452 |
|
|
result.pStart = this->RAMCache.pStart; |
453 |
|
|
result.NullExtensionSize = this->RAMCache.NullExtensionSize; |
454 |
|
|
return result; |
455 |
|
|
} |
456 |
|
|
|
457 |
|
|
/** |
458 |
|
|
* Frees the cached sample from RAM if loaded with |
459 |
|
|
* <i>LoadSampleData()</i> previously. |
460 |
|
|
* |
461 |
|
|
* @see LoadSampleData(); |
462 |
|
|
*/ |
463 |
|
|
void Sample::ReleaseSampleData() { |
464 |
|
|
if (RAMCache.pStart) delete[] (int8_t*) RAMCache.pStart; |
465 |
|
|
RAMCache.pStart = NULL; |
466 |
|
|
RAMCache.Size = 0; |
467 |
|
|
} |
468 |
|
|
|
469 |
|
|
/** |
470 |
|
|
* Sets the position within the sample (in sample points, not in |
471 |
|
|
* bytes). Use this method and <i>Read()</i> if you don't want to load |
472 |
|
|
* the sample into RAM, thus for disk streaming. |
473 |
|
|
* |
474 |
|
|
* Although the original Gigasampler engine doesn't allow positioning |
475 |
|
|
* within compressed samples, I decided to implement it. Even though |
476 |
|
|
* the Gigasampler format doesn't allow to define loops for compressed |
477 |
|
|
* samples at the moment, positioning within compressed samples might be |
478 |
|
|
* interesting for some sampler engines though. The only drawback about |
479 |
|
|
* my decision is that it takes longer to load compressed gig Files on |
480 |
|
|
* startup, because it's neccessary to scan the samples for some |
481 |
|
|
* mandatory informations. But I think as it doesn't affect the runtime |
482 |
|
|
* efficiency, nobody will have a problem with that. |
483 |
|
|
* |
484 |
|
|
* @param SampleCount number of sample points to jump |
485 |
|
|
* @param Whence optional: to which relation \a SampleCount refers |
486 |
|
|
* to, if omited <i>RIFF::stream_start</i> is assumed |
487 |
|
|
* @returns the new sample position |
488 |
|
|
* @see Read() |
489 |
|
|
*/ |
490 |
|
|
unsigned long Sample::SetPos(unsigned long SampleCount, RIFF::stream_whence_t Whence) { |
491 |
|
|
if (Compressed) { |
492 |
|
|
switch (Whence) { |
493 |
|
|
case RIFF::stream_curpos: |
494 |
|
|
this->SamplePos += SampleCount; |
495 |
|
|
break; |
496 |
|
|
case RIFF::stream_end: |
497 |
|
|
this->SamplePos = this->SamplesTotal - 1 - SampleCount; |
498 |
|
|
break; |
499 |
|
|
case RIFF::stream_backward: |
500 |
|
|
this->SamplePos -= SampleCount; |
501 |
|
|
break; |
502 |
|
|
case RIFF::stream_start: default: |
503 |
|
|
this->SamplePos = SampleCount; |
504 |
|
|
break; |
505 |
|
|
} |
506 |
|
|
if (this->SamplePos > this->SamplesTotal) this->SamplePos = this->SamplesTotal; |
507 |
|
|
|
508 |
|
|
unsigned long frame = this->SamplePos / 2048; // to which frame to jump |
509 |
|
|
this->FrameOffset = this->SamplePos % 2048; // offset (in sample points) within that frame |
510 |
|
|
pCkData->SetPos(FrameTable[frame]); // set chunk pointer to the start of sought frame |
511 |
|
|
return this->SamplePos; |
512 |
|
|
} |
513 |
|
|
else { // not compressed |
514 |
|
|
unsigned long orderedBytes = SampleCount * this->FrameSize; |
515 |
|
|
unsigned long result = pCkData->SetPos(orderedBytes, Whence); |
516 |
|
|
return (result == orderedBytes) ? SampleCount |
517 |
|
|
: result / this->FrameSize; |
518 |
|
|
} |
519 |
|
|
} |
520 |
|
|
|
521 |
|
|
/** |
522 |
|
|
* Returns the current position in the sample (in sample points). |
523 |
|
|
*/ |
524 |
|
|
unsigned long Sample::GetPos() { |
525 |
|
|
if (Compressed) return SamplePos; |
526 |
|
|
else return pCkData->GetPos() / FrameSize; |
527 |
|
|
} |
528 |
|
|
|
529 |
|
|
/** |
530 |
schoenebeck |
24 |
* Reads \a SampleCount number of sample points from the position stored |
531 |
|
|
* in \a pPlaybackState into the buffer pointed by \a pBuffer and moves |
532 |
|
|
* the position within the sample respectively, this method honors the |
533 |
|
|
* looping informations of the sample (if any). The sample wave stream |
534 |
|
|
* will be decompressed on the fly if using a compressed sample. Use this |
535 |
|
|
* method if you don't want to load the sample into RAM, thus for disk |
536 |
|
|
* streaming. All this methods needs to know to proceed with streaming |
537 |
|
|
* 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 |
539 |
|
|
* yourself before you use it to stream a sample: |
540 |
schoenebeck |
384 |
* @code |
541 |
|
|
* gig::playback_state_t playbackstate; |
542 |
|
|
* playbackstate.position = 0; |
543 |
|
|
* playbackstate.reverse = false; |
544 |
|
|
* playbackstate.loop_cycles_left = pSample->LoopPlayCount; |
545 |
|
|
* @endcode |
546 |
schoenebeck |
24 |
* 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. |
548 |
|
|
* The method already handles such cases by itself. |
549 |
|
|
* |
550 |
schoenebeck |
384 |
* <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 |
schoenebeck |
24 |
* @param pBuffer destination buffer |
555 |
|
|
* @param SampleCount number of sample points to read |
556 |
|
|
* @param pPlaybackState will be used to store and reload the playback |
557 |
|
|
* state for the next ReadAndLoop() call |
558 |
schoenebeck |
384 |
* @param pExternalDecompressionBuffer (optional) external buffer to use for decompression |
559 |
schoenebeck |
24 |
* @returns number of successfully read sample points |
560 |
schoenebeck |
384 |
* @see CreateDecompressionBuffer() |
561 |
schoenebeck |
24 |
*/ |
562 |
schoenebeck |
384 |
unsigned long Sample::ReadAndLoop(void* pBuffer, unsigned long SampleCount, playback_state_t* pPlaybackState, buffer_t* pExternalDecompressionBuffer) { |
563 |
schoenebeck |
24 |
unsigned long samplestoread = SampleCount, totalreadsamples = 0, readsamples, samplestoloopend; |
564 |
|
|
uint8_t* pDst = (uint8_t*) pBuffer; |
565 |
|
|
|
566 |
|
|
SetPos(pPlaybackState->position); // recover position from the last time |
567 |
|
|
|
568 |
|
|
if (this->Loops && GetPos() <= this->LoopEnd) { // honor looping if there are loop points defined |
569 |
|
|
|
570 |
|
|
switch (this->LoopType) { |
571 |
|
|
|
572 |
|
|
case loop_type_bidirectional: { //TODO: not tested yet! |
573 |
|
|
do { |
574 |
|
|
// if not endless loop check if max. number of loop cycles have been passed |
575 |
|
|
if (this->LoopPlayCount && !pPlaybackState->loop_cycles_left) break; |
576 |
|
|
|
577 |
|
|
if (!pPlaybackState->reverse) { // forward playback |
578 |
|
|
do { |
579 |
|
|
samplestoloopend = this->LoopEnd - GetPos(); |
580 |
schoenebeck |
384 |
readsamples = Read(&pDst[totalreadsamples * this->FrameSize], Min(samplestoread, samplestoloopend), pExternalDecompressionBuffer); |
581 |
schoenebeck |
24 |
samplestoread -= readsamples; |
582 |
|
|
totalreadsamples += readsamples; |
583 |
|
|
if (readsamples == samplestoloopend) { |
584 |
|
|
pPlaybackState->reverse = true; |
585 |
|
|
break; |
586 |
|
|
} |
587 |
|
|
} while (samplestoread && readsamples); |
588 |
|
|
} |
589 |
|
|
else { // backward playback |
590 |
|
|
|
591 |
|
|
// as we can only read forward from disk, we have to |
592 |
|
|
// determine the end position within the loop first, |
593 |
|
|
// read forward from that 'end' and finally after |
594 |
|
|
// reading, swap all sample frames so it reflects |
595 |
|
|
// backward playback |
596 |
|
|
|
597 |
|
|
unsigned long swapareastart = totalreadsamples; |
598 |
|
|
unsigned long loopoffset = GetPos() - this->LoopStart; |
599 |
|
|
unsigned long samplestoreadinloop = Min(samplestoread, loopoffset); |
600 |
|
|
unsigned long reverseplaybackend = GetPos() - samplestoreadinloop; |
601 |
|
|
|
602 |
|
|
SetPos(reverseplaybackend); |
603 |
|
|
|
604 |
|
|
// read samples for backward playback |
605 |
|
|
do { |
606 |
schoenebeck |
384 |
readsamples = Read(&pDst[totalreadsamples * this->FrameSize], samplestoreadinloop, pExternalDecompressionBuffer); |
607 |
schoenebeck |
24 |
samplestoreadinloop -= readsamples; |
608 |
|
|
samplestoread -= readsamples; |
609 |
|
|
totalreadsamples += readsamples; |
610 |
|
|
} while (samplestoreadinloop && readsamples); |
611 |
|
|
|
612 |
|
|
SetPos(reverseplaybackend); // pretend we really read backwards |
613 |
|
|
|
614 |
|
|
if (reverseplaybackend == this->LoopStart) { |
615 |
|
|
pPlaybackState->loop_cycles_left--; |
616 |
|
|
pPlaybackState->reverse = false; |
617 |
|
|
} |
618 |
|
|
|
619 |
|
|
// reverse the sample frames for backward playback |
620 |
|
|
SwapMemoryArea(&pDst[swapareastart * this->FrameSize], (totalreadsamples - swapareastart) * this->FrameSize, this->FrameSize); |
621 |
|
|
} |
622 |
|
|
} while (samplestoread && readsamples); |
623 |
|
|
break; |
624 |
|
|
} |
625 |
|
|
|
626 |
|
|
case loop_type_backward: { // TODO: not tested yet! |
627 |
|
|
// forward playback (not entered the loop yet) |
628 |
|
|
if (!pPlaybackState->reverse) do { |
629 |
|
|
samplestoloopend = this->LoopEnd - GetPos(); |
630 |
schoenebeck |
384 |
readsamples = Read(&pDst[totalreadsamples * this->FrameSize], Min(samplestoread, samplestoloopend), pExternalDecompressionBuffer); |
631 |
schoenebeck |
24 |
samplestoread -= readsamples; |
632 |
|
|
totalreadsamples += readsamples; |
633 |
|
|
if (readsamples == samplestoloopend) { |
634 |
|
|
pPlaybackState->reverse = true; |
635 |
|
|
break; |
636 |
|
|
} |
637 |
|
|
} while (samplestoread && readsamples); |
638 |
|
|
|
639 |
|
|
if (!samplestoread) break; |
640 |
|
|
|
641 |
|
|
// as we can only read forward from disk, we have to |
642 |
|
|
// determine the end position within the loop first, |
643 |
|
|
// read forward from that 'end' and finally after |
644 |
|
|
// reading, swap all sample frames so it reflects |
645 |
|
|
// backward playback |
646 |
|
|
|
647 |
|
|
unsigned long swapareastart = totalreadsamples; |
648 |
|
|
unsigned long loopoffset = GetPos() - this->LoopStart; |
649 |
|
|
unsigned long samplestoreadinloop = (this->LoopPlayCount) ? Min(samplestoread, pPlaybackState->loop_cycles_left * LoopSize - loopoffset) |
650 |
|
|
: samplestoread; |
651 |
|
|
unsigned long reverseplaybackend = this->LoopStart + Abs((loopoffset - samplestoreadinloop) % this->LoopSize); |
652 |
|
|
|
653 |
|
|
SetPos(reverseplaybackend); |
654 |
|
|
|
655 |
|
|
// read samples for backward playback |
656 |
|
|
do { |
657 |
|
|
// if not endless loop check if max. number of loop cycles have been passed |
658 |
|
|
if (this->LoopPlayCount && !pPlaybackState->loop_cycles_left) break; |
659 |
|
|
samplestoloopend = this->LoopEnd - GetPos(); |
660 |
schoenebeck |
384 |
readsamples = Read(&pDst[totalreadsamples * this->FrameSize], Min(samplestoreadinloop, samplestoloopend), pExternalDecompressionBuffer); |
661 |
schoenebeck |
24 |
samplestoreadinloop -= readsamples; |
662 |
|
|
samplestoread -= readsamples; |
663 |
|
|
totalreadsamples += readsamples; |
664 |
|
|
if (readsamples == samplestoloopend) { |
665 |
|
|
pPlaybackState->loop_cycles_left--; |
666 |
|
|
SetPos(this->LoopStart); |
667 |
|
|
} |
668 |
|
|
} while (samplestoreadinloop && readsamples); |
669 |
|
|
|
670 |
|
|
SetPos(reverseplaybackend); // pretend we really read backwards |
671 |
|
|
|
672 |
|
|
// reverse the sample frames for backward playback |
673 |
|
|
SwapMemoryArea(&pDst[swapareastart * this->FrameSize], (totalreadsamples - swapareastart) * this->FrameSize, this->FrameSize); |
674 |
|
|
break; |
675 |
|
|
} |
676 |
|
|
|
677 |
|
|
default: case loop_type_normal: { |
678 |
|
|
do { |
679 |
|
|
// if not endless loop check if max. number of loop cycles have been passed |
680 |
|
|
if (this->LoopPlayCount && !pPlaybackState->loop_cycles_left) break; |
681 |
|
|
samplestoloopend = this->LoopEnd - GetPos(); |
682 |
schoenebeck |
384 |
readsamples = Read(&pDst[totalreadsamples * this->FrameSize], Min(samplestoread, samplestoloopend), pExternalDecompressionBuffer); |
683 |
schoenebeck |
24 |
samplestoread -= readsamples; |
684 |
|
|
totalreadsamples += readsamples; |
685 |
|
|
if (readsamples == samplestoloopend) { |
686 |
|
|
pPlaybackState->loop_cycles_left--; |
687 |
|
|
SetPos(this->LoopStart); |
688 |
|
|
} |
689 |
|
|
} while (samplestoread && readsamples); |
690 |
|
|
break; |
691 |
|
|
} |
692 |
|
|
} |
693 |
|
|
} |
694 |
|
|
|
695 |
|
|
// read on without looping |
696 |
|
|
if (samplestoread) do { |
697 |
schoenebeck |
384 |
readsamples = Read(&pDst[totalreadsamples * this->FrameSize], samplestoread, pExternalDecompressionBuffer); |
698 |
schoenebeck |
24 |
samplestoread -= readsamples; |
699 |
|
|
totalreadsamples += readsamples; |
700 |
|
|
} while (readsamples && samplestoread); |
701 |
|
|
|
702 |
|
|
// store current position |
703 |
|
|
pPlaybackState->position = GetPos(); |
704 |
|
|
|
705 |
|
|
return totalreadsamples; |
706 |
|
|
} |
707 |
|
|
|
708 |
|
|
/** |
709 |
schoenebeck |
2 |
* Reads \a SampleCount number of sample points from the current |
710 |
|
|
* position into the buffer pointed by \a pBuffer and increments the |
711 |
|
|
* position within the sample. The sample wave stream will be |
712 |
|
|
* decompressed on the fly if using a compressed sample. Use this method |
713 |
|
|
* and <i>SetPos()</i> if you don't want to load the sample into RAM, |
714 |
|
|
* thus for disk streaming. |
715 |
|
|
* |
716 |
schoenebeck |
384 |
* <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 |
schoenebeck |
2 |
* @param pBuffer destination buffer |
721 |
|
|
* @param SampleCount number of sample points to read |
722 |
schoenebeck |
384 |
* @param pExternalDecompressionBuffer (optional) external buffer to use for decompression |
723 |
schoenebeck |
2 |
* @returns number of successfully read sample points |
724 |
schoenebeck |
384 |
* @see SetPos(), CreateDecompressionBuffer() |
725 |
schoenebeck |
2 |
*/ |
726 |
schoenebeck |
384 |
unsigned long Sample::Read(void* pBuffer, unsigned long SampleCount, buffer_t* pExternalDecompressionBuffer) { |
727 |
schoenebeck |
21 |
if (SampleCount == 0) return 0; |
728 |
schoenebeck |
317 |
if (!Compressed) { |
729 |
|
|
if (BitDepth == 24) { |
730 |
|
|
// 24 bit sample. For now just truncate to 16 bit. |
731 |
schoenebeck |
384 |
unsigned char* pSrc = (unsigned char*) ((pExternalDecompressionBuffer) ? pExternalDecompressionBuffer->pStart : this->InternalDecompressionBuffer.pStart); |
732 |
persson |
365 |
int16_t* pDst = static_cast<int16_t*>(pBuffer); |
733 |
|
|
if (Channels == 2) { // Stereo |
734 |
|
|
unsigned long readBytes = pCkData->Read(pSrc, SampleCount * 6, 1); |
735 |
schoenebeck |
317 |
pSrc++; |
736 |
persson |
365 |
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 |
schoenebeck |
317 |
} |
742 |
persson |
365 |
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 |
schoenebeck |
317 |
} |
752 |
persson |
365 |
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 |
schoenebeck |
317 |
} |
758 |
persson |
365 |
else { |
759 |
schoenebeck |
11 |
if (this->SamplePos >= this->SamplesTotal) return 0; |
760 |
persson |
365 |
//TODO: efficiency: maybe we should test for an average compression rate |
761 |
|
|
unsigned long assumedsize = GuessSize(SampleCount), |
762 |
schoenebeck |
2 |
remainingbytes = 0, // remaining bytes in the local buffer |
763 |
|
|
remainingsamples = SampleCount, |
764 |
persson |
365 |
copysamples, skipsamples, |
765 |
|
|
currentframeoffset = this->FrameOffset; // offset in current sample frame since last Read() |
766 |
schoenebeck |
2 |
this->FrameOffset = 0; |
767 |
|
|
|
768 |
schoenebeck |
384 |
buffer_t* pDecompressionBuffer = (pExternalDecompressionBuffer) ? pExternalDecompressionBuffer : &InternalDecompressionBuffer; |
769 |
|
|
|
770 |
|
|
// if decompression buffer too small, then reduce amount of samples to read |
771 |
|
|
if (pDecompressionBuffer->Size < assumedsize) { |
772 |
|
|
std::cerr << "gig::Read(): WARNING - decompression buffer size too small!" << std::endl; |
773 |
|
|
SampleCount = WorstCaseMaxSamples(pDecompressionBuffer); |
774 |
|
|
remainingsamples = SampleCount; |
775 |
|
|
assumedsize = GuessSize(SampleCount); |
776 |
schoenebeck |
2 |
} |
777 |
|
|
|
778 |
schoenebeck |
384 |
unsigned char* pSrc = (unsigned char*) pDecompressionBuffer->pStart; |
779 |
persson |
365 |
int16_t* pDst = static_cast<int16_t*>(pBuffer); |
780 |
schoenebeck |
2 |
remainingbytes = pCkData->Read(pSrc, assumedsize, 1); |
781 |
|
|
|
782 |
persson |
365 |
while (remainingsamples && remainingbytes) { |
783 |
|
|
unsigned long framesamples = SamplesPerFrame; |
784 |
|
|
unsigned long framebytes, rightChannelOffset = 0, nextFrameOffset; |
785 |
schoenebeck |
2 |
|
786 |
persson |
365 |
int mode_l = *pSrc++, mode_r = 0; |
787 |
|
|
|
788 |
|
|
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 |
schoenebeck |
2 |
} |
802 |
|
|
} |
803 |
persson |
365 |
else { |
804 |
|
|
framebytes = bytesPerFrame[mode_l] + 1; |
805 |
|
|
nextFrameOffset = bytesPerFrameNoHdr[mode_l]; |
806 |
|
|
if (remainingbytes < framebytes) { |
807 |
|
|
framesamples = SamplesInLastFrame; |
808 |
|
|
} |
809 |
|
|
} |
810 |
schoenebeck |
2 |
|
811 |
|
|
// determine how many samples in this frame to skip and read |
812 |
persson |
365 |
if (currentframeoffset + remainingsamples >= framesamples) { |
813 |
|
|
if (currentframeoffset <= framesamples) { |
814 |
|
|
copysamples = framesamples - currentframeoffset; |
815 |
|
|
skipsamples = currentframeoffset; |
816 |
|
|
} |
817 |
|
|
else { |
818 |
|
|
copysamples = 0; |
819 |
|
|
skipsamples = framesamples; |
820 |
|
|
} |
821 |
schoenebeck |
2 |
} |
822 |
|
|
else { |
823 |
persson |
365 |
// 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 |
schoenebeck |
2 |
copysamples = remainingsamples; |
827 |
persson |
365 |
skipsamples = currentframeoffset; |
828 |
|
|
pCkData->SetPos(remainingbytes, RIFF::stream_backward); |
829 |
|
|
this->FrameOffset = currentframeoffset + copysamples; |
830 |
|
|
} |
831 |
|
|
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 |
schoenebeck |
2 |
pCkData->SetPos(remainingbytes, RIFF::stream_backward); |
842 |
|
|
} |
843 |
|
|
} |
844 |
persson |
365 |
else remainingbytes = 0; |
845 |
schoenebeck |
2 |
|
846 |
persson |
365 |
currentframeoffset -= skipsamples; |
847 |
schoenebeck |
2 |
|
848 |
persson |
365 |
if (copysamples == 0) { |
849 |
|
|
// skip this frame |
850 |
|
|
pSrc += framebytes - Channels; |
851 |
|
|
} |
852 |
|
|
else { |
853 |
|
|
const unsigned char* const param_l = pSrc; |
854 |
|
|
if (BitDepth == 24) { |
855 |
|
|
if (mode_l != 2) pSrc += 12; |
856 |
schoenebeck |
2 |
|
857 |
persson |
365 |
if (Channels == 2) { // Stereo |
858 |
|
|
const unsigned char* const param_r = pSrc; |
859 |
|
|
if (mode_r != 2) pSrc += 12; |
860 |
|
|
|
861 |
persson |
437 |
Decompress24(mode_l, param_l, 2, pSrc, pDst, |
862 |
|
|
skipsamples, copysamples, TruncatedBits); |
863 |
persson |
372 |
Decompress24(mode_r, param_r, 2, pSrc + rightChannelOffset, pDst + 1, |
864 |
persson |
437 |
skipsamples, copysamples, TruncatedBits); |
865 |
persson |
365 |
pDst += copysamples << 1; |
866 |
schoenebeck |
2 |
} |
867 |
persson |
365 |
else { // Mono |
868 |
persson |
437 |
Decompress24(mode_l, param_l, 1, pSrc, pDst, |
869 |
|
|
skipsamples, copysamples, TruncatedBits); |
870 |
persson |
365 |
pDst += copysamples; |
871 |
schoenebeck |
2 |
} |
872 |
persson |
365 |
} |
873 |
|
|
else { // 16 bit |
874 |
|
|
if (mode_l) pSrc += 4; |
875 |
schoenebeck |
2 |
|
876 |
persson |
365 |
int step; |
877 |
|
|
if (Channels == 2) { // Stereo |
878 |
|
|
const unsigned char* const param_r = pSrc; |
879 |
|
|
if (mode_r) pSrc += 4; |
880 |
|
|
|
881 |
|
|
step = (2 - mode_l) + (2 - mode_r); |
882 |
persson |
372 |
Decompress16(mode_l, param_l, step, 2, pSrc, pDst, skipsamples, copysamples); |
883 |
|
|
Decompress16(mode_r, param_r, step, 2, pSrc + (2 - mode_l), pDst + 1, |
884 |
persson |
365 |
skipsamples, copysamples); |
885 |
|
|
pDst += copysamples << 1; |
886 |
schoenebeck |
2 |
} |
887 |
persson |
365 |
else { // Mono |
888 |
|
|
step = 2 - mode_l; |
889 |
persson |
372 |
Decompress16(mode_l, param_l, step, 1, pSrc, pDst, skipsamples, copysamples); |
890 |
persson |
365 |
pDst += copysamples; |
891 |
schoenebeck |
2 |
} |
892 |
persson |
365 |
} |
893 |
|
|
pSrc += nextFrameOffset; |
894 |
|
|
} |
895 |
schoenebeck |
2 |
|
896 |
persson |
365 |
// 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 |
schoenebeck |
384 |
remainingbytes = pCkData->Read(pDecompressionBuffer->pStart, assumedsize, 1); |
902 |
|
|
pSrc = (unsigned char*) pDecompressionBuffer->pStart; |
903 |
schoenebeck |
2 |
} |
904 |
persson |
365 |
} // while |
905 |
|
|
|
906 |
schoenebeck |
2 |
this->SamplePos += (SampleCount - remainingsamples); |
907 |
schoenebeck |
11 |
if (this->SamplePos > this->SamplesTotal) this->SamplePos = this->SamplesTotal; |
908 |
schoenebeck |
2 |
return (SampleCount - remainingsamples); |
909 |
|
|
} |
910 |
|
|
} |
911 |
|
|
|
912 |
schoenebeck |
384 |
/** |
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 |
schoenebeck |
2 |
Sample::~Sample() { |
955 |
|
|
Instances--; |
956 |
schoenebeck |
384 |
if (!Instances && InternalDecompressionBuffer.Size) { |
957 |
|
|
delete[] (unsigned char*) InternalDecompressionBuffer.pStart; |
958 |
|
|
InternalDecompressionBuffer.pStart = NULL; |
959 |
|
|
InternalDecompressionBuffer.Size = 0; |
960 |
schoenebeck |
355 |
} |
961 |
schoenebeck |
2 |
if (FrameTable) delete[] FrameTable; |
962 |
|
|
if (RAMCache.pStart) delete[] (int8_t*) RAMCache.pStart; |
963 |
|
|
} |
964 |
|
|
|
965 |
|
|
|
966 |
|
|
|
967 |
|
|
// *************** DimensionRegion *************** |
968 |
|
|
// * |
969 |
|
|
|
970 |
schoenebeck |
16 |
uint DimensionRegion::Instances = 0; |
971 |
|
|
DimensionRegion::VelocityTableMap* DimensionRegion::pVelocityTables = NULL; |
972 |
|
|
|
973 |
schoenebeck |
2 |
DimensionRegion::DimensionRegion(RIFF::List* _3ewl) : DLS::Sampler(_3ewl) { |
974 |
schoenebeck |
16 |
Instances++; |
975 |
|
|
|
976 |
schoenebeck |
2 |
memcpy(&Crossfade, &SamplerOptions, 4); |
977 |
schoenebeck |
16 |
if (!pVelocityTables) pVelocityTables = new VelocityTableMap; |
978 |
schoenebeck |
2 |
|
979 |
|
|
RIFF::Chunk* _3ewa = _3ewl->GetSubChunk(CHUNK_ID_3EWA); |
980 |
schoenebeck |
241 |
_3ewa->ReadInt32(); // unknown, always 0x0000008C ? |
981 |
schoenebeck |
2 |
LFO3Frequency = (double) GIG_EXP_DECODE(_3ewa->ReadInt32()); |
982 |
|
|
EG3Attack = (double) GIG_EXP_DECODE(_3ewa->ReadInt32()); |
983 |
|
|
_3ewa->ReadInt16(); // unknown |
984 |
|
|
LFO1InternalDepth = _3ewa->ReadUint16(); |
985 |
|
|
_3ewa->ReadInt16(); // unknown |
986 |
|
|
LFO3InternalDepth = _3ewa->ReadInt16(); |
987 |
|
|
_3ewa->ReadInt16(); // unknown |
988 |
|
|
LFO1ControlDepth = _3ewa->ReadUint16(); |
989 |
|
|
_3ewa->ReadInt16(); // unknown |
990 |
|
|
LFO3ControlDepth = _3ewa->ReadInt16(); |
991 |
|
|
EG1Attack = (double) GIG_EXP_DECODE(_3ewa->ReadInt32()); |
992 |
|
|
EG1Decay1 = (double) GIG_EXP_DECODE(_3ewa->ReadInt32()); |
993 |
|
|
_3ewa->ReadInt16(); // unknown |
994 |
|
|
EG1Sustain = _3ewa->ReadUint16(); |
995 |
|
|
EG1Release = (double) GIG_EXP_DECODE(_3ewa->ReadInt32()); |
996 |
schoenebeck |
36 |
EG1Controller = DecodeLeverageController(static_cast<_lev_ctrl_t>(_3ewa->ReadUint8())); |
997 |
schoenebeck |
2 |
uint8_t eg1ctrloptions = _3ewa->ReadUint8(); |
998 |
|
|
EG1ControllerInvert = eg1ctrloptions & 0x01; |
999 |
|
|
EG1ControllerAttackInfluence = GIG_EG_CTR_ATTACK_INFLUENCE_EXTRACT(eg1ctrloptions); |
1000 |
|
|
EG1ControllerDecayInfluence = GIG_EG_CTR_DECAY_INFLUENCE_EXTRACT(eg1ctrloptions); |
1001 |
|
|
EG1ControllerReleaseInfluence = GIG_EG_CTR_RELEASE_INFLUENCE_EXTRACT(eg1ctrloptions); |
1002 |
schoenebeck |
36 |
EG2Controller = DecodeLeverageController(static_cast<_lev_ctrl_t>(_3ewa->ReadUint8())); |
1003 |
schoenebeck |
2 |
uint8_t eg2ctrloptions = _3ewa->ReadUint8(); |
1004 |
|
|
EG2ControllerInvert = eg2ctrloptions & 0x01; |
1005 |
|
|
EG2ControllerAttackInfluence = GIG_EG_CTR_ATTACK_INFLUENCE_EXTRACT(eg2ctrloptions); |
1006 |
|
|
EG2ControllerDecayInfluence = GIG_EG_CTR_DECAY_INFLUENCE_EXTRACT(eg2ctrloptions); |
1007 |
|
|
EG2ControllerReleaseInfluence = GIG_EG_CTR_RELEASE_INFLUENCE_EXTRACT(eg2ctrloptions); |
1008 |
|
|
LFO1Frequency = (double) GIG_EXP_DECODE(_3ewa->ReadInt32()); |
1009 |
|
|
EG2Attack = (double) GIG_EXP_DECODE(_3ewa->ReadInt32()); |
1010 |
|
|
EG2Decay1 = (double) GIG_EXP_DECODE(_3ewa->ReadInt32()); |
1011 |
|
|
_3ewa->ReadInt16(); // unknown |
1012 |
|
|
EG2Sustain = _3ewa->ReadUint16(); |
1013 |
|
|
EG2Release = (double) GIG_EXP_DECODE(_3ewa->ReadInt32()); |
1014 |
|
|
_3ewa->ReadInt16(); // unknown |
1015 |
|
|
LFO2ControlDepth = _3ewa->ReadUint16(); |
1016 |
|
|
LFO2Frequency = (double) GIG_EXP_DECODE(_3ewa->ReadInt32()); |
1017 |
|
|
_3ewa->ReadInt16(); // unknown |
1018 |
|
|
LFO2InternalDepth = _3ewa->ReadUint16(); |
1019 |
|
|
int32_t eg1decay2 = _3ewa->ReadInt32(); |
1020 |
|
|
EG1Decay2 = (double) GIG_EXP_DECODE(eg1decay2); |
1021 |
|
|
EG1InfiniteSustain = (eg1decay2 == 0x7fffffff); |
1022 |
|
|
_3ewa->ReadInt16(); // unknown |
1023 |
|
|
EG1PreAttack = _3ewa->ReadUint16(); |
1024 |
|
|
int32_t eg2decay2 = _3ewa->ReadInt32(); |
1025 |
|
|
EG2Decay2 = (double) GIG_EXP_DECODE(eg2decay2); |
1026 |
|
|
EG2InfiniteSustain = (eg2decay2 == 0x7fffffff); |
1027 |
|
|
_3ewa->ReadInt16(); // unknown |
1028 |
|
|
EG2PreAttack = _3ewa->ReadUint16(); |
1029 |
|
|
uint8_t velocityresponse = _3ewa->ReadUint8(); |
1030 |
|
|
if (velocityresponse < 5) { |
1031 |
|
|
VelocityResponseCurve = curve_type_nonlinear; |
1032 |
|
|
VelocityResponseDepth = velocityresponse; |
1033 |
|
|
} |
1034 |
|
|
else if (velocityresponse < 10) { |
1035 |
|
|
VelocityResponseCurve = curve_type_linear; |
1036 |
|
|
VelocityResponseDepth = velocityresponse - 5; |
1037 |
|
|
} |
1038 |
|
|
else if (velocityresponse < 15) { |
1039 |
|
|
VelocityResponseCurve = curve_type_special; |
1040 |
|
|
VelocityResponseDepth = velocityresponse - 10; |
1041 |
|
|
} |
1042 |
|
|
else { |
1043 |
|
|
VelocityResponseCurve = curve_type_unknown; |
1044 |
|
|
VelocityResponseDepth = 0; |
1045 |
|
|
} |
1046 |
|
|
uint8_t releasevelocityresponse = _3ewa->ReadUint8(); |
1047 |
|
|
if (releasevelocityresponse < 5) { |
1048 |
|
|
ReleaseVelocityResponseCurve = curve_type_nonlinear; |
1049 |
|
|
ReleaseVelocityResponseDepth = releasevelocityresponse; |
1050 |
|
|
} |
1051 |
|
|
else if (releasevelocityresponse < 10) { |
1052 |
|
|
ReleaseVelocityResponseCurve = curve_type_linear; |
1053 |
|
|
ReleaseVelocityResponseDepth = releasevelocityresponse - 5; |
1054 |
|
|
} |
1055 |
|
|
else if (releasevelocityresponse < 15) { |
1056 |
|
|
ReleaseVelocityResponseCurve = curve_type_special; |
1057 |
|
|
ReleaseVelocityResponseDepth = releasevelocityresponse - 10; |
1058 |
|
|
} |
1059 |
|
|
else { |
1060 |
|
|
ReleaseVelocityResponseCurve = curve_type_unknown; |
1061 |
|
|
ReleaseVelocityResponseDepth = 0; |
1062 |
|
|
} |
1063 |
|
|
VelocityResponseCurveScaling = _3ewa->ReadUint8(); |
1064 |
schoenebeck |
36 |
AttenuationControllerThreshold = _3ewa->ReadInt8(); |
1065 |
schoenebeck |
2 |
_3ewa->ReadInt32(); // unknown |
1066 |
|
|
SampleStartOffset = (uint16_t) _3ewa->ReadInt16(); |
1067 |
|
|
_3ewa->ReadInt16(); // unknown |
1068 |
|
|
uint8_t pitchTrackDimensionBypass = _3ewa->ReadInt8(); |
1069 |
|
|
PitchTrack = GIG_PITCH_TRACK_EXTRACT(pitchTrackDimensionBypass); |
1070 |
|
|
if (pitchTrackDimensionBypass & 0x10) DimensionBypass = dim_bypass_ctrl_94; |
1071 |
|
|
else if (pitchTrackDimensionBypass & 0x20) DimensionBypass = dim_bypass_ctrl_95; |
1072 |
|
|
else DimensionBypass = dim_bypass_ctrl_none; |
1073 |
|
|
uint8_t pan = _3ewa->ReadUint8(); |
1074 |
schoenebeck |
269 |
Pan = (pan < 64) ? pan : -((int)pan - 63); // signed 7 bit -> signed 8 bit |
1075 |
schoenebeck |
2 |
SelfMask = _3ewa->ReadInt8() & 0x01; |
1076 |
|
|
_3ewa->ReadInt8(); // unknown |
1077 |
|
|
uint8_t lfo3ctrl = _3ewa->ReadUint8(); |
1078 |
|
|
LFO3Controller = static_cast<lfo3_ctrl_t>(lfo3ctrl & 0x07); // lower 3 bits |
1079 |
|
|
LFO3Sync = lfo3ctrl & 0x20; // bit 5 |
1080 |
schoenebeck |
36 |
InvertAttenuationController = lfo3ctrl & 0x80; // bit 7 |
1081 |
|
|
AttenuationController = DecodeLeverageController(static_cast<_lev_ctrl_t>(_3ewa->ReadUint8())); |
1082 |
schoenebeck |
2 |
uint8_t lfo2ctrl = _3ewa->ReadUint8(); |
1083 |
|
|
LFO2Controller = static_cast<lfo2_ctrl_t>(lfo2ctrl & 0x07); // lower 3 bits |
1084 |
|
|
LFO2FlipPhase = lfo2ctrl & 0x80; // bit 7 |
1085 |
|
|
LFO2Sync = lfo2ctrl & 0x20; // bit 5 |
1086 |
|
|
bool extResonanceCtrl = lfo2ctrl & 0x40; // bit 6 |
1087 |
|
|
uint8_t lfo1ctrl = _3ewa->ReadUint8(); |
1088 |
|
|
LFO1Controller = static_cast<lfo1_ctrl_t>(lfo1ctrl & 0x07); // lower 3 bits |
1089 |
|
|
LFO1FlipPhase = lfo1ctrl & 0x80; // bit 7 |
1090 |
|
|
LFO1Sync = lfo1ctrl & 0x40; // bit 6 |
1091 |
|
|
VCFResonanceController = (extResonanceCtrl) ? static_cast<vcf_res_ctrl_t>(GIG_VCF_RESONANCE_CTRL_EXTRACT(lfo1ctrl)) |
1092 |
|
|
: vcf_res_ctrl_none; |
1093 |
|
|
uint16_t eg3depth = _3ewa->ReadUint16(); |
1094 |
|
|
EG3Depth = (eg3depth <= 1200) ? eg3depth /* positives */ |
1095 |
|
|
: (-1) * (int16_t) ((eg3depth ^ 0xffff) + 1); /* binary complementary for negatives */ |
1096 |
|
|
_3ewa->ReadInt16(); // unknown |
1097 |
|
|
ChannelOffset = _3ewa->ReadUint8() / 4; |
1098 |
|
|
uint8_t regoptions = _3ewa->ReadUint8(); |
1099 |
|
|
MSDecode = regoptions & 0x01; // bit 0 |
1100 |
|
|
SustainDefeat = regoptions & 0x02; // bit 1 |
1101 |
|
|
_3ewa->ReadInt16(); // unknown |
1102 |
|
|
VelocityUpperLimit = _3ewa->ReadInt8(); |
1103 |
|
|
_3ewa->ReadInt8(); // unknown |
1104 |
|
|
_3ewa->ReadInt16(); // unknown |
1105 |
|
|
ReleaseTriggerDecay = _3ewa->ReadUint8(); // release trigger decay |
1106 |
|
|
_3ewa->ReadInt8(); // unknown |
1107 |
|
|
_3ewa->ReadInt8(); // unknown |
1108 |
|
|
EG1Hold = _3ewa->ReadUint8() & 0x80; // bit 7 |
1109 |
|
|
uint8_t vcfcutoff = _3ewa->ReadUint8(); |
1110 |
|
|
VCFEnabled = vcfcutoff & 0x80; // bit 7 |
1111 |
|
|
VCFCutoff = vcfcutoff & 0x7f; // lower 7 bits |
1112 |
|
|
VCFCutoffController = static_cast<vcf_cutoff_ctrl_t>(_3ewa->ReadUint8()); |
1113 |
|
|
VCFVelocityScale = _3ewa->ReadUint8(); |
1114 |
|
|
_3ewa->ReadInt8(); // unknown |
1115 |
|
|
uint8_t vcfresonance = _3ewa->ReadUint8(); |
1116 |
|
|
VCFResonance = vcfresonance & 0x7f; // lower 7 bits |
1117 |
|
|
VCFResonanceDynamic = !(vcfresonance & 0x80); // bit 7 |
1118 |
|
|
uint8_t vcfbreakpoint = _3ewa->ReadUint8(); |
1119 |
|
|
VCFKeyboardTracking = vcfbreakpoint & 0x80; // bit 7 |
1120 |
|
|
VCFKeyboardTrackingBreakpoint = vcfbreakpoint & 0x7f; // lower 7 bits |
1121 |
|
|
uint8_t vcfvelocity = _3ewa->ReadUint8(); |
1122 |
|
|
VCFVelocityDynamicRange = vcfvelocity % 5; |
1123 |
|
|
VCFVelocityCurve = static_cast<curve_type_t>(vcfvelocity / 5); |
1124 |
|
|
VCFType = static_cast<vcf_type_t>(_3ewa->ReadUint8()); |
1125 |
schoenebeck |
345 |
if (VCFType == vcf_type_lowpass) { |
1126 |
|
|
if (lfo3ctrl & 0x40) // bit 6 |
1127 |
|
|
VCFType = vcf_type_lowpassturbo; |
1128 |
|
|
} |
1129 |
schoenebeck |
16 |
|
1130 |
|
|
// 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; |
1132 |
|
|
if (pVelocityTables->count(tableKey)) { // if key exists |
1133 |
|
|
pVelocityAttenuationTable = (*pVelocityTables)[tableKey]; |
1134 |
|
|
} |
1135 |
|
|
else { |
1136 |
schoenebeck |
317 |
pVelocityAttenuationTable = |
1137 |
schoenebeck |
308 |
CreateVelocityTable(VelocityResponseCurve, |
1138 |
|
|
VelocityResponseDepth, |
1139 |
|
|
VelocityResponseCurveScaling); |
1140 |
schoenebeck |
16 |
(*pVelocityTables)[tableKey] = pVelocityAttenuationTable; // put the new table into the tables map |
1141 |
|
|
} |
1142 |
persson |
406 |
|
1143 |
|
|
SampleAttenuation = pow(10.0, -Gain / (20.0 * 655360)); |
1144 |
schoenebeck |
2 |
} |
1145 |
schoenebeck |
55 |
|
1146 |
schoenebeck |
36 |
leverage_ctrl_t DimensionRegion::DecodeLeverageController(_lev_ctrl_t EncodedController) { |
1147 |
|
|
leverage_ctrl_t decodedcontroller; |
1148 |
|
|
switch (EncodedController) { |
1149 |
|
|
// special controller |
1150 |
|
|
case _lev_ctrl_none: |
1151 |
|
|
decodedcontroller.type = leverage_ctrl_t::type_none; |
1152 |
|
|
decodedcontroller.controller_number = 0; |
1153 |
|
|
break; |
1154 |
|
|
case _lev_ctrl_velocity: |
1155 |
|
|
decodedcontroller.type = leverage_ctrl_t::type_velocity; |
1156 |
|
|
decodedcontroller.controller_number = 0; |
1157 |
|
|
break; |
1158 |
|
|
case _lev_ctrl_channelaftertouch: |
1159 |
|
|
decodedcontroller.type = leverage_ctrl_t::type_channelaftertouch; |
1160 |
|
|
decodedcontroller.controller_number = 0; |
1161 |
|
|
break; |
1162 |
schoenebeck |
55 |
|
1163 |
schoenebeck |
36 |
// ordinary MIDI control change controller |
1164 |
|
|
case _lev_ctrl_modwheel: |
1165 |
|
|
decodedcontroller.type = leverage_ctrl_t::type_controlchange; |
1166 |
|
|
decodedcontroller.controller_number = 1; |
1167 |
|
|
break; |
1168 |
|
|
case _lev_ctrl_breath: |
1169 |
|
|
decodedcontroller.type = leverage_ctrl_t::type_controlchange; |
1170 |
|
|
decodedcontroller.controller_number = 2; |
1171 |
|
|
break; |
1172 |
|
|
case _lev_ctrl_foot: |
1173 |
|
|
decodedcontroller.type = leverage_ctrl_t::type_controlchange; |
1174 |
|
|
decodedcontroller.controller_number = 4; |
1175 |
|
|
break; |
1176 |
|
|
case _lev_ctrl_effect1: |
1177 |
|
|
decodedcontroller.type = leverage_ctrl_t::type_controlchange; |
1178 |
|
|
decodedcontroller.controller_number = 12; |
1179 |
|
|
break; |
1180 |
|
|
case _lev_ctrl_effect2: |
1181 |
|
|
decodedcontroller.type = leverage_ctrl_t::type_controlchange; |
1182 |
|
|
decodedcontroller.controller_number = 13; |
1183 |
|
|
break; |
1184 |
|
|
case _lev_ctrl_genpurpose1: |
1185 |
|
|
decodedcontroller.type = leverage_ctrl_t::type_controlchange; |
1186 |
|
|
decodedcontroller.controller_number = 16; |
1187 |
|
|
break; |
1188 |
|
|
case _lev_ctrl_genpurpose2: |
1189 |
|
|
decodedcontroller.type = leverage_ctrl_t::type_controlchange; |
1190 |
|
|
decodedcontroller.controller_number = 17; |
1191 |
|
|
break; |
1192 |
|
|
case _lev_ctrl_genpurpose3: |
1193 |
|
|
decodedcontroller.type = leverage_ctrl_t::type_controlchange; |
1194 |
|
|
decodedcontroller.controller_number = 18; |
1195 |
|
|
break; |
1196 |
|
|
case _lev_ctrl_genpurpose4: |
1197 |
|
|
decodedcontroller.type = leverage_ctrl_t::type_controlchange; |
1198 |
|
|
decodedcontroller.controller_number = 19; |
1199 |
|
|
break; |
1200 |
|
|
case _lev_ctrl_portamentotime: |
1201 |
|
|
decodedcontroller.type = leverage_ctrl_t::type_controlchange; |
1202 |
|
|
decodedcontroller.controller_number = 5; |
1203 |
|
|
break; |
1204 |
|
|
case _lev_ctrl_sustainpedal: |
1205 |
|
|
decodedcontroller.type = leverage_ctrl_t::type_controlchange; |
1206 |
|
|
decodedcontroller.controller_number = 64; |
1207 |
|
|
break; |
1208 |
|
|
case _lev_ctrl_portamento: |
1209 |
|
|
decodedcontroller.type = leverage_ctrl_t::type_controlchange; |
1210 |
|
|
decodedcontroller.controller_number = 65; |
1211 |
|
|
break; |
1212 |
|
|
case _lev_ctrl_sostenutopedal: |
1213 |
|
|
decodedcontroller.type = leverage_ctrl_t::type_controlchange; |
1214 |
|
|
decodedcontroller.controller_number = 66; |
1215 |
|
|
break; |
1216 |
|
|
case _lev_ctrl_softpedal: |
1217 |
|
|
decodedcontroller.type = leverage_ctrl_t::type_controlchange; |
1218 |
|
|
decodedcontroller.controller_number = 67; |
1219 |
|
|
break; |
1220 |
|
|
case _lev_ctrl_genpurpose5: |
1221 |
|
|
decodedcontroller.type = leverage_ctrl_t::type_controlchange; |
1222 |
|
|
decodedcontroller.controller_number = 80; |
1223 |
|
|
break; |
1224 |
|
|
case _lev_ctrl_genpurpose6: |
1225 |
|
|
decodedcontroller.type = leverage_ctrl_t::type_controlchange; |
1226 |
|
|
decodedcontroller.controller_number = 81; |
1227 |
|
|
break; |
1228 |
|
|
case _lev_ctrl_genpurpose7: |
1229 |
|
|
decodedcontroller.type = leverage_ctrl_t::type_controlchange; |
1230 |
|
|
decodedcontroller.controller_number = 82; |
1231 |
|
|
break; |
1232 |
|
|
case _lev_ctrl_genpurpose8: |
1233 |
|
|
decodedcontroller.type = leverage_ctrl_t::type_controlchange; |
1234 |
|
|
decodedcontroller.controller_number = 83; |
1235 |
|
|
break; |
1236 |
|
|
case _lev_ctrl_effect1depth: |
1237 |
|
|
decodedcontroller.type = leverage_ctrl_t::type_controlchange; |
1238 |
|
|
decodedcontroller.controller_number = 91; |
1239 |
|
|
break; |
1240 |
|
|
case _lev_ctrl_effect2depth: |
1241 |
|
|
decodedcontroller.type = leverage_ctrl_t::type_controlchange; |
1242 |
|
|
decodedcontroller.controller_number = 92; |
1243 |
|
|
break; |
1244 |
|
|
case _lev_ctrl_effect3depth: |
1245 |
|
|
decodedcontroller.type = leverage_ctrl_t::type_controlchange; |
1246 |
|
|
decodedcontroller.controller_number = 93; |
1247 |
|
|
break; |
1248 |
|
|
case _lev_ctrl_effect4depth: |
1249 |
|
|
decodedcontroller.type = leverage_ctrl_t::type_controlchange; |
1250 |
|
|
decodedcontroller.controller_number = 94; |
1251 |
|
|
break; |
1252 |
|
|
case _lev_ctrl_effect5depth: |
1253 |
|
|
decodedcontroller.type = leverage_ctrl_t::type_controlchange; |
1254 |
|
|
decodedcontroller.controller_number = 95; |
1255 |
|
|
break; |
1256 |
schoenebeck |
55 |
|
1257 |
schoenebeck |
36 |
// unknown controller type |
1258 |
|
|
default: |
1259 |
|
|
throw gig::Exception("Unknown leverage controller type."); |
1260 |
|
|
} |
1261 |
|
|
return decodedcontroller; |
1262 |
|
|
} |
1263 |
schoenebeck |
2 |
|
1264 |
schoenebeck |
16 |
DimensionRegion::~DimensionRegion() { |
1265 |
|
|
Instances--; |
1266 |
|
|
if (!Instances) { |
1267 |
|
|
// delete the velocity->volume tables |
1268 |
|
|
VelocityTableMap::iterator iter; |
1269 |
|
|
for (iter = pVelocityTables->begin(); iter != pVelocityTables->end(); iter++) { |
1270 |
|
|
double* pTable = iter->second; |
1271 |
|
|
if (pTable) delete[] pTable; |
1272 |
|
|
} |
1273 |
|
|
pVelocityTables->clear(); |
1274 |
|
|
delete pVelocityTables; |
1275 |
|
|
pVelocityTables = NULL; |
1276 |
|
|
} |
1277 |
|
|
} |
1278 |
schoenebeck |
2 |
|
1279 |
schoenebeck |
16 |
/** |
1280 |
|
|
* Returns the correct amplitude factor for the given \a MIDIKeyVelocity. |
1281 |
|
|
* All involved parameters (VelocityResponseCurve, VelocityResponseDepth |
1282 |
|
|
* and VelocityResponseCurveScaling) involved are taken into account to |
1283 |
|
|
* calculate the amplitude factor. Use this method when a key was |
1284 |
|
|
* triggered to get the volume with which the sample should be played |
1285 |
|
|
* back. |
1286 |
|
|
* |
1287 |
schoenebeck |
36 |
* @param MIDIKeyVelocity MIDI velocity value of the triggered key (between 0 and 127) |
1288 |
|
|
* @returns amplitude factor (between 0.0 and 1.0) |
1289 |
schoenebeck |
16 |
*/ |
1290 |
|
|
double DimensionRegion::GetVelocityAttenuation(uint8_t MIDIKeyVelocity) { |
1291 |
|
|
return pVelocityAttenuationTable[MIDIKeyVelocity]; |
1292 |
|
|
} |
1293 |
schoenebeck |
2 |
|
1294 |
schoenebeck |
308 |
double* DimensionRegion::CreateVelocityTable(curve_type_t curveType, uint8_t depth, uint8_t scaling) { |
1295 |
schoenebeck |
317 |
|
1296 |
schoenebeck |
308 |
// line-segment approximations of the 15 velocity curves |
1297 |
schoenebeck |
16 |
|
1298 |
schoenebeck |
308 |
// 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 |
schoenebeck |
16 |
|
1305 |
schoenebeck |
308 |
// non-linear |
1306 |
|
|
const int non0[] = { 1, 4, 24, 5, 57, 17, 92, 57, 122, 127, 127, 127 }; |
1307 |
schoenebeck |
317 |
const int non1[] = { 1, 4, 46, 9, 93, 56, 118, 106, 123, 127, |
1308 |
schoenebeck |
308 |
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 |
schoenebeck |
317 |
|
1315 |
schoenebeck |
308 |
// special |
1316 |
schoenebeck |
317 |
const int spe0[] = { 1, 2, 76, 10, 90, 15, 95, 20, 99, 28, 103, 44, |
1317 |
schoenebeck |
308 |
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 |
schoenebeck |
317 |
const int spe2[] = { 1, 1, 33, 1, 53, 5, 61, 13, 69, 32, 79, 74, |
1321 |
schoenebeck |
308 |
85, 90, 91, 127, 127, 127 }; |
1322 |
schoenebeck |
317 |
const int spe3[] = { 1, 32, 28, 35, 66, 48, 89, 59, 95, 65, 99, 73, |
1323 |
schoenebeck |
308 |
117, 127, 127, 127 }; |
1324 |
schoenebeck |
317 |
const int spe4[] = { 1, 4, 23, 5, 49, 13, 57, 17, 92, 57, 122, 127, |
1325 |
schoenebeck |
308 |
127, 127 }; |
1326 |
schoenebeck |
317 |
|
1327 |
schoenebeck |
308 |
const int* const curves[] = { non0, non1, non2, non3, non4, |
1328 |
schoenebeck |
317 |
lin0, lin1, lin2, lin3, lin4, |
1329 |
schoenebeck |
308 |
spe0, spe1, spe2, spe3, spe4 }; |
1330 |
schoenebeck |
317 |
|
1331 |
schoenebeck |
308 |
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 |
schoenebeck |
317 |
|
1336 |
schoenebeck |
308 |
table[0] = 0; |
1337 |
|
|
for (int x = 1 ; x < 128 ; x++) { |
1338 |
|
|
|
1339 |
|
|
if (x > curve[2]) curve += 2; |
1340 |
schoenebeck |
317 |
double y = curve[1] + (x - curve[0]) * |
1341 |
schoenebeck |
308 |
(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 |
schoenebeck |
2 |
// *************** Region *************** |
1359 |
|
|
// * |
1360 |
|
|
|
1361 |
|
|
Region::Region(Instrument* pInstrument, RIFF::List* rgnList) : DLS::Region((DLS::Instrument*) pInstrument, rgnList) { |
1362 |
|
|
// Initialization |
1363 |
|
|
Dimensions = 0; |
1364 |
schoenebeck |
347 |
for (int i = 0; i < 256; i++) { |
1365 |
schoenebeck |
2 |
pDimensionRegions[i] = NULL; |
1366 |
|
|
} |
1367 |
schoenebeck |
282 |
Layers = 1; |
1368 |
schoenebeck |
347 |
File* file = (File*) GetParent()->GetParent(); |
1369 |
|
|
int dimensionBits = (file->pVersion && file->pVersion->major == 3) ? 8 : 5; |
1370 |
schoenebeck |
2 |
|
1371 |
|
|
// Actual Loading |
1372 |
|
|
|
1373 |
|
|
LoadDimensionRegions(rgnList); |
1374 |
|
|
|
1375 |
|
|
RIFF::Chunk* _3lnk = rgnList->GetSubChunk(CHUNK_ID_3LNK); |
1376 |
|
|
if (_3lnk) { |
1377 |
|
|
DimensionRegions = _3lnk->ReadUint32(); |
1378 |
schoenebeck |
347 |
for (int i = 0; i < dimensionBits; i++) { |
1379 |
schoenebeck |
2 |
dimension_t dimension = static_cast<dimension_t>(_3lnk->ReadUint8()); |
1380 |
|
|
uint8_t bits = _3lnk->ReadUint8(); |
1381 |
|
|
if (dimension == dimension_none) { // inactive dimension |
1382 |
|
|
pDimensionDefinitions[i].dimension = dimension_none; |
1383 |
|
|
pDimensionDefinitions[i].bits = 0; |
1384 |
|
|
pDimensionDefinitions[i].zones = 0; |
1385 |
|
|
pDimensionDefinitions[i].split_type = split_type_bit; |
1386 |
|
|
pDimensionDefinitions[i].ranges = NULL; |
1387 |
|
|
pDimensionDefinitions[i].zone_size = 0; |
1388 |
|
|
} |
1389 |
|
|
else { // active dimension |
1390 |
|
|
pDimensionDefinitions[i].dimension = dimension; |
1391 |
|
|
pDimensionDefinitions[i].bits = bits; |
1392 |
|
|
pDimensionDefinitions[i].zones = 0x01 << bits; // = pow(2,bits) |
1393 |
|
|
pDimensionDefinitions[i].split_type = (dimension == dimension_layer || |
1394 |
schoenebeck |
241 |
dimension == dimension_samplechannel || |
1395 |
persson |
437 |
dimension == dimension_releasetrigger || |
1396 |
|
|
dimension == dimension_roundrobin || |
1397 |
|
|
dimension == dimension_random) ? split_type_bit |
1398 |
|
|
: split_type_normal; |
1399 |
schoenebeck |
2 |
pDimensionDefinitions[i].ranges = NULL; // it's not possible to check velocity dimensions for custom defined ranges at this point |
1400 |
|
|
pDimensionDefinitions[i].zone_size = |
1401 |
|
|
(pDimensionDefinitions[i].split_type == split_type_normal) ? 128 / pDimensionDefinitions[i].zones |
1402 |
|
|
: 0; |
1403 |
|
|
Dimensions++; |
1404 |
schoenebeck |
282 |
|
1405 |
|
|
// if this is a layer dimension, remember the amount of layers |
1406 |
|
|
if (dimension == dimension_layer) Layers = pDimensionDefinitions[i].zones; |
1407 |
schoenebeck |
2 |
} |
1408 |
|
|
_3lnk->SetPos(6, RIFF::stream_curpos); // jump forward to next dimension definition |
1409 |
|
|
} |
1410 |
|
|
|
1411 |
|
|
// check velocity dimension (if there is one) for custom defined zone ranges |
1412 |
|
|
for (uint i = 0; i < Dimensions; i++) { |
1413 |
|
|
dimension_def_t* pDimDef = pDimensionDefinitions + i; |
1414 |
|
|
if (pDimDef->dimension == dimension_velocity) { |
1415 |
|
|
if (pDimensionRegions[0]->VelocityUpperLimit == 0) { |
1416 |
|
|
// no custom defined ranges |
1417 |
|
|
pDimDef->split_type = split_type_normal; |
1418 |
|
|
pDimDef->ranges = NULL; |
1419 |
|
|
} |
1420 |
|
|
else { // custom defined ranges |
1421 |
|
|
pDimDef->split_type = split_type_customvelocity; |
1422 |
|
|
pDimDef->ranges = new range_t[pDimDef->zones]; |
1423 |
schoenebeck |
347 |
uint8_t bits[8] = { 0 }; |
1424 |
schoenebeck |
2 |
int previousUpperLimit = -1; |
1425 |
|
|
for (int velocityZone = 0; velocityZone < pDimDef->zones; velocityZone++) { |
1426 |
|
|
bits[i] = velocityZone; |
1427 |
schoenebeck |
347 |
DimensionRegion* pDimRegion = GetDimensionRegionByBit(bits); |
1428 |
schoenebeck |
2 |
|
1429 |
|
|
pDimDef->ranges[velocityZone].low = previousUpperLimit + 1; |
1430 |
|
|
pDimDef->ranges[velocityZone].high = pDimRegion->VelocityUpperLimit; |
1431 |
|
|
previousUpperLimit = pDimDef->ranges[velocityZone].high; |
1432 |
|
|
// fill velocity table |
1433 |
|
|
for (int i = pDimDef->ranges[velocityZone].low; i <= pDimDef->ranges[velocityZone].high; i++) { |
1434 |
|
|
VelocityTable[i] = velocityZone; |
1435 |
|
|
} |
1436 |
|
|
} |
1437 |
|
|
} |
1438 |
|
|
} |
1439 |
|
|
} |
1440 |
|
|
|
1441 |
schoenebeck |
317 |
// 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 |
schoenebeck |
2 |
// load sample references |
1449 |
|
|
for (uint i = 0; i < DimensionRegions; i++) { |
1450 |
|
|
uint32_t wavepoolindex = _3lnk->ReadUint32(); |
1451 |
|
|
pDimensionRegions[i]->pSample = GetSampleFromWavePool(wavepoolindex); |
1452 |
|
|
} |
1453 |
|
|
} |
1454 |
|
|
else throw gig::Exception("Mandatory <3lnk> chunk not found."); |
1455 |
|
|
} |
1456 |
|
|
|
1457 |
|
|
void Region::LoadDimensionRegions(RIFF::List* rgn) { |
1458 |
|
|
RIFF::List* _3prg = rgn->GetSubList(LIST_TYPE_3PRG); |
1459 |
|
|
if (_3prg) { |
1460 |
|
|
int dimensionRegionNr = 0; |
1461 |
|
|
RIFF::List* _3ewl = _3prg->GetFirstSubList(); |
1462 |
|
|
while (_3ewl) { |
1463 |
|
|
if (_3ewl->GetListType() == LIST_TYPE_3EWL) { |
1464 |
|
|
pDimensionRegions[dimensionRegionNr] = new DimensionRegion(_3ewl); |
1465 |
|
|
dimensionRegionNr++; |
1466 |
|
|
} |
1467 |
|
|
_3ewl = _3prg->GetNextSubList(); |
1468 |
|
|
} |
1469 |
|
|
if (dimensionRegionNr == 0) throw gig::Exception("No dimension region found."); |
1470 |
|
|
} |
1471 |
|
|
} |
1472 |
|
|
|
1473 |
|
|
Region::~Region() { |
1474 |
|
|
for (uint i = 0; i < Dimensions; i++) { |
1475 |
|
|
if (pDimensionDefinitions[i].ranges) delete[] pDimensionDefinitions[i].ranges; |
1476 |
|
|
} |
1477 |
schoenebeck |
350 |
for (int i = 0; i < 256; i++) { |
1478 |
schoenebeck |
2 |
if (pDimensionRegions[i]) delete pDimensionRegions[i]; |
1479 |
|
|
} |
1480 |
|
|
} |
1481 |
|
|
|
1482 |
|
|
/** |
1483 |
|
|
* Use this method in your audio engine to get the appropriate dimension |
1484 |
|
|
* region with it's articulation data for the current situation. Just |
1485 |
|
|
* call the method with the current MIDI controller values and you'll get |
1486 |
|
|
* the DimensionRegion with the appropriate articulation data for the |
1487 |
|
|
* current situation (for this Region of course only). To do that you'll |
1488 |
|
|
* first have to look which dimensions with which controllers and in |
1489 |
|
|
* which order are defined for this Region when you load the .gig file. |
1490 |
|
|
* Special cases are e.g. layer or channel dimensions where you just put |
1491 |
|
|
* in the index numbers instead of a MIDI controller value (means 0 for |
1492 |
|
|
* left channel, 1 for right channel or 0 for layer 0, 1 for layer 1, |
1493 |
|
|
* etc.). |
1494 |
|
|
* |
1495 |
schoenebeck |
347 |
* @param DimValues MIDI controller values (0-127) for dimension 0 to 7 |
1496 |
schoenebeck |
2 |
* @returns adress to the DimensionRegion for the given situation |
1497 |
|
|
* @see pDimensionDefinitions |
1498 |
|
|
* @see Dimensions |
1499 |
|
|
*/ |
1500 |
schoenebeck |
347 |
DimensionRegion* Region::GetDimensionRegionByValue(const uint DimValues[8]) { |
1501 |
|
|
uint8_t bits[8] = { 0 }; |
1502 |
schoenebeck |
2 |
for (uint i = 0; i < Dimensions; i++) { |
1503 |
schoenebeck |
347 |
bits[i] = DimValues[i]; |
1504 |
schoenebeck |
2 |
switch (pDimensionDefinitions[i].split_type) { |
1505 |
|
|
case split_type_normal: |
1506 |
|
|
bits[i] /= pDimensionDefinitions[i].zone_size; |
1507 |
|
|
break; |
1508 |
|
|
case split_type_customvelocity: |
1509 |
|
|
bits[i] = VelocityTable[bits[i]]; |
1510 |
|
|
break; |
1511 |
schoenebeck |
241 |
case split_type_bit: // the value is already the sought dimension bit number |
1512 |
|
|
const uint8_t limiter_mask = (0xff << pDimensionDefinitions[i].bits) ^ 0xff; |
1513 |
|
|
bits[i] = bits[i] & limiter_mask; // just make sure the value don't uses more bits than allowed |
1514 |
|
|
break; |
1515 |
schoenebeck |
2 |
} |
1516 |
|
|
} |
1517 |
schoenebeck |
347 |
return GetDimensionRegionByBit(bits); |
1518 |
schoenebeck |
2 |
} |
1519 |
|
|
|
1520 |
|
|
/** |
1521 |
|
|
* Returns the appropriate DimensionRegion for the given dimension bit |
1522 |
|
|
* numbers (zone index). You usually use <i>GetDimensionRegionByValue</i> |
1523 |
|
|
* instead of calling this method directly! |
1524 |
|
|
* |
1525 |
schoenebeck |
347 |
* @param DimBits Bit numbers for dimension 0 to 7 |
1526 |
schoenebeck |
2 |
* @returns adress to the DimensionRegion for the given dimension |
1527 |
|
|
* bit numbers |
1528 |
|
|
* @see GetDimensionRegionByValue() |
1529 |
|
|
*/ |
1530 |
schoenebeck |
347 |
DimensionRegion* Region::GetDimensionRegionByBit(const uint8_t DimBits[8]) { |
1531 |
|
|
return pDimensionRegions[((((((DimBits[7] << pDimensionDefinitions[6].bits | DimBits[6]) |
1532 |
|
|
<< pDimensionDefinitions[5].bits | DimBits[5]) |
1533 |
|
|
<< pDimensionDefinitions[4].bits | DimBits[4]) |
1534 |
|
|
<< pDimensionDefinitions[3].bits | DimBits[3]) |
1535 |
|
|
<< pDimensionDefinitions[2].bits | DimBits[2]) |
1536 |
|
|
<< pDimensionDefinitions[1].bits | DimBits[1]) |
1537 |
|
|
<< pDimensionDefinitions[0].bits | DimBits[0]]; |
1538 |
schoenebeck |
2 |
} |
1539 |
|
|
|
1540 |
|
|
/** |
1541 |
|
|
* Returns pointer address to the Sample referenced with this region. |
1542 |
|
|
* This is the global Sample for the entire Region (not sure if this is |
1543 |
|
|
* actually used by the Gigasampler engine - I would only use the Sample |
1544 |
|
|
* referenced by the appropriate DimensionRegion instead of this sample). |
1545 |
|
|
* |
1546 |
|
|
* @returns address to Sample or NULL if there is no reference to a |
1547 |
|
|
* sample saved in the .gig file |
1548 |
|
|
*/ |
1549 |
|
|
Sample* Region::GetSample() { |
1550 |
|
|
if (pSample) return static_cast<gig::Sample*>(pSample); |
1551 |
|
|
else return static_cast<gig::Sample*>(pSample = GetSampleFromWavePool(WavePoolTableIndex)); |
1552 |
|
|
} |
1553 |
|
|
|
1554 |
|
|
Sample* Region::GetSampleFromWavePool(unsigned int WavePoolTableIndex) { |
1555 |
schoenebeck |
352 |
if ((int32_t)WavePoolTableIndex == -1) return NULL; |
1556 |
schoenebeck |
2 |
File* file = (File*) GetParent()->GetParent(); |
1557 |
|
|
unsigned long soughtoffset = file->pWavePoolTable[WavePoolTableIndex]; |
1558 |
|
|
Sample* sample = file->GetFirstSample(); |
1559 |
|
|
while (sample) { |
1560 |
|
|
if (sample->ulWavePoolOffset == soughtoffset) return static_cast<gig::Sample*>(pSample = sample); |
1561 |
|
|
sample = file->GetNextSample(); |
1562 |
|
|
} |
1563 |
|
|
return NULL; |
1564 |
|
|
} |
1565 |
|
|
|
1566 |
|
|
|
1567 |
|
|
|
1568 |
|
|
// *************** Instrument *************** |
1569 |
|
|
// * |
1570 |
|
|
|
1571 |
|
|
Instrument::Instrument(File* pFile, RIFF::List* insList) : DLS::Instrument((DLS::File*)pFile, insList) { |
1572 |
|
|
// Initialization |
1573 |
|
|
for (int i = 0; i < 128; i++) RegionKeyTable[i] = NULL; |
1574 |
|
|
RegionIndex = -1; |
1575 |
|
|
|
1576 |
|
|
// Loading |
1577 |
|
|
RIFF::List* lart = insList->GetSubList(LIST_TYPE_LART); |
1578 |
|
|
if (lart) { |
1579 |
|
|
RIFF::Chunk* _3ewg = lart->GetSubChunk(CHUNK_ID_3EWG); |
1580 |
|
|
if (_3ewg) { |
1581 |
|
|
EffectSend = _3ewg->ReadUint16(); |
1582 |
|
|
Attenuation = _3ewg->ReadInt32(); |
1583 |
|
|
FineTune = _3ewg->ReadInt16(); |
1584 |
|
|
PitchbendRange = _3ewg->ReadInt16(); |
1585 |
|
|
uint8_t dimkeystart = _3ewg->ReadUint8(); |
1586 |
|
|
PianoReleaseMode = dimkeystart & 0x01; |
1587 |
|
|
DimensionKeyRange.low = dimkeystart >> 1; |
1588 |
|
|
DimensionKeyRange.high = _3ewg->ReadUint8(); |
1589 |
|
|
} |
1590 |
|
|
else throw gig::Exception("Mandatory <3ewg> chunk not found."); |
1591 |
|
|
} |
1592 |
|
|
else throw gig::Exception("Mandatory <lart> list chunk not found."); |
1593 |
|
|
|
1594 |
|
|
RIFF::List* lrgn = insList->GetSubList(LIST_TYPE_LRGN); |
1595 |
|
|
if (!lrgn) throw gig::Exception("Mandatory chunks in <ins > chunk not found."); |
1596 |
|
|
pRegions = new Region*[Regions]; |
1597 |
schoenebeck |
350 |
for (uint i = 0; i < Regions; i++) pRegions[i] = NULL; |
1598 |
schoenebeck |
2 |
RIFF::List* rgn = lrgn->GetFirstSubList(); |
1599 |
|
|
unsigned int iRegion = 0; |
1600 |
|
|
while (rgn) { |
1601 |
|
|
if (rgn->GetListType() == LIST_TYPE_RGN) { |
1602 |
|
|
pRegions[iRegion] = new Region(this, rgn); |
1603 |
|
|
iRegion++; |
1604 |
|
|
} |
1605 |
|
|
rgn = lrgn->GetNextSubList(); |
1606 |
|
|
} |
1607 |
|
|
|
1608 |
|
|
// Creating Region Key Table for fast lookup |
1609 |
|
|
for (uint iReg = 0; iReg < Regions; iReg++) { |
1610 |
|
|
for (int iKey = pRegions[iReg]->KeyRange.low; iKey <= pRegions[iReg]->KeyRange.high; iKey++) { |
1611 |
|
|
RegionKeyTable[iKey] = pRegions[iReg]; |
1612 |
|
|
} |
1613 |
|
|
} |
1614 |
|
|
} |
1615 |
|
|
|
1616 |
|
|
Instrument::~Instrument() { |
1617 |
|
|
for (uint i = 0; i < Regions; i++) { |
1618 |
|
|
if (pRegions) { |
1619 |
|
|
if (pRegions[i]) delete (pRegions[i]); |
1620 |
|
|
} |
1621 |
|
|
} |
1622 |
schoenebeck |
350 |
if (pRegions) delete[] pRegions; |
1623 |
schoenebeck |
2 |
} |
1624 |
|
|
|
1625 |
|
|
/** |
1626 |
|
|
* Returns the appropriate Region for a triggered note. |
1627 |
|
|
* |
1628 |
|
|
* @param Key MIDI Key number of triggered note / key (0 - 127) |
1629 |
|
|
* @returns pointer adress to the appropriate Region or NULL if there |
1630 |
|
|
* there is no Region defined for the given \a Key |
1631 |
|
|
*/ |
1632 |
|
|
Region* Instrument::GetRegion(unsigned int Key) { |
1633 |
|
|
if (!pRegions || Key > 127) return NULL; |
1634 |
|
|
return RegionKeyTable[Key]; |
1635 |
|
|
/*for (int i = 0; i < Regions; i++) { |
1636 |
|
|
if (Key <= pRegions[i]->KeyRange.high && |
1637 |
|
|
Key >= pRegions[i]->KeyRange.low) return pRegions[i]; |
1638 |
|
|
} |
1639 |
|
|
return NULL;*/ |
1640 |
|
|
} |
1641 |
|
|
|
1642 |
|
|
/** |
1643 |
|
|
* Returns the first Region of the instrument. You have to call this |
1644 |
|
|
* method once before you use GetNextRegion(). |
1645 |
|
|
* |
1646 |
|
|
* @returns pointer address to first region or NULL if there is none |
1647 |
|
|
* @see GetNextRegion() |
1648 |
|
|
*/ |
1649 |
|
|
Region* Instrument::GetFirstRegion() { |
1650 |
|
|
if (!Regions) return NULL; |
1651 |
|
|
RegionIndex = 1; |
1652 |
|
|
return pRegions[0]; |
1653 |
|
|
} |
1654 |
|
|
|
1655 |
|
|
/** |
1656 |
|
|
* Returns the next Region of the instrument. You have to call |
1657 |
|
|
* GetFirstRegion() once before you can use this method. By calling this |
1658 |
|
|
* method multiple times it iterates through the available Regions. |
1659 |
|
|
* |
1660 |
|
|
* @returns pointer address to the next region or NULL if end reached |
1661 |
|
|
* @see GetFirstRegion() |
1662 |
|
|
*/ |
1663 |
|
|
Region* Instrument::GetNextRegion() { |
1664 |
persson |
365 |
if (RegionIndex < 0 || uint32_t(RegionIndex) >= Regions) return NULL; |
1665 |
schoenebeck |
2 |
return pRegions[RegionIndex++]; |
1666 |
|
|
} |
1667 |
|
|
|
1668 |
|
|
|
1669 |
|
|
|
1670 |
|
|
// *************** File *************** |
1671 |
|
|
// * |
1672 |
|
|
|
1673 |
|
|
File::File(RIFF::File* pRIFF) : DLS::File(pRIFF) { |
1674 |
|
|
pSamples = NULL; |
1675 |
|
|
pInstruments = NULL; |
1676 |
|
|
} |
1677 |
|
|
|
1678 |
schoenebeck |
350 |
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 |
schoenebeck |
355 |
delete pSamples; |
1688 |
schoenebeck |
350 |
|
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 |
schoenebeck |
355 |
delete pInstruments; |
1699 |
schoenebeck |
350 |
} |
1700 |
|
|
} |
1701 |
|
|
|
1702 |
schoenebeck |
2 |
Sample* File::GetFirstSample() { |
1703 |
|
|
if (!pSamples) LoadSamples(); |
1704 |
|
|
if (!pSamples) return NULL; |
1705 |
|
|
SamplesIterator = pSamples->begin(); |
1706 |
|
|
return static_cast<gig::Sample*>( (SamplesIterator != pSamples->end()) ? *SamplesIterator : NULL ); |
1707 |
|
|
} |
1708 |
|
|
|
1709 |
|
|
Sample* File::GetNextSample() { |
1710 |
|
|
if (!pSamples) return NULL; |
1711 |
|
|
SamplesIterator++; |
1712 |
|
|
return static_cast<gig::Sample*>( (SamplesIterator != pSamples->end()) ? *SamplesIterator : NULL ); |
1713 |
|
|
} |
1714 |
|
|
|
1715 |
|
|
void File::LoadSamples() { |
1716 |
|
|
RIFF::List* wvpl = pRIFF->GetSubList(LIST_TYPE_WVPL); |
1717 |
|
|
if (wvpl) { |
1718 |
|
|
unsigned long wvplFileOffset = wvpl->GetFilePos(); |
1719 |
|
|
RIFF::List* wave = wvpl->GetFirstSubList(); |
1720 |
|
|
while (wave) { |
1721 |
|
|
if (wave->GetListType() == LIST_TYPE_WAVE) { |
1722 |
|
|
if (!pSamples) pSamples = new SampleList; |
1723 |
|
|
unsigned long waveFileOffset = wave->GetFilePos(); |
1724 |
|
|
pSamples->push_back(new Sample(this, wave, waveFileOffset - wvplFileOffset)); |
1725 |
|
|
} |
1726 |
|
|
wave = wvpl->GetNextSubList(); |
1727 |
|
|
} |
1728 |
|
|
} |
1729 |
|
|
else throw gig::Exception("Mandatory <wvpl> chunk not found."); |
1730 |
|
|
} |
1731 |
|
|
|
1732 |
|
|
Instrument* File::GetFirstInstrument() { |
1733 |
|
|
if (!pInstruments) LoadInstruments(); |
1734 |
|
|
if (!pInstruments) return NULL; |
1735 |
|
|
InstrumentsIterator = pInstruments->begin(); |
1736 |
|
|
return (InstrumentsIterator != pInstruments->end()) ? *InstrumentsIterator : NULL; |
1737 |
|
|
} |
1738 |
|
|
|
1739 |
|
|
Instrument* File::GetNextInstrument() { |
1740 |
|
|
if (!pInstruments) return NULL; |
1741 |
|
|
InstrumentsIterator++; |
1742 |
|
|
return (InstrumentsIterator != pInstruments->end()) ? *InstrumentsIterator : NULL; |
1743 |
|
|
} |
1744 |
|
|
|
1745 |
schoenebeck |
21 |
/** |
1746 |
|
|
* Returns the instrument with the given index. |
1747 |
|
|
* |
1748 |
|
|
* @returns sought instrument or NULL if there's no such instrument |
1749 |
|
|
*/ |
1750 |
|
|
Instrument* File::GetInstrument(uint index) { |
1751 |
|
|
if (!pInstruments) LoadInstruments(); |
1752 |
|
|
if (!pInstruments) return NULL; |
1753 |
|
|
InstrumentsIterator = pInstruments->begin(); |
1754 |
|
|
for (uint i = 0; InstrumentsIterator != pInstruments->end(); i++) { |
1755 |
|
|
if (i == index) return *InstrumentsIterator; |
1756 |
|
|
InstrumentsIterator++; |
1757 |
|
|
} |
1758 |
|
|
return NULL; |
1759 |
|
|
} |
1760 |
|
|
|
1761 |
schoenebeck |
2 |
void File::LoadInstruments() { |
1762 |
|
|
RIFF::List* lstInstruments = pRIFF->GetSubList(LIST_TYPE_LINS); |
1763 |
|
|
if (lstInstruments) { |
1764 |
|
|
RIFF::List* lstInstr = lstInstruments->GetFirstSubList(); |
1765 |
|
|
while (lstInstr) { |
1766 |
|
|
if (lstInstr->GetListType() == LIST_TYPE_INS) { |
1767 |
|
|
if (!pInstruments) pInstruments = new InstrumentList; |
1768 |
|
|
pInstruments->push_back(new Instrument(this, lstInstr)); |
1769 |
|
|
} |
1770 |
|
|
lstInstr = lstInstruments->GetNextSubList(); |
1771 |
|
|
} |
1772 |
|
|
} |
1773 |
|
|
else throw gig::Exception("Mandatory <lins> list chunk not found."); |
1774 |
|
|
} |
1775 |
|
|
|
1776 |
|
|
|
1777 |
|
|
|
1778 |
|
|
// *************** Exception *************** |
1779 |
|
|
// * |
1780 |
|
|
|
1781 |
|
|
Exception::Exception(String Message) : DLS::Exception(Message) { |
1782 |
|
|
} |
1783 |
|
|
|
1784 |
|
|
void Exception::PrintMessage() { |
1785 |
|
|
std::cout << "gig::Exception: " << Message << std::endl; |
1786 |
|
|
} |
1787 |
|
|
|
1788 |
|
|
} // namespace gig |