summaryrefslogtreecommitdiff
path: root/RtAudio.cpp
diff options
context:
space:
mode:
authorGary Scavone <gary@music.mcgill.ca>2019-04-05 15:05:33 -0400
committerGary Scavone <gary@music.mcgill.ca>2019-04-19 15:11:35 -0400
commitf035d7a7b6de3568e51d6e8854b06078c91cea9f (patch)
treeb6036d45adb02c15e31c57f1e661c26d31bf8d42 /RtAudio.cpp
parent0e95564f14bea955bcbca94b224dc5fdd99458a8 (diff)
More updates for sample rate changes, plus added support to get current system rate (OS-X only).
Diffstat (limited to 'RtAudio.cpp')
-rw-r--r--RtAudio.cpp47
1 files changed, 11 insertions, 36 deletions
diff --git a/RtAudio.cpp b/RtAudio.cpp
index 84c9499..3f3c559 100644
--- a/RtAudio.cpp
+++ b/RtAudio.cpp
@@ -876,6 +876,13 @@ RtAudio::DeviceInfo RtApiCore :: getDeviceInfo( unsigned int device )
return info;
}
+ // Probe the currently configured sample rate
+ Float64 nominalRate;
+ dataSize = sizeof( Float64 );
+ property.mSelector = kAudioDevicePropertyNominalSampleRate;
+ result = AudioObjectGetPropertyData( id, &property, 0, NULL, &dataSize, &nominalRate );
+ if ( result == noErr ) info.currentSampleRate = (unsigned int) nominalRate;
+
// CoreAudio always uses 32-bit floating point data for PCM streams.
// Thus, any other "physical" formats supported by the device are of
// no interest to the client.
@@ -943,22 +950,6 @@ static OSStatus xrunListener( AudioObjectID /*inDevice*/,
return kAudioHardwareNoError;
}
-//static OSStatus rateListener( AudioObjectID inDevice,
-// UInt32 /*nAddresses*/,
-// const AudioObjectPropertyAddress /*properties*/[],
-// void* ratePointer )
-/*
-{
- Float64 *rate = (Float64 *) ratePointer;
- UInt32 dataSize = sizeof( Float64 );
- AudioObjectPropertyAddress property = { kAudioDevicePropertyNominalSampleRate,
- kAudioObjectPropertyScopeGlobal,
- kAudioObjectPropertyElementMaster };
- AudioObjectGetPropertyData( inDevice, &property, 0, NULL, &dataSize, rate );
- return kAudioHardwareNoError;
-}
-*/
-
bool RtApiCore :: probeDeviceOpen( unsigned int device, StreamMode mode, unsigned int channels,
unsigned int firstChannel, unsigned int sampleRate,
RtAudioFormat format, unsigned int *bufferSize,
@@ -1168,25 +1159,12 @@ bool RtApiCore :: probeDeviceOpen( unsigned int device, StreamMode mode, unsigne
return FAILURE;
}
- // Only change the sample rate if off by more than 1 Hz.
+ // Only try to change the sample rate if off by more than 1 Hz.
if ( fabs( nominalRate - (double)sampleRate ) > 1.0 ) {
- // Set a property listener for the sample rate change
- Float64 reportedRate = 0.0;
- /*
- AudioObjectPropertyAddress tmp = { kAudioDevicePropertyNominalSampleRate, kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMaster };
- result = AudioObjectAddPropertyListener( id, &tmp, rateListener, (void *) &reportedRate );
- if ( result != noErr ) {
- errorStream_ << "RtApiCore::probeDeviceOpen: system error (" << getErrorCode( result ) << ") setting sample rate property listener for device (" << device << ").";
- errorText_ = errorStream_.str();
- return FAILURE;
- }
- */
-
nominalRate = (Float64) sampleRate;
result = AudioObjectSetPropertyData( id, &property, 0, NULL, dataSize, &nominalRate );
if ( result != noErr ) {
- //AudioObjectRemovePropertyListener( id, &tmp, rateListener, (void *) &reportedRate );
errorStream_ << "RtApiCore::probeDeviceOpen: system error (" << getErrorCode( result ) << ") setting sample rate for device (" << device << ").";
errorText_ = errorStream_.str();
return FAILURE;
@@ -1194,18 +1172,15 @@ bool RtApiCore :: probeDeviceOpen( unsigned int device, StreamMode mode, unsigne
// Now wait until the reported nominal rate is what we just set.
UInt32 microCounter = 0;
+ Float64 reportedRate = 0.0;
while ( reportedRate != nominalRate ) {
microCounter += 5000;
- if ( microCounter > 5000000 ) break;
+ if ( microCounter > 2000000 ) break;
usleep( 5000 );
result = AudioObjectGetPropertyData( id, &property, 0, NULL, &dataSize, &reportedRate );
}
- std::cout << "microCounter = " << microCounter << std::endl;
-
- // Remove the property listener.
- //AudioObjectRemovePropertyListener( id, &tmp, rateListener, (void *) &reportedRate );
- if ( microCounter > 5000000 ) {
+ if ( microCounter > 2000000 ) {
errorStream_ << "RtApiCore::probeDeviceOpen: timeout waiting for sample rate update for device (" << device << ").";
errorText_ = errorStream_.str();
return FAILURE;