/[svn]/linuxsampler/tags/start/RIFF.h
ViewVC logotype

Contents of /linuxsampler/tags/start/RIFF.h

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC