                 print("AKNodeRecorder Error: Cannot create an empty audio file")
                 throw error
             }
-
+            
         } else {
-
+            
             do {
                 // We initialize AKAudioFile for writing (and check that we can write to)
                 self.internalAudioFile = try AKAudioFile(forWriting: file!.url,

                    permissionGranted = false
                }
            }

            if !permissionGranted {
                print("AKNodeRecorder Error: Permission to record not granted")
                throw NSError(domain: NSURLErrorDomain,
                              code: NSURLErrorUnknown,
                              userInfo: nil)
            }

            // Sets AVAudioSession Category to be Play and Record

            if (AKSettings.session.category != AKSettings.SessionCategory.playAndRecord.rawValue) {
                do {
                    try AKSettings.setSession(category: .playAndRecord)

                }
            }
        #endif
        
        
        if  node != nil {
            
            let recordingBufferLength: AVAudioFrameCount = AKSettings.recordingBufferLength.samplesCount
            recording = true
            
            print("recording")
            node!.avAudioNode.installTap(onBus: 0, bufferSize: recordingBufferLength,
                                         format: internalAudioFile.processingFormat) {
                                            (buffer: AVAudioPCMBuffer!, time: AVAudioTime!) -> Void in
                                            do {
                                                self.recordBufferDuration = Double(buffer.frameLength) / AKSettings.sampleRate
                                                try self.internalAudioFile.write(from: buffer)
                                                print("writing ( file duration:  \(self.internalAudioFile.duration) seconds)")
                                                
                                                // allow an optional timed stop
                                                if self.durationToRecord != 0 && self.internalAudioFile.duration >= self.durationToRecord {
                                                    self.stop()
                                                }
                                                
                                            } catch let error as NSError {
                                                print("Write failed: error -> \(error.localizedDescription)")
                                            }
            }
        } else {
            print("AKNodeRecorder Error: input node is not available")
        }
    }
    
    /// Stop recording
    open func stop() {
        if !recording {
            print("AKNodeRecorder Warning: Cannot stop recording, already stopped !")
            return
        }
        
        recording = false
        if  node != nil {
            if AKSettings.fixTruncatedRecordings {
