summaryrefslogtreecommitdiff
path: root/platform/osx
diff options
context:
space:
mode:
authorMarcelo Fernandez <marcelofg55@gmail.com>2017-08-29 16:47:44 -0300
committerMarcelo Fernandez <marcelofg55@gmail.com>2017-09-01 11:12:13 -0300
commitf231eadc9e2487c70db04f912578ec853f11737c (patch)
tree4e41ad5a1608dcb0f77e21c312c46d0a26613399 /platform/osx
parent06d7e36898d274c2403dcfbe5a83a9d858af0661 (diff)
Corrections to audio buffer size calculations
Diffstat (limited to 'platform/osx')
-rw-r--r--platform/osx/audio_driver_osx.cpp26
-rw-r--r--platform/osx/audio_driver_osx.h5
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;