Skip to content

Commit 56d21e3

Browse files
committed
Fixed an bug where filling the buffer completely would result in no audio
1 parent d692e95 commit 56d21e3

File tree

3 files changed

+7
-10
lines changed

3 files changed

+7
-10
lines changed

mumble/audio.c

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -327,9 +327,7 @@ void mumble_audio_thread(void *arg) {
327327
uv_mutex_lock(&sound->mutex);
328328
bool playing = sound->playing;
329329
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;
333331
size_t first_chunk = sound->buffer_size - sound->write_position;
334332
uv_mutex_unlock(&sound->mutex);
335333

@@ -367,6 +365,7 @@ void mumble_audio_thread(void *arg) {
367365
}
368366
// Update the write pointer in terms of float elements
369367
sound->write_position = (sound->write_position + copy_size) % sound->buffer_size;
368+
sound->used += copy_size;
370369
uv_mutex_unlock(&sound->mutex);
371370
free(output_audio);
372371
}
@@ -450,21 +449,16 @@ static void handle_audio_stream_end(lua_State *l, MumbleClient *client, AudioStr
450449
static void process_audio_file(lua_State *l, MumbleClient *client, AudioStream *sound, uint32_t frame_size, sf_count_t *biggest_read, bool *didLoop) {
451450
sf_count_t sample_size = (sf_count_t) client->audio_frames * (float) AUDIO_SAMPLE_RATE / 1000;
452451

453-
if (sample_size > PCM_BUFFER || sound->write_position == sound->read_position) {
452+
if (sample_size > PCM_BUFFER || sound->used <= 0) {
454453
// Our sample size is somehow too large to fit within the input buffer,
455454
// or our sound file buffer is empty..
456455
return;
457456
}
458457

459458
float input_buffer[PCM_BUFFER];
460459

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-
466460
// 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;
468462
sf_count_t frames_to_read = (frames_available < sample_size) ? frames_available : sample_size;
469463

470464
sf_count_t read = 0;
@@ -489,6 +483,7 @@ static void process_audio_file(lua_State *l, MumbleClient *client, AudioStream *
489483

490484
// Update read position safely
491485
sound->read_position = (sound->read_position + samples_to_read) % sound->buffer_size;
486+
sound->used -= samples_to_read;
492487

493488
read = frames_to_read; // Store actual frames read
494489
}

mumble/client.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ static int client_openAudio(lua_State *l) {
365365
sound->fade_stop = false;
366366
sound->buffer_size = buffer_size;
367367
sound->buffer = buffer;
368+
sound->used = 0;
368369
sound->read_position = 0;
369370
sound->write_position = 0;
370371
sound->src_state = src_state;

mumble/types.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ struct AudioStream {
9191
size_t read_position;
9292
size_t write_position;
9393
size_t buffer_size;
94+
size_t used;
9495
uv_mutex_t mutex;
9596
SRC_STATE *src_state;
9697
};

0 commit comments

Comments
 (0)