1 |
/*************************************************************************** |
2 |
* * |
3 |
* libgig - C++ cross-platform Gigasampler format file loader library * |
4 |
* * |
5 |
* Copyright (C) 2003 by Christian Schoenebeck * |
6 |
* <cuse@users.sourceforge.net> * |
7 |
* * |
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 |
namespace gig { |
27 |
|
28 |
// *************** Sample *************** |
29 |
// * |
30 |
|
31 |
unsigned int Sample::Instances = 0; |
32 |
void* Sample::pDecompressionBuffer = NULL; |
33 |
unsigned long Sample::DecompressionBufferSize = 0; |
34 |
|
35 |
Sample::Sample(File* pFile, RIFF::List* waveList, unsigned long WavePoolOffset) : DLS::Sample((DLS::File*) pFile, waveList, WavePoolOffset) { |
36 |
Instances++; |
37 |
|
38 |
RIFF::Chunk* _3gix = waveList->GetSubChunk(CHUNK_ID_3GIX); |
39 |
if (!_3gix) throw gig::Exception("Mandatory chunks in <wave> list chunk not found."); |
40 |
SampleGroup = _3gix->ReadInt16(); |
41 |
|
42 |
RIFF::Chunk* smpl = waveList->GetSubChunk(CHUNK_ID_SMPL); |
43 |
if (!smpl) throw gig::Exception("Mandatory chunks in <wave> list chunk not found."); |
44 |
Manufacturer = smpl->ReadInt32(); |
45 |
Product = smpl->ReadInt32(); |
46 |
SamplePeriod = smpl->ReadInt32(); |
47 |
MIDIUnityNote = smpl->ReadInt32(); |
48 |
MIDIPitchFraction = smpl->ReadInt32(); |
49 |
smpl->Read(&SMPTEFormat, 1, 4); |
50 |
SMPTEOffset = smpl->ReadInt32(); |
51 |
Loops = smpl->ReadInt32(); |
52 |
LoopID = smpl->ReadInt32(); |
53 |
smpl->Read(&LoopType, 1, 4); |
54 |
LoopStart = smpl->ReadInt32(); |
55 |
LoopEnd = smpl->ReadInt32(); |
56 |
LoopFraction = smpl->ReadInt32(); |
57 |
LoopPlayCount = smpl->ReadInt32(); |
58 |
|
59 |
FrameTable = NULL; |
60 |
SamplePos = 0; |
61 |
RAMCache.Size = 0; |
62 |
RAMCache.pStart = NULL; |
63 |
RAMCache.NullExtensionSize = 0; |
64 |
|
65 |
Compressed = (waveList->GetSubChunk(CHUNK_ID_EWAV)); |
66 |
if (Compressed) { |
67 |
ScanCompressedSample(); |
68 |
if (!pDecompressionBuffer) { |
69 |
pDecompressionBuffer = new int8_t[INITIAL_SAMPLE_BUFFER_SIZE]; |
70 |
DecompressionBufferSize = INITIAL_SAMPLE_BUFFER_SIZE; |
71 |
} |
72 |
} |
73 |
FrameOffset = 0; // just for streaming compressed samples |
74 |
} |
75 |
|
76 |
/// Scans compressed samples for mandatory informations (e.g. actual number of total sample points). |
77 |
void Sample::ScanCompressedSample() { |
78 |
//TODO: we have to add some more scans here (e.g. determine compression rate) |
79 |
this->SamplesTotal = 0; |
80 |
std::list<unsigned long> frameOffsets; |
81 |
|
82 |
// Scanning |
83 |
pCkData->SetPos(0); |
84 |
while (pCkData->GetState() == RIFF::stream_ready) { |
85 |
frameOffsets.push_back(pCkData->GetPos()); |
86 |
int16_t compressionmode = pCkData->ReadInt16(); |
87 |
this->SamplesTotal += 2048; |
88 |
switch (compressionmode) { |
89 |
case 1: // left channel compressed |
90 |
case 256: // right channel compressed |
91 |
pCkData->SetPos(6148, RIFF::stream_curpos); |
92 |
break; |
93 |
case 257: // both channels compressed |
94 |
pCkData->SetPos(4104, RIFF::stream_curpos); |
95 |
break; |
96 |
default: // both channels uncompressed |
97 |
pCkData->SetPos(8192, RIFF::stream_curpos); |
98 |
} |
99 |
} |
100 |
pCkData->SetPos(0); |
101 |
|
102 |
//FIXME: only seen compressed samples with 16 bit stereo so far |
103 |
this->FrameSize = 4; |
104 |
this->BitDepth = 16; |
105 |
|
106 |
// Build the frames table (which is used for fast resolving of a frame's chunk offset) |
107 |
if (FrameTable) delete[] FrameTable; |
108 |
FrameTable = new unsigned long[frameOffsets.size()]; |
109 |
std::list<unsigned long>::iterator end = frameOffsets.end(); |
110 |
std::list<unsigned long>::iterator iter = frameOffsets.begin(); |
111 |
for (int i = 0; iter != end; i++, iter++) { |
112 |
FrameTable[i] = *iter; |
113 |
} |
114 |
} |
115 |
|
116 |
/** |
117 |
* Loads (and uncompresses if needed) the whole sample wave into RAM. Use |
118 |
* ReleaseSampleData() to free the memory if you don't need the cached |
119 |
* sample data anymore. |
120 |
* |
121 |
* @returns buffer_t structure with start address and size of the buffer |
122 |
* in bytes |
123 |
* @see ReleaseSampleData(), Read(), SetPos() |
124 |
*/ |
125 |
buffer_t Sample::LoadSampleData() { |
126 |
return LoadSampleDataWithNullSamplesExtension(this->SamplesTotal, 0); // 0 amount of NullSamples |
127 |
} |
128 |
|
129 |
/** |
130 |
* Reads (uncompresses if needed) and caches the first \a SampleCount |
131 |
* numbers of SamplePoints in RAM. Use ReleaseSampleData() to free the |
132 |
* memory space if you don't need the cached samples anymore. There is no |
133 |
* guarantee that exactly \a SampleCount samples will be cached; this is |
134 |
* not an error. The size will be eventually truncated e.g. to the |
135 |
* beginning of a frame of a compressed sample. This is done for |
136 |
* efficiency reasons while streaming the wave by your sampler engine |
137 |
* later. Read the <i>Size</i> member of the <i>buffer_t</i> structure |
138 |
* that will be returned to determine the actual cached samples, but note |
139 |
* that the size is given in bytes! You get the number of actually cached |
140 |
* samples by dividing it by the frame size of the sample: |
141 |
* |
142 |
* buffer_t buf = pSample->LoadSampleData(acquired_samples); |
143 |
* long cachedsamples = buf.Size / pSample->FrameSize; |
144 |
* |
145 |
* @param SampleCount - number of sample points to load into RAM |
146 |
* @returns buffer_t structure with start address and size of |
147 |
* the cached sample data in bytes |
148 |
* @see ReleaseSampleData(), Read(), SetPos() |
149 |
*/ |
150 |
buffer_t Sample::LoadSampleData(unsigned long SampleCount) { |
151 |
return LoadSampleDataWithNullSamplesExtension(SampleCount, 0); // 0 amount of NullSamples |
152 |
} |
153 |
|
154 |
/** |
155 |
* Loads (and uncompresses if needed) the whole sample wave into RAM. Use |
156 |
* ReleaseSampleData() to free the memory if you don't need the cached |
157 |
* sample data anymore. |
158 |
* The method will add \a NullSamplesCount silence samples past the |
159 |
* official buffer end (this won't affect the 'Size' member of the |
160 |
* buffer_t structure, that means 'Size' always reflects the size of the |
161 |
* actual sample data, the buffer might be bigger though). Silence |
162 |
* samples past the official buffer are needed for differential |
163 |
* algorithms that always have to take subsequent samples into account |
164 |
* (resampling/interpolation would be an important example) and avoids |
165 |
* memory access faults in such cases. |
166 |
* |
167 |
* @param NullSamplesCount - number of silence samples the buffer should |
168 |
* be extended past it's data end |
169 |
* @returns buffer_t structure with start address and |
170 |
* size of the buffer in bytes |
171 |
* @see ReleaseSampleData(), Read(), SetPos() |
172 |
*/ |
173 |
buffer_t Sample::LoadSampleDataWithNullSamplesExtension(uint NullSamplesCount) { |
174 |
return LoadSampleDataWithNullSamplesExtension(this->SamplesTotal, NullSamplesCount); |
175 |
} |
176 |
|
177 |
/** |
178 |
* Reads (uncompresses if needed) and caches the first \a SampleCount |
179 |
* numbers of SamplePoints in RAM. Use ReleaseSampleData() to free the |
180 |
* memory space if you don't need the cached samples anymore. There is no |
181 |
* guarantee that exactly \a SampleCount samples will be cached; this is |
182 |
* not an error. The size will be eventually truncated e.g. to the |
183 |
* beginning of a frame of a compressed sample. This is done for |
184 |
* efficiency reasons while streaming the wave by your sampler engine |
185 |
* later. Read the <i>Size</i> member of the <i>buffer_t</i> structure |
186 |
* that will be returned to determine the actual cached samples, but note |
187 |
* that the size is given in bytes! You get the number of actually cached |
188 |
* samples by dividing it by the frame size of the sample: |
189 |
* |
190 |
* buffer_t buf = pSample->LoadSampleDataWithNullSamplesExtension(acquired_samples, null_samples); |
191 |
* long cachedsamples = buf.Size / pSample->FrameSize; |
192 |
* |
193 |
* The method will add \a NullSamplesCount silence samples past the |
194 |
* official buffer end (this won't affect the 'Size' member of the |
195 |
* buffer_t structure, that means 'Size' always reflects the size of the |
196 |
* actual sample data, the buffer might be bigger though). Silence |
197 |
* samples past the official buffer are needed for differential |
198 |
* algorithms that always have to take subsequent samples into account |
199 |
* (resampling/interpolation would be an important example) and avoids |
200 |
* memory access faults in such cases. |
201 |
* |
202 |
* @param SampleCount - number of sample points to load into RAM |
203 |
* @param NullSamplesCount - number of silence samples the buffer should |
204 |
* be extended past it's data end |
205 |
* @returns buffer_t structure with start address and |
206 |
* size of the cached sample data in bytes |
207 |
* @see ReleaseSampleData(), Read(), SetPos() |
208 |
*/ |
209 |
buffer_t Sample::LoadSampleDataWithNullSamplesExtension(unsigned long SampleCount, uint NullSamplesCount) { |
210 |
if (SampleCount > this->SamplesTotal) SampleCount = this->SamplesTotal; |
211 |
if (RAMCache.pStart) delete[] (int8_t*) RAMCache.pStart; |
212 |
unsigned long allocationsize = (SampleCount + NullSamplesCount) * this->FrameSize; |
213 |
RAMCache.pStart = new int8_t[allocationsize]; |
214 |
RAMCache.Size = Read(RAMCache.pStart, SampleCount) * this->FrameSize; |
215 |
RAMCache.NullExtensionSize = allocationsize - RAMCache.Size; |
216 |
// fill the remaining buffer space with silence samples |
217 |
memset((int8_t*)RAMCache.pStart + RAMCache.Size, 0, RAMCache.NullExtensionSize); |
218 |
return GetCache(); |
219 |
} |
220 |
|
221 |
/** |
222 |
* Returns current cached sample points. A buffer_t structure will be |
223 |
* returned which contains address pointer to the begin of the cache and |
224 |
* the size of the cached sample data in bytes. Use |
225 |
* <i>LoadSampleData()</i> to cache a specific amount of sample points in |
226 |
* RAM. |
227 |
* |
228 |
* @returns buffer_t structure with current cached sample points |
229 |
* @see LoadSampleData(); |
230 |
*/ |
231 |
buffer_t Sample::GetCache() { |
232 |
// return a copy of the buffer_t structure |
233 |
buffer_t result; |
234 |
result.Size = this->RAMCache.Size; |
235 |
result.pStart = this->RAMCache.pStart; |
236 |
result.NullExtensionSize = this->RAMCache.NullExtensionSize; |
237 |
return result; |
238 |
} |
239 |
|
240 |
/** |
241 |
* Frees the cached sample from RAM if loaded with |
242 |
* <i>LoadSampleData()</i> previously. |
243 |
* |
244 |
* @see LoadSampleData(); |
245 |
*/ |
246 |
void Sample::ReleaseSampleData() { |
247 |
if (RAMCache.pStart) delete[] (int8_t*) RAMCache.pStart; |
248 |
RAMCache.pStart = NULL; |
249 |
RAMCache.Size = 0; |
250 |
} |
251 |
|
252 |
/** |
253 |
* Sets the position within the sample (in sample points, not in |
254 |
* bytes). Use this method and <i>Read()</i> if you don't want to load |
255 |
* the sample into RAM, thus for disk streaming. |
256 |
* |
257 |
* Although the original Gigasampler engine doesn't allow positioning |
258 |
* within compressed samples, I decided to implement it. Even though |
259 |
* the Gigasampler format doesn't allow to define loops for compressed |
260 |
* samples at the moment, positioning within compressed samples might be |
261 |
* interesting for some sampler engines though. The only drawback about |
262 |
* my decision is that it takes longer to load compressed gig Files on |
263 |
* startup, because it's neccessary to scan the samples for some |
264 |
* mandatory informations. But I think as it doesn't affect the runtime |
265 |
* efficiency, nobody will have a problem with that. |
266 |
* |
267 |
* @param SampleCount number of sample points to jump |
268 |
* @param Whence optional: to which relation \a SampleCount refers |
269 |
* to, if omited <i>RIFF::stream_start</i> is assumed |
270 |
* @returns the new sample position |
271 |
* @see Read() |
272 |
*/ |
273 |
unsigned long Sample::SetPos(unsigned long SampleCount, RIFF::stream_whence_t Whence) { |
274 |
if (Compressed) { |
275 |
switch (Whence) { |
276 |
case RIFF::stream_curpos: |
277 |
this->SamplePos += SampleCount; |
278 |
break; |
279 |
case RIFF::stream_end: |
280 |
this->SamplePos = this->SamplesTotal - 1 - SampleCount; |
281 |
break; |
282 |
case RIFF::stream_backward: |
283 |
this->SamplePos -= SampleCount; |
284 |
break; |
285 |
case RIFF::stream_start: default: |
286 |
this->SamplePos = SampleCount; |
287 |
break; |
288 |
} |
289 |
if (this->SamplePos > this->SamplesTotal) this->SamplePos = this->SamplesTotal; |
290 |
|
291 |
unsigned long frame = this->SamplePos / 2048; // to which frame to jump |
292 |
this->FrameOffset = this->SamplePos % 2048; // offset (in sample points) within that frame |
293 |
pCkData->SetPos(FrameTable[frame]); // set chunk pointer to the start of sought frame |
294 |
return this->SamplePos; |
295 |
} |
296 |
else { // not compressed |
297 |
unsigned long orderedBytes = SampleCount * this->FrameSize; |
298 |
unsigned long result = pCkData->SetPos(orderedBytes, Whence); |
299 |
return (result == orderedBytes) ? SampleCount |
300 |
: result / this->FrameSize; |
301 |
} |
302 |
} |
303 |
|
304 |
/** |
305 |
* Returns the current position in the sample (in sample points). |
306 |
*/ |
307 |
unsigned long Sample::GetPos() { |
308 |
if (Compressed) return SamplePos; |
309 |
else return pCkData->GetPos() / FrameSize; |
310 |
} |
311 |
|
312 |
/** |
313 |
* Reads \a SampleCount number of sample points from the current |
314 |
* position into the buffer pointed by \a pBuffer and increments the |
315 |
* position within the sample. The sample wave stream will be |
316 |
* decompressed on the fly if using a compressed sample. Use this method |
317 |
* and <i>SetPos()</i> if you don't want to load the sample into RAM, |
318 |
* thus for disk streaming. |
319 |
* |
320 |
* @param pBuffer destination buffer |
321 |
* @param SampleCount number of sample points to read |
322 |
* @returns number of successfully read sample points |
323 |
* @see SetPos() |
324 |
*/ |
325 |
unsigned long Sample::Read(void* pBuffer, unsigned long SampleCount) { |
326 |
if (!Compressed) return pCkData->Read(pBuffer, SampleCount, FrameSize); |
327 |
else { //FIXME: no support for mono compressed samples yet, are there any? |
328 |
//TODO: efficiency: we simply assume here that all frames are compressed, maybe we should test for an average compression rate |
329 |
// best case needed buffer size (all frames compressed) |
330 |
unsigned long assumedsize = (SampleCount << 1) + // *2 (16 Bit, stereo, but assume all frames compressed) |
331 |
(SampleCount >> 10) + // 10 bytes header per 2048 sample points |
332 |
8194, // at least one worst case sample frame |
333 |
remainingbytes = 0, // remaining bytes in the local buffer |
334 |
remainingsamples = SampleCount, |
335 |
copysamples; |
336 |
int currentframeoffset = this->FrameOffset; // offset in current sample frame since last Read() |
337 |
this->FrameOffset = 0; |
338 |
|
339 |
if (assumedsize > this->DecompressionBufferSize) { |
340 |
// local buffer reallocation - hope this won't happen |
341 |
if (this->pDecompressionBuffer) delete[] (int8_t*) this->pDecompressionBuffer; |
342 |
this->pDecompressionBuffer = new int8_t[assumedsize << 1]; // double of current needed size |
343 |
this->DecompressionBufferSize = assumedsize; |
344 |
} |
345 |
|
346 |
int16_t compressionmode, left, dleft, right, dright; |
347 |
int8_t* pSrc = (int8_t*) this->pDecompressionBuffer; |
348 |
int16_t* pDst = (int16_t*) pBuffer; |
349 |
remainingbytes = pCkData->Read(pSrc, assumedsize, 1); |
350 |
|
351 |
while (remainingsamples) { |
352 |
|
353 |
// reload from disk to local buffer if needed |
354 |
if (remainingbytes < 8194) { |
355 |
if (pCkData->GetState() != RIFF::stream_ready) { |
356 |
this->SamplePos += (SampleCount - remainingsamples); |
357 |
//if (this->SamplePos > this->SamplesTotal) this->SamplePos = this->SamplesTotal; |
358 |
return (SampleCount - remainingsamples); |
359 |
} |
360 |
assumedsize = remainingsamples; |
361 |
assumedsize = (assumedsize << 1) + // *2 (16 Bit, stereo, but assume all frames compressed) |
362 |
(assumedsize >> 10) + // 10 bytes header per 2048 sample points |
363 |
8194; // at least one worst case sample frame |
364 |
pCkData->SetPos(remainingbytes, RIFF::stream_backward); |
365 |
if (pCkData->RemainingBytes() < assumedsize) assumedsize = pCkData->RemainingBytes(); |
366 |
remainingbytes = pCkData->Read(this->pDecompressionBuffer, assumedsize, 1); |
367 |
pSrc = (int8_t*) this->pDecompressionBuffer; |
368 |
} |
369 |
|
370 |
// determine how many samples in this frame to skip and read |
371 |
if (remainingsamples >= 2048) { |
372 |
copysamples = 2048 - currentframeoffset; |
373 |
remainingsamples -= copysamples; |
374 |
} |
375 |
else { |
376 |
copysamples = remainingsamples; |
377 |
if (currentframeoffset + copysamples > 2048) { |
378 |
copysamples = 2048 - currentframeoffset; |
379 |
remainingsamples -= copysamples; |
380 |
} |
381 |
else { |
382 |
pCkData->SetPos(remainingbytes, RIFF::stream_backward); |
383 |
remainingsamples = 0; |
384 |
this->FrameOffset = currentframeoffset + copysamples; |
385 |
} |
386 |
} |
387 |
|
388 |
// decompress and copy current frame from local buffer to destination buffer |
389 |
compressionmode = *(int16_t*)pSrc; pSrc+=2; |
390 |
switch (compressionmode) { |
391 |
case 1: // left channel compressed |
392 |
remainingbytes -= 6150; // (left 8 bit, right 16 bit, +6 byte header) |
393 |
if (!remainingsamples && copysamples == 2048) |
394 |
pCkData->SetPos(remainingbytes, RIFF::stream_backward); |
395 |
|
396 |
left = *(int16_t*)pSrc; pSrc+=2; |
397 |
dleft = *(int16_t*)pSrc; pSrc+=2; |
398 |
while (currentframeoffset) { |
399 |
dleft -= *pSrc; |
400 |
left -= dleft; |
401 |
pSrc+=3; // 8 bit left channel, skip uncompressed right channel (16 bit) |
402 |
currentframeoffset--; |
403 |
} |
404 |
while (copysamples) { |
405 |
dleft -= *pSrc; pSrc++; |
406 |
left -= dleft; |
407 |
*pDst = left; pDst++; |
408 |
*pDst = *(int16_t*)pSrc; pDst++; pSrc+=2; |
409 |
copysamples--; |
410 |
} |
411 |
break; |
412 |
case 256: // right channel compressed |
413 |
remainingbytes -= 6150; // (left 16 bit, right 8 bit, +6 byte header) |
414 |
if (!remainingsamples && copysamples == 2048) |
415 |
pCkData->SetPos(remainingbytes, RIFF::stream_backward); |
416 |
|
417 |
right = *(int16_t*)pSrc; pSrc+=2; |
418 |
dright = *(int16_t*)pSrc; pSrc+=2; |
419 |
if (currentframeoffset) { |
420 |
pSrc+=2; // skip uncompressed left channel, now we can increment by 3 |
421 |
while (currentframeoffset) { |
422 |
dright -= *pSrc; |
423 |
right -= dright; |
424 |
pSrc+=3; // 8 bit right channel, skip uncompressed left channel (16 bit) |
425 |
currentframeoffset--; |
426 |
} |
427 |
pSrc-=2; // back aligned to left channel |
428 |
} |
429 |
while (copysamples) { |
430 |
*pDst = *(int16_t*)pSrc; pDst++; pSrc+=2; |
431 |
dright -= *pSrc; pSrc++; |
432 |
right -= dright; |
433 |
*pDst = right; pDst++; |
434 |
copysamples--; |
435 |
} |
436 |
break; |
437 |
case 257: // both channels compressed |
438 |
remainingbytes -= 4106; // (left 8 bit, right 8 bit, +10 byte header) |
439 |
if (!remainingsamples && copysamples == 2048) |
440 |
pCkData->SetPos(remainingbytes, RIFF::stream_backward); |
441 |
|
442 |
left = *(int16_t*)pSrc; pSrc+=2; |
443 |
dleft = *(int16_t*)pSrc; pSrc+=2; |
444 |
right = *(int16_t*)pSrc; pSrc+=2; |
445 |
dright = *(int16_t*)pSrc; pSrc+=2; |
446 |
while (currentframeoffset) { |
447 |
dleft -= *pSrc; pSrc++; |
448 |
left -= dleft; |
449 |
dright -= *pSrc; pSrc++; |
450 |
right -= dright; |
451 |
currentframeoffset--; |
452 |
} |
453 |
while (copysamples) { |
454 |
dleft -= *pSrc; pSrc++; |
455 |
left -= dleft; |
456 |
dright -= *pSrc; pSrc++; |
457 |
right -= dright; |
458 |
*pDst = left; pDst++; |
459 |
*pDst = right; pDst++; |
460 |
copysamples--; |
461 |
} |
462 |
break; |
463 |
default: // both channels uncompressed |
464 |
remainingbytes -= 8194; // (left 16 bit, right 16 bit, +2 byte header) |
465 |
if (!remainingsamples && copysamples == 2048) |
466 |
pCkData->SetPos(remainingbytes, RIFF::stream_backward); |
467 |
|
468 |
pSrc += currentframeoffset << 2; |
469 |
currentframeoffset = 0; |
470 |
memcpy(pDst, pSrc, copysamples << 2); |
471 |
pDst += copysamples << 1; |
472 |
pSrc += copysamples << 2; |
473 |
break; |
474 |
} |
475 |
} |
476 |
this->SamplePos += (SampleCount - remainingsamples); |
477 |
//if (this->SamplePos > this->SamplesTotal) this->SamplePos = this->SamplesTotal; |
478 |
return (SampleCount - remainingsamples); |
479 |
} |
480 |
} |
481 |
|
482 |
Sample::~Sample() { |
483 |
Instances--; |
484 |
if (!Instances && pDecompressionBuffer) delete[] (int8_t*) pDecompressionBuffer; |
485 |
if (FrameTable) delete[] FrameTable; |
486 |
if (RAMCache.pStart) delete[] (int8_t*) RAMCache.pStart; |
487 |
} |
488 |
|
489 |
|
490 |
|
491 |
// *************** DimensionRegion *************** |
492 |
// * |
493 |
|
494 |
DimensionRegion::DimensionRegion(RIFF::List* _3ewl) : DLS::Sampler(_3ewl) { |
495 |
memcpy(&Crossfade, &SamplerOptions, 4); |
496 |
|
497 |
RIFF::Chunk* _3ewa = _3ewl->GetSubChunk(CHUNK_ID_3EWA); |
498 |
_3ewa->ReadInt32(); // unknown, allways 0x0000008C ? |
499 |
LFO3Frequency = (double) GIG_EXP_DECODE(_3ewa->ReadInt32()); |
500 |
EG3Attack = (double) GIG_EXP_DECODE(_3ewa->ReadInt32()); |
501 |
_3ewa->ReadInt16(); // unknown |
502 |
LFO1InternalDepth = _3ewa->ReadUint16(); |
503 |
_3ewa->ReadInt16(); // unknown |
504 |
LFO3InternalDepth = _3ewa->ReadInt16(); |
505 |
_3ewa->ReadInt16(); // unknown |
506 |
LFO1ControlDepth = _3ewa->ReadUint16(); |
507 |
_3ewa->ReadInt16(); // unknown |
508 |
LFO3ControlDepth = _3ewa->ReadInt16(); |
509 |
EG1Attack = (double) GIG_EXP_DECODE(_3ewa->ReadInt32()); |
510 |
EG1Decay1 = (double) GIG_EXP_DECODE(_3ewa->ReadInt32()); |
511 |
_3ewa->ReadInt16(); // unknown |
512 |
EG1Sustain = _3ewa->ReadUint16(); |
513 |
EG1Release = (double) GIG_EXP_DECODE(_3ewa->ReadInt32()); |
514 |
EG1Controller = static_cast<eg1_ctrl_t>(_3ewa->ReadUint8()); |
515 |
uint8_t eg1ctrloptions = _3ewa->ReadUint8(); |
516 |
EG1ControllerInvert = eg1ctrloptions & 0x01; |
517 |
EG1ControllerAttackInfluence = GIG_EG_CTR_ATTACK_INFLUENCE_EXTRACT(eg1ctrloptions); |
518 |
EG1ControllerDecayInfluence = GIG_EG_CTR_DECAY_INFLUENCE_EXTRACT(eg1ctrloptions); |
519 |
EG1ControllerReleaseInfluence = GIG_EG_CTR_RELEASE_INFLUENCE_EXTRACT(eg1ctrloptions); |
520 |
EG2Controller = static_cast<eg2_ctrl_t>(_3ewa->ReadUint8()); |
521 |
uint8_t eg2ctrloptions = _3ewa->ReadUint8(); |
522 |
EG2ControllerInvert = eg2ctrloptions & 0x01; |
523 |
EG2ControllerAttackInfluence = GIG_EG_CTR_ATTACK_INFLUENCE_EXTRACT(eg2ctrloptions); |
524 |
EG2ControllerDecayInfluence = GIG_EG_CTR_DECAY_INFLUENCE_EXTRACT(eg2ctrloptions); |
525 |
EG2ControllerReleaseInfluence = GIG_EG_CTR_RELEASE_INFLUENCE_EXTRACT(eg2ctrloptions); |
526 |
LFO1Frequency = (double) GIG_EXP_DECODE(_3ewa->ReadInt32()); |
527 |
EG2Attack = (double) GIG_EXP_DECODE(_3ewa->ReadInt32()); |
528 |
EG2Decay1 = (double) GIG_EXP_DECODE(_3ewa->ReadInt32()); |
529 |
_3ewa->ReadInt16(); // unknown |
530 |
EG2Sustain = _3ewa->ReadUint16(); |
531 |
EG2Release = (double) GIG_EXP_DECODE(_3ewa->ReadInt32()); |
532 |
_3ewa->ReadInt16(); // unknown |
533 |
LFO2ControlDepth = _3ewa->ReadUint16(); |
534 |
LFO2Frequency = (double) GIG_EXP_DECODE(_3ewa->ReadInt32()); |
535 |
_3ewa->ReadInt16(); // unknown |
536 |
LFO2InternalDepth = _3ewa->ReadUint16(); |
537 |
int32_t eg1decay2 = _3ewa->ReadInt32(); |
538 |
EG1Decay2 = (double) GIG_EXP_DECODE(eg1decay2); |
539 |
EG1InfiniteSustain = (eg1decay2 == 0x7fffffff); |
540 |
_3ewa->ReadInt16(); // unknown |
541 |
EG1PreAttack = _3ewa->ReadUint16(); |
542 |
int32_t eg2decay2 = _3ewa->ReadInt32(); |
543 |
EG2Decay2 = (double) GIG_EXP_DECODE(eg2decay2); |
544 |
EG2InfiniteSustain = (eg2decay2 == 0x7fffffff); |
545 |
_3ewa->ReadInt16(); // unknown |
546 |
EG2PreAttack = _3ewa->ReadUint16(); |
547 |
uint8_t velocityresponse = _3ewa->ReadUint8(); |
548 |
if (velocityresponse < 5) { |
549 |
VelocityResponseCurve = curve_type_nonlinear; |
550 |
VelocityResponseDepth = velocityresponse; |
551 |
} |
552 |
else if (velocityresponse < 10) { |
553 |
VelocityResponseCurve = curve_type_linear; |
554 |
VelocityResponseDepth = velocityresponse - 5; |
555 |
} |
556 |
else if (velocityresponse < 15) { |
557 |
VelocityResponseCurve = curve_type_special; |
558 |
VelocityResponseDepth = velocityresponse - 10; |
559 |
} |
560 |
else { |
561 |
VelocityResponseCurve = curve_type_unknown; |
562 |
VelocityResponseDepth = 0; |
563 |
} |
564 |
uint8_t releasevelocityresponse = _3ewa->ReadUint8(); |
565 |
if (releasevelocityresponse < 5) { |
566 |
ReleaseVelocityResponseCurve = curve_type_nonlinear; |
567 |
ReleaseVelocityResponseDepth = releasevelocityresponse; |
568 |
} |
569 |
else if (releasevelocityresponse < 10) { |
570 |
ReleaseVelocityResponseCurve = curve_type_linear; |
571 |
ReleaseVelocityResponseDepth = releasevelocityresponse - 5; |
572 |
} |
573 |
else if (releasevelocityresponse < 15) { |
574 |
ReleaseVelocityResponseCurve = curve_type_special; |
575 |
ReleaseVelocityResponseDepth = releasevelocityresponse - 10; |
576 |
} |
577 |
else { |
578 |
ReleaseVelocityResponseCurve = curve_type_unknown; |
579 |
ReleaseVelocityResponseDepth = 0; |
580 |
} |
581 |
VelocityResponseCurveScaling = _3ewa->ReadUint8(); |
582 |
AttenuationControlTreshold = _3ewa->ReadInt8(); |
583 |
_3ewa->ReadInt32(); // unknown |
584 |
SampleStartOffset = (uint16_t) _3ewa->ReadInt16(); |
585 |
_3ewa->ReadInt16(); // unknown |
586 |
uint8_t pitchTrackDimensionBypass = _3ewa->ReadInt8(); |
587 |
PitchTrack = GIG_PITCH_TRACK_EXTRACT(pitchTrackDimensionBypass); |
588 |
if (pitchTrackDimensionBypass & 0x10) DimensionBypass = dim_bypass_ctrl_94; |
589 |
else if (pitchTrackDimensionBypass & 0x20) DimensionBypass = dim_bypass_ctrl_95; |
590 |
else DimensionBypass = dim_bypass_ctrl_none; |
591 |
uint8_t pan = _3ewa->ReadUint8(); |
592 |
Pan = (pan < 64) ? pan : (-1) * (int8_t)pan - 63; |
593 |
SelfMask = _3ewa->ReadInt8() & 0x01; |
594 |
_3ewa->ReadInt8(); // unknown |
595 |
uint8_t lfo3ctrl = _3ewa->ReadUint8(); |
596 |
LFO3Controller = static_cast<lfo3_ctrl_t>(lfo3ctrl & 0x07); // lower 3 bits |
597 |
LFO3Sync = lfo3ctrl & 0x20; // bit 5 |
598 |
InvertAttenuationControl = lfo3ctrl & 0x80; // bit 7 |
599 |
if (VCFType == vcf_type_lowpass) { |
600 |
if (lfo3ctrl & 0x40) // bit 6 |
601 |
VCFType = vcf_type_lowpassturbo; |
602 |
} |
603 |
AttenuationControl = static_cast<attenuation_ctrl_t>(_3ewa->ReadUint8()); |
604 |
uint8_t lfo2ctrl = _3ewa->ReadUint8(); |
605 |
LFO2Controller = static_cast<lfo2_ctrl_t>(lfo2ctrl & 0x07); // lower 3 bits |
606 |
LFO2FlipPhase = lfo2ctrl & 0x80; // bit 7 |
607 |
LFO2Sync = lfo2ctrl & 0x20; // bit 5 |
608 |
bool extResonanceCtrl = lfo2ctrl & 0x40; // bit 6 |
609 |
uint8_t lfo1ctrl = _3ewa->ReadUint8(); |
610 |
LFO1Controller = static_cast<lfo1_ctrl_t>(lfo1ctrl & 0x07); // lower 3 bits |
611 |
LFO1FlipPhase = lfo1ctrl & 0x80; // bit 7 |
612 |
LFO1Sync = lfo1ctrl & 0x40; // bit 6 |
613 |
VCFResonanceController = (extResonanceCtrl) ? static_cast<vcf_res_ctrl_t>(GIG_VCF_RESONANCE_CTRL_EXTRACT(lfo1ctrl)) |
614 |
: vcf_res_ctrl_none; |
615 |
uint16_t eg3depth = _3ewa->ReadUint16(); |
616 |
EG3Depth = (eg3depth <= 1200) ? eg3depth /* positives */ |
617 |
: (-1) * (int16_t) ((eg3depth ^ 0xffff) + 1); /* binary complementary for negatives */ |
618 |
_3ewa->ReadInt16(); // unknown |
619 |
ChannelOffset = _3ewa->ReadUint8() / 4; |
620 |
uint8_t regoptions = _3ewa->ReadUint8(); |
621 |
MSDecode = regoptions & 0x01; // bit 0 |
622 |
SustainDefeat = regoptions & 0x02; // bit 1 |
623 |
_3ewa->ReadInt16(); // unknown |
624 |
VelocityUpperLimit = _3ewa->ReadInt8(); |
625 |
_3ewa->ReadInt8(); // unknown |
626 |
_3ewa->ReadInt16(); // unknown |
627 |
ReleaseTriggerDecay = _3ewa->ReadUint8(); // release trigger decay |
628 |
_3ewa->ReadInt8(); // unknown |
629 |
_3ewa->ReadInt8(); // unknown |
630 |
EG1Hold = _3ewa->ReadUint8() & 0x80; // bit 7 |
631 |
uint8_t vcfcutoff = _3ewa->ReadUint8(); |
632 |
VCFEnabled = vcfcutoff & 0x80; // bit 7 |
633 |
VCFCutoff = vcfcutoff & 0x7f; // lower 7 bits |
634 |
VCFCutoffController = static_cast<vcf_cutoff_ctrl_t>(_3ewa->ReadUint8()); |
635 |
VCFVelocityScale = _3ewa->ReadUint8(); |
636 |
_3ewa->ReadInt8(); // unknown |
637 |
uint8_t vcfresonance = _3ewa->ReadUint8(); |
638 |
VCFResonance = vcfresonance & 0x7f; // lower 7 bits |
639 |
VCFResonanceDynamic = !(vcfresonance & 0x80); // bit 7 |
640 |
uint8_t vcfbreakpoint = _3ewa->ReadUint8(); |
641 |
VCFKeyboardTracking = vcfbreakpoint & 0x80; // bit 7 |
642 |
VCFKeyboardTrackingBreakpoint = vcfbreakpoint & 0x7f; // lower 7 bits |
643 |
uint8_t vcfvelocity = _3ewa->ReadUint8(); |
644 |
VCFVelocityDynamicRange = vcfvelocity % 5; |
645 |
VCFVelocityCurve = static_cast<curve_type_t>(vcfvelocity / 5); |
646 |
VCFType = static_cast<vcf_type_t>(_3ewa->ReadUint8()); |
647 |
} |
648 |
|
649 |
|
650 |
|
651 |
// *************** Region *************** |
652 |
// * |
653 |
|
654 |
Region::Region(Instrument* pInstrument, RIFF::List* rgnList) : DLS::Region((DLS::Instrument*) pInstrument, rgnList) { |
655 |
// Initialization |
656 |
Dimensions = 0; |
657 |
for (int i = 0; i < 32; i++) { |
658 |
pDimensionRegions[i] = NULL; |
659 |
} |
660 |
|
661 |
// Actual Loading |
662 |
|
663 |
LoadDimensionRegions(rgnList); |
664 |
|
665 |
RIFF::Chunk* _3lnk = rgnList->GetSubChunk(CHUNK_ID_3LNK); |
666 |
if (_3lnk) { |
667 |
DimensionRegions = _3lnk->ReadUint32(); |
668 |
for (int i = 0; i < 5; i++) { |
669 |
dimension_t dimension = static_cast<dimension_t>(_3lnk->ReadUint8()); |
670 |
uint8_t bits = _3lnk->ReadUint8(); |
671 |
if (dimension == dimension_none) { // inactive dimension |
672 |
pDimensionDefinitions[i].dimension = dimension_none; |
673 |
pDimensionDefinitions[i].bits = 0; |
674 |
pDimensionDefinitions[i].zones = 0; |
675 |
pDimensionDefinitions[i].split_type = split_type_bit; |
676 |
pDimensionDefinitions[i].ranges = NULL; |
677 |
pDimensionDefinitions[i].zone_size = 0; |
678 |
} |
679 |
else { // active dimension |
680 |
pDimensionDefinitions[i].dimension = dimension; |
681 |
pDimensionDefinitions[i].bits = bits; |
682 |
pDimensionDefinitions[i].zones = 0x01 << bits; // = pow(2,bits) |
683 |
pDimensionDefinitions[i].split_type = (dimension == dimension_layer || |
684 |
dimension == dimension_samplechannel) ? split_type_bit |
685 |
: split_type_normal; |
686 |
pDimensionDefinitions[i].ranges = NULL; // it's not possible to check velocity dimensions for custom defined ranges at this point |
687 |
pDimensionDefinitions[i].zone_size = |
688 |
(pDimensionDefinitions[i].split_type == split_type_normal) ? 128 / pDimensionDefinitions[i].zones |
689 |
: 0; |
690 |
Dimensions++; |
691 |
} |
692 |
_3lnk->SetPos(6, RIFF::stream_curpos); // jump forward to next dimension definition |
693 |
} |
694 |
|
695 |
// check velocity dimension (if there is one) for custom defined zone ranges |
696 |
for (uint i = 0; i < Dimensions; i++) { |
697 |
dimension_def_t* pDimDef = pDimensionDefinitions + i; |
698 |
if (pDimDef->dimension == dimension_velocity) { |
699 |
if (pDimensionRegions[0]->VelocityUpperLimit == 0) { |
700 |
// no custom defined ranges |
701 |
pDimDef->split_type = split_type_normal; |
702 |
pDimDef->ranges = NULL; |
703 |
} |
704 |
else { // custom defined ranges |
705 |
pDimDef->split_type = split_type_customvelocity; |
706 |
pDimDef->ranges = new range_t[pDimDef->zones]; |
707 |
unsigned int bits[5] = {0,0,0,0,0}; |
708 |
int previousUpperLimit = -1; |
709 |
for (int velocityZone = 0; velocityZone < pDimDef->zones; velocityZone++) { |
710 |
bits[i] = velocityZone; |
711 |
DimensionRegion* pDimRegion = GetDimensionRegionByBit(bits[4],bits[3],bits[2],bits[1],bits[0]); |
712 |
|
713 |
pDimDef->ranges[velocityZone].low = previousUpperLimit + 1; |
714 |
pDimDef->ranges[velocityZone].high = pDimRegion->VelocityUpperLimit; |
715 |
previousUpperLimit = pDimDef->ranges[velocityZone].high; |
716 |
// fill velocity table |
717 |
for (int i = pDimDef->ranges[velocityZone].low; i <= pDimDef->ranges[velocityZone].high; i++) { |
718 |
VelocityTable[i] = velocityZone; |
719 |
} |
720 |
} |
721 |
} |
722 |
} |
723 |
} |
724 |
|
725 |
// load sample references |
726 |
_3lnk->SetPos(44); // jump to start of the wave pool indices (if not already there) |
727 |
for (uint i = 0; i < DimensionRegions; i++) { |
728 |
uint32_t wavepoolindex = _3lnk->ReadUint32(); |
729 |
pDimensionRegions[i]->pSample = GetSampleFromWavePool(wavepoolindex); |
730 |
} |
731 |
} |
732 |
else throw gig::Exception("Mandatory <3lnk> chunk not found."); |
733 |
} |
734 |
|
735 |
void Region::LoadDimensionRegions(RIFF::List* rgn) { |
736 |
RIFF::List* _3prg = rgn->GetSubList(LIST_TYPE_3PRG); |
737 |
if (_3prg) { |
738 |
int dimensionRegionNr = 0; |
739 |
RIFF::List* _3ewl = _3prg->GetFirstSubList(); |
740 |
while (_3ewl) { |
741 |
if (_3ewl->GetListType() == LIST_TYPE_3EWL) { |
742 |
pDimensionRegions[dimensionRegionNr] = new DimensionRegion(_3ewl); |
743 |
dimensionRegionNr++; |
744 |
} |
745 |
_3ewl = _3prg->GetNextSubList(); |
746 |
} |
747 |
if (dimensionRegionNr == 0) throw gig::Exception("No dimension region found."); |
748 |
} |
749 |
} |
750 |
|
751 |
Region::~Region() { |
752 |
for (uint i = 0; i < Dimensions; i++) { |
753 |
if (pDimensionDefinitions[i].ranges) delete[] pDimensionDefinitions[i].ranges; |
754 |
} |
755 |
for (int i = 0; i < 32; i++) { |
756 |
if (pDimensionRegions[i]) delete pDimensionRegions[i]; |
757 |
} |
758 |
} |
759 |
|
760 |
/** |
761 |
* Use this method in your audio engine to get the appropriate dimension |
762 |
* region with it's articulation data for the current situation. Just |
763 |
* call the method with the current MIDI controller values and you'll get |
764 |
* the DimensionRegion with the appropriate articulation data for the |
765 |
* current situation (for this Region of course only). To do that you'll |
766 |
* first have to look which dimensions with which controllers and in |
767 |
* which order are defined for this Region when you load the .gig file. |
768 |
* Special cases are e.g. layer or channel dimensions where you just put |
769 |
* in the index numbers instead of a MIDI controller value (means 0 for |
770 |
* left channel, 1 for right channel or 0 for layer 0, 1 for layer 1, |
771 |
* etc.). |
772 |
* |
773 |
* @param Dim4Val MIDI controller value (0-127) for dimension 4 |
774 |
* @param Dim3Val MIDI controller value (0-127) for dimension 3 |
775 |
* @param Dim2Val MIDI controller value (0-127) for dimension 2 |
776 |
* @param Dim1Val MIDI controller value (0-127) for dimension 1 |
777 |
* @param Dim0Val MIDI controller value (0-127) for dimension 0 |
778 |
* @returns adress to the DimensionRegion for the given situation |
779 |
* @see pDimensionDefinitions |
780 |
* @see Dimensions |
781 |
*/ |
782 |
DimensionRegion* Region::GetDimensionRegionByValue(uint Dim4Val, uint Dim3Val, uint Dim2Val, uint Dim1Val, uint Dim0Val) { |
783 |
unsigned int bits[5] = {Dim0Val,Dim1Val,Dim2Val,Dim3Val,Dim4Val}; |
784 |
for (uint i = 0; i < Dimensions; i++) { |
785 |
switch (pDimensionDefinitions[i].split_type) { |
786 |
case split_type_normal: |
787 |
bits[i] /= pDimensionDefinitions[i].zone_size; |
788 |
break; |
789 |
case split_type_customvelocity: |
790 |
bits[i] = VelocityTable[bits[i]]; |
791 |
break; |
792 |
// else the value is already the sought dimension bit number |
793 |
} |
794 |
} |
795 |
return GetDimensionRegionByBit(bits[4],bits[3],bits[2],bits[1],bits[0]); |
796 |
} |
797 |
|
798 |
/** |
799 |
* Returns the appropriate DimensionRegion for the given dimension bit |
800 |
* numbers (zone index). You usually use <i>GetDimensionRegionByValue</i> |
801 |
* instead of calling this method directly! |
802 |
* |
803 |
* @param Dim4Bit Bit number for dimension 4 |
804 |
* @param Dim3Bit Bit number for dimension 3 |
805 |
* @param Dim2Bit Bit number for dimension 2 |
806 |
* @param Dim1Bit Bit number for dimension 1 |
807 |
* @param Dim0Bit Bit number for dimension 0 |
808 |
* @returns adress to the DimensionRegion for the given dimension |
809 |
* bit numbers |
810 |
* @see GetDimensionRegionByValue() |
811 |
*/ |
812 |
DimensionRegion* Region::GetDimensionRegionByBit(uint8_t Dim4Bit, uint8_t Dim3Bit, uint8_t Dim2Bit, uint8_t Dim1Bit, uint8_t Dim0Bit) { |
813 |
return *(pDimensionRegions + ((((((((Dim4Bit << pDimensionDefinitions[3].bits) | Dim3Bit) |
814 |
<< pDimensionDefinitions[2].bits) | Dim2Bit) |
815 |
<< pDimensionDefinitions[1].bits) | Dim1Bit) |
816 |
<< pDimensionDefinitions[0].bits) | Dim0Bit) ); |
817 |
} |
818 |
|
819 |
/** |
820 |
* Returns pointer address to the Sample referenced with this region. |
821 |
* This is the global Sample for the entire Region (not sure if this is |
822 |
* actually used by the Gigasampler engine - I would only use the Sample |
823 |
* referenced by the appropriate DimensionRegion instead of this sample). |
824 |
* |
825 |
* @returns address to Sample or NULL if there is no reference to a |
826 |
* sample saved in the .gig file |
827 |
*/ |
828 |
Sample* Region::GetSample() { |
829 |
if (pSample) return static_cast<gig::Sample*>(pSample); |
830 |
else return static_cast<gig::Sample*>(pSample = GetSampleFromWavePool(WavePoolTableIndex)); |
831 |
} |
832 |
|
833 |
Sample* Region::GetSampleFromWavePool(unsigned int WavePoolTableIndex) { |
834 |
File* file = (File*) GetParent()->GetParent(); |
835 |
unsigned long soughtoffset = file->pWavePoolTable[WavePoolTableIndex]; |
836 |
Sample* sample = file->GetFirstSample(); |
837 |
while (sample) { |
838 |
if (sample->ulWavePoolOffset == soughtoffset) return static_cast<gig::Sample*>(pSample = sample); |
839 |
sample = file->GetNextSample(); |
840 |
} |
841 |
return NULL; |
842 |
} |
843 |
|
844 |
|
845 |
|
846 |
// *************** Instrument *************** |
847 |
// * |
848 |
|
849 |
Instrument::Instrument(File* pFile, RIFF::List* insList) : DLS::Instrument((DLS::File*)pFile, insList) { |
850 |
// Initialization |
851 |
for (int i = 0; i < 128; i++) RegionKeyTable[i] = NULL; |
852 |
RegionIndex = -1; |
853 |
|
854 |
// Loading |
855 |
RIFF::List* lart = insList->GetSubList(LIST_TYPE_LART); |
856 |
if (lart) { |
857 |
RIFF::Chunk* _3ewg = lart->GetSubChunk(CHUNK_ID_3EWG); |
858 |
if (_3ewg) { |
859 |
EffectSend = _3ewg->ReadUint16(); |
860 |
Attenuation = _3ewg->ReadInt32(); |
861 |
FineTune = _3ewg->ReadInt16(); |
862 |
PitchbendRange = _3ewg->ReadInt16(); |
863 |
uint8_t dimkeystart = _3ewg->ReadUint8(); |
864 |
PianoReleaseMode = dimkeystart & 0x01; |
865 |
DimensionKeyRange.low = dimkeystart >> 1; |
866 |
DimensionKeyRange.high = _3ewg->ReadUint8(); |
867 |
} |
868 |
else throw gig::Exception("Mandatory <3ewg> chunk not found."); |
869 |
} |
870 |
else throw gig::Exception("Mandatory <lart> list chunk not found."); |
871 |
|
872 |
RIFF::List* lrgn = insList->GetSubList(LIST_TYPE_LRGN); |
873 |
if (!lrgn) throw gig::Exception("Mandatory chunks in <ins > chunk not found."); |
874 |
pRegions = new Region*[Regions]; |
875 |
RIFF::List* rgn = lrgn->GetFirstSubList(); |
876 |
unsigned int iRegion = 0; |
877 |
while (rgn) { |
878 |
if (rgn->GetListType() == LIST_TYPE_RGN) { |
879 |
pRegions[iRegion] = new Region(this, rgn); |
880 |
iRegion++; |
881 |
} |
882 |
rgn = lrgn->GetNextSubList(); |
883 |
} |
884 |
|
885 |
// Creating Region Key Table for fast lookup |
886 |
for (uint iReg = 0; iReg < Regions; iReg++) { |
887 |
for (int iKey = pRegions[iReg]->KeyRange.low; iKey <= pRegions[iReg]->KeyRange.high; iKey++) { |
888 |
RegionKeyTable[iKey] = pRegions[iReg]; |
889 |
} |
890 |
} |
891 |
} |
892 |
|
893 |
Instrument::~Instrument() { |
894 |
for (uint i = 0; i < Regions; i++) { |
895 |
if (pRegions) { |
896 |
if (pRegions[i]) delete (pRegions[i]); |
897 |
} |
898 |
delete[] pRegions; |
899 |
} |
900 |
} |
901 |
|
902 |
/** |
903 |
* Returns the appropriate Region for a triggered note. |
904 |
* |
905 |
* @param Key MIDI Key number of triggered note / key (0 - 127) |
906 |
* @returns pointer adress to the appropriate Region or NULL if there |
907 |
* there is no Region defined for the given \a Key |
908 |
*/ |
909 |
Region* Instrument::GetRegion(unsigned int Key) { |
910 |
if (!pRegions || Key > 127) return NULL; |
911 |
return RegionKeyTable[Key]; |
912 |
/*for (int i = 0; i < Regions; i++) { |
913 |
if (Key <= pRegions[i]->KeyRange.high && |
914 |
Key >= pRegions[i]->KeyRange.low) return pRegions[i]; |
915 |
} |
916 |
return NULL;*/ |
917 |
} |
918 |
|
919 |
/** |
920 |
* Returns the first Region of the instrument. You have to call this |
921 |
* method once before you use GetNextRegion(). |
922 |
* |
923 |
* @returns pointer address to first region or NULL if there is none |
924 |
* @see GetNextRegion() |
925 |
*/ |
926 |
Region* Instrument::GetFirstRegion() { |
927 |
if (!Regions) return NULL; |
928 |
RegionIndex = 1; |
929 |
return pRegions[0]; |
930 |
} |
931 |
|
932 |
/** |
933 |
* Returns the next Region of the instrument. You have to call |
934 |
* GetFirstRegion() once before you can use this method. By calling this |
935 |
* method multiple times it iterates through the available Regions. |
936 |
* |
937 |
* @returns pointer address to the next region or NULL if end reached |
938 |
* @see GetFirstRegion() |
939 |
*/ |
940 |
Region* Instrument::GetNextRegion() { |
941 |
if (RegionIndex < 0 || RegionIndex >= Regions) return NULL; |
942 |
return pRegions[RegionIndex++]; |
943 |
} |
944 |
|
945 |
|
946 |
|
947 |
// *************** File *************** |
948 |
// * |
949 |
|
950 |
File::File(RIFF::File* pRIFF) : DLS::File(pRIFF) { |
951 |
pSamples = NULL; |
952 |
pInstruments = NULL; |
953 |
} |
954 |
|
955 |
Sample* File::GetFirstSample() { |
956 |
if (!pSamples) LoadSamples(); |
957 |
if (!pSamples) return NULL; |
958 |
SamplesIterator = pSamples->begin(); |
959 |
return static_cast<gig::Sample*>( (SamplesIterator != pSamples->end()) ? *SamplesIterator : NULL ); |
960 |
} |
961 |
|
962 |
Sample* File::GetNextSample() { |
963 |
if (!pSamples) return NULL; |
964 |
SamplesIterator++; |
965 |
return static_cast<gig::Sample*>( (SamplesIterator != pSamples->end()) ? *SamplesIterator : NULL ); |
966 |
} |
967 |
|
968 |
void File::LoadSamples() { |
969 |
RIFF::List* wvpl = pRIFF->GetSubList(LIST_TYPE_WVPL); |
970 |
if (wvpl) { |
971 |
unsigned long wvplFileOffset = wvpl->GetFilePos(); |
972 |
RIFF::List* wave = wvpl->GetFirstSubList(); |
973 |
while (wave) { |
974 |
if (wave->GetListType() == LIST_TYPE_WAVE) { |
975 |
if (!pSamples) pSamples = new SampleList; |
976 |
unsigned long waveFileOffset = wave->GetFilePos(); |
977 |
pSamples->push_back(new Sample(this, wave, waveFileOffset - wvplFileOffset)); |
978 |
} |
979 |
wave = wvpl->GetNextSubList(); |
980 |
} |
981 |
} |
982 |
else throw gig::Exception("Mandatory <wvpl> chunk not found."); |
983 |
} |
984 |
|
985 |
Instrument* File::GetFirstInstrument() { |
986 |
if (!pInstruments) LoadInstruments(); |
987 |
if (!pInstruments) return NULL; |
988 |
InstrumentsIterator = pInstruments->begin(); |
989 |
return (InstrumentsIterator != pInstruments->end()) ? *InstrumentsIterator : NULL; |
990 |
} |
991 |
|
992 |
Instrument* File::GetNextInstrument() { |
993 |
if (!pInstruments) return NULL; |
994 |
InstrumentsIterator++; |
995 |
return (InstrumentsIterator != pInstruments->end()) ? *InstrumentsIterator : NULL; |
996 |
} |
997 |
|
998 |
void File::LoadInstruments() { |
999 |
RIFF::List* lstInstruments = pRIFF->GetSubList(LIST_TYPE_LINS); |
1000 |
if (lstInstruments) { |
1001 |
RIFF::List* lstInstr = lstInstruments->GetFirstSubList(); |
1002 |
while (lstInstr) { |
1003 |
if (lstInstr->GetListType() == LIST_TYPE_INS) { |
1004 |
if (!pInstruments) pInstruments = new InstrumentList; |
1005 |
pInstruments->push_back(new Instrument(this, lstInstr)); |
1006 |
} |
1007 |
lstInstr = lstInstruments->GetNextSubList(); |
1008 |
} |
1009 |
} |
1010 |
else throw gig::Exception("Mandatory <lins> list chunk not found."); |
1011 |
} |
1012 |
|
1013 |
|
1014 |
|
1015 |
// *************** Exception *************** |
1016 |
// * |
1017 |
|
1018 |
Exception::Exception(String Message) : DLS::Exception(Message) { |
1019 |
} |
1020 |
|
1021 |
void Exception::PrintMessage() { |
1022 |
std::cout << "gig::Exception: " << Message << std::endl; |
1023 |
} |
1024 |
|
1025 |
} // namespace gig |