Format like smack-a-brother, and write comments as sentences

Four-space indents, braces on their own lines, and one-statement bodies without braces.
Semicolons in comments became full stops.
Designated initializers may leave fields out without a warning.
This commit is contained in:
shweet committed 2026-11-22 17:30:03 -05:00
1 parent 40f6b554b5
commit 7afdf709c3
176 files changed
+20500 -19048

No files matched your search

+69 -58
View File
@@ -6,104 +6,115 @@
#include <unordered_map>
#include <vector>
struct AudioEntry
{
struct AudioEntry
{
MIX_Audio* audio = nullptr;
std::vector<MIX_Track*> tracks; // one per overlapping play, reused once finished
std::atomic<float> squares[2]{};
std::atomic<int> sampleCount = {};
};
};
// Sums the squares of the left/right samples the track plays, for its levels.
static void SDLCALL levels_update(void* userdata, MIX_Track*, const SDL_AudioSpec* spec, float* pcm, int samples)
{
// Sums the squares of the left/right samples the track plays, for its levels.
static void SDLCALL levels_update(void* userdata, MIX_Track*, const SDL_AudioSpec* spec, float* pcm, int samples)
{
auto entry = (AudioEntry*)userdata;
auto channels = std::max(spec->channels, 1);
float squares[2]{};
for (int i = 0; i < samples; ++i)
squares[std::min(i % channels, 1)] += pcm[i] * pcm[i];
if (channels == 1) squares[1] = squares[0];
squares[std::min(i % channels, 1)] += pcm[i] * pcm[i];
if (channels == 1)
squares[1] = squares[0];
for (int channel = 0; channel < 2; ++channel)
entry->squares[channel] += squares[channel];
entry->squares[channel] += squares[channel];
entry->sampleCount += samples / channels;
}
}
static std::unordered_map<std::uint64_t, AudioEntry> entries;
static std::unordered_map<std::uint64_t, AudioEntry> entries;
static MIX_Mixer* audio_mixer_get()
{
static MIX_Mixer* audio_mixer_get()
{
static auto mixer = MIX_CreateMixerDevice(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, nullptr);
return mixer;
}
}
static AudioEntry* entry_get(const AudioData& data)
{
if (!data.is_valid()) return nullptr;
static AudioEntry* entry_get(const AudioData& data)
{
if (!data.is_valid())
return nullptr;
auto [it, isNew] = entries.try_emplace(data.uid);
if (isNew)
if (auto io = SDL_IOFromConstMem(data.bytes.data(), data.bytes.size()))
it->second.audio = MIX_LoadAudio_IO(audio_mixer_get(), io, true, true);
if (auto io = SDL_IOFromConstMem(data.bytes.data(), data.bytes.size()))
it->second.audio = MIX_LoadAudio_IO(audio_mixer_get(), io, true, true);
return it->second.audio ? &it->second : nullptr;
}
}
static bool is_on_mixer(MIX_Track* track, MIX_Mixer* mixer) { return !mixer || MIX_GetTrackMixer(track) == mixer; }
static bool is_on_mixer(MIX_Track* track, MIX_Mixer* mixer)
{
return !mixer || MIX_GetTrackMixer(track) == mixer;
}
// A sound can play over itself: while a track is still playing it, another track takes the new play.
void audio_play(const AudioData& data, bool isLoop, MIX_Mixer* mixer)
{
// A sound can play over itself: while a track is still playing it, another track takes the new play.
void audio_play(const AudioData& data, bool isLoop, MIX_Mixer* mixer)
{
auto entry = entry_get(data);
auto targetMixer = mixer ? mixer : audio_mixer_get();
if (!entry || !targetMixer) return;
if (!entry || !targetMixer)
return;
auto free = std::ranges::find_if(entry->tracks, [&](MIX_Track* track)
{ return is_on_mixer(track, targetMixer) && !MIX_TrackPlaying(track); });
auto free = std::ranges::find_if(
entry->tracks, [&](MIX_Track* track) { return is_on_mixer(track, targetMixer) && !MIX_TrackPlaying(track); });
auto track = free != entry->tracks.end() ? *free : MIX_CreateTrack(targetMixer);
if (!track) return;
if (!track)
return;
if (free == entry->tracks.end())
{
entry->tracks.push_back(track);
MIX_SetTrackCookedCallback(track, levels_update, entry);
MIX_SetTrackAudio(track, entry->audio);
entry->tracks.push_back(track);
MIX_SetTrackCookedCallback(track, levels_update, entry);
MIX_SetTrackAudio(track, entry->audio);
}
SDL_PropertiesID options = isLoop ? SDL_CreateProperties() : 0;
if (options) SDL_SetNumberProperty(options, MIX_PROP_PLAY_LOOPS_NUMBER, -1);
if (options)
SDL_SetNumberProperty(options, MIX_PROP_PLAY_LOOPS_NUMBER, -1);
MIX_PlayTrack(track, options);
if (options) SDL_DestroyProperties(options);
}
if (options)
SDL_DestroyProperties(options);
}
void audio_stop(const AudioData& data, MIX_Mixer* mixer)
{
void audio_stop(const AudioData& data, MIX_Mixer* mixer)
{
if (auto it = entries.find(data.uid); it != entries.end())
for (auto track : it->second.tracks)
if (is_on_mixer(track, mixer)) MIX_StopTrack(track, 0);
}
for (auto track : it->second.tracks)
if (is_on_mixer(track, mixer))
MIX_StopTrack(track, 0);
}
void audio_track_detach(const AudioData& data, MIX_Mixer* mixer)
{
void audio_track_detach(const AudioData& data, MIX_Mixer* mixer)
{
auto it = entries.find(data.uid);
if (it == entries.end()) return;
std::erase_if(it->second.tracks,
[&](MIX_Track* track)
{
if (!is_on_mixer(track, mixer)) return false;
MIX_DestroyTrack(track);
return true;
});
}
if (it == entries.end())
return;
std::erase_if(it->second.tracks, [&](MIX_Track* track) {
if (!is_on_mixer(track, mixer))
return false;
MIX_DestroyTrack(track);
return true;
});
}
bool is_audio_playing(const AudioData& data)
{
bool is_audio_playing(const AudioData& data)
{
auto it = entries.find(data.uid);
return it != entries.end() && std::ranges::any_of(it->second.tracks, MIX_TrackPlaying);
}
}
// The left/right loudness (RMS, 0-1) of what the track played since the last call.
glm::vec2 audio_levels_get(const AudioData& data)
{
// The left/right loudness (RMS, 0-1) of what the track played since the last call.
glm::vec2 audio_levels_get(const AudioData& data)
{
auto it = entries.find(data.uid);
if (it == entries.end()) return {};
if (it == entries.end())
return {};
auto& entry = it->second;
auto count = (float)std::max(entry.sampleCount.exchange(0), 1);
return {std::sqrt(entry.squares[0].exchange(0.0f) / count), std::sqrt(entry.squares[1].exchange(0.0f) / count)};
}
}