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
24 /* Title: File Loading */
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())
44 ALuint freq
, blockAlign
;
46 if(!stream
->GetFormat(&format
, &freq
, &blockAlign
))
48 SetError("Could not get sample format");
54 SetError("No valid format");
59 SetError("Invalid block size");
64 SetError("Invalid sample rate");
68 ALuint writePos
= 0, got
;
69 std::vector
<ALubyte
> data(freq
*blockAlign
);
70 while((got
=stream
->GetData(&data
[writePos
], data
.size()-writePos
)) > 0)
73 data
.resize(writePos
+ freq
*blockAlign
);
75 data
.resize(writePos
- (writePos
%blockAlign
));
78 alBufferData(buffer
, format
, &data
[0], data
.size(), freq
);
79 if(alGetError() != AL_NO_ERROR
)
81 SetError("Buffer load failed");
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
98 * A new buffer ID with the loaded sound, or AL_NONE on error.
101 * <alureBufferDataFromFile>
103 ALURE_API ALuint ALURE_APIENTRY
alureCreateBufferFromFile(const ALchar
*fname
)
105 if(alGetError() != AL_NO_ERROR
)
107 SetError("Existing OpenAL error");
112 alGenBuffers(1, &buf
);
113 if(alGetError() != AL_NO_ERROR
)
115 SetError("Buffer creation failed");
119 if(alureBufferDataFromFile(fname
, buf
) == AL_FALSE
)
121 alDeleteBuffers(1, &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.
135 * A new buffer ID with the loaded sound, or AL_NONE on error.
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");
149 alGenBuffers(1, &buf
);
150 if(alGetError() != AL_NO_ERROR
)
152 SetError("Buffer creation failed");
156 if(alureBufferDataFromMemory(fdata
, length
, buf
) == AL_FALSE
)
158 alDeleteBuffers(1, &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.
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");
185 if(load_stream(create_stream(fname
), buffer
) == false)
190 /* Function: alureBufferDataFromMemory
192 * Loads a file image from memory into an existing OpenAL buffer object,
193 * similar to alureBufferDataFromFile. Requires an active context.
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");
211 SetError("Invalid data length");
216 memData
.Data
= fdata
;
217 memData
.Length
= length
;
220 if(load_stream(create_stream(memData
), buffer
) == false)