Use unique function names for handling external libs, instead of winapi names
[alure.git] / src / buffer.cpp
blobb7b5484e634c9810e062860931718acabd9d3269
1 /*
2 * ALURE OpenAL utility library
3 * Copyright (c) 2009 by Chris Robinson.
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to
7 * deal in the Software without restriction, including without limitation the
8 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9 * sell copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
24 /* Title: File Loading */
26 #include "config.h"
28 #include "main.h"
30 #include <string.h>
32 #include <vector>
33 #include <memory>
36 bool load_stream(alureStream *_stream, ALuint buffer)
38 std::auto_ptr<std::istream> fstream(_stream->fstream);
39 std::auto_ptr<alureStream> stream(_stream);
40 if(!stream->IsValid())
41 return false;
43 ALenum format;
44 ALuint freq, blockAlign;
46 if(!stream->GetFormat(&format, &freq, &blockAlign))
48 SetError("Could not get sample format");
49 return false;
52 if(format == AL_NONE)
54 SetError("No valid format");
55 return false;
57 if(blockAlign == 0)
59 SetError("Invalid block size");
60 return false;
62 if(freq == 0)
64 SetError("Invalid sample rate");
65 return false;
68 ALuint writePos = 0, got;
69 std::vector<ALubyte> data(freq*blockAlign);
70 while((got=stream->GetData(&data[writePos], data.size()-writePos)) > 0)
72 writePos += got;
73 data.resize(writePos + freq*blockAlign);
75 data.resize(writePos - (writePos%blockAlign));
76 stream.reset(NULL);
78 alBufferData(buffer, format, &data[0], data.size(), freq);
79 if(alGetError() != AL_NO_ERROR)
81 SetError("Buffer load failed");
82 return false;
85 return true;
88 extern "C" {
90 /* Function: alureCreateBufferFromFile
92 * Loads the given file into a new OpenAL buffer object. The formats supported
93 * depend on the options the library was compiled with, what libraries are
94 * available at runtime, and the installed decode callbacks. Requires an active
95 * context.
97 * Returns:
98 * A new buffer ID with the loaded sound, or AL_NONE on error.
100 * See Also:
101 * <alureBufferDataFromFile>
103 ALURE_API ALuint ALURE_APIENTRY alureCreateBufferFromFile(const ALchar *fname)
105 if(alGetError() != AL_NO_ERROR)
107 SetError("Existing OpenAL error");
108 return AL_NONE;
111 ALuint buf;
112 alGenBuffers(1, &buf);
113 if(alGetError() != AL_NO_ERROR)
115 SetError("Buffer creation failed");
116 return AL_NONE;
119 if(alureBufferDataFromFile(fname, buf) == AL_FALSE)
121 alDeleteBuffers(1, &buf);
122 alGetError();
123 buf = AL_NONE;
126 return buf;
129 /* Function: alureCreateBufferFromMemory
131 * Loads a file image from memory into a new OpenAL buffer object, similar to
132 * alureCreateBufferFromFile. Requires an active context.
134 * Returns:
135 * A new buffer ID with the loaded sound, or AL_NONE on error.
137 * See Also:
138 * <alureBufferDataFromMemory>
140 ALURE_API ALuint ALURE_APIENTRY alureCreateBufferFromMemory(const ALubyte *fdata, ALsizei length)
142 if(alGetError() != AL_NO_ERROR)
144 SetError("Existing OpenAL error");
145 return AL_NONE;
148 ALuint buf;
149 alGenBuffers(1, &buf);
150 if(alGetError() != AL_NO_ERROR)
152 SetError("Buffer creation failed");
153 return AL_NONE;
156 if(alureBufferDataFromMemory(fdata, length, buf) == AL_FALSE)
158 alDeleteBuffers(1, &buf);
159 alGetError();
160 buf = AL_NONE;
163 return buf;
166 /* Function: alureBufferDataFromFile
168 * Loads the given file into an existing OpenAL buffer object. The previous
169 * contents of the buffer are replaced. Requires an active context.
171 * Returns:
172 * AL_FALSE on error.
174 * See Also:
175 * <alureCreateBufferFromFile>
177 ALURE_API ALboolean ALURE_APIENTRY alureBufferDataFromFile(const ALchar *fname, ALuint buffer)
179 if(alGetError() != AL_NO_ERROR)
181 SetError("Existing OpenAL error");
182 return AL_FALSE;
185 if(load_stream(create_stream(fname), buffer) == false)
186 return AL_FALSE;
187 return AL_TRUE;
190 /* Function: alureBufferDataFromMemory
192 * Loads a file image from memory into an existing OpenAL buffer object,
193 * similar to alureBufferDataFromFile. Requires an active context.
195 * Returns:
196 * AL_FALSE on error.
198 * See Also:
199 * <alureCreateBufferFromMemory>
201 ALURE_API ALboolean ALURE_APIENTRY alureBufferDataFromMemory(const ALubyte *fdata, ALsizei length, ALuint buffer)
203 if(alGetError() != AL_NO_ERROR)
205 SetError("Existing OpenAL error");
206 return AL_FALSE;
209 if(length < 0)
211 SetError("Invalid data length");
212 return AL_FALSE;
215 MemDataInfo memData;
216 memData.Data = fdata;
217 memData.Length = length;
218 memData.Pos = 0;
220 if(load_stream(create_stream(memData), buffer) == false)
221 return AL_FALSE;
222 return AL_TRUE;
225 } // extern "C"