--- libgig/trunk/src/RIFF.h 2005/02/17 02:22:26 384 +++ libgig/trunk/src/RIFF.h 2011/01/06 11:33:40 2155 @@ -1,8 +1,8 @@ /*************************************************************************** * * - * libgig - C++ cross-platform Gigasampler format file loader library * + * libgig - C++ cross-platform Gigasampler format file access library * * * - * Copyright (C) 2003-2005 by Christian Schoenebeck * + * Copyright (C) 2003-2011 by Christian Schoenebeck * * * * * * This library is free software; you can redistribute it and/or modify * @@ -24,8 +24,17 @@ #ifndef __RIFF_H__ #define __RIFF_H__ -#define POSIX 1 -#define DEBUG 0 +#ifdef WIN32 +# define POSIX 0 +#endif + +#ifndef POSIX +# define POSIX 1 +#endif + +#ifndef DEBUG +# define DEBUG 0 +#endif #include #include @@ -43,11 +52,24 @@ # include #endif // POSIX +#ifdef _MSC_VER +// Visual C++ 2008 doesn't have stdint.h +typedef __int8 int8_t; +typedef __int16 int16_t; +typedef __int32 int32_t; +typedef __int64 int64_t; +typedef unsigned __int8 uint8_t; +typedef unsigned __int16 uint16_t; +typedef unsigned __int32 uint32_t; +typedef unsigned __int64 uint64_t; +#else #include +#endif -//typedef unsigned char uint8_t; -//typedef unsigned short uint16_t; -//typedef unsigned int uint32_t; +#ifdef WIN32 +# include + typedef unsigned int uint; +#endif // WIN32 #include @@ -55,10 +77,34 @@ # define CHUNK_ID_RIFF 0x52494646 # define CHUNK_ID_RIFX 0x52494658 # define CHUNK_ID_LIST 0x4C495354 + +# define LIST_TYPE_INFO 0x494E464F +# define CHUNK_ID_ICMT 0x49434D54 +# define CHUNK_ID_ICOP 0x49434F50 +# define CHUNK_ID_ICRD 0x49435244 +# define CHUNK_ID_IENG 0x49454E47 +# define CHUNK_ID_INAM 0x494E414D +# define CHUNK_ID_IPRD 0x49505244 +# define CHUNK_ID_ISFT 0x49534654 + +# define CHUNK_ID_SMPL 0x736D706C + #else // little endian # define CHUNK_ID_RIFF 0x46464952 # define CHUNK_ID_RIFX 0x58464952 # define CHUNK_ID_LIST 0x5453494C + +# define LIST_TYPE_INFO 0x4F464E49 +# define CHUNK_ID_ICMT 0x544D4349 +# define CHUNK_ID_ICOP 0x504F4349 +# define CHUNK_ID_ICRD 0x44524349 +# define CHUNK_ID_IENG 0x474E4549 +# define CHUNK_ID_INAM 0x4D414E49 +# define CHUNK_ID_IPRD 0x44525049 +# define CHUNK_ID_ISFT 0x54465349 + +# define CHUNK_ID_SMPL 0x6C706D73 + #endif // WORDS_BIGENDIAN #define CHUNK_HEADER_SIZE 8 @@ -66,15 +112,41 @@ #define RIFF_HEADER_SIZE 12 -/** RIFF specific classes and definitions */ +/** + * @brief RIFF specific classes and definitions + * + * The Resource Interchange File Format (RIFF) is a generic tree-structured + * meta-format which stores data in so called "chunks". It can be compared + * to XML, but in contrast to XML, RIFF is entirely binary encoded, that is + * not ASCII based. RIFF is used as basis for many file formats like AVI, + * WAV, DLS and of course the Gigasampler file format. ;-) + * + * RIFF chunks can be seen as containers for data. There are two distinct + * types of chunks: + * + * - @e ordinary @e chunks are the leafs of the data tree which encapsulate + * the actual data of the file (i.e. the sample data of a .wav file) + * + * - @e list @e chunks are the nodes of the data tree which hold an + * arbitrary amount of subchunks (can be both, list chunks and/or ordinary + * chunks) + */ namespace RIFF { /* just symbol prototyping */ class Chunk; class List; + class File; typedef std::string String; + /** Whether file stream is open in read or in read/write mode. */ + typedef enum { + stream_mode_read = 0, + stream_mode_read_write = 1, + stream_mode_closed = 2 + } stream_mode_t; + /** Current state of the file stream. */ typedef enum { stream_ready = 0, @@ -90,20 +162,28 @@ stream_end = 3 } stream_whence_t; - /** Provides convenient methods to access data of RIFF chunks in general. */ + /** Alignment of data bytes in memory (system dependant). */ + typedef enum { + endian_little = 0, + endian_big = 1, + endian_native = 2 + } endian_t; + + /** @brief Ordinary RIFF Chunk + * + * Provides convenient methods to access data of ordinary RIFF chunks + * in general. + */ class Chunk { public: - #if POSIX - Chunk(int hFile, unsigned long StartPos, bool EndianNative, List* Parent); - #else - Chunk(FILE* hFile, unsigned long StartPos, bool EndianNative, List* Parent); - #endif // POSIX + Chunk(File* pFile, unsigned long StartPos, List* Parent); String GetChunkIDString(); - uint32_t GetChunkID() { return ChunkID; }; ///< Chunk ID in unsigned integer representation. - List* GetParent() { return pParent; }; ///< Returns pointer to the chunk's parent list chunk. - unsigned long GetSize() { return ChunkSize; }; ///< Chunk size in bytes (without header, thus the chunk data body) - unsigned long GetPos() { return ulPos; }; ///< Position within the chunk data body - unsigned long GetFilePos() { return ulStartPos + ulPos; }; ///< Current, actual offset in file. + uint32_t GetChunkID() { return ChunkID; } ///< Chunk ID in unsigned integer representation. + List* GetParent() { return pParent; } ///< Returns pointer to the chunk's parent list chunk. + unsigned long GetSize() { return CurrentChunkSize; } ///< Chunk size in bytes (without header, thus the chunk data body) + unsigned long GetNewSize() { return NewChunkSize; } ///< New chunk size if it was modified with Resize(). + unsigned long GetPos() { return ulPos; } ///< Position within the chunk data body + unsigned long GetFilePos() { return ulStartPos + ulPos; } ///< Current, actual offset in file. unsigned long SetPos(unsigned long Where, stream_whence_t Whence = stream_start); unsigned long RemainingBytes(); stream_state_t GetState(); @@ -120,25 +200,32 @@ uint16_t ReadUint16(); int32_t ReadInt32(); uint32_t ReadUint32(); - void* LoadChunkData(); ///< Load the whole chunk body in memory (on success returns a pointer to the data in RAM, else NULL). - void ReleaseChunkData(); ///< Free loaded chunk body data from memory (RAM). + unsigned long Write(void* pData, unsigned long WordCount, unsigned long WordSize); + unsigned long WriteInt8(int8_t* pData, unsigned long WordCount = 1); + unsigned long WriteUint8(uint8_t* pData, unsigned long WordCount = 1); + unsigned long WriteInt16(int16_t* pData, unsigned long WordCount = 1); + unsigned long WriteUint16(uint16_t* pData, unsigned long WordCount = 1); + unsigned long WriteInt32(int32_t* pData, unsigned long WordCount = 1); + unsigned long WriteUint32(uint32_t* pData, unsigned long WordCount = 1); + void* LoadChunkData(); + void ReleaseChunkData(); + void Resize(int iNewSize); virtual ~Chunk(); protected: uint32_t ChunkID; - uint32_t ChunkSize; /* in bytes */ + uint32_t CurrentChunkSize; /* in bytes */ + uint32_t NewChunkSize; /* in bytes (if chunk was scheduled to be resized) */ List* pParent; - #if POSIX - int hFile; - #else - FILE* hFile; - #endif // POSIX + File* pFile; unsigned long ulStartPos; /* actual position in file where chunk (without header) starts */ unsigned long ulPos; /* # of bytes from ulStartPos */ - bool bEndianNative; uint8_t* pChunkData; + unsigned long ulChunkDataSize; - Chunk(); + Chunk(File* pFile); + Chunk(File* pFile, List* pParent, uint32_t uiChunkID, uint uiBodySize); void ReadHeader(unsigned long fPos); + void WriteHeader(unsigned long fPos); unsigned long ReadSceptical(void* pData, unsigned long WordCount, unsigned long WordSize); inline void swapBytes_16(void* Word) { uint8_t byteCache = *((uint8_t*) Word); @@ -171,16 +258,20 @@ } return result; } + virtual unsigned long WriteChunk(unsigned long ulWritePos, unsigned long ulCurrentDataOffset); + virtual void __resetPos(); ///< Sets Chunk's read/write position to zero. + + friend class List; }; - /** Provides convenient methods to access data of RIFF list chunks and their subchunks. */ + /** @brief RIFF List Chunk + * + * Provides convenient methods to access data of RIFF list chunks and + * their subchunks. + */ class List : public Chunk { public: - #if POSIX - List(int hFile, unsigned long StartPos, bool EndianNative, List* Parent); - #else - List(FILE* hFile, unsigned long StartPos, bool EndianNative, List* Parent); - #endif // POSIX + List(File* pFile, unsigned long StartPos, List* Parent); String GetListTypeString(); uint32_t GetListType() { return ListType; } ///< Returns unsigned integer representation of the list's ID Chunk* GetSubChunk(uint32_t ChunkID); @@ -193,6 +284,10 @@ unsigned int CountSubChunks(uint32_t ChunkID); unsigned int CountSubLists(); unsigned int CountSubLists(uint32_t ListType); + Chunk* AddSubChunk(uint32_t uiChunkID, uint uiBodySize); + List* AddSubList(uint32_t uiListType); + void DeleteSubChunk(Chunk* pSubChunk); + void MoveSubChunk(Chunk* pSrc, Chunk* pDst); virtual ~List(); protected: typedef std::map ChunkMap; @@ -204,29 +299,82 @@ ChunkList::iterator ChunksIterator; ChunkList::iterator ListIterator; - List(); + List(File* pFile); + List(File* pFile, List* pParent, uint32_t uiListID); void ReadHeader(unsigned long fPos); + void WriteHeader(unsigned long fPos); void LoadSubChunks(); + void LoadSubChunksRecursively(); + virtual unsigned long WriteChunk(unsigned long ulWritePos, unsigned long ulCurrentDataOffset); + virtual void __resetPos(); ///< Sets List Chunk's read/write position to zero and causes all sub chunks to do the same. + void DeleteChunkList(); }; - /** Parses arbitrary RIFF files and provides together with it's base classes convenient methods to walk through the RIFF tree. */ + /** @brief RIFF File + * + * Handles arbitrary RIFF files and provides together with its base + * classes convenient methods to walk through, read and modify the + * file's RIFF tree. + */ class File : public List { public: + File(uint32_t FileType); File(const String& path); + stream_mode_t GetMode(); + bool SetMode(stream_mode_t NewMode); + void SetByteOrder(endian_t Endian); + String GetFileName(); + virtual void Save(); + virtual void Save(const String& path); virtual ~File(); + protected: + #if POSIX + int hFileRead; ///< handle / descriptor for reading from file + int hFileWrite; ///< handle / descriptor for writing to (some) file + #elif defined(WIN32) + HANDLE hFileRead; ///< handle / descriptor for reading from file + HANDLE hFileWrite; ///< handle / descriptor for writing to (some) file + #else + FILE* hFileRead; ///< handle / descriptor for reading from file + FILE* hFileWrite; ///< handle / descriptor for writing to (some) file + #endif // POSIX + String Filename; + bool bEndianNative; + + void LogAsResized(Chunk* pResizedChunk); + void UnlogResized(Chunk* pResizedChunk); + friend class Chunk; + friend class List; private: + stream_mode_t Mode; + ChunkList ResizedChunks; ///< All chunks which have been resized (enlarged / shortened). + unsigned long GetFileSize(); + void ResizeFile(unsigned long ulNewSize); + #if POSIX + unsigned long __GetFileSize(int hFile); + #elif defined(WIN32) + unsigned long __GetFileSize(HANDLE hFile); + #else + unsigned long __GetFileSize(FILE* hFile); + #endif + void Cleanup(); }; - /** Will be thrown whenever an error occurs while parsing a RIFF file. */ + /** + * Will be thrown whenever an error occurs while handling a RIFF file. + */ class Exception { public: String Message; - Exception(String Message) { Exception::Message = Message; }; + Exception(String Message) { Exception::Message = Message; } void PrintMessage(); - virtual ~Exception() {}; + virtual ~Exception() {} }; + String libraryName(); + String libraryVersion(); + } // namespace RIFF #endif // __RIFF_H__