|
| 1 | +/** |
| 2 | + * @file |
| 3 | + * |
| 4 | + * @author feliwir |
| 5 | + * |
| 6 | + * @brief Base class for caching loaded audio samples to reduce file IO. |
| 7 | + * |
| 8 | + * @copyright Thyme is free software: you can redistribute it and/or |
| 9 | + * modify it under the terms of the GNU General Public License |
| 10 | + * as published by the Free Software Foundation, either version |
| 11 | + * 2 of the License, or (at your option) any later version. |
| 12 | + * A full copy of the GNU General Public License can be found in |
| 13 | + * LICENSE |
| 14 | + */ |
| 15 | +#include "audiofilecache.h" |
| 16 | +#include "audioeventrts.h" |
| 17 | +#include "audiomanager.h" |
| 18 | +#include "filesystem.h" |
| 19 | + |
| 20 | +#include <captainslog.h> |
| 21 | + |
| 22 | +using namespace Thyme; |
| 23 | + |
| 24 | +/** |
| 25 | + * Opens an audio file. Reads from the cache if available or loads from file if not. |
| 26 | + */ |
| 27 | +AudioDataHandle AudioFileCache::Open_File(const Utf8String &filename, const AudioEventInfo *event_info) |
| 28 | +{ |
| 29 | + ScopedMutexClass lock(&m_mutex); |
| 30 | + |
| 31 | + captainslog_trace("AudioFileCache: opening file %s", filename.Str()); |
| 32 | + |
| 33 | + // Try to find existing data for this file to avoid loading it if unneeded. |
| 34 | + auto it = m_cacheMap.find(filename); |
| 35 | + |
| 36 | + if (it != m_cacheMap.end()) { |
| 37 | + ++(it->second.ref_count); |
| 38 | + |
| 39 | + return static_cast<AudioDataHandle>(it->second.wave_data); |
| 40 | + } |
| 41 | + |
| 42 | + // Load the file from disk |
| 43 | + File *file = g_theFileSystem->Open_File(filename.Str(), File::READ | File::BINARY | File::BUFFERED); |
| 44 | + |
| 45 | + if (file == nullptr) { |
| 46 | + if (filename.Is_Not_Empty()) { |
| 47 | + captainslog_warn("Missing audio file '%s', could not cache.", filename.Str()); |
| 48 | + } |
| 49 | + |
| 50 | + return nullptr; |
| 51 | + } |
| 52 | + |
| 53 | + OpenAudioFile open_audio; |
| 54 | + if (!Load_File(file, open_audio)) { |
| 55 | + captainslog_warn("Failed to load audio file '%s', could not cache.", filename.Str()); |
| 56 | + return nullptr; |
| 57 | + } |
| 58 | + |
| 59 | + file->Close(); |
| 60 | + |
| 61 | + open_audio.audio_event_info = event_info; |
| 62 | + open_audio.ref_count = 1; |
| 63 | + m_currentSize += open_audio.data_size; |
| 64 | + |
| 65 | + // m_maxSize prevents using overly large amounts of memory, so if we are over it, unload some other samples. |
| 66 | + if (m_currentSize > m_maxSize && !Free_Space_For_Sample(open_audio)) { |
| 67 | + captainslog_warn("Cannot play audio file since cache is full: %s", filename.Str()); |
| 68 | + m_currentSize -= open_audio.data_size; |
| 69 | + Release_Open_Audio(&open_audio); |
| 70 | + |
| 71 | + return nullptr; |
| 72 | + } |
| 73 | + |
| 74 | + m_cacheMap[filename] = open_audio; |
| 75 | + |
| 76 | + return static_cast<AudioDataHandle>(open_audio.wave_data); |
| 77 | +} |
| 78 | + |
| 79 | +/** |
| 80 | + * Opens an audio file for an event. Reads from the cache if available or loads from file if not. |
| 81 | + */ |
| 82 | +AudioDataHandle AudioFileCache::Open_File(AudioEventRTS *audio_event) |
| 83 | +{ |
| 84 | + Utf8String filename; |
| 85 | + |
| 86 | + // What part of an event are we playing? |
| 87 | + switch (audio_event->Get_Next_Play_Portion()) { |
| 88 | + case 0: |
| 89 | + filename = audio_event->Get_Attack_Name(); |
| 90 | + break; |
| 91 | + case 1: |
| 92 | + filename = audio_event->Get_File_Name(); |
| 93 | + break; |
| 94 | + case 2: |
| 95 | + filename = audio_event->Get_Decay_Name(); |
| 96 | + break; |
| 97 | + case 3: |
| 98 | + default: |
| 99 | + return nullptr; |
| 100 | + } |
| 101 | + |
| 102 | + return Open_File(filename, audio_event->Get_Event_Info()); |
| 103 | +} |
| 104 | + |
| 105 | +/** |
| 106 | + * Closes a file, reducing the references to it. Does not actually free the cache. |
| 107 | + */ |
| 108 | +void AudioFileCache::Close_File(AudioDataHandle file) |
| 109 | +{ |
| 110 | + if (file == nullptr) { |
| 111 | + return; |
| 112 | + } |
| 113 | + |
| 114 | + ScopedMutexClass lock(&m_mutex); |
| 115 | + |
| 116 | + for (auto it = m_cacheMap.begin(); it != m_cacheMap.end(); ++it) { |
| 117 | + if (static_cast<AudioDataHandle>(it->second.wave_data) == file) { |
| 118 | + --(it->second.ref_count); |
| 119 | + |
| 120 | + break; |
| 121 | + } |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +/** |
| 126 | + * Sets the maximum amount of memory in bytes that the cache should use. |
| 127 | + */ |
| 128 | +void AudioFileCache::Set_Max_Size(unsigned size) |
| 129 | +{ |
| 130 | + ScopedMutexClass lock(&m_mutex); |
| 131 | + m_maxSize = size; |
| 132 | +} |
| 133 | + |
| 134 | +/** |
| 135 | + * Attempts to free space by releasing files with no references |
| 136 | + */ |
| 137 | +unsigned AudioFileCache::Free_Space(unsigned required) |
| 138 | +{ |
| 139 | + std::list<Utf8String> to_free; |
| 140 | + unsigned freed = 0; |
| 141 | + |
| 142 | + // First check for samples that don't have any references. |
| 143 | + for (const auto &cached : m_cacheMap) { |
| 144 | + if (cached.second.ref_count == 0) { |
| 145 | + to_free.push_back(cached.first); |
| 146 | + freed += cached.second.data_size; |
| 147 | + |
| 148 | + // If required is "0" we free as much as possible |
| 149 | + if (required && freed >= required) { |
| 150 | + break; |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + for (const auto &file : to_free) { |
| 156 | + auto to_remove = m_cacheMap.find(file); |
| 157 | + |
| 158 | + if (to_remove != m_cacheMap.end()) { |
| 159 | + Release_Open_Audio(&to_remove->second); |
| 160 | + m_currentSize -= to_remove->second.data_size; |
| 161 | + m_cacheMap.erase(to_remove); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + return freed; |
| 166 | +} |
| 167 | + |
| 168 | +/** |
| 169 | + * Attempts to free space for a file by releasing files with no references and lower priority sounds. |
| 170 | + */ |
| 171 | +bool AudioFileCache::Free_Space_For_Sample(const OpenAudioFile &file) |
| 172 | +{ |
| 173 | + captainslog_assert(m_currentSize >= m_maxSize); // Assumed to be called only when we need more than allowed. |
| 174 | + std::list<Utf8String> to_free; |
| 175 | + unsigned required = m_currentSize - m_maxSize; |
| 176 | + unsigned freed = 0; |
| 177 | + |
| 178 | + // First check for samples that don't have any references. |
| 179 | + freed = Free_Space(required); |
| 180 | + |
| 181 | + // If we still don't have enough potential space freed up, look for lower priority sounds to remove. |
| 182 | + if (freed < required) { |
| 183 | + for (const auto &cached : m_cacheMap) { |
| 184 | + if (cached.second.ref_count != 0 |
| 185 | + && cached.second.audio_event_info->Get_Priority() < file.audio_event_info->Get_Priority()) { |
| 186 | + to_free.push_back(cached.first); |
| 187 | + freed += cached.second.data_size; |
| 188 | + |
| 189 | + if (freed >= required) { |
| 190 | + break; |
| 191 | + } |
| 192 | + } |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + // If we have enough space to free, do the actual freeing, otherwise we didn't succeed, no point bothering. |
| 197 | + if (freed < required) { |
| 198 | + return false; |
| 199 | + } |
| 200 | + |
| 201 | + for (const auto &file : to_free) { |
| 202 | + auto to_remove = m_cacheMap.find(file); |
| 203 | + |
| 204 | + if (to_remove != m_cacheMap.end()) { |
| 205 | + Release_Open_Audio(&to_remove->second); |
| 206 | + m_currentSize -= to_remove->second.data_size; |
| 207 | + m_cacheMap.erase(to_remove); |
| 208 | + } |
| 209 | + } |
| 210 | + |
| 211 | + return true; |
| 212 | +} |
0 commit comments