@@ -327,9 +327,7 @@ void mumble_audio_thread(void *arg) {
327
327
uv_mutex_lock (& sound -> mutex );
328
328
bool playing = sound -> playing ;
329
329
size_t buffer_size = sound -> buffer_size ;
330
- size_t available_space = (sound -> read_position > sound -> write_position ) ?
331
- (sound -> read_position - sound -> write_position ) :
332
- (sound -> buffer_size - sound -> write_position + sound -> read_position );
330
+ size_t available_space = sound -> buffer_size - sound -> used ;
333
331
size_t first_chunk = sound -> buffer_size - sound -> write_position ;
334
332
uv_mutex_unlock (& sound -> mutex );
335
333
@@ -367,6 +365,7 @@ void mumble_audio_thread(void *arg) {
367
365
}
368
366
// Update the write pointer in terms of float elements
369
367
sound -> write_position = (sound -> write_position + copy_size ) % sound -> buffer_size ;
368
+ sound -> used += copy_size ;
370
369
uv_mutex_unlock (& sound -> mutex );
371
370
free (output_audio );
372
371
}
@@ -450,21 +449,16 @@ static void handle_audio_stream_end(lua_State *l, MumbleClient *client, AudioStr
450
449
static void process_audio_file (lua_State * l , MumbleClient * client , AudioStream * sound , uint32_t frame_size , sf_count_t * biggest_read , bool * didLoop ) {
451
450
sf_count_t sample_size = (sf_count_t ) client -> audio_frames * (float ) AUDIO_SAMPLE_RATE / 1000 ;
452
451
453
- if (sample_size > PCM_BUFFER || sound -> write_position == sound -> read_position ) {
452
+ if (sample_size > PCM_BUFFER || sound -> used <= 0 ) {
454
453
// Our sample size is somehow too large to fit within the input buffer,
455
454
// or our sound file buffer is empty..
456
455
return ;
457
456
}
458
457
459
458
float input_buffer [PCM_BUFFER ];
460
459
461
- // Calculate how much data is available
462
- size_t used_space = (sound -> write_position >= sound -> read_position ) ?
463
- (sound -> write_position - sound -> read_position ) :
464
- (sound -> buffer_size - sound -> read_position + sound -> write_position );
465
-
466
460
// Calculate available frames
467
- sf_count_t frames_available = used_space / AUDIO_PLAYBACK_CHANNELS ;
461
+ sf_count_t frames_available = sound -> used / AUDIO_PLAYBACK_CHANNELS ;
468
462
sf_count_t frames_to_read = (frames_available < sample_size ) ? frames_available : sample_size ;
469
463
470
464
sf_count_t read = 0 ;
@@ -489,6 +483,7 @@ static void process_audio_file(lua_State *l, MumbleClient *client, AudioStream *
489
483
490
484
// Update read position safely
491
485
sound -> read_position = (sound -> read_position + samples_to_read ) % sound -> buffer_size ;
486
+ sound -> used -= samples_to_read ;
492
487
493
488
read = frames_to_read ; // Store actual frames read
494
489
}
0 commit comments