VERY hacky positional audio

This commit is contained in:
2025-10-06 22:17:41 +01:00
parent 59f643b72c
commit df2c02a6a9
7 changed files with 49 additions and 12 deletions

View File

@@ -13,6 +13,8 @@ struct Audio_Track {
Audio_Data *data;
F32 volume;
U32 n_played;
U32 next; // to play if playing, free otherwise
};
@@ -34,6 +36,8 @@ struct Audio_Context {
};
function void Audio_Init(M_Arena *arena, Audio_Context *audio, F32 volume);
function void Audio_Play(Audio_Context *audio, U32 index);
function U32 Audio_Play(Audio_Context *audio, U32 index);
function void Audio_ChangeVolume(Audio_Context *audio, U32 handle, F32 volume);
#endif // LD_OS_AUDIO_H_

View File

@@ -48,8 +48,8 @@ internal void SDL_SubmitAudio(void *user, SDL_AudioStream *stream, int needed, i
U32 off = track->n_played << 1; // played n frames, thus double to n samples
for (U32 f = 0; f < play; ++f) {
*l0++ += cast(F32) (track->data->samples[off + (2 * f) + 0]) * audio->volume;
*r0++ += cast(F32) (track->data->samples[off + (2 * f) + 1]) * audio->volume;
*l0++ += cast(F32) (track->data->samples[off + (2 * f) + 0]) * track->volume * audio->volume;
*r0++ += cast(F32) (track->data->samples[off + (2 * f) + 1]) * track->volume * audio->volume;
}
track->n_played += play;
@@ -136,7 +136,6 @@ void Audio_Init(M_Arena *arena, Audio_Context *audio, F32 volume) {
}
audio->n_tracks = 16;
//audio->tracks = M_ArenaPush(arena, Audio_Track, audio->n_tracks);
for (U32 it = 1; it < audio->n_tracks; ++it) {
audio->tracks[it].next = it + 1;
@@ -153,19 +152,30 @@ void Audio_Init(M_Arena *arena, Audio_Context *audio, F32 volume) {
}
}
void Audio_Play(Audio_Context *audio, U32 index) {
U32 Audio_Play(Audio_Context *audio, U32 index) {
U32 result = 0;
if (audio->free != 0 && SDL_LockAudioStream(audio->stream)) {
U32 idx = audio->free;
result = audio->free;
Audio_Track *track = &audio->tracks[idx];
Audio_Track *track = &audio->tracks[result];
audio->free = track->next;
track->playing = true;
track->data = &audio->sounds[index];
track->n_played = 0;
track->volume = 1.0f;
track->next = audio->head;
audio->head = idx;
audio->head = result;
SDL_UnlockAudioStream(audio->stream);
}
return result;
}
void Audio_ChangeVolume(Audio_Context *audio, U32 handle, F32 volume) {
if (SDL_LockAudioStream(audio->stream)) {
audio->tracks[handle].volume = volume;
SDL_UnlockAudioStream(audio->stream);
}
}