1 |
schoenebeck |
2 |
/*************************************************************************** |
2 |
|
|
* * |
3 |
schoenebeck |
933 |
* libgig - C++ cross-platform Gigasampler format file access library * |
4 |
schoenebeck |
2 |
* * |
5 |
schoenebeck |
3922 |
* Copyright (C) 2003-2021 by Christian Schoenebeck * |
6 |
schoenebeck |
384 |
* <cuse@users.sourceforge.net> * |
7 |
schoenebeck |
2 |
* * |
8 |
|
|
* This library is free software; you can redistribute it and/or modify * |
9 |
|
|
* it under the terms of the GNU General Public License as published by * |
10 |
|
|
* the Free Software Foundation; either version 2 of the License, or * |
11 |
|
|
* (at your option) any later version. * |
12 |
|
|
* * |
13 |
|
|
* This library is distributed in the hope that it will be useful, * |
14 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
15 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
16 |
|
|
* GNU General Public License for more details. * |
17 |
|
|
* * |
18 |
|
|
* You should have received a copy of the GNU General Public License * |
19 |
|
|
* along with this library; if not, write to the Free Software * |
20 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, * |
21 |
|
|
* MA 02111-1307 USA * |
22 |
|
|
***************************************************************************/ |
23 |
|
|
|
24 |
|
|
#include "DLS.h" |
25 |
|
|
|
26 |
persson |
1713 |
#include <algorithm> |
27 |
schoenebeck |
3474 |
#include <vector> |
28 |
schoenebeck |
800 |
#include <time.h> |
29 |
|
|
|
30 |
persson |
1209 |
#ifdef __APPLE__ |
31 |
|
|
#include <CoreFoundation/CFUUID.h> |
32 |
|
|
#elif defined(HAVE_UUID_UUID_H) |
33 |
|
|
#include <uuid/uuid.h> |
34 |
|
|
#endif |
35 |
|
|
|
36 |
schoenebeck |
800 |
#include "helper.h" |
37 |
|
|
|
38 |
|
|
// macros to decode connection transforms |
39 |
|
|
#define CONN_TRANSFORM_SRC(x) ((x >> 10) & 0x000F) |
40 |
|
|
#define CONN_TRANSFORM_CTL(x) ((x >> 4) & 0x000F) |
41 |
|
|
#define CONN_TRANSFORM_DST(x) (x & 0x000F) |
42 |
|
|
#define CONN_TRANSFORM_BIPOLAR_SRC(x) (x & 0x4000) |
43 |
|
|
#define CONN_TRANSFORM_BIPOLAR_CTL(x) (x & 0x0100) |
44 |
|
|
#define CONN_TRANSFORM_INVERT_SRC(x) (x & 0x8000) |
45 |
|
|
#define CONN_TRANSFORM_INVERT_CTL(x) (x & 0x0200) |
46 |
|
|
|
47 |
|
|
// macros to encode connection transforms |
48 |
|
|
#define CONN_TRANSFORM_SRC_ENCODE(x) ((x & 0x000F) << 10) |
49 |
|
|
#define CONN_TRANSFORM_CTL_ENCODE(x) ((x & 0x000F) << 4) |
50 |
|
|
#define CONN_TRANSFORM_DST_ENCODE(x) (x & 0x000F) |
51 |
|
|
#define CONN_TRANSFORM_BIPOLAR_SRC_ENCODE(x) ((x) ? 0x4000 : 0) |
52 |
|
|
#define CONN_TRANSFORM_BIPOLAR_CTL_ENCODE(x) ((x) ? 0x0100 : 0) |
53 |
|
|
#define CONN_TRANSFORM_INVERT_SRC_ENCODE(x) ((x) ? 0x8000 : 0) |
54 |
|
|
#define CONN_TRANSFORM_INVERT_CTL_ENCODE(x) ((x) ? 0x0200 : 0) |
55 |
|
|
|
56 |
persson |
918 |
#define DRUM_TYPE_MASK 0x80000000 |
57 |
schoenebeck |
800 |
|
58 |
|
|
#define F_RGN_OPTION_SELFNONEXCLUSIVE 0x0001 |
59 |
|
|
|
60 |
|
|
#define F_WAVELINK_PHASE_MASTER 0x0001 |
61 |
|
|
#define F_WAVELINK_MULTICHANNEL 0x0002 |
62 |
|
|
|
63 |
|
|
#define F_WSMP_NO_TRUNCATION 0x0001 |
64 |
|
|
#define F_WSMP_NO_COMPRESSION 0x0002 |
65 |
|
|
|
66 |
|
|
#define MIDI_BANK_COARSE(x) ((x & 0x00007F00) >> 8) // CC0 |
67 |
|
|
#define MIDI_BANK_FINE(x) (x & 0x0000007F) // CC32 |
68 |
|
|
#define MIDI_BANK_MERGE(coarse, fine) ((((uint16_t) coarse) << 7) | fine) // CC0 + CC32 |
69 |
|
|
#define MIDI_BANK_ENCODE(coarse, fine) (((coarse & 0x0000007F) << 8) | (fine & 0x0000007F)) |
70 |
|
|
|
71 |
schoenebeck |
2 |
namespace DLS { |
72 |
|
|
|
73 |
|
|
// *************** Connection *************** |
74 |
|
|
// * |
75 |
|
|
|
76 |
|
|
void Connection::Init(conn_block_t* Header) { |
77 |
|
|
Source = (conn_src_t) Header->source; |
78 |
|
|
Control = (conn_src_t) Header->control; |
79 |
|
|
Destination = (conn_dst_t) Header->destination; |
80 |
|
|
Scale = Header->scale; |
81 |
|
|
SourceTransform = (conn_trn_t) CONN_TRANSFORM_SRC(Header->transform); |
82 |
|
|
ControlTransform = (conn_trn_t) CONN_TRANSFORM_CTL(Header->transform); |
83 |
|
|
DestinationTransform = (conn_trn_t) CONN_TRANSFORM_DST(Header->transform); |
84 |
|
|
SourceInvert = CONN_TRANSFORM_INVERT_SRC(Header->transform); |
85 |
|
|
SourceBipolar = CONN_TRANSFORM_BIPOLAR_SRC(Header->transform); |
86 |
|
|
ControlInvert = CONN_TRANSFORM_INVERT_CTL(Header->transform); |
87 |
|
|
ControlBipolar = CONN_TRANSFORM_BIPOLAR_CTL(Header->transform); |
88 |
|
|
} |
89 |
|
|
|
90 |
schoenebeck |
800 |
Connection::conn_block_t Connection::ToConnBlock() { |
91 |
|
|
conn_block_t c; |
92 |
|
|
c.source = Source; |
93 |
|
|
c.control = Control; |
94 |
|
|
c.destination = Destination; |
95 |
|
|
c.scale = Scale; |
96 |
|
|
c.transform = CONN_TRANSFORM_SRC_ENCODE(SourceTransform) | |
97 |
|
|
CONN_TRANSFORM_CTL_ENCODE(ControlTransform) | |
98 |
|
|
CONN_TRANSFORM_DST_ENCODE(DestinationTransform) | |
99 |
|
|
CONN_TRANSFORM_INVERT_SRC_ENCODE(SourceInvert) | |
100 |
|
|
CONN_TRANSFORM_BIPOLAR_SRC_ENCODE(SourceBipolar) | |
101 |
|
|
CONN_TRANSFORM_INVERT_CTL_ENCODE(ControlInvert) | |
102 |
|
|
CONN_TRANSFORM_BIPOLAR_CTL_ENCODE(ControlBipolar); |
103 |
|
|
return c; |
104 |
|
|
} |
105 |
schoenebeck |
2 |
|
106 |
|
|
|
107 |
schoenebeck |
800 |
|
108 |
schoenebeck |
2 |
// *************** Articulation *************** |
109 |
|
|
// * |
110 |
|
|
|
111 |
schoenebeck |
800 |
/** @brief Constructor. |
112 |
|
|
* |
113 |
|
|
* Expects an 'artl' or 'art2' chunk to be given where the articulation |
114 |
|
|
* connections will be read from. |
115 |
|
|
* |
116 |
|
|
* @param artl - pointer to an 'artl' or 'art2' chunk |
117 |
|
|
* @throws Exception if no 'artl' or 'art2' chunk was given |
118 |
|
|
*/ |
119 |
|
|
Articulation::Articulation(RIFF::Chunk* artl) { |
120 |
|
|
pArticulationCk = artl; |
121 |
|
|
if (artl->GetChunkID() != CHUNK_ID_ART2 && |
122 |
|
|
artl->GetChunkID() != CHUNK_ID_ARTL) { |
123 |
|
|
throw DLS::Exception("<artl-ck> or <art2-ck> chunk expected"); |
124 |
schoenebeck |
2 |
} |
125 |
schoenebeck |
3478 |
|
126 |
|
|
artl->SetPos(0); |
127 |
|
|
|
128 |
schoenebeck |
800 |
HeaderSize = artl->ReadUint32(); |
129 |
|
|
Connections = artl->ReadUint32(); |
130 |
|
|
artl->SetPos(HeaderSize); |
131 |
schoenebeck |
2 |
|
132 |
|
|
pConnections = new Connection[Connections]; |
133 |
|
|
Connection::conn_block_t connblock; |
134 |
schoenebeck |
800 |
for (uint32_t i = 0; i < Connections; i++) { |
135 |
|
|
artl->Read(&connblock.source, 1, 2); |
136 |
|
|
artl->Read(&connblock.control, 1, 2); |
137 |
|
|
artl->Read(&connblock.destination, 1, 2); |
138 |
|
|
artl->Read(&connblock.transform, 1, 2); |
139 |
|
|
artl->Read(&connblock.scale, 1, 4); |
140 |
schoenebeck |
2 |
pConnections[i].Init(&connblock); |
141 |
|
|
} |
142 |
|
|
} |
143 |
|
|
|
144 |
|
|
Articulation::~Articulation() { |
145 |
|
|
if (pConnections) delete[] pConnections; |
146 |
|
|
} |
147 |
|
|
|
148 |
schoenebeck |
800 |
/** |
149 |
|
|
* Apply articulation connections to the respective RIFF chunks. You |
150 |
|
|
* have to call File::Save() to make changes persistent. |
151 |
schoenebeck |
2682 |
* |
152 |
|
|
* @param pProgress - callback function for progress notification |
153 |
schoenebeck |
800 |
*/ |
154 |
schoenebeck |
2682 |
void Articulation::UpdateChunks(progress_t* pProgress) { |
155 |
schoenebeck |
800 |
const int iEntrySize = 12; // 12 bytes per connection block |
156 |
|
|
pArticulationCk->Resize(HeaderSize + Connections * iEntrySize); |
157 |
|
|
uint8_t* pData = (uint8_t*) pArticulationCk->LoadChunkData(); |
158 |
persson |
1179 |
store16(&pData[0], HeaderSize); |
159 |
|
|
store16(&pData[2], Connections); |
160 |
schoenebeck |
800 |
for (uint32_t i = 0; i < Connections; i++) { |
161 |
|
|
Connection::conn_block_t c = pConnections[i].ToConnBlock(); |
162 |
persson |
1179 |
store16(&pData[HeaderSize + i * iEntrySize], c.source); |
163 |
|
|
store16(&pData[HeaderSize + i * iEntrySize + 2], c.control); |
164 |
|
|
store16(&pData[HeaderSize + i * iEntrySize + 4], c.destination); |
165 |
|
|
store16(&pData[HeaderSize + i * iEntrySize + 6], c.transform); |
166 |
|
|
store32(&pData[HeaderSize + i * iEntrySize + 8], c.scale); |
167 |
schoenebeck |
800 |
} |
168 |
|
|
} |
169 |
schoenebeck |
2 |
|
170 |
schoenebeck |
3478 |
/** @brief Remove all RIFF chunks associated with this Articulation object. |
171 |
|
|
* |
172 |
|
|
* At the moment Articulation::DeleteChunks() does nothing. It is |
173 |
|
|
* recommended to call this method explicitly though from deriving classes's |
174 |
|
|
* own overridden implementation of this method to avoid potential future |
175 |
|
|
* compatiblity issues. |
176 |
|
|
* |
177 |
|
|
* See Storage::DeleteChunks() for details. |
178 |
|
|
*/ |
179 |
|
|
void Articulation::DeleteChunks() { |
180 |
|
|
} |
181 |
schoenebeck |
2 |
|
182 |
schoenebeck |
800 |
|
183 |
schoenebeck |
3478 |
|
184 |
schoenebeck |
2 |
// *************** Articulator *************** |
185 |
|
|
// * |
186 |
|
|
|
187 |
|
|
Articulator::Articulator(RIFF::List* ParentList) { |
188 |
|
|
pParentList = ParentList; |
189 |
|
|
pArticulations = NULL; |
190 |
|
|
} |
191 |
|
|
|
192 |
schoenebeck |
3940 |
/** |
193 |
|
|
* Returns Articulation at supplied @a pos position within the articulation |
194 |
|
|
* list. If supplied @a pos is out of bounds then @c NULL is returned. |
195 |
|
|
* |
196 |
|
|
* @param pos - position of sought Articulation in articulation list |
197 |
|
|
* @returns pointer address to requested articulation or @c NULL if @a pos |
198 |
|
|
* is out of bounds |
199 |
|
|
*/ |
200 |
|
|
Articulation* Articulator::GetArticulation(size_t pos) { |
201 |
|
|
if (!pArticulations) LoadArticulations(); |
202 |
|
|
if (!pArticulations) return NULL; |
203 |
|
|
if (pos >= pArticulations->size()) return NULL; |
204 |
|
|
return (*pArticulations)[pos]; |
205 |
|
|
} |
206 |
|
|
|
207 |
|
|
/** |
208 |
|
|
* Returns the first Articulation in the list of articulations. You have to |
209 |
|
|
* call this method once before you can use GetNextArticulation(). |
210 |
|
|
* |
211 |
|
|
* @returns pointer address to first Articulation or NULL if there is none |
212 |
|
|
* @see GetNextArticulation() |
213 |
|
|
* @deprecated This method is not reentrant-safe, use GetArticulation() |
214 |
|
|
* instead. |
215 |
|
|
*/ |
216 |
schoenebeck |
2 |
Articulation* Articulator::GetFirstArticulation() { |
217 |
|
|
if (!pArticulations) LoadArticulations(); |
218 |
|
|
if (!pArticulations) return NULL; |
219 |
|
|
ArticulationsIterator = pArticulations->begin(); |
220 |
|
|
return (ArticulationsIterator != pArticulations->end()) ? *ArticulationsIterator : NULL; |
221 |
|
|
} |
222 |
|
|
|
223 |
schoenebeck |
3940 |
/** |
224 |
|
|
* Returns the next Articulation from the list of articulations. You have |
225 |
|
|
* to call GetFirstArticulation() once before you can use this method. By |
226 |
|
|
* calling this method multiple times it iterates through the available |
227 |
|
|
* articulations. |
228 |
|
|
* |
229 |
|
|
* @returns pointer address to the next Articulation or NULL if end reached |
230 |
|
|
* @see GetFirstArticulation() |
231 |
|
|
* @deprecated This method is not reentrant-safe, use GetArticulation() |
232 |
|
|
* instead. |
233 |
|
|
*/ |
234 |
schoenebeck |
2 |
Articulation* Articulator::GetNextArticulation() { |
235 |
|
|
if (!pArticulations) return NULL; |
236 |
|
|
ArticulationsIterator++; |
237 |
|
|
return (ArticulationsIterator != pArticulations->end()) ? *ArticulationsIterator : NULL; |
238 |
|
|
} |
239 |
|
|
|
240 |
|
|
void Articulator::LoadArticulations() { |
241 |
|
|
// prefer articulation level 2 |
242 |
|
|
RIFF::List* lart = pParentList->GetSubList(LIST_TYPE_LAR2); |
243 |
|
|
if (!lart) lart = pParentList->GetSubList(LIST_TYPE_LART); |
244 |
|
|
if (lart) { |
245 |
schoenebeck |
800 |
uint32_t artCkType = (lart->GetListType() == LIST_TYPE_LAR2) ? CHUNK_ID_ART2 |
246 |
|
|
: CHUNK_ID_ARTL; |
247 |
schoenebeck |
3922 |
size_t i = 0; |
248 |
|
|
for (RIFF::Chunk* art = lart->GetSubChunkAt(i); art; |
249 |
|
|
art = lart->GetSubChunkAt(++i)) |
250 |
|
|
{ |
251 |
schoenebeck |
800 |
if (art->GetChunkID() == artCkType) { |
252 |
schoenebeck |
2 |
if (!pArticulations) pArticulations = new ArticulationList; |
253 |
|
|
pArticulations->push_back(new Articulation(art)); |
254 |
|
|
} |
255 |
|
|
} |
256 |
|
|
} |
257 |
|
|
} |
258 |
|
|
|
259 |
|
|
Articulator::~Articulator() { |
260 |
|
|
if (pArticulations) { |
261 |
|
|
ArticulationList::iterator iter = pArticulations->begin(); |
262 |
|
|
ArticulationList::iterator end = pArticulations->end(); |
263 |
|
|
while (iter != end) { |
264 |
|
|
delete *iter; |
265 |
|
|
iter++; |
266 |
|
|
} |
267 |
|
|
delete pArticulations; |
268 |
|
|
} |
269 |
|
|
} |
270 |
|
|
|
271 |
schoenebeck |
800 |
/** |
272 |
|
|
* Apply all articulations to the respective RIFF chunks. You have to |
273 |
|
|
* call File::Save() to make changes persistent. |
274 |
schoenebeck |
2682 |
* |
275 |
|
|
* @param pProgress - callback function for progress notification |
276 |
schoenebeck |
800 |
*/ |
277 |
schoenebeck |
2682 |
void Articulator::UpdateChunks(progress_t* pProgress) { |
278 |
schoenebeck |
804 |
if (pArticulations) { |
279 |
|
|
ArticulationList::iterator iter = pArticulations->begin(); |
280 |
|
|
ArticulationList::iterator end = pArticulations->end(); |
281 |
|
|
for (; iter != end; ++iter) { |
282 |
schoenebeck |
2682 |
(*iter)->UpdateChunks(pProgress); |
283 |
schoenebeck |
804 |
} |
284 |
schoenebeck |
800 |
} |
285 |
|
|
} |
286 |
schoenebeck |
3478 |
|
287 |
|
|
/** @brief Remove all RIFF chunks associated with this Articulator object. |
288 |
|
|
* |
289 |
|
|
* See Storage::DeleteChunks() for details. |
290 |
|
|
*/ |
291 |
|
|
void Articulator::DeleteChunks() { |
292 |
|
|
if (pArticulations) { |
293 |
|
|
ArticulationList::iterator iter = pArticulations->begin(); |
294 |
|
|
ArticulationList::iterator end = pArticulations->end(); |
295 |
|
|
for (; iter != end; ++iter) { |
296 |
|
|
(*iter)->DeleteChunks(); |
297 |
|
|
} |
298 |
|
|
} |
299 |
|
|
} |
300 |
|
|
|
301 |
schoenebeck |
2394 |
/** |
302 |
|
|
* Not yet implemented in this version, since the .gig format does |
303 |
|
|
* not need to copy DLS articulators and so far nobody used pure |
304 |
|
|
* DLS instrument AFAIK. |
305 |
|
|
*/ |
306 |
|
|
void Articulator::CopyAssign(const Articulator* orig) { |
307 |
|
|
//TODO: implement deep copy assignment for this class |
308 |
|
|
} |
309 |
schoenebeck |
2 |
|
310 |
|
|
|
311 |
schoenebeck |
800 |
|
312 |
schoenebeck |
2 |
// *************** Info *************** |
313 |
|
|
// * |
314 |
|
|
|
315 |
schoenebeck |
800 |
/** @brief Constructor. |
316 |
|
|
* |
317 |
schoenebeck |
929 |
* Initializes the info strings with values provided by an INFO list chunk. |
318 |
schoenebeck |
800 |
* |
319 |
schoenebeck |
929 |
* @param list - pointer to a list chunk which contains an INFO list chunk |
320 |
schoenebeck |
800 |
*/ |
321 |
schoenebeck |
2 |
Info::Info(RIFF::List* list) { |
322 |
schoenebeck |
1416 |
pFixedStringLengths = NULL; |
323 |
schoenebeck |
800 |
pResourceListChunk = list; |
324 |
schoenebeck |
2 |
if (list) { |
325 |
|
|
RIFF::List* lstINFO = list->GetSubList(LIST_TYPE_INFO); |
326 |
|
|
if (lstINFO) { |
327 |
|
|
LoadString(CHUNK_ID_INAM, lstINFO, Name); |
328 |
|
|
LoadString(CHUNK_ID_IARL, lstINFO, ArchivalLocation); |
329 |
|
|
LoadString(CHUNK_ID_ICRD, lstINFO, CreationDate); |
330 |
|
|
LoadString(CHUNK_ID_ICMT, lstINFO, Comments); |
331 |
|
|
LoadString(CHUNK_ID_IPRD, lstINFO, Product); |
332 |
|
|
LoadString(CHUNK_ID_ICOP, lstINFO, Copyright); |
333 |
|
|
LoadString(CHUNK_ID_IART, lstINFO, Artists); |
334 |
|
|
LoadString(CHUNK_ID_IGNR, lstINFO, Genre); |
335 |
|
|
LoadString(CHUNK_ID_IKEY, lstINFO, Keywords); |
336 |
|
|
LoadString(CHUNK_ID_IENG, lstINFO, Engineer); |
337 |
|
|
LoadString(CHUNK_ID_ITCH, lstINFO, Technician); |
338 |
|
|
LoadString(CHUNK_ID_ISFT, lstINFO, Software); |
339 |
|
|
LoadString(CHUNK_ID_IMED, lstINFO, Medium); |
340 |
|
|
LoadString(CHUNK_ID_ISRC, lstINFO, Source); |
341 |
|
|
LoadString(CHUNK_ID_ISRF, lstINFO, SourceForm); |
342 |
|
|
LoadString(CHUNK_ID_ICMS, lstINFO, Commissioned); |
343 |
persson |
928 |
LoadString(CHUNK_ID_ISBJ, lstINFO, Subject); |
344 |
schoenebeck |
2 |
} |
345 |
|
|
} |
346 |
|
|
} |
347 |
|
|
|
348 |
schoenebeck |
823 |
Info::~Info() { |
349 |
|
|
} |
350 |
|
|
|
351 |
schoenebeck |
1416 |
/** |
352 |
|
|
* Forces specific Info fields to be of a fixed length when being saved |
353 |
|
|
* to a file. By default the respective RIFF chunk of an Info field |
354 |
|
|
* will have a size analogue to its actual string length. With this |
355 |
|
|
* method however this behavior can be overridden, allowing to force an |
356 |
|
|
* arbitrary fixed size individually for each Info field. |
357 |
|
|
* |
358 |
|
|
* This method is used as a workaround for the gig format, not for DLS. |
359 |
|
|
* |
360 |
|
|
* @param lengths - NULL terminated array of string_length_t elements |
361 |
|
|
*/ |
362 |
|
|
void Info::SetFixedStringLengths(const string_length_t* lengths) { |
363 |
|
|
pFixedStringLengths = lengths; |
364 |
|
|
} |
365 |
|
|
|
366 |
schoenebeck |
800 |
/** @brief Load given INFO field. |
367 |
|
|
* |
368 |
|
|
* Load INFO field from INFO chunk with chunk ID \a ChunkID from INFO |
369 |
|
|
* list chunk \a lstINFO and save value to \a s. |
370 |
|
|
*/ |
371 |
|
|
void Info::LoadString(uint32_t ChunkID, RIFF::List* lstINFO, String& s) { |
372 |
|
|
RIFF::Chunk* ck = lstINFO->GetSubChunk(ChunkID); |
373 |
schoenebeck |
929 |
::LoadString(ck, s); // function from helper.h |
374 |
schoenebeck |
800 |
} |
375 |
schoenebeck |
2 |
|
376 |
schoenebeck |
800 |
/** @brief Apply given INFO field to the respective chunk. |
377 |
|
|
* |
378 |
|
|
* Apply given info value to info chunk with ID \a ChunkID, which is a |
379 |
|
|
* subchunk of INFO list chunk \a lstINFO. If the given chunk already |
380 |
schoenebeck |
804 |
* exists, value \a s will be applied. Otherwise if it doesn't exist yet |
381 |
|
|
* and either \a s or \a sDefault is not an empty string, such a chunk |
382 |
|
|
* will be created and either \a s or \a sDefault will be applied |
383 |
|
|
* (depending on which one is not an empty string, if both are not an |
384 |
|
|
* empty string \a s will be preferred). |
385 |
schoenebeck |
800 |
* |
386 |
|
|
* @param ChunkID - 32 bit RIFF chunk ID of INFO subchunk |
387 |
|
|
* @param lstINFO - parent (INFO) RIFF list chunk |
388 |
|
|
* @param s - current value of info field |
389 |
|
|
* @param sDefault - default value |
390 |
|
|
*/ |
391 |
persson |
1180 |
void Info::SaveString(uint32_t ChunkID, RIFF::List* lstINFO, const String& s, const String& sDefault) { |
392 |
|
|
int size = 0; |
393 |
schoenebeck |
1416 |
if (pFixedStringLengths) { |
394 |
|
|
for (int i = 0 ; pFixedStringLengths[i].length ; i++) { |
395 |
|
|
if (pFixedStringLengths[i].chunkId == ChunkID) { |
396 |
|
|
size = pFixedStringLengths[i].length; |
397 |
persson |
1209 |
break; |
398 |
persson |
1180 |
} |
399 |
|
|
} |
400 |
|
|
} |
401 |
schoenebeck |
800 |
RIFF::Chunk* ck = lstINFO->GetSubChunk(ChunkID); |
402 |
persson |
1180 |
::SaveString(ChunkID, ck, lstINFO, s, sDefault, size != 0, size); // function from helper.h |
403 |
schoenebeck |
800 |
} |
404 |
schoenebeck |
2 |
|
405 |
schoenebeck |
800 |
/** @brief Update chunks with current info values. |
406 |
|
|
* |
407 |
|
|
* Apply current INFO field values to the respective INFO chunks. You |
408 |
|
|
* have to call File::Save() to make changes persistent. |
409 |
schoenebeck |
2682 |
* |
410 |
|
|
* @param pProgress - callback function for progress notification |
411 |
schoenebeck |
800 |
*/ |
412 |
schoenebeck |
2682 |
void Info::UpdateChunks(progress_t* pProgress) { |
413 |
schoenebeck |
800 |
if (!pResourceListChunk) return; |
414 |
|
|
|
415 |
|
|
// make sure INFO list chunk exists |
416 |
|
|
RIFF::List* lstINFO = pResourceListChunk->GetSubList(LIST_TYPE_INFO); |
417 |
|
|
|
418 |
persson |
918 |
String defaultName = ""; |
419 |
|
|
String defaultCreationDate = ""; |
420 |
|
|
String defaultSoftware = ""; |
421 |
|
|
String defaultComments = ""; |
422 |
schoenebeck |
800 |
|
423 |
persson |
918 |
uint32_t resourceType = pResourceListChunk->GetListType(); |
424 |
|
|
|
425 |
|
|
if (!lstINFO) { |
426 |
|
|
lstINFO = pResourceListChunk->AddSubList(LIST_TYPE_INFO); |
427 |
|
|
|
428 |
|
|
// assemble default values |
429 |
|
|
defaultName = "NONAME"; |
430 |
|
|
|
431 |
|
|
if (resourceType == RIFF_TYPE_DLS) { |
432 |
|
|
// get current date |
433 |
|
|
time_t now = time(NULL); |
434 |
|
|
tm* pNowBroken = localtime(&now); |
435 |
|
|
char buf[11]; |
436 |
|
|
strftime(buf, 11, "%F", pNowBroken); |
437 |
|
|
defaultCreationDate = buf; |
438 |
|
|
|
439 |
|
|
defaultComments = "Created with " + libraryName() + " " + libraryVersion(); |
440 |
|
|
} |
441 |
|
|
if (resourceType == RIFF_TYPE_DLS || resourceType == LIST_TYPE_INS) |
442 |
|
|
{ |
443 |
|
|
defaultSoftware = libraryName() + " " + libraryVersion(); |
444 |
|
|
} |
445 |
|
|
} |
446 |
|
|
|
447 |
schoenebeck |
800 |
// save values |
448 |
persson |
918 |
|
449 |
persson |
1180 |
SaveString(CHUNK_ID_IARL, lstINFO, ArchivalLocation, String("")); |
450 |
|
|
SaveString(CHUNK_ID_IART, lstINFO, Artists, String("")); |
451 |
|
|
SaveString(CHUNK_ID_ICMS, lstINFO, Commissioned, String("")); |
452 |
|
|
SaveString(CHUNK_ID_ICMT, lstINFO, Comments, defaultComments); |
453 |
|
|
SaveString(CHUNK_ID_ICOP, lstINFO, Copyright, String("")); |
454 |
|
|
SaveString(CHUNK_ID_ICRD, lstINFO, CreationDate, defaultCreationDate); |
455 |
|
|
SaveString(CHUNK_ID_IENG, lstINFO, Engineer, String("")); |
456 |
|
|
SaveString(CHUNK_ID_IGNR, lstINFO, Genre, String("")); |
457 |
|
|
SaveString(CHUNK_ID_IKEY, lstINFO, Keywords, String("")); |
458 |
|
|
SaveString(CHUNK_ID_IMED, lstINFO, Medium, String("")); |
459 |
|
|
SaveString(CHUNK_ID_INAM, lstINFO, Name, defaultName); |
460 |
|
|
SaveString(CHUNK_ID_IPRD, lstINFO, Product, String("")); |
461 |
|
|
SaveString(CHUNK_ID_ISBJ, lstINFO, Subject, String("")); |
462 |
|
|
SaveString(CHUNK_ID_ISFT, lstINFO, Software, defaultSoftware); |
463 |
|
|
SaveString(CHUNK_ID_ISRC, lstINFO, Source, String("")); |
464 |
|
|
SaveString(CHUNK_ID_ISRF, lstINFO, SourceForm, String("")); |
465 |
|
|
SaveString(CHUNK_ID_ITCH, lstINFO, Technician, String("")); |
466 |
schoenebeck |
800 |
} |
467 |
schoenebeck |
3478 |
|
468 |
|
|
/** @brief Remove all RIFF chunks associated with this Info object. |
469 |
|
|
* |
470 |
|
|
* At the moment Info::DeleteChunks() does nothing. It is |
471 |
|
|
* recommended to call this method explicitly though from deriving classes's |
472 |
|
|
* own overridden implementation of this method to avoid potential future |
473 |
|
|
* compatiblity issues. |
474 |
|
|
* |
475 |
|
|
* See Storage::DeleteChunks() for details. |
476 |
|
|
*/ |
477 |
|
|
void Info::DeleteChunks() { |
478 |
|
|
} |
479 |
|
|
|
480 |
schoenebeck |
2394 |
/** |
481 |
|
|
* Make a deep copy of the Info object given by @a orig and assign it to |
482 |
|
|
* this object. |
483 |
|
|
* |
484 |
|
|
* @param orig - original Info object to be copied from |
485 |
|
|
*/ |
486 |
|
|
void Info::CopyAssign(const Info* orig) { |
487 |
|
|
Name = orig->Name; |
488 |
|
|
ArchivalLocation = orig->ArchivalLocation; |
489 |
|
|
CreationDate = orig->CreationDate; |
490 |
|
|
Comments = orig->Comments; |
491 |
|
|
Product = orig->Product; |
492 |
|
|
Copyright = orig->Copyright; |
493 |
|
|
Artists = orig->Artists; |
494 |
|
|
Genre = orig->Genre; |
495 |
|
|
Keywords = orig->Keywords; |
496 |
|
|
Engineer = orig->Engineer; |
497 |
|
|
Technician = orig->Technician; |
498 |
|
|
Software = orig->Software; |
499 |
|
|
Medium = orig->Medium; |
500 |
|
|
Source = orig->Source; |
501 |
|
|
SourceForm = orig->SourceForm; |
502 |
|
|
Commissioned = orig->Commissioned; |
503 |
|
|
Subject = orig->Subject; |
504 |
|
|
//FIXME: hmm, is copying this pointer a good idea? |
505 |
|
|
pFixedStringLengths = orig->pFixedStringLengths; |
506 |
|
|
} |
507 |
schoenebeck |
800 |
|
508 |
|
|
|
509 |
|
|
|
510 |
schoenebeck |
2 |
// *************** Resource *************** |
511 |
|
|
// * |
512 |
|
|
|
513 |
schoenebeck |
800 |
/** @brief Constructor. |
514 |
|
|
* |
515 |
|
|
* Initializes the 'Resource' object with values provided by a given |
516 |
|
|
* INFO list chunk and a DLID chunk (the latter optional). |
517 |
|
|
* |
518 |
|
|
* @param Parent - pointer to parent 'Resource', NULL if this is |
519 |
|
|
* the toplevel 'Resource' object |
520 |
|
|
* @param lstResource - pointer to an INFO list chunk |
521 |
|
|
*/ |
522 |
schoenebeck |
2 |
Resource::Resource(Resource* Parent, RIFF::List* lstResource) { |
523 |
|
|
pParent = Parent; |
524 |
schoenebeck |
800 |
pResourceList = lstResource; |
525 |
schoenebeck |
2 |
|
526 |
|
|
pInfo = new Info(lstResource); |
527 |
|
|
|
528 |
|
|
RIFF::Chunk* ckDLSID = lstResource->GetSubChunk(CHUNK_ID_DLID); |
529 |
|
|
if (ckDLSID) { |
530 |
schoenebeck |
3478 |
ckDLSID->SetPos(0); |
531 |
|
|
|
532 |
schoenebeck |
2 |
pDLSID = new dlsid_t; |
533 |
schoenebeck |
11 |
ckDLSID->Read(&pDLSID->ulData1, 1, 4); |
534 |
|
|
ckDLSID->Read(&pDLSID->usData2, 1, 2); |
535 |
|
|
ckDLSID->Read(&pDLSID->usData3, 1, 2); |
536 |
|
|
ckDLSID->Read(pDLSID->abData, 8, 1); |
537 |
schoenebeck |
2 |
} |
538 |
|
|
else pDLSID = NULL; |
539 |
|
|
} |
540 |
|
|
|
541 |
|
|
Resource::~Resource() { |
542 |
|
|
if (pDLSID) delete pDLSID; |
543 |
|
|
if (pInfo) delete pInfo; |
544 |
|
|
} |
545 |
|
|
|
546 |
schoenebeck |
3478 |
/** @brief Remove all RIFF chunks associated with this Resource object. |
547 |
|
|
* |
548 |
|
|
* At the moment Resource::DeleteChunks() does nothing. It is recommended |
549 |
|
|
* to call this method explicitly though from deriving classes's own |
550 |
|
|
* overridden implementation of this method to avoid potential future |
551 |
|
|
* compatiblity issues. |
552 |
|
|
* |
553 |
|
|
* See Storage::DeleteChunks() for details. |
554 |
|
|
*/ |
555 |
|
|
void Resource::DeleteChunks() { |
556 |
|
|
} |
557 |
|
|
|
558 |
schoenebeck |
800 |
/** @brief Update chunks with current Resource data. |
559 |
|
|
* |
560 |
|
|
* Apply Resource data persistently below the previously given resource |
561 |
|
|
* list chunk. This will currently only include the INFO data. The DLSID |
562 |
|
|
* will not be applied at the moment (yet). |
563 |
|
|
* |
564 |
|
|
* You have to call File::Save() to make changes persistent. |
565 |
schoenebeck |
2682 |
* |
566 |
|
|
* @param pProgress - callback function for progress notification |
567 |
schoenebeck |
800 |
*/ |
568 |
schoenebeck |
2682 |
void Resource::UpdateChunks(progress_t* pProgress) { |
569 |
|
|
pInfo->UpdateChunks(pProgress); |
570 |
persson |
1209 |
|
571 |
|
|
if (pDLSID) { |
572 |
|
|
// make sure 'dlid' chunk exists |
573 |
|
|
RIFF::Chunk* ckDLSID = pResourceList->GetSubChunk(CHUNK_ID_DLID); |
574 |
|
|
if (!ckDLSID) ckDLSID = pResourceList->AddSubChunk(CHUNK_ID_DLID, 16); |
575 |
|
|
uint8_t* pData = (uint8_t*)ckDLSID->LoadChunkData(); |
576 |
|
|
// update 'dlid' chunk |
577 |
|
|
store32(&pData[0], pDLSID->ulData1); |
578 |
|
|
store16(&pData[4], pDLSID->usData2); |
579 |
|
|
store16(&pData[6], pDLSID->usData3); |
580 |
|
|
memcpy(&pData[8], pDLSID->abData, 8); |
581 |
|
|
} |
582 |
schoenebeck |
800 |
} |
583 |
schoenebeck |
2 |
|
584 |
persson |
1209 |
/** |
585 |
|
|
* Generates a new DLSID for the resource. |
586 |
|
|
*/ |
587 |
|
|
void Resource::GenerateDLSID() { |
588 |
schoenebeck |
3474 |
#if defined(WIN32) || defined(__APPLE__) || defined(HAVE_UUID_GENERATE) |
589 |
persson |
1209 |
if (!pDLSID) pDLSID = new dlsid_t; |
590 |
schoenebeck |
3474 |
GenerateDLSID(pDLSID); |
591 |
|
|
#endif |
592 |
|
|
} |
593 |
schoenebeck |
800 |
|
594 |
schoenebeck |
3474 |
void Resource::GenerateDLSID(dlsid_t* pDLSID) { |
595 |
persson |
1209 |
#ifdef WIN32 |
596 |
|
|
UUID uuid; |
597 |
|
|
UuidCreate(&uuid); |
598 |
|
|
pDLSID->ulData1 = uuid.Data1; |
599 |
persson |
1301 |
pDLSID->usData2 = uuid.Data2; |
600 |
|
|
pDLSID->usData3 = uuid.Data3; |
601 |
persson |
1209 |
memcpy(pDLSID->abData, uuid.Data4, 8); |
602 |
|
|
|
603 |
|
|
#elif defined(__APPLE__) |
604 |
|
|
|
605 |
|
|
CFUUIDRef uuidRef = CFUUIDCreate(NULL); |
606 |
|
|
CFUUIDBytes uuid = CFUUIDGetUUIDBytes(uuidRef); |
607 |
|
|
CFRelease(uuidRef); |
608 |
|
|
pDLSID->ulData1 = uuid.byte0 | uuid.byte1 << 8 | uuid.byte2 << 16 | uuid.byte3 << 24; |
609 |
|
|
pDLSID->usData2 = uuid.byte4 | uuid.byte5 << 8; |
610 |
|
|
pDLSID->usData3 = uuid.byte6 | uuid.byte7 << 8; |
611 |
|
|
pDLSID->abData[0] = uuid.byte8; |
612 |
|
|
pDLSID->abData[1] = uuid.byte9; |
613 |
|
|
pDLSID->abData[2] = uuid.byte10; |
614 |
|
|
pDLSID->abData[3] = uuid.byte11; |
615 |
|
|
pDLSID->abData[4] = uuid.byte12; |
616 |
|
|
pDLSID->abData[5] = uuid.byte13; |
617 |
|
|
pDLSID->abData[6] = uuid.byte14; |
618 |
|
|
pDLSID->abData[7] = uuid.byte15; |
619 |
schoenebeck |
3723 |
#elif defined(HAVE_UUID_GENERATE) |
620 |
persson |
1209 |
uuid_t uuid; |
621 |
|
|
uuid_generate(uuid); |
622 |
|
|
pDLSID->ulData1 = uuid[0] | uuid[1] << 8 | uuid[2] << 16 | uuid[3] << 24; |
623 |
|
|
pDLSID->usData2 = uuid[4] | uuid[5] << 8; |
624 |
|
|
pDLSID->usData3 = uuid[6] | uuid[7] << 8; |
625 |
|
|
memcpy(pDLSID->abData, &uuid[8], 8); |
626 |
schoenebeck |
3723 |
#else |
627 |
|
|
# error "Missing support for uuid generation" |
628 |
persson |
1209 |
#endif |
629 |
|
|
} |
630 |
schoenebeck |
2394 |
|
631 |
|
|
/** |
632 |
|
|
* Make a deep copy of the Resource object given by @a orig and assign it |
633 |
|
|
* to this object. |
634 |
|
|
* |
635 |
|
|
* @param orig - original Resource object to be copied from |
636 |
|
|
*/ |
637 |
|
|
void Resource::CopyAssign(const Resource* orig) { |
638 |
|
|
pInfo->CopyAssign(orig->pInfo); |
639 |
|
|
} |
640 |
persson |
1209 |
|
641 |
|
|
|
642 |
schoenebeck |
2 |
// *************** Sampler *************** |
643 |
|
|
// * |
644 |
|
|
|
645 |
|
|
Sampler::Sampler(RIFF::List* ParentList) { |
646 |
schoenebeck |
800 |
pParentList = ParentList; |
647 |
schoenebeck |
2 |
RIFF::Chunk* wsmp = ParentList->GetSubChunk(CHUNK_ID_WSMP); |
648 |
schoenebeck |
800 |
if (wsmp) { |
649 |
schoenebeck |
3478 |
wsmp->SetPos(0); |
650 |
|
|
|
651 |
schoenebeck |
800 |
uiHeaderSize = wsmp->ReadUint32(); |
652 |
|
|
UnityNote = wsmp->ReadUint16(); |
653 |
|
|
FineTune = wsmp->ReadInt16(); |
654 |
|
|
Gain = wsmp->ReadInt32(); |
655 |
|
|
SamplerOptions = wsmp->ReadUint32(); |
656 |
|
|
SampleLoops = wsmp->ReadUint32(); |
657 |
|
|
} else { // 'wsmp' chunk missing |
658 |
persson |
1388 |
uiHeaderSize = 20; |
659 |
persson |
1218 |
UnityNote = 60; |
660 |
schoenebeck |
800 |
FineTune = 0; // +- 0 cents |
661 |
|
|
Gain = 0; // 0 dB |
662 |
|
|
SamplerOptions = F_WSMP_NO_COMPRESSION; |
663 |
|
|
SampleLoops = 0; |
664 |
|
|
} |
665 |
schoenebeck |
2 |
NoSampleDepthTruncation = SamplerOptions & F_WSMP_NO_TRUNCATION; |
666 |
|
|
NoSampleCompression = SamplerOptions & F_WSMP_NO_COMPRESSION; |
667 |
|
|
pSampleLoops = (SampleLoops) ? new sample_loop_t[SampleLoops] : NULL; |
668 |
schoenebeck |
800 |
if (SampleLoops) { |
669 |
|
|
wsmp->SetPos(uiHeaderSize); |
670 |
|
|
for (uint32_t i = 0; i < SampleLoops; i++) { |
671 |
|
|
wsmp->Read(pSampleLoops + i, 4, 4); |
672 |
|
|
if (pSampleLoops[i].Size > sizeof(sample_loop_t)) { // if loop struct was extended |
673 |
|
|
wsmp->SetPos(pSampleLoops[i].Size - sizeof(sample_loop_t), RIFF::stream_curpos); |
674 |
|
|
} |
675 |
schoenebeck |
2 |
} |
676 |
|
|
} |
677 |
|
|
} |
678 |
|
|
|
679 |
|
|
Sampler::~Sampler() { |
680 |
|
|
if (pSampleLoops) delete[] pSampleLoops; |
681 |
|
|
} |
682 |
|
|
|
683 |
schoenebeck |
1358 |
void Sampler::SetGain(int32_t gain) { |
684 |
|
|
Gain = gain; |
685 |
|
|
} |
686 |
|
|
|
687 |
schoenebeck |
800 |
/** |
688 |
|
|
* Apply all sample player options to the respective RIFF chunk. You |
689 |
|
|
* have to call File::Save() to make changes persistent. |
690 |
schoenebeck |
2682 |
* |
691 |
|
|
* @param pProgress - callback function for progress notification |
692 |
schoenebeck |
800 |
*/ |
693 |
schoenebeck |
2682 |
void Sampler::UpdateChunks(progress_t* pProgress) { |
694 |
schoenebeck |
800 |
// make sure 'wsmp' chunk exists |
695 |
|
|
RIFF::Chunk* wsmp = pParentList->GetSubChunk(CHUNK_ID_WSMP); |
696 |
persson |
1388 |
int wsmpSize = uiHeaderSize + SampleLoops * 16; |
697 |
schoenebeck |
800 |
if (!wsmp) { |
698 |
persson |
1388 |
wsmp = pParentList->AddSubChunk(CHUNK_ID_WSMP, wsmpSize); |
699 |
|
|
} else if (wsmp->GetSize() != wsmpSize) { |
700 |
|
|
wsmp->Resize(wsmpSize); |
701 |
schoenebeck |
800 |
} |
702 |
|
|
uint8_t* pData = (uint8_t*) wsmp->LoadChunkData(); |
703 |
|
|
// update headers size |
704 |
persson |
1179 |
store32(&pData[0], uiHeaderSize); |
705 |
schoenebeck |
800 |
// update respective sampler options bits |
706 |
|
|
SamplerOptions = (NoSampleDepthTruncation) ? SamplerOptions | F_WSMP_NO_TRUNCATION |
707 |
|
|
: SamplerOptions & (~F_WSMP_NO_TRUNCATION); |
708 |
|
|
SamplerOptions = (NoSampleCompression) ? SamplerOptions | F_WSMP_NO_COMPRESSION |
709 |
|
|
: SamplerOptions & (~F_WSMP_NO_COMPRESSION); |
710 |
persson |
1179 |
store16(&pData[4], UnityNote); |
711 |
|
|
store16(&pData[6], FineTune); |
712 |
|
|
store32(&pData[8], Gain); |
713 |
|
|
store32(&pData[12], SamplerOptions); |
714 |
|
|
store32(&pData[16], SampleLoops); |
715 |
schoenebeck |
800 |
// update loop definitions |
716 |
|
|
for (uint32_t i = 0; i < SampleLoops; i++) { |
717 |
|
|
//FIXME: this does not handle extended loop structs correctly |
718 |
persson |
1179 |
store32(&pData[uiHeaderSize + i * 16], pSampleLoops[i].Size); |
719 |
|
|
store32(&pData[uiHeaderSize + i * 16 + 4], pSampleLoops[i].LoopType); |
720 |
|
|
store32(&pData[uiHeaderSize + i * 16 + 8], pSampleLoops[i].LoopStart); |
721 |
|
|
store32(&pData[uiHeaderSize + i * 16 + 12], pSampleLoops[i].LoopLength); |
722 |
schoenebeck |
800 |
} |
723 |
|
|
} |
724 |
schoenebeck |
2 |
|
725 |
schoenebeck |
3478 |
/** @brief Remove all RIFF chunks associated with this Sampler object. |
726 |
|
|
* |
727 |
|
|
* At the moment Sampler::DeleteChunks() does nothing. It is |
728 |
|
|
* recommended to call this method explicitly though from deriving classes's |
729 |
|
|
* own overridden implementation of this method to avoid potential future |
730 |
|
|
* compatiblity issues. |
731 |
|
|
* |
732 |
|
|
* See Storage::DeleteChunks() for details. |
733 |
|
|
*/ |
734 |
|
|
void Sampler::DeleteChunks() { |
735 |
|
|
} |
736 |
|
|
|
737 |
schoenebeck |
1154 |
/** |
738 |
|
|
* Adds a new sample loop with the provided loop definition. |
739 |
|
|
* |
740 |
schoenebeck |
1194 |
* @param pLoopDef - points to a loop definition that is to be copied |
741 |
schoenebeck |
1154 |
*/ |
742 |
|
|
void Sampler::AddSampleLoop(sample_loop_t* pLoopDef) { |
743 |
|
|
sample_loop_t* pNewLoops = new sample_loop_t[SampleLoops + 1]; |
744 |
|
|
// copy old loops array |
745 |
|
|
for (int i = 0; i < SampleLoops; i++) { |
746 |
|
|
pNewLoops[i] = pSampleLoops[i]; |
747 |
|
|
} |
748 |
|
|
// add the new loop |
749 |
|
|
pNewLoops[SampleLoops] = *pLoopDef; |
750 |
schoenebeck |
1155 |
// auto correct size field |
751 |
|
|
pNewLoops[SampleLoops].Size = sizeof(DLS::sample_loop_t); |
752 |
schoenebeck |
1154 |
// free the old array and update the member variables |
753 |
|
|
if (SampleLoops) delete[] pSampleLoops; |
754 |
|
|
pSampleLoops = pNewLoops; |
755 |
|
|
SampleLoops++; |
756 |
|
|
} |
757 |
schoenebeck |
2 |
|
758 |
schoenebeck |
1154 |
/** |
759 |
|
|
* Deletes an existing sample loop. |
760 |
|
|
* |
761 |
|
|
* @param pLoopDef - pointer to existing loop definition |
762 |
|
|
* @throws Exception - if given loop definition does not exist |
763 |
|
|
*/ |
764 |
|
|
void Sampler::DeleteSampleLoop(sample_loop_t* pLoopDef) { |
765 |
|
|
sample_loop_t* pNewLoops = new sample_loop_t[SampleLoops - 1]; |
766 |
|
|
// copy old loops array (skipping given loop) |
767 |
|
|
for (int i = 0, o = 0; i < SampleLoops; i++) { |
768 |
|
|
if (&pSampleLoops[i] == pLoopDef) continue; |
769 |
persson |
2310 |
if (o == SampleLoops - 1) { |
770 |
|
|
delete[] pNewLoops; |
771 |
schoenebeck |
1154 |
throw Exception("Could not delete Sample Loop, because it does not exist"); |
772 |
persson |
2310 |
} |
773 |
schoenebeck |
1154 |
pNewLoops[o] = pSampleLoops[i]; |
774 |
|
|
o++; |
775 |
|
|
} |
776 |
|
|
// free the old array and update the member variables |
777 |
|
|
if (SampleLoops) delete[] pSampleLoops; |
778 |
|
|
pSampleLoops = pNewLoops; |
779 |
|
|
SampleLoops--; |
780 |
|
|
} |
781 |
schoenebeck |
2394 |
|
782 |
|
|
/** |
783 |
|
|
* Make a deep copy of the Sampler object given by @a orig and assign it |
784 |
|
|
* to this object. |
785 |
|
|
* |
786 |
|
|
* @param orig - original Sampler object to be copied from |
787 |
|
|
*/ |
788 |
|
|
void Sampler::CopyAssign(const Sampler* orig) { |
789 |
|
|
// copy trivial scalars |
790 |
|
|
UnityNote = orig->UnityNote; |
791 |
|
|
FineTune = orig->FineTune; |
792 |
|
|
Gain = orig->Gain; |
793 |
|
|
NoSampleDepthTruncation = orig->NoSampleDepthTruncation; |
794 |
|
|
NoSampleCompression = orig->NoSampleCompression; |
795 |
|
|
SamplerOptions = orig->SamplerOptions; |
796 |
|
|
|
797 |
|
|
// copy sample loops |
798 |
|
|
if (SampleLoops) delete[] pSampleLoops; |
799 |
|
|
pSampleLoops = new sample_loop_t[orig->SampleLoops]; |
800 |
|
|
memcpy(pSampleLoops, orig->pSampleLoops, orig->SampleLoops * sizeof(sample_loop_t)); |
801 |
|
|
SampleLoops = orig->SampleLoops; |
802 |
|
|
} |
803 |
schoenebeck |
800 |
|
804 |
schoenebeck |
1154 |
|
805 |
schoenebeck |
2 |
// *************** Sample *************** |
806 |
|
|
// * |
807 |
|
|
|
808 |
schoenebeck |
800 |
/** @brief Constructor. |
809 |
|
|
* |
810 |
|
|
* Load an existing sample or create a new one. A 'wave' list chunk must |
811 |
|
|
* be given to this constructor. In case the given 'wave' list chunk |
812 |
|
|
* contains a 'fmt' and 'data' chunk, the format and sample data will be |
813 |
|
|
* loaded from there, otherwise default values will be used and those |
814 |
|
|
* chunks will be created when File::Save() will be called later on. |
815 |
|
|
* |
816 |
|
|
* @param pFile - pointer to DLS::File where this sample is |
817 |
|
|
* located (or will be located) |
818 |
|
|
* @param waveList - pointer to 'wave' list chunk which is (or |
819 |
|
|
* will be) associated with this sample |
820 |
|
|
* @param WavePoolOffset - offset of this sample data from wave pool |
821 |
|
|
* ('wvpl') list chunk |
822 |
|
|
*/ |
823 |
schoenebeck |
2912 |
Sample::Sample(File* pFile, RIFF::List* waveList, file_offset_t WavePoolOffset) : Resource(pFile, waveList) { |
824 |
schoenebeck |
800 |
pWaveList = waveList; |
825 |
schoenebeck |
2912 |
ullWavePoolOffset = WavePoolOffset - LIST_HEADER_SIZE(waveList->GetFile()->GetFileOffsetSize()); |
826 |
schoenebeck |
2 |
pCkFormat = waveList->GetSubChunk(CHUNK_ID_FMT); |
827 |
|
|
pCkData = waveList->GetSubChunk(CHUNK_ID_DATA); |
828 |
schoenebeck |
800 |
if (pCkFormat) { |
829 |
schoenebeck |
3478 |
pCkFormat->SetPos(0); |
830 |
|
|
|
831 |
schoenebeck |
800 |
// common fields |
832 |
|
|
FormatTag = pCkFormat->ReadUint16(); |
833 |
|
|
Channels = pCkFormat->ReadUint16(); |
834 |
|
|
SamplesPerSecond = pCkFormat->ReadUint32(); |
835 |
|
|
AverageBytesPerSecond = pCkFormat->ReadUint32(); |
836 |
|
|
BlockAlign = pCkFormat->ReadUint16(); |
837 |
|
|
// PCM format specific |
838 |
schoenebeck |
1050 |
if (FormatTag == DLS_WAVE_FORMAT_PCM) { |
839 |
schoenebeck |
800 |
BitDepth = pCkFormat->ReadUint16(); |
840 |
persson |
928 |
FrameSize = (BitDepth / 8) * Channels; |
841 |
schoenebeck |
800 |
} else { // unsupported sample data format |
842 |
|
|
BitDepth = 0; |
843 |
|
|
FrameSize = 0; |
844 |
|
|
} |
845 |
|
|
} else { // 'fmt' chunk missing |
846 |
schoenebeck |
1050 |
FormatTag = DLS_WAVE_FORMAT_PCM; |
847 |
schoenebeck |
800 |
BitDepth = 16; |
848 |
|
|
Channels = 1; |
849 |
|
|
SamplesPerSecond = 44100; |
850 |
|
|
AverageBytesPerSecond = (BitDepth / 8) * SamplesPerSecond * Channels; |
851 |
|
|
FrameSize = (BitDepth / 8) * Channels; |
852 |
|
|
BlockAlign = FrameSize; |
853 |
schoenebeck |
2 |
} |
854 |
schoenebeck |
1050 |
SamplesTotal = (pCkData) ? (FormatTag == DLS_WAVE_FORMAT_PCM) ? pCkData->GetSize() / FrameSize |
855 |
|
|
: 0 |
856 |
schoenebeck |
800 |
: 0; |
857 |
schoenebeck |
2 |
} |
858 |
|
|
|
859 |
schoenebeck |
800 |
/** @brief Destructor. |
860 |
|
|
* |
861 |
schoenebeck |
3478 |
* Frees all memory occupied by this sample. |
862 |
schoenebeck |
800 |
*/ |
863 |
|
|
Sample::~Sample() { |
864 |
schoenebeck |
3478 |
if (pCkData) |
865 |
|
|
pCkData->ReleaseChunkData(); |
866 |
|
|
if (pCkFormat) |
867 |
|
|
pCkFormat->ReleaseChunkData(); |
868 |
schoenebeck |
800 |
} |
869 |
schoenebeck |
3478 |
|
870 |
|
|
/** @brief Remove all RIFF chunks associated with this Sample object. |
871 |
|
|
* |
872 |
|
|
* See Storage::DeleteChunks() for details. |
873 |
|
|
*/ |
874 |
|
|
void Sample::DeleteChunks() { |
875 |
|
|
// handle base class |
876 |
|
|
Resource::DeleteChunks(); |
877 |
|
|
|
878 |
|
|
// handle own RIFF chunks |
879 |
|
|
if (pWaveList) { |
880 |
|
|
RIFF::List* pParent = pWaveList->GetParent(); |
881 |
|
|
pParent->DeleteSubChunk(pWaveList); |
882 |
|
|
pWaveList = NULL; |
883 |
|
|
} |
884 |
|
|
} |
885 |
|
|
|
886 |
schoenebeck |
2482 |
/** |
887 |
|
|
* Make a deep copy of the Sample object given by @a orig (without the |
888 |
|
|
* actual sample waveform data however) and assign it to this object. |
889 |
|
|
* |
890 |
|
|
* This is a special internal variant of CopyAssign() which only copies the |
891 |
|
|
* most mandatory member variables. It will be called by gig::Sample |
892 |
|
|
* descendent instead of CopyAssign() since gig::Sample has its own |
893 |
|
|
* implementation to access and copy the actual sample waveform data. |
894 |
|
|
* |
895 |
|
|
* @param orig - original Sample object to be copied from |
896 |
|
|
*/ |
897 |
|
|
void Sample::CopyAssignCore(const Sample* orig) { |
898 |
|
|
// handle base classes |
899 |
|
|
Resource::CopyAssign(orig); |
900 |
|
|
// handle actual own attributes of this class |
901 |
|
|
FormatTag = orig->FormatTag; |
902 |
|
|
Channels = orig->Channels; |
903 |
|
|
SamplesPerSecond = orig->SamplesPerSecond; |
904 |
|
|
AverageBytesPerSecond = orig->AverageBytesPerSecond; |
905 |
|
|
BlockAlign = orig->BlockAlign; |
906 |
|
|
BitDepth = orig->BitDepth; |
907 |
|
|
SamplesTotal = orig->SamplesTotal; |
908 |
|
|
FrameSize = orig->FrameSize; |
909 |
|
|
} |
910 |
|
|
|
911 |
|
|
/** |
912 |
|
|
* Make a deep copy of the Sample object given by @a orig and assign it to |
913 |
|
|
* this object. |
914 |
|
|
* |
915 |
|
|
* @param orig - original Sample object to be copied from |
916 |
|
|
*/ |
917 |
|
|
void Sample::CopyAssign(const Sample* orig) { |
918 |
|
|
CopyAssignCore(orig); |
919 |
|
|
|
920 |
|
|
// copy sample waveform data (reading directly from disc) |
921 |
|
|
Resize(orig->GetSize()); |
922 |
|
|
char* buf = (char*) LoadSampleData(); |
923 |
|
|
Sample* pOrig = (Sample*) orig; //HACK: circumventing the constness here for now |
924 |
schoenebeck |
2912 |
const file_offset_t restorePos = pOrig->pCkData->GetPos(); |
925 |
schoenebeck |
2482 |
pOrig->SetPos(0); |
926 |
schoenebeck |
2912 |
for (file_offset_t todo = pOrig->GetSize(), i = 0; todo; ) { |
927 |
schoenebeck |
2482 |
const int iReadAtOnce = 64*1024; |
928 |
schoenebeck |
2912 |
file_offset_t n = (iReadAtOnce < todo) ? iReadAtOnce : todo; |
929 |
schoenebeck |
2482 |
n = pOrig->Read(&buf[i], n); |
930 |
|
|
if (!n) break; |
931 |
|
|
todo -= n; |
932 |
|
|
i += (n * pOrig->FrameSize); |
933 |
|
|
} |
934 |
|
|
pOrig->pCkData->SetPos(restorePos); |
935 |
|
|
} |
936 |
schoenebeck |
800 |
|
937 |
|
|
/** @brief Load sample data into RAM. |
938 |
|
|
* |
939 |
|
|
* In case the respective 'data' chunk exists, the sample data will be |
940 |
|
|
* loaded into RAM (if not done already) and a pointer to the data in |
941 |
|
|
* RAM will be returned. If this is a new sample, you have to call |
942 |
|
|
* Resize() with the desired sample size to create the mandatory RIFF |
943 |
|
|
* chunk for the sample wave data. |
944 |
|
|
* |
945 |
|
|
* You can call LoadChunkData() again if you previously scheduled to |
946 |
|
|
* enlarge the sample data RIFF chunk with a Resize() call. In that case |
947 |
|
|
* the buffer will be enlarged to the new, scheduled size and you can |
948 |
|
|
* already place the sample wave data to the buffer and finally call |
949 |
|
|
* File::Save() to enlarge the sample data's chunk physically and write |
950 |
|
|
* the new sample wave data in one rush. This approach is definitely |
951 |
|
|
* recommended if you have to enlarge and write new sample data to a lot |
952 |
|
|
* of samples. |
953 |
|
|
* |
954 |
|
|
* <b>Caution:</b> the buffer pointer will be invalidated once |
955 |
|
|
* File::Save() was called. You have to call LoadChunkData() again to |
956 |
|
|
* get a new, valid pointer whenever File::Save() was called. |
957 |
|
|
* |
958 |
|
|
* @returns pointer to sample data in RAM, NULL in case respective |
959 |
|
|
* 'data' chunk does not exist (yet) |
960 |
|
|
* @throws Exception if data buffer could not be enlarged |
961 |
|
|
* @see Resize(), File::Save() |
962 |
|
|
*/ |
963 |
schoenebeck |
2 |
void* Sample::LoadSampleData() { |
964 |
schoenebeck |
800 |
return (pCkData) ? pCkData->LoadChunkData() : NULL; |
965 |
schoenebeck |
2 |
} |
966 |
|
|
|
967 |
schoenebeck |
800 |
/** @brief Free sample data from RAM. |
968 |
|
|
* |
969 |
|
|
* In case sample data was previously successfully loaded into RAM with |
970 |
|
|
* LoadSampleData(), this method will free the sample data from RAM. |
971 |
|
|
*/ |
972 |
schoenebeck |
2 |
void Sample::ReleaseSampleData() { |
973 |
schoenebeck |
800 |
if (pCkData) pCkData->ReleaseChunkData(); |
974 |
schoenebeck |
2 |
} |
975 |
|
|
|
976 |
schoenebeck |
800 |
/** @brief Returns sample size. |
977 |
|
|
* |
978 |
|
|
* Returns the sample wave form's data size (in sample points). This is |
979 |
|
|
* actually the current, physical size (converted to sample points) of |
980 |
|
|
* the RIFF chunk which encapsulates the sample's wave data. The |
981 |
|
|
* returned value is dependant to the current FrameSize value. |
982 |
|
|
* |
983 |
schoenebeck |
1050 |
* @returns number of sample points or 0 if FormatTag != DLS_WAVE_FORMAT_PCM |
984 |
schoenebeck |
800 |
* @see FrameSize, FormatTag |
985 |
|
|
*/ |
986 |
schoenebeck |
2912 |
file_offset_t Sample::GetSize() const { |
987 |
schoenebeck |
1050 |
if (FormatTag != DLS_WAVE_FORMAT_PCM) return 0; |
988 |
schoenebeck |
800 |
return (pCkData) ? pCkData->GetSize() / FrameSize : 0; |
989 |
|
|
} |
990 |
|
|
|
991 |
|
|
/** @brief Resize sample. |
992 |
|
|
* |
993 |
|
|
* Resizes the sample's wave form data, that is the actual size of |
994 |
|
|
* sample wave data possible to be written for this sample. This call |
995 |
|
|
* will return immediately and just schedule the resize operation. You |
996 |
|
|
* should call File::Save() to actually perform the resize operation(s) |
997 |
|
|
* "physically" to the file. As this can take a while on large files, it |
998 |
|
|
* is recommended to call Resize() first on all samples which have to be |
999 |
|
|
* resized and finally to call File::Save() to perform all those resize |
1000 |
|
|
* operations in one rush. |
1001 |
|
|
* |
1002 |
|
|
* The actual size (in bytes) is dependant to the current FrameSize |
1003 |
|
|
* value. You may want to set FrameSize before calling Resize(). |
1004 |
|
|
* |
1005 |
|
|
* <b>Caution:</b> You cannot directly write to enlarged samples before |
1006 |
|
|
* calling File::Save() as this might exceed the current sample's |
1007 |
|
|
* boundary! |
1008 |
|
|
* |
1009 |
schoenebeck |
1050 |
* Also note: only DLS_WAVE_FORMAT_PCM is currently supported, that is |
1010 |
|
|
* FormatTag must be DLS_WAVE_FORMAT_PCM. Trying to resize samples with |
1011 |
schoenebeck |
800 |
* other formats will fail! |
1012 |
|
|
* |
1013 |
schoenebeck |
2922 |
* @param NewSize - new sample wave data size in sample points (must be |
1014 |
|
|
* greater than zero) |
1015 |
|
|
* @throws Exception if FormatTag != DLS_WAVE_FORMAT_PCM |
1016 |
|
|
* @throws Exception if \a NewSize is less than 1 or unrealistic large |
1017 |
schoenebeck |
800 |
* @see File::Save(), FrameSize, FormatTag |
1018 |
|
|
*/ |
1019 |
schoenebeck |
2922 |
void Sample::Resize(file_offset_t NewSize) { |
1020 |
schoenebeck |
1050 |
if (FormatTag != DLS_WAVE_FORMAT_PCM) throw Exception("Sample's format is not DLS_WAVE_FORMAT_PCM"); |
1021 |
schoenebeck |
2922 |
if (NewSize < 1) throw Exception("Sample size must be at least one sample point"); |
1022 |
|
|
if ((NewSize >> 48) != 0) |
1023 |
|
|
throw Exception("Unrealistic high DLS sample size detected"); |
1024 |
|
|
const file_offset_t sizeInBytes = NewSize * FrameSize; |
1025 |
schoenebeck |
800 |
pCkData = pWaveList->GetSubChunk(CHUNK_ID_DATA); |
1026 |
schoenebeck |
2922 |
if (pCkData) pCkData->Resize(sizeInBytes); |
1027 |
|
|
else pCkData = pWaveList->AddSubChunk(CHUNK_ID_DATA, sizeInBytes); |
1028 |
schoenebeck |
800 |
} |
1029 |
|
|
|
1030 |
schoenebeck |
2 |
/** |
1031 |
|
|
* Sets the position within the sample (in sample points, not in |
1032 |
|
|
* bytes). Use this method and <i>Read()</i> if you don't want to load |
1033 |
|
|
* the sample into RAM, thus for disk streaming. |
1034 |
|
|
* |
1035 |
schoenebeck |
1050 |
* Also note: only DLS_WAVE_FORMAT_PCM is currently supported, that is |
1036 |
|
|
* FormatTag must be DLS_WAVE_FORMAT_PCM. Trying to reposition the sample |
1037 |
schoenebeck |
800 |
* with other formats will fail! |
1038 |
|
|
* |
1039 |
schoenebeck |
2 |
* @param SampleCount number of sample points |
1040 |
|
|
* @param Whence to which relation \a SampleCount refers to |
1041 |
schoenebeck |
800 |
* @returns new position within the sample, 0 if |
1042 |
schoenebeck |
1050 |
* FormatTag != DLS_WAVE_FORMAT_PCM |
1043 |
schoenebeck |
800 |
* @throws Exception if no data RIFF chunk was created for the sample yet |
1044 |
|
|
* @see FrameSize, FormatTag |
1045 |
schoenebeck |
2 |
*/ |
1046 |
schoenebeck |
2912 |
file_offset_t Sample::SetPos(file_offset_t SampleCount, RIFF::stream_whence_t Whence) { |
1047 |
schoenebeck |
1050 |
if (FormatTag != DLS_WAVE_FORMAT_PCM) return 0; // failed: wave data not PCM format |
1048 |
schoenebeck |
800 |
if (!pCkData) throw Exception("No data chunk created for sample yet, call Sample::Resize() to create one"); |
1049 |
schoenebeck |
2912 |
file_offset_t orderedBytes = SampleCount * FrameSize; |
1050 |
|
|
file_offset_t result = pCkData->SetPos(orderedBytes, Whence); |
1051 |
schoenebeck |
2 |
return (result == orderedBytes) ? SampleCount |
1052 |
|
|
: result / FrameSize; |
1053 |
|
|
} |
1054 |
|
|
|
1055 |
|
|
/** |
1056 |
|
|
* Reads \a SampleCount number of sample points from the current |
1057 |
|
|
* position into the buffer pointed by \a pBuffer and increments the |
1058 |
|
|
* position within the sample. Use this method and <i>SetPos()</i> if you |
1059 |
|
|
* don't want to load the sample into RAM, thus for disk streaming. |
1060 |
|
|
* |
1061 |
|
|
* @param pBuffer destination buffer |
1062 |
|
|
* @param SampleCount number of sample points to read |
1063 |
|
|
*/ |
1064 |
schoenebeck |
2912 |
file_offset_t Sample::Read(void* pBuffer, file_offset_t SampleCount) { |
1065 |
schoenebeck |
1050 |
if (FormatTag != DLS_WAVE_FORMAT_PCM) return 0; // failed: wave data not PCM format |
1066 |
schoenebeck |
11 |
return pCkData->Read(pBuffer, SampleCount, FrameSize); // FIXME: channel inversion due to endian correction? |
1067 |
schoenebeck |
2 |
} |
1068 |
|
|
|
1069 |
schoenebeck |
800 |
/** @brief Write sample wave data. |
1070 |
|
|
* |
1071 |
|
|
* Writes \a SampleCount number of sample points from the buffer pointed |
1072 |
|
|
* by \a pBuffer and increments the position within the sample. Use this |
1073 |
|
|
* method to directly write the sample data to disk, i.e. if you don't |
1074 |
|
|
* want or cannot load the whole sample data into RAM. |
1075 |
|
|
* |
1076 |
|
|
* You have to Resize() the sample to the desired size and call |
1077 |
|
|
* File::Save() <b>before</b> using Write(). |
1078 |
|
|
* |
1079 |
|
|
* @param pBuffer - source buffer |
1080 |
|
|
* @param SampleCount - number of sample points to write |
1081 |
|
|
* @throws Exception if current sample size is too small |
1082 |
|
|
* @see LoadSampleData() |
1083 |
|
|
*/ |
1084 |
schoenebeck |
2912 |
file_offset_t Sample::Write(void* pBuffer, file_offset_t SampleCount) { |
1085 |
schoenebeck |
1050 |
if (FormatTag != DLS_WAVE_FORMAT_PCM) return 0; // failed: wave data not PCM format |
1086 |
schoenebeck |
800 |
if (GetSize() < SampleCount) throw Exception("Could not write sample data, current sample size to small"); |
1087 |
|
|
return pCkData->Write(pBuffer, SampleCount, FrameSize); // FIXME: channel inversion due to endian correction? |
1088 |
|
|
} |
1089 |
schoenebeck |
2 |
|
1090 |
schoenebeck |
800 |
/** |
1091 |
|
|
* Apply sample and its settings to the respective RIFF chunks. You have |
1092 |
|
|
* to call File::Save() to make changes persistent. |
1093 |
|
|
* |
1094 |
schoenebeck |
2682 |
* @param pProgress - callback function for progress notification |
1095 |
schoenebeck |
1050 |
* @throws Exception if FormatTag != DLS_WAVE_FORMAT_PCM or no sample data |
1096 |
schoenebeck |
800 |
* was provided yet |
1097 |
|
|
*/ |
1098 |
schoenebeck |
2682 |
void Sample::UpdateChunks(progress_t* pProgress) { |
1099 |
schoenebeck |
1050 |
if (FormatTag != DLS_WAVE_FORMAT_PCM) |
1100 |
schoenebeck |
800 |
throw Exception("Could not save sample, only PCM format is supported"); |
1101 |
|
|
// we refuse to do anything if not sample wave form was provided yet |
1102 |
|
|
if (!pCkData) |
1103 |
|
|
throw Exception("Could not save sample, there is no sample data to save"); |
1104 |
|
|
// update chunks of base class as well |
1105 |
schoenebeck |
2682 |
Resource::UpdateChunks(pProgress); |
1106 |
schoenebeck |
800 |
// make sure 'fmt' chunk exists |
1107 |
|
|
RIFF::Chunk* pCkFormat = pWaveList->GetSubChunk(CHUNK_ID_FMT); |
1108 |
|
|
if (!pCkFormat) pCkFormat = pWaveList->AddSubChunk(CHUNK_ID_FMT, 16); // assumes PCM format |
1109 |
|
|
uint8_t* pData = (uint8_t*) pCkFormat->LoadChunkData(); |
1110 |
|
|
// update 'fmt' chunk |
1111 |
persson |
1179 |
store16(&pData[0], FormatTag); |
1112 |
|
|
store16(&pData[2], Channels); |
1113 |
|
|
store32(&pData[4], SamplesPerSecond); |
1114 |
|
|
store32(&pData[8], AverageBytesPerSecond); |
1115 |
|
|
store16(&pData[12], BlockAlign); |
1116 |
|
|
store16(&pData[14], BitDepth); // assuming PCM format |
1117 |
schoenebeck |
800 |
} |
1118 |
schoenebeck |
2 |
|
1119 |
schoenebeck |
800 |
|
1120 |
|
|
|
1121 |
schoenebeck |
2 |
// *************** Region *************** |
1122 |
|
|
// * |
1123 |
|
|
|
1124 |
|
|
Region::Region(Instrument* pInstrument, RIFF::List* rgnList) : Resource(pInstrument, rgnList), Articulator(rgnList), Sampler(rgnList) { |
1125 |
|
|
pCkRegion = rgnList; |
1126 |
|
|
|
1127 |
schoenebeck |
3048 |
// articulation information |
1128 |
schoenebeck |
2 |
RIFF::Chunk* rgnh = rgnList->GetSubChunk(CHUNK_ID_RGNH); |
1129 |
schoenebeck |
800 |
if (rgnh) { |
1130 |
schoenebeck |
3478 |
rgnh->SetPos(0); |
1131 |
|
|
|
1132 |
schoenebeck |
800 |
rgnh->Read(&KeyRange, 2, 2); |
1133 |
|
|
rgnh->Read(&VelocityRange, 2, 2); |
1134 |
|
|
FormatOptionFlags = rgnh->ReadUint16(); |
1135 |
|
|
KeyGroup = rgnh->ReadUint16(); |
1136 |
|
|
// Layer is optional |
1137 |
|
|
if (rgnh->RemainingBytes() >= sizeof(uint16_t)) { |
1138 |
|
|
rgnh->Read(&Layer, 1, sizeof(uint16_t)); |
1139 |
|
|
} else Layer = 0; |
1140 |
|
|
} else { // 'rgnh' chunk is missing |
1141 |
|
|
KeyRange.low = 0; |
1142 |
|
|
KeyRange.high = 127; |
1143 |
|
|
VelocityRange.low = 0; |
1144 |
|
|
VelocityRange.high = 127; |
1145 |
|
|
FormatOptionFlags = F_RGN_OPTION_SELFNONEXCLUSIVE; |
1146 |
|
|
KeyGroup = 0; |
1147 |
|
|
Layer = 0; |
1148 |
schoenebeck |
2 |
} |
1149 |
schoenebeck |
800 |
SelfNonExclusive = FormatOptionFlags & F_RGN_OPTION_SELFNONEXCLUSIVE; |
1150 |
schoenebeck |
2 |
|
1151 |
schoenebeck |
3048 |
// sample information |
1152 |
schoenebeck |
2 |
RIFF::Chunk* wlnk = rgnList->GetSubChunk(CHUNK_ID_WLNK); |
1153 |
schoenebeck |
800 |
if (wlnk) { |
1154 |
schoenebeck |
3478 |
wlnk->SetPos(0); |
1155 |
|
|
|
1156 |
schoenebeck |
800 |
WaveLinkOptionFlags = wlnk->ReadUint16(); |
1157 |
|
|
PhaseGroup = wlnk->ReadUint16(); |
1158 |
|
|
Channel = wlnk->ReadUint32(); |
1159 |
|
|
WavePoolTableIndex = wlnk->ReadUint32(); |
1160 |
|
|
} else { // 'wlnk' chunk is missing |
1161 |
|
|
WaveLinkOptionFlags = 0; |
1162 |
|
|
PhaseGroup = 0; |
1163 |
|
|
Channel = 0; // mono |
1164 |
|
|
WavePoolTableIndex = 0; // first entry in wave pool table |
1165 |
|
|
} |
1166 |
|
|
PhaseMaster = WaveLinkOptionFlags & F_WAVELINK_PHASE_MASTER; |
1167 |
|
|
MultiChannel = WaveLinkOptionFlags & F_WAVELINK_MULTICHANNEL; |
1168 |
schoenebeck |
2 |
|
1169 |
|
|
pSample = NULL; |
1170 |
|
|
} |
1171 |
|
|
|
1172 |
schoenebeck |
800 |
/** @brief Destructor. |
1173 |
|
|
* |
1174 |
schoenebeck |
3478 |
* Intended to free up all memory occupied by this Region object. ATM this |
1175 |
|
|
* destructor implementation does nothing though. |
1176 |
schoenebeck |
800 |
*/ |
1177 |
schoenebeck |
2 |
Region::~Region() { |
1178 |
|
|
} |
1179 |
|
|
|
1180 |
schoenebeck |
3478 |
/** @brief Remove all RIFF chunks associated with this Region object. |
1181 |
|
|
* |
1182 |
|
|
* See Storage::DeleteChunks() for details. |
1183 |
|
|
*/ |
1184 |
|
|
void Region::DeleteChunks() { |
1185 |
|
|
// handle base classes |
1186 |
|
|
Resource::DeleteChunks(); |
1187 |
|
|
Articulator::DeleteChunks(); |
1188 |
|
|
Sampler::DeleteChunks(); |
1189 |
|
|
|
1190 |
|
|
// handle own RIFF chunks |
1191 |
|
|
if (pCkRegion) { |
1192 |
|
|
RIFF::List* pParent = pCkRegion->GetParent(); |
1193 |
|
|
pParent->DeleteSubChunk(pCkRegion); |
1194 |
|
|
pCkRegion = NULL; |
1195 |
|
|
} |
1196 |
|
|
} |
1197 |
|
|
|
1198 |
schoenebeck |
2 |
Sample* Region::GetSample() { |
1199 |
|
|
if (pSample) return pSample; |
1200 |
|
|
File* file = (File*) GetParent()->GetParent(); |
1201 |
schoenebeck |
2912 |
uint64_t soughtoffset = file->pWavePoolTable[WavePoolTableIndex]; |
1202 |
schoenebeck |
3929 |
size_t i = 0; |
1203 |
|
|
for (Sample* sample = file->GetSample(i); sample; |
1204 |
|
|
sample = file->GetSample(++i)) |
1205 |
|
|
{ |
1206 |
schoenebeck |
2912 |
if (sample->ullWavePoolOffset == soughtoffset) return (pSample = sample); |
1207 |
schoenebeck |
2 |
} |
1208 |
|
|
return NULL; |
1209 |
|
|
} |
1210 |
|
|
|
1211 |
schoenebeck |
800 |
/** |
1212 |
|
|
* Assign another sample to this Region. |
1213 |
|
|
* |
1214 |
|
|
* @param pSample - sample to be assigned |
1215 |
|
|
*/ |
1216 |
|
|
void Region::SetSample(Sample* pSample) { |
1217 |
|
|
this->pSample = pSample; |
1218 |
|
|
WavePoolTableIndex = 0; // we update this offset when we Save() |
1219 |
|
|
} |
1220 |
schoenebeck |
2 |
|
1221 |
schoenebeck |
800 |
/** |
1222 |
schoenebeck |
1335 |
* Modifies the key range of this Region and makes sure the respective |
1223 |
|
|
* chunks are in correct order. |
1224 |
|
|
* |
1225 |
|
|
* @param Low - lower end of key range |
1226 |
|
|
* @param High - upper end of key range |
1227 |
|
|
*/ |
1228 |
|
|
void Region::SetKeyRange(uint16_t Low, uint16_t High) { |
1229 |
|
|
KeyRange.low = Low; |
1230 |
|
|
KeyRange.high = High; |
1231 |
|
|
|
1232 |
|
|
// make sure regions are already loaded |
1233 |
|
|
Instrument* pInstrument = (Instrument*) GetParent(); |
1234 |
|
|
if (!pInstrument->pRegions) pInstrument->LoadRegions(); |
1235 |
|
|
if (!pInstrument->pRegions) return; |
1236 |
|
|
|
1237 |
|
|
// find the r which is the first one to the right of this region |
1238 |
|
|
// at its new position |
1239 |
|
|
Region* r = NULL; |
1240 |
|
|
Region* prev_region = NULL; |
1241 |
|
|
for ( |
1242 |
|
|
Instrument::RegionList::iterator iter = pInstrument->pRegions->begin(); |
1243 |
|
|
iter != pInstrument->pRegions->end(); iter++ |
1244 |
|
|
) { |
1245 |
|
|
if ((*iter)->KeyRange.low > this->KeyRange.low) { |
1246 |
|
|
r = *iter; |
1247 |
|
|
break; |
1248 |
|
|
} |
1249 |
|
|
prev_region = *iter; |
1250 |
|
|
} |
1251 |
|
|
|
1252 |
|
|
// place this region before r if it's not already there |
1253 |
|
|
if (prev_region != this) pInstrument->MoveRegion(this, r); |
1254 |
|
|
} |
1255 |
|
|
|
1256 |
|
|
/** |
1257 |
schoenebeck |
800 |
* Apply Region settings to the respective RIFF chunks. You have to |
1258 |
|
|
* call File::Save() to make changes persistent. |
1259 |
|
|
* |
1260 |
schoenebeck |
2682 |
* @param pProgress - callback function for progress notification |
1261 |
schoenebeck |
800 |
* @throws Exception - if the Region's sample could not be found |
1262 |
|
|
*/ |
1263 |
schoenebeck |
2682 |
void Region::UpdateChunks(progress_t* pProgress) { |
1264 |
schoenebeck |
800 |
// make sure 'rgnh' chunk exists |
1265 |
|
|
RIFF::Chunk* rgnh = pCkRegion->GetSubChunk(CHUNK_ID_RGNH); |
1266 |
persson |
918 |
if (!rgnh) rgnh = pCkRegion->AddSubChunk(CHUNK_ID_RGNH, Layer ? 14 : 12); |
1267 |
schoenebeck |
800 |
uint8_t* pData = (uint8_t*) rgnh->LoadChunkData(); |
1268 |
|
|
FormatOptionFlags = (SelfNonExclusive) |
1269 |
|
|
? FormatOptionFlags | F_RGN_OPTION_SELFNONEXCLUSIVE |
1270 |
|
|
: FormatOptionFlags & (~F_RGN_OPTION_SELFNONEXCLUSIVE); |
1271 |
|
|
// update 'rgnh' chunk |
1272 |
persson |
1179 |
store16(&pData[0], KeyRange.low); |
1273 |
|
|
store16(&pData[2], KeyRange.high); |
1274 |
|
|
store16(&pData[4], VelocityRange.low); |
1275 |
|
|
store16(&pData[6], VelocityRange.high); |
1276 |
|
|
store16(&pData[8], FormatOptionFlags); |
1277 |
|
|
store16(&pData[10], KeyGroup); |
1278 |
|
|
if (rgnh->GetSize() >= 14) store16(&pData[12], Layer); |
1279 |
schoenebeck |
2 |
|
1280 |
persson |
918 |
// update chunks of base classes as well (but skip Resource, |
1281 |
|
|
// as a rgn doesn't seem to have dlid and INFO chunks) |
1282 |
schoenebeck |
2682 |
Articulator::UpdateChunks(pProgress); |
1283 |
|
|
Sampler::UpdateChunks(pProgress); |
1284 |
schoenebeck |
800 |
|
1285 |
|
|
// make sure 'wlnk' chunk exists |
1286 |
|
|
RIFF::Chunk* wlnk = pCkRegion->GetSubChunk(CHUNK_ID_WLNK); |
1287 |
|
|
if (!wlnk) wlnk = pCkRegion->AddSubChunk(CHUNK_ID_WLNK, 12); |
1288 |
|
|
pData = (uint8_t*) wlnk->LoadChunkData(); |
1289 |
|
|
WaveLinkOptionFlags = (PhaseMaster) |
1290 |
|
|
? WaveLinkOptionFlags | F_WAVELINK_PHASE_MASTER |
1291 |
|
|
: WaveLinkOptionFlags & (~F_WAVELINK_PHASE_MASTER); |
1292 |
|
|
WaveLinkOptionFlags = (MultiChannel) |
1293 |
|
|
? WaveLinkOptionFlags | F_WAVELINK_MULTICHANNEL |
1294 |
|
|
: WaveLinkOptionFlags & (~F_WAVELINK_MULTICHANNEL); |
1295 |
|
|
// get sample's wave pool table index |
1296 |
|
|
int index = -1; |
1297 |
|
|
File* pFile = (File*) GetParent()->GetParent(); |
1298 |
schoenebeck |
804 |
if (pFile->pSamples) { |
1299 |
|
|
File::SampleList::iterator iter = pFile->pSamples->begin(); |
1300 |
|
|
File::SampleList::iterator end = pFile->pSamples->end(); |
1301 |
|
|
for (int i = 0; iter != end; ++iter, i++) { |
1302 |
|
|
if (*iter == pSample) { |
1303 |
|
|
index = i; |
1304 |
|
|
break; |
1305 |
|
|
} |
1306 |
schoenebeck |
800 |
} |
1307 |
|
|
} |
1308 |
|
|
WavePoolTableIndex = index; |
1309 |
|
|
// update 'wlnk' chunk |
1310 |
persson |
1179 |
store16(&pData[0], WaveLinkOptionFlags); |
1311 |
|
|
store16(&pData[2], PhaseGroup); |
1312 |
|
|
store32(&pData[4], Channel); |
1313 |
|
|
store32(&pData[8], WavePoolTableIndex); |
1314 |
schoenebeck |
800 |
} |
1315 |
schoenebeck |
2394 |
|
1316 |
|
|
/** |
1317 |
|
|
* Make a (semi) deep copy of the Region object given by @a orig and assign |
1318 |
|
|
* it to this object. |
1319 |
|
|
* |
1320 |
|
|
* Note that the sample pointer referenced by @a orig is simply copied as |
1321 |
|
|
* memory address. Thus the respective sample is shared, not duplicated! |
1322 |
|
|
* |
1323 |
|
|
* @param orig - original Region object to be copied from |
1324 |
|
|
*/ |
1325 |
|
|
void Region::CopyAssign(const Region* orig) { |
1326 |
|
|
// handle base classes |
1327 |
|
|
Resource::CopyAssign(orig); |
1328 |
|
|
Articulator::CopyAssign(orig); |
1329 |
|
|
Sampler::CopyAssign(orig); |
1330 |
|
|
// handle actual own attributes of this class |
1331 |
|
|
// (the trivial ones) |
1332 |
|
|
VelocityRange = orig->VelocityRange; |
1333 |
|
|
KeyGroup = orig->KeyGroup; |
1334 |
|
|
Layer = orig->Layer; |
1335 |
|
|
SelfNonExclusive = orig->SelfNonExclusive; |
1336 |
|
|
PhaseMaster = orig->PhaseMaster; |
1337 |
|
|
PhaseGroup = orig->PhaseGroup; |
1338 |
|
|
MultiChannel = orig->MultiChannel; |
1339 |
|
|
Channel = orig->Channel; |
1340 |
schoenebeck |
2482 |
// only take the raw sample reference if the two Region objects are |
1341 |
|
|
// part of the same file |
1342 |
|
|
if (GetParent()->GetParent() == orig->GetParent()->GetParent()) { |
1343 |
|
|
WavePoolTableIndex = orig->WavePoolTableIndex; |
1344 |
|
|
pSample = orig->pSample; |
1345 |
|
|
} else { |
1346 |
|
|
WavePoolTableIndex = -1; |
1347 |
|
|
pSample = NULL; |
1348 |
|
|
} |
1349 |
schoenebeck |
2394 |
FormatOptionFlags = orig->FormatOptionFlags; |
1350 |
|
|
WaveLinkOptionFlags = orig->WaveLinkOptionFlags; |
1351 |
|
|
// handle the last, a bit sensible attribute |
1352 |
|
|
SetKeyRange(orig->KeyRange.low, orig->KeyRange.high); |
1353 |
|
|
} |
1354 |
schoenebeck |
800 |
|
1355 |
|
|
|
1356 |
schoenebeck |
2 |
// *************** Instrument *************** |
1357 |
|
|
// * |
1358 |
|
|
|
1359 |
schoenebeck |
800 |
/** @brief Constructor. |
1360 |
|
|
* |
1361 |
|
|
* Load an existing instrument definition or create a new one. An 'ins' |
1362 |
|
|
* list chunk must be given to this constructor. In case this 'ins' list |
1363 |
|
|
* chunk contains a 'insh' chunk, the instrument data fields will be |
1364 |
|
|
* loaded from there, otherwise default values will be used and the |
1365 |
|
|
* 'insh' chunk will be created once File::Save() was called. |
1366 |
|
|
* |
1367 |
|
|
* @param pFile - pointer to DLS::File where this instrument is |
1368 |
|
|
* located (or will be located) |
1369 |
|
|
* @param insList - pointer to 'ins' list chunk which is (or will be) |
1370 |
|
|
* associated with this instrument |
1371 |
|
|
*/ |
1372 |
schoenebeck |
2 |
Instrument::Instrument(File* pFile, RIFF::List* insList) : Resource(pFile, insList), Articulator(insList) { |
1373 |
|
|
pCkInstrument = insList; |
1374 |
|
|
|
1375 |
schoenebeck |
800 |
midi_locale_t locale; |
1376 |
schoenebeck |
2 |
RIFF::Chunk* insh = pCkInstrument->GetSubChunk(CHUNK_ID_INSH); |
1377 |
schoenebeck |
800 |
if (insh) { |
1378 |
schoenebeck |
3478 |
insh->SetPos(0); |
1379 |
|
|
|
1380 |
schoenebeck |
800 |
Regions = insh->ReadUint32(); |
1381 |
|
|
insh->Read(&locale, 2, 4); |
1382 |
|
|
} else { // 'insh' chunk missing |
1383 |
|
|
Regions = 0; |
1384 |
|
|
locale.bank = 0; |
1385 |
|
|
locale.instrument = 0; |
1386 |
|
|
} |
1387 |
|
|
|
1388 |
schoenebeck |
2 |
MIDIProgram = locale.instrument; |
1389 |
|
|
IsDrum = locale.bank & DRUM_TYPE_MASK; |
1390 |
|
|
MIDIBankCoarse = (uint8_t) MIDI_BANK_COARSE(locale.bank); |
1391 |
|
|
MIDIBankFine = (uint8_t) MIDI_BANK_FINE(locale.bank); |
1392 |
|
|
MIDIBank = MIDI_BANK_MERGE(MIDIBankCoarse, MIDIBankFine); |
1393 |
|
|
|
1394 |
schoenebeck |
800 |
pRegions = NULL; |
1395 |
schoenebeck |
2 |
} |
1396 |
|
|
|
1397 |
schoenebeck |
3926 |
/** |
1398 |
schoenebeck |
3963 |
* Returns the amount of regions of this instrument. |
1399 |
|
|
* |
1400 |
|
|
* @see GetRegionAt() |
1401 |
|
|
*/ |
1402 |
|
|
size_t Instrument::CountRegions() { |
1403 |
|
|
if (!pRegions) LoadRegions(); |
1404 |
|
|
if (!pRegions) return 0; |
1405 |
|
|
return pRegions->size(); |
1406 |
|
|
} |
1407 |
|
|
|
1408 |
|
|
/** |
1409 |
schoenebeck |
3926 |
* Returns Region at supplied @a pos position within the region list of |
1410 |
|
|
* this instrument. If supplied @a pos is out of bounds then @c NULL is |
1411 |
|
|
* returned. |
1412 |
|
|
* |
1413 |
|
|
* @param pos - position of sought Region in region list |
1414 |
|
|
* @returns pointer address to requested region or @c NULL if @a pos is |
1415 |
|
|
* out of bounds |
1416 |
schoenebeck |
3963 |
* @see CountRegions() |
1417 |
schoenebeck |
3926 |
*/ |
1418 |
|
|
Region* Instrument::GetRegionAt(size_t pos) { |
1419 |
|
|
if (!pRegions) LoadRegions(); |
1420 |
|
|
if (!pRegions) return NULL; |
1421 |
|
|
if (pos >= pRegions->size()) return NULL; |
1422 |
|
|
return (*pRegions)[pos]; |
1423 |
|
|
} |
1424 |
|
|
|
1425 |
|
|
/** |
1426 |
|
|
* Returns the first Region of the instrument. You have to call this |
1427 |
|
|
* method once before you use GetNextRegion(). |
1428 |
|
|
* |
1429 |
|
|
* @returns pointer address to first region or NULL if there is none |
1430 |
|
|
* @see GetNextRegion() |
1431 |
|
|
* @deprecated This method is not reentrant-safe, use GetRegionAt() |
1432 |
|
|
* instead. |
1433 |
|
|
*/ |
1434 |
schoenebeck |
2 |
Region* Instrument::GetFirstRegion() { |
1435 |
|
|
if (!pRegions) LoadRegions(); |
1436 |
|
|
if (!pRegions) return NULL; |
1437 |
|
|
RegionsIterator = pRegions->begin(); |
1438 |
|
|
return (RegionsIterator != pRegions->end()) ? *RegionsIterator : NULL; |
1439 |
|
|
} |
1440 |
|
|
|
1441 |
schoenebeck |
3926 |
/** |
1442 |
|
|
* Returns the next Region of the instrument. You have to call |
1443 |
|
|
* GetFirstRegion() once before you can use this method. By calling this |
1444 |
|
|
* method multiple times it iterates through the available Regions. |
1445 |
|
|
* |
1446 |
|
|
* @returns pointer address to the next region or NULL if end reached |
1447 |
|
|
* @see GetFirstRegion() |
1448 |
|
|
* @deprecated This method is not reentrant-safe, use GetRegionAt() |
1449 |
|
|
* instead. |
1450 |
|
|
*/ |
1451 |
schoenebeck |
2 |
Region* Instrument::GetNextRegion() { |
1452 |
|
|
if (!pRegions) return NULL; |
1453 |
|
|
RegionsIterator++; |
1454 |
|
|
return (RegionsIterator != pRegions->end()) ? *RegionsIterator : NULL; |
1455 |
|
|
} |
1456 |
|
|
|
1457 |
|
|
void Instrument::LoadRegions() { |
1458 |
schoenebeck |
823 |
if (!pRegions) pRegions = new RegionList; |
1459 |
schoenebeck |
2 |
RIFF::List* lrgn = pCkInstrument->GetSubList(LIST_TYPE_LRGN); |
1460 |
schoenebeck |
823 |
if (lrgn) { |
1461 |
|
|
uint32_t regionCkType = (lrgn->GetSubList(LIST_TYPE_RGN2)) ? LIST_TYPE_RGN2 : LIST_TYPE_RGN; // prefer regions level 2 |
1462 |
schoenebeck |
3922 |
size_t i = 0; |
1463 |
|
|
for (RIFF::List* rgn = lrgn->GetSubListAt(i); rgn; |
1464 |
|
|
rgn = lrgn->GetSubListAt(++i)) |
1465 |
|
|
{ |
1466 |
schoenebeck |
823 |
if (rgn->GetListType() == regionCkType) { |
1467 |
|
|
pRegions->push_back(new Region(this, rgn)); |
1468 |
|
|
} |
1469 |
schoenebeck |
2 |
} |
1470 |
|
|
} |
1471 |
|
|
} |
1472 |
|
|
|
1473 |
schoenebeck |
800 |
Region* Instrument::AddRegion() { |
1474 |
schoenebeck |
823 |
if (!pRegions) LoadRegions(); |
1475 |
schoenebeck |
800 |
RIFF::List* lrgn = pCkInstrument->GetSubList(LIST_TYPE_LRGN); |
1476 |
|
|
if (!lrgn) lrgn = pCkInstrument->AddSubList(LIST_TYPE_LRGN); |
1477 |
|
|
RIFF::List* rgn = lrgn->AddSubList(LIST_TYPE_RGN); |
1478 |
|
|
Region* pNewRegion = new Region(this, rgn); |
1479 |
schoenebeck |
3976 |
const size_t idxIt = RegionsIterator - pRegions->begin(); |
1480 |
schoenebeck |
800 |
pRegions->push_back(pNewRegion); |
1481 |
schoenebeck |
3976 |
RegionsIterator = pRegions->begin() + std::min(idxIt, pRegions->size()); // avoid iterator invalidation |
1482 |
schoenebeck |
3053 |
Regions = (uint32_t) pRegions->size(); |
1483 |
schoenebeck |
800 |
return pNewRegion; |
1484 |
|
|
} |
1485 |
|
|
|
1486 |
persson |
1102 |
void Instrument::MoveRegion(Region* pSrc, Region* pDst) { |
1487 |
|
|
RIFF::List* lrgn = pCkInstrument->GetSubList(LIST_TYPE_LRGN); |
1488 |
schoenebeck |
2698 |
lrgn->MoveSubChunk(pSrc->pCkRegion, (RIFF::Chunk*) (pDst ? pDst->pCkRegion : 0)); |
1489 |
schoenebeck |
3926 |
for (size_t i = 0; i < pRegions->size(); ++i) { |
1490 |
|
|
if ((*pRegions)[i] == pSrc) { |
1491 |
schoenebeck |
3976 |
const size_t idxIt = RegionsIterator - pRegions->begin(); |
1492 |
schoenebeck |
3926 |
pRegions->erase(pRegions->begin() + i); |
1493 |
|
|
RegionList::iterator iter = find(pRegions->begin(), pRegions->end(), pDst); |
1494 |
|
|
pRegions->insert(iter, pSrc); |
1495 |
schoenebeck |
3976 |
RegionsIterator = pRegions->begin() + std::min(idxIt, pRegions->size()); // avoid iterator invalidation |
1496 |
schoenebeck |
3926 |
} |
1497 |
|
|
} |
1498 |
persson |
1102 |
} |
1499 |
|
|
|
1500 |
schoenebeck |
800 |
void Instrument::DeleteRegion(Region* pRegion) { |
1501 |
schoenebeck |
802 |
if (!pRegions) return; |
1502 |
schoenebeck |
800 |
RegionList::iterator iter = find(pRegions->begin(), pRegions->end(), pRegion); |
1503 |
|
|
if (iter == pRegions->end()) return; |
1504 |
schoenebeck |
3976 |
const size_t idxIt = RegionsIterator - pRegions->begin(); |
1505 |
schoenebeck |
800 |
pRegions->erase(iter); |
1506 |
schoenebeck |
3976 |
RegionsIterator = pRegions->begin() + std::min(idxIt, pRegions->size()); // avoid iterator invalidation |
1507 |
schoenebeck |
3053 |
Regions = (uint32_t) pRegions->size(); |
1508 |
schoenebeck |
3478 |
pRegion->DeleteChunks(); |
1509 |
schoenebeck |
800 |
delete pRegion; |
1510 |
|
|
} |
1511 |
|
|
|
1512 |
|
|
/** |
1513 |
|
|
* Apply Instrument with all its Regions to the respective RIFF chunks. |
1514 |
|
|
* You have to call File::Save() to make changes persistent. |
1515 |
|
|
* |
1516 |
schoenebeck |
2682 |
* @param pProgress - callback function for progress notification |
1517 |
schoenebeck |
800 |
* @throws Exception - on errors |
1518 |
|
|
*/ |
1519 |
schoenebeck |
2682 |
void Instrument::UpdateChunks(progress_t* pProgress) { |
1520 |
schoenebeck |
800 |
// first update base classes' chunks |
1521 |
schoenebeck |
2682 |
Resource::UpdateChunks(pProgress); |
1522 |
|
|
Articulator::UpdateChunks(pProgress); |
1523 |
schoenebeck |
800 |
// make sure 'insh' chunk exists |
1524 |
|
|
RIFF::Chunk* insh = pCkInstrument->GetSubChunk(CHUNK_ID_INSH); |
1525 |
|
|
if (!insh) insh = pCkInstrument->AddSubChunk(CHUNK_ID_INSH, 12); |
1526 |
|
|
uint8_t* pData = (uint8_t*) insh->LoadChunkData(); |
1527 |
|
|
// update 'insh' chunk |
1528 |
schoenebeck |
3053 |
Regions = (pRegions) ? uint32_t(pRegions->size()) : 0; |
1529 |
schoenebeck |
800 |
midi_locale_t locale; |
1530 |
|
|
locale.instrument = MIDIProgram; |
1531 |
|
|
locale.bank = MIDI_BANK_ENCODE(MIDIBankCoarse, MIDIBankFine); |
1532 |
|
|
locale.bank = (IsDrum) ? locale.bank | DRUM_TYPE_MASK : locale.bank & (~DRUM_TYPE_MASK); |
1533 |
|
|
MIDIBank = MIDI_BANK_MERGE(MIDIBankCoarse, MIDIBankFine); // just a sync, when we're at it |
1534 |
persson |
1179 |
store32(&pData[0], Regions); |
1535 |
|
|
store32(&pData[4], locale.bank); |
1536 |
|
|
store32(&pData[8], locale.instrument); |
1537 |
schoenebeck |
800 |
// update Region's chunks |
1538 |
schoenebeck |
804 |
if (!pRegions) return; |
1539 |
schoenebeck |
800 |
RegionList::iterator iter = pRegions->begin(); |
1540 |
|
|
RegionList::iterator end = pRegions->end(); |
1541 |
schoenebeck |
2682 |
for (int i = 0; iter != end; ++iter, ++i) { |
1542 |
schoenebeck |
3488 |
if (pProgress) { |
1543 |
|
|
// divide local progress into subprogress |
1544 |
|
|
progress_t subprogress; |
1545 |
|
|
__divide_progress(pProgress, &subprogress, pRegions->size(), i); |
1546 |
|
|
// do the actual work |
1547 |
|
|
(*iter)->UpdateChunks(&subprogress); |
1548 |
|
|
} else |
1549 |
|
|
(*iter)->UpdateChunks(NULL); |
1550 |
schoenebeck |
800 |
} |
1551 |
schoenebeck |
3488 |
if (pProgress) |
1552 |
|
|
__notify_progress(pProgress, 1.0); // notify done |
1553 |
schoenebeck |
800 |
} |
1554 |
|
|
|
1555 |
|
|
/** @brief Destructor. |
1556 |
|
|
* |
1557 |
schoenebeck |
3478 |
* Frees all memory occupied by this instrument. |
1558 |
schoenebeck |
800 |
*/ |
1559 |
schoenebeck |
2 |
Instrument::~Instrument() { |
1560 |
|
|
if (pRegions) { |
1561 |
|
|
RegionList::iterator iter = pRegions->begin(); |
1562 |
|
|
RegionList::iterator end = pRegions->end(); |
1563 |
|
|
while (iter != end) { |
1564 |
|
|
delete *iter; |
1565 |
|
|
iter++; |
1566 |
|
|
} |
1567 |
|
|
delete pRegions; |
1568 |
|
|
} |
1569 |
|
|
} |
1570 |
schoenebeck |
3478 |
|
1571 |
|
|
/** @brief Remove all RIFF chunks associated with this Instrument object. |
1572 |
|
|
* |
1573 |
|
|
* See Storage::DeleteChunks() for details. |
1574 |
|
|
*/ |
1575 |
|
|
void Instrument::DeleteChunks() { |
1576 |
|
|
// handle base classes |
1577 |
|
|
Resource::DeleteChunks(); |
1578 |
|
|
Articulator::DeleteChunks(); |
1579 |
|
|
|
1580 |
|
|
// handle RIFF chunks of members |
1581 |
|
|
if (pRegions) { |
1582 |
|
|
RegionList::iterator it = pRegions->begin(); |
1583 |
|
|
RegionList::iterator end = pRegions->end(); |
1584 |
|
|
for (; it != end; ++it) |
1585 |
|
|
(*it)->DeleteChunks(); |
1586 |
|
|
} |
1587 |
|
|
|
1588 |
|
|
// handle own RIFF chunks |
1589 |
|
|
if (pCkInstrument) { |
1590 |
|
|
RIFF::List* pParent = pCkInstrument->GetParent(); |
1591 |
|
|
pParent->DeleteSubChunk(pCkInstrument); |
1592 |
|
|
pCkInstrument = NULL; |
1593 |
|
|
} |
1594 |
|
|
} |
1595 |
|
|
|
1596 |
schoenebeck |
2394 |
void Instrument::CopyAssignCore(const Instrument* orig) { |
1597 |
|
|
// handle base classes |
1598 |
|
|
Resource::CopyAssign(orig); |
1599 |
|
|
Articulator::CopyAssign(orig); |
1600 |
|
|
// handle actual own attributes of this class |
1601 |
|
|
// (the trivial ones) |
1602 |
|
|
IsDrum = orig->IsDrum; |
1603 |
|
|
MIDIBank = orig->MIDIBank; |
1604 |
|
|
MIDIBankCoarse = orig->MIDIBankCoarse; |
1605 |
|
|
MIDIBankFine = orig->MIDIBankFine; |
1606 |
|
|
MIDIProgram = orig->MIDIProgram; |
1607 |
|
|
} |
1608 |
|
|
|
1609 |
|
|
/** |
1610 |
|
|
* Make a (semi) deep copy of the Instrument object given by @a orig and assign |
1611 |
|
|
* it to this object. |
1612 |
|
|
* |
1613 |
|
|
* Note that all sample pointers referenced by @a orig are simply copied as |
1614 |
|
|
* memory address. Thus the respective samples are shared, not duplicated! |
1615 |
|
|
* |
1616 |
|
|
* @param orig - original Instrument object to be copied from |
1617 |
|
|
*/ |
1618 |
|
|
void Instrument::CopyAssign(const Instrument* orig) { |
1619 |
|
|
CopyAssignCore(orig); |
1620 |
|
|
// delete all regions first |
1621 |
schoenebeck |
3927 |
while (Regions) DeleteRegion(GetRegionAt(0)); |
1622 |
schoenebeck |
2394 |
// now recreate and copy regions |
1623 |
|
|
{ |
1624 |
|
|
RegionList::const_iterator it = orig->pRegions->begin(); |
1625 |
|
|
for (int i = 0; i < orig->Regions; ++i, ++it) { |
1626 |
|
|
Region* dstRgn = AddRegion(); |
1627 |
|
|
//NOTE: Region does semi-deep copy ! |
1628 |
|
|
dstRgn->CopyAssign(*it); |
1629 |
|
|
} |
1630 |
|
|
} |
1631 |
|
|
} |
1632 |
schoenebeck |
2 |
|
1633 |
|
|
|
1634 |
|
|
// *************** File *************** |
1635 |
|
|
// * |
1636 |
|
|
|
1637 |
schoenebeck |
800 |
/** @brief Constructor. |
1638 |
|
|
* |
1639 |
|
|
* Default constructor, use this to create an empty DLS file. You have |
1640 |
|
|
* to add samples, instruments and finally call Save() to actually write |
1641 |
|
|
* a DLS file. |
1642 |
|
|
*/ |
1643 |
persson |
1184 |
File::File() : Resource(NULL, pRIFF = new RIFF::File(RIFF_TYPE_DLS)) { |
1644 |
|
|
pRIFF->SetByteOrder(RIFF::endian_little); |
1645 |
schoenebeck |
3463 |
bOwningRiff = true; |
1646 |
schoenebeck |
800 |
pVersion = new version_t; |
1647 |
|
|
pVersion->major = 0; |
1648 |
|
|
pVersion->minor = 0; |
1649 |
|
|
pVersion->release = 0; |
1650 |
|
|
pVersion->build = 0; |
1651 |
|
|
|
1652 |
|
|
Instruments = 0; |
1653 |
|
|
WavePoolCount = 0; |
1654 |
|
|
pWavePoolTable = NULL; |
1655 |
|
|
pWavePoolTableHi = NULL; |
1656 |
|
|
WavePoolHeaderSize = 8; |
1657 |
|
|
|
1658 |
|
|
pSamples = NULL; |
1659 |
|
|
pInstruments = NULL; |
1660 |
|
|
|
1661 |
|
|
b64BitWavePoolOffsets = false; |
1662 |
|
|
} |
1663 |
|
|
|
1664 |
|
|
/** @brief Constructor. |
1665 |
|
|
* |
1666 |
|
|
* Load an existing DLS file. |
1667 |
|
|
* |
1668 |
|
|
* @param pRIFF - pointer to a RIFF file which is actually the DLS file |
1669 |
|
|
* to load |
1670 |
|
|
* @throws Exception if given file is not a DLS file, expected chunks |
1671 |
|
|
* are missing |
1672 |
|
|
*/ |
1673 |
schoenebeck |
2 |
File::File(RIFF::File* pRIFF) : Resource(NULL, pRIFF) { |
1674 |
|
|
if (!pRIFF) throw DLS::Exception("NULL pointer reference to RIFF::File object."); |
1675 |
|
|
this->pRIFF = pRIFF; |
1676 |
schoenebeck |
3463 |
bOwningRiff = false; |
1677 |
schoenebeck |
2 |
RIFF::Chunk* ckVersion = pRIFF->GetSubChunk(CHUNK_ID_VERS); |
1678 |
|
|
if (ckVersion) { |
1679 |
schoenebeck |
3478 |
ckVersion->SetPos(0); |
1680 |
|
|
|
1681 |
schoenebeck |
2 |
pVersion = new version_t; |
1682 |
schoenebeck |
11 |
ckVersion->Read(pVersion, 4, 2); |
1683 |
schoenebeck |
2 |
} |
1684 |
|
|
else pVersion = NULL; |
1685 |
|
|
|
1686 |
|
|
RIFF::Chunk* colh = pRIFF->GetSubChunk(CHUNK_ID_COLH); |
1687 |
|
|
if (!colh) throw DLS::Exception("Mandatory chunks in RIFF list chunk not found."); |
1688 |
schoenebeck |
3478 |
colh->SetPos(0); |
1689 |
schoenebeck |
2 |
Instruments = colh->ReadUint32(); |
1690 |
|
|
|
1691 |
|
|
RIFF::Chunk* ptbl = pRIFF->GetSubChunk(CHUNK_ID_PTBL); |
1692 |
persson |
902 |
if (!ptbl) { // pool table is missing - this is probably an ".art" file |
1693 |
|
|
WavePoolCount = 0; |
1694 |
|
|
pWavePoolTable = NULL; |
1695 |
|
|
pWavePoolTableHi = NULL; |
1696 |
|
|
WavePoolHeaderSize = 8; |
1697 |
|
|
b64BitWavePoolOffsets = false; |
1698 |
|
|
} else { |
1699 |
schoenebeck |
3478 |
ptbl->SetPos(0); |
1700 |
|
|
|
1701 |
persson |
902 |
WavePoolHeaderSize = ptbl->ReadUint32(); |
1702 |
|
|
WavePoolCount = ptbl->ReadUint32(); |
1703 |
|
|
pWavePoolTable = new uint32_t[WavePoolCount]; |
1704 |
|
|
pWavePoolTableHi = new uint32_t[WavePoolCount]; |
1705 |
|
|
ptbl->SetPos(WavePoolHeaderSize); |
1706 |
schoenebeck |
2 |
|
1707 |
persson |
902 |
// Check for 64 bit offsets (used in gig v3 files) |
1708 |
|
|
b64BitWavePoolOffsets = (ptbl->GetSize() - WavePoolHeaderSize == WavePoolCount * 8); |
1709 |
|
|
if (b64BitWavePoolOffsets) { |
1710 |
|
|
for (int i = 0 ; i < WavePoolCount ; i++) { |
1711 |
|
|
pWavePoolTableHi[i] = ptbl->ReadUint32(); |
1712 |
|
|
pWavePoolTable[i] = ptbl->ReadUint32(); |
1713 |
schoenebeck |
2909 |
//NOTE: disabled this 2GB check, not sure why this check was still left here (Christian, 2016-05-12) |
1714 |
|
|
//if (pWavePoolTable[i] & 0x80000000) |
1715 |
|
|
// throw DLS::Exception("Files larger than 2 GB not yet supported"); |
1716 |
persson |
902 |
} |
1717 |
|
|
} else { // conventional 32 bit offsets |
1718 |
|
|
ptbl->Read(pWavePoolTable, WavePoolCount, sizeof(uint32_t)); |
1719 |
|
|
for (int i = 0 ; i < WavePoolCount ; i++) pWavePoolTableHi[i] = 0; |
1720 |
schoenebeck |
317 |
} |
1721 |
persson |
666 |
} |
1722 |
schoenebeck |
317 |
|
1723 |
schoenebeck |
2 |
pSamples = NULL; |
1724 |
|
|
pInstruments = NULL; |
1725 |
|
|
} |
1726 |
|
|
|
1727 |
|
|
File::~File() { |
1728 |
|
|
if (pInstruments) { |
1729 |
|
|
InstrumentList::iterator iter = pInstruments->begin(); |
1730 |
|
|
InstrumentList::iterator end = pInstruments->end(); |
1731 |
|
|
while (iter != end) { |
1732 |
|
|
delete *iter; |
1733 |
|
|
iter++; |
1734 |
|
|
} |
1735 |
|
|
delete pInstruments; |
1736 |
|
|
} |
1737 |
|
|
|
1738 |
|
|
if (pSamples) { |
1739 |
|
|
SampleList::iterator iter = pSamples->begin(); |
1740 |
|
|
SampleList::iterator end = pSamples->end(); |
1741 |
|
|
while (iter != end) { |
1742 |
|
|
delete *iter; |
1743 |
|
|
iter++; |
1744 |
|
|
} |
1745 |
|
|
delete pSamples; |
1746 |
|
|
} |
1747 |
|
|
|
1748 |
|
|
if (pWavePoolTable) delete[] pWavePoolTable; |
1749 |
persson |
666 |
if (pWavePoolTableHi) delete[] pWavePoolTableHi; |
1750 |
schoenebeck |
2 |
if (pVersion) delete pVersion; |
1751 |
persson |
834 |
for (std::list<RIFF::File*>::iterator i = ExtensionFiles.begin() ; i != ExtensionFiles.end() ; i++) |
1752 |
|
|
delete *i; |
1753 |
schoenebeck |
3463 |
if (bOwningRiff) |
1754 |
|
|
delete pRIFF; |
1755 |
schoenebeck |
2 |
} |
1756 |
|
|
|
1757 |
schoenebeck |
3928 |
/** |
1758 |
|
|
* Returns Sample object of @a index. |
1759 |
|
|
* |
1760 |
|
|
* @param index - position of sample in sample list (0..n) |
1761 |
|
|
* @returns sample object or NULL if index is out of bounds |
1762 |
|
|
*/ |
1763 |
|
|
Sample* File::GetSample(size_t index) { |
1764 |
|
|
if (!pSamples) LoadSamples(); |
1765 |
|
|
if (!pSamples) return NULL; |
1766 |
|
|
if (index >= pSamples->size()) return NULL; |
1767 |
|
|
return (*pSamples)[index]; |
1768 |
|
|
} |
1769 |
|
|
|
1770 |
|
|
/** |
1771 |
|
|
* Returns a pointer to the first <i>Sample</i> object of the file, |
1772 |
|
|
* <i>NULL</i> otherwise. |
1773 |
|
|
* |
1774 |
|
|
* @deprecated This method is not reentrant-safe, use GetSample() |
1775 |
|
|
* instead. |
1776 |
|
|
*/ |
1777 |
schoenebeck |
2 |
Sample* File::GetFirstSample() { |
1778 |
|
|
if (!pSamples) LoadSamples(); |
1779 |
|
|
if (!pSamples) return NULL; |
1780 |
|
|
SamplesIterator = pSamples->begin(); |
1781 |
|
|
return (SamplesIterator != pSamples->end()) ? *SamplesIterator : NULL; |
1782 |
|
|
} |
1783 |
|
|
|
1784 |
schoenebeck |
3928 |
/** |
1785 |
|
|
* Returns a pointer to the next <i>Sample</i> object of the file, |
1786 |
|
|
* <i>NULL</i> otherwise. |
1787 |
|
|
* |
1788 |
|
|
* @deprecated This method is not reentrant-safe, use GetSample() |
1789 |
|
|
* instead. |
1790 |
|
|
*/ |
1791 |
schoenebeck |
2 |
Sample* File::GetNextSample() { |
1792 |
|
|
if (!pSamples) return NULL; |
1793 |
|
|
SamplesIterator++; |
1794 |
|
|
return (SamplesIterator != pSamples->end()) ? *SamplesIterator : NULL; |
1795 |
|
|
} |
1796 |
|
|
|
1797 |
|
|
void File::LoadSamples() { |
1798 |
schoenebeck |
823 |
if (!pSamples) pSamples = new SampleList; |
1799 |
schoenebeck |
2 |
RIFF::List* wvpl = pRIFF->GetSubList(LIST_TYPE_WVPL); |
1800 |
|
|
if (wvpl) { |
1801 |
schoenebeck |
3478 |
file_offset_t wvplFileOffset = wvpl->GetFilePos() - |
1802 |
|
|
wvpl->GetPos(); // should be zero, but just to be sure |
1803 |
schoenebeck |
3922 |
size_t i = 0; |
1804 |
|
|
for (RIFF::List* wave = wvpl->GetSubListAt(i); wave; |
1805 |
|
|
wave = wvpl->GetSubListAt(++i)) |
1806 |
|
|
{ |
1807 |
schoenebeck |
2 |
if (wave->GetListType() == LIST_TYPE_WAVE) { |
1808 |
schoenebeck |
3478 |
file_offset_t waveFileOffset = wave->GetFilePos() - |
1809 |
|
|
wave->GetPos(); // should be zero, but just to be sure |
1810 |
schoenebeck |
2 |
pSamples->push_back(new Sample(this, wave, waveFileOffset - wvplFileOffset)); |
1811 |
|
|
} |
1812 |
|
|
} |
1813 |
|
|
} |
1814 |
|
|
else { // Seen a dwpl list chunk instead of a wvpl list chunk in some file (officially not DLS compliant) |
1815 |
|
|
RIFF::List* dwpl = pRIFF->GetSubList(LIST_TYPE_DWPL); |
1816 |
|
|
if (dwpl) { |
1817 |
schoenebeck |
3478 |
file_offset_t dwplFileOffset = dwpl->GetFilePos() - |
1818 |
|
|
dwpl->GetPos(); // should be zero, but just to be sure |
1819 |
schoenebeck |
3922 |
size_t i = 0; |
1820 |
|
|
for (RIFF::List* wave = dwpl->GetSubListAt(i); wave; |
1821 |
|
|
wave = dwpl->GetSubListAt(++i)) |
1822 |
|
|
{ |
1823 |
schoenebeck |
2 |
if (wave->GetListType() == LIST_TYPE_WAVE) { |
1824 |
schoenebeck |
3478 |
file_offset_t waveFileOffset = wave->GetFilePos() - |
1825 |
|
|
wave->GetPos(); // should be zero, but just to be sure |
1826 |
schoenebeck |
2 |
pSamples->push_back(new Sample(this, wave, waveFileOffset - dwplFileOffset)); |
1827 |
|
|
} |
1828 |
|
|
} |
1829 |
|
|
} |
1830 |
|
|
} |
1831 |
|
|
} |
1832 |
|
|
|
1833 |
schoenebeck |
800 |
/** @brief Add a new sample. |
1834 |
|
|
* |
1835 |
|
|
* This will create a new Sample object for the DLS file. You have to |
1836 |
|
|
* call Save() to make this persistent to the file. |
1837 |
|
|
* |
1838 |
|
|
* @returns pointer to new Sample object |
1839 |
|
|
*/ |
1840 |
|
|
Sample* File::AddSample() { |
1841 |
schoenebeck |
809 |
if (!pSamples) LoadSamples(); |
1842 |
schoenebeck |
800 |
__ensureMandatoryChunksExist(); |
1843 |
|
|
RIFF::List* wvpl = pRIFF->GetSubList(LIST_TYPE_WVPL); |
1844 |
|
|
// create new Sample object and its respective 'wave' list chunk |
1845 |
|
|
RIFF::List* wave = wvpl->AddSubList(LIST_TYPE_WAVE); |
1846 |
|
|
Sample* pSample = new Sample(this, wave, 0 /*arbitrary value, we update offsets when we save*/); |
1847 |
schoenebeck |
3974 |
const size_t idxIt = SamplesIterator - pSamples->begin(); |
1848 |
schoenebeck |
800 |
pSamples->push_back(pSample); |
1849 |
schoenebeck |
3974 |
SamplesIterator = pSamples->begin() + std::min(idxIt, pSamples->size()); // avoid iterator invalidation |
1850 |
schoenebeck |
800 |
return pSample; |
1851 |
|
|
} |
1852 |
|
|
|
1853 |
|
|
/** @brief Delete a sample. |
1854 |
|
|
* |
1855 |
|
|
* This will delete the given Sample object from the DLS file. You have |
1856 |
|
|
* to call Save() to make this persistent to the file. |
1857 |
|
|
* |
1858 |
|
|
* @param pSample - sample to delete |
1859 |
|
|
*/ |
1860 |
|
|
void File::DeleteSample(Sample* pSample) { |
1861 |
schoenebeck |
802 |
if (!pSamples) return; |
1862 |
schoenebeck |
800 |
SampleList::iterator iter = find(pSamples->begin(), pSamples->end(), pSample); |
1863 |
|
|
if (iter == pSamples->end()) return; |
1864 |
schoenebeck |
3974 |
const size_t idxIt = SamplesIterator - pSamples->begin(); |
1865 |
schoenebeck |
800 |
pSamples->erase(iter); |
1866 |
schoenebeck |
3974 |
SamplesIterator = pSamples->begin() + std::min(idxIt, pSamples->size()); // avoid iterator invalidation |
1867 |
schoenebeck |
3478 |
pSample->DeleteChunks(); |
1868 |
schoenebeck |
800 |
delete pSample; |
1869 |
|
|
} |
1870 |
|
|
|
1871 |
schoenebeck |
3941 |
/** |
1872 |
|
|
* Returns the instrument with the given @a index from the list of |
1873 |
|
|
* instruments of this file. |
1874 |
|
|
* |
1875 |
|
|
* @param index - number of the sought instrument (0..n) |
1876 |
|
|
* @returns sought instrument or NULL if there's no such instrument |
1877 |
|
|
*/ |
1878 |
|
|
Instrument* File::GetInstrument(size_t index) { |
1879 |
|
|
if (!pInstruments) LoadInstruments(); |
1880 |
|
|
if (!pInstruments) return NULL; |
1881 |
|
|
if (index >= pInstruments->size()) return NULL; |
1882 |
|
|
return (*pInstruments)[index]; |
1883 |
|
|
} |
1884 |
|
|
|
1885 |
|
|
/** |
1886 |
|
|
* Returns a pointer to the first <i>Instrument</i> object of the file, |
1887 |
|
|
* <i>NULL</i> otherwise. |
1888 |
|
|
* |
1889 |
|
|
* @deprecated This method is not reentrant-safe, use GetInstrument() |
1890 |
|
|
* instead. |
1891 |
|
|
*/ |
1892 |
schoenebeck |
2 |
Instrument* File::GetFirstInstrument() { |
1893 |
|
|
if (!pInstruments) LoadInstruments(); |
1894 |
|
|
if (!pInstruments) return NULL; |
1895 |
|
|
InstrumentsIterator = pInstruments->begin(); |
1896 |
|
|
return (InstrumentsIterator != pInstruments->end()) ? *InstrumentsIterator : NULL; |
1897 |
|
|
} |
1898 |
|
|
|
1899 |
schoenebeck |
3941 |
/** |
1900 |
|
|
* Returns a pointer to the next <i>Instrument</i> object of the file, |
1901 |
|
|
* <i>NULL</i> otherwise. |
1902 |
|
|
* |
1903 |
|
|
* @deprecated This method is not reentrant-safe, use GetInstrument() |
1904 |
|
|
* instead. |
1905 |
|
|
*/ |
1906 |
schoenebeck |
2 |
Instrument* File::GetNextInstrument() { |
1907 |
|
|
if (!pInstruments) return NULL; |
1908 |
|
|
InstrumentsIterator++; |
1909 |
|
|
return (InstrumentsIterator != pInstruments->end()) ? *InstrumentsIterator : NULL; |
1910 |
|
|
} |
1911 |
|
|
|
1912 |
|
|
void File::LoadInstruments() { |
1913 |
schoenebeck |
823 |
if (!pInstruments) pInstruments = new InstrumentList; |
1914 |
schoenebeck |
2 |
RIFF::List* lstInstruments = pRIFF->GetSubList(LIST_TYPE_LINS); |
1915 |
|
|
if (lstInstruments) { |
1916 |
schoenebeck |
3922 |
size_t i = 0; |
1917 |
|
|
for (RIFF::List* lstInstr = lstInstruments->GetSubListAt(i); |
1918 |
|
|
lstInstr; lstInstr = lstInstruments->GetSubListAt(++i)) |
1919 |
|
|
{ |
1920 |
schoenebeck |
2 |
if (lstInstr->GetListType() == LIST_TYPE_INS) { |
1921 |
|
|
pInstruments->push_back(new Instrument(this, lstInstr)); |
1922 |
|
|
} |
1923 |
|
|
} |
1924 |
|
|
} |
1925 |
|
|
} |
1926 |
|
|
|
1927 |
schoenebeck |
800 |
/** @brief Add a new instrument definition. |
1928 |
|
|
* |
1929 |
|
|
* This will create a new Instrument object for the DLS file. You have |
1930 |
|
|
* to call Save() to make this persistent to the file. |
1931 |
|
|
* |
1932 |
|
|
* @returns pointer to new Instrument object |
1933 |
|
|
*/ |
1934 |
|
|
Instrument* File::AddInstrument() { |
1935 |
schoenebeck |
809 |
if (!pInstruments) LoadInstruments(); |
1936 |
schoenebeck |
800 |
__ensureMandatoryChunksExist(); |
1937 |
|
|
RIFF::List* lstInstruments = pRIFF->GetSubList(LIST_TYPE_LINS); |
1938 |
|
|
RIFF::List* lstInstr = lstInstruments->AddSubList(LIST_TYPE_INS); |
1939 |
|
|
Instrument* pInstrument = new Instrument(this, lstInstr); |
1940 |
|
|
pInstruments->push_back(pInstrument); |
1941 |
|
|
return pInstrument; |
1942 |
|
|
} |
1943 |
schoenebeck |
2 |
|
1944 |
schoenebeck |
809 |
/** @brief Delete an instrument. |
1945 |
schoenebeck |
800 |
* |
1946 |
|
|
* This will delete the given Instrument object from the DLS file. You |
1947 |
|
|
* have to call Save() to make this persistent to the file. |
1948 |
|
|
* |
1949 |
|
|
* @param pInstrument - instrument to delete |
1950 |
|
|
*/ |
1951 |
|
|
void File::DeleteInstrument(Instrument* pInstrument) { |
1952 |
schoenebeck |
802 |
if (!pInstruments) return; |
1953 |
schoenebeck |
800 |
InstrumentList::iterator iter = find(pInstruments->begin(), pInstruments->end(), pInstrument); |
1954 |
|
|
if (iter == pInstruments->end()) return; |
1955 |
|
|
pInstruments->erase(iter); |
1956 |
schoenebeck |
3478 |
pInstrument->DeleteChunks(); |
1957 |
schoenebeck |
800 |
delete pInstrument; |
1958 |
|
|
} |
1959 |
schoenebeck |
2 |
|
1960 |
schoenebeck |
2329 |
/** |
1961 |
schoenebeck |
3481 |
* Returns the underlying RIFF::File used for persistency of this DLS::File |
1962 |
|
|
* object. |
1963 |
|
|
*/ |
1964 |
|
|
RIFF::File* File::GetRiffFile() { |
1965 |
|
|
return pRIFF; |
1966 |
|
|
} |
1967 |
|
|
|
1968 |
|
|
/** |
1969 |
schoenebeck |
2329 |
* Returns extension file of given index. Extension files are used |
1970 |
|
|
* sometimes to circumvent the 2 GB file size limit of the RIFF format and |
1971 |
|
|
* of certain operating systems in general. In this case, instead of just |
1972 |
|
|
* using one file, the content is spread among several files with similar |
1973 |
|
|
* file name scheme. This is especially used by some GigaStudio sound |
1974 |
|
|
* libraries. |
1975 |
|
|
* |
1976 |
|
|
* @param index - index of extension file |
1977 |
|
|
* @returns sought extension file, NULL if index out of bounds |
1978 |
|
|
* @see GetFileName() |
1979 |
|
|
*/ |
1980 |
|
|
RIFF::File* File::GetExtensionFile(int index) { |
1981 |
|
|
if (index < 0 || index >= ExtensionFiles.size()) return NULL; |
1982 |
|
|
std::list<RIFF::File*>::iterator iter = ExtensionFiles.begin(); |
1983 |
|
|
for (int i = 0; iter != ExtensionFiles.end(); ++iter, ++i) |
1984 |
|
|
if (i == index) return *iter; |
1985 |
|
|
return NULL; |
1986 |
|
|
} |
1987 |
|
|
|
1988 |
schoenebeck |
2274 |
/** @brief File name of this DLS file. |
1989 |
|
|
* |
1990 |
|
|
* This method returns the file name as it was provided when loading |
1991 |
|
|
* the respective DLS file. However in case the File object associates |
1992 |
|
|
* an empty, that is new DLS file, which was not yet saved to disk, |
1993 |
|
|
* this method will return an empty string. |
1994 |
schoenebeck |
2329 |
* |
1995 |
|
|
* @see GetExtensionFile() |
1996 |
schoenebeck |
2274 |
*/ |
1997 |
|
|
String File::GetFileName() { |
1998 |
|
|
return pRIFF->GetFileName(); |
1999 |
|
|
} |
2000 |
schoenebeck |
2482 |
|
2001 |
|
|
/** |
2002 |
|
|
* You may call this method store a future file name, so you don't have to |
2003 |
|
|
* to pass it to the Save() call later on. |
2004 |
|
|
*/ |
2005 |
|
|
void File::SetFileName(const String& name) { |
2006 |
|
|
pRIFF->SetFileName(name); |
2007 |
|
|
} |
2008 |
schoenebeck |
2274 |
|
2009 |
schoenebeck |
800 |
/** |
2010 |
|
|
* Apply all the DLS file's current instruments, samples and settings to |
2011 |
|
|
* the respective RIFF chunks. You have to call Save() to make changes |
2012 |
|
|
* persistent. |
2013 |
|
|
* |
2014 |
schoenebeck |
2682 |
* @param pProgress - callback function for progress notification |
2015 |
schoenebeck |
800 |
* @throws Exception - on errors |
2016 |
|
|
*/ |
2017 |
schoenebeck |
2682 |
void File::UpdateChunks(progress_t* pProgress) { |
2018 |
schoenebeck |
800 |
// first update base class's chunks |
2019 |
schoenebeck |
2682 |
Resource::UpdateChunks(pProgress); |
2020 |
schoenebeck |
800 |
|
2021 |
|
|
// if version struct exists, update 'vers' chunk |
2022 |
|
|
if (pVersion) { |
2023 |
|
|
RIFF::Chunk* ckVersion = pRIFF->GetSubChunk(CHUNK_ID_VERS); |
2024 |
|
|
if (!ckVersion) ckVersion = pRIFF->AddSubChunk(CHUNK_ID_VERS, 8); |
2025 |
|
|
uint8_t* pData = (uint8_t*) ckVersion->LoadChunkData(); |
2026 |
persson |
1179 |
store16(&pData[0], pVersion->minor); |
2027 |
|
|
store16(&pData[2], pVersion->major); |
2028 |
|
|
store16(&pData[4], pVersion->build); |
2029 |
|
|
store16(&pData[6], pVersion->release); |
2030 |
schoenebeck |
800 |
} |
2031 |
|
|
|
2032 |
|
|
// update 'colh' chunk |
2033 |
schoenebeck |
3053 |
Instruments = (pInstruments) ? uint32_t(pInstruments->size()) : 0; |
2034 |
schoenebeck |
800 |
RIFF::Chunk* colh = pRIFF->GetSubChunk(CHUNK_ID_COLH); |
2035 |
|
|
if (!colh) colh = pRIFF->AddSubChunk(CHUNK_ID_COLH, 4); |
2036 |
|
|
uint8_t* pData = (uint8_t*) colh->LoadChunkData(); |
2037 |
persson |
1179 |
store32(pData, Instruments); |
2038 |
schoenebeck |
800 |
|
2039 |
|
|
// update instrument's chunks |
2040 |
|
|
if (pInstruments) { |
2041 |
schoenebeck |
3488 |
if (pProgress) { |
2042 |
|
|
// divide local progress into subprogress |
2043 |
|
|
progress_t subprogress; |
2044 |
|
|
__divide_progress(pProgress, &subprogress, 20.f, 0.f); // arbitrarily subdivided into 5% of total progress |
2045 |
schoenebeck |
2682 |
|
2046 |
|
|
// do the actual work |
2047 |
schoenebeck |
3488 |
InstrumentList::iterator iter = pInstruments->begin(); |
2048 |
|
|
InstrumentList::iterator end = pInstruments->end(); |
2049 |
|
|
for (int i = 0; iter != end; ++iter, ++i) { |
2050 |
|
|
// divide subprogress into sub-subprogress |
2051 |
|
|
progress_t subsubprogress; |
2052 |
|
|
__divide_progress(&subprogress, &subsubprogress, pInstruments->size(), i); |
2053 |
|
|
// do the actual work |
2054 |
|
|
(*iter)->UpdateChunks(&subsubprogress); |
2055 |
|
|
} |
2056 |
|
|
|
2057 |
|
|
__notify_progress(&subprogress, 1.0); // notify subprogress done |
2058 |
|
|
} else { |
2059 |
|
|
InstrumentList::iterator iter = pInstruments->begin(); |
2060 |
|
|
InstrumentList::iterator end = pInstruments->end(); |
2061 |
|
|
for (int i = 0; iter != end; ++iter, ++i) { |
2062 |
|
|
(*iter)->UpdateChunks(NULL); |
2063 |
|
|
} |
2064 |
schoenebeck |
800 |
} |
2065 |
|
|
} |
2066 |
|
|
|
2067 |
|
|
// update 'ptbl' chunk |
2068 |
schoenebeck |
3053 |
const int iSamples = (pSamples) ? int(pSamples->size()) : 0; |
2069 |
schoenebeck |
2912 |
int iPtblOffsetSize = (b64BitWavePoolOffsets) ? 8 : 4; |
2070 |
schoenebeck |
800 |
RIFF::Chunk* ptbl = pRIFF->GetSubChunk(CHUNK_ID_PTBL); |
2071 |
|
|
if (!ptbl) ptbl = pRIFF->AddSubChunk(CHUNK_ID_PTBL, 1 /*anything, we'll resize*/); |
2072 |
schoenebeck |
2912 |
int iPtblSize = WavePoolHeaderSize + iPtblOffsetSize * iSamples; |
2073 |
schoenebeck |
800 |
ptbl->Resize(iPtblSize); |
2074 |
|
|
pData = (uint8_t*) ptbl->LoadChunkData(); |
2075 |
|
|
WavePoolCount = iSamples; |
2076 |
persson |
1179 |
store32(&pData[4], WavePoolCount); |
2077 |
schoenebeck |
800 |
// we actually update the sample offsets in the pool table when we Save() |
2078 |
|
|
memset(&pData[WavePoolHeaderSize], 0, iPtblSize - WavePoolHeaderSize); |
2079 |
|
|
|
2080 |
|
|
// update sample's chunks |
2081 |
|
|
if (pSamples) { |
2082 |
schoenebeck |
3488 |
if (pProgress) { |
2083 |
|
|
// divide local progress into subprogress |
2084 |
|
|
progress_t subprogress; |
2085 |
|
|
__divide_progress(pProgress, &subprogress, 20.f, 1.f); // arbitrarily subdivided into 95% of total progress |
2086 |
schoenebeck |
2682 |
|
2087 |
|
|
// do the actual work |
2088 |
schoenebeck |
3488 |
SampleList::iterator iter = pSamples->begin(); |
2089 |
|
|
SampleList::iterator end = pSamples->end(); |
2090 |
|
|
for (int i = 0; iter != end; ++iter, ++i) { |
2091 |
|
|
// divide subprogress into sub-subprogress |
2092 |
|
|
progress_t subsubprogress; |
2093 |
|
|
__divide_progress(&subprogress, &subsubprogress, pSamples->size(), i); |
2094 |
|
|
// do the actual work |
2095 |
|
|
(*iter)->UpdateChunks(&subsubprogress); |
2096 |
|
|
} |
2097 |
|
|
|
2098 |
|
|
__notify_progress(&subprogress, 1.0); // notify subprogress done |
2099 |
|
|
} else { |
2100 |
|
|
SampleList::iterator iter = pSamples->begin(); |
2101 |
|
|
SampleList::iterator end = pSamples->end(); |
2102 |
|
|
for (int i = 0; iter != end; ++iter, ++i) { |
2103 |
|
|
(*iter)->UpdateChunks(NULL); |
2104 |
|
|
} |
2105 |
schoenebeck |
800 |
} |
2106 |
|
|
} |
2107 |
schoenebeck |
2682 |
|
2108 |
schoenebeck |
3474 |
// if there are any extension files, gather which ones are regular |
2109 |
|
|
// extension files used as wave pool files (.gx00, .gx01, ... , .gx98) |
2110 |
|
|
// and which one is probably a convolution (GigaPulse) file (always to |
2111 |
|
|
// be saved as .gx99) |
2112 |
|
|
std::list<RIFF::File*> poolFiles; // < for (.gx00, .gx01, ... , .gx98) files |
2113 |
|
|
RIFF::File* pGigaPulseFile = NULL; // < for .gx99 file |
2114 |
|
|
if (!ExtensionFiles.empty()) { |
2115 |
|
|
std::list<RIFF::File*>::iterator it = ExtensionFiles.begin(); |
2116 |
|
|
for (; it != ExtensionFiles.end(); ++it) { |
2117 |
|
|
//FIXME: the .gx99 file is always used by GSt for convolution |
2118 |
|
|
// data (GigaPulse); so we should better detect by subchunk |
2119 |
|
|
// whether the extension file is intended for convolution |
2120 |
|
|
// instead of checkking for a file name, because the latter does |
2121 |
|
|
// not work for saving new gigs created from scratch |
2122 |
|
|
const std::string oldName = (*it)->GetFileName(); |
2123 |
|
|
const bool isGigaPulseFile = (extensionOfPath(oldName) == "gx99"); |
2124 |
|
|
if (isGigaPulseFile) |
2125 |
|
|
pGigaPulseFile = *it; |
2126 |
|
|
else |
2127 |
|
|
poolFiles.push_back(*it); |
2128 |
|
|
} |
2129 |
|
|
} |
2130 |
|
|
|
2131 |
|
|
// update the 'xfil' chunk which describes all extension files (wave |
2132 |
|
|
// pool files) except the .gx99 file |
2133 |
|
|
if (!poolFiles.empty()) { |
2134 |
|
|
const int n = poolFiles.size(); |
2135 |
|
|
const int iHeaderSize = 4; |
2136 |
|
|
const int iEntrySize = 144; |
2137 |
|
|
|
2138 |
|
|
// make sure chunk exists, and with correct size |
2139 |
|
|
RIFF::Chunk* ckXfil = pRIFF->GetSubChunk(CHUNK_ID_XFIL); |
2140 |
|
|
if (ckXfil) |
2141 |
|
|
ckXfil->Resize(iHeaderSize + n * iEntrySize); |
2142 |
|
|
else |
2143 |
|
|
ckXfil = pRIFF->AddSubChunk(CHUNK_ID_XFIL, iHeaderSize + n * iEntrySize); |
2144 |
|
|
|
2145 |
|
|
uint8_t* pData = (uint8_t*) ckXfil->LoadChunkData(); |
2146 |
|
|
|
2147 |
|
|
// re-assemble the chunk's content |
2148 |
|
|
store32(pData, n); |
2149 |
|
|
std::list<RIFF::File*>::iterator itExtFile = poolFiles.begin(); |
2150 |
|
|
for (int i = 0, iOffset = 4; i < n; |
2151 |
|
|
++itExtFile, ++i, iOffset += iEntrySize) |
2152 |
|
|
{ |
2153 |
|
|
// update the filename string and 5 byte extension of each extension file |
2154 |
|
|
std::string file = lastPathComponent( |
2155 |
|
|
(*itExtFile)->GetFileName() |
2156 |
|
|
); |
2157 |
|
|
if (file.length() + 6 > 128) |
2158 |
|
|
throw Exception("Fatal error, extension filename length exceeds 122 byte maximum"); |
2159 |
|
|
uint8_t* pStrings = &pData[iOffset]; |
2160 |
|
|
memset(pStrings, 0, 128); |
2161 |
|
|
memcpy(pStrings, file.c_str(), file.length()); |
2162 |
|
|
pStrings += file.length() + 1; |
2163 |
|
|
std::string ext = file.substr(file.length()-5); |
2164 |
|
|
memcpy(pStrings, ext.c_str(), 5); |
2165 |
|
|
// update the dlsid of the extension file |
2166 |
|
|
uint8_t* pId = &pData[iOffset + 128]; |
2167 |
|
|
dlsid_t id; |
2168 |
|
|
RIFF::Chunk* ckDLSID = (*itExtFile)->GetSubChunk(CHUNK_ID_DLID); |
2169 |
|
|
if (ckDLSID) { |
2170 |
|
|
ckDLSID->Read(&id.ulData1, 1, 4); |
2171 |
|
|
ckDLSID->Read(&id.usData2, 1, 2); |
2172 |
|
|
ckDLSID->Read(&id.usData3, 1, 2); |
2173 |
|
|
ckDLSID->Read(id.abData, 8, 1); |
2174 |
|
|
} else { |
2175 |
|
|
ckDLSID = (*itExtFile)->AddSubChunk(CHUNK_ID_DLID, 16); |
2176 |
|
|
Resource::GenerateDLSID(&id); |
2177 |
|
|
uint8_t* pData = (uint8_t*)ckDLSID->LoadChunkData(); |
2178 |
|
|
store32(&pData[0], id.ulData1); |
2179 |
|
|
store16(&pData[4], id.usData2); |
2180 |
|
|
store16(&pData[6], id.usData3); |
2181 |
|
|
memcpy(&pData[8], id.abData, 8); |
2182 |
|
|
} |
2183 |
|
|
store32(&pId[0], id.ulData1); |
2184 |
|
|
store16(&pId[4], id.usData2); |
2185 |
|
|
store16(&pId[6], id.usData3); |
2186 |
|
|
memcpy(&pId[8], id.abData, 8); |
2187 |
|
|
} |
2188 |
|
|
} else { |
2189 |
|
|
// in case there was a 'xfil' chunk, remove it |
2190 |
|
|
RIFF::Chunk* ckXfil = pRIFF->GetSubChunk(CHUNK_ID_XFIL); |
2191 |
|
|
if (ckXfil) pRIFF->DeleteSubChunk(ckXfil); |
2192 |
|
|
} |
2193 |
|
|
|
2194 |
|
|
// update the 'doxf' chunk which describes a .gx99 extension file |
2195 |
|
|
// which contains convolution data (GigaPulse) |
2196 |
|
|
if (pGigaPulseFile) { |
2197 |
|
|
RIFF::Chunk* ckDoxf = pRIFF->GetSubChunk(CHUNK_ID_DOXF); |
2198 |
|
|
if (!ckDoxf) ckDoxf = pRIFF->AddSubChunk(CHUNK_ID_DOXF, 148); |
2199 |
|
|
|
2200 |
|
|
uint8_t* pData = (uint8_t*) ckDoxf->LoadChunkData(); |
2201 |
|
|
|
2202 |
|
|
// update the dlsid from the extension file |
2203 |
|
|
uint8_t* pId = &pData[132]; |
2204 |
|
|
RIFF::Chunk* ckDLSID = pGigaPulseFile->GetSubChunk(CHUNK_ID_DLID); |
2205 |
|
|
if (!ckDLSID) { //TODO: auto generate DLS ID if missing |
2206 |
|
|
throw Exception("Fatal error, GigaPulse file does not contain a DLS ID chunk"); |
2207 |
|
|
} else { |
2208 |
|
|
dlsid_t id; |
2209 |
|
|
// read DLS ID from extension files's DLS ID chunk |
2210 |
|
|
uint8_t* pData = (uint8_t*) ckDLSID->LoadChunkData(); |
2211 |
|
|
id.ulData1 = load32(&pData[0]); |
2212 |
|
|
id.usData2 = load16(&pData[4]); |
2213 |
|
|
id.usData3 = load16(&pData[6]); |
2214 |
|
|
memcpy(id.abData, &pData[8], 8); |
2215 |
|
|
// store DLS ID to 'doxf' chunk |
2216 |
|
|
store32(&pId[0], id.ulData1); |
2217 |
|
|
store16(&pId[4], id.usData2); |
2218 |
|
|
store16(&pId[6], id.usData3); |
2219 |
|
|
memcpy(&pId[8], id.abData, 8); |
2220 |
|
|
} |
2221 |
|
|
} else { |
2222 |
|
|
// in case there was a 'doxf' chunk, remove it |
2223 |
|
|
RIFF::Chunk* ckDoxf = pRIFF->GetSubChunk(CHUNK_ID_DOXF); |
2224 |
|
|
if (ckDoxf) pRIFF->DeleteSubChunk(ckDoxf); |
2225 |
|
|
} |
2226 |
|
|
|
2227 |
schoenebeck |
2912 |
// the RIFF file to be written might now been grown >= 4GB or might |
2228 |
|
|
// been shrunk < 4GB, so we might need to update the wave pool offset |
2229 |
|
|
// size and thus accordingly we would need to resize the wave pool |
2230 |
|
|
// chunk |
2231 |
|
|
const file_offset_t finalFileSize = pRIFF->GetRequiredFileSize(); |
2232 |
schoenebeck |
3474 |
const bool bRequires64Bit = (finalFileSize >> 32) != 0 || // < native 64 bit gig file |
2233 |
|
|
poolFiles.size() > 0; // < 32 bit gig file where the hi 32 bits are used as extension file nr |
2234 |
schoenebeck |
2912 |
if (b64BitWavePoolOffsets != bRequires64Bit) { |
2235 |
|
|
b64BitWavePoolOffsets = bRequires64Bit; |
2236 |
|
|
iPtblOffsetSize = (b64BitWavePoolOffsets) ? 8 : 4; |
2237 |
|
|
iPtblSize = WavePoolHeaderSize + iPtblOffsetSize * iSamples; |
2238 |
|
|
ptbl->Resize(iPtblSize); |
2239 |
|
|
} |
2240 |
|
|
|
2241 |
schoenebeck |
3488 |
if (pProgress) |
2242 |
|
|
__notify_progress(pProgress, 1.0); // notify done |
2243 |
schoenebeck |
800 |
} |
2244 |
|
|
|
2245 |
|
|
/** @brief Save changes to another file. |
2246 |
|
|
* |
2247 |
|
|
* Make all changes persistent by writing them to another file. |
2248 |
|
|
* <b>Caution:</b> this method is optimized for writing to |
2249 |
|
|
* <b>another</b> file, do not use it to save the changes to the same |
2250 |
|
|
* file! Use Save() (without path argument) in that case instead! |
2251 |
|
|
* Ignoring this might result in a corrupted file! |
2252 |
|
|
* |
2253 |
|
|
* After calling this method, this File object will be associated with |
2254 |
|
|
* the new file (given by \a Path) afterwards. |
2255 |
|
|
* |
2256 |
|
|
* @param Path - path and file name where everything should be written to |
2257 |
schoenebeck |
2682 |
* @param pProgress - optional: callback function for progress notification |
2258 |
schoenebeck |
800 |
*/ |
2259 |
schoenebeck |
2682 |
void File::Save(const String& Path, progress_t* pProgress) { |
2260 |
schoenebeck |
3474 |
// calculate number of tasks to notify progress appropriately |
2261 |
|
|
const size_t nExtFiles = ExtensionFiles.size(); |
2262 |
|
|
const float tasks = 2.f + nExtFiles; |
2263 |
|
|
|
2264 |
|
|
// save extension files (if required) |
2265 |
|
|
if (!ExtensionFiles.empty()) { |
2266 |
|
|
// for assembling path of extension files to be saved to |
2267 |
|
|
const std::string baseName = pathWithoutExtension(Path); |
2268 |
|
|
// save the individual extension files |
2269 |
|
|
std::list<RIFF::File*>::iterator it = ExtensionFiles.begin(); |
2270 |
|
|
for (int i = 0; it != ExtensionFiles.end(); ++i, ++it) { |
2271 |
|
|
//FIXME: the .gx99 file is always used by GSt for convolution |
2272 |
|
|
// data (GigaPulse); so we should better detect by subchunk |
2273 |
|
|
// whether the extension file is intended for convolution |
2274 |
|
|
// instead of checkking for a file name, because the latter does |
2275 |
|
|
// not work for saving new gigs created from scratch |
2276 |
|
|
const std::string oldName = (*it)->GetFileName(); |
2277 |
|
|
const bool isGigaPulseFile = (extensionOfPath(oldName) == "gx99"); |
2278 |
schoenebeck |
3483 |
std::string ext = (isGigaPulseFile) ? ".gx99" : strPrint(".gx%02d", i+1); |
2279 |
|
|
std::string newPath = baseName + ext; |
2280 |
schoenebeck |
3474 |
// save extension file to its new location |
2281 |
schoenebeck |
3488 |
if (pProgress) { |
2282 |
|
|
// divide local progress into subprogress |
2283 |
|
|
progress_t subprogress; |
2284 |
|
|
__divide_progress(pProgress, &subprogress, tasks, 0.f + i); // subdivided into amount of extension files |
2285 |
|
|
// do the actual work |
2286 |
|
|
(*it)->Save(newPath, &subprogress); |
2287 |
|
|
} else |
2288 |
|
|
(*it)->Save(newPath); |
2289 |
schoenebeck |
3474 |
} |
2290 |
|
|
} |
2291 |
|
|
|
2292 |
schoenebeck |
3488 |
if (pProgress) { |
2293 |
schoenebeck |
2682 |
// divide local progress into subprogress |
2294 |
|
|
progress_t subprogress; |
2295 |
schoenebeck |
3474 |
__divide_progress(pProgress, &subprogress, tasks, 1.f + nExtFiles); // arbitrarily subdivided into 50% (minus extension files progress) |
2296 |
schoenebeck |
2682 |
// do the actual work |
2297 |
|
|
UpdateChunks(&subprogress); |
2298 |
schoenebeck |
3488 |
} else |
2299 |
|
|
UpdateChunks(NULL); |
2300 |
|
|
|
2301 |
|
|
if (pProgress) { |
2302 |
schoenebeck |
2682 |
// divide local progress into subprogress |
2303 |
|
|
progress_t subprogress; |
2304 |
schoenebeck |
3474 |
__divide_progress(pProgress, &subprogress, tasks, 2.f + nExtFiles); // arbitrarily subdivided into 50% (minus extension files progress) |
2305 |
schoenebeck |
2682 |
// do the actual work |
2306 |
|
|
pRIFF->Save(Path, &subprogress); |
2307 |
schoenebeck |
3488 |
} else |
2308 |
|
|
pRIFF->Save(Path); |
2309 |
|
|
|
2310 |
schoenebeck |
2609 |
UpdateFileOffsets(); |
2311 |
schoenebeck |
3488 |
|
2312 |
|
|
if (pProgress) |
2313 |
|
|
__notify_progress(pProgress, 1.0); // notify done |
2314 |
schoenebeck |
800 |
} |
2315 |
|
|
|
2316 |
|
|
/** @brief Save changes to same file. |
2317 |
|
|
* |
2318 |
|
|
* Make all changes persistent by writing them to the actual (same) |
2319 |
|
|
* file. The file might temporarily grow to a higher size than it will |
2320 |
|
|
* have at the end of the saving process. |
2321 |
|
|
* |
2322 |
schoenebeck |
2682 |
* @param pProgress - optional: callback function for progress notification |
2323 |
schoenebeck |
3048 |
* @throws RIFF::Exception if any kind of IO error occurred |
2324 |
|
|
* @throws DLS::Exception if any kind of DLS specific error occurred |
2325 |
schoenebeck |
800 |
*/ |
2326 |
schoenebeck |
2682 |
void File::Save(progress_t* pProgress) { |
2327 |
schoenebeck |
3474 |
// calculate number of tasks to notify progress appropriately |
2328 |
|
|
const size_t nExtFiles = ExtensionFiles.size(); |
2329 |
|
|
const float tasks = 2.f + nExtFiles; |
2330 |
|
|
|
2331 |
|
|
// save extension files (if required) |
2332 |
|
|
if (!ExtensionFiles.empty()) { |
2333 |
|
|
std::list<RIFF::File*>::iterator it = ExtensionFiles.begin(); |
2334 |
|
|
for (int i = 0; it != ExtensionFiles.end(); ++i, ++it) { |
2335 |
|
|
// save extension file |
2336 |
schoenebeck |
3488 |
if (pProgress) { |
2337 |
|
|
// divide local progress into subprogress |
2338 |
|
|
progress_t subprogress; |
2339 |
|
|
__divide_progress(pProgress, &subprogress, tasks, 0.f + i); // subdivided into amount of extension files |
2340 |
|
|
// do the actual work |
2341 |
|
|
(*it)->Save(&subprogress); |
2342 |
|
|
} else |
2343 |
|
|
(*it)->Save(); |
2344 |
schoenebeck |
3474 |
} |
2345 |
|
|
} |
2346 |
|
|
|
2347 |
schoenebeck |
3488 |
if (pProgress) { |
2348 |
schoenebeck |
2682 |
// divide local progress into subprogress |
2349 |
|
|
progress_t subprogress; |
2350 |
schoenebeck |
3474 |
__divide_progress(pProgress, &subprogress, tasks, 1.f + nExtFiles); // arbitrarily subdivided into 50% (minus extension files progress) |
2351 |
schoenebeck |
2682 |
// do the actual work |
2352 |
|
|
UpdateChunks(&subprogress); |
2353 |
schoenebeck |
3488 |
} else |
2354 |
|
|
UpdateChunks(NULL); |
2355 |
|
|
|
2356 |
|
|
if (pProgress) { |
2357 |
schoenebeck |
2682 |
// divide local progress into subprogress |
2358 |
|
|
progress_t subprogress; |
2359 |
schoenebeck |
3474 |
__divide_progress(pProgress, &subprogress, tasks, 2.f + nExtFiles); // arbitrarily subdivided into 50% (minus extension files progress) |
2360 |
schoenebeck |
2682 |
// do the actual work |
2361 |
|
|
pRIFF->Save(&subprogress); |
2362 |
schoenebeck |
3488 |
} else |
2363 |
|
|
pRIFF->Save(); |
2364 |
|
|
|
2365 |
schoenebeck |
2609 |
UpdateFileOffsets(); |
2366 |
schoenebeck |
3488 |
|
2367 |
|
|
if (pProgress) |
2368 |
|
|
__notify_progress(pProgress, 1.0); // notify done |
2369 |
schoenebeck |
2609 |
} |
2370 |
|
|
|
2371 |
|
|
/** @brief Updates all file offsets stored all over the file. |
2372 |
|
|
* |
2373 |
|
|
* This virtual method is called whenever the overall file layout has been |
2374 |
|
|
* changed (i.e. file or individual RIFF chunks have been resized). It is |
2375 |
|
|
* then the responsibility of this method to update all file offsets stored |
2376 |
|
|
* in the file format. For example samples are referenced by instruments by |
2377 |
|
|
* file offsets. The gig format also stores references to instrument |
2378 |
|
|
* scripts as file offsets, and thus it overrides this method to update |
2379 |
|
|
* those file offsets as well. |
2380 |
|
|
*/ |
2381 |
|
|
void File::UpdateFileOffsets() { |
2382 |
schoenebeck |
800 |
__UpdateWavePoolTableChunk(); |
2383 |
|
|
} |
2384 |
|
|
|
2385 |
|
|
/** |
2386 |
|
|
* Checks if all (for DLS) mandatory chunks exist, if not they will be |
2387 |
|
|
* created. Note that those chunks will not be made persistent until |
2388 |
|
|
* Save() was called. |
2389 |
|
|
*/ |
2390 |
|
|
void File::__ensureMandatoryChunksExist() { |
2391 |
|
|
// enusre 'lins' list chunk exists (mandatory for instrument definitions) |
2392 |
|
|
RIFF::List* lstInstruments = pRIFF->GetSubList(LIST_TYPE_LINS); |
2393 |
|
|
if (!lstInstruments) pRIFF->AddSubList(LIST_TYPE_LINS); |
2394 |
|
|
// ensure 'ptbl' chunk exists (mandatory for samples) |
2395 |
|
|
RIFF::Chunk* ptbl = pRIFF->GetSubChunk(CHUNK_ID_PTBL); |
2396 |
|
|
if (!ptbl) { |
2397 |
|
|
const int iOffsetSize = (b64BitWavePoolOffsets) ? 8 : 4; |
2398 |
|
|
ptbl = pRIFF->AddSubChunk(CHUNK_ID_PTBL, WavePoolHeaderSize + iOffsetSize); |
2399 |
|
|
} |
2400 |
|
|
// enusre 'wvpl' list chunk exists (mandatory for samples) |
2401 |
|
|
RIFF::List* wvpl = pRIFF->GetSubList(LIST_TYPE_WVPL); |
2402 |
|
|
if (!wvpl) pRIFF->AddSubList(LIST_TYPE_WVPL); |
2403 |
|
|
} |
2404 |
|
|
|
2405 |
|
|
/** |
2406 |
|
|
* Updates (persistently) the wave pool table with offsets to all |
2407 |
|
|
* currently available samples. <b>Caution:</b> this method assumes the |
2408 |
schoenebeck |
804 |
* 'ptbl' chunk to be already of the correct size and the file to be |
2409 |
|
|
* writable, so usually this method is only called after a Save() call. |
2410 |
schoenebeck |
800 |
* |
2411 |
|
|
* @throws Exception - if 'ptbl' chunk is too small (should only occur |
2412 |
|
|
* if there's a bug) |
2413 |
|
|
*/ |
2414 |
|
|
void File::__UpdateWavePoolTableChunk() { |
2415 |
|
|
__UpdateWavePoolTable(); |
2416 |
|
|
RIFF::Chunk* ptbl = pRIFF->GetSubChunk(CHUNK_ID_PTBL); |
2417 |
|
|
const int iOffsetSize = (b64BitWavePoolOffsets) ? 8 : 4; |
2418 |
|
|
// check if 'ptbl' chunk is large enough |
2419 |
schoenebeck |
3053 |
WavePoolCount = (pSamples) ? uint32_t(pSamples->size()) : 0; |
2420 |
schoenebeck |
2912 |
const file_offset_t ulRequiredSize = WavePoolHeaderSize + iOffsetSize * WavePoolCount; |
2421 |
schoenebeck |
800 |
if (ptbl->GetSize() < ulRequiredSize) throw Exception("Fatal error, 'ptbl' chunk too small"); |
2422 |
schoenebeck |
804 |
// save the 'ptbl' chunk's current read/write position |
2423 |
schoenebeck |
2912 |
file_offset_t ullOriginalPos = ptbl->GetPos(); |
2424 |
schoenebeck |
800 |
// update headers |
2425 |
schoenebeck |
804 |
ptbl->SetPos(0); |
2426 |
persson |
1179 |
uint32_t tmp = WavePoolHeaderSize; |
2427 |
|
|
ptbl->WriteUint32(&tmp); |
2428 |
|
|
tmp = WavePoolCount; |
2429 |
|
|
ptbl->WriteUint32(&tmp); |
2430 |
schoenebeck |
800 |
// update offsets |
2431 |
schoenebeck |
804 |
ptbl->SetPos(WavePoolHeaderSize); |
2432 |
schoenebeck |
800 |
if (b64BitWavePoolOffsets) { |
2433 |
|
|
for (int i = 0 ; i < WavePoolCount ; i++) { |
2434 |
persson |
1179 |
tmp = pWavePoolTableHi[i]; |
2435 |
|
|
ptbl->WriteUint32(&tmp); |
2436 |
|
|
tmp = pWavePoolTable[i]; |
2437 |
|
|
ptbl->WriteUint32(&tmp); |
2438 |
schoenebeck |
800 |
} |
2439 |
|
|
} else { // conventional 32 bit offsets |
2440 |
persson |
1179 |
for (int i = 0 ; i < WavePoolCount ; i++) { |
2441 |
|
|
tmp = pWavePoolTable[i]; |
2442 |
|
|
ptbl->WriteUint32(&tmp); |
2443 |
|
|
} |
2444 |
schoenebeck |
800 |
} |
2445 |
schoenebeck |
804 |
// restore 'ptbl' chunk's original read/write position |
2446 |
schoenebeck |
2912 |
ptbl->SetPos(ullOriginalPos); |
2447 |
schoenebeck |
800 |
} |
2448 |
|
|
|
2449 |
|
|
/** |
2450 |
|
|
* Updates the wave pool table with offsets to all currently available |
2451 |
|
|
* samples. <b>Caution:</b> this method assumes the 'wvpl' list chunk |
2452 |
|
|
* exists already. |
2453 |
|
|
*/ |
2454 |
|
|
void File::__UpdateWavePoolTable() { |
2455 |
schoenebeck |
3053 |
WavePoolCount = (pSamples) ? uint32_t(pSamples->size()) : 0; |
2456 |
schoenebeck |
800 |
// resize wave pool table arrays |
2457 |
|
|
if (pWavePoolTable) delete[] pWavePoolTable; |
2458 |
|
|
if (pWavePoolTableHi) delete[] pWavePoolTableHi; |
2459 |
|
|
pWavePoolTable = new uint32_t[WavePoolCount]; |
2460 |
|
|
pWavePoolTableHi = new uint32_t[WavePoolCount]; |
2461 |
schoenebeck |
802 |
if (!pSamples) return; |
2462 |
schoenebeck |
3474 |
// update offsets in wave pool table |
2463 |
schoenebeck |
800 |
RIFF::List* wvpl = pRIFF->GetSubList(LIST_TYPE_WVPL); |
2464 |
schoenebeck |
3478 |
uint64_t wvplFileOffset = wvpl->GetFilePos() - |
2465 |
|
|
wvpl->GetPos(); // mandatory, since position might have changed |
2466 |
schoenebeck |
3474 |
if (!b64BitWavePoolOffsets) { // conventional 32 bit offsets (and no extension files) ... |
2467 |
schoenebeck |
800 |
SampleList::iterator iter = pSamples->begin(); |
2468 |
|
|
SampleList::iterator end = pSamples->end(); |
2469 |
|
|
for (int i = 0 ; iter != end ; ++iter, i++) { |
2470 |
schoenebeck |
3478 |
uint64_t _64BitOffset = |
2471 |
|
|
(*iter)->pWaveList->GetFilePos() - |
2472 |
|
|
(*iter)->pWaveList->GetPos() - // should be zero, but just to be sure |
2473 |
|
|
wvplFileOffset - |
2474 |
|
|
LIST_HEADER_SIZE(pRIFF->GetFileOffsetSize()); |
2475 |
schoenebeck |
2912 |
(*iter)->ullWavePoolOffset = _64BitOffset; |
2476 |
schoenebeck |
800 |
pWavePoolTable[i] = (uint32_t) _64BitOffset; |
2477 |
|
|
} |
2478 |
schoenebeck |
3474 |
} else { // a) native 64 bit offsets without extension files or b) 32 bit offsets with extension files ... |
2479 |
|
|
if (ExtensionFiles.empty()) { // native 64 bit offsets (and no extension files) [not compatible with GigaStudio] ... |
2480 |
|
|
SampleList::iterator iter = pSamples->begin(); |
2481 |
|
|
SampleList::iterator end = pSamples->end(); |
2482 |
|
|
for (int i = 0 ; iter != end ; ++iter, i++) { |
2483 |
schoenebeck |
3478 |
uint64_t _64BitOffset = |
2484 |
|
|
(*iter)->pWaveList->GetFilePos() - |
2485 |
|
|
(*iter)->pWaveList->GetPos() - // should be zero, but just to be sure |
2486 |
|
|
wvplFileOffset - |
2487 |
|
|
LIST_HEADER_SIZE(pRIFF->GetFileOffsetSize()); |
2488 |
schoenebeck |
3474 |
(*iter)->ullWavePoolOffset = _64BitOffset; |
2489 |
|
|
pWavePoolTableHi[i] = (uint32_t) (_64BitOffset >> 32); |
2490 |
|
|
pWavePoolTable[i] = (uint32_t) _64BitOffset; |
2491 |
|
|
} |
2492 |
|
|
} else { // 32 bit offsets with extension files (GigaStudio legacy support) ... |
2493 |
|
|
// the main gig and the extension files may contain wave data |
2494 |
|
|
std::vector<RIFF::File*> poolFiles; |
2495 |
|
|
poolFiles.push_back(pRIFF); |
2496 |
|
|
poolFiles.insert(poolFiles.end(), ExtensionFiles.begin(), ExtensionFiles.end()); |
2497 |
|
|
|
2498 |
|
|
RIFF::File* pCurPoolFile = NULL; |
2499 |
|
|
int fileNo = 0; |
2500 |
|
|
int waveOffset = 0; |
2501 |
|
|
SampleList::iterator iter = pSamples->begin(); |
2502 |
|
|
SampleList::iterator end = pSamples->end(); |
2503 |
|
|
for (int i = 0 ; iter != end ; ++iter, i++) { |
2504 |
|
|
RIFF::File* pPoolFile = (*iter)->pWaveList->GetFile(); |
2505 |
|
|
// if this sample is located in the same pool file as the |
2506 |
|
|
// last we reuse the previously computed fileNo and waveOffset |
2507 |
|
|
if (pPoolFile != pCurPoolFile) { // it is a different pool file than the last sample ... |
2508 |
|
|
pCurPoolFile = pPoolFile; |
2509 |
|
|
|
2510 |
|
|
std::vector<RIFF::File*>::iterator sIter; |
2511 |
|
|
sIter = std::find(poolFiles.begin(), poolFiles.end(), pPoolFile); |
2512 |
|
|
if (sIter != poolFiles.end()) |
2513 |
|
|
fileNo = std::distance(poolFiles.begin(), sIter); |
2514 |
|
|
else |
2515 |
|
|
throw DLS::Exception("Fatal error, unknown pool file"); |
2516 |
|
|
|
2517 |
|
|
RIFF::List* extWvpl = pCurPoolFile->GetSubList(LIST_TYPE_WVPL); |
2518 |
|
|
if (!extWvpl) |
2519 |
|
|
throw DLS::Exception("Fatal error, pool file has no 'wvpl' list chunk"); |
2520 |
schoenebeck |
3478 |
waveOffset = |
2521 |
|
|
extWvpl->GetFilePos() - |
2522 |
|
|
extWvpl->GetPos() + // mandatory, since position might have changed |
2523 |
|
|
LIST_HEADER_SIZE(pCurPoolFile->GetFileOffsetSize()); |
2524 |
schoenebeck |
3474 |
} |
2525 |
schoenebeck |
3478 |
uint64_t _64BitOffset = |
2526 |
|
|
(*iter)->pWaveList->GetFilePos() - |
2527 |
|
|
(*iter)->pWaveList->GetPos() - // should be zero, but just to be sure |
2528 |
|
|
waveOffset; |
2529 |
schoenebeck |
3474 |
// pWavePoolTableHi stores file number when extension files are in use |
2530 |
|
|
pWavePoolTableHi[i] = (uint32_t) fileNo; |
2531 |
|
|
pWavePoolTable[i] = (uint32_t) _64BitOffset; |
2532 |
|
|
(*iter)->ullWavePoolOffset = _64BitOffset; |
2533 |
|
|
} |
2534 |
|
|
} |
2535 |
schoenebeck |
800 |
} |
2536 |
|
|
} |
2537 |
|
|
|
2538 |
|
|
|
2539 |
schoenebeck |
2 |
// *************** Exception *************** |
2540 |
|
|
// * |
2541 |
|
|
|
2542 |
schoenebeck |
3198 |
Exception::Exception() : RIFF::Exception() { |
2543 |
schoenebeck |
2 |
} |
2544 |
|
|
|
2545 |
schoenebeck |
3198 |
Exception::Exception(String format, ...) : RIFF::Exception() { |
2546 |
|
|
va_list arg; |
2547 |
|
|
va_start(arg, format); |
2548 |
|
|
Message = assemble(format, arg); |
2549 |
|
|
va_end(arg); |
2550 |
|
|
} |
2551 |
|
|
|
2552 |
|
|
Exception::Exception(String format, va_list arg) : RIFF::Exception() { |
2553 |
|
|
Message = assemble(format, arg); |
2554 |
|
|
} |
2555 |
|
|
|
2556 |
schoenebeck |
2 |
void Exception::PrintMessage() { |
2557 |
|
|
std::cout << "DLS::Exception: " << Message << std::endl; |
2558 |
|
|
} |
2559 |
|
|
|
2560 |
schoenebeck |
518 |
|
2561 |
|
|
// *************** functions *************** |
2562 |
|
|
// * |
2563 |
|
|
|
2564 |
|
|
/** |
2565 |
|
|
* Returns the name of this C++ library. This is usually "libgig" of |
2566 |
|
|
* course. This call is equivalent to RIFF::libraryName() and |
2567 |
|
|
* gig::libraryName(). |
2568 |
|
|
*/ |
2569 |
|
|
String libraryName() { |
2570 |
|
|
return PACKAGE; |
2571 |
|
|
} |
2572 |
|
|
|
2573 |
|
|
/** |
2574 |
|
|
* Returns version of this C++ library. This call is equivalent to |
2575 |
|
|
* RIFF::libraryVersion() and gig::libraryVersion(). |
2576 |
|
|
*/ |
2577 |
|
|
String libraryVersion() { |
2578 |
|
|
return VERSION; |
2579 |
|
|
} |
2580 |
|
|
|
2581 |
schoenebeck |
2 |
} // namespace DLS |