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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3326 - (hide annotations) (download) (as text)
Sat Jul 22 09:01:59 2017 UTC (6 years, 9 months ago) by schoenebeck
File MIME type: text/x-c++hdr
File size: 18363 byte(s)
- RIFF.h: Just got rid of some more C-style typedefs.

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

  ViewVC Help
Powered by ViewVC