unsigned int SimpleAudioEngine::playEffect(const char* pszFilePath, bool bLoop)
	{
		EffectsMap::iterator iter = s_effects.find(pszFilePath);

		if (iter == s_effects.end())
		{
			preloadEffect(pszFilePath);

			// let's try again
			iter = s_effects.find(pszFilePath);
			if (iter == s_effects.end())
			{
				fprintf(stderr, "could not find play sound %s\n", pszFilePath);
				return -1;
			}
		}

		checkALError("playEffect");
		iter->second->isLooped = bLoop;
		alSourcei(iter->second->source, AL_LOOPING, iter->second->isLooped ? AL_TRUE : AL_FALSE);
		alSourcePlay(iter->second->source);
		checkALError("playEffect");

		return iter->second->source;
	}
Example #2
0
	unsigned int SimpleAudioEngine::playEffect(const char* pszFilePath, bool bLoop)
	{
		// Changing file path to full path
    	std::string fullPath = CCFileUtils::sharedFileUtils()->fullPathForFilename(pszFilePath);

		EffectsMap::iterator iter = s_effects.find(fullPath);

		if (iter == s_effects.end())
		{
			preloadEffect(fullPath.c_str());

			// let's try again
			iter = s_effects.find(fullPath);
			if (iter == s_effects.end())
			{
				fprintf(stderr, "could not find play sound %s\n", fullPath.c_str());
				return -1;
			}
		}

		checkALError("playEffect");
		iter->second->isLooped = bLoop;
		alSourcei(iter->second->source, AL_LOOPING, iter->second->isLooped ? AL_TRUE : AL_FALSE);
		alSourcePlay(iter->second->source);
		checkALError("playEffect");

		return iter->second->source;
	}
	void SimpleAudioEngine::preloadEffect(const char* pszFilePath)
	{
		EffectsMap::iterator iter = s_effects.find(pszFilePath);

		// check if we have this already
		if (iter == s_effects.end())
		{
			ALuint 		buffer;
			ALuint 		source;
			soundData  *data = new soundData;

			string path = pszFilePath;

			buffer = alutCreateBufferFromFile(path.data());

			if (buffer == AL_NONE)
			{
				fprintf(stderr, "Error loading file: '%s'\n", path.data());
				alDeleteBuffers(1, &buffer);
				return;
			}

			alGenSources(1, &source);
			alSourcei(source, AL_BUFFER, buffer);

			data->isLooped = false;
			data->buffer = buffer;
			data->source = source;

			s_effects.insert(EffectsMap::value_type(pszFilePath, data));
		}
	}
	void SimpleAudioEngine::end()
	{
		checkALError("end");

		// clear all the sounds
	    EffectsMap::const_iterator end = s_effects.end();
	    for (EffectsMap::iterator it = s_effects.begin(); it != end; it++)
	    {
	        alSourceStop(it->second->source);
	        checkALError("end");
			alDeleteBuffers(1, &it->second->buffer);
			checkALError("end");
			alDeleteSources(1, &it->second->source);
			checkALError("end");
			delete it->second;
	    }
	    s_effects.clear();

		// and the background too
		stopBackground(true);

		for (BackgroundMusicsMap::iterator it = s_backgroundMusics.begin(); it != s_backgroundMusics.end(); ++it)
		{
			alSourceStop(it->second->source);
			checkALError("end");
			alDeleteBuffers(1, &it->second->buffer);
			checkALError("end");
			alDeleteSources(1, &it->second->source);
			checkALError("end");
			delete it->second;
		}
		s_backgroundMusics.clear();
	}
	void SimpleAudioEngine::pauseAllEffects()
	{
		EffectsMap::iterator iter = s_effects.begin();

		if (iter != s_effects.end())
	    {
			alSourcePause(iter->second->source);
			checkALError("pauseAllEffects");
	    }
	}
	void SimpleAudioEngine::resumeAllEffects()
	{
		EffectsMap::iterator iter = s_effects.begin();

		if (iter != s_effects.end())
	    {
			checkALError("resumeAllEffects");
			alSourcePlay(iter->second->source);
			checkALError("resumeAllEffects");
	    }
	}
void SimpleAudioEngine::stopAllEffects()
{
    EffectsMap::iterator iter = s_effects.begin();

    if (iter != s_effects.end())
    {
        checkALError("stopAllEffects:init");
        alSourceStop(iter->second->source);
        checkALError("stopAllEffects:alSourceStop");
    }
}
    void SimpleAudioEngine::stopAllEffects()
    {
		EffectsMap::iterator iter = s_effects.begin();

		if (iter != s_effects.end())
	    {
	        alSourceStop(iter->second->source);
			int err = alGetError();
			if (err != AL_NO_ERROR)
				printALError(err);
	    }
    }
Example #9
0
	void SimpleAudioEngine::preloadEffect(const char* pszFilePath)
	{
		// Changing file path to full path
    	std::string fullPath = CCFileUtils::sharedFileUtils()->fullPathForFilename(pszFilePath);

		EffectsMap::iterator iter = s_effects.find(fullPath);

		// check if we have this already
		if (iter == s_effects.end())
		{
			ALuint 		buffer;
			ALuint 		source;
			soundData  *data = new soundData;
			string 	    path = fullPath;

			checkALError("preloadEffect");

			if (isOGGFile(path.data()))
			{
				buffer = createBufferFromOGG(path.data());
			}
			else
			{
				buffer = alutCreateBufferFromFile(path.data());
				checkALError("preloadEffect");
			}

			if (buffer == AL_NONE)
			{
				fprintf(stderr, "Error loading file: '%s'\n", path.data());
				alDeleteBuffers(1, &buffer);
				return;
			}

			alGenSources(1, &source);

			if (checkALError("preloadEffect") != AL_NO_ERROR)
			{
				alDeleteBuffers(1, &buffer);
				return;
			}

			alSourcei(source, AL_BUFFER, buffer);
			checkALError("preloadEffect");

			data->isLooped = false;
			data->buffer = buffer;
			data->source = source;

			s_effects.insert(EffectsMap::value_type(fullPath, data));
		}
	}
void SimpleAudioEngine::resumeAllEffects()
{
    EffectsMap::iterator iter = s_effects.begin();
    ALint state;
    while (iter != s_effects.end())
    {
        alGetSourcei(iter->second->source, AL_SOURCE_STATE, &state);
        if (state == AL_PAUSED)
            alSourcePlay(iter->second->source);
        checkALError("resumeAllEffects:alSourcePlay");
        ++iter;
    }
}
void SimpleAudioEngine::setEffectsVolume(float volume)
{
    if (volume != s_effectVolume)
    {
        EffectsMap::const_iterator end = s_effects.end();
        for (EffectsMap::const_iterator it = s_effects.begin(); it != end; it++)
        {
            alSourcef(it->second->source, AL_GAIN, volume * it->second->gain);
        }

        s_effectVolume = volume;
    }
}
Example #12
0
	void SimpleAudioEngine::pauseAllEffects()
	{
		EffectsMap::iterator iter = s_effects.begin();
		ALint state;
		while (iter != s_effects.end())
	    {
			alGetSourcei(iter->second->source, AL_SOURCE_STATE, &state);
			if (state == AL_PLAYING)
				alSourcePause(iter->second->source);
			checkALError("pauseAllEffects");
			++iter;
	    }
	}
