示例#1
0
bool AudioEngine::_LoadAudio(AudioDescriptor *audio, const std::string &filename)
{
    std::map<std::string, private_audio::AudioCacheElement>::iterator it = _audio_cache.find(filename);
    if(it != _audio_cache.end()) {
        it->second.audio->AddOwners(*audio->GetOwners());
        // Once the owners have been copied, we don't need the given descriptor anymore.
        delete audio;
        // Return a success since basically everything will keep on working as expected.
        return true;
    }

    // (1) If the cache is not full, try loading the audio and adding it in
    if(_audio_cache.size() < _max_cache_size) {
        if(audio->LoadAudio(filename) == false) {
            IF_PRINT_WARNING(AUDIO_DEBUG) << "could not add new audio file into cache because load operation failed: " << filename << std::endl;
            return false;
        }

        _audio_cache.insert(std::make_pair(filename, AudioCacheElement(SDL_GetTicks(), audio)));
        return true;
    }

    // (2) The cache is full, so find an element to remove. First make sure that at least one piece of audio is stopped
    std::map<std::string, AudioCacheElement>::iterator lru_element = _audio_cache.end();
    for(std::map<std::string, AudioCacheElement>::iterator i = _audio_cache.begin(); i != _audio_cache.end(); i++) {
        if(i->second.audio->GetState() == AUDIO_STATE_STOPPED) {
            lru_element = i;
            break;
        }
    }

    if(lru_element == _audio_cache.end()) {
        IF_PRINT_WARNING(AUDIO_DEBUG) << "failed to remove element from cache because no piece of audio was in the stopped state" << std::endl;
        return false;
    }

    for(std::map<std::string, AudioCacheElement>::iterator i = _audio_cache.begin(); i != _audio_cache.end(); i++) {
        if(i->second.audio->GetState() == AUDIO_STATE_STOPPED && i->second.last_update_time < lru_element->second.last_update_time) {
            lru_element = i;
        }
    }

    delete lru_element->second.audio;
    _audio_cache.erase(lru_element);

    if(audio->LoadAudio(filename) == false) {
        IF_PRINT_WARNING(AUDIO_DEBUG) << "could not add new audio file into cache because load operation failed: " << filename << std::endl;
        return false;
    }

    _audio_cache.insert(std::make_pair(filename, AudioCacheElement(SDL_GetTicks(), audio)));
    return true;
} // bool AudioEngine::_LoadAudio(AudioDescriptor* audio, const std::string& filename)
示例#2
0
bool AudioEngine::_LoadAudio(AudioDescriptor* audio, const std::string& filename) {
	if (_audio_cache.find(filename) != _audio_cache.end()) {
		IF_PRINT_WARNING(AUDIO_DEBUG) << "audio was already contained within the cache: " << filename << endl;
		return false;
	}

	// (1) If the cache is not full, try loading the audio and adding it in
	if (_audio_cache.size() < _max_cache_size) {
		if (audio->LoadAudio(filename) == false) {
			IF_PRINT_WARNING(AUDIO_DEBUG) << "could not add new audio file into cache because load operation failed: " << filename << endl;
			return false;
		}

		_audio_cache.insert(make_pair(filename, AudioCacheElement(SDL_GetTicks(), audio)));
		return true;
	}

	// (2) The cache is full, so find an element to remove. First make sure that at least one piece of audio is stopped
	map<std::string, AudioCacheElement>::iterator lru_element = _audio_cache.end();
	for (map<std::string, AudioCacheElement>::iterator i = _audio_cache.begin(); i != _audio_cache.end(); i++) {
		if (i->second.audio->GetState() == AUDIO_STATE_STOPPED) {
			lru_element = i;
			break;
		}
	}

	if (lru_element == _audio_cache.end()) {
		IF_PRINT_WARNING(AUDIO_DEBUG) << "failed to remove element from cache because no piece of audio was in the stopped state" << endl;
		return false;
	}

	for (map<std::string, AudioCacheElement>::iterator i = _audio_cache.begin(); i != _audio_cache.end(); i++) {
		if (i->second.audio->GetState() == AUDIO_STATE_STOPPED && i->second.last_update_time < lru_element->second.last_update_time) {
			lru_element = i;
		}
	}

	delete lru_element->second.audio;
	_audio_cache.erase(lru_element);

	if (audio->LoadAudio(filename) == false) {
		IF_PRINT_WARNING(AUDIO_DEBUG) << "could not add new audio file into cache because load operation failed: " << filename << endl;
		return false;
	}

	_audio_cache.insert(make_pair(filename, AudioCacheElement(SDL_GetTicks(), audio)));
	return true;
} // bool AudioEngine::_LoadAudio(AudioDescriptor* audio, const std::string& filename)
示例#3
0
bool AudioEngine::_LoadAudio(const std::string &filename, bool is_music, vt_mode_manager::GameMode *gm)
{
    if(!DoesFileExist(filename))
        return false;

    std::map<std::string, private_audio::AudioCacheElement>::iterator it = _audio_cache.find(filename);
    if(it != _audio_cache.end()) {

        if (gm)
            it->second.audio->AddOwner(gm);

        // Return a success since basically everything will keep on working as expected.
        return true;
    }

    // Creates the new audio object and adds its potential game mode owner.
    AudioDescriptor *audio = NULL;
    if (is_music)
        audio = new MusicDescriptor();
    else
        audio = new SoundDescriptor();

    if (gm)
        audio->AddOwner(gm);

    // (1) If the cache is not full, try loading the audio and adding it in
    if(_audio_cache.size() < _max_cache_size) {
        if(audio->LoadAudio(filename) == false) {
            IF_PRINT_WARNING(AUDIO_DEBUG) << "could not add new audio file into cache because load operation failed: " << filename << std::endl;
            delete audio;
            return false;
        }

        _audio_cache.insert(std::make_pair(filename, AudioCacheElement(SDL_GetTicks(), audio)));
        return true;
    }

    // (2) The cache is full, so find an element to remove. First make sure that at least one piece of audio is stopped
    std::map<std::string, AudioCacheElement>::iterator lru_element = _audio_cache.end();
    for(std::map<std::string, AudioCacheElement>::iterator i = _audio_cache.begin(); i != _audio_cache.end(); ++i) {
        if(i->second.audio->GetState() == AUDIO_STATE_STOPPED) {
            lru_element = i;
            break;
        }
    }

    if(lru_element == _audio_cache.end()) {
        IF_PRINT_WARNING(AUDIO_DEBUG) << "failed to remove element from cache because no piece of audio was in the stopped state" << std::endl;
        delete audio;
        return false;
    }

    for(std::map<std::string, AudioCacheElement>::iterator i = _audio_cache.begin(); i != _audio_cache.end(); ++i) {
        if(i->second.audio->GetState() == AUDIO_STATE_STOPPED && i->second.last_update_time < lru_element->second.last_update_time) {
            lru_element = i;
        }
    }

    delete lru_element->second.audio;
    _audio_cache.erase(lru_element);

    if(audio->LoadAudio(filename) == false) {
        IF_PRINT_WARNING(AUDIO_DEBUG) << "could not add new audio file into cache because load operation failed: " << filename << std::endl;
        delete audio;
        return false;
    }

    _audio_cache.insert(std::make_pair(filename, AudioCacheElement(SDL_GetTicks(), audio)));
    return true;
} // bool AudioEngine::_LoadAudio(AudioDescriptor* audio, const std::string& filename)