1 |
schoenebeck |
2 |
/*************************************************************************** |
2 |
|
|
* * |
3 |
|
|
* libgig - C++ cross-platform Gigasampler format file loader library * |
4 |
|
|
* * |
5 |
schoenebeck |
55 |
* Copyright (C) 2003, 2004 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 |
|
|
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 |
schoenebeck |
21 |
FineTune = smpl->ReadInt32(); |
49 |
schoenebeck |
2 |
smpl->Read(&SMPTEFormat, 1, 4); |
50 |
|
|
SMPTEOffset = smpl->ReadInt32(); |
51 |
|
|
Loops = smpl->ReadInt32(); |
52 |
schoenebeck |
21 |
uint32_t manufByt = smpl->ReadInt32(); |
53 |
schoenebeck |
2 |
LoopID = smpl->ReadInt32(); |
54 |
|
|
smpl->Read(&LoopType, 1, 4); |
55 |
|
|
LoopStart = smpl->ReadInt32(); |
56 |
|
|
LoopEnd = smpl->ReadInt32(); |
57 |
|
|
LoopFraction = smpl->ReadInt32(); |
58 |
|
|
LoopPlayCount = smpl->ReadInt32(); |
59 |
|
|
|
60 |
|
|
FrameTable = NULL; |
61 |
|
|
SamplePos = 0; |
62 |
|
|
RAMCache.Size = 0; |
63 |
|
|
RAMCache.pStart = NULL; |
64 |
|
|
RAMCache.NullExtensionSize = 0; |
65 |
|
|
|
66 |
|
|
Compressed = (waveList->GetSubChunk(CHUNK_ID_EWAV)); |
67 |
|
|
if (Compressed) { |
68 |
|
|
ScanCompressedSample(); |
69 |
|
|
} |
70 |
schoenebeck |
317 |
|
71 |
|
|
if (BitDepth > 24) throw gig::Exception("Only samples up to 24 bit supported"); |
72 |
|
|
if (Compressed && Channels == 1) throw gig::Exception("Mono compressed samples not yet supported"); |
73 |
|
|
if (Compressed && BitDepth == 24) throw gig::Exception("24 bit compressed samples not yet supported"); |
74 |
|
|
|
75 |
|
|
// we use a buffer for decompression and for truncating 24 bit samples to 16 bit |
76 |
|
|
if ((Compressed || BitDepth == 24) && !pDecompressionBuffer) { |
77 |
|
|
pDecompressionBuffer = new int8_t[INITIAL_SAMPLE_BUFFER_SIZE]; |
78 |
|
|
DecompressionBufferSize = INITIAL_SAMPLE_BUFFER_SIZE; |
79 |
|
|
} |
80 |
schoenebeck |
2 |
FrameOffset = 0; // just for streaming compressed samples |
81 |
schoenebeck |
21 |
|
82 |
schoenebeck |
27 |
LoopSize = LoopEnd - LoopStart; |
83 |
schoenebeck |
2 |
} |
84 |
|
|
|
85 |
|
|
/// Scans compressed samples for mandatory informations (e.g. actual number of total sample points). |
86 |
|
|
void Sample::ScanCompressedSample() { |
87 |
|
|
//TODO: we have to add some more scans here (e.g. determine compression rate) |
88 |
|
|
this->SamplesTotal = 0; |
89 |
|
|
std::list<unsigned long> frameOffsets; |
90 |
|
|
|
91 |
|
|
// Scanning |
92 |
|
|
pCkData->SetPos(0); |
93 |
|
|
while (pCkData->GetState() == RIFF::stream_ready) { |
94 |
|
|
frameOffsets.push_back(pCkData->GetPos()); |
95 |
|
|
int16_t compressionmode = pCkData->ReadInt16(); |
96 |
|
|
this->SamplesTotal += 2048; |
97 |
|
|
switch (compressionmode) { |
98 |
|
|
case 1: // left channel compressed |
99 |
|
|
case 256: // right channel compressed |
100 |
|
|
pCkData->SetPos(6148, RIFF::stream_curpos); |
101 |
|
|
break; |
102 |
|
|
case 257: // both channels compressed |
103 |
|
|
pCkData->SetPos(4104, RIFF::stream_curpos); |
104 |
|
|
break; |
105 |
|
|
default: // both channels uncompressed |
106 |
|
|
pCkData->SetPos(8192, RIFF::stream_curpos); |
107 |
|
|
} |
108 |
|
|
} |
109 |
|
|
pCkData->SetPos(0); |
110 |
|
|
|
111 |
|
|
//FIXME: only seen compressed samples with 16 bit stereo so far |
112 |
|
|
this->FrameSize = 4; |
113 |
|
|
this->BitDepth = 16; |
114 |
|
|
|
115 |
|
|
// Build the frames table (which is used for fast resolving of a frame's chunk offset) |
116 |
|
|
if (FrameTable) delete[] FrameTable; |
117 |
|
|
FrameTable = new unsigned long[frameOffsets.size()]; |
118 |
|
|
std::list<unsigned long>::iterator end = frameOffsets.end(); |
119 |
|
|
std::list<unsigned long>::iterator iter = frameOffsets.begin(); |
120 |
|
|
for (int i = 0; iter != end; i++, iter++) { |
121 |
|
|
FrameTable[i] = *iter; |
122 |
|
|
} |
123 |
|
|
} |
124 |
|
|
|
125 |
|
|
/** |
126 |
|
|
* Loads (and uncompresses if needed) the whole sample wave into RAM. Use |
127 |
|
|
* ReleaseSampleData() to free the memory if you don't need the cached |
128 |
|
|
* sample data anymore. |
129 |
|
|
* |
130 |
|
|
* @returns buffer_t structure with start address and size of the buffer |
131 |
|
|
* in bytes |
132 |
|
|
* @see ReleaseSampleData(), Read(), SetPos() |
133 |
|
|
*/ |
134 |
|
|
buffer_t Sample::LoadSampleData() { |
135 |
|
|
return LoadSampleDataWithNullSamplesExtension(this->SamplesTotal, 0); // 0 amount of NullSamples |
136 |
|
|
} |
137 |
|
|
|
138 |
|
|
/** |
139 |
|
|
* Reads (uncompresses if needed) and caches the first \a SampleCount |
140 |
|
|
* numbers of SamplePoints in RAM. Use ReleaseSampleData() to free the |
141 |
|
|
* memory space if you don't need the cached samples anymore. There is no |
142 |
|
|
* guarantee that exactly \a SampleCount samples will be cached; this is |
143 |
|
|
* not an error. The size will be eventually truncated e.g. to the |
144 |
|
|
* beginning of a frame of a compressed sample. This is done for |
145 |
|
|
* efficiency reasons while streaming the wave by your sampler engine |
146 |
|
|
* later. Read the <i>Size</i> member of the <i>buffer_t</i> structure |
147 |
|
|
* that will be returned to determine the actual cached samples, but note |
148 |
|
|
* that the size is given in bytes! You get the number of actually cached |
149 |
|
|
* samples by dividing it by the frame size of the sample: |
150 |
|
|
* |
151 |
|
|
* buffer_t buf = pSample->LoadSampleData(acquired_samples); |
152 |
|
|
* long cachedsamples = buf.Size / pSample->FrameSize; |
153 |
|
|
* |
154 |
|
|
* @param SampleCount - number of sample points to load into RAM |
155 |
|
|
* @returns buffer_t structure with start address and size of |
156 |
|
|
* the cached sample data in bytes |
157 |
|
|
* @see ReleaseSampleData(), Read(), SetPos() |
158 |
|
|
*/ |
159 |
|
|
buffer_t Sample::LoadSampleData(unsigned long SampleCount) { |
160 |
|
|
return LoadSampleDataWithNullSamplesExtension(SampleCount, 0); // 0 amount of NullSamples |
161 |
|
|
} |
162 |
|
|
|
163 |
|
|
/** |
164 |
|
|
* Loads (and uncompresses if needed) the whole sample wave into RAM. Use |
165 |
|
|
* ReleaseSampleData() to free the memory if you don't need the cached |
166 |
|
|
* sample data anymore. |
167 |
|
|
* The method will add \a NullSamplesCount silence samples past the |
168 |
|
|
* official buffer end (this won't affect the 'Size' member of the |
169 |
|
|
* buffer_t structure, that means 'Size' always reflects the size of the |
170 |
|
|
* actual sample data, the buffer might be bigger though). Silence |
171 |
|
|
* samples past the official buffer are needed for differential |
172 |
|
|
* algorithms that always have to take subsequent samples into account |
173 |
|
|
* (resampling/interpolation would be an important example) and avoids |
174 |
|
|
* memory access faults in such cases. |
175 |
|
|
* |
176 |
|
|
* @param NullSamplesCount - number of silence samples the buffer should |
177 |
|
|
* be extended past it's data end |
178 |
|
|
* @returns buffer_t structure with start address and |
179 |
|
|
* size of the buffer in bytes |
180 |
|
|
* @see ReleaseSampleData(), Read(), SetPos() |
181 |
|
|
*/ |
182 |
|
|
buffer_t Sample::LoadSampleDataWithNullSamplesExtension(uint NullSamplesCount) { |
183 |
|
|
return LoadSampleDataWithNullSamplesExtension(this->SamplesTotal, NullSamplesCount); |
184 |
|
|
} |
185 |
|
|
|
186 |
|
|
/** |
187 |
|
|
* Reads (uncompresses if needed) and caches the first \a SampleCount |
188 |
|
|
* numbers of SamplePoints in RAM. Use ReleaseSampleData() to free the |
189 |
|
|
* memory space if you don't need the cached samples anymore. There is no |
190 |
|
|
* guarantee that exactly \a SampleCount samples will be cached; this is |
191 |
|
|
* not an error. The size will be eventually truncated e.g. to the |
192 |
|
|
* beginning of a frame of a compressed sample. This is done for |
193 |
|
|
* efficiency reasons while streaming the wave by your sampler engine |
194 |
|
|
* later. Read the <i>Size</i> member of the <i>buffer_t</i> structure |
195 |
|
|
* that will be returned to determine the actual cached samples, but note |
196 |
|
|
* that the size is given in bytes! You get the number of actually cached |
197 |
|
|
* samples by dividing it by the frame size of the sample: |
198 |
|
|
* |
199 |
|
|
* buffer_t buf = pSample->LoadSampleDataWithNullSamplesExtension(acquired_samples, null_samples); |
200 |
|
|
* long cachedsamples = buf.Size / pSample->FrameSize; |
201 |
|
|
* |
202 |
|
|
* The method will add \a NullSamplesCount silence samples past the |
203 |
|
|
* official buffer end (this won't affect the 'Size' member of the |
204 |
|
|
* buffer_t structure, that means 'Size' always reflects the size of the |
205 |
|
|
* actual sample data, the buffer might be bigger though). Silence |
206 |
|
|
* samples past the official buffer are needed for differential |
207 |
|
|
* algorithms that always have to take subsequent samples into account |
208 |
|
|
* (resampling/interpolation would be an important example) and avoids |
209 |
|
|
* memory access faults in such cases. |
210 |
|
|
* |
211 |
|
|
* @param SampleCount - number of sample points to load into RAM |
212 |
|
|
* @param NullSamplesCount - number of silence samples the buffer should |
213 |
|
|
* be extended past it's data end |
214 |
|
|
* @returns buffer_t structure with start address and |
215 |
|
|
* size of the cached sample data in bytes |
216 |
|
|
* @see ReleaseSampleData(), Read(), SetPos() |
217 |
|
|
*/ |
218 |
|
|
buffer_t Sample::LoadSampleDataWithNullSamplesExtension(unsigned long SampleCount, uint NullSamplesCount) { |
219 |
|
|
if (SampleCount > this->SamplesTotal) SampleCount = this->SamplesTotal; |
220 |
|
|
if (RAMCache.pStart) delete[] (int8_t*) RAMCache.pStart; |
221 |
|
|
unsigned long allocationsize = (SampleCount + NullSamplesCount) * this->FrameSize; |
222 |
|
|
RAMCache.pStart = new int8_t[allocationsize]; |
223 |
|
|
RAMCache.Size = Read(RAMCache.pStart, SampleCount) * this->FrameSize; |
224 |
|
|
RAMCache.NullExtensionSize = allocationsize - RAMCache.Size; |
225 |
|
|
// fill the remaining buffer space with silence samples |
226 |
|
|
memset((int8_t*)RAMCache.pStart + RAMCache.Size, 0, RAMCache.NullExtensionSize); |
227 |
|
|
return GetCache(); |
228 |
|
|
} |
229 |
|
|
|
230 |
|
|
/** |
231 |
|
|
* Returns current cached sample points. A buffer_t structure will be |
232 |
|
|
* returned which contains address pointer to the begin of the cache and |
233 |
|
|
* the size of the cached sample data in bytes. Use |
234 |
|
|
* <i>LoadSampleData()</i> to cache a specific amount of sample points in |
235 |
|
|
* RAM. |
236 |
|
|
* |
237 |
|
|
* @returns buffer_t structure with current cached sample points |
238 |
|
|
* @see LoadSampleData(); |
239 |
|
|
*/ |
240 |
|
|
buffer_t Sample::GetCache() { |
241 |
|
|
// return a copy of the buffer_t structure |
242 |
|
|
buffer_t result; |
243 |
|
|
result.Size = this->RAMCache.Size; |
244 |
|
|
result.pStart = this->RAMCache.pStart; |
245 |
|
|
result.NullExtensionSize = this->RAMCache.NullExtensionSize; |
246 |
|
|
return result; |
247 |
|
|
} |
248 |
|
|
|
249 |
|
|
/** |
250 |
|
|
* Frees the cached sample from RAM if loaded with |
251 |
|
|
* <i>LoadSampleData()</i> previously. |
252 |
|
|
* |
253 |
|
|
* @see LoadSampleData(); |
254 |
|
|
*/ |
255 |
|
|
void Sample::ReleaseSampleData() { |
256 |
|
|
if (RAMCache.pStart) delete[] (int8_t*) RAMCache.pStart; |
257 |
|
|
RAMCache.pStart = NULL; |
258 |
|
|
RAMCache.Size = 0; |
259 |
|
|
} |
260 |
|
|
|
261 |
|
|
/** |
262 |
|
|
* Sets the position within the sample (in sample points, not in |
263 |
|
|
* bytes). Use this method and <i>Read()</i> if you don't want to load |
264 |
|
|
* the sample into RAM, thus for disk streaming. |
265 |
|
|
* |
266 |
|
|
* Although the original Gigasampler engine doesn't allow positioning |
267 |
|
|
* within compressed samples, I decided to implement it. Even though |
268 |
|
|
* the Gigasampler format doesn't allow to define loops for compressed |
269 |
|
|
* samples at the moment, positioning within compressed samples might be |
270 |
|
|
* interesting for some sampler engines though. The only drawback about |
271 |
|
|
* my decision is that it takes longer to load compressed gig Files on |
272 |
|
|
* startup, because it's neccessary to scan the samples for some |
273 |
|
|
* mandatory informations. But I think as it doesn't affect the runtime |
274 |
|
|
* efficiency, nobody will have a problem with that. |
275 |
|
|
* |
276 |
|
|
* @param SampleCount number of sample points to jump |
277 |
|
|
* @param Whence optional: to which relation \a SampleCount refers |
278 |
|
|
* to, if omited <i>RIFF::stream_start</i> is assumed |
279 |
|
|
* @returns the new sample position |
280 |
|
|
* @see Read() |
281 |
|
|
*/ |
282 |
|
|
unsigned long Sample::SetPos(unsigned long SampleCount, RIFF::stream_whence_t Whence) { |
283 |
|
|
if (Compressed) { |
284 |
|
|
switch (Whence) { |
285 |
|
|
case RIFF::stream_curpos: |
286 |
|
|
this->SamplePos += SampleCount; |
287 |
|
|
break; |
288 |
|
|
case RIFF::stream_end: |
289 |
|
|
this->SamplePos = this->SamplesTotal - 1 - SampleCount; |
290 |
|
|
break; |
291 |
|
|
case RIFF::stream_backward: |
292 |
|
|
this->SamplePos -= SampleCount; |
293 |
|
|
break; |
294 |
|
|
case RIFF::stream_start: default: |
295 |
|
|
this->SamplePos = SampleCount; |
296 |
|
|
break; |
297 |
|
|
} |
298 |
|
|
if (this->SamplePos > this->SamplesTotal) this->SamplePos = this->SamplesTotal; |
299 |
|
|
|
300 |
|
|
unsigned long frame = this->SamplePos / 2048; // to which frame to jump |
301 |
|
|
this->FrameOffset = this->SamplePos % 2048; // offset (in sample points) within that frame |
302 |
|
|
pCkData->SetPos(FrameTable[frame]); // set chunk pointer to the start of sought frame |
303 |
|
|
return this->SamplePos; |
304 |
|
|
} |
305 |
|
|
else { // not compressed |
306 |
|
|
unsigned long orderedBytes = SampleCount * this->FrameSize; |
307 |
|
|
unsigned long result = pCkData->SetPos(orderedBytes, Whence); |
308 |
|
|
return (result == orderedBytes) ? SampleCount |
309 |
|
|
: result / this->FrameSize; |
310 |
|
|
} |
311 |
|
|
} |
312 |
|
|
|
313 |
|
|
/** |
314 |
|
|
* Returns the current position in the sample (in sample points). |
315 |
|
|
*/ |
316 |
|
|
unsigned long Sample::GetPos() { |
317 |
|
|
if (Compressed) return SamplePos; |
318 |
|
|
else return pCkData->GetPos() / FrameSize; |
319 |
|
|
} |
320 |
|
|
|
321 |
|
|
/** |
322 |
schoenebeck |
24 |
* Reads \a SampleCount number of sample points from the position stored |
323 |
|
|
* in \a pPlaybackState into the buffer pointed by \a pBuffer and moves |
324 |
|
|
* the position within the sample respectively, this method honors the |
325 |
|
|
* looping informations of the sample (if any). The sample wave stream |
326 |
|
|
* will be decompressed on the fly if using a compressed sample. Use this |
327 |
|
|
* method if you don't want to load the sample into RAM, thus for disk |
328 |
|
|
* streaming. All this methods needs to know to proceed with streaming |
329 |
|
|
* for the next time you call this method is stored in \a pPlaybackState. |
330 |
|
|
* You have to allocate and initialize the playback_state_t structure by |
331 |
|
|
* yourself before you use it to stream a sample: |
332 |
|
|
* |
333 |
|
|
* <i> |
334 |
|
|
* gig::playback_state_t playbackstate; <br> |
335 |
|
|
* playbackstate.position = 0; <br> |
336 |
|
|
* playbackstate.reverse = false; <br> |
337 |
|
|
* playbackstate.loop_cycles_left = pSample->LoopPlayCount; <br> |
338 |
|
|
* </i> |
339 |
|
|
* |
340 |
|
|
* You don't have to take care of things like if there is actually a loop |
341 |
|
|
* defined or if the current read position is located within a loop area. |
342 |
|
|
* The method already handles such cases by itself. |
343 |
|
|
* |
344 |
|
|
* @param pBuffer destination buffer |
345 |
|
|
* @param SampleCount number of sample points to read |
346 |
|
|
* @param pPlaybackState will be used to store and reload the playback |
347 |
|
|
* state for the next ReadAndLoop() call |
348 |
|
|
* @returns number of successfully read sample points |
349 |
|
|
*/ |
350 |
|
|
unsigned long Sample::ReadAndLoop(void* pBuffer, unsigned long SampleCount, playback_state_t* pPlaybackState) { |
351 |
|
|
unsigned long samplestoread = SampleCount, totalreadsamples = 0, readsamples, samplestoloopend; |
352 |
|
|
uint8_t* pDst = (uint8_t*) pBuffer; |
353 |
|
|
|
354 |
|
|
SetPos(pPlaybackState->position); // recover position from the last time |
355 |
|
|
|
356 |
|
|
if (this->Loops && GetPos() <= this->LoopEnd) { // honor looping if there are loop points defined |
357 |
|
|
|
358 |
|
|
switch (this->LoopType) { |
359 |
|
|
|
360 |
|
|
case loop_type_bidirectional: { //TODO: not tested yet! |
361 |
|
|
do { |
362 |
|
|
// if not endless loop check if max. number of loop cycles have been passed |
363 |
|
|
if (this->LoopPlayCount && !pPlaybackState->loop_cycles_left) break; |
364 |
|
|
|
365 |
|
|
if (!pPlaybackState->reverse) { // forward playback |
366 |
|
|
do { |
367 |
|
|
samplestoloopend = this->LoopEnd - GetPos(); |
368 |
|
|
readsamples = Read(&pDst[totalreadsamples * this->FrameSize], Min(samplestoread, samplestoloopend)); |
369 |
|
|
samplestoread -= readsamples; |
370 |
|
|
totalreadsamples += readsamples; |
371 |
|
|
if (readsamples == samplestoloopend) { |
372 |
|
|
pPlaybackState->reverse = true; |
373 |
|
|
break; |
374 |
|
|
} |
375 |
|
|
} while (samplestoread && readsamples); |
376 |
|
|
} |
377 |
|
|
else { // backward playback |
378 |
|
|
|
379 |
|
|
// as we can only read forward from disk, we have to |
380 |
|
|
// determine the end position within the loop first, |
381 |
|
|
// read forward from that 'end' and finally after |
382 |
|
|
// reading, swap all sample frames so it reflects |
383 |
|
|
// backward playback |
384 |
|
|
|
385 |
|
|
unsigned long swapareastart = totalreadsamples; |
386 |
|
|
unsigned long loopoffset = GetPos() - this->LoopStart; |
387 |
|
|
unsigned long samplestoreadinloop = Min(samplestoread, loopoffset); |
388 |
|
|
unsigned long reverseplaybackend = GetPos() - samplestoreadinloop; |
389 |
|
|
|
390 |
|
|
SetPos(reverseplaybackend); |
391 |
|
|
|
392 |
|
|
// read samples for backward playback |
393 |
|
|
do { |
394 |
|
|
readsamples = Read(&pDst[totalreadsamples * this->FrameSize], samplestoreadinloop); |
395 |
|
|
samplestoreadinloop -= readsamples; |
396 |
|
|
samplestoread -= readsamples; |
397 |
|
|
totalreadsamples += readsamples; |
398 |
|
|
} while (samplestoreadinloop && readsamples); |
399 |
|
|
|
400 |
|
|
SetPos(reverseplaybackend); // pretend we really read backwards |
401 |
|
|
|
402 |
|
|
if (reverseplaybackend == this->LoopStart) { |
403 |
|
|
pPlaybackState->loop_cycles_left--; |
404 |
|
|
pPlaybackState->reverse = false; |
405 |
|
|
} |
406 |
|
|
|
407 |
|
|
// reverse the sample frames for backward playback |
408 |
|
|
SwapMemoryArea(&pDst[swapareastart * this->FrameSize], (totalreadsamples - swapareastart) * this->FrameSize, this->FrameSize); |
409 |
|
|
} |
410 |
|
|
} while (samplestoread && readsamples); |
411 |
|
|
break; |
412 |
|
|
} |
413 |
|
|
|
414 |
|
|
case loop_type_backward: { // TODO: not tested yet! |
415 |
|
|
// forward playback (not entered the loop yet) |
416 |
|
|
if (!pPlaybackState->reverse) do { |
417 |
|
|
samplestoloopend = this->LoopEnd - GetPos(); |
418 |
|
|
readsamples = Read(&pDst[totalreadsamples * this->FrameSize], Min(samplestoread, samplestoloopend)); |
419 |
|
|
samplestoread -= readsamples; |
420 |
|
|
totalreadsamples += readsamples; |
421 |
|
|
if (readsamples == samplestoloopend) { |
422 |
|
|
pPlaybackState->reverse = true; |
423 |
|
|
break; |
424 |
|
|
} |
425 |
|
|
} while (samplestoread && readsamples); |
426 |
|
|
|
427 |
|
|
if (!samplestoread) break; |
428 |
|
|
|
429 |
|
|
// as we can only read forward from disk, we have to |
430 |
|
|
// determine the end position within the loop first, |
431 |
|
|
// read forward from that 'end' and finally after |
432 |
|
|
// reading, swap all sample frames so it reflects |
433 |
|
|
// backward playback |
434 |
|
|
|
435 |
|
|
unsigned long swapareastart = totalreadsamples; |
436 |
|
|
unsigned long loopoffset = GetPos() - this->LoopStart; |
437 |
|
|
unsigned long samplestoreadinloop = (this->LoopPlayCount) ? Min(samplestoread, pPlaybackState->loop_cycles_left * LoopSize - loopoffset) |
438 |
|
|
: samplestoread; |
439 |
|
|
unsigned long reverseplaybackend = this->LoopStart + Abs((loopoffset - samplestoreadinloop) % this->LoopSize); |
440 |
|
|
|
441 |
|
|
SetPos(reverseplaybackend); |
442 |
|
|
|
443 |
|
|
// read samples for backward playback |
444 |
|
|
do { |
445 |
|
|
// if not endless loop check if max. number of loop cycles have been passed |
446 |
|
|
if (this->LoopPlayCount && !pPlaybackState->loop_cycles_left) break; |
447 |
|
|
samplestoloopend = this->LoopEnd - GetPos(); |
448 |
|
|
readsamples = Read(&pDst[totalreadsamples * this->FrameSize], Min(samplestoreadinloop, samplestoloopend)); |
449 |
|
|
samplestoreadinloop -= readsamples; |
450 |
|
|
samplestoread -= readsamples; |
451 |
|
|
totalreadsamples += readsamples; |
452 |
|
|
if (readsamples == samplestoloopend) { |
453 |
|
|
pPlaybackState->loop_cycles_left--; |
454 |
|
|
SetPos(this->LoopStart); |
455 |
|
|
} |
456 |
|
|
} while (samplestoreadinloop && readsamples); |
457 |
|
|
|
458 |
|
|
SetPos(reverseplaybackend); // pretend we really read backwards |
459 |
|
|
|
460 |
|
|
// reverse the sample frames for backward playback |
461 |
|
|
SwapMemoryArea(&pDst[swapareastart * this->FrameSize], (totalreadsamples - swapareastart) * this->FrameSize, this->FrameSize); |
462 |
|
|
break; |
463 |
|
|
} |
464 |
|
|
|
465 |
|
|
default: case loop_type_normal: { |
466 |
|
|
do { |
467 |
|
|
// if not endless loop check if max. number of loop cycles have been passed |
468 |
|
|
if (this->LoopPlayCount && !pPlaybackState->loop_cycles_left) break; |
469 |
|
|
samplestoloopend = this->LoopEnd - GetPos(); |
470 |
|
|
readsamples = Read(&pDst[totalreadsamples * this->FrameSize], Min(samplestoread, samplestoloopend)); |
471 |
|
|
samplestoread -= readsamples; |
472 |
|
|
totalreadsamples += readsamples; |
473 |
|
|
if (readsamples == samplestoloopend) { |
474 |
|
|
pPlaybackState->loop_cycles_left--; |
475 |
|
|
SetPos(this->LoopStart); |
476 |
|
|
} |
477 |
|
|
} while (samplestoread && readsamples); |
478 |
|
|
break; |
479 |
|
|
} |
480 |
|
|
} |
481 |
|
|
} |
482 |
|
|
|
483 |
|
|
// read on without looping |
484 |
|
|
if (samplestoread) do { |
485 |
|
|
readsamples = Read(&pDst[totalreadsamples * this->FrameSize], samplestoread); |
486 |
|
|
samplestoread -= readsamples; |
487 |
|
|
totalreadsamples += readsamples; |
488 |
|
|
} while (readsamples && samplestoread); |
489 |
|
|
|
490 |
|
|
// store current position |
491 |
|
|
pPlaybackState->position = GetPos(); |
492 |
|
|
|
493 |
|
|
return totalreadsamples; |
494 |
|
|
} |
495 |
|
|
|
496 |
|
|
/** |
497 |
schoenebeck |
2 |
* Reads \a SampleCount number of sample points from the current |
498 |
|
|
* position into the buffer pointed by \a pBuffer and increments the |
499 |
|
|
* position within the sample. The sample wave stream will be |
500 |
|
|
* decompressed on the fly if using a compressed sample. Use this method |
501 |
|
|
* and <i>SetPos()</i> if you don't want to load the sample into RAM, |
502 |
|
|
* thus for disk streaming. |
503 |
|
|
* |
504 |
|
|
* @param pBuffer destination buffer |
505 |
|
|
* @param SampleCount number of sample points to read |
506 |
|
|
* @returns number of successfully read sample points |
507 |
|
|
* @see SetPos() |
508 |
|
|
*/ |
509 |
|
|
unsigned long Sample::Read(void* pBuffer, unsigned long SampleCount) { |
510 |
schoenebeck |
21 |
if (SampleCount == 0) return 0; |
511 |
schoenebeck |
317 |
if (!Compressed) { |
512 |
|
|
if (BitDepth == 24) { |
513 |
|
|
// 24 bit sample. For now just truncate to 16 bit. |
514 |
|
|
int8_t* pSrc = (int8_t*)this->pDecompressionBuffer; |
515 |
|
|
int8_t* pDst = (int8_t*)pBuffer; |
516 |
|
|
unsigned long n = pCkData->Read(pSrc, SampleCount, FrameSize); |
517 |
|
|
for (int i = SampleCount * (FrameSize / 3) ; i > 0 ; i--) { |
518 |
|
|
pSrc++; |
519 |
|
|
*pDst++ = *pSrc++; |
520 |
|
|
*pDst++ = *pSrc++; |
521 |
|
|
} |
522 |
|
|
return SampleCount; |
523 |
|
|
} else { |
524 |
|
|
return pCkData->Read(pBuffer, SampleCount, FrameSize); //FIXME: channel inversion due to endian correction? |
525 |
|
|
} |
526 |
|
|
} |
527 |
schoenebeck |
2 |
else { //FIXME: no support for mono compressed samples yet, are there any? |
528 |
schoenebeck |
11 |
if (this->SamplePos >= this->SamplesTotal) return 0; |
529 |
schoenebeck |
2 |
//TODO: efficiency: we simply assume here that all frames are compressed, maybe we should test for an average compression rate |
530 |
|
|
// best case needed buffer size (all frames compressed) |
531 |
|
|
unsigned long assumedsize = (SampleCount << 1) + // *2 (16 Bit, stereo, but assume all frames compressed) |
532 |
|
|
(SampleCount >> 10) + // 10 bytes header per 2048 sample points |
533 |
|
|
8194, // at least one worst case sample frame |
534 |
|
|
remainingbytes = 0, // remaining bytes in the local buffer |
535 |
|
|
remainingsamples = SampleCount, |
536 |
|
|
copysamples; |
537 |
|
|
int currentframeoffset = this->FrameOffset; // offset in current sample frame since last Read() |
538 |
|
|
this->FrameOffset = 0; |
539 |
|
|
|
540 |
|
|
if (assumedsize > this->DecompressionBufferSize) { |
541 |
|
|
// local buffer reallocation - hope this won't happen |
542 |
|
|
if (this->pDecompressionBuffer) delete[] (int8_t*) this->pDecompressionBuffer; |
543 |
|
|
this->pDecompressionBuffer = new int8_t[assumedsize << 1]; // double of current needed size |
544 |
|
|
this->DecompressionBufferSize = assumedsize; |
545 |
|
|
} |
546 |
|
|
|
547 |
|
|
int16_t compressionmode, left, dleft, right, dright; |
548 |
|
|
int8_t* pSrc = (int8_t*) this->pDecompressionBuffer; |
549 |
|
|
int16_t* pDst = (int16_t*) pBuffer; |
550 |
|
|
remainingbytes = pCkData->Read(pSrc, assumedsize, 1); |
551 |
|
|
|
552 |
|
|
while (remainingsamples) { |
553 |
|
|
|
554 |
|
|
// reload from disk to local buffer if needed |
555 |
|
|
if (remainingbytes < 8194) { |
556 |
|
|
if (pCkData->GetState() != RIFF::stream_ready) { |
557 |
schoenebeck |
11 |
this->SamplePos = this->SamplesTotal; |
558 |
schoenebeck |
2 |
return (SampleCount - remainingsamples); |
559 |
|
|
} |
560 |
|
|
assumedsize = remainingsamples; |
561 |
|
|
assumedsize = (assumedsize << 1) + // *2 (16 Bit, stereo, but assume all frames compressed) |
562 |
|
|
(assumedsize >> 10) + // 10 bytes header per 2048 sample points |
563 |
|
|
8194; // at least one worst case sample frame |
564 |
|
|
pCkData->SetPos(remainingbytes, RIFF::stream_backward); |
565 |
|
|
if (pCkData->RemainingBytes() < assumedsize) assumedsize = pCkData->RemainingBytes(); |
566 |
|
|
remainingbytes = pCkData->Read(this->pDecompressionBuffer, assumedsize, 1); |
567 |
|
|
pSrc = (int8_t*) this->pDecompressionBuffer; |
568 |
|
|
} |
569 |
|
|
|
570 |
|
|
// determine how many samples in this frame to skip and read |
571 |
|
|
if (remainingsamples >= 2048) { |
572 |
|
|
copysamples = 2048 - currentframeoffset; |
573 |
|
|
remainingsamples -= copysamples; |
574 |
|
|
} |
575 |
|
|
else { |
576 |
|
|
copysamples = remainingsamples; |
577 |
|
|
if (currentframeoffset + copysamples > 2048) { |
578 |
|
|
copysamples = 2048 - currentframeoffset; |
579 |
|
|
remainingsamples -= copysamples; |
580 |
|
|
} |
581 |
|
|
else { |
582 |
|
|
pCkData->SetPos(remainingbytes, RIFF::stream_backward); |
583 |
|
|
remainingsamples = 0; |
584 |
|
|
this->FrameOffset = currentframeoffset + copysamples; |
585 |
|
|
} |
586 |
|
|
} |
587 |
|
|
|
588 |
|
|
// decompress and copy current frame from local buffer to destination buffer |
589 |
|
|
compressionmode = *(int16_t*)pSrc; pSrc+=2; |
590 |
|
|
switch (compressionmode) { |
591 |
|
|
case 1: // left channel compressed |
592 |
|
|
remainingbytes -= 6150; // (left 8 bit, right 16 bit, +6 byte header) |
593 |
|
|
if (!remainingsamples && copysamples == 2048) |
594 |
|
|
pCkData->SetPos(remainingbytes, RIFF::stream_backward); |
595 |
|
|
|
596 |
|
|
left = *(int16_t*)pSrc; pSrc+=2; |
597 |
|
|
dleft = *(int16_t*)pSrc; pSrc+=2; |
598 |
|
|
while (currentframeoffset) { |
599 |
|
|
dleft -= *pSrc; |
600 |
|
|
left -= dleft; |
601 |
|
|
pSrc+=3; // 8 bit left channel, skip uncompressed right channel (16 bit) |
602 |
|
|
currentframeoffset--; |
603 |
|
|
} |
604 |
|
|
while (copysamples) { |
605 |
|
|
dleft -= *pSrc; pSrc++; |
606 |
|
|
left -= dleft; |
607 |
|
|
*pDst = left; pDst++; |
608 |
|
|
*pDst = *(int16_t*)pSrc; pDst++; pSrc+=2; |
609 |
|
|
copysamples--; |
610 |
|
|
} |
611 |
|
|
break; |
612 |
|
|
case 256: // right channel compressed |
613 |
|
|
remainingbytes -= 6150; // (left 16 bit, right 8 bit, +6 byte header) |
614 |
|
|
if (!remainingsamples && copysamples == 2048) |
615 |
|
|
pCkData->SetPos(remainingbytes, RIFF::stream_backward); |
616 |
|
|
|
617 |
|
|
right = *(int16_t*)pSrc; pSrc+=2; |
618 |
|
|
dright = *(int16_t*)pSrc; pSrc+=2; |
619 |
|
|
if (currentframeoffset) { |
620 |
|
|
pSrc+=2; // skip uncompressed left channel, now we can increment by 3 |
621 |
|
|
while (currentframeoffset) { |
622 |
|
|
dright -= *pSrc; |
623 |
|
|
right -= dright; |
624 |
|
|
pSrc+=3; // 8 bit right channel, skip uncompressed left channel (16 bit) |
625 |
|
|
currentframeoffset--; |
626 |
|
|
} |
627 |
|
|
pSrc-=2; // back aligned to left channel |
628 |
|
|
} |
629 |
|
|
while (copysamples) { |
630 |
|
|
*pDst = *(int16_t*)pSrc; pDst++; pSrc+=2; |
631 |
|
|
dright -= *pSrc; pSrc++; |
632 |
|
|
right -= dright; |
633 |
|
|
*pDst = right; pDst++; |
634 |
|
|
copysamples--; |
635 |
|
|
} |
636 |
|
|
break; |
637 |
|
|
case 257: // both channels compressed |
638 |
|
|
remainingbytes -= 4106; // (left 8 bit, right 8 bit, +10 byte header) |
639 |
|
|
if (!remainingsamples && copysamples == 2048) |
640 |
|
|
pCkData->SetPos(remainingbytes, RIFF::stream_backward); |
641 |
|
|
|
642 |
|
|
left = *(int16_t*)pSrc; pSrc+=2; |
643 |
|
|
dleft = *(int16_t*)pSrc; pSrc+=2; |
644 |
|
|
right = *(int16_t*)pSrc; pSrc+=2; |
645 |
|
|
dright = *(int16_t*)pSrc; pSrc+=2; |
646 |
|
|
while (currentframeoffset) { |
647 |
|
|
dleft -= *pSrc; pSrc++; |
648 |
|
|
left -= dleft; |
649 |
|
|
dright -= *pSrc; pSrc++; |
650 |
|
|
right -= dright; |
651 |
|
|
currentframeoffset--; |
652 |
|
|
} |
653 |
|
|
while (copysamples) { |
654 |
|
|
dleft -= *pSrc; pSrc++; |
655 |
|
|
left -= dleft; |
656 |
|
|
dright -= *pSrc; pSrc++; |
657 |
|
|
right -= dright; |
658 |
|
|
*pDst = left; pDst++; |
659 |
|
|
*pDst = right; pDst++; |
660 |
|
|
copysamples--; |
661 |
|
|
} |
662 |
|
|
break; |
663 |
|
|
default: // both channels uncompressed |
664 |
|
|
remainingbytes -= 8194; // (left 16 bit, right 16 bit, +2 byte header) |
665 |
|
|
if (!remainingsamples && copysamples == 2048) |
666 |
|
|
pCkData->SetPos(remainingbytes, RIFF::stream_backward); |
667 |
|
|
|
668 |
|
|
pSrc += currentframeoffset << 2; |
669 |
|
|
currentframeoffset = 0; |
670 |
|
|
memcpy(pDst, pSrc, copysamples << 2); |
671 |
|
|
pDst += copysamples << 1; |
672 |
|
|
pSrc += copysamples << 2; |
673 |
|
|
break; |
674 |
|
|
} |
675 |
|
|
} |
676 |
|
|
this->SamplePos += (SampleCount - remainingsamples); |
677 |
schoenebeck |
11 |
if (this->SamplePos > this->SamplesTotal) this->SamplePos = this->SamplesTotal; |
678 |
schoenebeck |
2 |
return (SampleCount - remainingsamples); |
679 |
|
|
} |
680 |
|
|
} |
681 |
|
|
|
682 |
|
|
Sample::~Sample() { |
683 |
|
|
Instances--; |
684 |
|
|
if (!Instances && pDecompressionBuffer) delete[] (int8_t*) pDecompressionBuffer; |
685 |
|
|
if (FrameTable) delete[] FrameTable; |
686 |
|
|
if (RAMCache.pStart) delete[] (int8_t*) RAMCache.pStart; |
687 |
|
|
} |
688 |
|
|
|
689 |
|
|
|
690 |
|
|
|
691 |
|
|
// *************** DimensionRegion *************** |
692 |
|
|
// * |
693 |
|
|
|
694 |
schoenebeck |
16 |
uint DimensionRegion::Instances = 0; |
695 |
|
|
DimensionRegion::VelocityTableMap* DimensionRegion::pVelocityTables = NULL; |
696 |
|
|
|
697 |
schoenebeck |
2 |
DimensionRegion::DimensionRegion(RIFF::List* _3ewl) : DLS::Sampler(_3ewl) { |
698 |
schoenebeck |
16 |
Instances++; |
699 |
|
|
|
700 |
schoenebeck |
2 |
memcpy(&Crossfade, &SamplerOptions, 4); |
701 |
schoenebeck |
16 |
if (!pVelocityTables) pVelocityTables = new VelocityTableMap; |
702 |
schoenebeck |
2 |
|
703 |
|
|
RIFF::Chunk* _3ewa = _3ewl->GetSubChunk(CHUNK_ID_3EWA); |
704 |
schoenebeck |
241 |
_3ewa->ReadInt32(); // unknown, always 0x0000008C ? |
705 |
schoenebeck |
2 |
LFO3Frequency = (double) GIG_EXP_DECODE(_3ewa->ReadInt32()); |
706 |
|
|
EG3Attack = (double) GIG_EXP_DECODE(_3ewa->ReadInt32()); |
707 |
|
|
_3ewa->ReadInt16(); // unknown |
708 |
|
|
LFO1InternalDepth = _3ewa->ReadUint16(); |
709 |
|
|
_3ewa->ReadInt16(); // unknown |
710 |
|
|
LFO3InternalDepth = _3ewa->ReadInt16(); |
711 |
|
|
_3ewa->ReadInt16(); // unknown |
712 |
|
|
LFO1ControlDepth = _3ewa->ReadUint16(); |
713 |
|
|
_3ewa->ReadInt16(); // unknown |
714 |
|
|
LFO3ControlDepth = _3ewa->ReadInt16(); |
715 |
|
|
EG1Attack = (double) GIG_EXP_DECODE(_3ewa->ReadInt32()); |
716 |
|
|
EG1Decay1 = (double) GIG_EXP_DECODE(_3ewa->ReadInt32()); |
717 |
|
|
_3ewa->ReadInt16(); // unknown |
718 |
|
|
EG1Sustain = _3ewa->ReadUint16(); |
719 |
|
|
EG1Release = (double) GIG_EXP_DECODE(_3ewa->ReadInt32()); |
720 |
schoenebeck |
36 |
EG1Controller = DecodeLeverageController(static_cast<_lev_ctrl_t>(_3ewa->ReadUint8())); |
721 |
schoenebeck |
2 |
uint8_t eg1ctrloptions = _3ewa->ReadUint8(); |
722 |
|
|
EG1ControllerInvert = eg1ctrloptions & 0x01; |
723 |
|
|
EG1ControllerAttackInfluence = GIG_EG_CTR_ATTACK_INFLUENCE_EXTRACT(eg1ctrloptions); |
724 |
|
|
EG1ControllerDecayInfluence = GIG_EG_CTR_DECAY_INFLUENCE_EXTRACT(eg1ctrloptions); |
725 |
|
|
EG1ControllerReleaseInfluence = GIG_EG_CTR_RELEASE_INFLUENCE_EXTRACT(eg1ctrloptions); |
726 |
schoenebeck |
36 |
EG2Controller = DecodeLeverageController(static_cast<_lev_ctrl_t>(_3ewa->ReadUint8())); |
727 |
schoenebeck |
2 |
uint8_t eg2ctrloptions = _3ewa->ReadUint8(); |
728 |
|
|
EG2ControllerInvert = eg2ctrloptions & 0x01; |
729 |
|
|
EG2ControllerAttackInfluence = GIG_EG_CTR_ATTACK_INFLUENCE_EXTRACT(eg2ctrloptions); |
730 |
|
|
EG2ControllerDecayInfluence = GIG_EG_CTR_DECAY_INFLUENCE_EXTRACT(eg2ctrloptions); |
731 |
|
|
EG2ControllerReleaseInfluence = GIG_EG_CTR_RELEASE_INFLUENCE_EXTRACT(eg2ctrloptions); |
732 |
|
|
LFO1Frequency = (double) GIG_EXP_DECODE(_3ewa->ReadInt32()); |
733 |
|
|
EG2Attack = (double) GIG_EXP_DECODE(_3ewa->ReadInt32()); |
734 |
|
|
EG2Decay1 = (double) GIG_EXP_DECODE(_3ewa->ReadInt32()); |
735 |
|
|
_3ewa->ReadInt16(); // unknown |
736 |
|
|
EG2Sustain = _3ewa->ReadUint16(); |
737 |
|
|
EG2Release = (double) GIG_EXP_DECODE(_3ewa->ReadInt32()); |
738 |
|
|
_3ewa->ReadInt16(); // unknown |
739 |
|
|
LFO2ControlDepth = _3ewa->ReadUint16(); |
740 |
|
|
LFO2Frequency = (double) GIG_EXP_DECODE(_3ewa->ReadInt32()); |
741 |
|
|
_3ewa->ReadInt16(); // unknown |
742 |
|
|
LFO2InternalDepth = _3ewa->ReadUint16(); |
743 |
|
|
int32_t eg1decay2 = _3ewa->ReadInt32(); |
744 |
|
|
EG1Decay2 = (double) GIG_EXP_DECODE(eg1decay2); |
745 |
|
|
EG1InfiniteSustain = (eg1decay2 == 0x7fffffff); |
746 |
|
|
_3ewa->ReadInt16(); // unknown |
747 |
|
|
EG1PreAttack = _3ewa->ReadUint16(); |
748 |
|
|
int32_t eg2decay2 = _3ewa->ReadInt32(); |
749 |
|
|
EG2Decay2 = (double) GIG_EXP_DECODE(eg2decay2); |
750 |
|
|
EG2InfiniteSustain = (eg2decay2 == 0x7fffffff); |
751 |
|
|
_3ewa->ReadInt16(); // unknown |
752 |
|
|
EG2PreAttack = _3ewa->ReadUint16(); |
753 |
|
|
uint8_t velocityresponse = _3ewa->ReadUint8(); |
754 |
|
|
if (velocityresponse < 5) { |
755 |
|
|
VelocityResponseCurve = curve_type_nonlinear; |
756 |
|
|
VelocityResponseDepth = velocityresponse; |
757 |
|
|
} |
758 |
|
|
else if (velocityresponse < 10) { |
759 |
|
|
VelocityResponseCurve = curve_type_linear; |
760 |
|
|
VelocityResponseDepth = velocityresponse - 5; |
761 |
|
|
} |
762 |
|
|
else if (velocityresponse < 15) { |
763 |
|
|
VelocityResponseCurve = curve_type_special; |
764 |
|
|
VelocityResponseDepth = velocityresponse - 10; |
765 |
|
|
} |
766 |
|
|
else { |
767 |
|
|
VelocityResponseCurve = curve_type_unknown; |
768 |
|
|
VelocityResponseDepth = 0; |
769 |
|
|
} |
770 |
|
|
uint8_t releasevelocityresponse = _3ewa->ReadUint8(); |
771 |
|
|
if (releasevelocityresponse < 5) { |
772 |
|
|
ReleaseVelocityResponseCurve = curve_type_nonlinear; |
773 |
|
|
ReleaseVelocityResponseDepth = releasevelocityresponse; |
774 |
|
|
} |
775 |
|
|
else if (releasevelocityresponse < 10) { |
776 |
|
|
ReleaseVelocityResponseCurve = curve_type_linear; |
777 |
|
|
ReleaseVelocityResponseDepth = releasevelocityresponse - 5; |
778 |
|
|
} |
779 |
|
|
else if (releasevelocityresponse < 15) { |
780 |
|
|
ReleaseVelocityResponseCurve = curve_type_special; |
781 |
|
|
ReleaseVelocityResponseDepth = releasevelocityresponse - 10; |
782 |
|
|
} |
783 |
|
|
else { |
784 |
|
|
ReleaseVelocityResponseCurve = curve_type_unknown; |
785 |
|
|
ReleaseVelocityResponseDepth = 0; |
786 |
|
|
} |
787 |
|
|
VelocityResponseCurveScaling = _3ewa->ReadUint8(); |
788 |
schoenebeck |
36 |
AttenuationControllerThreshold = _3ewa->ReadInt8(); |
789 |
schoenebeck |
2 |
_3ewa->ReadInt32(); // unknown |
790 |
|
|
SampleStartOffset = (uint16_t) _3ewa->ReadInt16(); |
791 |
|
|
_3ewa->ReadInt16(); // unknown |
792 |
|
|
uint8_t pitchTrackDimensionBypass = _3ewa->ReadInt8(); |
793 |
|
|
PitchTrack = GIG_PITCH_TRACK_EXTRACT(pitchTrackDimensionBypass); |
794 |
|
|
if (pitchTrackDimensionBypass & 0x10) DimensionBypass = dim_bypass_ctrl_94; |
795 |
|
|
else if (pitchTrackDimensionBypass & 0x20) DimensionBypass = dim_bypass_ctrl_95; |
796 |
|
|
else DimensionBypass = dim_bypass_ctrl_none; |
797 |
|
|
uint8_t pan = _3ewa->ReadUint8(); |
798 |
schoenebeck |
269 |
Pan = (pan < 64) ? pan : -((int)pan - 63); // signed 7 bit -> signed 8 bit |
799 |
schoenebeck |
2 |
SelfMask = _3ewa->ReadInt8() & 0x01; |
800 |
|
|
_3ewa->ReadInt8(); // unknown |
801 |
|
|
uint8_t lfo3ctrl = _3ewa->ReadUint8(); |
802 |
|
|
LFO3Controller = static_cast<lfo3_ctrl_t>(lfo3ctrl & 0x07); // lower 3 bits |
803 |
|
|
LFO3Sync = lfo3ctrl & 0x20; // bit 5 |
804 |
schoenebeck |
36 |
InvertAttenuationController = lfo3ctrl & 0x80; // bit 7 |
805 |
schoenebeck |
2 |
if (VCFType == vcf_type_lowpass) { |
806 |
|
|
if (lfo3ctrl & 0x40) // bit 6 |
807 |
|
|
VCFType = vcf_type_lowpassturbo; |
808 |
|
|
} |
809 |
schoenebeck |
36 |
AttenuationController = DecodeLeverageController(static_cast<_lev_ctrl_t>(_3ewa->ReadUint8())); |
810 |
schoenebeck |
2 |
uint8_t lfo2ctrl = _3ewa->ReadUint8(); |
811 |
|
|
LFO2Controller = static_cast<lfo2_ctrl_t>(lfo2ctrl & 0x07); // lower 3 bits |
812 |
|
|
LFO2FlipPhase = lfo2ctrl & 0x80; // bit 7 |
813 |
|
|
LFO2Sync = lfo2ctrl & 0x20; // bit 5 |
814 |
|
|
bool extResonanceCtrl = lfo2ctrl & 0x40; // bit 6 |
815 |
|
|
uint8_t lfo1ctrl = _3ewa->ReadUint8(); |
816 |
|
|
LFO1Controller = static_cast<lfo1_ctrl_t>(lfo1ctrl & 0x07); // lower 3 bits |
817 |
|
|
LFO1FlipPhase = lfo1ctrl & 0x80; // bit 7 |
818 |
|
|
LFO1Sync = lfo1ctrl & 0x40; // bit 6 |
819 |
|
|
VCFResonanceController = (extResonanceCtrl) ? static_cast<vcf_res_ctrl_t>(GIG_VCF_RESONANCE_CTRL_EXTRACT(lfo1ctrl)) |
820 |
|
|
: vcf_res_ctrl_none; |
821 |
|
|
uint16_t eg3depth = _3ewa->ReadUint16(); |
822 |
|
|
EG3Depth = (eg3depth <= 1200) ? eg3depth /* positives */ |
823 |
|
|
: (-1) * (int16_t) ((eg3depth ^ 0xffff) + 1); /* binary complementary for negatives */ |
824 |
|
|
_3ewa->ReadInt16(); // unknown |
825 |
|
|
ChannelOffset = _3ewa->ReadUint8() / 4; |
826 |
|
|
uint8_t regoptions = _3ewa->ReadUint8(); |
827 |
|
|
MSDecode = regoptions & 0x01; // bit 0 |
828 |
|
|
SustainDefeat = regoptions & 0x02; // bit 1 |
829 |
|
|
_3ewa->ReadInt16(); // unknown |
830 |
|
|
VelocityUpperLimit = _3ewa->ReadInt8(); |
831 |
|
|
_3ewa->ReadInt8(); // unknown |
832 |
|
|
_3ewa->ReadInt16(); // unknown |
833 |
|
|
ReleaseTriggerDecay = _3ewa->ReadUint8(); // release trigger decay |
834 |
|
|
_3ewa->ReadInt8(); // unknown |
835 |
|
|
_3ewa->ReadInt8(); // unknown |
836 |
|
|
EG1Hold = _3ewa->ReadUint8() & 0x80; // bit 7 |
837 |
|
|
uint8_t vcfcutoff = _3ewa->ReadUint8(); |
838 |
|
|
VCFEnabled = vcfcutoff & 0x80; // bit 7 |
839 |
|
|
VCFCutoff = vcfcutoff & 0x7f; // lower 7 bits |
840 |
|
|
VCFCutoffController = static_cast<vcf_cutoff_ctrl_t>(_3ewa->ReadUint8()); |
841 |
|
|
VCFVelocityScale = _3ewa->ReadUint8(); |
842 |
|
|
_3ewa->ReadInt8(); // unknown |
843 |
|
|
uint8_t vcfresonance = _3ewa->ReadUint8(); |
844 |
|
|
VCFResonance = vcfresonance & 0x7f; // lower 7 bits |
845 |
|
|
VCFResonanceDynamic = !(vcfresonance & 0x80); // bit 7 |
846 |
|
|
uint8_t vcfbreakpoint = _3ewa->ReadUint8(); |
847 |
|
|
VCFKeyboardTracking = vcfbreakpoint & 0x80; // bit 7 |
848 |
|
|
VCFKeyboardTrackingBreakpoint = vcfbreakpoint & 0x7f; // lower 7 bits |
849 |
|
|
uint8_t vcfvelocity = _3ewa->ReadUint8(); |
850 |
|
|
VCFVelocityDynamicRange = vcfvelocity % 5; |
851 |
|
|
VCFVelocityCurve = static_cast<curve_type_t>(vcfvelocity / 5); |
852 |
|
|
VCFType = static_cast<vcf_type_t>(_3ewa->ReadUint8()); |
853 |
schoenebeck |
16 |
|
854 |
|
|
// get the corresponding velocity->volume table from the table map or create & calculate that table if it doesn't exist yet |
855 |
|
|
uint32_t tableKey = (VelocityResponseCurve<<16) | (VelocityResponseDepth<<8) | VelocityResponseCurveScaling; |
856 |
|
|
if (pVelocityTables->count(tableKey)) { // if key exists |
857 |
|
|
pVelocityAttenuationTable = (*pVelocityTables)[tableKey]; |
858 |
|
|
} |
859 |
|
|
else { |
860 |
schoenebeck |
317 |
pVelocityAttenuationTable = |
861 |
schoenebeck |
308 |
CreateVelocityTable(VelocityResponseCurve, |
862 |
|
|
VelocityResponseDepth, |
863 |
|
|
VelocityResponseCurveScaling); |
864 |
schoenebeck |
16 |
(*pVelocityTables)[tableKey] = pVelocityAttenuationTable; // put the new table into the tables map |
865 |
|
|
} |
866 |
schoenebeck |
2 |
} |
867 |
schoenebeck |
55 |
|
868 |
schoenebeck |
36 |
leverage_ctrl_t DimensionRegion::DecodeLeverageController(_lev_ctrl_t EncodedController) { |
869 |
|
|
leverage_ctrl_t decodedcontroller; |
870 |
|
|
switch (EncodedController) { |
871 |
|
|
// special controller |
872 |
|
|
case _lev_ctrl_none: |
873 |
|
|
decodedcontroller.type = leverage_ctrl_t::type_none; |
874 |
|
|
decodedcontroller.controller_number = 0; |
875 |
|
|
break; |
876 |
|
|
case _lev_ctrl_velocity: |
877 |
|
|
decodedcontroller.type = leverage_ctrl_t::type_velocity; |
878 |
|
|
decodedcontroller.controller_number = 0; |
879 |
|
|
break; |
880 |
|
|
case _lev_ctrl_channelaftertouch: |
881 |
|
|
decodedcontroller.type = leverage_ctrl_t::type_channelaftertouch; |
882 |
|
|
decodedcontroller.controller_number = 0; |
883 |
|
|
break; |
884 |
schoenebeck |
55 |
|
885 |
schoenebeck |
36 |
// ordinary MIDI control change controller |
886 |
|
|
case _lev_ctrl_modwheel: |
887 |
|
|
decodedcontroller.type = leverage_ctrl_t::type_controlchange; |
888 |
|
|
decodedcontroller.controller_number = 1; |
889 |
|
|
break; |
890 |
|
|
case _lev_ctrl_breath: |
891 |
|
|
decodedcontroller.type = leverage_ctrl_t::type_controlchange; |
892 |
|
|
decodedcontroller.controller_number = 2; |
893 |
|
|
break; |
894 |
|
|
case _lev_ctrl_foot: |
895 |
|
|
decodedcontroller.type = leverage_ctrl_t::type_controlchange; |
896 |
|
|
decodedcontroller.controller_number = 4; |
897 |
|
|
break; |
898 |
|
|
case _lev_ctrl_effect1: |
899 |
|
|
decodedcontroller.type = leverage_ctrl_t::type_controlchange; |
900 |
|
|
decodedcontroller.controller_number = 12; |
901 |
|
|
break; |
902 |
|
|
case _lev_ctrl_effect2: |
903 |
|
|
decodedcontroller.type = leverage_ctrl_t::type_controlchange; |
904 |
|
|
decodedcontroller.controller_number = 13; |
905 |
|
|
break; |
906 |
|
|
case _lev_ctrl_genpurpose1: |
907 |
|
|
decodedcontroller.type = leverage_ctrl_t::type_controlchange; |
908 |
|
|
decodedcontroller.controller_number = 16; |
909 |
|
|
break; |
910 |
|
|
case _lev_ctrl_genpurpose2: |
911 |
|
|
decodedcontroller.type = leverage_ctrl_t::type_controlchange; |
912 |
|
|
decodedcontroller.controller_number = 17; |
913 |
|
|
break; |
914 |
|
|
case _lev_ctrl_genpurpose3: |
915 |
|
|
decodedcontroller.type = leverage_ctrl_t::type_controlchange; |
916 |
|
|
decodedcontroller.controller_number = 18; |
917 |
|
|
break; |
918 |
|
|
case _lev_ctrl_genpurpose4: |
919 |
|
|
decodedcontroller.type = leverage_ctrl_t::type_controlchange; |
920 |
|
|
decodedcontroller.controller_number = 19; |
921 |
|
|
break; |
922 |
|
|
case _lev_ctrl_portamentotime: |
923 |
|
|
decodedcontroller.type = leverage_ctrl_t::type_controlchange; |
924 |
|
|
decodedcontroller.controller_number = 5; |
925 |
|
|
break; |
926 |
|
|
case _lev_ctrl_sustainpedal: |
927 |
|
|
decodedcontroller.type = leverage_ctrl_t::type_controlchange; |
928 |
|
|
decodedcontroller.controller_number = 64; |
929 |
|
|
break; |
930 |
|
|
case _lev_ctrl_portamento: |
931 |
|
|
decodedcontroller.type = leverage_ctrl_t::type_controlchange; |
932 |
|
|
decodedcontroller.controller_number = 65; |
933 |
|
|
break; |
934 |
|
|
case _lev_ctrl_sostenutopedal: |
935 |
|
|
decodedcontroller.type = leverage_ctrl_t::type_controlchange; |
936 |
|
|
decodedcontroller.controller_number = 66; |
937 |
|
|
break; |
938 |
|
|
case _lev_ctrl_softpedal: |
939 |
|
|
decodedcontroller.type = leverage_ctrl_t::type_controlchange; |
940 |
|
|
decodedcontroller.controller_number = 67; |
941 |
|
|
break; |
942 |
|
|
case _lev_ctrl_genpurpose5: |
943 |
|
|
decodedcontroller.type = leverage_ctrl_t::type_controlchange; |
944 |
|
|
decodedcontroller.controller_number = 80; |
945 |
|
|
break; |
946 |
|
|
case _lev_ctrl_genpurpose6: |
947 |
|
|
decodedcontroller.type = leverage_ctrl_t::type_controlchange; |
948 |
|
|
decodedcontroller.controller_number = 81; |
949 |
|
|
break; |
950 |
|
|
case _lev_ctrl_genpurpose7: |
951 |
|
|
decodedcontroller.type = leverage_ctrl_t::type_controlchange; |
952 |
|
|
decodedcontroller.controller_number = 82; |
953 |
|
|
break; |
954 |
|
|
case _lev_ctrl_genpurpose8: |
955 |
|
|
decodedcontroller.type = leverage_ctrl_t::type_controlchange; |
956 |
|
|
decodedcontroller.controller_number = 83; |
957 |
|
|
break; |
958 |
|
|
case _lev_ctrl_effect1depth: |
959 |
|
|
decodedcontroller.type = leverage_ctrl_t::type_controlchange; |
960 |
|
|
decodedcontroller.controller_number = 91; |
961 |
|
|
break; |
962 |
|
|
case _lev_ctrl_effect2depth: |
963 |
|
|
decodedcontroller.type = leverage_ctrl_t::type_controlchange; |
964 |
|
|
decodedcontroller.controller_number = 92; |
965 |
|
|
break; |
966 |
|
|
case _lev_ctrl_effect3depth: |
967 |
|
|
decodedcontroller.type = leverage_ctrl_t::type_controlchange; |
968 |
|
|
decodedcontroller.controller_number = 93; |
969 |
|
|
break; |
970 |
|
|
case _lev_ctrl_effect4depth: |
971 |
|
|
decodedcontroller.type = leverage_ctrl_t::type_controlchange; |
972 |
|
|
decodedcontroller.controller_number = 94; |
973 |
|
|
break; |
974 |
|
|
case _lev_ctrl_effect5depth: |
975 |
|
|
decodedcontroller.type = leverage_ctrl_t::type_controlchange; |
976 |
|
|
decodedcontroller.controller_number = 95; |
977 |
|
|
break; |
978 |
schoenebeck |
55 |
|
979 |
schoenebeck |
36 |
// unknown controller type |
980 |
|
|
default: |
981 |
|
|
throw gig::Exception("Unknown leverage controller type."); |
982 |
|
|
} |
983 |
|
|
return decodedcontroller; |
984 |
|
|
} |
985 |
schoenebeck |
2 |
|
986 |
schoenebeck |
16 |
DimensionRegion::~DimensionRegion() { |
987 |
|
|
Instances--; |
988 |
|
|
if (!Instances) { |
989 |
|
|
// delete the velocity->volume tables |
990 |
|
|
VelocityTableMap::iterator iter; |
991 |
|
|
for (iter = pVelocityTables->begin(); iter != pVelocityTables->end(); iter++) { |
992 |
|
|
double* pTable = iter->second; |
993 |
|
|
if (pTable) delete[] pTable; |
994 |
|
|
} |
995 |
|
|
pVelocityTables->clear(); |
996 |
|
|
delete pVelocityTables; |
997 |
|
|
pVelocityTables = NULL; |
998 |
|
|
} |
999 |
|
|
} |
1000 |
schoenebeck |
2 |
|
1001 |
schoenebeck |
16 |
/** |
1002 |
|
|
* Returns the correct amplitude factor for the given \a MIDIKeyVelocity. |
1003 |
|
|
* All involved parameters (VelocityResponseCurve, VelocityResponseDepth |
1004 |
|
|
* and VelocityResponseCurveScaling) involved are taken into account to |
1005 |
|
|
* calculate the amplitude factor. Use this method when a key was |
1006 |
|
|
* triggered to get the volume with which the sample should be played |
1007 |
|
|
* back. |
1008 |
|
|
* |
1009 |
schoenebeck |
36 |
* @param MIDIKeyVelocity MIDI velocity value of the triggered key (between 0 and 127) |
1010 |
|
|
* @returns amplitude factor (between 0.0 and 1.0) |
1011 |
schoenebeck |
16 |
*/ |
1012 |
|
|
double DimensionRegion::GetVelocityAttenuation(uint8_t MIDIKeyVelocity) { |
1013 |
|
|
return pVelocityAttenuationTable[MIDIKeyVelocity]; |
1014 |
|
|
} |
1015 |
schoenebeck |
2 |
|
1016 |
schoenebeck |
308 |
double* DimensionRegion::CreateVelocityTable(curve_type_t curveType, uint8_t depth, uint8_t scaling) { |
1017 |
schoenebeck |
317 |
|
1018 |
schoenebeck |
308 |
// line-segment approximations of the 15 velocity curves |
1019 |
schoenebeck |
16 |
|
1020 |
schoenebeck |
308 |
// linear |
1021 |
|
|
const int lin0[] = { 1, 1, 127, 127 }; |
1022 |
|
|
const int lin1[] = { 1, 21, 127, 127 }; |
1023 |
|
|
const int lin2[] = { 1, 45, 127, 127 }; |
1024 |
|
|
const int lin3[] = { 1, 74, 127, 127 }; |
1025 |
|
|
const int lin4[] = { 1, 127, 127, 127 }; |
1026 |
schoenebeck |
16 |
|
1027 |
schoenebeck |
308 |
// non-linear |
1028 |
|
|
const int non0[] = { 1, 4, 24, 5, 57, 17, 92, 57, 122, 127, 127, 127 }; |
1029 |
schoenebeck |
317 |
const int non1[] = { 1, 4, 46, 9, 93, 56, 118, 106, 123, 127, |
1030 |
schoenebeck |
308 |
127, 127 }; |
1031 |
|
|
const int non2[] = { 1, 4, 46, 9, 57, 20, 102, 107, 107, 127, |
1032 |
|
|
127, 127 }; |
1033 |
|
|
const int non3[] = { 1, 15, 10, 19, 67, 73, 80, 80, 90, 98, 98, 127, |
1034 |
|
|
127, 127 }; |
1035 |
|
|
const int non4[] = { 1, 25, 33, 57, 82, 81, 92, 127, 127, 127 }; |
1036 |
schoenebeck |
317 |
|
1037 |
schoenebeck |
308 |
// special |
1038 |
schoenebeck |
317 |
const int spe0[] = { 1, 2, 76, 10, 90, 15, 95, 20, 99, 28, 103, 44, |
1039 |
schoenebeck |
308 |
113, 127, 127, 127 }; |
1040 |
|
|
const int spe1[] = { 1, 2, 27, 5, 67, 18, 89, 29, 95, 35, 107, 67, |
1041 |
|
|
118, 127, 127, 127 }; |
1042 |
schoenebeck |
317 |
const int spe2[] = { 1, 1, 33, 1, 53, 5, 61, 13, 69, 32, 79, 74, |
1043 |
schoenebeck |
308 |
85, 90, 91, 127, 127, 127 }; |
1044 |
schoenebeck |
317 |
const int spe3[] = { 1, 32, 28, 35, 66, 48, 89, 59, 95, 65, 99, 73, |
1045 |
schoenebeck |
308 |
117, 127, 127, 127 }; |
1046 |
schoenebeck |
317 |
const int spe4[] = { 1, 4, 23, 5, 49, 13, 57, 17, 92, 57, 122, 127, |
1047 |
schoenebeck |
308 |
127, 127 }; |
1048 |
schoenebeck |
317 |
|
1049 |
schoenebeck |
308 |
const int* const curves[] = { non0, non1, non2, non3, non4, |
1050 |
schoenebeck |
317 |
lin0, lin1, lin2, lin3, lin4, |
1051 |
schoenebeck |
308 |
spe0, spe1, spe2, spe3, spe4 }; |
1052 |
schoenebeck |
317 |
|
1053 |
schoenebeck |
308 |
double* const table = new double[128]; |
1054 |
|
|
|
1055 |
|
|
const int* curve = curves[curveType * 5 + depth]; |
1056 |
|
|
const int s = scaling == 0 ? 20 : scaling; // 0 or 20 means no scaling |
1057 |
schoenebeck |
317 |
|
1058 |
schoenebeck |
308 |
table[0] = 0; |
1059 |
|
|
for (int x = 1 ; x < 128 ; x++) { |
1060 |
|
|
|
1061 |
|
|
if (x > curve[2]) curve += 2; |
1062 |
schoenebeck |
317 |
double y = curve[1] + (x - curve[0]) * |
1063 |
schoenebeck |
308 |
(double(curve[3] - curve[1]) / (curve[2] - curve[0])); |
1064 |
|
|
y = y / 127; |
1065 |
|
|
|
1066 |
|
|
// Scale up for s > 20, down for s < 20. When |
1067 |
|
|
// down-scaling, the curve still ends at 1.0. |
1068 |
|
|
if (s < 20 && y >= 0.5) |
1069 |
|
|
y = y / ((2 - 40.0 / s) * y + 40.0 / s - 1); |
1070 |
|
|
else |
1071 |
|
|
y = y * (s / 20.0); |
1072 |
|
|
if (y > 1) y = 1; |
1073 |
|
|
|
1074 |
|
|
table[x] = y; |
1075 |
|
|
} |
1076 |
|
|
return table; |
1077 |
|
|
} |
1078 |
|
|
|
1079 |
|
|
|
1080 |
schoenebeck |
2 |
// *************** Region *************** |
1081 |
|
|
// * |
1082 |
|
|
|
1083 |
|
|
Region::Region(Instrument* pInstrument, RIFF::List* rgnList) : DLS::Region((DLS::Instrument*) pInstrument, rgnList) { |
1084 |
|
|
// Initialization |
1085 |
|
|
Dimensions = 0; |
1086 |
|
|
for (int i = 0; i < 32; i++) { |
1087 |
|
|
pDimensionRegions[i] = NULL; |
1088 |
|
|
} |
1089 |
schoenebeck |
282 |
Layers = 1; |
1090 |
schoenebeck |
2 |
|
1091 |
|
|
// Actual Loading |
1092 |
|
|
|
1093 |
|
|
LoadDimensionRegions(rgnList); |
1094 |
|
|
|
1095 |
|
|
RIFF::Chunk* _3lnk = rgnList->GetSubChunk(CHUNK_ID_3LNK); |
1096 |
|
|
if (_3lnk) { |
1097 |
|
|
DimensionRegions = _3lnk->ReadUint32(); |
1098 |
|
|
for (int i = 0; i < 5; i++) { |
1099 |
|
|
dimension_t dimension = static_cast<dimension_t>(_3lnk->ReadUint8()); |
1100 |
|
|
uint8_t bits = _3lnk->ReadUint8(); |
1101 |
|
|
if (dimension == dimension_none) { // inactive dimension |
1102 |
|
|
pDimensionDefinitions[i].dimension = dimension_none; |
1103 |
|
|
pDimensionDefinitions[i].bits = 0; |
1104 |
|
|
pDimensionDefinitions[i].zones = 0; |
1105 |
|
|
pDimensionDefinitions[i].split_type = split_type_bit; |
1106 |
|
|
pDimensionDefinitions[i].ranges = NULL; |
1107 |
|
|
pDimensionDefinitions[i].zone_size = 0; |
1108 |
|
|
} |
1109 |
|
|
else { // active dimension |
1110 |
|
|
pDimensionDefinitions[i].dimension = dimension; |
1111 |
|
|
pDimensionDefinitions[i].bits = bits; |
1112 |
|
|
pDimensionDefinitions[i].zones = 0x01 << bits; // = pow(2,bits) |
1113 |
|
|
pDimensionDefinitions[i].split_type = (dimension == dimension_layer || |
1114 |
schoenebeck |
241 |
dimension == dimension_samplechannel || |
1115 |
|
|
dimension == dimension_releasetrigger) ? split_type_bit |
1116 |
|
|
: split_type_normal; |
1117 |
schoenebeck |
2 |
pDimensionDefinitions[i].ranges = NULL; // it's not possible to check velocity dimensions for custom defined ranges at this point |
1118 |
|
|
pDimensionDefinitions[i].zone_size = |
1119 |
|
|
(pDimensionDefinitions[i].split_type == split_type_normal) ? 128 / pDimensionDefinitions[i].zones |
1120 |
|
|
: 0; |
1121 |
|
|
Dimensions++; |
1122 |
schoenebeck |
282 |
|
1123 |
|
|
// if this is a layer dimension, remember the amount of layers |
1124 |
|
|
if (dimension == dimension_layer) Layers = pDimensionDefinitions[i].zones; |
1125 |
schoenebeck |
2 |
} |
1126 |
|
|
_3lnk->SetPos(6, RIFF::stream_curpos); // jump forward to next dimension definition |
1127 |
|
|
} |
1128 |
|
|
|
1129 |
|
|
// check velocity dimension (if there is one) for custom defined zone ranges |
1130 |
|
|
for (uint i = 0; i < Dimensions; i++) { |
1131 |
|
|
dimension_def_t* pDimDef = pDimensionDefinitions + i; |
1132 |
|
|
if (pDimDef->dimension == dimension_velocity) { |
1133 |
|
|
if (pDimensionRegions[0]->VelocityUpperLimit == 0) { |
1134 |
|
|
// no custom defined ranges |
1135 |
|
|
pDimDef->split_type = split_type_normal; |
1136 |
|
|
pDimDef->ranges = NULL; |
1137 |
|
|
} |
1138 |
|
|
else { // custom defined ranges |
1139 |
|
|
pDimDef->split_type = split_type_customvelocity; |
1140 |
|
|
pDimDef->ranges = new range_t[pDimDef->zones]; |
1141 |
|
|
unsigned int bits[5] = {0,0,0,0,0}; |
1142 |
|
|
int previousUpperLimit = -1; |
1143 |
|
|
for (int velocityZone = 0; velocityZone < pDimDef->zones; velocityZone++) { |
1144 |
|
|
bits[i] = velocityZone; |
1145 |
|
|
DimensionRegion* pDimRegion = GetDimensionRegionByBit(bits[4],bits[3],bits[2],bits[1],bits[0]); |
1146 |
|
|
|
1147 |
|
|
pDimDef->ranges[velocityZone].low = previousUpperLimit + 1; |
1148 |
|
|
pDimDef->ranges[velocityZone].high = pDimRegion->VelocityUpperLimit; |
1149 |
|
|
previousUpperLimit = pDimDef->ranges[velocityZone].high; |
1150 |
|
|
// fill velocity table |
1151 |
|
|
for (int i = pDimDef->ranges[velocityZone].low; i <= pDimDef->ranges[velocityZone].high; i++) { |
1152 |
|
|
VelocityTable[i] = velocityZone; |
1153 |
|
|
} |
1154 |
|
|
} |
1155 |
|
|
} |
1156 |
|
|
} |
1157 |
|
|
} |
1158 |
|
|
|
1159 |
schoenebeck |
317 |
// jump to start of the wave pool indices (if not already there) |
1160 |
|
|
File* file = (File*) GetParent()->GetParent(); |
1161 |
|
|
if (file->pVersion && file->pVersion->major == 3) |
1162 |
|
|
_3lnk->SetPos(68); // version 3 has a different 3lnk structure |
1163 |
|
|
else |
1164 |
|
|
_3lnk->SetPos(44); |
1165 |
|
|
|
1166 |
schoenebeck |
2 |
// load sample references |
1167 |
|
|
for (uint i = 0; i < DimensionRegions; i++) { |
1168 |
|
|
uint32_t wavepoolindex = _3lnk->ReadUint32(); |
1169 |
|
|
pDimensionRegions[i]->pSample = GetSampleFromWavePool(wavepoolindex); |
1170 |
|
|
} |
1171 |
|
|
} |
1172 |
|
|
else throw gig::Exception("Mandatory <3lnk> chunk not found."); |
1173 |
|
|
} |
1174 |
|
|
|
1175 |
|
|
void Region::LoadDimensionRegions(RIFF::List* rgn) { |
1176 |
|
|
RIFF::List* _3prg = rgn->GetSubList(LIST_TYPE_3PRG); |
1177 |
|
|
if (_3prg) { |
1178 |
|
|
int dimensionRegionNr = 0; |
1179 |
|
|
RIFF::List* _3ewl = _3prg->GetFirstSubList(); |
1180 |
|
|
while (_3ewl) { |
1181 |
|
|
if (_3ewl->GetListType() == LIST_TYPE_3EWL) { |
1182 |
|
|
pDimensionRegions[dimensionRegionNr] = new DimensionRegion(_3ewl); |
1183 |
|
|
dimensionRegionNr++; |
1184 |
|
|
} |
1185 |
|
|
_3ewl = _3prg->GetNextSubList(); |
1186 |
|
|
} |
1187 |
|
|
if (dimensionRegionNr == 0) throw gig::Exception("No dimension region found."); |
1188 |
|
|
} |
1189 |
|
|
} |
1190 |
|
|
|
1191 |
|
|
Region::~Region() { |
1192 |
|
|
for (uint i = 0; i < Dimensions; i++) { |
1193 |
|
|
if (pDimensionDefinitions[i].ranges) delete[] pDimensionDefinitions[i].ranges; |
1194 |
|
|
} |
1195 |
|
|
for (int i = 0; i < 32; i++) { |
1196 |
|
|
if (pDimensionRegions[i]) delete pDimensionRegions[i]; |
1197 |
|
|
} |
1198 |
|
|
} |
1199 |
|
|
|
1200 |
|
|
/** |
1201 |
|
|
* Use this method in your audio engine to get the appropriate dimension |
1202 |
|
|
* region with it's articulation data for the current situation. Just |
1203 |
|
|
* call the method with the current MIDI controller values and you'll get |
1204 |
|
|
* the DimensionRegion with the appropriate articulation data for the |
1205 |
|
|
* current situation (for this Region of course only). To do that you'll |
1206 |
|
|
* first have to look which dimensions with which controllers and in |
1207 |
|
|
* which order are defined for this Region when you load the .gig file. |
1208 |
|
|
* Special cases are e.g. layer or channel dimensions where you just put |
1209 |
|
|
* in the index numbers instead of a MIDI controller value (means 0 for |
1210 |
|
|
* left channel, 1 for right channel or 0 for layer 0, 1 for layer 1, |
1211 |
|
|
* etc.). |
1212 |
|
|
* |
1213 |
|
|
* @param Dim4Val MIDI controller value (0-127) for dimension 4 |
1214 |
|
|
* @param Dim3Val MIDI controller value (0-127) for dimension 3 |
1215 |
|
|
* @param Dim2Val MIDI controller value (0-127) for dimension 2 |
1216 |
|
|
* @param Dim1Val MIDI controller value (0-127) for dimension 1 |
1217 |
|
|
* @param Dim0Val MIDI controller value (0-127) for dimension 0 |
1218 |
|
|
* @returns adress to the DimensionRegion for the given situation |
1219 |
|
|
* @see pDimensionDefinitions |
1220 |
|
|
* @see Dimensions |
1221 |
|
|
*/ |
1222 |
|
|
DimensionRegion* Region::GetDimensionRegionByValue(uint Dim4Val, uint Dim3Val, uint Dim2Val, uint Dim1Val, uint Dim0Val) { |
1223 |
schoenebeck |
241 |
uint8_t bits[5] = {Dim0Val,Dim1Val,Dim2Val,Dim3Val,Dim4Val}; |
1224 |
schoenebeck |
2 |
for (uint i = 0; i < Dimensions; i++) { |
1225 |
|
|
switch (pDimensionDefinitions[i].split_type) { |
1226 |
|
|
case split_type_normal: |
1227 |
|
|
bits[i] /= pDimensionDefinitions[i].zone_size; |
1228 |
|
|
break; |
1229 |
|
|
case split_type_customvelocity: |
1230 |
|
|
bits[i] = VelocityTable[bits[i]]; |
1231 |
|
|
break; |
1232 |
schoenebeck |
241 |
case split_type_bit: // the value is already the sought dimension bit number |
1233 |
|
|
const uint8_t limiter_mask = (0xff << pDimensionDefinitions[i].bits) ^ 0xff; |
1234 |
|
|
bits[i] = bits[i] & limiter_mask; // just make sure the value don't uses more bits than allowed |
1235 |
|
|
break; |
1236 |
schoenebeck |
2 |
} |
1237 |
|
|
} |
1238 |
|
|
return GetDimensionRegionByBit(bits[4],bits[3],bits[2],bits[1],bits[0]); |
1239 |
|
|
} |
1240 |
|
|
|
1241 |
|
|
/** |
1242 |
|
|
* Returns the appropriate DimensionRegion for the given dimension bit |
1243 |
|
|
* numbers (zone index). You usually use <i>GetDimensionRegionByValue</i> |
1244 |
|
|
* instead of calling this method directly! |
1245 |
|
|
* |
1246 |
|
|
* @param Dim4Bit Bit number for dimension 4 |
1247 |
|
|
* @param Dim3Bit Bit number for dimension 3 |
1248 |
|
|
* @param Dim2Bit Bit number for dimension 2 |
1249 |
|
|
* @param Dim1Bit Bit number for dimension 1 |
1250 |
|
|
* @param Dim0Bit Bit number for dimension 0 |
1251 |
|
|
* @returns adress to the DimensionRegion for the given dimension |
1252 |
|
|
* bit numbers |
1253 |
|
|
* @see GetDimensionRegionByValue() |
1254 |
|
|
*/ |
1255 |
|
|
DimensionRegion* Region::GetDimensionRegionByBit(uint8_t Dim4Bit, uint8_t Dim3Bit, uint8_t Dim2Bit, uint8_t Dim1Bit, uint8_t Dim0Bit) { |
1256 |
|
|
return *(pDimensionRegions + ((((((((Dim4Bit << pDimensionDefinitions[3].bits) | Dim3Bit) |
1257 |
|
|
<< pDimensionDefinitions[2].bits) | Dim2Bit) |
1258 |
|
|
<< pDimensionDefinitions[1].bits) | Dim1Bit) |
1259 |
|
|
<< pDimensionDefinitions[0].bits) | Dim0Bit) ); |
1260 |
|
|
} |
1261 |
|
|
|
1262 |
|
|
/** |
1263 |
|
|
* Returns pointer address to the Sample referenced with this region. |
1264 |
|
|
* This is the global Sample for the entire Region (not sure if this is |
1265 |
|
|
* actually used by the Gigasampler engine - I would only use the Sample |
1266 |
|
|
* referenced by the appropriate DimensionRegion instead of this sample). |
1267 |
|
|
* |
1268 |
|
|
* @returns address to Sample or NULL if there is no reference to a |
1269 |
|
|
* sample saved in the .gig file |
1270 |
|
|
*/ |
1271 |
|
|
Sample* Region::GetSample() { |
1272 |
|
|
if (pSample) return static_cast<gig::Sample*>(pSample); |
1273 |
|
|
else return static_cast<gig::Sample*>(pSample = GetSampleFromWavePool(WavePoolTableIndex)); |
1274 |
|
|
} |
1275 |
|
|
|
1276 |
|
|
Sample* Region::GetSampleFromWavePool(unsigned int WavePoolTableIndex) { |
1277 |
|
|
File* file = (File*) GetParent()->GetParent(); |
1278 |
|
|
unsigned long soughtoffset = file->pWavePoolTable[WavePoolTableIndex]; |
1279 |
|
|
Sample* sample = file->GetFirstSample(); |
1280 |
|
|
while (sample) { |
1281 |
|
|
if (sample->ulWavePoolOffset == soughtoffset) return static_cast<gig::Sample*>(pSample = sample); |
1282 |
|
|
sample = file->GetNextSample(); |
1283 |
|
|
} |
1284 |
|
|
return NULL; |
1285 |
|
|
} |
1286 |
|
|
|
1287 |
|
|
|
1288 |
|
|
|
1289 |
|
|
// *************** Instrument *************** |
1290 |
|
|
// * |
1291 |
|
|
|
1292 |
|
|
Instrument::Instrument(File* pFile, RIFF::List* insList) : DLS::Instrument((DLS::File*)pFile, insList) { |
1293 |
|
|
// Initialization |
1294 |
|
|
for (int i = 0; i < 128; i++) RegionKeyTable[i] = NULL; |
1295 |
|
|
RegionIndex = -1; |
1296 |
|
|
|
1297 |
|
|
// Loading |
1298 |
|
|
RIFF::List* lart = insList->GetSubList(LIST_TYPE_LART); |
1299 |
|
|
if (lart) { |
1300 |
|
|
RIFF::Chunk* _3ewg = lart->GetSubChunk(CHUNK_ID_3EWG); |
1301 |
|
|
if (_3ewg) { |
1302 |
|
|
EffectSend = _3ewg->ReadUint16(); |
1303 |
|
|
Attenuation = _3ewg->ReadInt32(); |
1304 |
|
|
FineTune = _3ewg->ReadInt16(); |
1305 |
|
|
PitchbendRange = _3ewg->ReadInt16(); |
1306 |
|
|
uint8_t dimkeystart = _3ewg->ReadUint8(); |
1307 |
|
|
PianoReleaseMode = dimkeystart & 0x01; |
1308 |
|
|
DimensionKeyRange.low = dimkeystart >> 1; |
1309 |
|
|
DimensionKeyRange.high = _3ewg->ReadUint8(); |
1310 |
|
|
} |
1311 |
|
|
else throw gig::Exception("Mandatory <3ewg> chunk not found."); |
1312 |
|
|
} |
1313 |
|
|
else throw gig::Exception("Mandatory <lart> list chunk not found."); |
1314 |
|
|
|
1315 |
|
|
RIFF::List* lrgn = insList->GetSubList(LIST_TYPE_LRGN); |
1316 |
|
|
if (!lrgn) throw gig::Exception("Mandatory chunks in <ins > chunk not found."); |
1317 |
|
|
pRegions = new Region*[Regions]; |
1318 |
|
|
RIFF::List* rgn = lrgn->GetFirstSubList(); |
1319 |
|
|
unsigned int iRegion = 0; |
1320 |
|
|
while (rgn) { |
1321 |
|
|
if (rgn->GetListType() == LIST_TYPE_RGN) { |
1322 |
|
|
pRegions[iRegion] = new Region(this, rgn); |
1323 |
|
|
iRegion++; |
1324 |
|
|
} |
1325 |
|
|
rgn = lrgn->GetNextSubList(); |
1326 |
|
|
} |
1327 |
|
|
|
1328 |
|
|
// Creating Region Key Table for fast lookup |
1329 |
|
|
for (uint iReg = 0; iReg < Regions; iReg++) { |
1330 |
|
|
for (int iKey = pRegions[iReg]->KeyRange.low; iKey <= pRegions[iReg]->KeyRange.high; iKey++) { |
1331 |
|
|
RegionKeyTable[iKey] = pRegions[iReg]; |
1332 |
|
|
} |
1333 |
|
|
} |
1334 |
|
|
} |
1335 |
|
|
|
1336 |
|
|
Instrument::~Instrument() { |
1337 |
|
|
for (uint i = 0; i < Regions; i++) { |
1338 |
|
|
if (pRegions) { |
1339 |
|
|
if (pRegions[i]) delete (pRegions[i]); |
1340 |
|
|
} |
1341 |
|
|
delete[] pRegions; |
1342 |
|
|
} |
1343 |
|
|
} |
1344 |
|
|
|
1345 |
|
|
/** |
1346 |
|
|
* Returns the appropriate Region for a triggered note. |
1347 |
|
|
* |
1348 |
|
|
* @param Key MIDI Key number of triggered note / key (0 - 127) |
1349 |
|
|
* @returns pointer adress to the appropriate Region or NULL if there |
1350 |
|
|
* there is no Region defined for the given \a Key |
1351 |
|
|
*/ |
1352 |
|
|
Region* Instrument::GetRegion(unsigned int Key) { |
1353 |
|
|
if (!pRegions || Key > 127) return NULL; |
1354 |
|
|
return RegionKeyTable[Key]; |
1355 |
|
|
/*for (int i = 0; i < Regions; i++) { |
1356 |
|
|
if (Key <= pRegions[i]->KeyRange.high && |
1357 |
|
|
Key >= pRegions[i]->KeyRange.low) return pRegions[i]; |
1358 |
|
|
} |
1359 |
|
|
return NULL;*/ |
1360 |
|
|
} |
1361 |
|
|
|
1362 |
|
|
/** |
1363 |
|
|
* Returns the first Region of the instrument. You have to call this |
1364 |
|
|
* method once before you use GetNextRegion(). |
1365 |
|
|
* |
1366 |
|
|
* @returns pointer address to first region or NULL if there is none |
1367 |
|
|
* @see GetNextRegion() |
1368 |
|
|
*/ |
1369 |
|
|
Region* Instrument::GetFirstRegion() { |
1370 |
|
|
if (!Regions) return NULL; |
1371 |
|
|
RegionIndex = 1; |
1372 |
|
|
return pRegions[0]; |
1373 |
|
|
} |
1374 |
|
|
|
1375 |
|
|
/** |
1376 |
|
|
* Returns the next Region of the instrument. You have to call |
1377 |
|
|
* GetFirstRegion() once before you can use this method. By calling this |
1378 |
|
|
* method multiple times it iterates through the available Regions. |
1379 |
|
|
* |
1380 |
|
|
* @returns pointer address to the next region or NULL if end reached |
1381 |
|
|
* @see GetFirstRegion() |
1382 |
|
|
*/ |
1383 |
|
|
Region* Instrument::GetNextRegion() { |
1384 |
|
|
if (RegionIndex < 0 || RegionIndex >= Regions) return NULL; |
1385 |
|
|
return pRegions[RegionIndex++]; |
1386 |
|
|
} |
1387 |
|
|
|
1388 |
|
|
|
1389 |
|
|
|
1390 |
|
|
// *************** File *************** |
1391 |
|
|
// * |
1392 |
|
|
|
1393 |
|
|
File::File(RIFF::File* pRIFF) : DLS::File(pRIFF) { |
1394 |
|
|
pSamples = NULL; |
1395 |
|
|
pInstruments = NULL; |
1396 |
|
|
} |
1397 |
|
|
|
1398 |
|
|
Sample* File::GetFirstSample() { |
1399 |
|
|
if (!pSamples) LoadSamples(); |
1400 |
|
|
if (!pSamples) return NULL; |
1401 |
|
|
SamplesIterator = pSamples->begin(); |
1402 |
|
|
return static_cast<gig::Sample*>( (SamplesIterator != pSamples->end()) ? *SamplesIterator : NULL ); |
1403 |
|
|
} |
1404 |
|
|
|
1405 |
|
|
Sample* File::GetNextSample() { |
1406 |
|
|
if (!pSamples) return NULL; |
1407 |
|
|
SamplesIterator++; |
1408 |
|
|
return static_cast<gig::Sample*>( (SamplesIterator != pSamples->end()) ? *SamplesIterator : NULL ); |
1409 |
|
|
} |
1410 |
|
|
|
1411 |
|
|
void File::LoadSamples() { |
1412 |
|
|
RIFF::List* wvpl = pRIFF->GetSubList(LIST_TYPE_WVPL); |
1413 |
|
|
if (wvpl) { |
1414 |
|
|
unsigned long wvplFileOffset = wvpl->GetFilePos(); |
1415 |
|
|
RIFF::List* wave = wvpl->GetFirstSubList(); |
1416 |
|
|
while (wave) { |
1417 |
|
|
if (wave->GetListType() == LIST_TYPE_WAVE) { |
1418 |
|
|
if (!pSamples) pSamples = new SampleList; |
1419 |
|
|
unsigned long waveFileOffset = wave->GetFilePos(); |
1420 |
|
|
pSamples->push_back(new Sample(this, wave, waveFileOffset - wvplFileOffset)); |
1421 |
|
|
} |
1422 |
|
|
wave = wvpl->GetNextSubList(); |
1423 |
|
|
} |
1424 |
|
|
} |
1425 |
|
|
else throw gig::Exception("Mandatory <wvpl> chunk not found."); |
1426 |
|
|
} |
1427 |
|
|
|
1428 |
|
|
Instrument* File::GetFirstInstrument() { |
1429 |
|
|
if (!pInstruments) LoadInstruments(); |
1430 |
|
|
if (!pInstruments) return NULL; |
1431 |
|
|
InstrumentsIterator = pInstruments->begin(); |
1432 |
|
|
return (InstrumentsIterator != pInstruments->end()) ? *InstrumentsIterator : NULL; |
1433 |
|
|
} |
1434 |
|
|
|
1435 |
|
|
Instrument* File::GetNextInstrument() { |
1436 |
|
|
if (!pInstruments) return NULL; |
1437 |
|
|
InstrumentsIterator++; |
1438 |
|
|
return (InstrumentsIterator != pInstruments->end()) ? *InstrumentsIterator : NULL; |
1439 |
|
|
} |
1440 |
|
|
|
1441 |
schoenebeck |
21 |
/** |
1442 |
|
|
* Returns the instrument with the given index. |
1443 |
|
|
* |
1444 |
|
|
* @returns sought instrument or NULL if there's no such instrument |
1445 |
|
|
*/ |
1446 |
|
|
Instrument* File::GetInstrument(uint index) { |
1447 |
|
|
if (!pInstruments) LoadInstruments(); |
1448 |
|
|
if (!pInstruments) return NULL; |
1449 |
|
|
InstrumentsIterator = pInstruments->begin(); |
1450 |
|
|
for (uint i = 0; InstrumentsIterator != pInstruments->end(); i++) { |
1451 |
|
|
if (i == index) return *InstrumentsIterator; |
1452 |
|
|
InstrumentsIterator++; |
1453 |
|
|
} |
1454 |
|
|
return NULL; |
1455 |
|
|
} |
1456 |
|
|
|
1457 |
schoenebeck |
2 |
void File::LoadInstruments() { |
1458 |
|
|
RIFF::List* lstInstruments = pRIFF->GetSubList(LIST_TYPE_LINS); |
1459 |
|
|
if (lstInstruments) { |
1460 |
|
|
RIFF::List* lstInstr = lstInstruments->GetFirstSubList(); |
1461 |
|
|
while (lstInstr) { |
1462 |
|
|
if (lstInstr->GetListType() == LIST_TYPE_INS) { |
1463 |
|
|
if (!pInstruments) pInstruments = new InstrumentList; |
1464 |
|
|
pInstruments->push_back(new Instrument(this, lstInstr)); |
1465 |
|
|
} |
1466 |
|
|
lstInstr = lstInstruments->GetNextSubList(); |
1467 |
|
|
} |
1468 |
|
|
} |
1469 |
|
|
else throw gig::Exception("Mandatory <lins> list chunk not found."); |
1470 |
|
|
} |
1471 |
|
|
|
1472 |
|
|
|
1473 |
|
|
|
1474 |
|
|
// *************** Exception *************** |
1475 |
|
|
// * |
1476 |
|
|
|
1477 |
|
|
Exception::Exception(String Message) : DLS::Exception(Message) { |
1478 |
|
|
} |
1479 |
|
|
|
1480 |
|
|
void Exception::PrintMessage() { |
1481 |
|
|
std::cout << "gig::Exception: " << Message << std::endl; |
1482 |
|
|
} |
1483 |
|
|
|
1484 |
|
|
} // namespace gig |