Skip to content

Commit

Permalink
AVAudioGameEngine handle sample rate changes
Browse files Browse the repository at this point in the history
Signed-off-by: Joseph Mattiello <[email protected]>
  • Loading branch information
JoeMatt committed Nov 11, 2024
1 parent eeedcb7 commit 5d05f2e
Showing 1 changed file with 46 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,52 @@ final public class AVAudioEngineGameAudioEngine: AudioEngineProtocol {
}
}

/// Delegate for audio sample rate changes
public weak var delegate: PVAudioDelegate?

public init() {
configureAudioSession()
#if !os(macOS)
NotificationCenter.default.addObserver(
self,
selector: #selector(handleAudioRouteChange),
name: AVAudioSession.routeChangeNotification,
object: nil
)
#endif
}

deinit {
stopAudio()
#if !os(macOS)
NotificationCenter.default.removeObserver(self)
#endif
}

@objc private func handleAudioRouteChange(notification: Notification) {
#if !os(macOS)
guard let userInfo = notification.userInfo,
let reasonValue = userInfo[AVAudioSessionRouteChangeReasonKey] as? UInt,
let reason = AVAudioSession.RouteChangeReason(rawValue: reasonValue)
else { return }

switch reason {
case .newDeviceAvailable, .oldDeviceUnavailable:
do {
stopAudio()
configureAudioSession()
startAudio()
} catch {
handleAudioError(error)
}
default:
break
}
#endif
}

private func notifySampleRateChange() {
delegate?.audioSampleRateDidChange()
}

public func setVolume(_ volume: Float) {
Expand All @@ -67,6 +111,8 @@ final public class AVAudioEngineGameAudioEngine: AudioEngineProtocol {

public func setupAudioGraph(for gameCore: EmulatorCoreAudioDataSource) throws {
self.gameCore = gameCore
// Notify delegate when sample rate changes during setup
notifySampleRateChange()
}

/// Stream description computed property for audio format configuration
Expand Down

0 comments on commit 5d05f2e

Please sign in to comment.