| 257 | } |
| 258 | |
| 259 | bool sbuffer::load(bool trydl) |
| 260 | { |
| 261 | if(!name) return false; |
| 262 | if(id) return true; |
| 263 | alclearerr(); |
| 264 | alGenBuffers(1, &id); |
| 265 | if(!ALERR) |
| 266 | { |
| 267 | const char *exts[] = { "", ".wav", ".ogg" }; |
| 268 | string filepath; |
| 269 | loopi(sizeof(exts)/sizeof(exts[0])) |
| 270 | { |
| 271 | formatstring(filepath)("packages/audio/%s%s", name, exts[i]); |
| 272 | stream *f = openfile(path(filepath), "rb"); |
| 273 | if(!f) continue; |
| 274 | |
| 275 | size_t len = strlen(filepath); |
| 276 | if(len >= 4 && !strcasecmp(filepath + len - 4, ".ogg")) |
| 277 | { |
| 278 | OggVorbis_File oggfile; |
| 279 | if(!ov_open_callbacks(f, &oggfile, NULL, 0, oggcallbacks)) |
| 280 | { |
| 281 | vorbis_info *info = ov_info(&oggfile, -1); |
| 282 | |
| 283 | const size_t BUFSIZE = 32*1024; |
| 284 | vector<char> buf; |
| 285 | int bitstream; |
| 286 | long bytes; |
| 287 | |
| 288 | do |
| 289 | { |
| 290 | char buffer[BUFSIZE]; |
| 291 | bytes = ov_read(&oggfile, buffer, BUFSIZE, isbigendian(), 2, 1, &bitstream); |
| 292 | loopi(bytes) buf.add(buffer[i]); |
| 293 | } while(bytes > 0); |
| 294 | |
| 295 | alBufferData(id, info->channels == 2 ? AL_FORMAT_STEREO16 : AL_FORMAT_MONO16, buf.getbuf(), buf.length(), info->rate); |
| 296 | ov_clear(&oggfile); |
| 297 | } |
| 298 | else |
| 299 | { |
| 300 | delete f; |
| 301 | continue; |
| 302 | } |
| 303 | } |
| 304 | else |
| 305 | { |
| 306 | SDL_AudioSpec wavspec; |
| 307 | uint32_t wavlen; |
| 308 | uint8_t *wavbuf; |
| 309 | |
| 310 | if(!SDL_LoadWAV_RW(f->rwops(), 1, &wavspec, &wavbuf, &wavlen)) |
| 311 | { |
| 312 | SDL_ClearError(); |
| 313 | continue; |
| 314 | } |
| 315 | |
| 316 | ALenum format; |
nothing calls this directly
no test coverage detected