         
         // listen for iPod playback changes
         musicPlayer.beginGeneratingPlaybackNotifications()
+        NotificationCenter.default.addObserver(self, selector: #selector(updateChannelMap), name: AVAudioSession.routeChangeNotification, object: nil)
         NotificationCenter.default.addObserver(self, selector: #selector(playbackStateDidChange(_:)), name:  .MPMusicPlayerControllerPlaybackStateDidChange, object: nil)
     }
     

        engine.connect(player, to: mixer, fromBus: 0, toBus: 0, format: outputHWFormat)
        
        updateChannelMap()
        startAudioEngine()
    }
    
    func startAudioEngine() {

        return session.currentRoute.outputs.compactMap { $0.channels }.flatMap { $0 }
    }
    
    @objc func updateChannelMap() {
        let channelCount = Int(engine.outputNode.outputFormat(forBus: 0).channelCount)
        
        // with 6 channels [-1, -1, 0, 1, -1, -1] would use channels 3 & 4
