<commit_before><commit_msg>Initial Keyboard View for playgrounds<commit_after>//
//  TeyboardView.swift
//  AudioKit
//
//  Created by <NAME>, revision history on Github.
//  Copyright (c) 2016 <NAME>. All rights reserved.
//

import UIKit

public protocol KeyboardDelegate {
    func noteOn(note: Int)
    func noteOff(note: Int)
}

public class KeyboardView: UIView {
    
    public var delegate: KeyboardDelegate?
    var keys: [UIView] = []
    
    public var lowestKey = 48
    public var totalKeys = 88
    let notesWithFlats  = ["C", "D♭", "D", "E♭", "E", "F", "G♭", "G", "A♭", "A", "B♭", "B"]
    let notesWithSharps = ["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"]
    
    
    public init(width: Int, height: Int) {
        super.init(frame: CGRect(x: 0, y: 0, width: width, height: height))
        let allowedNotes = notesWithSharps //["A", "B", "C#", "D", "E", "F#", "G"]
        
        let keyWidth = width / totalKeys - 1
        let height = Int(frame.height)
        
        let blackFrame = UIView(frame: CGRect(x: 0, y: 0, width: (keyWidth + 1) * totalKeys + 1, height: height))
        blackFrame.backgroundColor = UIColor.blackColor()
        self.addSubview(blackFrame)
        
        var keyCount = 0
        var increment = 0
        while keyCount < totalKeys {
            if  allowedNotes.indexOf(notesWithFlats[(lowestKey + increment) % 12]) != nil || allowedNotes.indexOf(notesWithSharps[(lowestKey + increment) % 12]) != nil {
                let newButton = UIView(frame:CGRect(x: 0, y: 0, width: keyWidth, height: height - 2))
                if notesWithSharps[(lowestKey + increment) % 12].rangeOfString("#") != nil {
                    newButton.backgroundColor = UIColor.blackColor()
                } else {
                    newButton.backgroundColor = UIColor.whiteColor()
                }
                
                newButton.setNeedsDisplay()
                
                newButton.frame.origin.x = CGFloat(keyCount * (keyWidth + 1)) + 1
                newButton.frame.origin.y = CGFloat(1)
                newButton.tag = lowestKey + increment
                keys.append(newButton)
                self.addSubview(newButton)
                keyCount += 1
                
            }
            increment += 1
            
        }
    }
    
    required public init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    // *********************************************************
    // MARK: - Handle Touches
    // *********************************************************
    
    override public func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        for touch in touches {
            let touchPoint = touch.locationInView(self)
            
            for key in keys {
                if CGRectContainsPoint(key.frame, touchPoint) {
                    delegate?.noteOn(key.tag)
                }
            }
            
        }
    }
    
    override public func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
        for touch in touches {
            let touchPoint = touch.locationInView(self)
            let prevPoint = touch.previousLocationInView(self)
            
            for key in keys {
                if CGRectContainsPoint(key.frame, touchPoint) {
                    delegate?.noteOn(key.tag)
                }
            }
            
            // determine vertical value
            //setPercentagesWithTouchPoint(touchPoint)
        }
    }
    
    override public func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
        for touch in touches {
            let prevPoint = touch.previousLocationInView(self)
            delegate?.noteOff(0)
        }
    }
}

{'fraction_non_alphanumeric': 0.14383923849814914, 'fraction_numerical': 0.008461131676361713, 'mean_word_length': 1.8231343283582089, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 8, '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': '4627649', 'n_tokens_mistral': 1135, 'n_tokens_neox': 1076, 'n_words': 263}