Debian Codegraph @

plugins/sound/OggFileLoader.h

raw
1 #pragma once
2
3 #include <vector>
4
5 #ifdef __APPLE__
6 #include <OpenAL/al.h>
7 #else
8 #include <AL/al.h>
9 #endif
10
11 #include <vorbis/vorbisfile.h>
12 #include <fmt/format.h>
13
14 #include "iarchive.h"
15 #include "stream/ScopedArchiveBuffer.h"
16 #include "OggFileStream.h"
17
18 namespace sound
19 {
20
21 /**
22 * greebo: Loader class creating an AL buffer from an OGG file.
23 */
24 class OggFileLoader
25 {
26 private:
27 class FileWrapper
28 {
29 private:
30 OggVorbis_File _oggFile;
31 OggFileStream _stream;
32 int _openResult;
33
34 public:
35 FileWrapper(ArchiveFile& file) :
36 _stream(file)
37 {
38 // Setup the callbacks and point them to the helper class
39 ov_callbacks callbacks;
40 callbacks.read_func = OggFileStream::oggReadFunc;
41 callbacks.seek_func = OggFileStream::oggSeekFunc;
42 callbacks.close_func = OggFileStream::oggCloseFunc;
43 callbacks.tell_func = OggFileStream::oggTellFunc;
44
45 // Open the OGG data stream using the custom callbacks
46 _openResult = ov_open_callbacks(static_cast<void*>(&_stream), &_oggFile, nullptr, 0, callbacks);
47 }
48
49 // Return the pointer to the handle structure needed to call the ov_* functions
50 OggVorbis_File* getHandle()
51 {
52 if (_openResult != 0)
53 {
54 throw std::runtime_error(fmt::format("Error opening OGG file (error code: {0}", _openResult));
55 }
56
57 return &_oggFile;
58 }
59
60 ~FileWrapper()
61 {
62 // Clean up the OGG routines
63 ov_clear(&_oggFile);
64 }
65 };
66 public:
67 /**
68 * greebo: Determines the OGG file length in seconds.
69 * @throws: std::runtime_error if an error occurs.
70 */
71 static float GetDuration(ArchiveFile& vfsFile)
72 {
73 FileWrapper file(vfsFile);
74
75 return static_cast<float>(ov_time_total(file.getHandle(), -1));
76 }
77
78 /**
79 * greebo: Loads an OGG file from the given stream into OpenAL,
80 * returns the openAL buffer handle.
81 *
82 * @throws: std::runtime_error if an error occurs.
83 */
84 static ALuint LoadFromFile(ArchiveFile& vfsFile)
85 {
86 FileWrapper file(vfsFile);
87
88 // Get some information about the OGG file
89 vorbis_info* vorbisInfo = ov_info(file.getHandle(), -1);
90
91 // Check the number of channels
92 ALenum format = (vorbisInfo->channels == 1) ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16;
93
94 // Get the sample Rate
95 ALsizei freq = static_cast<ALsizei>(vorbisInfo->rate);
96
97 long bytes;
98 char smallBuffer[4096];
99
100 std::vector<char> largeBuffer;
101 largeBuffer.reserve(vfsFile.size() * 2);
102
103 do
104 {
105 int bitStream;
106 // Read a chunk of decoded data from the vorbis file
107 bytes = ov_read(file.getHandle(), smallBuffer, sizeof(smallBuffer), 0, 2, 1, &bitStream);
108
109 if (bytes == OV_HOLE)
110 {
111 rError() << "Error decoding OGG: OV_HOLE.\n";
112 }
113 else if (bytes == OV_EBADLINK)
114 {
115 rError() << "Error decoding OGG: OV_EBADLINK.\n";
116 }
117 else
118 {
119 // Stuff this into the variable-sized buffer
120 largeBuffer.insert(largeBuffer.end(), smallBuffer, smallBuffer + bytes);
121 }
122 }
123 while (bytes > 0);
124
125 ALuint bufferNum = 0;
126 // Allocate a new buffer
127 alGenBuffers(1, &bufferNum);
128
129 // Upload sound data to buffer
130 alBufferData(bufferNum,
131 format,
132 largeBuffer.data(),
133 static_cast<ALsizei>(largeBuffer.size()),
134 freq);
135
136 return bufferNum;
137 }
138 };
139
140 }