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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 384 - (hide annotations) (download) (as text)
Thu Feb 17 02:22:26 2005 UTC (19 years, 1 month ago) by schoenebeck
File MIME type: text/x-c++hdr
File size: 9711 byte(s)
* gig::Sample: external decompression buffers can now be used for streaming
  with Read() and ReadAndLoop() to avoid race conditions in case of
  multiple streaming threads
* fixed some memory leaks caused by non virtual base constructors

1 schoenebeck 2 /***************************************************************************
2     * *
3     * libgig - C++ cross-platform Gigasampler format file loader library *
4     * *
5 schoenebeck 384 * Copyright (C) 2003-2005 by Christian Schoenebeck *
6     * <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     #define POSIX 1
28     #define DEBUG 0
29    
30     #include <string>
31     #include <list>
32     #include <map>
33     #include <iostream>
34    
35 schoenebeck 11 #ifdef HAVE_CONFIG_H
36     # include <config.h>
37     #endif
38    
39 schoenebeck 2 #if POSIX
40     # include <sys/types.h>
41     # include <sys/stat.h>
42     # include <fcntl.h>
43     # include <unistd.h>
44     #endif // POSIX
45    
46     #include <stdint.h>
47    
48     //typedef unsigned char uint8_t;
49     //typedef unsigned short uint16_t;
50     //typedef unsigned int uint32_t;
51    
52     #include <stdio.h>
53    
54     #if WORDS_BIGENDIAN
55     # define CHUNK_ID_RIFF 0x52494646
56     # define CHUNK_ID_RIFX 0x52494658
57     # define CHUNK_ID_LIST 0x4C495354
58     #else // little endian
59     # define CHUNK_ID_RIFF 0x46464952
60     # define CHUNK_ID_RIFX 0x58464952
61     # define CHUNK_ID_LIST 0x5453494C
62     #endif // WORDS_BIGENDIAN
63    
64     #define CHUNK_HEADER_SIZE 8
65     #define LIST_HEADER_SIZE 12
66     #define RIFF_HEADER_SIZE 12
67    
68    
69     /** RIFF specific classes and definitions */
70     namespace RIFF {
71    
72     /* just symbol prototyping */
73     class Chunk;
74     class List;
75    
76     typedef std::string String;
77    
78     /** Current state of the file stream. */
79     typedef enum {
80     stream_ready = 0,
81     stream_end_reached = 1,
82     stream_closed = 2
83     } stream_state_t;
84    
85     /** File stream position dependent to these relations. */
86     typedef enum {
87     stream_start = 0,
88     stream_curpos = 1,
89     stream_backward = 2,
90     stream_end = 3
91     } stream_whence_t;
92    
93     /** Provides convenient methods to access data of RIFF chunks in general. */
94     class Chunk {
95     public:
96     #if POSIX
97     Chunk(int hFile, unsigned long StartPos, bool EndianNative, List* Parent);
98     #else
99     Chunk(FILE* hFile, unsigned long StartPos, bool EndianNative, List* Parent);
100     #endif // POSIX
101     String GetChunkIDString();
102 schoenebeck 11 uint32_t GetChunkID() { return ChunkID; }; ///< Chunk ID in unsigned integer representation.
103     List* GetParent() { return pParent; }; ///< Returns pointer to the chunk's parent list chunk.
104     unsigned long GetSize() { return ChunkSize; }; ///< Chunk size in bytes (without header, thus the chunk data body)
105     unsigned long GetPos() { return ulPos; }; ///< Position within the chunk data body
106 schoenebeck 2 unsigned long GetFilePos() { return ulStartPos + ulPos; }; ///< Current, actual offset in file.
107     unsigned long SetPos(unsigned long Where, stream_whence_t Whence = stream_start);
108     unsigned long RemainingBytes();
109     stream_state_t GetState();
110     unsigned long Read(void* pData, unsigned long WordCount, unsigned long WordSize);
111     unsigned long ReadInt8(int8_t* pData, unsigned long WordCount = 1);
112     unsigned long ReadUint8(uint8_t* pData, unsigned long WordCount = 1);
113     unsigned long ReadInt16(int16_t* pData, unsigned long WordCount = 1);
114     unsigned long ReadUint16(uint16_t* pData, unsigned long WordCount = 1);
115     unsigned long ReadInt32(int32_t* pData, unsigned long WordCount = 1);
116     unsigned long ReadUint32(uint32_t* pData, unsigned long WordCount = 1);
117     int8_t ReadInt8();
118     uint8_t ReadUint8();
119     int16_t ReadInt16();
120     uint16_t ReadUint16();
121     int32_t ReadInt32();
122     uint32_t ReadUint32();
123     void* LoadChunkData(); ///< Load the whole chunk body in memory (on success returns a pointer to the data in RAM, else NULL).
124     void ReleaseChunkData(); ///< Free loaded chunk body data from memory (RAM).
125 schoenebeck 384 virtual ~Chunk();
126 schoenebeck 2 protected:
127     uint32_t ChunkID;
128     uint32_t ChunkSize; /* in bytes */
129     List* pParent;
130     #if POSIX
131     int hFile;
132     #else
133     FILE* hFile;
134     #endif // POSIX
135     unsigned long ulStartPos; /* actual position in file where chunk (without header) starts */
136     unsigned long ulPos; /* # of bytes from ulStartPos */
137     bool bEndianNative;
138     uint8_t* pChunkData;
139    
140     Chunk();
141     void ReadHeader(unsigned long fPos);
142     unsigned long ReadSceptical(void* pData, unsigned long WordCount, unsigned long WordSize);
143     inline void swapBytes_16(void* Word) {
144     uint8_t byteCache = *((uint8_t*) Word);
145     *((uint8_t*) Word) = *((uint8_t*) Word + 1);
146     *((uint8_t*) Word + 1) = byteCache;
147     }
148     inline void swapBytes_32(void* Word) {
149     uint8_t byteCache = *((uint8_t*) Word);
150     *((uint8_t*) Word) = *((uint8_t*) Word + 3);
151     *((uint8_t*) Word + 3) = byteCache;
152     byteCache = *((uint8_t*) Word + 1);
153     *((uint8_t*) Word + 1) = *((uint8_t*) Word + 2);
154     *((uint8_t*) Word + 2) = byteCache;
155     }
156     inline void swapBytes(void* Word, unsigned long WordSize) {
157     uint8_t byteCache;
158     unsigned long lo = 0, hi = WordSize - 1;
159     for (; lo < hi; hi--, lo++) {
160     byteCache = *((uint8_t*) Word + lo);
161     *((uint8_t*) Word + lo) = *((uint8_t*) Word + hi);
162     *((uint8_t*) Word + hi) = byteCache;
163     }
164     }
165     inline String convertToString(uint32_t word) {
166     String result;
167     for (int i = 0; i < 4; i++) {
168     uint8_t byte = *((uint8_t*)(&word) + i);
169     char c = byte;
170     result += c;
171     }
172     return result;
173     }
174     };
175    
176     /** Provides convenient methods to access data of RIFF list chunks and their subchunks. */
177     class List : public Chunk {
178     public:
179     #if POSIX
180     List(int hFile, unsigned long StartPos, bool EndianNative, List* Parent);
181     #else
182     List(FILE* hFile, unsigned long StartPos, bool EndianNative, List* Parent);
183     #endif // POSIX
184     String GetListTypeString();
185 schoenebeck 11 uint32_t GetListType() { return ListType; } ///< Returns unsigned integer representation of the list's ID
186     Chunk* GetSubChunk(uint32_t ChunkID);
187     List* GetSubList(uint32_t ListType);
188 schoenebeck 2 Chunk* GetFirstSubChunk();
189     Chunk* GetNextSubChunk();
190     List* GetFirstSubList();
191     List* GetNextSubList();
192     unsigned int CountSubChunks();
193     unsigned int CountSubChunks(uint32_t ChunkID);
194     unsigned int CountSubLists();
195     unsigned int CountSubLists(uint32_t ListType);
196 schoenebeck 384 virtual ~List();
197 schoenebeck 2 protected:
198     typedef std::map<uint32_t, RIFF::Chunk*> ChunkMap;
199     typedef std::list<Chunk*> ChunkList;
200    
201     uint32_t ListType;
202     ChunkList* pSubChunks;
203     ChunkMap* pSubChunksMap;
204     ChunkList::iterator ChunksIterator;
205     ChunkList::iterator ListIterator;
206    
207     List();
208     void ReadHeader(unsigned long fPos);
209     void LoadSubChunks();
210     };
211    
212     /** Parses arbitrary RIFF files and provides together with it's base classes convenient methods to walk through the RIFF tree. */
213     class File : public List {
214     public:
215     File(const String& path);
216 schoenebeck 384 virtual ~File();
217 schoenebeck 2 private:
218     unsigned long GetFileSize();
219     };
220    
221     /** Will be thrown whenever an error occurs while parsing a RIFF file. */
222     class Exception {
223     public:
224     String Message;
225    
226     Exception(String Message) { Exception::Message = Message; };
227     void PrintMessage();
228     virtual ~Exception() {};
229     };
230    
231     } // namespace RIFF
232     #endif // __RIFF_H__

  ViewVC Help
Powered by ViewVC