mirror of
https://github.com/juce-framework/JUCE.git
synced 2026-01-10 23:44:24 +00:00
iOS: Fix an issue in which the reported sample rate may not always be correct
This commit is contained in:
parent
0864d614fd
commit
bd7eff0705
1 changed files with 46 additions and 50 deletions
|
|
@ -481,66 +481,62 @@ struct iOSAudioIODevice::Pimpl final : public AsyncUpdater
|
|||
{
|
||||
auto session = [AVAudioSession sharedInstance];
|
||||
|
||||
if (exactlyEqual (session.sampleRate, rate))
|
||||
return rate;
|
||||
|
||||
const auto startingSampleRate = session.sampleRate;
|
||||
|
||||
setAudioSessionActive (false);
|
||||
|
||||
JUCE_NSERROR_CHECK ([session setPreferredSampleRate: rate error: &error]);
|
||||
|
||||
setAudioSessionActive (true);
|
||||
|
||||
if (! exactlyEqual (startingSampleRate, session.sampleRate))
|
||||
return session.sampleRate;
|
||||
|
||||
AudioStreamBasicDescription stream{};
|
||||
stream.mFormatID = kAudioFormatLinearPCM;
|
||||
stream.mSampleRate = rate;
|
||||
stream.mChannelsPerFrame = 2;
|
||||
stream.mBitsPerChannel = 32;
|
||||
stream.mFramesPerPacket = 1;
|
||||
stream.mBytesPerFrame = stream.mChannelsPerFrame * stream.mBitsPerChannel / 8;
|
||||
stream.mBytesPerPacket = stream.mBytesPerFrame * stream.mFramesPerPacket;
|
||||
stream.mFormatFlags = stream.mBitsPerChannel;
|
||||
stream.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger
|
||||
| kLinearPCMFormatFlagIsBigEndian
|
||||
| kLinearPCMFormatFlagIsPacked;
|
||||
|
||||
AudioQueueRef audioQueue;
|
||||
|
||||
auto err = AudioQueueNewOutput (&stream,
|
||||
[] (auto, auto, auto) {},
|
||||
nullptr,
|
||||
nullptr,
|
||||
kCFRunLoopCommonModes,
|
||||
0,
|
||||
&audioQueue);
|
||||
|
||||
if (err != noErr || audioQueue == nullptr)
|
||||
if (@available (ios 18, *))
|
||||
{
|
||||
AudioStreamBasicDescription stream{};
|
||||
stream.mFormatID = kAudioFormatLinearPCM;
|
||||
stream.mSampleRate = rate;
|
||||
stream.mChannelsPerFrame = 2;
|
||||
stream.mBitsPerChannel = 32;
|
||||
stream.mFramesPerPacket = 1;
|
||||
stream.mBytesPerFrame = stream.mChannelsPerFrame * stream.mBitsPerChannel / 8;
|
||||
stream.mBytesPerPacket = stream.mBytesPerFrame * stream.mFramesPerPacket;
|
||||
stream.mFormatFlags = stream.mBitsPerChannel;
|
||||
stream.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger
|
||||
| kLinearPCMFormatFlagIsBigEndian
|
||||
| kLinearPCMFormatFlagIsPacked;
|
||||
|
||||
AudioQueueRef audioQueue;
|
||||
|
||||
auto err = AudioQueueNewOutput (&stream,
|
||||
[] (auto, auto, auto) {},
|
||||
nullptr,
|
||||
nullptr,
|
||||
kCFRunLoopCommonModes,
|
||||
0,
|
||||
&audioQueue);
|
||||
|
||||
if (err != noErr || audioQueue == nullptr)
|
||||
{
|
||||
jassertfalse;
|
||||
return session.sampleRate;
|
||||
}
|
||||
|
||||
const ScopeGuard disposeAudioQueueOnReturn { [&]
|
||||
{
|
||||
AudioQueueDispose (audioQueue, true);
|
||||
}};
|
||||
|
||||
Float64 deviceSampleRate{};
|
||||
UInt32 size = sizeof (deviceSampleRate);
|
||||
|
||||
err = AudioQueueGetProperty (audioQueue,
|
||||
kAudioQueueDeviceProperty_SampleRate,
|
||||
&deviceSampleRate,
|
||||
&size);
|
||||
|
||||
if (err == noErr && size == sizeof (deviceSampleRate))
|
||||
return deviceSampleRate;
|
||||
|
||||
jassertfalse;
|
||||
return session.sampleRate;
|
||||
}
|
||||
|
||||
const ScopeGuard disposeAudioQueueOnReturn { [&]
|
||||
{
|
||||
AudioQueueDispose (audioQueue, true);
|
||||
}};
|
||||
|
||||
Float64 deviceSampleRate{};
|
||||
UInt32 size = sizeof (deviceSampleRate);
|
||||
|
||||
err = AudioQueueGetProperty (audioQueue,
|
||||
kAudioQueueDeviceProperty_SampleRate,
|
||||
&deviceSampleRate,
|
||||
&size);
|
||||
|
||||
if (err == noErr && size == sizeof (deviceSampleRate))
|
||||
return deviceSampleRate;
|
||||
|
||||
jassertfalse;
|
||||
return session.sampleRate;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue