wyAudioPlayer::~wyAudioPlayer() { // ensure audio is stopped stop(); // free temp buffer if(m_tempBuffer) { wyFree(m_tempBuffer); m_tempBuffer = NULL; } // free source if(m_stream->isSingleBuffer() && m_source != 0) { wyOpenAL::freeSource(m_source); } // free buffer alDeleteBuffers(m_stream->isSingleBuffer() ? 1 : MAX_BUFFER, m_buffers); wyFree(m_buffers); // release stream wyObjectRelease(m_stream); }
void KLAL_RebuildBuffer(KLAL* p) { if( !p ) p = &klal; p->current = 0;//(p->current+1)%KLAL_AL_RINGNUM; if(p->bufid) { alSourcei( p->src, AL_BUFFER, 0 ); #if KLAL_ERRCHECK ALenum aen = alGetError(); if( aen!=AL_NO_ERROR ) { KLLog("[ KLAL ] %s %u\n", __func__, aen); } #endif alDeleteBuffers( 1, &p->bufid ); #if KLAL_ERRCHECK aen = alGetError(); if( aen!=AL_NO_ERROR ) { KLLog("[ KLAL ] %s %u\n", __func__, aen); } #endif p->bufid=0; } alGenBuffers(1, &p->bufid); #if KLAL_ERRCHECK ALenum aen = alGetError(); if( aen!=AL_NO_ERROR ) { KLLog("[ KLAL ] %s %u\n", __func__, aen); } #endif }
void Audio::playAudioBuffer(ALuint alSource, const int16_t *data, int samples, unsigned channels, int sampleRate) { assert(channels == 1 || channels == 2); QMutexLocker locker(&audioLock); if (!(alOutDev && outputInitialized)) return; ALuint bufid; ALint processed = 0, queued = 16; alGetSourcei(alSource, AL_BUFFERS_PROCESSED, &processed); alGetSourcei(alSource, AL_BUFFERS_QUEUED, &queued); alSourcei(alSource, AL_LOOPING, AL_FALSE); if (processed) { ALuint bufids[processed]; alSourceUnqueueBuffers(alSource, processed, bufids); alDeleteBuffers(processed - 1, bufids + 1); bufid = bufids[0]; } else if (queued < 16) { alGenBuffers(1, &bufid); } else { return; } alBufferData(bufid, (channels == 1) ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16, data, samples * 2 * channels, sampleRate); alSourceQueueBuffers(alSource, 1, &bufid); ALint state; alGetSourcei(alSource, AL_SOURCE_STATE, &state); if (state != AL_PLAYING) alSourcePlay(alSource); }
SoundEffect::~SoundEffect() { for (size_t i = 0; i < m_children.size(); i++) { m_children[i]->Stop(); if (m_children[i]->m_isFireAndForget) { // return the instance to the queue m_instancePool.push_back(m_children[i]); } else { // unlink (we don't own the pointer, so it's someone else's job to delete the instance) m_children[i]->m_parent = nullptr; } } #if defined NXNA_AUDIOENGINE_OPENAL alDeleteBuffers(1, (ALuint*)&m_buffer); #endif }
/** * @brief Close active audio output device */ void OpenAL2::cleanupOutput() { OpenAL::cleanupOutput(); if (echoCancelSupported) { alcMakeContextCurrent(alOutContext); alSourceStop(alProxySource); ALint processed = 0; ALuint bufids[PROXY_BUFFER_COUNT]; alGetSourcei(alProxySource, AL_BUFFERS_PROCESSED, &processed); alSourceUnqueueBuffers(alProxySource, processed, bufids); alDeleteBuffers(processed, bufids); alcMakeContextCurrent(nullptr); alcDestroyContext(alOutContext); alOutContext = nullptr; alcCloseDevice(alOutDev); alOutDev = nullptr; } else { alOutContext = nullptr; alOutDev = nullptr; } }
SoundManager::~SoundManager() { // Clear all the sources. for (int sourceIndex = 0; sourceIndex < SOUNDMANAGER_MAX_NBR_OF_SOURCES; sourceIndex++) { ALuint source = mSourceIds[sourceIndex]; alDeleteSources(1, &source); ALenum error = alGetError(); if (error != AL_NO_ERROR) { reportOpenALError(error); } } // Clear the buffers by iterating through the hash table keys QHashIterator<ALuint, ALuint> iterator(mAttachedSourceIds); while (iterator.hasNext()) { iterator.next(); // Get the buffer id and delete it. ALuint bufferId = iterator.key(); alDeleteBuffers(1, &bufferId); ALenum error = alGetError(); if (error != AL_NO_ERROR) { reportOpenALError(error); } } // Clear the QHash for buffer IDs. mAttachedSourceIds.clear(); // Exit the ALUT. if (alutExit() == false) { reportALUTError(alutGetError()); } }
void KLAL_Unload(KLAL* p) { if( !p ) p = &klal; if(p->isLoaded) { KLAL_Stop(p); alSourcei( p->src, AL_BUFFER, 0 ); #if KLAL_ERRCHECK ALenum aen = alGetError(); if( aen!=AL_NO_ERROR ) { KLLog("[ KLAL ] \n\tFile:%s\n\tLine:%u\n\tErr:%x\n", __FUNCTION__, __LINE__, aen); } #endif alDeleteBuffers( 1, &p->bufid ); #if KLAL_ERRCHECK aen = alGetError(); if( aen!=AL_NO_ERROR ) { KLLog("[ KLAL ] \n\tFile:%s\n\tLine:%u\n\tErr:%x\n", __FUNCTION__, __LINE__, aen); } #endif p->bufid = 0; alDeleteSources( 1, &p->src ); #if KLAL_ERRCHECK aen = alGetError(); if( aen!=AL_NO_ERROR ) { KLLog("[ KLAL ] \n\tFile:%s\n\tLine:%u\n\tErr:%x\n", __FUNCTION__, __LINE__, aen); } #endif p->src = 0; sfree( p->pData ); p->isLoaded = 0; } }
static void stopBackground(bool bReleaseData) { alSourceStop(s_backgroundSource); if (bReleaseData) { for (BackgroundMusicsMap::iterator it = s_backgroundMusics.begin(); it != s_backgroundMusics.end(); ++it) { if (it->second->source == s_backgroundSource) { alDeleteBuffers(1, &it->second->buffer); checkALError("stopBackground"); alDeleteSources(1, &it->second->source); checkALError("stopBackground"); delete it->second; s_backgroundMusics.erase(it); break; } } } s_backgroundSource = AL_NONE; }
int play_sound_internal(Notification what, bool loop) { uint32_t source; uint32_t buffer; m_open_device(); alGenSources(1, &source); alGenBuffers(1, &buffer); buffer = alutCreateBufferFromFile(Control.sounds[what]); alSourcei(source, AL_BUFFER, buffer); alSourcei(source, AL_LOOPING, loop); int rc = play_source(source, buffer, loop); if (rc < 0) { alSourceStop(source); alDeleteSources(1, &source); alDeleteBuffers(1,&buffer); return -1; } return rc; }
void MALContext::deleteBuffer(unsigned int * bufferId) { M_PROFILE_SCOPE(MALContext::deleteBuffer); if(*bufferId > 0) { // unlink sources map<unsigned int, unsigned int>::iterator mit (m_sources.begin()), mend(m_sources.end()); for(;mit!=mend;++mit) { if(mit->second == (*bufferId)) { stopSource(mit->first); setSourceBufferId(mit->first, 0); } } // delete buffer alDeleteBuffers(1, bufferId); } }
void EdenSoundUnload(EdenSound_t **sound_p) { if (!sound_p) return; if (!(*sound_p)) return; #ifdef EDEN_HAVE_OPENAL alSourceStop((*sound_p)->uiSource); alDeleteSources(1, &(*sound_p)->uiSource); alDeleteBuffers(1, &(*sound_p)->uiBuffer); #endif // EDEN_HAVE_OPENAL free(*sound_p); *sound_p = NULL; // One-time cleanup. refCount--; if (refCount == 0) { #ifdef EDEN_HAVE_OPENAL ALFWShutdownOpenAL(); ALFWShutdown(); #endif // EDEN_HAVE_OPENAL } }
static void al_free(void *data) { al_t *al = (al_t*)data; if (!al) return; alSourceStop(al->source); alDeleteSources(1, &al->source); if (al->buffers) alDeleteBuffers(al->num_buffers, al->buffers); free(al->buffers); free(al->res_buf); alcMakeContextCurrent(NULL); if (al->ctx) alcDestroyContext(al->ctx); if (al->handle) alcCloseDevice(al->handle); free(al); }
void OpenAL_AudioInterface::update(float time_increase) { int i,state,nProcessed; OpenAL_Buffer buff; // process played buffers alGetSourcei(mSource,AL_BUFFERS_PROCESSED,&nProcessed); for (i=0;i<nProcessed;i++) { buff=mBufferQueue.front(); mBufferQueue.pop(); mNumPlayedSamples+=buff.nSamples; alSourceUnqueueBuffers(mSource,1,&buff.id); alDeleteBuffers(1,&buff.id); } // control playback and return time position alGetSourcei(mSource,AL_SOURCE_STATE,&state); if (state == AL_PLAYING) { alGetSourcef(mSource,AL_SEC_OFFSET,&mTime); mTime+=(float) mNumPlayedSamples/mFreq; mTimeOffset=0; } else { mTime=(float) mNumProcessedSamples/mFreq+mTimeOffset; mTimeOffset+=time_increase; } //Deactivated output //if (mTimeOffset > 0) printf("%.2f\n",mTimeOffset); float duration=mClip->getDuration(); if (mTime > duration) mTime=duration; }
/*! Destroyes the stream. */ Stream::~Stream() { if( isPlaying() ) { stopSound(); } release(); alDeleteBuffers( NUMBUFFERSOGG, _uiBuffers ); if( _pDecodeBuffer ) { free( _pDecodeBuffer ); _pDecodeBuffer = NULL; } //fn_ov_clear(sOggVorbisFile); if( _sOggVorbisFile ) { delete( _sOggVorbisFile ); } }
void SimpleAudioEngine::unloadEffect(const char* pszFilePath) { // Changing file path to full path std::string fullPath = FileUtils::getInstance()->fullPathForFilename(pszFilePath); EffectsMap::iterator iter = s_effects.find(fullPath); if (iter != s_effects.end()) { checkALError("unloadEffect:init"); alSourceStop(iter->second->source); checkALError("unloadEffect:alSourceStop"); alDeleteSources(1, &iter->second->source); checkALError("unloadEffect:DeletSources"); alDeleteBuffers(1, &iter->second->buffer); checkALError("unloadEffect:alDeleteBuffers"); delete iter->second; s_effects.erase(iter); } }
osaOpenAL::~osaOpenAL() { delete SoundSettings; SoundSettings = 0; delete CAIHeader; CAIHeader = 0; delete WAVHeader; WAVHeader = 0; if (SoundFile){ fclose(SoundFile); } if (Data){ alDeleteBuffers(1, SoundBuffer); alDeleteSources(1, SoundSource); delete[] Data; } if (!CloseOpenAL()) { CMN_LOG_CLASS_RUN_ERROR << "Failed to close OpenAL" << std::endl; } }
SoundAL::~SoundAL() { if (sourceId) { alSourceStop(sourceId); alSourcei(sourceId, AL_BUFFER, 0); alDeleteSources(1, &sourceId); if (AudioAL::checkOpenALError()) { log("Failed to delete OpenAL source"); } } if (outputBuffer) { alDeleteBuffers(1, &outputBuffer); if (AudioAL::checkOpenALError()) { log("Failed to delete OpenAL buffer"); } } }
DWORD WINAPI stream_run(void * data) { SoundThread * me = (SoundThread *) data; while(me->running) { if(me->sound) { Sound * sound = me->sound; ALuint uiSource; ALint iState = AL_PLAYING; ALuint uiBuffers[1]; alGenBuffers(1, uiBuffers); alGenSources(1, &uiSource); alBufferData(uiBuffers[0], sound->format, sound->data, sound->size, sound->freq); assert(AL_NO_ERROR == alGetError()); alSourceQueueBuffers(uiSource, 1, &uiBuffers[0]); alSourcef(uiSource, AL_GAIN, sound->volume); alSourcePlay(uiSource); while(iState == AL_PLAYING) { Sleep(16); alGetSourcei(uiSource, AL_SOURCE_STATE, &iState); } alSourceStop(uiSource); alSourcei(uiSource, AL_BUFFER, 0); alDeleteSources(1, &uiSource); alDeleteBuffers(1, uiBuffers); me->occupied = false; me->sound = 0; } if(me->running) SuspendThread(me->handle); } return 0; }
/** Play a 44100Hz mono 16bit PCM sound */ void Audio::playMono16Sound(const QByteArray& data) { QMutexLocker lock(&audioOutLock); if (!alOutDev) { if (!openOutput(Settings::getInstance().getOutDev())) return; } ALuint buffer; alGenBuffers(1, &buffer); alBufferData(buffer, AL_FORMAT_MONO16, data.data(), data.size(), 44100); alSourcef(alMainSource, AL_GAIN, outputVolume); alSourcei(alMainSource, AL_BUFFER, buffer); alSourcePlay(alMainSource); ALint sizeInBytes; ALint channels; ALint bits; alGetBufferi(buffer, AL_SIZE, &sizeInBytes); alGetBufferi(buffer, AL_CHANNELS, &channels); alGetBufferi(buffer, AL_BITS, &bits); int lengthInSamples = sizeInBytes * 8 / (channels * bits); ALint frequency; alGetBufferi(buffer, AL_FREQUENCY, &frequency); qreal duration = (lengthInSamples / static_cast<qreal>(frequency)) * 1000; int remaining = timer->interval(); if (duration > remaining) timer->start(duration); alDeleteBuffers(1, &buffer); }
void playSound() { char * sound [] = {"scale-a6.wav", "scale-c6.wav", "scale-c7.wav" , "scale-d6.wav", "scale-e6.wav", "scale-f6.wav", "scale-g6.wav", "scale-h6.wav"}; int index = rand() % 8; ALuint buffer, source; ALuint state; // Initialize the environment alutInit(0, NULL); // Capture errors alGetError(); // Load pcm data into buffer buffer = alutCreateBufferFromFile(sound[index]); // Create sound source (use buffer to fill source) alGenSources(1, &source); alSourcei(source, AL_BUFFER, buffer); // Play alSourcePlay(source); // Wait for the song to complete do { alGetSourcei(source, AL_SOURCE_STATE, &state); } while (state == AL_PLAYING); // Clean up sources and buffers alDeleteSources(1, &source); alDeleteBuffers(1, &buffer); // Exit everything alutExit(); }
void CsAudioManager::Cleanup() { //for (int i = 0; i < mSourceList.size(); i++) { // mSourceList[i]->UnloadSource(); //} mSourceList.clear(); alDeleteBuffers(NUM_SOUNDS, Buffer); ALCcontext* pCurContext; ALCdevice* pCurDevice; // Get the current context. pCurContext = alcGetCurrentContext(); // Get the device used by that context. pCurDevice = alcGetContextsDevice(pCurContext); // Reset the current context to NULL. alcMakeContextCurrent(0); // Release the context and the device. alcDestroyContext(pCurContext); alcCloseDevice(pCurDevice); }
VoiceMessages::~VoiceMessages() { { QMutexLocker lock(&voicemsgsMutex); voicemsgs = 0; } for (int32 i = 0; i < AudioVoiceMsgSimultaneously; ++i) { alSourceStop(_data[i].source); if (alIsBuffer(_data[i].buffers[0])) { alDeleteBuffers(3, _data[i].buffers); for (int32 j = 0; j < 3; ++j) { _data[i].buffers[j] = _data[i].samplesCount[j] = 0; } } if (alIsSource(_data[i].source)) { alDeleteSources(1, &_data[i].source); _data[i].source = 0; } } _faderThread.quit(); _loaderThread.quit(); _faderThread.wait(); _loaderThread.wait(); }
void ttLibC_AlPlayer_close(ttLibC_AlPlayer **player) { ttLibC_AlPlayer_ *target = (ttLibC_AlPlayer_ *)*player; if(target == NULL) { return; } if(target->source != 0) { alDeleteSources(1, &target->source); } for(uint32_t i = 0;i < target->total_buffer_num;++ i) { if(target->buffers[i] != 0) { alDeleteBuffers(1, &target->buffers[i]); target->buffers[i] = 0; } } if(target->context != NULL) { alcMakeContextCurrent(NULL); alcDestroyContext(target->context); } if(target->device != NULL) { alcCloseDevice(target->device); } ttLibC_free(target); *player = NULL; }
void x_async_snd_shutdown() { if (!g_sound.g_audio_enable) return ; printf("async_snd_shutdown\n"); if (!dev) { printf("err: openal device not initialized\n"); return ; } alDeleteSources(1, &source); source = 0; alDeleteBuffers(AL_NUM_BUFFERS, buffers); alcMakeContextCurrent(NULL); alcDestroyContext(ctx); ctx=NULL; alcCloseDevice(dev); dev=NULL; }
// // background audio // void SimpleAudioEngine::preloadBackgroundMusic(const char* pszFilePath) { if (!s_isBackgroundInitialized || s_currentBackgroundStr != pszFilePath) { string path = pszFilePath; if (isOGGFile(path.data())) { s_backgroundBuffer = createBufferFromOGG(path.data()); } else { s_backgroundBuffer = alutCreateBufferFromFile(path.data()); } checkALError("preloadBackgroundMusic"); if (s_backgroundBuffer == AL_NONE) { fprintf(stderr, "Error loading file: '%s'\n", path.data()); alDeleteBuffers(1, &s_backgroundBuffer); return; } alGenSources(1, &s_backgroundSource); checkALError("preloadBackgroundMusic"); alSourcei(s_backgroundSource, AL_BUFFER, s_backgroundBuffer); checkALError("preloadBackgroundMusic"); s_currentBackgroundStr = pszFilePath; } s_currentBackgroundStr = pszFilePath; s_isBackgroundInitialized = true; }
//////////////////////////////////////////////////////////// /// Load sound into buffer //////////////////////////////////////////////////////////// void SoundBufferLoad(struct Sound * SoundStruct, char * File) { UInt32 MinorTime = 0; // Minor time means the most old file UInt32 Dimension = 0; // Dimension found Int32 i = 0; // Search if the sound is loaded for (i = 0; i < SOUND_BUFFER_SIZE; i++) { if (SoundData[i].File != NULL) { if (strcmp(SoundData[i].File, File) == 0) { // The sound is loaded.. just play it SoundStruct = &SoundData[i]; return; } } } // Search free sound for (i = 0; i < SOUND_BUFFER_SIZE; i++) { if (SoundData[i].Buffer == 0) { // Load sound if (SOUND_FROM_MEMORY) { char * BufferData; UInt32 BufferSize; // Load buffer from pack // if (!PackLoadData(File, BufferData, &BufferSize)) // { // return; // } // Load buffer data from memory if (SoundFileOpenReadFromMemory(SoundStruct, BufferData, BufferSize)) { // Destroy data Delete(BufferData); return; } } else { if (SoundFileOpenReadFromFile(SoundStruct, File)) { strcpy(SoundStruct->File, File); return; } } } } // Search last used for (i = 0; i < SOUND_BUFFER_SIZE; i++) { if (SoundStatus(&SoundData[i]) == Stopped) { if (SoundData[i].Time < MinorTime) { MinorTime = SoundData[i].Time; Dimension = i; } } } if (MinorTime != 0) { if (SoundData[Dimension].Buffer != 0) { // Remove data from the previous sound ALCheck(alDeleteBuffers(1, &SoundData[Dimension].Buffer)); // Delete source ALCheck(alDeleteSources(1, &SoundData[Dimension].Source)); } // Return last used SoundStruct = &SoundData[Dimension]; } else { // There isn't sounds available SoundStruct = NULL; } }
/* * void KillALData() * * We have allocated memory for our buffers and sources which needs * to be returned to the system. This function frees that memory. */ void KillALData() { alDeleteBuffers(1, &Buffer); alDeleteSources(1, &Source); alutExit(); }
void dll_al_DeleteBuffers(ALsizei n, ALuint *buffers) { alDeleteBuffers(n, buffers); }
bool SoundBuffer::Create(AudioFormat format, unsigned int sampleCount, unsigned int sampleRate, const Int16* samples) { Destroy(); #if NAZARA_AUDIO_SAFE if (!IsFormatSupported(format)) { NazaraError("Audio format is not supported"); return false; } if (sampleCount == 0) { NazaraError("Sample rate must be different from zero"); return false; } if (sampleRate == 0) { NazaraError("Sample rate must be different from zero"); return false; } if (!samples) { NazaraError("Invalid sample source"); return false; } #endif // On vide le stack d'erreurs while (alGetError() != AL_NO_ERROR); ALuint buffer; alGenBuffers(1, &buffer); if (alGetError() != AL_NO_ERROR) { NazaraError("Failed to create OpenAL buffer"); return false; } alBufferData(buffer, OpenAL::AudioFormat[format], samples, sampleCount*sizeof(Int16), sampleRate); if (alGetError() != AL_NO_ERROR) { alDeleteBuffers(1, &buffer); NazaraError("Failed to set OpenAL buffer"); return false; } m_impl = new SoundBufferImpl; m_impl->buffer = buffer; m_impl->duration = (1000*sampleCount / (format * sampleRate)); m_impl->format = format; m_impl->sampleCount = sampleCount; m_impl->sampleRate = sampleRate; m_impl->samples.reset(new Int16[sampleCount]); std::memcpy(&m_impl->samples[0], samples, sampleCount*sizeof(Int16)); return true; }
/// Initialize OpenAl and return true on success. // TODO rewrite with std::unique_ptr and custom deleter to remove long // chain of "destructor" code. void Sound::Impl::InitOpenAL() { ALCcontext *context; ALCdevice *device; ALenum error_code; ALboolean status; device = alcOpenDevice(nullptr); if (!device) { ErrorLogger() << "Unable to initialise default OpenAL device."; m_initialized = false; return; } context = alcCreateContext(device, nullptr); if (!context) { error_code = alGetError(); ErrorLogger() << "Unable to create OpenAL context: " << alGetString(error_code) << "\n"; alcCloseDevice(device); m_initialized = false; return; } status = alcMakeContextCurrent(context); error_code = alGetError(); if (status != AL_TRUE || error_code != AL_NO_ERROR) { ErrorLogger() << "Unable to make OpenAL context current: " << alGetString(error_code) << "\n"; alcDestroyContext(context); alcCloseDevice(device); m_initialized = false; return; } alListenerf(AL_GAIN, 1.0); error_code = alGetError(); if (error_code != AL_NO_ERROR) { ErrorLogger() << "Unable to create OpenAL listener: " << alGetString(error_code) << "\n" << "Disabling OpenAL sound system!\n"; alcMakeContextCurrent(nullptr); alcDestroyContext(context); alcCloseDevice(device); m_initialized = false; return; } alGenSources(NUM_SOURCES, m_sources); error_code = alGetError(); if (error_code != AL_NO_ERROR) { ErrorLogger() << "Unable to create OpenAL sources: " << alGetString(error_code) << "\n" << "Disabling OpenAL sound system!\n"; alcMakeContextCurrent(nullptr); alcDestroyContext(context); alcCloseDevice(device); m_initialized = false; return; } alGenBuffers(NUM_MUSIC_BUFFERS, m_music_buffers); error_code = alGetError(); if (error_code != AL_NO_ERROR) { ErrorLogger() << "Unable to create OpenAL buffers: " << alGetString(error_code) << "\n" << "Disabling OpenAL sound system!\n"; alDeleteSources(NUM_SOURCES, m_sources); alcMakeContextCurrent(nullptr); alcDestroyContext(context); alcCloseDevice(device); m_initialized = false; return; } for (int i = 0; i < NUM_SOURCES; ++i) { alSourcei(m_sources[i], AL_SOURCE_RELATIVE, AL_TRUE); error_code = alGetError(); if (error_code != AL_NO_ERROR) { ErrorLogger() << "Unable to set OpenAL source to relative: " << alGetString(error_code) << "\n" << "Disabling OpenAL sound system!\n"; alDeleteBuffers(NUM_MUSIC_BUFFERS, m_music_buffers); alDeleteSources(NUM_SOURCES, m_sources); alcMakeContextCurrent(nullptr); alcDestroyContext(context); alcCloseDevice(device); m_initialized = false; return; } } DebugLogger() << "OpenAL initialized. Version " << alGetString(AL_VERSION) << " Renderer " << alGetString(AL_RENDERER) << " Vendor " << alGetString(AL_VENDOR) << "\n" << " Extensions: " << alGetString(AL_EXTENSIONS) << "\n"; m_initialized = true; }