<commit_before>//
//  Waver.swift
//  Waver-Swift
//
//  Created by kevinzhow on 15/4/1.
//  Copyright (c) 2015年 kevinzhow. All rights reserved.
//

import UIKit

class Waver: UIView {
    
    var displayLink: CADisplayLink!
    
    var numberOfWaves: Int = 5
    
    var waveColor: UIColor = UIColor(red: 50/255.0, green: 167/255.0, blue: 255/255.0, alpha: 1.0)
    
    private var phase: CGFloat = 0
    
    var presented = false
    
    var level: CGFloat! {
        didSet {
            self.phase+=self.phaseShift; // Move the wave
            self.amplitude = fmax( level, self.idleAmplitude)
            
            self.appendValue(pow(CGFloat(level),3))
            
            self.updateMeters()
        }
    }
    
    var mainWaveWidth: CGFloat = 2.0
    
    var decorativeWavesWidth: CGFloat = 1.0
    
    var idleAmplitude: CGFloat = 0.01
    
    var frequency: CGFloat = 1.2
    
    internal private(set) var amplitude: CGFloat = 1.0
    
    var density: CGFloat = 1.0
    
    var phaseShift: CGFloat = -0.25
    
    internal private(set) var waves: NSMutableArray = []
    
    //
    
    var waveHeight: CGFloat!
    var waveWidth: CGFloat!
    var waveMid: CGFloat!
    var maxAmplitude: CGFloat!
    
    // Sample Data
    
    var waveSampleCount = 0
    
    var waveSamples = [CGFloat]()
    
    var waveTotalCount: CGFloat!
    
    var waveSquareWidth: CGFloat = 2.0
    
    var waveGap: CGFloat = 1.0
    
    var maxSquareWaveLength = 256
    
    var maxTime = 60
    
    var fps = 6
    
    //

    var waverCallback: ((waver: Waver) -> ())? {
        didSet {
            displayLink = CADisplayLink(target: self, selector: Selector("callbackWaver"))
            displayLink.addToRunLoop(NSRunLoop.currentRunLoop(), forMode: NSRunLoopCommonModes)
            
            for var i = 0; i < self.numberOfWaves; ++i {
                let waveline = CAShapeLayer()
                waveline.lineCap       = kCALineCapButt
                waveline.lineJoin      = kCALineJoinRound
                waveline.strokeColor   = UIColor.clearColor().CGColor
                waveline.fillColor     = UIColor.clearColor().CGColor
                waveline.lineWidth = (i==0 ? self.mainWaveWidth : self.decorativeWavesWidth)

                let floatI = CGFloat(i)
                let progressIndex = floatI/CGFloat(self.numberOfWaves)
                let progress = 1.0 - progressIndex
                let multiplier = min(1.0, (progress/3.0*2.0) + (1.0/3.0))
                
                waveline.strokeColor   = waveColor.colorWithAlphaComponent(( i == 0 ? 1.0 : 1.0*multiplier*0.4)).CGColor
                
                self.layer.addSublayer(waveline)
                self.waves.addObject(waveline)
            }
            
        }
    }

    deinit {
        displayLink.invalidate()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setup()
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setup()
    }
    
    func appendValue(newValue: CGFloat) {
        
        if ++waveSampleCount % fps == 0{
            
            waveSamples.append(newValue)
            
            updateMeters()
        }
    }
    
    private func setup() {
        
        self.waveHeight = CGRectGetHeight(self.bounds) * 0.9
        self.waveWidth = CGRectGetWidth(self.bounds)
        self.waveMid = self.waveWidth/2.0
        self.maxAmplitude = self.waveHeight - 4.0
        
    }
    
    func callbackWaver() {
        if presented {
            waverCallback!(waver: self)
        }
    }
    
    private func updateMeters() {
        UIGraphicsBeginImageContext(self.frame.size)
        
        for var i=0; i < self.numberOfWaves; ++i {
            
            
            let wavelinePath = UIBezierPath()
            
            // Progress is a value between 1.0 and -0.5, determined by the current wave idx, which is used to alter the wave's amplitude.
            let progress = 1.0 - CGFloat(i)/CGFloat(self.numberOfWaves)
            let normedAmplitude = (1.5*progress-0.5)*self.amplitude
            
            
            for var x = 0 as CGFloat; x<self.waveWidth + self.density; x += self.density {
                
                //Thanks to https://github.com/stefanceriu/SCSiriWaveformView
                // We use a parable to scale the sinus wave, that has its peak in the middle of the view.
                let scaling = -pow(x/self.waveMid-1, 2) + 1 // make center bigger
                
                var y = scaling*self.maxAmplitude*normedAmplitude
                let temp = 2.0*CGFloat(M_PI)*(x/self.waveWidth)*self.frequency
                let temp2 = temp+self.phase
                y = CGFloat(y)*CGFloat(sinf(Float(temp2))) + self.waveHeight
                
                if (x==0) {
                    wavelinePath.moveToPoint(CGPointMake(x, y))
                }
                else {
                    wavelinePath.addLineToPoint(CGPointMake(x, y))
                }
            }
            
            let waveline = self.waves.objectAtIndex(i) as! CAShapeLayer
            waveline.path = wavelinePath.CGPath
            
        }
        
        UIGraphicsEndImageContext()
    }
    
