Debian Codegraph @

plugins/sound/WavFileLoader.h

raw
1 #pragma once
2
3 #include <stdexcept>
4 #include "idatastream.h"
5
6 #ifdef __APPLE__
7 #include <OpenAL/al.h>
8 #else
9 #include <AL/al.h>
10 #endif
11
12 class InputStream;
13
14 namespace sound {
15
16 /**
17 * greebo: Loader class creating an AL buffer from a WAV file.
18 *
19 * Modeled after the one used by the Ogre3D people, found it posted
20 * somewhere on the net.
21 */
22 class WavFileLoader
23 {
24 private:
25 struct FileInfo
26 {
27 char magic[5];
28 unsigned int size;
29 char fileFormat[5];
30 unsigned short audioFormat;
31 unsigned short channels;
32 unsigned int freq;
33 unsigned short bps; // bits per sample
34
35 FileInfo()
36 {
37 magic[4] = '\0';
38 fileFormat[4] = '\0';
39 audioFormat = 0;
40 }
41
42 ALenum getAlFormat() const
43 {
44 if (channels == 1)
45 {
46 return bps == 8 ? AL_FORMAT_MONO8 : AL_FORMAT_MONO16;
47 }
48 else
49 {
50 return bps == 8 ? AL_FORMAT_STEREO16 : AL_FORMAT_STEREO16;
51 }
52 }
53 };
54
55 typedef StreamBase::byte_type byte;
56
57 public:
58 /**
59 * greebo: Determines the WAV file length in seconds.
60 * @throws: std::runtime_error if an error occurs.
61 */
62 static float GetDuration(InputStream& stream)
63 {
64 FileInfo info;
65 ParseFileInfo(stream, info);
66
67 SkipToRemainingData(stream);
68
69 // The next four bytes are the remaining size of the file
70 unsigned int remainingSize = 0;
71 stream.read(reinterpret_cast<byte*>(&remainingSize), sizeof(remainingSize));
72
73 // Calculate how many samples we have in the payload, then calculate the duration
74 auto numSamples = remainingSize / (info.bps >> 3);
75 auto numSamplesPerChannel = numSamples / info.channels;
76
77 return static_cast<float>(numSamplesPerChannel) / info.freq;
78 }
79
80 /**
81 * greebo: Loads a WAV file from a stream into OpenAL,
82 * returns the openAL buffer handle.
83 *
84 * @throws: std::runtime_error if an error occurs.
85 */
86 static ALuint LoadFromStream(InputStream& stream)
87 {
88 FileInfo info;
89 ParseFileInfo(stream, info);
90
91 SkipToRemainingData(stream);
92
93 // The next four bytes are the remaining size of the file
94 unsigned int remainingSize = 0;
95 stream.read(reinterpret_cast<byte*>(&remainingSize), sizeof(remainingSize));
96
97 ALuint bufferNum = 0;
98 alGenBuffers(1, &bufferNum);
99
100 std::vector<byte> data(remainingSize);
101 stream.read(data.data(), remainingSize);
102
103 alBufferData(bufferNum, info.getAlFormat(), data.data(), static_cast<ALsizei>(remainingSize), info.freq);
104
105 return bufferNum;
106 }
107
108 private:
109 // Assuming that the FMT chunk has been parsed, this seeks forward to the
110 // beginning of the sound data
111 static void SkipToRemainingData(InputStream& stream)
112 {
113 char buffer[5];
114 buffer[4] = '\0';
115
116 // check 'data' sub chunk (2)
117 stream.read(reinterpret_cast<byte*>(buffer), 4);
118
119 if (std::string(buffer) != "data" && std::string(buffer) != "fact")
120 {
121 throw std::runtime_error("No 'data' subchunk.");
122 }
123
124 // fact is an option section we don't need to worry about
125 if (std::string(buffer) == "fact")
126 {
127 byte temp[8];
128 stream.read(temp, 8);
129
130 // Now we should hit the data chunk
131 stream.read(reinterpret_cast<byte*>(buffer), 4);
132 if (std::string(buffer) != "data")
133 {
134 throw std::runtime_error("No 'data' subchunk.");
135 }
136 }
137 }
138
139 // Reads the file header, leaves the stream right after the end of the first chunk
140 static void ParseFileInfo(InputStream& stream, FileInfo& info)
141 {
142 // check magic
143 stream.read(reinterpret_cast<byte*>(info.magic), 4);
144
145 if (std::string(info.magic) != "RIFF")
146 {
147 throw std::runtime_error("No wav file");
148 }
149
150 // The next 4 bytes are the file size, we can skip this since we get the size from the DataStream
151 stream.read(reinterpret_cast<byte*>(&info.size), sizeof(info.size));
152
153 // check file format
154 stream.read(reinterpret_cast<byte*>(info.fileFormat), 4);
155
156 if (std::string(info.fileFormat) != "WAVE")
157 {
158 throw std::runtime_error("Wrong wav file format");
159 }
160
161 // check 'fmt ' sub chunk (1)
162 char buffer[5];
163 buffer[4] = '\0';
164 stream.read(reinterpret_cast<byte*>(buffer), 4);
165 if (std::string(buffer) != "fmt ") {
166 throw std::runtime_error("No 'fmt ' subchunk.");
167 }
168
169 // read (1)'s size
170 unsigned int subChunk1Size(0);
171 stream.read(reinterpret_cast<byte*>(&subChunk1Size), 4);
172
173 if (subChunk1Size < 16)
174 {
175 throw std::runtime_error("'fmt ' chunk too small.");
176 }
177
178 // check PCM audio format
179 stream.read(reinterpret_cast<byte*>(&info.audioFormat), sizeof(info.audioFormat));
180
181 if (info.audioFormat != 1)
182 {
183 throw std::runtime_error("Audio format is not PCM.");
184 }
185
186 // read number of channels
187 stream.read(reinterpret_cast<byte*>(&info.channels), sizeof(info.channels));
188
189 // read frequency (sample rate)
190 stream.read(reinterpret_cast<byte*>(&info.freq), sizeof(info.freq));
191
192 // skip 6 bytes (Byte rate (4), Block align (2))
193 byte temp[6];
194 stream.read(temp, 6);
195
196 // read bits per sample
197 stream.read(reinterpret_cast<byte*>(&info.bps), sizeof(info.bps));
198 }
199 };
200
201 } // namespace sound