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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2912 - (show annotations) (download) (as text)
Tue May 17 14:30:10 2016 UTC (7 years, 11 months ago) by schoenebeck
File MIME type: text/x-c++hdr
File size: 17989 byte(s)
* gig.cpp/.h: GIG FORMAT EXTENSION: Added support for saving gig file
  larger than 4 GB as one single monolithic gig file. A new custom RIFF
  chunk "FFmt" was added to distinguish such monolithic large .gig files
  from old ones which were splitted over several (.gx01, .gx02, ...)
  "extension" files before.
* DLS.cpp/.h: Sample class: wave pool offsets are now 64 bits (to allow
  support for files larger than 4 GB).
* RIFF.cpp/.h: Addded support for RIFF files larger than 4 GB, by default
  the required internal RIFF file offset size is automatically detected
  (that is RIFF files < 4 GB automatically use 32 bit offsets while
  files >= 4 GB automatically use 64 bit offsets), a particular offset
  size can be forced with a new option added to the RIFF File constructor
  though.
* RIFF.cpp/.h: When saving a modified, grown RIFF file, the temporary file
  size during Save() operation will no longer be larger than the final
  grown file size.
* Automake: Set environment variable GCC_COLORS=auto to allow GCC to auto
  detect whether it (sh/c)ould output its messages in color.
* Bumped version (4.0.0.svn3).

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

  ViewVC Help
Powered by ViewVC