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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3481 - (show 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 /***************************************************************************
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 std::vector<progress_t> subdivide(std::vector<float> vSubTaskPortions);
225 };
226
227 /** @brief Ordinary RIFF Chunk
228 *
229 * Provides convenient methods to access data of ordinary RIFF chunks
230 * in general.
231 */
232 class Chunk {
233 public:
234 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 file_offset_t GetFilePos() const { return ullStartPos + ullPos; } ///< Current, actual offset in file of current chunk data body read/write position.
243 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 int8_t ReadInt8();
254 uint8_t ReadUint8();
255 int16_t ReadInt16();
256 uint16_t ReadUint16();
257 int32_t ReadInt32();
258 uint32_t ReadUint32();
259 void ReadString(String& s, int size);
260 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 void* LoadChunkData();
268 void ReleaseChunkData();
269 void Resize(file_offset_t NewSize);
270 virtual ~Chunk();
271 protected:
272 uint32_t ChunkID;
273 file_offset_t ullCurrentChunkSize; /* in bytes */
274 file_offset_t ullNewChunkSize; /* in bytes (if chunk was scheduled to be resized) */
275 List* pParent;
276 File* pFile;
277 file_offset_t ullStartPos; /* actual position in file where chunk data (without header) starts */
278 file_offset_t ullPos; /* # of bytes from ulStartPos */
279 uint8_t* pChunkData;
280 file_offset_t ullChunkDataSize;
281
282 Chunk(File* pFile);
283 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 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 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 virtual void __resetPos(); ///< Sets Chunk's read/write position to zero.
299
300 friend class List;
301 };
302
303 /** @brief RIFF List Chunk
304 *
305 * Provides convenient methods to access data of RIFF list chunks and
306 * their subchunks.
307 */
308 class List : public Chunk {
309 public:
310 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 Chunk* GetSubChunk(uint32_t ChunkID);
314 List* GetSubList(uint32_t ListType);
315 Chunk* GetFirstSubChunk();
316 Chunk* GetNextSubChunk();
317 List* GetFirstSubList();
318 List* GetNextSubList();
319 size_t CountSubChunks();
320 size_t CountSubChunks(uint32_t ChunkID);
321 size_t CountSubLists();
322 size_t CountSubLists(uint32_t ListType);
323 Chunk* AddSubChunk(uint32_t uiChunkID, file_offset_t ullBodySize);
324 List* AddSubList(uint32_t uiListType);
325 void DeleteSubChunk(Chunk* pSubChunk);
326 void MoveSubChunk(Chunk* pSrc, Chunk* pDst); // read API doc comments !!!
327 void MoveSubChunk(Chunk* pSrc, List* pNewParent);
328 virtual ~List();
329 protected:
330 typedef std::map<uint32_t, RIFF::Chunk*> ChunkMap;
331 typedef std::list<Chunk*> ChunkList;
332 typedef std::set<Chunk*> ChunkSet;
333
334 uint32_t ListType;
335 ChunkList* pSubChunks;
336 ChunkMap* pSubChunksMap;
337 ChunkList::iterator ChunksIterator;
338 ChunkList::iterator ListIterator;
339
340 List(File* pFile);
341 List(File* pFile, List* pParent, uint32_t uiListID);
342 void ReadHeader(file_offset_t filePos);
343 void WriteHeader(file_offset_t filePos);
344 void LoadSubChunks(progress_t* pProgress = NULL);
345 void LoadSubChunksRecursively(progress_t* pProgress = NULL);
346 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 virtual void __resetPos(); ///< Sets List Chunk's read/write position to zero and causes all sub chunks to do the same.
349 void DeleteChunkList();
350 };
351
352 /** @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 class File : public List {
359 public:
360 File(uint32_t FileType);
361 File(const String& path);
362 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 bool SetMode(stream_mode_t NewMode);
365 void SetByteOrder(endian_t Endian);
366 String GetFileName() const;
367 void SetFileName(const String& path);
368 bool IsNew() const;
369 layout_t GetLayout() const;
370 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 virtual void Save(progress_t* pProgress = NULL);
377 virtual void Save(const String& path, progress_t* pProgress = NULL);
378 virtual ~File();
379 protected:
380 #if POSIX
381 int hFileRead; ///< handle / descriptor for reading from file
382 int hFileWrite; ///< handle / descriptor for writing to (some) file
383 #elif defined(WIN32)
384 HANDLE hFileRead; ///< handle / descriptor for reading from file
385 HANDLE hFileWrite; ///< handle / descriptor for writing to (some) file
386 #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 bool bIsNewFile;
393 layout_t Layout; ///< An ordinary RIFF file is always set to layout_standard.
394 offset_size_t FileOffsetPreference;
395 int FileOffsetSize; ///< Size of file offsets (in bytes) when this file was opened (or saved the last time).
396
397 friend class Chunk;
398 friend class List;
399 private:
400 stream_mode_t Mode;
401
402 void __openExistingFile(const String& path, uint32_t* FileType = NULL);
403 void ResizeFile(file_offset_t ullNewSize);
404 #if POSIX
405 file_offset_t __GetFileSize(int hFile) const;
406 #elif defined(WIN32)
407 file_offset_t __GetFileSize(HANDLE hFile) const;
408 #else
409 file_offset_t __GetFileSize(FILE* hFile) const;
410 #endif
411 int FileOffsetSizeFor(file_offset_t fileSize) const;
412 void Cleanup();
413 };
414
415 /**
416 * Will be thrown whenever an error occurs while handling a RIFF file.
417 */
418 class Exception {
419 public:
420 String Message;
421
422 Exception(String format, ...);
423 Exception(String format, va_list arg);
424 void PrintMessage();
425 virtual ~Exception() {}
426
427 protected:
428 Exception();
429 static String assemble(String format, va_list arg);
430 };
431
432 String libraryName();
433 String libraryVersion();
434
435 } // namespace RIFF
436 #endif // __RIFF_H__

  ViewVC Help
Powered by ViewVC