Now that I know that your AudioDeviceManager::initialiseWithDefaultDevices(int,int) builds the hardware setup based on the input device's settings regardless of if the output device is set to something different, I'm sharing the code that i use to build a custom AudioDeviceSetup that is based on the system output device's settings
here's the code to set it up:
void setupDefaultAudioDevice() {
/*
this is for some unexpected behavior on OS X, where JUCE tries to build an
audioDeviceManager setup based on the Default System Input's settings. if your output
device is not your input device, JUCE will try to reclock your output device to the input
device's settings. this corrects that and initializes the AudioDeviceManager with the output device's settings.
*/
juce::AudioDeviceManager::AudioDeviceSetup defaultDeviceSetup;
#ifdef JUCE_MAC
int inChan = 1;
int outChan = 1;
Float64 sampleRate;
ChordieApp::MacAudioUtilities::GetDefaultOutputDeviceSampleRate(&sampleRate);
CFStringRef deviceName;
ChordieApp::MacAudioUtilities::GetDefaultOutputDeviceName( &deviceName);
CFShow( deviceName );
String name = String( CFStringGetCStringPtr(deviceName, kCFStringEncodingMacRoman) );
UInt32 bufferSize;
ChordieApp::MacAudioUtilities::GetDefaultOutputDeviceBufferSize( &bufferSize);
defaultDeviceSetup.sampleRate = sampleRate;
defaultDeviceSetup.outputDeviceName = name;
defaultDeviceSetup.inputDeviceName = name;
defaultDeviceSetup.bufferSize = bufferSize;
audioDeviceManager->initialise(inChan, outChan, nullptr, false, name, &defaultDeviceSetup );
#elif JUCE_WINDOWS
audioDeviceManager->initialiseWithDefaultDevices(1, 1);
#endif
}
and here is the utility class that does the hard work:
#ifndef MACAUDIOUTILITIES_H_INCLUDED #define MACAUDIOUTILITIES_H_INCLUDED #include <AudioToolbox/AudioToolbox.h> #include "../JuceLibraryCode/JuceHeader.h" //============================================================================== /* */ namespace ChordieApp { class MacAudioUtilities { public: MacAudioUtilities() {} ~MacAudioUtilities() {} static OSStatus GetDefaultOutputDeviceSampleRate(Float64 *outSampleRate) { OSStatus error; AudioDeviceID deviceID = 0; AudioObjectPropertyAddress propertyAddress; UInt32 propertySize; // propertyAddress.mSelector = kAudioHardwarePropertyDefaultSystemOutputDevice; propertyAddress.mScope = kAudioObjectPropertyScopeGlobal; propertyAddress.mElement = 0; propertySize = sizeof(AudioDeviceID); // error = AudioHardwareServiceGetPropertyData( kAudioObjectSystemObject, &propertyAddress, 0, nullptr, &propertySize, &deviceID); if( error) return error; // propertyAddress.mSelector = kAudioDevicePropertyNominalSampleRate; propertyAddress.mScope = kAudioObjectPropertyScopeGlobal; propertyAddress.mElement = 0; propertySize = sizeof(Float64); //gets property( nominal sample rate) error = AudioHardwareServiceGetPropertyData(deviceID, &propertyAddress, 0, nullptr, &propertySize, outSampleRate); return error; } static OSStatus GetDefaultOutputDeviceName(CFStringRef *name) { OSStatus error; AudioDeviceID deviceID = 0; AudioObjectPropertyAddress propertyAddress; UInt32 propertySize; //sets which property to check propertyAddress.mSelector = kAudioHardwarePropertyDefaultSystemOutputDevice; propertyAddress.mScope = kAudioObjectPropertyScopeGlobal; propertyAddress.mElement = 0; propertySize = sizeof(AudioDeviceID); //gets property (system output device) error = AudioHardwareServiceGetPropertyData( kAudioObjectSystemObject, &propertyAddress, 0, nullptr, &propertySize, &deviceID); if( error) return error; //we couldn't get the default system device //sets which property to check propertyAddress.mSelector = kAudioObjectPropertyName; propertyAddress.mScope = kAudioObjectPropertyScopeGlobal; propertyAddress.mElement = 0; propertySize = sizeof(String); //gets property (name) error = AudioHardwareServiceGetPropertyData(deviceID, &propertyAddress, 0, nullptr, &propertySize, name); return error; } static OSStatus GetDefaultOutputDeviceBufferSize(UInt32 *bufferSize) { OSStatus error; AudioDeviceID deviceID = 0; AudioObjectPropertyAddress propertyAddress; UInt32 propertySize; //sets which property to check propertyAddress.mSelector = kAudioHardwarePropertyDefaultSystemOutputDevice; propertyAddress.mScope = kAudioObjectPropertyScopeGlobal; propertyAddress.mElement = 0; propertySize = sizeof(AudioDeviceID); //gets property (system output device) error = AudioHardwareServiceGetPropertyData( kAudioObjectSystemObject, &propertyAddress, 0, nullptr, &propertySize, &deviceID); if( error) return error; //we couldn't get the default system device //sets which property to check propertyAddress.mSelector = kAudioDevicePropertyBufferFrameSize; propertyAddress.mScope = kAudioObjectPropertyScopeGlobal; propertyAddress.mElement = 0; propertySize = sizeof(bufferSize); //UInt32 //gets property (bufferSize) error = AudioHardwareServiceGetPropertyData(deviceID, &propertyAddress, 0, nullptr, &propertySize, bufferSize); return error; } private: JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(MacAudioUtilities) }; }//end namespace #endif // MACAUDIOUTILITIES_H_INCLUDED