void SimpleAudioEngine::preloadEffect(const char* pszFilePath)
{
    // Changing file path to full path
    std::string fullPath = FileUtils::getInstance()->fullPathForFilename(pszFilePath);

    EffectsMap::iterator iter = s_effects.find(fullPath);

    // check if we have this already
    if (iter == s_effects.end())
    {
        ALuint      buffer = AL_NONE;
        ALuint      source = AL_NONE;
        

        checkALError("preloadEffect:init");
        OpenALFile file;
        file.debugName = pszFilePath;
        file.file = fopen(fullPath.c_str(), "rb");
        if (!file.file) {
            fprintf(stderr, "Cannot read file: '%s'\n", fullPath.data());
            return;
        }
        
        bool success = false;
        const std::vector<OpenALDecoder *> &decoders = OpenALDecoder::getDecoders();
        for (size_t i = 0, n = decoders.size(); !success && i < n; ++i)
            success = decoders[i]->decode(file, buffer);
        file.clear();

        alGenSources(1, &source);

        if (checkALError("preloadEffect:alGenSources") != AL_NO_ERROR)
        {
            alDeleteBuffers(1, &buffer);
            return;
        }

        alSourcei(source, AL_BUFFER, buffer);
        checkALError("preloadEffect:alSourcei");
        
        soundData  *data = new soundData;
        data->isLooped = false;
        data->buffer = buffer;
        data->source = source;
        data->pitch = 1.0;
        data->pan = 0.0;
        data->gain = 1.0;

        s_effects.insert(EffectsMap::value_type(fullPath, data));
    }
}
unsigned int SimpleAudioEngine::playEffect(const char* pszFilePath, bool bLoop,
                                           float pitch, float pan, float gain)
{
    std::string fullPath = FileUtils::getInstance()->fullPathForFilename(pszFilePath);

    EffectsMap::iterator iter = s_effects.find(fullPath);

    if (iter == s_effects.end())
    {
        preloadEffect(fullPath.c_str());

        // let's try again
        iter = s_effects.find(fullPath);
        if (iter == s_effects.end())
        {
            fprintf(stderr, "could not find play sound %s\n", fullPath.c_str());
            return -1;
        }
    }

    checkALError("playEffect:init");

    soundData &d = *iter->second;
    d.isLooped = bLoop;
    d.pitch = pitch;
    d.pan = pan;
    d.gain = gain;
    alSourcei(d.source, AL_LOOPING, d.isLooped ? AL_TRUE : AL_FALSE);
    alSourcef(d.source, AL_GAIN, s_effectVolume * d.gain);
    alSourcef(d.source, AL_PITCH, d.pitch);
    float sourcePosAL[] = {d.pan, 0.0f, 0.0f};//Set position - just using left and right panning
    alSourcefv(d.source, AL_POSITION, sourcePosAL);
    alSourcePlay(d.source);
    checkALError("playEffect:alSourcePlay");

    return d.source;
}
	void SimpleAudioEngine::unloadEffect(const char* pszFilePath)
	{
		EffectsMap::iterator iter = s_effects.find(pszFilePath);

		if (iter != s_effects.end())
	    {
	        alSourceStop(iter->second->source);
			alDeleteSources(1, &iter->second->source);
			alDeleteBuffers(1, &iter->second->buffer);
			delete iter->second;

			int err = alGetError();
			if (err != AL_NO_ERROR)
				printALError(err);

			s_effects.erase(iter);
	    }
	}
