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