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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3481 - (hide annotations) (download) (as text)
Fri Feb 22 12:12:50 2019 UTC (5 years, 2 months ago) by schoenebeck
File MIME type: text/x-c++hdr
File size: 18765 byte(s)
* gig.h, gig.cpp: Added File::GetRiffFile() method.
* DLS.h, DLS.cpp: Added File::GetRiffFile() method.
* sf2.h, sf2.cpp: Added Sample::GetFile() and
  File::GetRiffFile() methods.
* RIFF.h, RIFF.cpp: Added a 2nd (overridden)
  progress_t::subdivide() method which allows a more
  fine graded control into which portions the subtasks
  are divided to.
* RIFF Fix: API doc comment for Chunk::GetFilePos() was
  completely wrong.
* Bumped version (4.1.0.svn14).

1 schoenebeck 2 /***************************************************************************
2     * *
3 schoenebeck 933 * libgig - C++ cross-platform Gigasampler format file access library *
4 schoenebeck 2 * *
5 persson 3468 * Copyright (C) 2003-2019 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 3478 #ifndef OVERRIDE
40     # if defined(__cplusplus) && __cplusplus >= 201103L
41     # define OVERRIDE override
42 schoenebeck 3479 # else
43     # define OVERRIDE
44 schoenebeck 3478 # endif
45     #endif
46    
47 schoenebeck 2 #include <string>
48     #include <list>
49     #include <map>
50 schoenebeck 2703 #include <set>
51 schoenebeck 3478 #include <vector>
52 schoenebeck 2 #include <iostream>
53 schoenebeck 3198 #include <stdarg.h>
54 schoenebeck 2
55 schoenebeck 11 #ifdef HAVE_CONFIG_H
56     # include <config.h>
57     #endif
58    
59 schoenebeck 2 #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 schoenebeck 3476 #if defined _MSC_VER && _MSC_VER < 1600
67 persson 1869 // 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 schoenebeck 2 #include <stdint.h>
78 persson 1869 #endif
79 schoenebeck 2
80 schoenebeck 1050 #ifdef WIN32
81 schoenebeck 2916 # if (_WIN32 && !_WIN64) || (__GNUC__ && !(__x86_64__ || __ppc64__)) /* if 32 bit windows compilation */
82 persson 3468 # 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 schoenebeck 2916 # endif
87 schoenebeck 1050 # include <windows.h>
88     typedef unsigned int uint;
89     #endif // WIN32
90 schoenebeck 2
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 persson 2044
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 schoenebeck 2 #else // little endian
110     # define CHUNK_ID_RIFF 0x46464952
111     # define CHUNK_ID_RIFX 0x58464952
112     # define CHUNK_ID_LIST 0x5453494C
113 persson 2044
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 schoenebeck 2 #endif // WORDS_BIGENDIAN
126    
127 schoenebeck 2912 #define CHUNK_HEADER_SIZE(fileOffsetSize) (4 + fileOffsetSize)
128     #define LIST_HEADER_SIZE(fileOffsetSize) (8 + fileOffsetSize)
129     #define RIFF_HEADER_SIZE(fileOffsetSize) (8 + fileOffsetSize)
130 schoenebeck 2
131 schoenebeck 1193 /**
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 schoenebeck 2 namespace RIFF {
151    
152     /* just symbol prototyping */
153     class Chunk;
154     class List;
155 schoenebeck 780 class File;
156 schoenebeck 2
157     typedef std::string String;
158    
159 schoenebeck 2912 /** Type used by libgig for handling file positioning during file I/O tasks. */
160     typedef uint64_t file_offset_t;
161    
162 schoenebeck 780 /** Whether file stream is open in read or in read/write mode. */
163 schoenebeck 3326 enum stream_mode_t {
164 schoenebeck 798 stream_mode_read = 0,
165     stream_mode_read_write = 1,
166     stream_mode_closed = 2
167 schoenebeck 3326 };
168 schoenebeck 780
169 schoenebeck 2 /** Current state of the file stream. */
170 schoenebeck 3326 enum stream_state_t {
171 schoenebeck 2 stream_ready = 0,
172     stream_end_reached = 1,
173     stream_closed = 2
174 schoenebeck 3326 };
175 schoenebeck 2
176     /** File stream position dependent to these relations. */
177 schoenebeck 3326 enum stream_whence_t {
178 schoenebeck 2 stream_start = 0,
179     stream_curpos = 1,
180     stream_backward = 2,
181     stream_end = 3
182 schoenebeck 3326 };
183 schoenebeck 2
184 schoenebeck 1193 /** Alignment of data bytes in memory (system dependant). */
185 schoenebeck 3326 enum endian_t {
186 persson 1183 endian_little = 0,
187     endian_big = 1,
188     endian_native = 2
189 schoenebeck 3326 };
190 persson 1183
191 schoenebeck 2912 /** General RIFF chunk structure of a RIFF file. */
192 schoenebeck 2543 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 schoenebeck 2912 /** 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 schoenebeck 2682 /**
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 schoenebeck 3478 std::vector<progress_t> subdivide(int iSubtasks);
224 schoenebeck 3481 std::vector<progress_t> subdivide(std::vector<float> vSubTaskPortions);
225 schoenebeck 2682 };
226    
227 schoenebeck 1193 /** @brief Ordinary RIFF Chunk
228     *
229     * Provides convenient methods to access data of ordinary RIFF chunks
230     * in general.
231     */
232 schoenebeck 2 class Chunk {
233     public:
234 schoenebeck 2912 Chunk(File* pFile, file_offset_t StartPos, List* Parent);
235     String GetChunkIDString() const;
236     uint32_t GetChunkID() const { return ChunkID; } ///< Chunk ID in unsigned integer representation.
237     File* GetFile() const { return pFile; } ///< Returns pointer to the chunk's File object.
238     List* GetParent() const { return pParent; } ///< Returns pointer to the chunk's parent list chunk.
239     file_offset_t GetSize() const { return ullCurrentChunkSize; } ///< Chunk size in bytes (without header, thus the chunk data body)
240     file_offset_t GetNewSize() const { return ullNewChunkSize; } ///< New chunk size if it was modified with Resize(), otherwise value returned will be equal to GetSize().
241     file_offset_t GetPos() const { return ullPos; } ///< Position within the chunk data body (starting with 0).
242 schoenebeck 3481 file_offset_t GetFilePos() const { return ullStartPos + ullPos; } ///< Current, actual offset in file of current chunk data body read/write position.
243 schoenebeck 2912 file_offset_t SetPos(file_offset_t Where, stream_whence_t Whence = stream_start);
244     file_offset_t RemainingBytes() const;
245     stream_state_t GetState() const;
246     file_offset_t Read(void* pData, file_offset_t WordCount, file_offset_t WordSize);
247     file_offset_t ReadInt8(int8_t* pData, file_offset_t WordCount = 1);
248     file_offset_t ReadUint8(uint8_t* pData, file_offset_t WordCount = 1);
249     file_offset_t ReadInt16(int16_t* pData, file_offset_t WordCount = 1);
250     file_offset_t ReadUint16(uint16_t* pData, file_offset_t WordCount = 1);
251     file_offset_t ReadInt32(int32_t* pData, file_offset_t WordCount = 1);
252     file_offset_t ReadUint32(uint32_t* pData, file_offset_t WordCount = 1);
253 schoenebeck 2 int8_t ReadInt8();
254     uint8_t ReadUint8();
255     int16_t ReadInt16();
256     uint16_t ReadUint16();
257     int32_t ReadInt32();
258     uint32_t ReadUint32();
259 persson 2450 void ReadString(String& s, int size);
260 schoenebeck 2912 file_offset_t Write(void* pData, file_offset_t WordCount, file_offset_t WordSize);
261     file_offset_t WriteInt8(int8_t* pData, file_offset_t WordCount = 1);
262     file_offset_t WriteUint8(uint8_t* pData, file_offset_t WordCount = 1);
263     file_offset_t WriteInt16(int16_t* pData, file_offset_t WordCount = 1);
264     file_offset_t WriteUint16(uint16_t* pData, file_offset_t WordCount = 1);
265     file_offset_t WriteInt32(int32_t* pData, file_offset_t WordCount = 1);
266     file_offset_t WriteUint32(uint32_t* pData, file_offset_t WordCount = 1);
267 schoenebeck 780 void* LoadChunkData();
268     void ReleaseChunkData();
269 schoenebeck 2912 void Resize(file_offset_t NewSize);
270 schoenebeck 384 virtual ~Chunk();
271 schoenebeck 2 protected:
272     uint32_t ChunkID;
273 schoenebeck 2912 file_offset_t ullCurrentChunkSize; /* in bytes */
274     file_offset_t ullNewChunkSize; /* in bytes (if chunk was scheduled to be resized) */
275 schoenebeck 2 List* pParent;
276 schoenebeck 780 File* pFile;
277 schoenebeck 2912 file_offset_t ullStartPos; /* actual position in file where chunk data (without header) starts */
278     file_offset_t ullPos; /* # of bytes from ulStartPos */
279 schoenebeck 2 uint8_t* pChunkData;
280 schoenebeck 2912 file_offset_t ullChunkDataSize;
281 schoenebeck 2
282 schoenebeck 780 Chunk(File* pFile);
283 schoenebeck 2912 Chunk(File* pFile, List* pParent, uint32_t uiChunkID, file_offset_t ullBodySize);
284     void ReadHeader(file_offset_t filePos);
285     void WriteHeader(file_offset_t filePos);
286     file_offset_t ReadSceptical(void* pData, file_offset_t WordCount, file_offset_t WordSize);
287     inline static String convertToString(uint32_t word) {
288 schoenebeck 2 String result;
289     for (int i = 0; i < 4; i++) {
290     uint8_t byte = *((uint8_t*)(&word) + i);
291     char c = byte;
292     result += c;
293     }
294     return result;
295     }
296 schoenebeck 2912 virtual file_offset_t RequiredPhysicalSize(int fileOffsetSize);
297     virtual file_offset_t WriteChunk(file_offset_t ullWritePos, file_offset_t ullCurrentDataOffset, progress_t* pProgress = NULL);
298 schoenebeck 780 virtual void __resetPos(); ///< Sets Chunk's read/write position to zero.
299    
300     friend class List;
301 schoenebeck 2 };
302    
303 schoenebeck 1193 /** @brief RIFF List Chunk
304     *
305     * Provides convenient methods to access data of RIFF list chunks and
306     * their subchunks.
307     */
308 schoenebeck 2 class List : public Chunk {
309     public:
310 schoenebeck 2912 List(File* pFile, file_offset_t StartPos, List* Parent);
311     String GetListTypeString() const;
312     uint32_t GetListType() const { return ListType; } ///< Returns unsigned integer representation of the list's ID
313 schoenebeck 11 Chunk* GetSubChunk(uint32_t ChunkID);
314     List* GetSubList(uint32_t ListType);
315 schoenebeck 2 Chunk* GetFirstSubChunk();
316     Chunk* GetNextSubChunk();
317     List* GetFirstSubList();
318     List* GetNextSubList();
319 schoenebeck 2922 size_t CountSubChunks();
320     size_t CountSubChunks(uint32_t ChunkID);
321     size_t CountSubLists();
322     size_t CountSubLists(uint32_t ListType);
323 schoenebeck 2912 Chunk* AddSubChunk(uint32_t uiChunkID, file_offset_t ullBodySize);
324 schoenebeck 780 List* AddSubList(uint32_t uiListType);
325     void DeleteSubChunk(Chunk* pSubChunk);
326 schoenebeck 2584 void MoveSubChunk(Chunk* pSrc, Chunk* pDst); // read API doc comments !!!
327     void MoveSubChunk(Chunk* pSrc, List* pNewParent);
328 schoenebeck 384 virtual ~List();
329 schoenebeck 2 protected:
330     typedef std::map<uint32_t, RIFF::Chunk*> ChunkMap;
331     typedef std::list<Chunk*> ChunkList;
332 schoenebeck 2703 typedef std::set<Chunk*> ChunkSet;
333 schoenebeck 2
334     uint32_t ListType;
335     ChunkList* pSubChunks;
336     ChunkMap* pSubChunksMap;
337     ChunkList::iterator ChunksIterator;
338     ChunkList::iterator ListIterator;
339    
340 schoenebeck 780 List(File* pFile);
341     List(File* pFile, List* pParent, uint32_t uiListID);
342 schoenebeck 2912 void ReadHeader(file_offset_t filePos);
343     void WriteHeader(file_offset_t filePos);
344 schoenebeck 2682 void LoadSubChunks(progress_t* pProgress = NULL);
345     void LoadSubChunksRecursively(progress_t* pProgress = NULL);
346 schoenebeck 2912 virtual file_offset_t RequiredPhysicalSize(int fileOffsetSize);
347     virtual file_offset_t WriteChunk(file_offset_t ullWritePos, file_offset_t ullCurrentDataOffset, progress_t* pProgress = NULL);
348 schoenebeck 780 virtual void __resetPos(); ///< Sets List Chunk's read/write position to zero and causes all sub chunks to do the same.
349 persson 1869 void DeleteChunkList();
350 schoenebeck 2 };
351    
352 schoenebeck 1193 /** @brief RIFF File
353     *
354     * Handles arbitrary RIFF files and provides together with its base
355     * classes convenient methods to walk through, read and modify the
356     * file's RIFF tree.
357     */
358 schoenebeck 2 class File : public List {
359     public:
360 schoenebeck 798 File(uint32_t FileType);
361 schoenebeck 2 File(const String& path);
362 schoenebeck 2912 File(const String& path, uint32_t FileType, endian_t Endian, layout_t layout, offset_size_t fileOffsetSize = offset_size_auto);
363     stream_mode_t GetMode() const;
364 schoenebeck 780 bool SetMode(stream_mode_t NewMode);
365 persson 1184 void SetByteOrder(endian_t Endian);
366 schoenebeck 2912 String GetFileName() const;
367 schoenebeck 2482 void SetFileName(const String& path);
368     bool IsNew() const;
369 schoenebeck 2543 layout_t GetLayout() const;
370 schoenebeck 2912 file_offset_t GetCurrentFileSize() const;
371     file_offset_t GetRequiredFileSize();
372     file_offset_t GetRequiredFileSize(offset_size_t fileOffsetSize);
373     int GetFileOffsetSize() const;
374     int GetRequiredFileOffsetSize();
375    
376 schoenebeck 2682 virtual void Save(progress_t* pProgress = NULL);
377     virtual void Save(const String& path, progress_t* pProgress = NULL);
378 schoenebeck 384 virtual ~File();
379 schoenebeck 780 protected:
380     #if POSIX
381     int hFileRead; ///< handle / descriptor for reading from file
382     int hFileWrite; ///< handle / descriptor for writing to (some) file
383 schoenebeck 1050 #elif defined(WIN32)
384     HANDLE hFileRead; ///< handle / descriptor for reading from file
385     HANDLE hFileWrite; ///< handle / descriptor for writing to (some) file
386 schoenebeck 780 #else
387     FILE* hFileRead; ///< handle / descriptor for reading from file
388     FILE* hFileWrite; ///< handle / descriptor for writing to (some) file
389     #endif // POSIX
390     String Filename;
391     bool bEndianNative;
392 schoenebeck 2482 bool bIsNewFile;
393 schoenebeck 2543 layout_t Layout; ///< An ordinary RIFF file is always set to layout_standard.
394 schoenebeck 2912 offset_size_t FileOffsetPreference;
395     int FileOffsetSize; ///< Size of file offsets (in bytes) when this file was opened (or saved the last time).
396 schoenebeck 780
397     friend class Chunk;
398     friend class List;
399 schoenebeck 2 private:
400 schoenebeck 780 stream_mode_t Mode;
401    
402 schoenebeck 2543 void __openExistingFile(const String& path, uint32_t* FileType = NULL);
403 schoenebeck 2912 void ResizeFile(file_offset_t ullNewSize);
404 schoenebeck 798 #if POSIX
405 schoenebeck 2912 file_offset_t __GetFileSize(int hFile) const;
406 schoenebeck 1050 #elif defined(WIN32)
407 schoenebeck 2912 file_offset_t __GetFileSize(HANDLE hFile) const;
408 schoenebeck 798 #else
409 schoenebeck 2912 file_offset_t __GetFileSize(FILE* hFile) const;
410 schoenebeck 798 #endif
411 schoenebeck 2912 int FileOffsetSizeFor(file_offset_t fileSize) const;
412 persson 2155 void Cleanup();
413 schoenebeck 2 };
414    
415 schoenebeck 1193 /**
416     * Will be thrown whenever an error occurs while handling a RIFF file.
417     */
418 schoenebeck 2 class Exception {
419     public:
420     String Message;
421    
422 schoenebeck 3198 Exception(String format, ...);
423     Exception(String format, va_list arg);
424 schoenebeck 2 void PrintMessage();
425 persson 1713 virtual ~Exception() {}
426 schoenebeck 3198
427     protected:
428     Exception();
429     static String assemble(String format, va_list arg);
430 schoenebeck 2 };
431    
432 schoenebeck 518 String libraryName();
433     String libraryVersion();
434    
435 schoenebeck 2 } // namespace RIFF
436     #endif // __RIFF_H__

  ViewVC Help
Powered by ViewVC