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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3479 - (show annotations) (download) (as text)
Thu Feb 21 20:31:31 2019 UTC (5 years, 2 months ago) by schoenebeck
File MIME type: text/x-c++hdr
File size: 18663 byte(s)
- Fixed compiler error with old (pre C++11) compilers.

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

  ViewVC Help
Powered by ViewVC