Skip to content

Commit 5c51adc

Browse files
committed
Don't bother inserting silence when reading from audio buffer
Keep track of active stream status per client
1 parent c73a2c3 commit 5c51adc

File tree

3 files changed

+14
-21
lines changed

3 files changed

+14
-21
lines changed

mumble/audio.c

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,6 @@ void audio_transmission_event(lua_State *l, MumbleClient *client) {
701701
mumble_hook_call(client, "OnAudioStream", 3);
702702

703703
// Keep track of when an audio buffer is outputting data
704-
static bool stream_active = false;
705704
bool streamed_audio = false;
706705

707706
while (current != NULL) {
@@ -735,39 +734,29 @@ void audio_transmission_event(lua_State *l, MumbleClient *client) {
735734
continue;
736735
}
737736

738-
streamed_audio = true;
739737
sf_count_t input_frame_size = (sf_count_t)(client->audio_frames * (float)context->samplerate / 1000.0);
740738
size_t input_frames_actual = length / sizeof(float) / context->channels;
741739
sf_count_t input_frames = input_frames_actual < input_frame_size ? input_frames_actual : input_frame_size;
742-
size_t missing_frames = (didLoop || biggest_read > 0) ? 0 : (input_frame_size - input_frames);
743740
double resample_ratio = (double)AUDIO_SAMPLE_RATE / context->samplerate;
744741

745-
// Handle the audio output for streamed frames
746-
for (int i = 0; i < input_frame_size; i++) {
747-
if (i < missing_frames) {
748-
// Insert silence for each channel
749-
for (int j = 0; j < context->channels; j++) {
750-
input_buffer[i * context->channels + j] = 0.0;
751-
}
752-
} else {
753-
for (int j = 0; j < context->channels; j++) {
754-
float sample;
755-
buffer_readFloat(buffer, &sample);
756-
input_buffer[i * context->channels + j] = sample;
757-
}
742+
for (int i = 0; i < input_frames; i++) {
743+
for (int j = 0; j < context->channels; j++) {
744+
float sample;
745+
buffer_readFloat(buffer, &sample);
746+
input_buffer[i * context->channels + j] = sample;
758747
}
759748
}
760749

761750
if (context->channels == 1) {
762-
float *stereo_buffer = convert_mono_to_multi(input_buffer, input_frame_size, AUDIO_PLAYBACK_CHANNELS);
751+
float *stereo_buffer = convert_mono_to_multi(input_buffer, input_frames, AUDIO_PLAYBACK_CHANNELS);
763752
if (!stereo_buffer) {
764753
free(input_buffer);
765754
continue;
766755
}
767756
free(input_buffer);
768757
input_buffer = stereo_buffer;
769758
} else if (context->channels > 2) {
770-
float *stereo_buffer = downmix_to_stereo(input_buffer, input_frame_size, context->channels);
759+
float *stereo_buffer = downmix_to_stereo(input_buffer, input_frames, context->channels);
771760
if (!stereo_buffer) {
772761
free(input_buffer);
773762
continue;
@@ -777,9 +766,10 @@ void audio_transmission_event(lua_State *l, MumbleClient *client) {
777766
}
778767

779768
float *output_audio = NULL;
780-
int resampled_frames = resample_audio(context->src_state, input_buffer, &output_audio, input_frame_size, output_frames, resample_ratio, false);
769+
int resampled_frames = resample_audio(context->src_state, input_buffer, &output_audio, input_frames, output_frames, resample_ratio, false);
781770
free(input_buffer);
782771
if (resampled_frames > 0) {
772+
streamed_audio = true;
783773
for (int i = 0; i < resampled_frames; i++) {
784774
client->audio_output[i].l += output_audio[i * 2];
785775
client->audio_output[i].r += output_audio[i * 2 + 1];
@@ -797,7 +787,7 @@ void audio_transmission_event(lua_State *l, MumbleClient *client) {
797787
}
798788

799789
// All streams output nothing
800-
bool stream_ended = stream_active && !streamed_audio;
790+
bool stream_ended = client->audio_stream_active && !streamed_audio;
801791

802792
// Something isn't looping, and either we stopped reading data or a buffer stream stopped sening data.
803793
bool end_frame = !didLoop && (biggest_read < output_frames && stream_ended);
@@ -807,7 +797,7 @@ void audio_transmission_event(lua_State *l, MumbleClient *client) {
807797
encode_and_send_audio(client, output_frames, end_frame);
808798
}
809799

810-
stream_active = streamed_audio;
800+
client->audio_stream_active = streamed_audio;
811801

812802
lua_stackguard_exit(l);
813803
}

mumble/mumble.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,8 @@ static int mumble_client_new(lua_State *l) {
551551

552552
client->recording = false;
553553

554+
client->audio_stream_active = false;
555+
554556
// Create a thread that buffers the reading of open audio files
555557
client->audio_thread_running = true;
556558
uv_thread_create(&client->audio_thread, mumble_audio_thread, client);

mumble/types.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ struct MumbleClient {
196196

197197
uv_thread_t audio_thread;
198198
bool audio_thread_running;
199+
bool audio_stream_active;
199200

200201
uv_mutex_t main_mutex;
201202
uv_mutex_t inner_mutex;

0 commit comments

Comments
 (0)