2
2
3
3
#include "audio_manager.h"
4
4
5
- #include <stdint.h>
6
5
#include <string.h>
6
+ #include <math.h>
7
7
8
8
#include <miniaudio.h>
9
9
@@ -80,6 +80,10 @@ void ska_audio_manager_finalize() {
80
80
}
81
81
82
82
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
+ }
83
87
pthread_mutex_lock (& audio_mutex );
84
88
// Create audio instance and add to instances array
85
89
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) {
99
103
if (!ska_asset_manager_has_audio_source (filePath )) {
100
104
ska_logger_error ("Doesn't have audio source loaded at path '%s' loaded! Aborting..." , filePath );
101
105
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 ;
105
106
}
106
107
SkaAudioSource * audioSource = ska_asset_manager_get_audio_source (filePath );
107
108
ska_audio_manager_play_sound (audioSource , loops );
@@ -143,40 +144,35 @@ void audio_data_callback(ma_device* device, void* output, const void* input, ma_
143
144
continue ;
144
145
}
145
146
146
- const int32_t channels = audioInst -> source -> channels ;
147
+ const int32 channels = audioInst -> source -> channels ;
147
148
const f64 pitch = audioInst -> source -> pitch ;
148
149
int16 * sampleOut = (int16 * ) output ;
149
150
int16 * samples = (int16 * ) audioInst -> source -> samples ;
150
- uint64_t samplesToWrite = (uint64 ) frame_count ;
151
+ uint64 samplesToWrite = (uint64 ) frame_count ;
151
152
152
153
// Write to output
153
154
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 ;
160
156
161
- uint64 leftId = (uint64 ) startSamplePosition ;
157
+ uint64 curIndex = (uint64 ) startSamplePosition ;
162
158
if (channels > 1 ) {
163
- leftId &= ~((uint64 )( 0x01 ) );
159
+ curIndex &= ~((uint64 )0x01 );
164
160
}
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 ];
171
166
172
- * sampleOut ++ += leftSample ; // Left
173
- * sampleOut ++ += rightSample ; // Right
167
+ * sampleOut ++ += sampleLeftNext ;
168
+ * sampleOut ++ += sampleRightNext ;
174
169
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 );
177
173
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 ) {
180
176
audioInst -> sample_position = 0 ;
181
177
if (!audioInst -> does_loop ) {
182
178
ska_logger_debug ("Audio instance with id '%u' is queued for deletion!" , audioInst -> id );
0 commit comments