<commit_before>import AVFoundation

@objc public protocol RecorderDelegate: AVAudioRecorderDelegate {
    optional func audioMeterDidUpdate(db: Float)
}

public class Recording : NSObject {
    var recorder: AVAudioRecorder!
    var player: AVAudioPlayer!
    var session: AVAudioSession!
    var delegate: RecorderDelegate!
    var metering: Bool
    var url: NSURL!
    var meterTimer: NSTimer!
    
    var directory: NSString {
        return NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as NSString
    }
    
    var bitRate = 192000
    var sampleRate = 44100.0
    var channels = 1
    
    public init(to: NSString, on: RecorderDelegate, withMetering:Bool = false)
    {
        self.delegate = on
        self.metering = withMetering
        self.session  = AVAudioSession.sharedInstance()
        
        super.init()
        
        self.url = NSURL(fileURLWithPath: directory.stringByAppendingPathComponent(to))
    }
    
    public func record()
    {
        self.session.setCategory(AVAudioSessionCategoryRecord, error: nil)
        
        self.recorder = AVAudioRecorder(URL: url, settings: [
            AVFormatIDKey: kAudioFormatAppleLossless,
            AVEncoderAudioQualityKey: AVAudioQuality.Max.rawValue,
            AVEncoderBitRateKey: bitRate,
            AVNumberOfChannelsKey: channels,
            AVSampleRateKey: sampleRate
        ]
        , error: nil)

        recorder.delegate = delegate
        
        recorder.prepareToRecord()
        
        if metering {
            recorder.meteringEnabled = metering
            startMetering()
        }
        
        recorder.record()
    }
    
    public func stop()
    {
        recorder.stop()
        
        if metering {
            stopMetering()
        }
    }
    
    public func play()
    {
        self.session.setCategory(AVAudioSessionCategoryPlayback, error: nil)
        
        self.player = AVAudioPlayer(contentsOfURL: url, error: nil)
        player.play()
    }
    
    public func startMetering()
    {
        meterTimer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "updateMeter", userInfo: nil, repeats: true)
    }
    
    public func updateMeter()
    {
        recorder.updateMeters()

        var db = recorder.averagePowerForChannel(0)

        delegate.audioMeterDidUpdate?(db)
    }
    
    public func stopMetering()
    {
        meterTimer.invalidate()
    }
    
}<commit_msg>Update meter twice as often<commit_after>import AVFoundation

@objc public protocol RecorderDelegate: AVAudioRecorderDelegate {
    optional func audioMeterDidUpdate(db: Float)
}

public class Recording : NSObject {
    var recorder: AVAudioRecorder!
    var player: AVAudioPlayer!
    var session: AVAudioSession!
    var delegate: RecorderDelegate!
    var metering: Bool
    var url: NSURL!
    var meterTimer: NSTimer!
    
    var directory: NSString {
        return NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as NSString
    }
    
    var bitRate = 192000
    var sampleRate = 44100.0
    var channels = 1
    
    public init(to: NSString, on: RecorderDelegate, withMetering:Bool = false)
    {
        self.delegate = on
        self.metering = withMetering
        self.session  = AVAudioSession.sharedInstance()
        
        super.init()
        
        self.url = NSURL(fileURLWithPath: directory.stringByAppendingPathComponent(to))
    }
    
    public func record()
    {
        self.session.setCategory(AVAudioSessionCategoryRecord, error: nil)
        
        self.recorder = AVAudioRecorder(URL: url, settings: [
            AVFormatIDKey: kAudioFormatAppleLossless,
            AVEncoderAudioQualityKey: AVAudioQuality.Max.rawValue,
            AVEncoderBitRateKey: bitRate,
            AVNumberOfChannelsKey: channels,
            AVSampleRateKey: sampleRate
        ]
        , error: nil)

        recorder.delegate = delegate
        
        recorder.prepareToRecord()
        
        if metering {
            recorder.meteringEnabled = metering
            startMetering()
        }
        
        recorder.record()
    }
    
    public func stop()
    {
        recorder.stop()
        
        if metering {
            stopMetering()
        }
    }
    
    public func play()
    {
        self.session.setCategory(AVAudioSessionCategoryPlayback, error: nil)
        
        self.player = AVAudioPlayer(contentsOfURL: url, error: nil)
        player.play()
    }
    
    public func startMetering()
    {
        meterTimer = NSTimer.scheduledTimerWithTimeInterval(0.05, target: self, selector: "updateMeter", userInfo: nil, repeats: true)
    }
    
    public func updateMeter()
    {
        recorder.updateMeters()

        var db = recorder.averagePowerForChannel(0)

        delegate.audioMeterDidUpdate?(db)
    }
    
    public func stopMetering()
    {
        meterTimer.invalidate()
    }
    
}
{'fraction_non_alphanumeric': 0.07610227501509965, 'fraction_numerical': 0.007046506945842561, 'mean_word_length': 2.1542857142857144, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '91745', 'n_tokens_mistral': 1466, 'n_tokens_neox': 1409, 'n_words': 296}