Skip to content

Commit c6a5fd8

Browse files
committed
Small tweaks to audio implementation, as it was breaking on web.
1 parent cfaf92d commit c6a5fd8

File tree

2 files changed

+23
-27
lines changed

2 files changed

+23
-27
lines changed

seika/audio/audio_manager.c

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
#include "audio_manager.h"
44

5-
#include <stdint.h>
65
#include <string.h>
6+
#include <math.h>
77

88
#include <miniaudio.h>
99

@@ -80,6 +80,10 @@ void ska_audio_manager_finalize() {
8080
}
8181

8282
void ska_audio_manager_play_sound(SkaAudioSource* audioSource, bool loops) {
83+
if (audio_instances->count >= SKA_MAX_AUDIO_INSTANCES) {
84+
ska_logger_warn("Reached max audio instances of '%d', not playing sound!", SKA_MAX_AUDIO_INSTANCES);
85+
return;
86+
}
8387
pthread_mutex_lock(&audio_mutex);
8488
// Create audio instance and add to instances array
8589
static uint32 audioInstanceId = 0; // TODO: temp id for now in case we need to grab a hold of an audio instance for roll back later...
@@ -99,9 +103,6 @@ void ska_audio_manager_play_sound2(const char* filePath, bool loops) {
99103
if (!ska_asset_manager_has_audio_source(filePath)) {
100104
ska_logger_error("Doesn't have audio source loaded at path '%s' loaded! Aborting...", filePath);
101105
return;
102-
} else if (audio_instances->count >= SKA_MAX_AUDIO_INSTANCES) {
103-
ska_logger_warn("Reached max audio instances of '%d', not playing sound!", SKA_MAX_AUDIO_INSTANCES);
104-
return;
105106
}
106107
SkaAudioSource* audioSource = ska_asset_manager_get_audio_source(filePath);
107108
ska_audio_manager_play_sound(audioSource, loops);
@@ -143,40 +144,35 @@ void audio_data_callback(ma_device* device, void* output, const void* input, ma_
143144
continue;
144145
}
145146

146-
const int32_t channels = audioInst->source->channels;
147+
const int32 channels = audioInst->source->channels;
147148
const f64 pitch = audioInst->source->pitch;
148149
int16* sampleOut = (int16*) output;
149150
int16* samples = (int16*) audioInst->source->samples;
150-
uint64_t samplesToWrite = (uint64) frame_count;
151+
uint64 samplesToWrite = (uint64) frame_count;
151152

152153
// Write to output
153154
for (uint64 writeSample = 0; writeSample < samplesToWrite; writeSample++) {
154-
f64 startSamplePosition = audioInst->sample_position;
155-
156-
f64 targetSamplePosition = startSamplePosition + (f64)channels * pitch;
157-
if (targetSamplePosition >= audioInst->source->sample_count) {
158-
targetSamplePosition -= (f64)audioInst->source->sample_count;
159-
}
155+
const f64 startSamplePosition = audioInst->sample_position;
160156

161-
uint64 leftId = (uint64) startSamplePosition;
157+
uint64 curIndex = (uint64) startSamplePosition;
162158
if (channels > 1) {
163-
leftId &= ~((uint64)(0x01));
159+
curIndex &= ~((uint64)0x01);
164160
}
165-
const uint64 rightId = leftId + (uint64)(channels - 1);
166-
const int16 startLeftSample = samples[leftId + channels];
167-
const int16 startRightSample = samples[rightId + channels];
168-
169-
const int16 leftSample = (int16)(startLeftSample / channels);
170-
const int16 rightSample = (int16)(startRightSample / channels);
161+
const uint64 nextIndex = (curIndex + channels) % audioInst->source->sample_count;
162+
const uint64 leftIndexNext = nextIndex;
163+
const uint64 rightIndexNext = nextIndex + 1;
164+
const int16 sampleLeftNext = samples[leftIndexNext];
165+
const int16 sampleRightNext = samples[rightIndexNext];
171166

172-
*sampleOut++ += leftSample; // Left
173-
*sampleOut++ += rightSample; // Right
167+
*sampleOut++ += sampleLeftNext;
168+
*sampleOut++ += sampleRightNext;
174169

175-
// Possibly need fixed sampling instead
176-
audioInst->sample_position = targetSamplePosition;
170+
// Update the instance's sample position and clamp it
171+
const f64 targetSamplePosition = startSamplePosition + (f64)channels * pitch;
172+
audioInst->sample_position = fmod(targetSamplePosition, audioInst->source->sample_count);
177173

178-
const bool isAtEnd = audioInst->sample_position >= audioInst->source->sample_count - channels - 1;
179-
if (isAtEnd) {
174+
// Check if the raw target sample position has passed boundary
175+
if (targetSamplePosition >= (f64)audioInst->source->sample_count - channels) {
180176
audioInst->sample_position = 0;
181177
if (!audioInst->does_loop) {
182178
ska_logger_debug("Audio instance with id '%u' is queued for deletion!", audioInst->id);

seika/version_info.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44

55
#define SKA_VERSION_MAJOR 0
66
#define SKA_VERSION_MINOR 2
7-
#define SKA_VERSION_PATCH 11
7+
#define SKA_VERSION_PATCH 12
88

99
#define SKA_VERSION (SKA_MACRO_TO_STRING(SKA_VERSION_MAJOR) "." SKA_MACRO_TO_STRING(SKA_VERSION_MINOR) "." SKA_MACRO_TO_STRING(SKA_VERSION_PATCH))

0 commit comments

Comments
 (0)