diff --git a/drivers/coreaudio/audio_driver_coreaudio.cpp b/drivers/coreaudio/audio_driver_coreaudio.cpp index 1344a9075e6..e1f47cb8c2f 100644 --- a/drivers/coreaudio/audio_driver_coreaudio.cpp +++ b/drivers/coreaudio/audio_driver_coreaudio.cpp @@ -174,9 +174,9 @@ Error AudioDriverCoreAudio::init() { unsigned int buffer_size = buffer_frames * channels; samples_in.resize(buffer_size); input_buf.resize(buffer_size); - audio_input_buffer.resize(buffer_size * 8); - audio_input_position = 0; - audio_input_size = 0; + input_buffer.resize(buffer_size * 8); + input_position = 0; + input_size = 0; if (OS::get_singleton()->is_stdout_verbose()) { print_line("CoreAudio: detected " + itos(channels) + " channels"); @@ -247,17 +247,6 @@ OSStatus AudioDriverCoreAudio::output_callback(void *inRefCon, return 0; }; -void AudioDriverCoreAudio::_input_write_sample(int32_t sample) { - - audio_input_buffer.write[audio_input_position++] = sample; - if (audio_input_position >= audio_input_buffer.size()) { - audio_input_position = 0; - } - if (audio_input_size < audio_input_buffer.size()) { - audio_input_size++; - } -} - OSStatus AudioDriverCoreAudio::input_callback(void *inRefCon, AudioUnitRenderActionFlags *ioActionFlags, const AudioTimeStamp *inTimeStamp, @@ -281,11 +270,11 @@ OSStatus AudioDriverCoreAudio::input_callback(void *inRefCon, if (result == noErr) { for (int i = 0; i < inNumberFrames * ad->capture_channels; i++) { int32_t sample = ad->input_buf[i] << 16; - ad->_input_write_sample(sample); + ad->input_buffer_write(sample); if (ad->capture_channels == 1) { // In case input device is single channel convert it to Stereo - ad->_input_write_sample(sample); + ad->input_buffer_write(sample); } } } else { @@ -545,8 +534,8 @@ void AudioDriverCoreAudio::_set_device(const String &device, bool capture) { ERR_FAIL_COND(result != noErr); // Reset audio input to keep synchronisation. - audio_input_position = 0; - audio_input_size = 0; + input_position = 0; + input_size = 0; } } diff --git a/drivers/coreaudio/audio_driver_coreaudio.h b/drivers/coreaudio/audio_driver_coreaudio.h index 53a3e5e038f..d3f7c8d5969 100644 --- a/drivers/coreaudio/audio_driver_coreaudio.h +++ b/drivers/coreaudio/audio_driver_coreaudio.h @@ -61,7 +61,6 @@ class AudioDriverCoreAudio : public AudioDriver { #ifdef OSX_ENABLED Array _get_device_list(bool capture = false); void _set_device(const String &device, bool capture = false); - void _input_write_sample(int32_t sample); static OSStatus input_device_address_cb(AudioObjectID inObjectID, UInt32 inNumberAddresses, const AudioObjectPropertyAddress *inAddresses, diff --git a/drivers/pulseaudio/audio_driver_pulseaudio.cpp b/drivers/pulseaudio/audio_driver_pulseaudio.cpp index 71e75915660..66fdfc9c1b2 100644 --- a/drivers/pulseaudio/audio_driver_pulseaudio.cpp +++ b/drivers/pulseaudio/audio_driver_pulseaudio.cpp @@ -380,7 +380,7 @@ void AudioDriverPulseAudio::thread_func(void *p_udata) { size_t bytes = pa_stream_readable_size(ad->pa_rec_str); if (bytes > 0) { const void *ptr = NULL; - size_t maxbytes = ad->audio_input_buffer.size() * sizeof(int16_t); + size_t maxbytes = ad->input_buffer.size() * sizeof(int16_t); bytes = MIN(bytes, maxbytes); ret = pa_stream_peek(ad->pa_rec_str, &ptr, &bytes); @@ -389,9 +389,9 @@ void AudioDriverPulseAudio::thread_func(void *p_udata) { } else { int16_t *srcptr = (int16_t *)ptr; for (size_t i = bytes >> 1; i > 0; i--) { - ad->audio_input_buffer.write[ad->audio_input_position++] = int32_t(*srcptr++) << 16; - if (ad->audio_input_position >= ad->audio_input_buffer.size()) { - ad->audio_input_position = 0; + ad->input_buffer.write[ad->input_position++] = int32_t(*srcptr++) << 16; + if (ad->input_position >= ad->input_buffer.size()) { + ad->input_position = 0; } } @@ -601,11 +601,11 @@ Error AudioDriverPulseAudio::capture_init_device() { ERR_FAIL_V(ERR_CANT_OPEN); } - audio_input_buffer.resize(input_buffer_frames * 8); - for (int i = 0; i < audio_input_buffer.size(); i++) { - audio_input_buffer.write[i] = 0; + input_buffer.resize(input_buffer_frames * 8); + for (int i = 0; i < input_buffer.size(); i++) { + input_buffer.write[i] = 0; } - audio_input_position = 0; + input_position = 0; return OK; } diff --git a/drivers/wasapi/audio_driver_wasapi.cpp b/drivers/wasapi/audio_driver_wasapi.cpp index b641e213a81..827aecd1c8a 100644 --- a/drivers/wasapi/audio_driver_wasapi.cpp +++ b/drivers/wasapi/audio_driver_wasapi.cpp @@ -320,8 +320,8 @@ Error AudioDriverWASAPI::init_render_device(bool reinit) { // Sample rate is independent of channels (ref: https://stackoverflow.com/questions/11048825/audio-sample-frequency-rely-on-channels) samples_in.resize(buffer_frames * channels); - audio_input_position = 0; - audio_input_size = 0; + input_position = 0; + input_size = 0; if (OS::get_singleton()->is_stdout_verbose()) { print_line("WASAPI: detected " + itos(channels) + " channels"); @@ -343,12 +343,9 @@ Error AudioDriverWASAPI::init_capture_device(bool reinit) { ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN); // Set the buffer size - audio_input_buffer.resize(max_frames * CAPTURE_BUFFER_CHANNELS); - for (int i = 0; i < audio_input_buffer.size(); i++) { - audio_input_buffer.write[i] = 0; - } - audio_input_position = 0; - audio_input_size = 0; + input_buffer.resize(max_frames * CAPTURE_BUFFER_CHANNELS); + input_position = 0; + input_size = 0; return OK; } @@ -682,31 +679,24 @@ void AudioDriverWASAPI::thread_func(void *p_udata) { // fixme: Only works for floating point atm for (int j = 0; j < num_frames_available; j++) { - int32_t sample_channel[CAPTURE_BUFFER_CHANNELS]; + int32_t l, r; if (flags & AUDCLNT_BUFFERFLAGS_SILENT) { - sample_channel[0] = sample_channel[1] = 0; + l = r = 0; } else { if (ad->audio_input.channels == 2) { - sample_channel[0] = read_sample(ad->audio_input.format_tag, ad->audio_input.bits_per_sample, data, j * 2); - sample_channel[1] = read_sample(ad->audio_input.format_tag, ad->audio_input.bits_per_sample, data, j * 2 + 1); + l = read_sample(ad->audio_input.format_tag, ad->audio_input.bits_per_sample, data, j * 2); + r = read_sample(ad->audio_input.format_tag, ad->audio_input.bits_per_sample, data, j * 2 + 1); } else if (ad->audio_input.channels == 1) { - sample_channel[0] = sample_channel[1] = read_sample(ad->audio_input.format_tag, ad->audio_input.bits_per_sample, data, j); + l = r = read_sample(ad->audio_input.format_tag, ad->audio_input.bits_per_sample, data, j); } else { - sample_channel[0] = sample_channel[1] = 0; + l = r = 0; ERR_PRINT("WASAPI: unsupported channel count in microphone!"); } } - for (int k = 0; k < CAPTURE_BUFFER_CHANNELS; k++) { - ad->audio_input_buffer.write[ad->audio_input_position++] = sample_channel[k]; - if (ad->audio_input_position >= ad->audio_input_buffer.size()) { - ad->audio_input_position = 0; - } - if (ad->audio_input_size < ad->audio_input_buffer.size()) { - ad->audio_input_size++; - } - } + input_buffer_write(l); + input_buffer_write(r); } read_frames += num_frames_available; diff --git a/servers/audio/audio_stream.cpp b/servers/audio/audio_stream.cpp index f7622b8a4ec..618ebd526d5 100644 --- a/servers/audio/audio_stream.cpp +++ b/servers/audio/audio_stream.cpp @@ -134,18 +134,18 @@ void AudioStreamPlaybackMicrophone::_mix_internal(AudioFrame *p_buffer, int p_fr AudioDriver::get_singleton()->lock(); - Vector buf = AudioDriver::get_singleton()->get_audio_input_buffer(); - unsigned int audio_input_size = AudioDriver::get_singleton()->get_audio_input_size(); + Vector buf = AudioDriver::get_singleton()->get_input_buffer(); + unsigned int input_size = AudioDriver::get_singleton()->get_input_size(); // p_frames is multipled by two since an AudioFrame is stereo - if ((p_frames * 2) > audio_input_size) { + if ((p_frames * 2) > input_size) { for (int i = 0; i < p_frames; i++) { p_buffer[i] = AudioFrame(0.0f, 0.0f); } input_ofs = 0; } else { for (int i = 0; i < p_frames; i++) { - if (audio_input_size >= input_ofs) { + if (input_size >= input_ofs) { float l = (buf[input_ofs++] >> 16) / 32768.f; if (input_ofs >= buf.size()) { input_ofs = 0; diff --git a/servers/audio_server.cpp b/servers/audio_server.cpp index 7c8d61f5456..14318f282bd 100644 --- a/servers/audio_server.cpp +++ b/servers/audio_server.cpp @@ -80,6 +80,17 @@ double AudioDriver::get_mix_time() const { return total; } +void AudioDriver::input_buffer_write(int32_t sample) { + + input_buffer.write[input_position++] = sample; + if (input_position >= input_buffer.size()) { + input_position = 0; + } + if (input_size < input_buffer.size()) { + input_size++; + } +} + AudioDriver::SpeakerMode AudioDriver::get_speaker_mode_by_total_channels(int p_channels) const { switch (p_channels) { case 4: return SPEAKER_SURROUND_31; diff --git a/servers/audio_server.h b/servers/audio_server.h index a9ed66a2d59..2663a0f968d 100644 --- a/servers/audio_server.h +++ b/servers/audio_server.h @@ -53,12 +53,13 @@ class AudioDriver { #endif protected: - Vector audio_input_buffer; - unsigned int audio_input_position; - unsigned int audio_input_size; + Vector input_buffer; + unsigned int input_position; + unsigned int input_size; void audio_server_process(int p_frames, int32_t *p_buffer, bool p_update_mix_time = true); void update_mix_time(int p_frames); + void input_buffer_write(int32_t sample); #ifdef DEBUG_ENABLED _FORCE_INLINE_ void start_counting_ticks() { prof_ticks = OS::get_singleton()->get_ticks_usec(); } @@ -108,9 +109,9 @@ public: SpeakerMode get_speaker_mode_by_total_channels(int p_channels) const; int get_total_channels_by_speaker_mode(SpeakerMode) const; - Vector get_audio_input_buffer() { return audio_input_buffer; } - unsigned int get_audio_input_position() { return audio_input_position; } - unsigned int get_audio_input_size() { return audio_input_size; } + Vector get_input_buffer() { return input_buffer; } + unsigned int get_input_position() { return input_position; } + unsigned int get_input_size() { return input_size; } #ifdef DEBUG_ENABLED uint64_t get_profiling_time() const { return prof_time; }