/[svn]/libgig/trunk/src/RIFF.h
ViewVC logotype

Contents of /libgig/trunk/src/RIFF.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3478 - (show annotations) (download) (as text)
Thu Feb 21 20:10:08 2019 UTC (5 years, 2 months ago) by schoenebeck
File MIME type: text/x-c++hdr
File size: 18637 byte(s)
* Fix: Don't automatically delete RIFF chunks from DLS/gig classes'
  destructors. Added new virtual method DeleteChunks() to those classes
  for this which must be explicitly called instead to remove their RIFF
  chunks.
* Fix: Many methods of DLS/gig classes assumed a RIFF chunk read position
  of zero; which is unsafe per se.
* Added C++11 "override" keyword where appropriate.
* DLS.cpp, DLS.h: Added new abstract interface base class DLS::Storage
  which is derived by the respective classes for implementing (the old)
  UpdateChunks() and the new DeleteChunks() method.
* RIFF.cpp, RIFF.h: Added new method progress_t::subdivide().
* Bumped version (4.1.0.svn13).

1 /***************************************************************************
2 * *
3 * libgig - C++ cross-platform Gigasampler format file access library *
4 * *
5 * Copyright (C) 2003-2019 by Christian Schoenebeck *
6 * <cuse@users.sourceforge.net> *
7 * *
8 * This library is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * This library is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU General Public License *
19 * along with this library; if not, write to the Free Software *
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
21 * MA 02111-1307 USA *
22 ***************************************************************************/
23
24 #ifndef __RIFF_H__
25 #define __RIFF_H__
26
27 #ifdef WIN32
28 # define POSIX 0
29 #endif
30
31 #ifndef POSIX
32 # define POSIX 1
33 #endif
34
35 #ifndef DEBUG
36 # define DEBUG 0
37 #endif
38
39 #ifndef OVERRIDE
40 # if defined(__cplusplus) && __cplusplus >= 201103L
41 # define OVERRIDE override
42 # endif
43 #endif
44
45 #include <string>
46 #include <list>
47 #include <map>
48 #include <set>
49 #include <vector>
50 #include <iostream>
51 #include <stdarg.h>
52
53 #ifdef HAVE_CONFIG_H
54 # include <config.h>
55 #endif
56
57 #if POSIX
58 # include <sys/types.h>
59 # include <sys/stat.h>
60 # include <fcntl.h>
61 # include <unistd.h>
62 #endif // POSIX
63
64 #if defined _MSC_VER && _MSC_VER < 1600
65 // Visual C++ 2008 doesn't have stdint.h
66 typedef __int8 int8_t;
67 typedef __int16 int16_t;
68 typedef __int32 int32_t;
69 typedef __int64 int64_t;
70 typedef unsigned __int8 uint8_t;
71 typedef unsigned __int16 uint16_t;
72 typedef unsigned __int32 uint32_t;
73 typedef unsigned __int64 uint64_t;
74 #else
75 #include <stdint.h>
76 #endif
77
78 #ifdef WIN32
79 # if (_WIN32 && !_WIN64) || (__GNUC__ && !(__x86_64__ || __ppc64__)) /* if 32 bit windows compilation */
80 # if _WIN32_WINNT < 0x0501
81 # undef _WIN32_WINNT
82 # define _WIN32_WINNT 0x0501 /* Win XP (no service pack): required for 32 bit compilation for GetFileSizeEx() to be declared by windows.h */
83 # endif
84 # endif
85 # include <windows.h>
86 typedef unsigned int uint;
87 #endif // WIN32
88
89 #include <stdio.h>
90
91 #if WORDS_BIGENDIAN
92 # define CHUNK_ID_RIFF 0x52494646
93 # define CHUNK_ID_RIFX 0x52494658
94 # define CHUNK_ID_LIST 0x4C495354
95
96 # define LIST_TYPE_INFO 0x494E464F
97 # define CHUNK_ID_ICMT 0x49434D54
98 # define CHUNK_ID_ICOP 0x49434F50
99 # define CHUNK_ID_ICRD 0x49435244
100 # define CHUNK_ID_IENG 0x49454E47
101 # define CHUNK_ID_INAM 0x494E414D
102 # define CHUNK_ID_IPRD 0x49505244
103 # define CHUNK_ID_ISFT 0x49534654
104
105 # define CHUNK_ID_SMPL 0x736D706C
106
107 #else // little endian
108 # define CHUNK_ID_RIFF 0x46464952
109 # define CHUNK_ID_RIFX 0x58464952
110 # define CHUNK_ID_LIST 0x5453494C
111
112 # define LIST_TYPE_INFO 0x4F464E49
113 # define CHUNK_ID_ICMT 0x544D4349
114 # define CHUNK_ID_ICOP 0x504F4349
115 # define CHUNK_ID_ICRD 0x44524349
116 # define CHUNK_ID_IENG 0x474E4549
117 # define CHUNK_ID_INAM 0x4D414E49
118 # define CHUNK_ID_IPRD 0x44525049
119 # define CHUNK_ID_ISFT 0x54465349
120
121 # define CHUNK_ID_SMPL 0x6C706D73
122
123 #endif // WORDS_BIGENDIAN
124
125 #define CHUNK_HEADER_SIZE(fileOffsetSize) (4 + fileOffsetSize)
126 #define LIST_HEADER_SIZE(fileOffsetSize) (8 + fileOffsetSize)
127 #define RIFF_HEADER_SIZE(fileOffsetSize) (8 + fileOffsetSize)
128
129 /**
130 * @brief RIFF specific classes and definitions
131 *
132 * The Resource Interchange File Format (RIFF) is a generic tree-structured
133 * meta-format which stores data in so called "chunks". It can be compared
134 * to XML, but in contrast to XML, RIFF is entirely binary encoded, that is
135 * not ASCII based. RIFF is used as basis for many file formats like AVI,
136 * WAV, DLS and of course the Gigasampler file format. ;-)
137 *
138 * RIFF chunks can be seen as containers for data. There are two distinct
139 * types of chunks:
140 *
141 * - @e ordinary @e chunks are the leafs of the data tree which encapsulate
142 * the actual data of the file (i.e. the sample data of a .wav file)
143 *
144 * - @e list @e chunks are the nodes of the data tree which hold an
145 * arbitrary amount of subchunks (can be both, list chunks and/or ordinary
146 * chunks)
147 */
148 namespace RIFF {
149
150 /* just symbol prototyping */
151 class Chunk;
152 class List;
153 class File;
154
155 typedef std::string String;
156
157 /** Type used by libgig for handling file positioning during file I/O tasks. */
158 typedef uint64_t file_offset_t;
159
160 /** Whether file stream is open in read or in read/write mode. */
161 enum stream_mode_t {
162 stream_mode_read = 0,
163 stream_mode_read_write = 1,
164 stream_mode_closed = 2
165 };
166
167 /** Current state of the file stream. */
168 enum stream_state_t {
169 stream_ready = 0,
170 stream_end_reached = 1,
171 stream_closed = 2
172 };
173
174 /** File stream position dependent to these relations. */
175 enum stream_whence_t {
176 stream_start = 0,
177 stream_curpos = 1,
178 stream_backward = 2,
179 stream_end = 3
180 };
181
182 /** Alignment of data bytes in memory (system dependant). */
183 enum endian_t {
184 endian_little = 0,
185 endian_big = 1,
186 endian_native = 2
187 };
188
189 /** General RIFF chunk structure of a RIFF file. */
190 enum layout_t {
191 layout_standard = 0, ///< Standard RIFF file layout: First chunk in file is a List chunk which contains all other chunks and there are no chunks outside the scope of that very first (List) chunk.
192 layout_flat = 1 ///< Not a "real" RIFF file: First chunk in file is an ordinary data chunk, not a List chunk, and there might be other chunks after that first chunk.
193 };
194
195 /** Size of RIFF file offsets used in all RIFF chunks' headers. @see File::GetFileOffsetSize() */
196 enum offset_size_t {
197 offset_size_auto = 0, ///< Use 32 bit offsets for files smaller than 4 GB, use 64 bit offsets for files equal or larger than 4 GB.
198 offset_size_32bit = 4, ///< Always use 32 bit offsets (even for files larger than 4 GB).
199 offset_size_64bit = 8 ///< Always use 64 bit offsets (even for files smaller than 4 GB).
200 };
201
202 /**
203 * @brief Used for indicating the progress of a certain task.
204 *
205 * The function pointer argument has to be supplied with a valid
206 * function of the given signature which will then be called on
207 * progress changes. An equivalent progress_t structure will be passed
208 * back as argument to the callback function on each progress change.
209 * The factor field of the supplied progress_t structure will then
210 * reflect the current progress as value between 0.0 and 1.0. You might
211 * want to use the custom field for data needed in your callback
212 * function.
213 */
214 struct progress_t {
215 void (*callback)(progress_t*); ///< Callback function pointer which has to be assigned to a function for progress notification.
216 float factor; ///< Reflects current progress as value between 0.0 and 1.0.
217 void* custom; ///< This pointer can be used for arbitrary data.
218 float __range_min; ///< Only for internal usage, do not modify!
219 float __range_max; ///< Only for internal usage, do not modify!
220 progress_t();
221 std::vector<progress_t> subdivide(int iSubtasks);
222 };
223
224 /** @brief Ordinary RIFF Chunk
225 *
226 * Provides convenient methods to access data of ordinary RIFF chunks
227 * in general.
228 */
229 class Chunk {
230 public:
231 Chunk(File* pFile, file_offset_t StartPos, List* Parent);
232 String GetChunkIDString() const;
233 uint32_t GetChunkID() const { return ChunkID; } ///< Chunk ID in unsigned integer representation.
234 File* GetFile() const { return pFile; } ///< Returns pointer to the chunk's File object.
235 List* GetParent() const { return pParent; } ///< Returns pointer to the chunk's parent list chunk.
236 file_offset_t GetSize() const { return ullCurrentChunkSize; } ///< Chunk size in bytes (without header, thus the chunk data body)
237 file_offset_t GetNewSize() const { return ullNewChunkSize; } ///< New chunk size if it was modified with Resize(), otherwise value returned will be equal to GetSize().
238 file_offset_t GetPos() const { return ullPos; } ///< Position within the chunk data body (starting with 0).
239 file_offset_t GetFilePos() const { return ullStartPos + ullPos; } ///< Current, actual offset of chunk data body start in file.
240 file_offset_t SetPos(file_offset_t Where, stream_whence_t Whence = stream_start);
241 file_offset_t RemainingBytes() const;
242 stream_state_t GetState() const;
243 file_offset_t Read(void* pData, file_offset_t WordCount, file_offset_t WordSize);
244 file_offset_t ReadInt8(int8_t* pData, file_offset_t WordCount = 1);
245 file_offset_t ReadUint8(uint8_t* pData, file_offset_t WordCount = 1);
246 file_offset_t ReadInt16(int16_t* pData, file_offset_t WordCount = 1);
247 file_offset_t ReadUint16(uint16_t* pData, file_offset_t WordCount = 1);
248 file_offset_t ReadInt32(int32_t* pData, file_offset_t WordCount = 1);
249 file_offset_t ReadUint32(uint32_t* pData, file_offset_t WordCount = 1);
250 int8_t ReadInt8();
251 uint8_t ReadUint8();
252 int16_t ReadInt16();
253 uint16_t ReadUint16();
254 int32_t ReadInt32();
255 uint32_t ReadUint32();
256 void ReadString(String& s, int size);
257 file_offset_t Write(void* pData, file_offset_t WordCount, file_offset_t WordSize);
258 file_offset_t WriteInt8(int8_t* pData, file_offset_t WordCount = 1);
259 file_offset_t WriteUint8(uint8_t* pData, file_offset_t WordCount = 1);
260 file_offset_t WriteInt16(int16_t* pData, file_offset_t WordCount = 1);
261 file_offset_t WriteUint16(uint16_t* pData, file_offset_t WordCount = 1);
262 file_offset_t WriteInt32(int32_t* pData, file_offset_t WordCount = 1);
263 file_offset_t WriteUint32(uint32_t* pData, file_offset_t WordCount = 1);
264 void* LoadChunkData();
265 void ReleaseChunkData();
266 void Resize(file_offset_t NewSize);
267 virtual ~Chunk();
268 protected:
269 uint32_t ChunkID;
270 file_offset_t ullCurrentChunkSize; /* in bytes */
271 file_offset_t ullNewChunkSize; /* in bytes (if chunk was scheduled to be resized) */
272 List* pParent;
273 File* pFile;
274 file_offset_t ullStartPos; /* actual position in file where chunk data (without header) starts */
275 file_offset_t ullPos; /* # of bytes from ulStartPos */
276 uint8_t* pChunkData;
277 file_offset_t ullChunkDataSize;
278
279 Chunk(File* pFile);
280 Chunk(File* pFile, List* pParent, uint32_t uiChunkID, file_offset_t ullBodySize);
281 void ReadHeader(file_offset_t filePos);
282 void WriteHeader(file_offset_t filePos);
283 file_offset_t ReadSceptical(void* pData, file_offset_t WordCount, file_offset_t WordSize);
284 inline static String convertToString(uint32_t word) {
285 String result;
286 for (int i = 0; i < 4; i++) {
287 uint8_t byte = *((uint8_t*)(&word) + i);
288 char c = byte;
289 result += c;
290 }
291 return result;
292 }
293 virtual file_offset_t RequiredPhysicalSize(int fileOffsetSize);
294 virtual file_offset_t WriteChunk(file_offset_t ullWritePos, file_offset_t ullCurrentDataOffset, progress_t* pProgress = NULL);
295 virtual void __resetPos(); ///< Sets Chunk's read/write position to zero.
296
297 friend class List;
298 };
299
300 /** @brief RIFF List Chunk
301 *
302 * Provides convenient methods to access data of RIFF list chunks and
303 * their subchunks.
304 */
305 class List : public Chunk {
306 public:
307 List(File* pFile, file_offset_t StartPos, List* Parent);
308 String GetListTypeString() const;
309 uint32_t GetListType() const { return ListType; } ///< Returns unsigned integer representation of the list's ID
310 Chunk* GetSubChunk(uint32_t ChunkID);
311 List* GetSubList(uint32_t ListType);
312 Chunk* GetFirstSubChunk();
313 Chunk* GetNextSubChunk();
314 List* GetFirstSubList();
315 List* GetNextSubList();
316 size_t CountSubChunks();
317 size_t CountSubChunks(uint32_t ChunkID);
318 size_t CountSubLists();
319 size_t CountSubLists(uint32_t ListType);
320 Chunk* AddSubChunk(uint32_t uiChunkID, file_offset_t ullBodySize);
321 List* AddSubList(uint32_t uiListType);
322 void DeleteSubChunk(Chunk* pSubChunk);
323 void MoveSubChunk(Chunk* pSrc, Chunk* pDst); // read API doc comments !!!
324 void MoveSubChunk(Chunk* pSrc, List* pNewParent);
325 virtual ~List();
326 protected:
327 typedef std::map<uint32_t, RIFF::Chunk*> ChunkMap;
328 typedef std::list<Chunk*> ChunkList;
329 typedef std::set<Chunk*> ChunkSet;
330
331 uint32_t ListType;
332 ChunkList* pSubChunks;
333 ChunkMap* pSubChunksMap;
334 ChunkList::iterator ChunksIterator;
335 ChunkList::iterator ListIterator;
336
337 List(File* pFile);
338 List(File* pFile, List* pParent, uint32_t uiListID);
339 void ReadHeader(file_offset_t filePos);
340 void WriteHeader(file_offset_t filePos);
341 void LoadSubChunks(progress_t* pProgress = NULL);
342 void LoadSubChunksRecursively(progress_t* pProgress = NULL);
343 virtual file_offset_t RequiredPhysicalSize(int fileOffsetSize);
344 virtual file_offset_t WriteChunk(file_offset_t ullWritePos, file_offset_t ullCurrentDataOffset, progress_t* pProgress = NULL);
345 virtual void __resetPos(); ///< Sets List Chunk's read/write position to zero and causes all sub chunks to do the same.
346 void DeleteChunkList();
347 };
348
349 /** @brief RIFF File
350 *
351 * Handles arbitrary RIFF files and provides together with its base
352 * classes convenient methods to walk through, read and modify the
353 * file's RIFF tree.
354 */
355 class File : public List {
356 public:
357 File(uint32_t FileType);
358 File(const String& path);
359 File(const String& path, uint32_t FileType, endian_t Endian, layout_t layout, offset_size_t fileOffsetSize = offset_size_auto);
360 stream_mode_t GetMode() const;
361 bool SetMode(stream_mode_t NewMode);
362 void SetByteOrder(endian_t Endian);
363 String GetFileName() const;
364 void SetFileName(const String& path);
365 bool IsNew() const;
366 layout_t GetLayout() const;
367 file_offset_t GetCurrentFileSize() const;
368 file_offset_t GetRequiredFileSize();
369 file_offset_t GetRequiredFileSize(offset_size_t fileOffsetSize);
370 int GetFileOffsetSize() const;
371 int GetRequiredFileOffsetSize();
372
373 virtual void Save(progress_t* pProgress = NULL);
374 virtual void Save(const String& path, progress_t* pProgress = NULL);
375 virtual ~File();
376 protected:
377 #if POSIX
378 int hFileRead; ///< handle / descriptor for reading from file
379 int hFileWrite; ///< handle / descriptor for writing to (some) file
380 #elif defined(WIN32)
381 HANDLE hFileRead; ///< handle / descriptor for reading from file
382 HANDLE hFileWrite; ///< handle / descriptor for writing to (some) file
383 #else
384 FILE* hFileRead; ///< handle / descriptor for reading from file
385 FILE* hFileWrite; ///< handle / descriptor for writing to (some) file
386 #endif // POSIX
387 String Filename;
388 bool bEndianNative;
389 bool bIsNewFile;
390 layout_t Layout; ///< An ordinary RIFF file is always set to layout_standard.
391 offset_size_t FileOffsetPreference;
392 int FileOffsetSize; ///< Size of file offsets (in bytes) when this file was opened (or saved the last time).
393
394 friend class Chunk;
395 friend class List;
396 private:
397 stream_mode_t Mode;
398
399 void __openExistingFile(const String& path, uint32_t* FileType = NULL);
400 void ResizeFile(file_offset_t ullNewSize);
401 #if POSIX
402 file_offset_t __GetFileSize(int hFile) const;
403 #elif defined(WIN32)
404 file_offset_t __GetFileSize(HANDLE hFile) const;
405 #else
406 file_offset_t __GetFileSize(FILE* hFile) const;
407 #endif
408 int FileOffsetSizeFor(file_offset_t fileSize) const;
409 void Cleanup();
410 };
411
412 /**
413 * Will be thrown whenever an error occurs while handling a RIFF file.
414 */
415 class Exception {
416 public:
417 String Message;
418
419 Exception(String format, ...);
420 Exception(String format, va_list arg);
421 void PrintMessage();
422 virtual ~Exception() {}
423
424 protected:
425 Exception();
426 static String assemble(String format, va_list arg);
427 };
428
429 String libraryName();
430 String libraryVersion();
431
432 } // namespace RIFF
433 #endif // __RIFF_H__

  ViewVC Help
Powered by ViewVC