Audio::Audio(const int _buffAl,const char* dir): buffAl(_buffAl) { load=false; ALFWInit(); // Initialize Framework if (!ALFWInitOpenAL()) { if (!ALFWInitOpenAL()) { ALFWprintf("Failed to initialize OpenAL\n"); ALFWShutdown(); return; } } // Generate an AL Buffer alGenBuffers(buffAl, &uiBuffer ); if (!ALFWLoadWaveToBuffer((char*)ALFWaddMediaPath(dir), uiBuffer)) { ALFWprintf("Failed to load %s\n", ALFWaddMediaPath(dir)); return; } load=true; lastTime = 0; }
Sound::Sound( const std::string& fn) : m_source(0), m_buffer(1, 0), m_initialised(true) { alGenBuffers(1, &m_buffer[0]); if( !ALFWLoadWaveToBuffer(fn.c_str(), m_buffer[0]) ){ alDeleteBuffers(m_buffer.size(), m_buffer.data()); throw std::runtime_error("Error loading " + fn); } alGenSources(1, &m_source); alSourcei(m_source, AL_BUFFER, m_buffer[0]); }
void PlayWavLoop(void * param) { ALuint uiBuffer; ALuint uiSource; ALint iState; t *args = (t*) param; char sound[30]; sprintf(sound, args->wavFile); //int y = args->data2; free(args); // Generate an AL Buffer alGenBuffers( 1, &uiBuffer ); // Load Wave file into OpenAL Buffer if (!ALFWLoadWaveToBuffer(sound, uiBuffer)) { sprintf(g_ALError, "Failed to load %s\n", sound); return; } // Generate a Source to playback the Buffer alGenSources( 1, &uiSource ); // Attach Source to Buffer alSourcei( uiSource, AL_BUFFER, uiBuffer ); while (true) { // loop forever // Play Source alSourcePlay( uiSource ); sprintf(g_ALError, "Playing Source "); do { Sleep(100); // Get Source State alGetSourcei( uiSource, AL_SOURCE_STATE, &iState); } while (iState == AL_PLAYING); } // Clean up by deleting Source(s) and Buffer(s) alSourceStop(uiSource); alDeleteSources(1, &uiSource); alDeleteBuffers(1, &uiBuffer); }
SoundSource::SoundSource(string soundFileName ) :VisualObject( ), soundFileName(soundFileName) { // Initialize OpenAL if it has not been initialized yet if ( SoundSource::soundInitialized == false ) { OpenALInit(); SoundSource::soundInitialized = true; } // Load the sound ALuint uiBuffer; // Generate an AL Buffer alGenBuffers( 1, &uiBuffer ); // Load Wave file into OpenAL Buffer if (!ALFWLoadWaveToBuffer(soundFileName.c_str(), uiBuffer)) { ALFWprintf("Failed to load %s.\n", soundFileName.c_str()); } else { ALFWprintf("Loaded %s sound file.\n", soundFileName.c_str()); // Generate a Source to playback the Buffer alGenSources( 1, &uiSource ); // Attach Source to Buffer alSourcei( uiSource, AL_BUFFER, uiBuffer ); // Set source properties alSourcef(uiSource, AL_PITCH, 1.0f); // pitch multiplier alSourcef(uiSource, AL_GAIN, 1.0f); // source gain value // determines if the positions are relative to the listener alSourcei(uiSource, AL_SOURCE_RELATIVE, AL_FALSE); // Position for sound sources alSource3f( uiSource, AL_POSITION, 0.0, 0.0, 0.0); // Velocity for sound sources alSource3f(uiSource, AL_VELOCITY, 0.0, 0.0, 0.0); } } // end SoundSource constructor
Sound::Sound( const std::vector< std::string >& fns) : m_source(0), m_buffer(fns.size(), 0), m_initialised(true) { alGenBuffers(fns.size(), m_buffer.data()); for(size_t i = 0; i < m_buffer.size(); ++i){ if (!ALFWLoadWaveToBuffer(fns[i].c_str(), m_buffer[i])){ alDeleteBuffers(m_buffer.size(), m_buffer.data()); throw std::runtime_error("Error loading " + fns[i]); } } alGenSources(1, &m_source); //always one source alSourcei(m_source, AL_BUFFER, m_buffer[0]); //for now attach first buffer only }
void SoundSource::initialize() { ALuint uiBuffer; // Generate an AL Buffer alGenBuffers( 1, &uiBuffer ); // Load Wave file into OpenAL Buffer if (!ALFWLoadWaveToBuffer(soundFileName.c_str(), uiBuffer)) { ALFWprintf("Failed to load %s.\n", soundFileName.c_str()); } else { //ALFWprintf("Loaded %s sound file.\n", soundFileName.c_str()); } // Generate a Source to playback the Buffer alGenSources( 1, &uiSource ); // Attach Source to Buffer alSourcei( uiSource, AL_BUFFER, uiBuffer ); // Set source properties alSourcef(uiSource, AL_PITCH, 1.0f); // pitch multiplier alSourcef(uiSource, AL_GAIN, 1.0f); // source gain value // determines if the positions are relative to the listener alSourcei(uiSource, AL_SOURCE_RELATIVE, AL_FALSE); // turns looping on (AL_TRUE) or off (AL_FALSE) alSourcei(uiSource, AL_LOOPING, AL_TRUE); // Position for sound sources alSource3f( uiSource, AL_POSITION, 0.0, 0.0, 0.0); // Velocity for sound sources alSource3f(uiSource, AL_VELOCITY, 0.0, 0.0, 0.0); //alSourcePlay( uiSource); //play(); VisualObject::initialize(); } // end initialize
BOOL XOALObj::Load( LPCSTR szFilename ) { // ALenum error; // Create some OpenAL Buffer Objects alGenBuffers(1, &buffer); // Load Wave file into OpenAL Buffer if( !ALFWLoadWaveToBuffer( szFilename, buffer ) ) { // XLOG_ALERT( "Failed to load %s", szFilename ); return FALSE; } // Create some OpenAL Source Objects alGenSources(1, &source); { ALenum error = AL_NO_ERROR; alGetError(); // Clear the error // Turn Looping ON // alSourcei(source, AL_LOOPING, AL_TRUE); alSourcei(source, AL_LOOPING, AL_FALSE); // CGPoint sourcePos; // sourcePos = CGPointMake(0., -70.); // Set Source Position // float sourcePosAL[] = {sourcePos.x, 25.0, sourcePos.y}; // alSourcefv(source, AL_POSITION, sourcePosAL); // Set Source Reference Distance // alSourcef(source, AL_REFERENCE_DISTANCE, 50.0f); // attach OpenAL Buffer to OpenAL Source alSourcei(source, AL_BUFFER, buffer); if((error = alGetError()) != AL_NO_ERROR) { // NSLog(@"Error attaching buffer to source: %x\n", error); // exit(1); XBREAK(1); } } return TRUE; }
EdenSound_t *EdenSoundLoad(const char *pathname) { EdenSound_t *sound = NULL; #ifdef EDEN_HAVE_OPENAL // One-time initialisation. if (refCount == 0) { #ifdef EDEN_HAVE_OPENAL ALFWInit(); if (!ALFWInitOpenAL()) { ALFWprintf("Failed to initialize OpenAL\n"); ALFWShutdown(); } #endif // EDEN_HAVE_OPENAL } if (!(sound = (EdenSound_t *)calloc(1, sizeof(EdenSound_t)))) { EDEN_LOGe("Out of memory!\n"); return (NULL); } #ifdef EDEN_HAVE_OPENAL alGenBuffers(1, &sound->uiBuffer); #ifdef _WIN32 if (!ALFWLoadWaveToBuffer(pathname, sound->uiBuffer)) { EDEN_LOGe("Failed to load .wav file '%s'.\n", pathname); } #endif #ifdef __APPLE__ if (!ALFWLoadFileToBuffer(pathname, sound->uiBuffer)) { EDEN_LOGe("Failed to load audio file '%s'.\n", pathname); } #endif alGenSources(1, &sound->uiSource); alSourcei(sound->uiSource, AL_BUFFER, sound->uiBuffer); #endif // EDEN_HAVE_OPENAL refCount++; #endif // EDEN_HAVE_OPENAL return (sound); }