1 |
/*************************************************************************** |
2 |
* * |
3 |
* libgig - C++ cross-platform Gigasampler format file loader library * |
4 |
* * |
5 |
* Copyright (C) 2003-2005 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 "DLS.h" |
25 |
|
26 |
#include <time.h> |
27 |
|
28 |
#include "helper.h" |
29 |
|
30 |
// macros to decode connection transforms |
31 |
#define CONN_TRANSFORM_SRC(x) ((x >> 10) & 0x000F) |
32 |
#define CONN_TRANSFORM_CTL(x) ((x >> 4) & 0x000F) |
33 |
#define CONN_TRANSFORM_DST(x) (x & 0x000F) |
34 |
#define CONN_TRANSFORM_BIPOLAR_SRC(x) (x & 0x4000) |
35 |
#define CONN_TRANSFORM_BIPOLAR_CTL(x) (x & 0x0100) |
36 |
#define CONN_TRANSFORM_INVERT_SRC(x) (x & 0x8000) |
37 |
#define CONN_TRANSFORM_INVERT_CTL(x) (x & 0x0200) |
38 |
|
39 |
// macros to encode connection transforms |
40 |
#define CONN_TRANSFORM_SRC_ENCODE(x) ((x & 0x000F) << 10) |
41 |
#define CONN_TRANSFORM_CTL_ENCODE(x) ((x & 0x000F) << 4) |
42 |
#define CONN_TRANSFORM_DST_ENCODE(x) (x & 0x000F) |
43 |
#define CONN_TRANSFORM_BIPOLAR_SRC_ENCODE(x) ((x) ? 0x4000 : 0) |
44 |
#define CONN_TRANSFORM_BIPOLAR_CTL_ENCODE(x) ((x) ? 0x0100 : 0) |
45 |
#define CONN_TRANSFORM_INVERT_SRC_ENCODE(x) ((x) ? 0x8000 : 0) |
46 |
#define CONN_TRANSFORM_INVERT_CTL_ENCODE(x) ((x) ? 0x0200 : 0) |
47 |
|
48 |
#define DRUM_TYPE_MASK 0x80000000 |
49 |
|
50 |
#define F_RGN_OPTION_SELFNONEXCLUSIVE 0x0001 |
51 |
|
52 |
#define F_WAVELINK_PHASE_MASTER 0x0001 |
53 |
#define F_WAVELINK_MULTICHANNEL 0x0002 |
54 |
|
55 |
#define F_WSMP_NO_TRUNCATION 0x0001 |
56 |
#define F_WSMP_NO_COMPRESSION 0x0002 |
57 |
|
58 |
#define MIDI_BANK_COARSE(x) ((x & 0x00007F00) >> 8) // CC0 |
59 |
#define MIDI_BANK_FINE(x) (x & 0x0000007F) // CC32 |
60 |
#define MIDI_BANK_MERGE(coarse, fine) ((((uint16_t) coarse) << 7) | fine) // CC0 + CC32 |
61 |
#define MIDI_BANK_ENCODE(coarse, fine) (((coarse & 0x0000007F) << 8) | (fine & 0x0000007F)) |
62 |
|
63 |
namespace DLS { |
64 |
|
65 |
// *************** Connection *************** |
66 |
// * |
67 |
|
68 |
void Connection::Init(conn_block_t* Header) { |
69 |
Source = (conn_src_t) Header->source; |
70 |
Control = (conn_src_t) Header->control; |
71 |
Destination = (conn_dst_t) Header->destination; |
72 |
Scale = Header->scale; |
73 |
SourceTransform = (conn_trn_t) CONN_TRANSFORM_SRC(Header->transform); |
74 |
ControlTransform = (conn_trn_t) CONN_TRANSFORM_CTL(Header->transform); |
75 |
DestinationTransform = (conn_trn_t) CONN_TRANSFORM_DST(Header->transform); |
76 |
SourceInvert = CONN_TRANSFORM_INVERT_SRC(Header->transform); |
77 |
SourceBipolar = CONN_TRANSFORM_BIPOLAR_SRC(Header->transform); |
78 |
ControlInvert = CONN_TRANSFORM_INVERT_CTL(Header->transform); |
79 |
ControlBipolar = CONN_TRANSFORM_BIPOLAR_CTL(Header->transform); |
80 |
} |
81 |
|
82 |
Connection::conn_block_t Connection::ToConnBlock() { |
83 |
conn_block_t c; |
84 |
c.source = Source; |
85 |
c.control = Control; |
86 |
c.destination = Destination; |
87 |
c.scale = Scale; |
88 |
c.transform = CONN_TRANSFORM_SRC_ENCODE(SourceTransform) | |
89 |
CONN_TRANSFORM_CTL_ENCODE(ControlTransform) | |
90 |
CONN_TRANSFORM_DST_ENCODE(DestinationTransform) | |
91 |
CONN_TRANSFORM_INVERT_SRC_ENCODE(SourceInvert) | |
92 |
CONN_TRANSFORM_BIPOLAR_SRC_ENCODE(SourceBipolar) | |
93 |
CONN_TRANSFORM_INVERT_CTL_ENCODE(ControlInvert) | |
94 |
CONN_TRANSFORM_BIPOLAR_CTL_ENCODE(ControlBipolar); |
95 |
return c; |
96 |
} |
97 |
|
98 |
|
99 |
|
100 |
// *************** Articulation *************** |
101 |
// * |
102 |
|
103 |
/** @brief Constructor. |
104 |
* |
105 |
* Expects an 'artl' or 'art2' chunk to be given where the articulation |
106 |
* connections will be read from. |
107 |
* |
108 |
* @param artl - pointer to an 'artl' or 'art2' chunk |
109 |
* @throws Exception if no 'artl' or 'art2' chunk was given |
110 |
*/ |
111 |
Articulation::Articulation(RIFF::Chunk* artl) { |
112 |
pArticulationCk = artl; |
113 |
if (artl->GetChunkID() != CHUNK_ID_ART2 && |
114 |
artl->GetChunkID() != CHUNK_ID_ARTL) { |
115 |
throw DLS::Exception("<artl-ck> or <art2-ck> chunk expected"); |
116 |
} |
117 |
HeaderSize = artl->ReadUint32(); |
118 |
Connections = artl->ReadUint32(); |
119 |
artl->SetPos(HeaderSize); |
120 |
|
121 |
pConnections = new Connection[Connections]; |
122 |
Connection::conn_block_t connblock; |
123 |
for (uint32_t i = 0; i < Connections; i++) { |
124 |
artl->Read(&connblock.source, 1, 2); |
125 |
artl->Read(&connblock.control, 1, 2); |
126 |
artl->Read(&connblock.destination, 1, 2); |
127 |
artl->Read(&connblock.transform, 1, 2); |
128 |
artl->Read(&connblock.scale, 1, 4); |
129 |
pConnections[i].Init(&connblock); |
130 |
} |
131 |
} |
132 |
|
133 |
Articulation::~Articulation() { |
134 |
if (pConnections) delete[] pConnections; |
135 |
} |
136 |
|
137 |
/** |
138 |
* Apply articulation connections to the respective RIFF chunks. You |
139 |
* have to call File::Save() to make changes persistent. |
140 |
*/ |
141 |
void Articulation::UpdateChunks() { |
142 |
const int iEntrySize = 12; // 12 bytes per connection block |
143 |
pArticulationCk->Resize(HeaderSize + Connections * iEntrySize); |
144 |
uint8_t* pData = (uint8_t*) pArticulationCk->LoadChunkData(); |
145 |
memcpy(&pData[0], &HeaderSize, 2); |
146 |
memcpy(&pData[2], &Connections, 2); |
147 |
for (uint32_t i = 0; i < Connections; i++) { |
148 |
Connection::conn_block_t c = pConnections[i].ToConnBlock(); |
149 |
memcpy(&pData[HeaderSize + i * iEntrySize], &c.source, 2); |
150 |
memcpy(&pData[HeaderSize + i * iEntrySize + 2], &c.control, 2); |
151 |
memcpy(&pData[HeaderSize + i * iEntrySize + 4], &c.destination, 2); |
152 |
memcpy(&pData[HeaderSize + i * iEntrySize + 6], &c.transform, 2); |
153 |
memcpy(&pData[HeaderSize + i * iEntrySize + 8], &c.scale, 4); |
154 |
} |
155 |
} |
156 |
|
157 |
|
158 |
|
159 |
// *************** Articulator *************** |
160 |
// * |
161 |
|
162 |
Articulator::Articulator(RIFF::List* ParentList) { |
163 |
pParentList = ParentList; |
164 |
pArticulations = NULL; |
165 |
} |
166 |
|
167 |
Articulation* Articulator::GetFirstArticulation() { |
168 |
if (!pArticulations) LoadArticulations(); |
169 |
if (!pArticulations) return NULL; |
170 |
ArticulationsIterator = pArticulations->begin(); |
171 |
return (ArticulationsIterator != pArticulations->end()) ? *ArticulationsIterator : NULL; |
172 |
} |
173 |
|
174 |
Articulation* Articulator::GetNextArticulation() { |
175 |
if (!pArticulations) return NULL; |
176 |
ArticulationsIterator++; |
177 |
return (ArticulationsIterator != pArticulations->end()) ? *ArticulationsIterator : NULL; |
178 |
} |
179 |
|
180 |
void Articulator::LoadArticulations() { |
181 |
// prefer articulation level 2 |
182 |
RIFF::List* lart = pParentList->GetSubList(LIST_TYPE_LAR2); |
183 |
if (!lart) lart = pParentList->GetSubList(LIST_TYPE_LART); |
184 |
if (lart) { |
185 |
uint32_t artCkType = (lart->GetListType() == LIST_TYPE_LAR2) ? CHUNK_ID_ART2 |
186 |
: CHUNK_ID_ARTL; |
187 |
RIFF::Chunk* art = lart->GetFirstSubChunk(); |
188 |
while (art) { |
189 |
if (art->GetChunkID() == artCkType) { |
190 |
if (!pArticulations) pArticulations = new ArticulationList; |
191 |
pArticulations->push_back(new Articulation(art)); |
192 |
} |
193 |
art = lart->GetNextSubChunk(); |
194 |
} |
195 |
} |
196 |
} |
197 |
|
198 |
Articulator::~Articulator() { |
199 |
if (pArticulations) { |
200 |
ArticulationList::iterator iter = pArticulations->begin(); |
201 |
ArticulationList::iterator end = pArticulations->end(); |
202 |
while (iter != end) { |
203 |
delete *iter; |
204 |
iter++; |
205 |
} |
206 |
delete pArticulations; |
207 |
} |
208 |
} |
209 |
|
210 |
/** |
211 |
* Apply all articulations to the respective RIFF chunks. You have to |
212 |
* call File::Save() to make changes persistent. |
213 |
*/ |
214 |
void Articulator::UpdateChunks() { |
215 |
if (pArticulations) { |
216 |
ArticulationList::iterator iter = pArticulations->begin(); |
217 |
ArticulationList::iterator end = pArticulations->end(); |
218 |
for (; iter != end; ++iter) { |
219 |
(*iter)->UpdateChunks(); |
220 |
} |
221 |
} |
222 |
} |
223 |
|
224 |
|
225 |
|
226 |
// *************** Info *************** |
227 |
// * |
228 |
|
229 |
/** @brief Constructor. |
230 |
* |
231 |
* Initializes the info strings with values provided by a INFO list chunk. |
232 |
* |
233 |
* @param list - pointer to a list chunk which contains a INFO list chunk |
234 |
*/ |
235 |
Info::Info(RIFF::List* list) { |
236 |
UseFixedLengthStrings = false; |
237 |
pResourceListChunk = list; |
238 |
if (list) { |
239 |
RIFF::List* lstINFO = list->GetSubList(LIST_TYPE_INFO); |
240 |
if (lstINFO) { |
241 |
LoadString(CHUNK_ID_INAM, lstINFO, Name); |
242 |
LoadString(CHUNK_ID_IARL, lstINFO, ArchivalLocation); |
243 |
LoadString(CHUNK_ID_ICRD, lstINFO, CreationDate); |
244 |
LoadString(CHUNK_ID_ICMT, lstINFO, Comments); |
245 |
LoadString(CHUNK_ID_IPRD, lstINFO, Product); |
246 |
LoadString(CHUNK_ID_ICOP, lstINFO, Copyright); |
247 |
LoadString(CHUNK_ID_IART, lstINFO, Artists); |
248 |
LoadString(CHUNK_ID_IGNR, lstINFO, Genre); |
249 |
LoadString(CHUNK_ID_IKEY, lstINFO, Keywords); |
250 |
LoadString(CHUNK_ID_IENG, lstINFO, Engineer); |
251 |
LoadString(CHUNK_ID_ITCH, lstINFO, Technician); |
252 |
LoadString(CHUNK_ID_ISFT, lstINFO, Software); |
253 |
LoadString(CHUNK_ID_IMED, lstINFO, Medium); |
254 |
LoadString(CHUNK_ID_ISRC, lstINFO, Source); |
255 |
LoadString(CHUNK_ID_ISRF, lstINFO, SourceForm); |
256 |
LoadString(CHUNK_ID_ICMS, lstINFO, Commissioned); |
257 |
} |
258 |
} |
259 |
} |
260 |
|
261 |
Info::~Info() { |
262 |
} |
263 |
|
264 |
/** @brief Load given INFO field. |
265 |
* |
266 |
* Load INFO field from INFO chunk with chunk ID \a ChunkID from INFO |
267 |
* list chunk \a lstINFO and save value to \a s. |
268 |
*/ |
269 |
void Info::LoadString(uint32_t ChunkID, RIFF::List* lstINFO, String& s) { |
270 |
RIFF::Chunk* ck = lstINFO->GetSubChunk(ChunkID); |
271 |
if (ck) { |
272 |
const char* str = (char*)ck->LoadChunkData(); |
273 |
int size = ck->GetSize(); |
274 |
int len; |
275 |
for (len = 0 ; len < size ; len++) |
276 |
if (str[len] == '\0') break; |
277 |
s.assign(str, len); |
278 |
ck->ReleaseChunkData(); |
279 |
} |
280 |
} |
281 |
|
282 |
/** @brief Apply given INFO field to the respective chunk. |
283 |
* |
284 |
* Apply given info value to info chunk with ID \a ChunkID, which is a |
285 |
* subchunk of INFO list chunk \a lstINFO. If the given chunk already |
286 |
* exists, value \a s will be applied. Otherwise if it doesn't exist yet |
287 |
* and either \a s or \a sDefault is not an empty string, such a chunk |
288 |
* will be created and either \a s or \a sDefault will be applied |
289 |
* (depending on which one is not an empty string, if both are not an |
290 |
* empty string \a s will be preferred). |
291 |
* |
292 |
* @param ChunkID - 32 bit RIFF chunk ID of INFO subchunk |
293 |
* @param lstINFO - parent (INFO) RIFF list chunk |
294 |
* @param s - current value of info field |
295 |
* @param sDefault - default value |
296 |
* @param size - wanted size of the INFO chunk. This is ignored if UseFixedLengthStrings is false. |
297 |
*/ |
298 |
void Info::SaveString(uint32_t ChunkID, RIFF::List* lstINFO, const String& s, const String& sDefault, int size) { |
299 |
RIFF::Chunk* ck = lstINFO->GetSubChunk(ChunkID); |
300 |
if (ck) { // if chunk exists already, use 's' as value |
301 |
if (!UseFixedLengthStrings) size = s.size() + 1; |
302 |
ck->Resize(size); |
303 |
char* pData = (char*) ck->LoadChunkData(); |
304 |
strncpy(pData, s.c_str(), size); |
305 |
} else if (s != "" || sDefault != "") { // create chunk |
306 |
const String& sToSave = (s != "") ? s : sDefault; |
307 |
if (!UseFixedLengthStrings) size = sToSave.size() + 1; |
308 |
ck = lstINFO->AddSubChunk(ChunkID, size); |
309 |
char* pData = (char*) ck->LoadChunkData(); |
310 |
strncpy(pData, sToSave.c_str(), size); |
311 |
} |
312 |
} |
313 |
|
314 |
/** @brief Update chunks with current info values. |
315 |
* |
316 |
* Apply current INFO field values to the respective INFO chunks. You |
317 |
* have to call File::Save() to make changes persistent. |
318 |
*/ |
319 |
void Info::UpdateChunks() { |
320 |
if (!pResourceListChunk) return; |
321 |
|
322 |
// make sure INFO list chunk exists |
323 |
RIFF::List* lstINFO = pResourceListChunk->GetSubList(LIST_TYPE_INFO); |
324 |
|
325 |
String defaultName = ""; |
326 |
String defaultCreationDate = ""; |
327 |
String defaultSoftware = ""; |
328 |
String defaultComments = ""; |
329 |
|
330 |
uint32_t resourceType = pResourceListChunk->GetListType(); |
331 |
|
332 |
if (!lstINFO) { |
333 |
lstINFO = pResourceListChunk->AddSubList(LIST_TYPE_INFO); |
334 |
|
335 |
// assemble default values |
336 |
defaultName = "NONAME"; |
337 |
|
338 |
if (resourceType == RIFF_TYPE_DLS) { |
339 |
// get current date |
340 |
time_t now = time(NULL); |
341 |
tm* pNowBroken = localtime(&now); |
342 |
char buf[11]; |
343 |
strftime(buf, 11, "%F", pNowBroken); |
344 |
defaultCreationDate = buf; |
345 |
|
346 |
defaultComments = "Created with " + libraryName() + " " + libraryVersion(); |
347 |
} |
348 |
if (resourceType == RIFF_TYPE_DLS || resourceType == LIST_TYPE_INS) |
349 |
{ |
350 |
defaultSoftware = libraryName() + " " + libraryVersion(); |
351 |
} |
352 |
} |
353 |
|
354 |
// save values |
355 |
|
356 |
// (the string size values are for gig files; they are only |
357 |
// used if UseFixedLengthStrings is set to true) |
358 |
SaveString(CHUNK_ID_INAM, lstINFO, Name, defaultName, |
359 |
resourceType == RIFF_TYPE_DLS ? 128 : 64); |
360 |
SaveString(CHUNK_ID_IARL, lstINFO, ArchivalLocation, String(""), 256); |
361 |
SaveString(CHUNK_ID_ICRD, lstINFO, CreationDate, defaultCreationDate, 128); |
362 |
SaveString(CHUNK_ID_ICMT, lstINFO, Comments, defaultComments, 1024); |
363 |
SaveString(CHUNK_ID_IPRD, lstINFO, Product, String(""), 128); |
364 |
SaveString(CHUNK_ID_ICOP, lstINFO, Copyright, String(""), 128); |
365 |
SaveString(CHUNK_ID_IART, lstINFO, Artists, String(""), 128); |
366 |
SaveString(CHUNK_ID_IGNR, lstINFO, Genre, String(""), 128); |
367 |
SaveString(CHUNK_ID_IKEY, lstINFO, Keywords, String(""), 128); |
368 |
SaveString(CHUNK_ID_IENG, lstINFO, Engineer, String(""), 128); |
369 |
SaveString(CHUNK_ID_ITCH, lstINFO, Technician, String(""), 128); |
370 |
SaveString(CHUNK_ID_ISFT, lstINFO, Software, defaultSoftware, |
371 |
resourceType == LIST_TYPE_INS ? |
372 |
(Software == "" ? defaultSoftware.length() : Software.length()) : 128); |
373 |
SaveString(CHUNK_ID_IMED, lstINFO, Medium, String(""), 128); |
374 |
SaveString(CHUNK_ID_ISRC, lstINFO, Source, String(""), 128); |
375 |
SaveString(CHUNK_ID_ISRF, lstINFO, SourceForm, String(""), 128); |
376 |
SaveString(CHUNK_ID_ICMS, lstINFO, Commissioned, String(""), 128); |
377 |
} |
378 |
|
379 |
|
380 |
|
381 |
// *************** Resource *************** |
382 |
// * |
383 |
|
384 |
/** @brief Constructor. |
385 |
* |
386 |
* Initializes the 'Resource' object with values provided by a given |
387 |
* INFO list chunk and a DLID chunk (the latter optional). |
388 |
* |
389 |
* @param Parent - pointer to parent 'Resource', NULL if this is |
390 |
* the toplevel 'Resource' object |
391 |
* @param lstResource - pointer to an INFO list chunk |
392 |
*/ |
393 |
Resource::Resource(Resource* Parent, RIFF::List* lstResource) { |
394 |
pParent = Parent; |
395 |
pResourceList = lstResource; |
396 |
|
397 |
pInfo = new Info(lstResource); |
398 |
|
399 |
RIFF::Chunk* ckDLSID = lstResource->GetSubChunk(CHUNK_ID_DLID); |
400 |
if (ckDLSID) { |
401 |
pDLSID = new dlsid_t; |
402 |
ckDLSID->Read(&pDLSID->ulData1, 1, 4); |
403 |
ckDLSID->Read(&pDLSID->usData2, 1, 2); |
404 |
ckDLSID->Read(&pDLSID->usData3, 1, 2); |
405 |
ckDLSID->Read(pDLSID->abData, 8, 1); |
406 |
} |
407 |
else pDLSID = NULL; |
408 |
} |
409 |
|
410 |
Resource::~Resource() { |
411 |
if (pDLSID) delete pDLSID; |
412 |
if (pInfo) delete pInfo; |
413 |
} |
414 |
|
415 |
/** @brief Update chunks with current Resource data. |
416 |
* |
417 |
* Apply Resource data persistently below the previously given resource |
418 |
* list chunk. This will currently only include the INFO data. The DLSID |
419 |
* will not be applied at the moment (yet). |
420 |
* |
421 |
* You have to call File::Save() to make changes persistent. |
422 |
*/ |
423 |
void Resource::UpdateChunks() { |
424 |
pInfo->UpdateChunks(); |
425 |
//TODO: save DLSID |
426 |
} |
427 |
|
428 |
|
429 |
|
430 |
// *************** Sampler *************** |
431 |
// * |
432 |
|
433 |
Sampler::Sampler(RIFF::List* ParentList) { |
434 |
pParentList = ParentList; |
435 |
RIFF::Chunk* wsmp = ParentList->GetSubChunk(CHUNK_ID_WSMP); |
436 |
if (wsmp) { |
437 |
uiHeaderSize = wsmp->ReadUint32(); |
438 |
UnityNote = wsmp->ReadUint16(); |
439 |
FineTune = wsmp->ReadInt16(); |
440 |
Gain = wsmp->ReadInt32(); |
441 |
SamplerOptions = wsmp->ReadUint32(); |
442 |
SampleLoops = wsmp->ReadUint32(); |
443 |
} else { // 'wsmp' chunk missing |
444 |
uiHeaderSize = 0; |
445 |
UnityNote = 64; |
446 |
FineTune = 0; // +- 0 cents |
447 |
Gain = 0; // 0 dB |
448 |
SamplerOptions = F_WSMP_NO_COMPRESSION; |
449 |
SampleLoops = 0; |
450 |
} |
451 |
NoSampleDepthTruncation = SamplerOptions & F_WSMP_NO_TRUNCATION; |
452 |
NoSampleCompression = SamplerOptions & F_WSMP_NO_COMPRESSION; |
453 |
pSampleLoops = (SampleLoops) ? new sample_loop_t[SampleLoops] : NULL; |
454 |
if (SampleLoops) { |
455 |
wsmp->SetPos(uiHeaderSize); |
456 |
for (uint32_t i = 0; i < SampleLoops; i++) { |
457 |
wsmp->Read(pSampleLoops + i, 4, 4); |
458 |
if (pSampleLoops[i].Size > sizeof(sample_loop_t)) { // if loop struct was extended |
459 |
wsmp->SetPos(pSampleLoops[i].Size - sizeof(sample_loop_t), RIFF::stream_curpos); |
460 |
} |
461 |
} |
462 |
} |
463 |
} |
464 |
|
465 |
Sampler::~Sampler() { |
466 |
if (pSampleLoops) delete[] pSampleLoops; |
467 |
} |
468 |
|
469 |
/** |
470 |
* Apply all sample player options to the respective RIFF chunk. You |
471 |
* have to call File::Save() to make changes persistent. |
472 |
*/ |
473 |
void Sampler::UpdateChunks() { |
474 |
// make sure 'wsmp' chunk exists |
475 |
RIFF::Chunk* wsmp = pParentList->GetSubChunk(CHUNK_ID_WSMP); |
476 |
if (!wsmp) { |
477 |
uiHeaderSize = 20; |
478 |
wsmp = pParentList->AddSubChunk(CHUNK_ID_WSMP, uiHeaderSize + SampleLoops * 16); |
479 |
} |
480 |
uint8_t* pData = (uint8_t*) wsmp->LoadChunkData(); |
481 |
// update headers size |
482 |
memcpy(&pData[0], &uiHeaderSize, 4); |
483 |
// update respective sampler options bits |
484 |
SamplerOptions = (NoSampleDepthTruncation) ? SamplerOptions | F_WSMP_NO_TRUNCATION |
485 |
: SamplerOptions & (~F_WSMP_NO_TRUNCATION); |
486 |
SamplerOptions = (NoSampleCompression) ? SamplerOptions | F_WSMP_NO_COMPRESSION |
487 |
: SamplerOptions & (~F_WSMP_NO_COMPRESSION); |
488 |
// update loop definitions |
489 |
for (uint32_t i = 0; i < SampleLoops; i++) { |
490 |
//FIXME: this does not handle extended loop structs correctly |
491 |
memcpy(&pData[uiHeaderSize + i * 16], pSampleLoops + i, 4 * 4); |
492 |
} |
493 |
} |
494 |
|
495 |
|
496 |
|
497 |
// *************** Sample *************** |
498 |
// * |
499 |
|
500 |
/** @brief Constructor. |
501 |
* |
502 |
* Load an existing sample or create a new one. A 'wave' list chunk must |
503 |
* be given to this constructor. In case the given 'wave' list chunk |
504 |
* contains a 'fmt' and 'data' chunk, the format and sample data will be |
505 |
* loaded from there, otherwise default values will be used and those |
506 |
* chunks will be created when File::Save() will be called later on. |
507 |
* |
508 |
* @param pFile - pointer to DLS::File where this sample is |
509 |
* located (or will be located) |
510 |
* @param waveList - pointer to 'wave' list chunk which is (or |
511 |
* will be) associated with this sample |
512 |
* @param WavePoolOffset - offset of this sample data from wave pool |
513 |
* ('wvpl') list chunk |
514 |
*/ |
515 |
Sample::Sample(File* pFile, RIFF::List* waveList, unsigned long WavePoolOffset) : Resource(pFile, waveList) { |
516 |
pWaveList = waveList; |
517 |
ulWavePoolOffset = WavePoolOffset - LIST_HEADER_SIZE; |
518 |
pCkFormat = waveList->GetSubChunk(CHUNK_ID_FMT); |
519 |
pCkData = waveList->GetSubChunk(CHUNK_ID_DATA); |
520 |
if (pCkFormat) { |
521 |
// common fields |
522 |
FormatTag = pCkFormat->ReadUint16(); |
523 |
Channels = pCkFormat->ReadUint16(); |
524 |
SamplesPerSecond = pCkFormat->ReadUint32(); |
525 |
AverageBytesPerSecond = pCkFormat->ReadUint32(); |
526 |
BlockAlign = pCkFormat->ReadUint16(); |
527 |
// PCM format specific |
528 |
if (FormatTag == WAVE_FORMAT_PCM) { |
529 |
BitDepth = pCkFormat->ReadUint16(); |
530 |
FrameSize = (FormatTag == WAVE_FORMAT_PCM) ? (BitDepth / 8) * Channels |
531 |
: 0; |
532 |
} else { // unsupported sample data format |
533 |
BitDepth = 0; |
534 |
FrameSize = 0; |
535 |
} |
536 |
} else { // 'fmt' chunk missing |
537 |
FormatTag = WAVE_FORMAT_PCM; |
538 |
BitDepth = 16; |
539 |
Channels = 1; |
540 |
SamplesPerSecond = 44100; |
541 |
AverageBytesPerSecond = (BitDepth / 8) * SamplesPerSecond * Channels; |
542 |
FrameSize = (BitDepth / 8) * Channels; |
543 |
BlockAlign = FrameSize; |
544 |
} |
545 |
SamplesTotal = (pCkData) ? (FormatTag == WAVE_FORMAT_PCM) ? pCkData->GetSize() / FrameSize |
546 |
: 0 |
547 |
: 0; |
548 |
} |
549 |
|
550 |
/** @brief Destructor. |
551 |
* |
552 |
* Removes RIFF chunks associated with this Sample and frees all |
553 |
* memory occupied by this sample. |
554 |
*/ |
555 |
Sample::~Sample() { |
556 |
RIFF::List* pParent = pWaveList->GetParent(); |
557 |
pParent->DeleteSubChunk(pWaveList); |
558 |
} |
559 |
|
560 |
/** @brief Load sample data into RAM. |
561 |
* |
562 |
* In case the respective 'data' chunk exists, the sample data will be |
563 |
* loaded into RAM (if not done already) and a pointer to the data in |
564 |
* RAM will be returned. If this is a new sample, you have to call |
565 |
* Resize() with the desired sample size to create the mandatory RIFF |
566 |
* chunk for the sample wave data. |
567 |
* |
568 |
* You can call LoadChunkData() again if you previously scheduled to |
569 |
* enlarge the sample data RIFF chunk with a Resize() call. In that case |
570 |
* the buffer will be enlarged to the new, scheduled size and you can |
571 |
* already place the sample wave data to the buffer and finally call |
572 |
* File::Save() to enlarge the sample data's chunk physically and write |
573 |
* the new sample wave data in one rush. This approach is definitely |
574 |
* recommended if you have to enlarge and write new sample data to a lot |
575 |
* of samples. |
576 |
* |
577 |
* <b>Caution:</b> the buffer pointer will be invalidated once |
578 |
* File::Save() was called. You have to call LoadChunkData() again to |
579 |
* get a new, valid pointer whenever File::Save() was called. |
580 |
* |
581 |
* @returns pointer to sample data in RAM, NULL in case respective |
582 |
* 'data' chunk does not exist (yet) |
583 |
* @throws Exception if data buffer could not be enlarged |
584 |
* @see Resize(), File::Save() |
585 |
*/ |
586 |
void* Sample::LoadSampleData() { |
587 |
return (pCkData) ? pCkData->LoadChunkData() : NULL; |
588 |
} |
589 |
|
590 |
/** @brief Free sample data from RAM. |
591 |
* |
592 |
* In case sample data was previously successfully loaded into RAM with |
593 |
* LoadSampleData(), this method will free the sample data from RAM. |
594 |
*/ |
595 |
void Sample::ReleaseSampleData() { |
596 |
if (pCkData) pCkData->ReleaseChunkData(); |
597 |
} |
598 |
|
599 |
/** @brief Returns sample size. |
600 |
* |
601 |
* Returns the sample wave form's data size (in sample points). This is |
602 |
* actually the current, physical size (converted to sample points) of |
603 |
* the RIFF chunk which encapsulates the sample's wave data. The |
604 |
* returned value is dependant to the current FrameSize value. |
605 |
* |
606 |
* @returns number of sample points or 0 if FormatTag != WAVE_FORMAT_PCM |
607 |
* @see FrameSize, FormatTag |
608 |
*/ |
609 |
unsigned long Sample::GetSize() { |
610 |
if (FormatTag != WAVE_FORMAT_PCM) return 0; |
611 |
return (pCkData) ? pCkData->GetSize() / FrameSize : 0; |
612 |
} |
613 |
|
614 |
/** @brief Resize sample. |
615 |
* |
616 |
* Resizes the sample's wave form data, that is the actual size of |
617 |
* sample wave data possible to be written for this sample. This call |
618 |
* will return immediately and just schedule the resize operation. You |
619 |
* should call File::Save() to actually perform the resize operation(s) |
620 |
* "physically" to the file. As this can take a while on large files, it |
621 |
* is recommended to call Resize() first on all samples which have to be |
622 |
* resized and finally to call File::Save() to perform all those resize |
623 |
* operations in one rush. |
624 |
* |
625 |
* The actual size (in bytes) is dependant to the current FrameSize |
626 |
* value. You may want to set FrameSize before calling Resize(). |
627 |
* |
628 |
* <b>Caution:</b> You cannot directly write to enlarged samples before |
629 |
* calling File::Save() as this might exceed the current sample's |
630 |
* boundary! |
631 |
* |
632 |
* Also note: only WAVE_FORMAT_PCM is currently supported, that is |
633 |
* FormatTag must be WAVE_FORMAT_PCM. Trying to resize samples with |
634 |
* other formats will fail! |
635 |
* |
636 |
* @param iNewSize - new sample wave data size in sample points (must be |
637 |
* greater than zero) |
638 |
* @throws Excecption if FormatTag != WAVE_FORMAT_PCM |
639 |
* @throws Exception if \a iNewSize is less than 1 |
640 |
* @see File::Save(), FrameSize, FormatTag |
641 |
*/ |
642 |
void Sample::Resize(int iNewSize) { |
643 |
if (FormatTag != WAVE_FORMAT_PCM) throw Exception("Sample's format is not WAVE_FORMAT_PCM"); |
644 |
if (iNewSize < 1) throw Exception("Sample size must be at least one sample point"); |
645 |
const int iSizeInBytes = iNewSize * FrameSize; |
646 |
pCkData = pWaveList->GetSubChunk(CHUNK_ID_DATA); |
647 |
if (pCkData) pCkData->Resize(iSizeInBytes); |
648 |
else pCkData = pWaveList->AddSubChunk(CHUNK_ID_DATA, iSizeInBytes); |
649 |
} |
650 |
|
651 |
/** |
652 |
* Sets the position within the sample (in sample points, not in |
653 |
* bytes). Use this method and <i>Read()</i> if you don't want to load |
654 |
* the sample into RAM, thus for disk streaming. |
655 |
* |
656 |
* Also note: only WAVE_FORMAT_PCM is currently supported, that is |
657 |
* FormatTag must be WAVE_FORMAT_PCM. Trying to reposition the sample |
658 |
* with other formats will fail! |
659 |
* |
660 |
* @param SampleCount number of sample points |
661 |
* @param Whence to which relation \a SampleCount refers to |
662 |
* @returns new position within the sample, 0 if |
663 |
* FormatTag != WAVE_FORMAT_PCM |
664 |
* @throws Exception if no data RIFF chunk was created for the sample yet |
665 |
* @see FrameSize, FormatTag |
666 |
*/ |
667 |
unsigned long Sample::SetPos(unsigned long SampleCount, RIFF::stream_whence_t Whence) { |
668 |
if (FormatTag != WAVE_FORMAT_PCM) return 0; // failed: wave data not PCM format |
669 |
if (!pCkData) throw Exception("No data chunk created for sample yet, call Sample::Resize() to create one"); |
670 |
unsigned long orderedBytes = SampleCount * FrameSize; |
671 |
unsigned long result = pCkData->SetPos(orderedBytes, Whence); |
672 |
return (result == orderedBytes) ? SampleCount |
673 |
: result / FrameSize; |
674 |
} |
675 |
|
676 |
/** |
677 |
* Reads \a SampleCount number of sample points from the current |
678 |
* position into the buffer pointed by \a pBuffer and increments the |
679 |
* position within the sample. Use this method and <i>SetPos()</i> if you |
680 |
* don't want to load the sample into RAM, thus for disk streaming. |
681 |
* |
682 |
* @param pBuffer destination buffer |
683 |
* @param SampleCount number of sample points to read |
684 |
*/ |
685 |
unsigned long Sample::Read(void* pBuffer, unsigned long SampleCount) { |
686 |
if (FormatTag != WAVE_FORMAT_PCM) return 0; // failed: wave data not PCM format |
687 |
return pCkData->Read(pBuffer, SampleCount, FrameSize); // FIXME: channel inversion due to endian correction? |
688 |
} |
689 |
|
690 |
/** @brief Write sample wave data. |
691 |
* |
692 |
* Writes \a SampleCount number of sample points from the buffer pointed |
693 |
* by \a pBuffer and increments the position within the sample. Use this |
694 |
* method to directly write the sample data to disk, i.e. if you don't |
695 |
* want or cannot load the whole sample data into RAM. |
696 |
* |
697 |
* You have to Resize() the sample to the desired size and call |
698 |
* File::Save() <b>before</b> using Write(). |
699 |
* |
700 |
* @param pBuffer - source buffer |
701 |
* @param SampleCount - number of sample points to write |
702 |
* @throws Exception if current sample size is too small |
703 |
* @see LoadSampleData() |
704 |
*/ |
705 |
unsigned long Sample::Write(void* pBuffer, unsigned long SampleCount) { |
706 |
if (FormatTag != WAVE_FORMAT_PCM) return 0; // failed: wave data not PCM format |
707 |
if (GetSize() < SampleCount) throw Exception("Could not write sample data, current sample size to small"); |
708 |
return pCkData->Write(pBuffer, SampleCount, FrameSize); // FIXME: channel inversion due to endian correction? |
709 |
} |
710 |
|
711 |
/** |
712 |
* Apply sample and its settings to the respective RIFF chunks. You have |
713 |
* to call File::Save() to make changes persistent. |
714 |
* |
715 |
* @throws Exception if FormatTag != WAVE_FORMAT_PCM or no sample data |
716 |
* was provided yet |
717 |
*/ |
718 |
void Sample::UpdateChunks() { |
719 |
if (FormatTag != WAVE_FORMAT_PCM) |
720 |
throw Exception("Could not save sample, only PCM format is supported"); |
721 |
// we refuse to do anything if not sample wave form was provided yet |
722 |
if (!pCkData) |
723 |
throw Exception("Could not save sample, there is no sample data to save"); |
724 |
// update chunks of base class as well |
725 |
Resource::UpdateChunks(); |
726 |
// make sure 'fmt' chunk exists |
727 |
RIFF::Chunk* pCkFormat = pWaveList->GetSubChunk(CHUNK_ID_FMT); |
728 |
if (!pCkFormat) pCkFormat = pWaveList->AddSubChunk(CHUNK_ID_FMT, 16); // assumes PCM format |
729 |
uint8_t* pData = (uint8_t*) pCkFormat->LoadChunkData(); |
730 |
// update 'fmt' chunk |
731 |
memcpy(&pData[0], &FormatTag, 2); |
732 |
memcpy(&pData[2], &Channels, 2); |
733 |
memcpy(&pData[4], &SamplesPerSecond, 4); |
734 |
memcpy(&pData[8], &AverageBytesPerSecond, 4); |
735 |
memcpy(&pData[12], &BlockAlign, 2); |
736 |
memcpy(&pData[14], &BitDepth, 2); // assuming PCM format |
737 |
} |
738 |
|
739 |
|
740 |
|
741 |
// *************** Region *************** |
742 |
// * |
743 |
|
744 |
Region::Region(Instrument* pInstrument, RIFF::List* rgnList) : Resource(pInstrument, rgnList), Articulator(rgnList), Sampler(rgnList) { |
745 |
pCkRegion = rgnList; |
746 |
|
747 |
// articulation informations |
748 |
RIFF::Chunk* rgnh = rgnList->GetSubChunk(CHUNK_ID_RGNH); |
749 |
if (rgnh) { |
750 |
rgnh->Read(&KeyRange, 2, 2); |
751 |
rgnh->Read(&VelocityRange, 2, 2); |
752 |
FormatOptionFlags = rgnh->ReadUint16(); |
753 |
KeyGroup = rgnh->ReadUint16(); |
754 |
// Layer is optional |
755 |
if (rgnh->RemainingBytes() >= sizeof(uint16_t)) { |
756 |
rgnh->Read(&Layer, 1, sizeof(uint16_t)); |
757 |
} else Layer = 0; |
758 |
} else { // 'rgnh' chunk is missing |
759 |
KeyRange.low = 0; |
760 |
KeyRange.high = 127; |
761 |
VelocityRange.low = 0; |
762 |
VelocityRange.high = 127; |
763 |
FormatOptionFlags = F_RGN_OPTION_SELFNONEXCLUSIVE; |
764 |
KeyGroup = 0; |
765 |
Layer = 0; |
766 |
} |
767 |
SelfNonExclusive = FormatOptionFlags & F_RGN_OPTION_SELFNONEXCLUSIVE; |
768 |
|
769 |
// sample informations |
770 |
RIFF::Chunk* wlnk = rgnList->GetSubChunk(CHUNK_ID_WLNK); |
771 |
if (wlnk) { |
772 |
WaveLinkOptionFlags = wlnk->ReadUint16(); |
773 |
PhaseGroup = wlnk->ReadUint16(); |
774 |
Channel = wlnk->ReadUint32(); |
775 |
WavePoolTableIndex = wlnk->ReadUint32(); |
776 |
} else { // 'wlnk' chunk is missing |
777 |
WaveLinkOptionFlags = 0; |
778 |
PhaseGroup = 0; |
779 |
Channel = 0; // mono |
780 |
WavePoolTableIndex = 0; // first entry in wave pool table |
781 |
} |
782 |
PhaseMaster = WaveLinkOptionFlags & F_WAVELINK_PHASE_MASTER; |
783 |
MultiChannel = WaveLinkOptionFlags & F_WAVELINK_MULTICHANNEL; |
784 |
|
785 |
pSample = NULL; |
786 |
} |
787 |
|
788 |
/** @brief Destructor. |
789 |
* |
790 |
* Removes RIFF chunks associated with this Region. |
791 |
*/ |
792 |
Region::~Region() { |
793 |
RIFF::List* pParent = pCkRegion->GetParent(); |
794 |
pParent->DeleteSubChunk(pCkRegion); |
795 |
} |
796 |
|
797 |
Sample* Region::GetSample() { |
798 |
if (pSample) return pSample; |
799 |
File* file = (File*) GetParent()->GetParent(); |
800 |
unsigned long soughtoffset = file->pWavePoolTable[WavePoolTableIndex]; |
801 |
Sample* sample = file->GetFirstSample(); |
802 |
while (sample) { |
803 |
if (sample->ulWavePoolOffset == soughtoffset) return (pSample = sample); |
804 |
sample = file->GetNextSample(); |
805 |
} |
806 |
return NULL; |
807 |
} |
808 |
|
809 |
/** |
810 |
* Assign another sample to this Region. |
811 |
* |
812 |
* @param pSample - sample to be assigned |
813 |
*/ |
814 |
void Region::SetSample(Sample* pSample) { |
815 |
this->pSample = pSample; |
816 |
WavePoolTableIndex = 0; // we update this offset when we Save() |
817 |
} |
818 |
|
819 |
/** |
820 |
* Apply Region settings to the respective RIFF chunks. You have to |
821 |
* call File::Save() to make changes persistent. |
822 |
* |
823 |
* @throws Exception - if the Region's sample could not be found |
824 |
*/ |
825 |
void Region::UpdateChunks() { |
826 |
// make sure 'rgnh' chunk exists |
827 |
RIFF::Chunk* rgnh = pCkRegion->GetSubChunk(CHUNK_ID_RGNH); |
828 |
if (!rgnh) rgnh = pCkRegion->AddSubChunk(CHUNK_ID_RGNH, Layer ? 14 : 12); |
829 |
uint8_t* pData = (uint8_t*) rgnh->LoadChunkData(); |
830 |
FormatOptionFlags = (SelfNonExclusive) |
831 |
? FormatOptionFlags | F_RGN_OPTION_SELFNONEXCLUSIVE |
832 |
: FormatOptionFlags & (~F_RGN_OPTION_SELFNONEXCLUSIVE); |
833 |
// update 'rgnh' chunk |
834 |
memcpy(&pData[0], &KeyRange, 2 * 2); |
835 |
memcpy(&pData[4], &VelocityRange, 2 * 2); |
836 |
memcpy(&pData[8], &FormatOptionFlags, 2); |
837 |
memcpy(&pData[10], &KeyGroup, 2); |
838 |
if (rgnh->GetSize() >= 14) memcpy(&pData[12], &Layer, 2); |
839 |
|
840 |
// update chunks of base classes as well (but skip Resource, |
841 |
// as a rgn doesn't seem to have dlid and INFO chunks) |
842 |
Articulator::UpdateChunks(); |
843 |
Sampler::UpdateChunks(); |
844 |
|
845 |
// make sure 'wlnk' chunk exists |
846 |
RIFF::Chunk* wlnk = pCkRegion->GetSubChunk(CHUNK_ID_WLNK); |
847 |
if (!wlnk) wlnk = pCkRegion->AddSubChunk(CHUNK_ID_WLNK, 12); |
848 |
pData = (uint8_t*) wlnk->LoadChunkData(); |
849 |
WaveLinkOptionFlags = (PhaseMaster) |
850 |
? WaveLinkOptionFlags | F_WAVELINK_PHASE_MASTER |
851 |
: WaveLinkOptionFlags & (~F_WAVELINK_PHASE_MASTER); |
852 |
WaveLinkOptionFlags = (MultiChannel) |
853 |
? WaveLinkOptionFlags | F_WAVELINK_MULTICHANNEL |
854 |
: WaveLinkOptionFlags & (~F_WAVELINK_MULTICHANNEL); |
855 |
// get sample's wave pool table index |
856 |
int index = -1; |
857 |
File* pFile = (File*) GetParent()->GetParent(); |
858 |
if (pFile->pSamples) { |
859 |
File::SampleList::iterator iter = pFile->pSamples->begin(); |
860 |
File::SampleList::iterator end = pFile->pSamples->end(); |
861 |
for (int i = 0; iter != end; ++iter, i++) { |
862 |
if (*iter == pSample) { |
863 |
index = i; |
864 |
break; |
865 |
} |
866 |
} |
867 |
} |
868 |
if (index < 0) throw Exception("Could not save Region, could not find Region's sample"); |
869 |
WavePoolTableIndex = index; |
870 |
// update 'wlnk' chunk |
871 |
memcpy(&pData[0], &WaveLinkOptionFlags, 2); |
872 |
memcpy(&pData[2], &PhaseGroup, 2); |
873 |
memcpy(&pData[4], &Channel, 4); |
874 |
memcpy(&pData[8], &WavePoolTableIndex, 4); |
875 |
} |
876 |
|
877 |
|
878 |
|
879 |
// *************** Instrument *************** |
880 |
// * |
881 |
|
882 |
/** @brief Constructor. |
883 |
* |
884 |
* Load an existing instrument definition or create a new one. An 'ins' |
885 |
* list chunk must be given to this constructor. In case this 'ins' list |
886 |
* chunk contains a 'insh' chunk, the instrument data fields will be |
887 |
* loaded from there, otherwise default values will be used and the |
888 |
* 'insh' chunk will be created once File::Save() was called. |
889 |
* |
890 |
* @param pFile - pointer to DLS::File where this instrument is |
891 |
* located (or will be located) |
892 |
* @param insList - pointer to 'ins' list chunk which is (or will be) |
893 |
* associated with this instrument |
894 |
*/ |
895 |
Instrument::Instrument(File* pFile, RIFF::List* insList) : Resource(pFile, insList), Articulator(insList) { |
896 |
pCkInstrument = insList; |
897 |
|
898 |
midi_locale_t locale; |
899 |
RIFF::Chunk* insh = pCkInstrument->GetSubChunk(CHUNK_ID_INSH); |
900 |
if (insh) { |
901 |
Regions = insh->ReadUint32(); |
902 |
insh->Read(&locale, 2, 4); |
903 |
} else { // 'insh' chunk missing |
904 |
Regions = 0; |
905 |
locale.bank = 0; |
906 |
locale.instrument = 0; |
907 |
} |
908 |
|
909 |
MIDIProgram = locale.instrument; |
910 |
IsDrum = locale.bank & DRUM_TYPE_MASK; |
911 |
MIDIBankCoarse = (uint8_t) MIDI_BANK_COARSE(locale.bank); |
912 |
MIDIBankFine = (uint8_t) MIDI_BANK_FINE(locale.bank); |
913 |
MIDIBank = MIDI_BANK_MERGE(MIDIBankCoarse, MIDIBankFine); |
914 |
|
915 |
pRegions = NULL; |
916 |
} |
917 |
|
918 |
Region* Instrument::GetFirstRegion() { |
919 |
if (!pRegions) LoadRegions(); |
920 |
if (!pRegions) return NULL; |
921 |
RegionsIterator = pRegions->begin(); |
922 |
return (RegionsIterator != pRegions->end()) ? *RegionsIterator : NULL; |
923 |
} |
924 |
|
925 |
Region* Instrument::GetNextRegion() { |
926 |
if (!pRegions) return NULL; |
927 |
RegionsIterator++; |
928 |
return (RegionsIterator != pRegions->end()) ? *RegionsIterator : NULL; |
929 |
} |
930 |
|
931 |
void Instrument::LoadRegions() { |
932 |
if (!pRegions) pRegions = new RegionList; |
933 |
RIFF::List* lrgn = pCkInstrument->GetSubList(LIST_TYPE_LRGN); |
934 |
if (lrgn) { |
935 |
uint32_t regionCkType = (lrgn->GetSubList(LIST_TYPE_RGN2)) ? LIST_TYPE_RGN2 : LIST_TYPE_RGN; // prefer regions level 2 |
936 |
RIFF::List* rgn = lrgn->GetFirstSubList(); |
937 |
while (rgn) { |
938 |
if (rgn->GetListType() == regionCkType) { |
939 |
pRegions->push_back(new Region(this, rgn)); |
940 |
} |
941 |
rgn = lrgn->GetNextSubList(); |
942 |
} |
943 |
} |
944 |
} |
945 |
|
946 |
Region* Instrument::AddRegion() { |
947 |
if (!pRegions) LoadRegions(); |
948 |
RIFF::List* lrgn = pCkInstrument->GetSubList(LIST_TYPE_LRGN); |
949 |
if (!lrgn) lrgn = pCkInstrument->AddSubList(LIST_TYPE_LRGN); |
950 |
RIFF::List* rgn = lrgn->AddSubList(LIST_TYPE_RGN); |
951 |
Region* pNewRegion = new Region(this, rgn); |
952 |
pRegions->push_back(pNewRegion); |
953 |
Regions = pRegions->size(); |
954 |
return pNewRegion; |
955 |
} |
956 |
|
957 |
void Instrument::DeleteRegion(Region* pRegion) { |
958 |
if (!pRegions) return; |
959 |
RegionList::iterator iter = find(pRegions->begin(), pRegions->end(), pRegion); |
960 |
if (iter == pRegions->end()) return; |
961 |
pRegions->erase(iter); |
962 |
Regions = pRegions->size(); |
963 |
delete pRegion; |
964 |
} |
965 |
|
966 |
/** |
967 |
* Apply Instrument with all its Regions to the respective RIFF chunks. |
968 |
* You have to call File::Save() to make changes persistent. |
969 |
* |
970 |
* @throws Exception - on errors |
971 |
*/ |
972 |
void Instrument::UpdateChunks() { |
973 |
// first update base classes' chunks |
974 |
Resource::UpdateChunks(); |
975 |
Articulator::UpdateChunks(); |
976 |
// make sure 'insh' chunk exists |
977 |
RIFF::Chunk* insh = pCkInstrument->GetSubChunk(CHUNK_ID_INSH); |
978 |
if (!insh) insh = pCkInstrument->AddSubChunk(CHUNK_ID_INSH, 12); |
979 |
uint8_t* pData = (uint8_t*) insh->LoadChunkData(); |
980 |
// update 'insh' chunk |
981 |
Regions = (pRegions) ? pRegions->size() : 0; |
982 |
midi_locale_t locale; |
983 |
locale.instrument = MIDIProgram; |
984 |
locale.bank = MIDI_BANK_ENCODE(MIDIBankCoarse, MIDIBankFine); |
985 |
locale.bank = (IsDrum) ? locale.bank | DRUM_TYPE_MASK : locale.bank & (~DRUM_TYPE_MASK); |
986 |
MIDIBank = MIDI_BANK_MERGE(MIDIBankCoarse, MIDIBankFine); // just a sync, when we're at it |
987 |
memcpy(&pData[0], &Regions, 4); |
988 |
memcpy(&pData[4], &locale, 2 * 4); |
989 |
// update Region's chunks |
990 |
if (!pRegions) return; |
991 |
RegionList::iterator iter = pRegions->begin(); |
992 |
RegionList::iterator end = pRegions->end(); |
993 |
for (; iter != end; ++iter) { |
994 |
(*iter)->UpdateChunks(); |
995 |
} |
996 |
} |
997 |
|
998 |
/** @brief Destructor. |
999 |
* |
1000 |
* Removes RIFF chunks associated with this Instrument and frees all |
1001 |
* memory occupied by this instrument. |
1002 |
*/ |
1003 |
Instrument::~Instrument() { |
1004 |
if (pRegions) { |
1005 |
RegionList::iterator iter = pRegions->begin(); |
1006 |
RegionList::iterator end = pRegions->end(); |
1007 |
while (iter != end) { |
1008 |
delete *iter; |
1009 |
iter++; |
1010 |
} |
1011 |
delete pRegions; |
1012 |
} |
1013 |
// remove instrument's chunks |
1014 |
RIFF::List* pParent = pCkInstrument->GetParent(); |
1015 |
pParent->DeleteSubChunk(pCkInstrument); |
1016 |
} |
1017 |
|
1018 |
|
1019 |
|
1020 |
// *************** File *************** |
1021 |
// * |
1022 |
|
1023 |
/** @brief Constructor. |
1024 |
* |
1025 |
* Default constructor, use this to create an empty DLS file. You have |
1026 |
* to add samples, instruments and finally call Save() to actually write |
1027 |
* a DLS file. |
1028 |
*/ |
1029 |
File::File() : Resource(NULL, pRIFF = new RIFF::File(RIFF_TYPE_DLS)) { |
1030 |
pVersion = new version_t; |
1031 |
pVersion->major = 0; |
1032 |
pVersion->minor = 0; |
1033 |
pVersion->release = 0; |
1034 |
pVersion->build = 0; |
1035 |
|
1036 |
Instruments = 0; |
1037 |
WavePoolCount = 0; |
1038 |
pWavePoolTable = NULL; |
1039 |
pWavePoolTableHi = NULL; |
1040 |
WavePoolHeaderSize = 8; |
1041 |
|
1042 |
pSamples = NULL; |
1043 |
pInstruments = NULL; |
1044 |
|
1045 |
b64BitWavePoolOffsets = false; |
1046 |
} |
1047 |
|
1048 |
/** @brief Constructor. |
1049 |
* |
1050 |
* Load an existing DLS file. |
1051 |
* |
1052 |
* @param pRIFF - pointer to a RIFF file which is actually the DLS file |
1053 |
* to load |
1054 |
* @throws Exception if given file is not a DLS file, expected chunks |
1055 |
* are missing |
1056 |
*/ |
1057 |
File::File(RIFF::File* pRIFF) : Resource(NULL, pRIFF) { |
1058 |
if (!pRIFF) throw DLS::Exception("NULL pointer reference to RIFF::File object."); |
1059 |
this->pRIFF = pRIFF; |
1060 |
|
1061 |
RIFF::Chunk* ckVersion = pRIFF->GetSubChunk(CHUNK_ID_VERS); |
1062 |
if (ckVersion) { |
1063 |
pVersion = new version_t; |
1064 |
ckVersion->Read(pVersion, 4, 2); |
1065 |
} |
1066 |
else pVersion = NULL; |
1067 |
|
1068 |
RIFF::Chunk* colh = pRIFF->GetSubChunk(CHUNK_ID_COLH); |
1069 |
if (!colh) throw DLS::Exception("Mandatory chunks in RIFF list chunk not found."); |
1070 |
Instruments = colh->ReadUint32(); |
1071 |
|
1072 |
RIFF::Chunk* ptbl = pRIFF->GetSubChunk(CHUNK_ID_PTBL); |
1073 |
if (!ptbl) { // pool table is missing - this is probably an ".art" file |
1074 |
WavePoolCount = 0; |
1075 |
pWavePoolTable = NULL; |
1076 |
pWavePoolTableHi = NULL; |
1077 |
WavePoolHeaderSize = 8; |
1078 |
b64BitWavePoolOffsets = false; |
1079 |
} else { |
1080 |
WavePoolHeaderSize = ptbl->ReadUint32(); |
1081 |
WavePoolCount = ptbl->ReadUint32(); |
1082 |
pWavePoolTable = new uint32_t[WavePoolCount]; |
1083 |
pWavePoolTableHi = new uint32_t[WavePoolCount]; |
1084 |
ptbl->SetPos(WavePoolHeaderSize); |
1085 |
|
1086 |
// Check for 64 bit offsets (used in gig v3 files) |
1087 |
b64BitWavePoolOffsets = (ptbl->GetSize() - WavePoolHeaderSize == WavePoolCount * 8); |
1088 |
if (b64BitWavePoolOffsets) { |
1089 |
for (int i = 0 ; i < WavePoolCount ; i++) { |
1090 |
pWavePoolTableHi[i] = ptbl->ReadUint32(); |
1091 |
pWavePoolTable[i] = ptbl->ReadUint32(); |
1092 |
if (pWavePoolTable[i] & 0x80000000) |
1093 |
throw DLS::Exception("Files larger than 2 GB not yet supported"); |
1094 |
} |
1095 |
} else { // conventional 32 bit offsets |
1096 |
ptbl->Read(pWavePoolTable, WavePoolCount, sizeof(uint32_t)); |
1097 |
for (int i = 0 ; i < WavePoolCount ; i++) pWavePoolTableHi[i] = 0; |
1098 |
} |
1099 |
} |
1100 |
|
1101 |
pSamples = NULL; |
1102 |
pInstruments = NULL; |
1103 |
} |
1104 |
|
1105 |
File::~File() { |
1106 |
if (pInstruments) { |
1107 |
InstrumentList::iterator iter = pInstruments->begin(); |
1108 |
InstrumentList::iterator end = pInstruments->end(); |
1109 |
while (iter != end) { |
1110 |
delete *iter; |
1111 |
iter++; |
1112 |
} |
1113 |
delete pInstruments; |
1114 |
} |
1115 |
|
1116 |
if (pSamples) { |
1117 |
SampleList::iterator iter = pSamples->begin(); |
1118 |
SampleList::iterator end = pSamples->end(); |
1119 |
while (iter != end) { |
1120 |
delete *iter; |
1121 |
iter++; |
1122 |
} |
1123 |
delete pSamples; |
1124 |
} |
1125 |
|
1126 |
if (pWavePoolTable) delete[] pWavePoolTable; |
1127 |
if (pWavePoolTableHi) delete[] pWavePoolTableHi; |
1128 |
if (pVersion) delete pVersion; |
1129 |
for (std::list<RIFF::File*>::iterator i = ExtensionFiles.begin() ; i != ExtensionFiles.end() ; i++) |
1130 |
delete *i; |
1131 |
} |
1132 |
|
1133 |
Sample* File::GetFirstSample() { |
1134 |
if (!pSamples) LoadSamples(); |
1135 |
if (!pSamples) return NULL; |
1136 |
SamplesIterator = pSamples->begin(); |
1137 |
return (SamplesIterator != pSamples->end()) ? *SamplesIterator : NULL; |
1138 |
} |
1139 |
|
1140 |
Sample* File::GetNextSample() { |
1141 |
if (!pSamples) return NULL; |
1142 |
SamplesIterator++; |
1143 |
return (SamplesIterator != pSamples->end()) ? *SamplesIterator : NULL; |
1144 |
} |
1145 |
|
1146 |
void File::LoadSamples() { |
1147 |
if (!pSamples) pSamples = new SampleList; |
1148 |
RIFF::List* wvpl = pRIFF->GetSubList(LIST_TYPE_WVPL); |
1149 |
if (wvpl) { |
1150 |
unsigned long wvplFileOffset = wvpl->GetFilePos(); |
1151 |
RIFF::List* wave = wvpl->GetFirstSubList(); |
1152 |
while (wave) { |
1153 |
if (wave->GetListType() == LIST_TYPE_WAVE) { |
1154 |
unsigned long waveFileOffset = wave->GetFilePos(); |
1155 |
pSamples->push_back(new Sample(this, wave, waveFileOffset - wvplFileOffset)); |
1156 |
} |
1157 |
wave = wvpl->GetNextSubList(); |
1158 |
} |
1159 |
} |
1160 |
else { // Seen a dwpl list chunk instead of a wvpl list chunk in some file (officially not DLS compliant) |
1161 |
RIFF::List* dwpl = pRIFF->GetSubList(LIST_TYPE_DWPL); |
1162 |
if (dwpl) { |
1163 |
unsigned long dwplFileOffset = dwpl->GetFilePos(); |
1164 |
RIFF::List* wave = dwpl->GetFirstSubList(); |
1165 |
while (wave) { |
1166 |
if (wave->GetListType() == LIST_TYPE_WAVE) { |
1167 |
unsigned long waveFileOffset = wave->GetFilePos(); |
1168 |
pSamples->push_back(new Sample(this, wave, waveFileOffset - dwplFileOffset)); |
1169 |
} |
1170 |
wave = dwpl->GetNextSubList(); |
1171 |
} |
1172 |
} |
1173 |
} |
1174 |
} |
1175 |
|
1176 |
/** @brief Add a new sample. |
1177 |
* |
1178 |
* This will create a new Sample object for the DLS file. You have to |
1179 |
* call Save() to make this persistent to the file. |
1180 |
* |
1181 |
* @returns pointer to new Sample object |
1182 |
*/ |
1183 |
Sample* File::AddSample() { |
1184 |
if (!pSamples) LoadSamples(); |
1185 |
__ensureMandatoryChunksExist(); |
1186 |
RIFF::List* wvpl = pRIFF->GetSubList(LIST_TYPE_WVPL); |
1187 |
// create new Sample object and its respective 'wave' list chunk |
1188 |
RIFF::List* wave = wvpl->AddSubList(LIST_TYPE_WAVE); |
1189 |
Sample* pSample = new Sample(this, wave, 0 /*arbitrary value, we update offsets when we save*/); |
1190 |
pSamples->push_back(pSample); |
1191 |
return pSample; |
1192 |
} |
1193 |
|
1194 |
/** @brief Delete a sample. |
1195 |
* |
1196 |
* This will delete the given Sample object from the DLS file. You have |
1197 |
* to call Save() to make this persistent to the file. |
1198 |
* |
1199 |
* @param pSample - sample to delete |
1200 |
*/ |
1201 |
void File::DeleteSample(Sample* pSample) { |
1202 |
if (!pSamples) return; |
1203 |
SampleList::iterator iter = find(pSamples->begin(), pSamples->end(), pSample); |
1204 |
if (iter == pSamples->end()) return; |
1205 |
pSamples->erase(iter); |
1206 |
delete pSample; |
1207 |
} |
1208 |
|
1209 |
Instrument* File::GetFirstInstrument() { |
1210 |
if (!pInstruments) LoadInstruments(); |
1211 |
if (!pInstruments) return NULL; |
1212 |
InstrumentsIterator = pInstruments->begin(); |
1213 |
return (InstrumentsIterator != pInstruments->end()) ? *InstrumentsIterator : NULL; |
1214 |
} |
1215 |
|
1216 |
Instrument* File::GetNextInstrument() { |
1217 |
if (!pInstruments) return NULL; |
1218 |
InstrumentsIterator++; |
1219 |
return (InstrumentsIterator != pInstruments->end()) ? *InstrumentsIterator : NULL; |
1220 |
} |
1221 |
|
1222 |
void File::LoadInstruments() { |
1223 |
if (!pInstruments) pInstruments = new InstrumentList; |
1224 |
RIFF::List* lstInstruments = pRIFF->GetSubList(LIST_TYPE_LINS); |
1225 |
if (lstInstruments) { |
1226 |
RIFF::List* lstInstr = lstInstruments->GetFirstSubList(); |
1227 |
while (lstInstr) { |
1228 |
if (lstInstr->GetListType() == LIST_TYPE_INS) { |
1229 |
pInstruments->push_back(new Instrument(this, lstInstr)); |
1230 |
} |
1231 |
lstInstr = lstInstruments->GetNextSubList(); |
1232 |
} |
1233 |
} |
1234 |
} |
1235 |
|
1236 |
/** @brief Add a new instrument definition. |
1237 |
* |
1238 |
* This will create a new Instrument object for the DLS file. You have |
1239 |
* to call Save() to make this persistent to the file. |
1240 |
* |
1241 |
* @returns pointer to new Instrument object |
1242 |
*/ |
1243 |
Instrument* File::AddInstrument() { |
1244 |
if (!pInstruments) LoadInstruments(); |
1245 |
__ensureMandatoryChunksExist(); |
1246 |
RIFF::List* lstInstruments = pRIFF->GetSubList(LIST_TYPE_LINS); |
1247 |
RIFF::List* lstInstr = lstInstruments->AddSubList(LIST_TYPE_INS); |
1248 |
Instrument* pInstrument = new Instrument(this, lstInstr); |
1249 |
pInstruments->push_back(pInstrument); |
1250 |
return pInstrument; |
1251 |
} |
1252 |
|
1253 |
/** @brief Delete an instrument. |
1254 |
* |
1255 |
* This will delete the given Instrument object from the DLS file. You |
1256 |
* have to call Save() to make this persistent to the file. |
1257 |
* |
1258 |
* @param pInstrument - instrument to delete |
1259 |
*/ |
1260 |
void File::DeleteInstrument(Instrument* pInstrument) { |
1261 |
if (!pInstruments) return; |
1262 |
InstrumentList::iterator iter = find(pInstruments->begin(), pInstruments->end(), pInstrument); |
1263 |
if (iter == pInstruments->end()) return; |
1264 |
pInstruments->erase(iter); |
1265 |
delete pInstrument; |
1266 |
} |
1267 |
|
1268 |
/** |
1269 |
* Apply all the DLS file's current instruments, samples and settings to |
1270 |
* the respective RIFF chunks. You have to call Save() to make changes |
1271 |
* persistent. |
1272 |
* |
1273 |
* @throws Exception - on errors |
1274 |
*/ |
1275 |
void File::UpdateChunks() { |
1276 |
// first update base class's chunks |
1277 |
Resource::UpdateChunks(); |
1278 |
|
1279 |
// if version struct exists, update 'vers' chunk |
1280 |
if (pVersion) { |
1281 |
RIFF::Chunk* ckVersion = pRIFF->GetSubChunk(CHUNK_ID_VERS); |
1282 |
if (!ckVersion) ckVersion = pRIFF->AddSubChunk(CHUNK_ID_VERS, 8); |
1283 |
uint8_t* pData = (uint8_t*) ckVersion->LoadChunkData(); |
1284 |
memcpy(pData, pVersion, 2 * 4); |
1285 |
} |
1286 |
|
1287 |
// update 'colh' chunk |
1288 |
Instruments = (pInstruments) ? pInstruments->size() : 0; |
1289 |
RIFF::Chunk* colh = pRIFF->GetSubChunk(CHUNK_ID_COLH); |
1290 |
if (!colh) colh = pRIFF->AddSubChunk(CHUNK_ID_COLH, 4); |
1291 |
uint8_t* pData = (uint8_t*) colh->LoadChunkData(); |
1292 |
memcpy(pData, &Instruments, 4); |
1293 |
|
1294 |
// update instrument's chunks |
1295 |
if (pInstruments) { |
1296 |
InstrumentList::iterator iter = pInstruments->begin(); |
1297 |
InstrumentList::iterator end = pInstruments->end(); |
1298 |
for (; iter != end; ++iter) { |
1299 |
(*iter)->UpdateChunks(); |
1300 |
} |
1301 |
} |
1302 |
|
1303 |
// update 'ptbl' chunk |
1304 |
const int iSamples = (pSamples) ? pSamples->size() : 0; |
1305 |
const int iPtblOffsetSize = (b64BitWavePoolOffsets) ? 8 : 4; |
1306 |
RIFF::Chunk* ptbl = pRIFF->GetSubChunk(CHUNK_ID_PTBL); |
1307 |
if (!ptbl) ptbl = pRIFF->AddSubChunk(CHUNK_ID_PTBL, 1 /*anything, we'll resize*/); |
1308 |
const int iPtblSize = WavePoolHeaderSize + iPtblOffsetSize * iSamples; |
1309 |
ptbl->Resize(iPtblSize); |
1310 |
pData = (uint8_t*) ptbl->LoadChunkData(); |
1311 |
WavePoolCount = iSamples; |
1312 |
memcpy(&pData[4], &WavePoolCount, 4); |
1313 |
// we actually update the sample offsets in the pool table when we Save() |
1314 |
memset(&pData[WavePoolHeaderSize], 0, iPtblSize - WavePoolHeaderSize); |
1315 |
|
1316 |
// update sample's chunks |
1317 |
if (pSamples) { |
1318 |
SampleList::iterator iter = pSamples->begin(); |
1319 |
SampleList::iterator end = pSamples->end(); |
1320 |
for (; iter != end; ++iter) { |
1321 |
(*iter)->UpdateChunks(); |
1322 |
} |
1323 |
} |
1324 |
} |
1325 |
|
1326 |
/** @brief Save changes to another file. |
1327 |
* |
1328 |
* Make all changes persistent by writing them to another file. |
1329 |
* <b>Caution:</b> this method is optimized for writing to |
1330 |
* <b>another</b> file, do not use it to save the changes to the same |
1331 |
* file! Use Save() (without path argument) in that case instead! |
1332 |
* Ignoring this might result in a corrupted file! |
1333 |
* |
1334 |
* After calling this method, this File object will be associated with |
1335 |
* the new file (given by \a Path) afterwards. |
1336 |
* |
1337 |
* @param Path - path and file name where everything should be written to |
1338 |
*/ |
1339 |
void File::Save(const String& Path) { |
1340 |
UpdateChunks(); |
1341 |
pRIFF->Save(Path); |
1342 |
__UpdateWavePoolTableChunk(); |
1343 |
} |
1344 |
|
1345 |
/** @brief Save changes to same file. |
1346 |
* |
1347 |
* Make all changes persistent by writing them to the actual (same) |
1348 |
* file. The file might temporarily grow to a higher size than it will |
1349 |
* have at the end of the saving process. |
1350 |
* |
1351 |
* @throws RIFF::Exception if any kind of IO error occured |
1352 |
* @throws DLS::Exception if any kind of DLS specific error occured |
1353 |
*/ |
1354 |
void File::Save() { |
1355 |
UpdateChunks(); |
1356 |
pRIFF->Save(); |
1357 |
__UpdateWavePoolTableChunk(); |
1358 |
} |
1359 |
|
1360 |
/** |
1361 |
* Checks if all (for DLS) mandatory chunks exist, if not they will be |
1362 |
* created. Note that those chunks will not be made persistent until |
1363 |
* Save() was called. |
1364 |
*/ |
1365 |
void File::__ensureMandatoryChunksExist() { |
1366 |
// enusre 'lins' list chunk exists (mandatory for instrument definitions) |
1367 |
RIFF::List* lstInstruments = pRIFF->GetSubList(LIST_TYPE_LINS); |
1368 |
if (!lstInstruments) pRIFF->AddSubList(LIST_TYPE_LINS); |
1369 |
// ensure 'ptbl' chunk exists (mandatory for samples) |
1370 |
RIFF::Chunk* ptbl = pRIFF->GetSubChunk(CHUNK_ID_PTBL); |
1371 |
if (!ptbl) { |
1372 |
const int iOffsetSize = (b64BitWavePoolOffsets) ? 8 : 4; |
1373 |
ptbl = pRIFF->AddSubChunk(CHUNK_ID_PTBL, WavePoolHeaderSize + iOffsetSize); |
1374 |
} |
1375 |
// enusre 'wvpl' list chunk exists (mandatory for samples) |
1376 |
RIFF::List* wvpl = pRIFF->GetSubList(LIST_TYPE_WVPL); |
1377 |
if (!wvpl) pRIFF->AddSubList(LIST_TYPE_WVPL); |
1378 |
} |
1379 |
|
1380 |
/** |
1381 |
* Updates (persistently) the wave pool table with offsets to all |
1382 |
* currently available samples. <b>Caution:</b> this method assumes the |
1383 |
* 'ptbl' chunk to be already of the correct size and the file to be |
1384 |
* writable, so usually this method is only called after a Save() call. |
1385 |
* |
1386 |
* @throws Exception - if 'ptbl' chunk is too small (should only occur |
1387 |
* if there's a bug) |
1388 |
*/ |
1389 |
void File::__UpdateWavePoolTableChunk() { |
1390 |
__UpdateWavePoolTable(); |
1391 |
RIFF::Chunk* ptbl = pRIFF->GetSubChunk(CHUNK_ID_PTBL); |
1392 |
const int iOffsetSize = (b64BitWavePoolOffsets) ? 8 : 4; |
1393 |
// check if 'ptbl' chunk is large enough |
1394 |
WavePoolCount = (pSamples) ? pSamples->size() : 0; |
1395 |
const unsigned long ulRequiredSize = WavePoolHeaderSize + iOffsetSize * WavePoolCount; |
1396 |
if (ptbl->GetSize() < ulRequiredSize) throw Exception("Fatal error, 'ptbl' chunk too small"); |
1397 |
// save the 'ptbl' chunk's current read/write position |
1398 |
unsigned long ulOriginalPos = ptbl->GetPos(); |
1399 |
// update headers |
1400 |
ptbl->SetPos(0); |
1401 |
ptbl->WriteUint32(&WavePoolHeaderSize); |
1402 |
ptbl->WriteUint32(&WavePoolCount); |
1403 |
// update offsets |
1404 |
ptbl->SetPos(WavePoolHeaderSize); |
1405 |
if (b64BitWavePoolOffsets) { |
1406 |
for (int i = 0 ; i < WavePoolCount ; i++) { |
1407 |
ptbl->WriteUint32(&pWavePoolTableHi[i]); |
1408 |
ptbl->WriteUint32(&pWavePoolTable[i]); |
1409 |
} |
1410 |
} else { // conventional 32 bit offsets |
1411 |
for (int i = 0 ; i < WavePoolCount ; i++) |
1412 |
ptbl->WriteUint32(&pWavePoolTable[i]); |
1413 |
} |
1414 |
// restore 'ptbl' chunk's original read/write position |
1415 |
ptbl->SetPos(ulOriginalPos); |
1416 |
} |
1417 |
|
1418 |
/** |
1419 |
* Updates the wave pool table with offsets to all currently available |
1420 |
* samples. <b>Caution:</b> this method assumes the 'wvpl' list chunk |
1421 |
* exists already. |
1422 |
*/ |
1423 |
void File::__UpdateWavePoolTable() { |
1424 |
WavePoolCount = (pSamples) ? pSamples->size() : 0; |
1425 |
// resize wave pool table arrays |
1426 |
if (pWavePoolTable) delete[] pWavePoolTable; |
1427 |
if (pWavePoolTableHi) delete[] pWavePoolTableHi; |
1428 |
pWavePoolTable = new uint32_t[WavePoolCount]; |
1429 |
pWavePoolTableHi = new uint32_t[WavePoolCount]; |
1430 |
if (!pSamples) return; |
1431 |
// update offsets int wave pool table |
1432 |
RIFF::List* wvpl = pRIFF->GetSubList(LIST_TYPE_WVPL); |
1433 |
uint64_t wvplFileOffset = wvpl->GetFilePos(); |
1434 |
if (b64BitWavePoolOffsets) { |
1435 |
SampleList::iterator iter = pSamples->begin(); |
1436 |
SampleList::iterator end = pSamples->end(); |
1437 |
for (int i = 0 ; iter != end ; ++iter, i++) { |
1438 |
uint64_t _64BitOffset = (*iter)->pWaveList->GetFilePos() - wvplFileOffset - LIST_HEADER_SIZE; |
1439 |
(*iter)->ulWavePoolOffset = _64BitOffset; |
1440 |
pWavePoolTableHi[i] = (uint32_t) (_64BitOffset >> 32); |
1441 |
pWavePoolTable[i] = (uint32_t) _64BitOffset; |
1442 |
} |
1443 |
} else { // conventional 32 bit offsets |
1444 |
SampleList::iterator iter = pSamples->begin(); |
1445 |
SampleList::iterator end = pSamples->end(); |
1446 |
for (int i = 0 ; iter != end ; ++iter, i++) { |
1447 |
uint64_t _64BitOffset = (*iter)->pWaveList->GetFilePos() - wvplFileOffset - LIST_HEADER_SIZE; |
1448 |
(*iter)->ulWavePoolOffset = _64BitOffset; |
1449 |
pWavePoolTable[i] = (uint32_t) _64BitOffset; |
1450 |
} |
1451 |
} |
1452 |
} |
1453 |
|
1454 |
|
1455 |
|
1456 |
// *************** Exception *************** |
1457 |
// * |
1458 |
|
1459 |
Exception::Exception(String Message) : RIFF::Exception(Message) { |
1460 |
} |
1461 |
|
1462 |
void Exception::PrintMessage() { |
1463 |
std::cout << "DLS::Exception: " << Message << std::endl; |
1464 |
} |
1465 |
|
1466 |
|
1467 |
// *************** functions *************** |
1468 |
// * |
1469 |
|
1470 |
/** |
1471 |
* Returns the name of this C++ library. This is usually "libgig" of |
1472 |
* course. This call is equivalent to RIFF::libraryName() and |
1473 |
* gig::libraryName(). |
1474 |
*/ |
1475 |
String libraryName() { |
1476 |
return PACKAGE; |
1477 |
} |
1478 |
|
1479 |
/** |
1480 |
* Returns version of this C++ library. This call is equivalent to |
1481 |
* RIFF::libraryVersion() and gig::libraryVersion(). |
1482 |
*/ |
1483 |
String libraryVersion() { |
1484 |
return VERSION; |
1485 |
} |
1486 |
|
1487 |
} // namespace DLS |