diff options
author | RĂ©mi Verschelde <rverschelde@gmail.com> | 2017-09-01 21:41:24 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-09-01 21:41:24 +0200 |
commit | 3694c58d3cb9f2af41a8f964e00712bb5578af76 (patch) | |
tree | 4dc1bf7bba4a1d76220e9044a6c364867aca56c4 /platform/osx | |
parent | 5a69a663c727a48d38d1ccc66b18318c21b692f3 (diff) | |
parent | f231eadc9e2487c70db04f912578ec853f11737c (diff) |
Merge pull request #10775 from marcelofg55/buffersize_fixes
Corrections to audio buffer size calculations
Diffstat (limited to 'platform/osx')
-rw-r--r-- | platform/osx/audio_driver_osx.cpp | 26 | ||||
-rw-r--r-- | platform/osx/audio_driver_osx.h | 5 |
2 files changed, 20 insertions, 11 deletions
diff --git a/platform/osx/audio_driver_osx.cpp b/platform/osx/audio_driver_osx.cpp index 8c5a734f76..78c52af201 100644 --- a/platform/osx/audio_driver_osx.cpp +++ b/platform/osx/audio_driver_osx.cpp @@ -77,7 +77,7 @@ Error AudioDriverOSX::initDevice() { break; }*/ - mix_rate = GLOBAL_DEF("audio/mix_rate", 44100); + mix_rate = GLOBAL_DEF("audio/mix_rate", DEFAULT_MIX_RATE); zeromem(&strdesc, sizeof(strdesc)); strdesc.mFormatID = kAudioFormatLinearPCM; @@ -92,15 +92,19 @@ Error AudioDriverOSX::initDevice() { result = AudioUnitSetProperty(audio_unit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, kOutputBus, &strdesc, sizeof(strdesc)); ERR_FAIL_COND_V(result != noErr, FAILED); - int latency = GLOBAL_DEF("audio/output_latency", 25); - unsigned int buffer_size = closest_power_of_2(latency * mix_rate / 1000); + int latency = GLOBAL_DEF("audio/output_latency", DEFAULT_OUTPUT_LATENCY); + // Sample rate is independent of channels (ref: https://stackoverflow.com/questions/11048825/audio-sample-frequency-rely-on-channels) + buffer_frames = closest_power_of_2(latency * mix_rate / 1000); - if (OS::get_singleton()->is_stdout_verbose()) { - print_line("audio buffer size: " + itos(buffer_size) + " calculated latency: " + itos(buffer_size * 1000 / mix_rate)); - } + result = AudioUnitSetProperty(audio_unit, kAudioDevicePropertyBufferFrameSize, kAudioUnitScope_Global, kOutputBus, &buffer_frames, sizeof(UInt32)); + ERR_FAIL_COND_V(result != noErr, FAILED); + buffer_size = buffer_frames * channels; samples_in.resize(buffer_size); - buffer_frames = buffer_size / channels; + + if (OS::get_singleton()->is_stdout_verbose()) { + print_line("audio buffer frames: " + itos(buffer_frames) + " calculated latency: " + itos(buffer_frames * 1000 / mix_rate) + "ms"); + } AURenderCallbackStruct callback; zeromem(&callback, sizeof(AURenderCallbackStruct)); @@ -234,7 +238,7 @@ void AudioDriverOSX::start() { }; int AudioDriverOSX::get_mix_rate() const { - return 44100; + return mix_rate; }; AudioDriver::SpeakerMode AudioDriverOSX::get_speaker_mode() const { @@ -282,8 +286,12 @@ AudioDriverOSX::AudioDriverOSX() { active = false; mutex = NULL; - mix_rate = 44100; + mix_rate = 0; channels = 2; + + buffer_size = 0; + buffer_frames = 0; + samples_in.clear(); }; diff --git a/platform/osx/audio_driver_osx.h b/platform/osx/audio_driver_osx.h index ac178b89f3..a7e68c8141 100644 --- a/platform/osx/audio_driver_osx.h +++ b/platform/osx/audio_driver_osx.h @@ -45,8 +45,9 @@ class AudioDriverOSX : public AudioDriver { Mutex *mutex; int mix_rate; - int channels; - int buffer_frames; + unsigned int channels; + unsigned int buffer_frames; + unsigned int buffer_size; Vector<int32_t> samples_in; |