8 #include "vorbis/codec.h"
9 #include "vorbis/vorbisfile.h"
10 #include "vorbis/vorbisenc.h"
26 #include "../util/Exception.hxx"
27 #include "../util/LOG.hxx"
40 json config_json = json::parse(jsonFileContent,
nullptr,
false);
43 if (config_json.is_discarded())
46 m_audioConfig = config_json;
62 _setmode(_fileno(stdin), _O_BINARY);
63 _setmode(_fileno(stdout), _O_BINARY);
67 ifstream file(path, std::ifstream::in | std::ifstream::binary);
70 if (ov_fopen(path.c_str(), &vf) < 0)
72 LOG(
LOG_ERROR) <<
"Input does not appear to be an Ogg bitstream. \n" << stderr;
88 long ret = ov_read(&vf, pcmout, 4096, 0, 2, 1, ¤t_section);
98 LOG(
LOG_ERROR) <<
"ERROR: OV_HOLE found in initial read of buffer\n";
102 LOG(
LOG_ERROR) <<
"ERROR: OV_EBADLINK found in initial read of buffer\n";
106 LOG(
LOG_ERROR) <<
"ERROR: OV_EINVAL found in initial read of buffer\n";
119 vorbis_info *vorbisInfo = ov_info(&vf, -1);
125 LOG(
LOG_DEBUG) <<
"Audio data read successful! Loaded into decoded audio buffer.\n";
131 if (m_soundtracks.count(
id) > 0)
133 m_soundtracks[id].age =
m_Age++;
137 bool isMusic =
false;
139 if (m_audioConfig.Music.count(
id.get()) > 0)
141 config = &m_audioConfig.Music.at(
id.
get());
144 else if (m_audioConfig.Sound.count(
id.get()) > 0)
145 config = &m_audioConfig.Sound.at(
id.
get());
150 LOG(
LOG_INFO) <<
"Fetching " <<
id.get() <<
" at " << filepath;
153 if (LoadAudioWithOggVorbis(filepath.c_str(), dataBuffer) == -1)
163 m_soundtracks[id] = SoundtrackResource{
SoundtrackUPtr{soundtrack}, m_Age++};
165 LOG(
LOG_INFO) <<
"Resource cache is now at " << (m_CacheSize / 1000000) <<
"MB";
166 if (m_CacheSize > MAX_RESOURCE_BYTES::value)
174 size_t total_size = 0;
175 total_size += m_soundtracks.size();
178 std::vector<uint32_t> all_ages;
180 all_ages.insert<AgeIt>(all_ages.cend(),
AgeIterator{m_soundtracks.begin()},
AgeIterator{m_soundtracks.end()});
181 std::nth_element(all_ages.begin(), all_ages.begin() + total_size, all_ages.end());
182 uint32_t median = all_ages[total_size];
183 for (
auto it = m_soundtracks.begin(); it != m_soundtracks.end();)
185 if (it->second.age < median and !it->second.resource->isPlaying)
188 alGetBufferi(it->second.resource->buffer, AL_SIZE, &sizeBytes);
190 it = m_soundtracks.erase(it);