    func compressSamples() -> [Float]? {
        
        println("Begin compress")

        if waveSamples.count < 1 {
            return nil
        }
        
        let sampleMax = waveSamples.maxElement()!
<commit_msg>replace C loop in Waver<commit_after>//
//  Waver.swift
//  Waver-Swift
//
//  Created by kevinzhow on 15/4/1.
//  Copyright (c) 2015年 kevinzhow. All rights reserved.
//

import UIKit

class Waver: UIView {
    
    var displayLink: CADisplayLink!
    
    var numberOfWaves: Int = 5
    
    var waveColor: UIColor = UIColor(red: 50/255.0, green: 167/255.0, blue: 255/255.0, alpha: 1.0)
    
    private var phase: CGFloat = 0
    
    var presented = false
    
    var level: CGFloat! {
        didSet {
            self.phase+=self.phaseShift; // Move the wave
            self.amplitude = fmax( level, self.idleAmplitude)
            
            self.appendValue(pow(CGFloat(level),3))
            
            self.updateMeters()
        }
    }
    
    var mainWaveWidth: CGFloat = 2.0
    
    var decorativeWavesWidth: CGFloat = 1.0
    
    var idleAmplitude: CGFloat = 0.01
    
    var frequency: CGFloat = 1.2
    
    internal private(set) var amplitude: CGFloat = 1.0
    
    var density: CGFloat = 1.0
    
    var phaseShift: CGFloat = -0.25
    
    internal private(set) var waves: NSMutableArray = []
    
    //
    
    var waveHeight: CGFloat!
    var waveWidth: CGFloat!
    var waveMid: CGFloat!
    var maxAmplitude: CGFloat!
    
    // Sample Data
    
    var waveSampleCount = 0
    
    var waveSamples = [CGFloat]()
    
    var waveTotalCount: CGFloat!
    
    var waveSquareWidth: CGFloat = 2.0
    
    var waveGap: CGFloat = 1.0
    
    var maxSquareWaveLength = 256
    
    var maxTime = 60
    
    var fps = 6
    
    //

    var waverCallback: ((waver: Waver) -> ())? {
        didSet {
            displayLink = CADisplayLink(target: self, selector: #selector(Waver.callbackWaver))
            displayLink.addToRunLoop(NSRunLoop.currentRunLoop(), forMode: NSRunLoopCommonModes)
            
            (0..<self.numberOfWaves).forEach { i in
                let waveline = CAShapeLayer()
                waveline.lineCap       = kCALineCapButt
                waveline.lineJoin      = kCALineJoinRound
                waveline.strokeColor   = UIColor.clearColor().CGColor
                waveline.fillColor     = UIColor.clearColor().CGColor
                waveline.lineWidth = (i==0 ? self.mainWaveWidth : self.decorativeWavesWidth)

                let floatI = CGFloat(i)
                let progressIndex = floatI/CGFloat(self.numberOfWaves)
                let progress = 1.0 - progressIndex
                let multiplier = min(1.0, (progress/3.0*2.0) + (1.0/3.0))
                
                waveline.strokeColor   = waveColor.colorWithAlphaComponent(( i == 0 ? 1.0 : 1.0*multiplier*0.4)).CGColor
                
                self.layer.addSublayer(waveline)
                self.waves.addObject(waveline)
            }
        }
    }

    deinit {
        displayLink.invalidate()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setup()
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setup()
    }
    
    func appendValue(newValue: CGFloat) {

        waveSampleCount += 1

        if waveSampleCount % fps == 0{
            
            waveSamples.append(newValue)
            
            updateMeters()
        }
    }
    
    private func setup() {
        
        self.waveHeight = CGRectGetHeight(self.bounds) * 0.9
        self.waveWidth = CGRectGetWidth(self.bounds)
        self.waveMid = self.waveWidth/2.0
        self.maxAmplitude = self.waveHeight - 4.0
        
    }
    
    func callbackWaver() {
        if presented {
            waverCallback!(waver: self)
        }
    }
    
    private func updateMeters() {
        UIGraphicsBeginImageContext(self.frame.size)
        
        (0..<self.numberOfWaves).forEach { i in
            
            let wavelinePath = UIBezierPath()
            
            // Progress is a value between 1.0 and -0.5, determined by the current wave idx, which is used to alter the wave's amplitude.
            let progress = 1.0 - CGFloat(i)/CGFloat(self.numberOfWaves)
            let normedAmplitude = (1.5*progress-0.5)*self.amplitude

            var x: CGFloat = 0
            while x < self.waveWidth + self.density {

                //Thanks to https://github.com/stefanceriu/SCSiriWaveformView
                // We use a parable to scale the sinus wave, that has its peak in the middle of the view.
                let scaling = -pow(x/self.waveMid-1, 2) + 1 // make center bigger
                
                var y = scaling*self.maxAmplitude*normedAmplitude
                let temp = 2.0*CGFloat(M_PI)*(x/self.waveWidth)*self.frequency
                let temp2 = temp+self.phase
                y = CGFloat(y)*CGFloat(sinf(Float(temp2))) + self.waveHeight
                
                if (x==0) {
                    wavelinePath.moveToPoint(CGPointMake(x, y))
                }
                else {
                    wavelinePath.addLineToPoint(CGPointMake(x, y))
                }

                x += self.density
            }
            
            let waveline = self.waves.objectAtIndex(i) as! CAShapeLayer
            waveline.path = wavelinePath.CGPath
            
        }
        
        UIGraphicsEndImageContext()
    }
    
    func compressSamples() -> [Float]? {
        
        println("Begin compress")

        if waveSamples.count < 1 {
            return nil
        }
        
        let sampleMax = waveSamples.maxElement()!

{'fraction_non_alphanumeric': 0.0932364956876986, 'fraction_numerical': 0.01988197911938266, 'mean_word_length': 1.5190944431740223, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 7, 'https://': 2, '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': '3287950', 'n_tokens_mistral': 3585, 'n_tokens_neox': 3374, 'n_words': 715}