audio_context::audio_context(const audio_settings& settings) 
		: device(settings.output_device_name) 
	{
#if BUILD_OPENAL
		context = alcCreateContext(device, nullptr);

		if (!context || !set_as_current()) {
			if (context) {
				AL_CHECK(alcDestroyContext(context));
			}

			throw audio_error(
				"Failed to set an OpenAL context on device: %x", 
				settings.output_device_name
			);
		}

		AL_CHECK(alEnable(AL_SOURCE_DISTANCE_MODEL));

		device.log_hrtf_status();

		apply(settings, true);

		{
			ALint num_stereo_sources;
			ALint num_mono_sources;

			alcGetIntegerv(device, ALC_STEREO_SOURCES, 1, &num_stereo_sources);
			alcGetIntegerv(device, ALC_MONO_SOURCES, 1, &num_mono_sources);

			LOG_NVPS(num_stereo_sources, num_mono_sources);
		}
#endif
	}
Example #2
0
AudioSource::AudioSource(AudioBuffer* buffer, ALuint source) 
    : _alSource(source), _buffer(buffer), _looped(false), _gain(1.0f), _pitch(1.0f), _node(NULL)
{
    GP_ASSERT(buffer);
    AL_CHECK( alSourcei(_alSource, AL_BUFFER, buffer->_alBuffer) );
    AL_CHECK( alSourcei(_alSource, AL_LOOPING, _looped) );
    AL_CHECK( alSourcef(_alSource, AL_PITCH, _pitch) );
    AL_CHECK( alSourcef(_alSource, AL_GAIN, _gain) );
    AL_CHECK( alSourcefv(_alSource, AL_VELOCITY, (const ALfloat*)&_velocity) );
}
Example #3
0
void AudioController::update(float elapsedTime)
{
    AudioListener* listener = AudioListener::getInstance();
    if (listener)
    {
        AL_CHECK( alListenerf(AL_GAIN, listener->getGain()) );
        AL_CHECK( alListenerfv(AL_ORIENTATION, (ALfloat*)listener->getOrientation()) );
        AL_CHECK( alListenerfv(AL_VELOCITY, (ALfloat*)&listener->getVelocity()) );
        AL_CHECK( alListenerfv(AL_POSITION, (ALfloat*)&listener->getPosition()) );
    }
}
Example #4
0
int addSource(int bufferId) {
	
	ALuint src = 0;
	alGenSources(1, &src );
		
	LOG_DEBUG("addSource( %i ) -> id %i", bufferId, src);	
	AL_CHECK();
	
	alSourcei(src, AL_BUFFER, bufferId);
	alSourcef(src, AL_ROLLOFF_FACTOR,  0.25);
	
	AL_CHECK();
	return src;
}
AudioBuffer::~AudioBuffer()
{
    // Remove the buffer from the cache.
    unsigned int bufferCount = (unsigned int)__buffers.size();

    if (!_streamed)
    {
        unsigned int bufferCount = (unsigned int)__buffers.size();
        for (unsigned int i = 0; i < bufferCount; i++)
        {
            if (this == __buffers[i])
            {
                __buffers.erase(__buffers.begin() + i);
                break;
            }
        }
    }
    else if (_streamStateOgg.get())
    {
        ov_clear(&_streamStateOgg->oggFile);
    }

    for (int i = 0; i < STREAMING_BUFFER_QUEUE_SIZE; i++)
    {
        if (_alBufferQueue[i])
        {
            AL_CHECK(alDeleteBuffers(1, &_alBufferQueue[i]));
            _alBufferQueue[i] = 0;
        }
    }
}
Example #6
0
AudioSource* AudioSource::clone(NodeCloneContext &context) const
{
    GP_ASSERT(_buffer);

    ALuint alSource = 0;
    AL_CHECK( alGenSources(1, &alSource) );
    if (AL_LAST_ERROR())
    {
        GP_ERROR("Error generating audio source.");
        return NULL;
    }
    AudioSource* audioClone = new AudioSource(_buffer, alSource);

    _buffer->addRef();
    audioClone->setLooped(isLooped());
    audioClone->setGain(getGain());
    audioClone->setPitch(getPitch());
    audioClone->setVelocity(getVelocity());
    if (Node* node = getNode())
    {
        Node* clonedNode = context.findClonedNode(node);
        if (clonedNode)
        {
            audioClone->setNode(clonedNode);
        }
    }
    return audioClone;
}
Example #7
0
void AudioSource::transformChanged(Transform* transform, long cookie)
{
    if (_node)
    {
        Vector3 translation = _node->getTranslationWorld();
        AL_CHECK( alSourcefv(_alSource, AL_POSITION, (const ALfloat*)&translation.x) );
    }
}
Example #8
0
AudioDevice::AudioDevice() {
	if(!alureInitDevice(NULL, NULL)) {
		std::cerr << "Error: OpenAl cannot initialize Device" << std::endl;
	}
	AL_CHECK();
	
	std::cout << "initialized audio device" << std::endl;
}
Example #9
0
int releaseBuffer(int bufferId) {
	LOG_DEBUG("releaseBuffer( %i )", bufferId);
	ALuint buffer = bufferId;	
	alDeleteBuffers(1, &buffer);
	
	AL_CHECK();
	return SUCCESS;
}
bool AudioBuffer::streamData(ALuint buffer, bool looped)
{
    static char buffers[STREAMING_BUFFER_SIZE];
    
    if (_streamStateWav.get())
    {
        ALsizei bytesRead = _fileStream->read(buffers, sizeof(char), STREAMING_BUFFER_SIZE);
        if (bytesRead != STREAMING_BUFFER_SIZE)
        {
            if (looped)
                _fileStream->seek(_streamStateWav->dataStart, SEEK_SET);
        }
        if (bytesRead > 0)
            AL_CHECK(alBufferData(buffer, _streamStateWav->format, buffers, bytesRead, _streamStateWav->frequency));
        
        return bytesRead > 0 || looped;
    }
    else if (_streamStateOgg.get())
    {
        int section;
        int result = 0;
        ALsizei bytesRead = 0;

        while (bytesRead < STREAMING_BUFFER_SIZE)
        {
            result = ov_read(&_streamStateOgg->oggFile, buffers + bytesRead, STREAMING_BUFFER_SIZE - bytesRead, 0, 2, 1, &section);
            if (result > 0)
            {
                bytesRead += result;
            }
            else
            {
                if (looped)
                    ov_pcm_seek(&_streamStateOgg->oggFile, _streamStateOgg->dataStart);
                break;
            }
        }

        if (bytesRead > 0)
            AL_CHECK(alBufferData(buffer, _streamStateOgg->format, buffers, bytesRead, _streamStateOgg->frequency));
        
        return (bytesRead > 0) || looped;
    }
    
    return false;
}
Example #11
0
AudioSource::~AudioSource()
{
    if (_alSource)
    {
        AL_CHECK( alDeleteSources(1, &_alSource) );
        _alSource = 0;
    }
    SAFE_RELEASE(_buffer);
}
Example #12
0
void AudioSource::setLooped(bool looped)
{
    AL_CHECK( alSourcei(_alSource, AL_LOOPING, (looped) ? AL_TRUE : AL_FALSE) );
    if (AL_LAST_ERROR())
    {
        GP_ERROR("Failed to set audio source's looped attribute with error: %d", AL_LAST_ERROR());
    }
    _looped = looped;
}
	bool audio_context::set_as_current() {
#if BUILD_OPENAL
		auto result = alcMakeContextCurrent(context);
		AL_CHECK(result);
		return result == ALC_TRUE;
#else
		return true;
#endif
	}
