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 static bool load_stream(alureStream
*_stream
, ALuint buffer
)
41 std::auto_ptr
<std::istream
> fstream(_stream
->fstream
);
42 std::auto_ptr
<alureStream
> stream(_stream
);
45 ALuint freq
, blockAlign
;
47 if(!stream
->GetFormat(&format
, &freq
, &blockAlign
))
49 SetError("Could not get sample format");
55 SetError("No valid format");
60 SetError("Invalid block size");
65 SetError("Invalid sample rate");
69 ALuint writePos
= 0, got
;
70 std::vector
<ALubyte
> data(freq
*blockAlign
);
71 while((got
=stream
->GetData(&data
[writePos
], data
.size()-writePos
)) > 0)
74 data
.resize(writePos
+ freq
*blockAlign
);
76 data
.resize(writePos
- (writePos
%blockAlign
));
79 alBufferData(buffer
, format
, &data
[0], data
.size(), freq
);
80 if(alGetError() != AL_NO_ERROR
)
82 SetError("Buffer load failed");
91 /* Function: alureCreateBufferFromFile
93 * Loads the given file into a new OpenAL buffer object. The formats supported
94 * depend on the options the library was compiled with, what libraries are
95 * available at runtime, and the installed decode callbacks. Requires an active
99 * A new buffer ID with the loaded sound, or AL_NONE on error.
102 * <alureBufferDataFromFile>
104 ALURE_API ALuint ALURE_APIENTRY
alureCreateBufferFromFile(const ALchar
*fname
)
106 if(alGetError() != AL_NO_ERROR
)
108 SetError("Existing OpenAL error");
113 alGenBuffers(1, &buf
);
114 if(alGetError() != AL_NO_ERROR
)
116 SetError("Buffer creation failed");
120 if(alureBufferDataFromFile(fname
, buf
) == AL_FALSE
)
122 alDeleteBuffers(1, &buf
);
130 /* Function: alureCreateBufferFromMemory
132 * Loads a file image from memory into a new OpenAL buffer object, similar to
133 * alureCreateBufferFromFile. Requires an active context.
136 * A new buffer ID with the loaded sound, or AL_NONE on error.
139 * <alureBufferDataFromMemory>
141 ALURE_API ALuint ALURE_APIENTRY
alureCreateBufferFromMemory(const ALubyte
*fdata
, ALsizei length
)
143 if(alGetError() != AL_NO_ERROR
)
145 SetError("Existing OpenAL error");
150 alGenBuffers(1, &buf
);
151 if(alGetError() != AL_NO_ERROR
)
153 SetError("Buffer creation failed");
157 if(alureBufferDataFromMemory(fdata
, length
, buf
) == AL_FALSE
)
159 alDeleteBuffers(1, &buf
);
167 /* Function: alureBufferDataFromFile
169 * Loads the given file into an existing OpenAL buffer object. The previous
170 * contents of the buffer are replaced. Requires an active context.
176 * <alureCreateBufferFromFile>
178 ALURE_API ALboolean ALURE_APIENTRY
alureBufferDataFromFile(const ALchar
*fname
, ALuint buffer
)
180 if(alGetError() != AL_NO_ERROR
)
182 SetError("Existing OpenAL error");
186 if(!buffer
|| !alIsBuffer(buffer
))
188 SetError("Invalid buffer ID");
192 if(load_stream(create_stream(fname
), buffer
) == false)
197 /* Function: alureBufferDataFromMemory
199 * Loads a file image from memory into an existing OpenAL buffer object,
200 * similar to alureBufferDataFromFile. Requires an active context.
206 * <alureCreateBufferFromMemory>
208 ALURE_API ALboolean ALURE_APIENTRY
alureBufferDataFromMemory(const ALubyte
*fdata
, ALsizei length
, ALuint buffer
)
210 if(alGetError() != AL_NO_ERROR
)
212 SetError("Existing OpenAL error");
216 if(!buffer
|| !alIsBuffer(buffer
))
218 SetError("Invalid buffer ID");
224 SetError("Invalid data length");
229 memData
.Data
= fdata
;
230 memData
.Length
= length
;
233 if(load_stream(create_stream(memData
), buffer
) == false)