static ALenum warn_if_error(ALenum err, const char *desc) { if(err == AL_NO_ERROR) return err; errorstream<<"WARNING: "<<desc<<": "<<alErrorString(err)<<std::endl; return err; }
int Sound_Buffer::Loader::function() { #ifndef DISABLE_AL std::pair<ALuint, float> loaded_ogg = load_ogg_vorbis(m_filename); //if(loaded_ogg.first == AL_NONE) // loaded_ogg.first = alutCreateBufferFromFile(m_filename.c_str()); if(loaded_ogg.first == AL_NONE) { std::cerr << "OpenAL error on '" << m_filename << "': " << alErrorString(alGetError()) << std::endl; return -1; } m_buffer = loaded_ogg.first; m_duration = loaded_ogg.second; #endif return 0; }
Sound_Buffer::Sound_Buffer() : m_buffer(AL_NONE), m_loader(0), m_thread(0) { #ifndef DISABLE_AL Sound &sr = get_Sound(); if(dynamic_cast<Sound_AL *>(&sr)) { alGenBuffers(1, &m_buffer); if(m_buffer == AL_NONE) { std::cerr << "OpenAL error on alGenBuffers: " << alErrorString(alGetError()) << std::endl; throw Sound_Buffer_Init_Failure(); } char buffer[64]; memset(buffer, 0, sizeof(buffer)); alBufferData(m_buffer, AL_FORMAT_MONO16, buffer, sizeof(buffer), 22050); } #endif }
SoundBuffer* loadOggFile(const std::string &filepath) { int endian = 0; // 0 for Little-Endian, 1 for Big-Endian int bitStream; long bytes; char array[BUFFER_SIZE]; // Local fixed size array vorbis_info *pInfo; OggVorbis_File oggFile; // Do a dumb-ass static string copy for old versions of ov_fopen // because they expect a non-const char* char nonconst[10000]; snprintf(nonconst, 10000, "%s", filepath.c_str()); // Try opening the given file //if(ov_fopen(filepath.c_str(), &oggFile) != 0) if(ov_fopen(nonconst, &oggFile) != 0) { infostream<<"Audio: Error opening "<<filepath<<" for decoding"<<std::endl; return NULL; } SoundBuffer *snd = new SoundBuffer; // Get some information about the OGG file pInfo = ov_info(&oggFile, -1); // Check the number of channels... always use 16-bit samples if(pInfo->channels == 1) snd->format = AL_FORMAT_MONO16; else snd->format = AL_FORMAT_STEREO16; // The frequency of the sampling rate snd->freq = pInfo->rate; // Keep reading until all is read do { // Read up to a buffer's worth of decoded sound data bytes = ov_read(&oggFile, array, BUFFER_SIZE, endian, 2, 1, &bitStream); if(bytes < 0) { ov_clear(&oggFile); infostream<<"Audio: Error decoding "<<filepath<<std::endl; return NULL; } // Append to end of buffer snd->buffer.insert(snd->buffer.end(), array, array + bytes); } while (bytes > 0); alGenBuffers(1, &snd->buffer_id); alBufferData(snd->buffer_id, snd->format, &(snd->buffer[0]), snd->buffer.size(), snd->freq); ALenum error = alGetError(); if(error != AL_NO_ERROR){ infostream<<"Audio: OpenAL error: "<<alErrorString(error) <<"preparing sound buffer"<<std::endl; } infostream<<"Audio file "<<filepath<<" loaded"<<std::endl; // Clean up! ov_clear(&oggFile); return snd; }
SoundBuffer *load_opened_ogg_file(OggVorbis_File *oggFile, const std::string &filename_for_logging) { int endian = 0; // 0 for Little-Endian, 1 for Big-Endian int bitStream; long bytes; char array[BUFFER_SIZE]; // Local fixed size array vorbis_info *pInfo; SoundBuffer *snd = new SoundBuffer; // Get some information about the OGG file pInfo = ov_info(oggFile, -1); // Check the number of channels... always use 16-bit samples if(pInfo->channels == 1) snd->format = AL_FORMAT_MONO16; else snd->format = AL_FORMAT_STEREO16; // The frequency of the sampling rate snd->freq = pInfo->rate; // Keep reading until all is read do { // Read up to a buffer's worth of decoded sound data bytes = ov_read(oggFile, array, BUFFER_SIZE, endian, 2, 1, &bitStream); if(bytes < 0) { ov_clear(oggFile); infostream << "Audio: Error decoding " << filename_for_logging << std::endl; return NULL; } // Append to end of buffer snd->buffer.insert(snd->buffer.end(), array, array + bytes); } while (bytes > 0); alGenBuffers(1, &snd->buffer_id); alBufferData(snd->buffer_id, snd->format, &(snd->buffer[0]), snd->buffer.size(), snd->freq); ALenum error = alGetError(); if(error != AL_NO_ERROR){ infostream<<"Audio: OpenAL error: "<<alErrorString(error) <<"preparing sound buffer"<<std::endl; } infostream << "Audio file " << filename_for_logging << " loaded" << std::endl; // Clean up! ov_clear(oggFile); return snd; }