Example #14
0
	void init()
	{
		s_al_device = alcOpenDevice(NULL);
		CE_ASSERT(s_al_device, "Cannot open OpenAL audio device");

		s_al_context = alcCreateContext(s_al_device, NULL);
		CE_ASSERT(s_al_context, "Cannot create OpenAL context");

		AL_CHECK(alcMakeContextCurrent(s_al_context));

		CE_LOGD("OpenAL Vendor   : %s", alGetString(AL_VENDOR));
		CE_LOGD("OpenAL Version  : %s", alGetString(AL_VERSION));
		CE_LOGD("OpenAL Renderer : %s", alGetString(AL_RENDERER));

		AL_CHECK(alDistanceModel(AL_INVERSE_DISTANCE_CLAMPED));
		AL_CHECK(alDopplerFactor(1.0f));
		AL_CHECK(alDopplerVelocity(343.0f));
	}
Example #15
0
int stop(int sourceId) {
	if(INITIALIZED) {
		LOG_DEBUG("stop( %i )", sourceId);
		alSourceStop(sourceId);
		
		AL_CHECK();
	}
	return SUCCESS;
}
Example #16
0
int setRolloff(int sourceId, float rolloff) {
	if(INITIALIZED) {
		LOG_DEBUG("setRolloff( %i, %.2f )", sourceId, rolloff);
		alSourcef(sourceId, AL_ROLLOFF_FACTOR, rolloff);
		
		AL_CHECK();
	}
	return SUCCESS;
}
Example #17
0
int setPitch(int sourceId, float pitch) {
	if(INITIALIZED) {
		LOG_DEBUG("setPitch( %i, %.2f )", sourceId, pitch);
		alSourcef(sourceId, AL_PITCH, pitch);
		
		AL_CHECK();
	}
	return SUCCESS;
}
Example #18
0
int setGain(int sourceId, float gain) {
	if(INITIALIZED) {
		LOG_DEBUG("setGain( %i, %.2f )", sourceId, gain);
		alSourcef(sourceId, AL_GAIN, gain);
		
		AL_CHECK();
	}
	return SUCCESS;
}
Example #19
0
int releaseSource(int sourceId) {
	LOG_DEBUG("releaseSource( %i )", sourceId);	
	
	ALuint source = sourceId;	
	alDeleteSources(1, &source);
	
	AL_CHECK();
	return SUCCESS;
}
Example #20
0
int setListenerOrientation(float xAt, float yAt, float zAt, float xUp, float yUp, float zUp) {
	if(INITIALIZED) {
		 LOG_ERROR("setListenerOrientation( %.2f, %.2f, %.2f, %.2f, %.2f, %.2f  )", xAt, yAt, zAt, xUp, yUp, zUp);
		float listenerOri []  = {xAt, yAt, zAt, xUp, yUp, zUp};
		alListenerfv(AL_ORIENTATION, listenerOri);
		
		AL_CHECK();
	}
	return SUCCESS;
}
Example #21
0
int setListenerPosition(float x, float y, float z) {
	if(INITIALIZED) {
		 LOG_ERROR("setListenerPosition( %.2f, %.2f, %.2f )", x, y, z);
		float listenerPos []  = {x, y, z};
		alListenerfv(AL_POSITION, listenerPos);
		
		AL_CHECK();
	}
	return SUCCESS;
}
Example #22
0
int play(int sourceId, int loop) {
	if(INITIALIZED) {
		LOG_DEBUG("play( %i, %i )", sourceId, loop);
		alSourcei (sourceId, AL_LOOPING,  loop);
		alSourcePlay(sourceId);
		
		AL_CHECK();
	}
	return SUCCESS;
}
Example #23
0
int setPositon(int sourceId, float x, float y, float z) {
	if(INITIALIZED) {
		 LOG_ERROR("setPositon( %i, %.2f, %.2f, %.2f )", sourceId, x, y, z);
		float pos [] = {x, y, z};
		alSourcefv(sourceId, AL_POSITION, pos);
		
		AL_CHECK();
	}
	return SUCCESS;
}
Example #24
0
void AudioSource::play()
{
    AL_CHECK( alSourcePlay(_alSource) );

    // Add the source to the controller's list of currently playing sources.
//    AudioController* audioController = Game::getInstance()->getAudioController();
//    GP_ASSERT(audioController);
//    if (audioController->_playingSources.find(this) == audioController->_playingSources.end())
//        audioController->_playingSources.insert(this);
}
Example #25
0
void AudioSource::stop()
{
    AL_CHECK( alSourceStop(_alSource) );

    // Remove the source from the controller's set of currently playing sources.
//    AudioController* audioController = Game::getInstance()->getAudioController();
//    GP_ASSERT(audioController);
//    std::set<AudioSource*>::iterator iter = audioController->_playingSources.find(this);
//    if (iter != audioController->_playingSources.end())
//        audioController->_playingSources.erase(iter);
}
Example #26
0
void AudioSource::pause()
{
    AL_CHECK( alSourcePause(_alSource) );

    // Remove the source from the controller's set of currently playing sources
    // if the source is being paused by the user and not the controller itself.
//    AudioController* audioController = Game::getInstance()->getAudioController();
//    GP_ASSERT(audioController);
    //if (audioController->_pausingSource != this)
//    {
//        std::set<AudioSource*>::iterator iter = audioController->_playingSources.find(this);
//        if (iter != audioController->_playingSources.end())
//            audioController->_playingSources.erase(iter);
//    }
}
Example #27
0
AudioSource::State AudioSource::getState() const
{
    ALint state;
    AL_CHECK( alGetSourcei(_alSource, AL_SOURCE_STATE, &state) );

    switch (state)
    {
        case AL_PLAYING: 
            return PLAYING;
        case AL_PAUSED:  
            return PAUSED;
        case AL_STOPPED: 
            return STOPPED;
        default:         
            return INITIAL;
    }
    return INITIAL;
}
Example #28
0
AudioBuffer::~AudioBuffer()
{
    // Remove the buffer from the cache.
    unsigned int bufferCount = (unsigned int)__buffers.size();
    for (unsigned int i = 0; i < bufferCount; i++)
    {
        if (this == __buffers[i])
        {
            __buffers.erase(__buffers.begin() + i);
            break;
        }
    }

    if (_alBuffer)
    {
        AL_CHECK( alDeleteBuffers(1, &_alBuffer) );
        _alBuffer = 0;
    }
}
Example #29
0
void CStreamItem::ReleaseOpenALStream()
{	
	if (m_ALSource != 0)
	{
		int num_processed;
		AL_CHECK
		alGetSourcei(m_ALSource, AL_BUFFERS_PROCESSED, &num_processed);
		AL_CHECK

		if (num_processed > 0)
		{
			ALuint* al_buf = new ALuint[num_processed];
			alSourceUnqueueBuffers(m_ALSource, num_processed, al_buf);
			AL_CHECK
			delete[] al_buf;
		}
		alSourcei(m_ALSource, AL_BUFFER, NULL);
		AL_CHECK
		((CSoundManager*)g_SoundManager)->ReleaseALSource(m_ALSource);
		AL_CHECK
		m_ALSource = 0;
	}
Example #30
0
AudioSource* AudioSource::create(const char* url)
{
    // Load from a .audio file.
    std::string pathStr = url;
    if (pathStr.find(".audio") != std::string::npos)
    {
        Properties* properties = Properties::create(url);
        if (properties == NULL)
        {
            GP_ERROR("Failed to create audio source from .audio file.");
            return NULL;
        }

        AudioSource* audioSource = create((strlen(properties->getNamespace()) > 0) ? properties : properties->getNextNamespace());
        SAFE_DELETE(properties);
        return audioSource;
    }

    // Create an audio buffer from this URL.
    AudioBuffer* buffer = AudioBuffer::create(url);
    if (buffer == NULL)
        return NULL;

    // Load the audio source.
    ALuint alSource = 0;

    AL_CHECK( alGenSources(1, &alSource) );
    if (AL_LAST_ERROR())
    {
        SAFE_RELEASE(buffer);
        GP_ERROR("Error generating audio source.");
        return NULL;
    }
    
    return new AudioSource(buffer, alSource);
}