void SimpleAudioEngine::end()
{
    checkALError("end:init");

    // clear all the sound effects
    EffectsMap::const_iterator end = s_effects.end();
    for (auto it = s_effects.begin(); it != end; ++it)
    {
        alSourceStop(it->second->source);
        checkALError("end:alSourceStop");

        alDeleteSources(1, &it->second->source);
        checkALError("end:alDeleteSources");

        alDeleteBuffers(1, &it->second->buffer);
        checkALError("end:alDeleteBuffers");

        delete it->second;
    }
    s_effects.clear();

    // and the background music too
    stopBackground(true);

    for (auto it = s_backgroundMusics.begin(); it != s_backgroundMusics.end(); ++it)
    {
        alSourceStop(it->second->source);
        checkALError("end:alSourceStop");

        alDeleteSources(1, &it->second->source);
        checkALError("end:alDeleteSources");

        alDeleteBuffers(1, &it->second->buffer);
        checkALError("end:alDeleteBuffers");

        delete it->second;
    }
    s_backgroundMusics.clear();

    CC_SAFE_DELETE(s_engine);
}
	void SimpleAudioEngine::end()
	{
		// clear all the sounds
	    EffectsMap::const_iterator end = s_effects.end();
	    for (EffectsMap::iterator it = s_effects.begin(); it != end; it++)
	    {
            Mix_FreeChunk(it->second->chunk);
			delete it->second;
	    }
	    s_effects.clear();

		// and the background too
		stopBackground(true);

		for (BackgroundMusicsMap::iterator it = s_backgroundMusics.begin(); it != s_backgroundMusics.end(); ++it)
		{
            Mix_FreeMusic(it->second->music);
			delete it->second;
		}
		s_backgroundMusics.clear();
	}
	void SimpleAudioEngine::unloadEffect(const char* pszFilePath)
	{
		EffectsMap::iterator iter = s_effects.find(pszFilePath);

		if (iter != s_effects.end())
	    {
			checkALError("unloadEffect");

	        alSourceStop(iter->second->source);
	        checkALError("unloadEffect");

			alDeleteSources(1, &iter->second->source);
			checkALError("unloadEffect");

			alDeleteBuffers(1, &iter->second->buffer);
			checkALError("unloadEffect");
			delete iter->second;

			s_effects.erase(iter);
	    }
	}
	void SimpleAudioEngine::end()
	{
		// clear all the sounds
	    EffectsMap::const_iterator end = s_effects.end();
	    for (EffectsMap::iterator it = s_effects.begin(); it != end; it++)
	    {
	        alSourceStop(it->second->source);

			alDeleteBuffers(1, &it->second->buffer);
			alDeleteSources(1, &it->second->source);
			delete it->second;
	    }
	    s_effects.clear();

		if (s_isBackgroundInitialized)
		{
			s_isBackgroundInitialized = false;
		}

		// and the background too
		stopBackground(true);
	}
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);
    }
}
void System<EffectsComp>::update(double elapsed)
{

    std::map<ObjectId,EffectsComp>::iterator iCom = components_.begin();

    while(iCom!=components_.end())
    {
        EffectsComp* effectsComp = &iCom->second;
        EffectsMap* effectsMap = effectsComp->getEffects();
//        std::cout << "Address of effectsMap: "<< effectsMap << std::endl;
//        std::cout << "copy of effects map size : "<< effectsMap->size() << std::endl;
//        std::cout << "orig effects map size : " << effectsComp->getEffects()->size() << std::endl;
        //update each effect in component as necessary
        EffectsMap::iterator iEffect = effectsMap->begin();
        while(iEffect!=effectsMap->end())
        {
             //update timer if necessary
            if(iEffect->second->hasDuration())
            {
                iEffect->second->updateDuration(elapsed);
                //check if timer has expired
                if (iEffect->second->hasExpired())
                {
                    //do on end messages
                    std::vector<Parameters> endParams = iEffect->second->getOnEnd();
                    std::vector<Parameters>::iterator iParams = endParams.begin();
                    while(iParams!= endParams.end())
                    {
                        if(iParams->size()>0)
                        {
                            Message msg(effectsComp->getId(), effectsComp->getId(), *iParams);
                            Telegram tel(effectsComp->getId(), effectsComp->getId(), 0.0, msg);
                            core_->getMessageCentre()->addTelegram(tel);
                        }
                        ++iParams;
                    }

                    //delete params
                    effectsMap->erase(iEffect++);
                    continue;
                }

            }

            //carry out onUpdate messages
            std::vector<Parameters> updateParams = iEffect->second->getOnUpdate();
            std::vector<Parameters>::iterator iParams = updateParams.begin();
            while(iParams!= updateParams.end())
            {
                if(iParams->size()>0)
                {
                    Message msg(effectsComp->getId(), effectsComp->getId(), *iParams);
                    Telegram tel(effectsComp->getId(), effectsComp->getId(), 0.0, msg);
                    core_->getMessageCentre()->addTelegram(tel);
                }
                ++iParams;
            }

            ++iEffect;

        }

        ++iCom;
    }


}