Skip to content

Commit

Permalink
Trackpad drummer
Browse files Browse the repository at this point in the history
  • Loading branch information
Dev1an committed Jun 18, 2017
1 parent 5664ad9 commit 09034db
Show file tree
Hide file tree
Showing 12 changed files with 150 additions and 8 deletions.
7 changes: 7 additions & 0 deletions Magic Drumpad.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
74447AB81EF6AD7400C428B9 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 74447AB61EF6AD7400C428B9 /* Main.storyboard */; };
74447AC41EF6AD7400C428B9 /* Magic_DrumpadTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74447AC31EF6AD7400C428B9 /* Magic_DrumpadTests.swift */; };
74447ACF1EF6AD7400C428B9 /* Magic_DrumpadUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74447ACE1EF6AD7400C428B9 /* Magic_DrumpadUITests.swift */; };
7489205F1EF701800098ABDF /* Drummer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7489205E1EF701800098ABDF /* Drummer.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -46,6 +47,7 @@
74447ACA1EF6AD7400C428B9 /* Magic DrumpadUITests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "Magic DrumpadUITests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
74447ACE1EF6AD7400C428B9 /* Magic_DrumpadUITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Magic_DrumpadUITests.swift; sourceTree = "<group>"; };
74447AD01EF6AD7400C428B9 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
7489205E1EF701800098ABDF /* Drummer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Drummer.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -97,6 +99,7 @@
isa = PBXGroup;
children = (
74447AB01EF6AD7400C428B9 /* AppDelegate.swift */,
7489205E1EF701800098ABDF /* Drummer.swift */,
74447AB21EF6AD7400C428B9 /* ViewController.swift */,
74447AB41EF6AD7400C428B9 /* Assets.xcassets */,
74447AB61EF6AD7400C428B9 /* Main.storyboard */,
Expand Down Expand Up @@ -254,6 +257,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
7489205F1EF701800098ABDF /* Drummer.swift in Sources */,
74447AB31EF6AD7400C428B9 /* ViewController.swift in Sources */,
74447AB11EF6AD7400C428B9 /* AppDelegate.swift in Sources */,
);
Expand Down Expand Up @@ -515,6 +519,7 @@
74447AD51EF6AD7400C428B9 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
74447AD61EF6AD7400C428B9 /* Build configuration list for PBXNativeTarget "Magic DrumpadTests" */ = {
isa = XCConfigurationList;
Expand All @@ -523,6 +528,7 @@
74447AD81EF6AD7400C428B9 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
74447AD91EF6AD7400C428B9 /* Build configuration list for PBXNativeTarget "Magic DrumpadUITests" */ = {
isa = XCConfigurationList;
Expand All @@ -531,6 +537,7 @@
74447ADB1EF6AD7400C428B9 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
Expand Down
1 change: 1 addition & 0 deletions Magic Drumpad/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {

func applicationDidFinishLaunching(_ aNotification: Notification) {
// Insert code here to initialize your application
initDrummers()
}

func applicationWillTerminate(_ aNotification: Notification) {
Expand Down
6 changes: 6 additions & 0 deletions Magic Drumpad/Assets.xcassets/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"version" : 1,
"author" : "xcode"
}
}
12 changes: 12 additions & 0 deletions Magic Drumpad/Assets.xcassets/drum1.dataset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"info" : {
"version" : 1,
"author" : "xcode"
},
"data" : [
{
"idiom" : "universal",
"filename" : "drum1.wav"
}
]
}
Binary file not shown.
12 changes: 12 additions & 0 deletions Magic Drumpad/Assets.xcassets/drum2.dataset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"info" : {
"version" : 1,
"author" : "xcode"
},
"data" : [
{
"idiom" : "universal",
"filename" : "drum2.wav"
}
]
}
Binary file not shown.
12 changes: 12 additions & 0 deletions Magic Drumpad/Assets.xcassets/drum3.dataset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"info" : {
"version" : 1,
"author" : "xcode"
},
"data" : [
{
"idiom" : "universal",
"filename" : "drum3.wav"
}
]
}
Binary file not shown.
52 changes: 46 additions & 6 deletions Magic Drumpad/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -705,11 +705,32 @@
<objects>
<viewController id="XfG-lQ-9wD" customClass="ViewController" customModule="Magic_Drumpad" customModuleProvider="target" sceneMemberID="viewController">
<view key="view" wantsLayer="YES" id="m2S-Jp-Qdl">
<rect key="frame" x="0.0" y="0.0" width="480" height="270"/>
<rect key="frame" x="0.0" y="0.0" width="480" height="284"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<box boxType="custom" cornerRadius="4" title="Box" translatesAutoresizingMaskIntoConstraints="NO" id="UlH-Fm-SOT">
<rect key="frame" x="0.0" y="0.0" width="480" height="137"/>
<view key="contentView" id="iLk-pr-yGu">
<rect key="frame" x="1" y="1" width="478" height="135"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
</view>
</box>
<box boxType="custom" cornerRadius="4" title="Box" translatesAutoresizingMaskIntoConstraints="NO" id="i6D-u0-ncL">
<rect key="frame" x="245" y="145" width="235" height="139"/>
<view key="contentView" id="51Q-eU-RV9">
<rect key="frame" x="1" y="1" width="233" height="137"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
</view>
</box>
<box boxType="custom" cornerRadius="4" title="Box" translatesAutoresizingMaskIntoConstraints="NO" id="eAi-lg-W3O">
<rect key="frame" x="0.0" y="145" width="234" height="139"/>
<view key="contentView" ambiguous="YES" id="Bdt-aR-xFc">
<rect key="frame" x="1" y="1" width="232" height="137"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
</view>
</box>
<box fixedFrame="YES" boxType="custom" borderType="none" cornerRadius="20" title="Box" transparent="YES" translatesAutoresizingMaskIntoConstraints="NO" id="der-H1-t4P">
<rect key="frame" x="49" y="178" width="40" height="40"/>
<rect key="frame" x="10" y="129" width="40" height="40"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<view key="contentView" ambiguous="YES" id="RrE-EP-fj4">
<rect key="frame" x="0.0" y="0.0" width="40" height="40"/>
Expand All @@ -718,7 +739,7 @@
<color key="fillColor" name="keyboardFocusIndicatorColor" catalog="System" colorSpace="catalog"/>
</box>
<box fixedFrame="YES" boxType="custom" borderType="none" cornerRadius="20" title="Box" transparent="YES" translatesAutoresizingMaskIntoConstraints="NO" id="Gw6-IW-t3a">
<rect key="frame" x="109" y="178" width="40" height="40"/>
<rect key="frame" x="70" y="129" width="40" height="40"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<view key="contentView" ambiguous="YES" id="sYI-KM-RnC">
<rect key="frame" x="0.0" y="0.0" width="40" height="40"/>
Expand All @@ -727,7 +748,7 @@
<color key="fillColor" name="keyboardFocusIndicatorColor" catalog="System" colorSpace="catalog"/>
</box>
<box fixedFrame="YES" boxType="custom" borderType="none" cornerRadius="20" title="Box" transparent="YES" translatesAutoresizingMaskIntoConstraints="NO" id="h5L-47-BGF">
<rect key="frame" x="169" y="178" width="40" height="40"/>
<rect key="frame" x="130" y="129" width="40" height="40"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<view key="contentView" ambiguous="YES" id="tF9-aB-APH">
<rect key="frame" x="0.0" y="0.0" width="40" height="40"/>
Expand All @@ -736,7 +757,7 @@
<color key="fillColor" name="keyboardFocusIndicatorColor" catalog="System" colorSpace="catalog"/>
</box>
<box fixedFrame="YES" boxType="custom" borderType="none" cornerRadius="20" title="Box" transparent="YES" translatesAutoresizingMaskIntoConstraints="NO" id="ZIC-lB-IG1">
<rect key="frame" x="229" y="178" width="40" height="40"/>
<rect key="frame" x="190" y="129" width="40" height="40"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<view key="contentView" ambiguous="YES" id="1po-6S-oJV">
<rect key="frame" x="0.0" y="0.0" width="40" height="40"/>
Expand All @@ -745,7 +766,7 @@
<color key="fillColor" name="keyboardFocusIndicatorColor" catalog="System" colorSpace="catalog"/>
</box>
<box fixedFrame="YES" boxType="custom" borderType="none" cornerRadius="20" title="Box" transparent="YES" translatesAutoresizingMaskIntoConstraints="NO" id="q9h-Xm-FBj">
<rect key="frame" x="289" y="178" width="40" height="40"/>
<rect key="frame" x="250" y="129" width="40" height="40"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<view key="contentView" ambiguous="YES" id="oeV-5B-eSh">
<rect key="frame" x="0.0" y="0.0" width="40" height="40"/>
Expand All @@ -754,13 +775,32 @@
<color key="fillColor" name="keyboardFocusIndicatorColor" catalog="System" colorSpace="catalog"/>
</box>
</subviews>
<constraints>
<constraint firstItem="eAi-lg-W3O" firstAttribute="leading" secondItem="m2S-Jp-Qdl" secondAttribute="leading" id="044-u6-hhP"/>
<constraint firstAttribute="bottom" secondItem="UlH-Fm-SOT" secondAttribute="bottom" id="5u0-D8-9nK"/>
<constraint firstItem="eAi-lg-W3O" firstAttribute="width" secondItem="51Q-eU-RV9" secondAttribute="width" id="B2n-iQ-ZZ2"/>
<constraint firstItem="i6D-u0-ncL" firstAttribute="width" secondItem="Bdt-aR-xFc" secondAttribute="width" id="EFp-HE-9cb"/>
<constraint firstAttribute="trailing" secondItem="UlH-Fm-SOT" secondAttribute="trailing" id="FMu-9V-LKG"/>
<constraint firstItem="UlH-Fm-SOT" firstAttribute="top" secondItem="Bdt-aR-xFc" secondAttribute="bottom" constant="8" id="GSy-aU-qAB"/>
<constraint firstItem="UlH-Fm-SOT" firstAttribute="leading" secondItem="m2S-Jp-Qdl" secondAttribute="leading" id="HHC-Jo-Vyw"/>
<constraint firstItem="UlH-Fm-SOT" firstAttribute="height" secondItem="51Q-eU-RV9" secondAttribute="height" id="Jii-Jz-woV"/>
<constraint firstItem="i6D-u0-ncL" firstAttribute="leading" secondItem="Bdt-aR-xFc" secondAttribute="trailing" constant="8" id="VHZ-m1-BNd"/>
<constraint firstItem="UlH-Fm-SOT" firstAttribute="top" secondItem="i6D-u0-ncL" secondAttribute="bottom" constant="8" id="ePs-1h-3Z5"/>
<constraint firstAttribute="trailing" secondItem="i6D-u0-ncL" secondAttribute="trailing" id="i6c-TX-96F"/>
<constraint firstItem="eAi-lg-W3O" firstAttribute="top" secondItem="m2S-Jp-Qdl" secondAttribute="top" id="kmc-m9-RZC"/>
<constraint firstItem="i6D-u0-ncL" firstAttribute="top" secondItem="m2S-Jp-Qdl" secondAttribute="top" id="uF5-gg-nsR"/>
<constraint firstItem="UlH-Fm-SOT" firstAttribute="top" secondItem="eAi-lg-W3O" secondAttribute="bottom" constant="8" id="xLD-Y5-DI7"/>
</constraints>
</view>
<connections>
<outlet property="fingerView1" destination="der-H1-t4P" id="OLi-KD-3Ta"/>
<outlet property="fingerView2" destination="Gw6-IW-t3a" id="4zM-bU-BBT"/>
<outlet property="fingerView3" destination="h5L-47-BGF" id="5V5-DW-p1C"/>
<outlet property="fingerView4" destination="ZIC-lB-IG1" id="FgZ-gF-UVZ"/>
<outlet property="fingerView5" destination="q9h-Xm-FBj" id="uuy-KE-ofG"/>
<outlet property="region1" destination="UlH-Fm-SOT" id="iMQ-tl-hBI"/>
<outlet property="region2" destination="i6D-u0-ncL" id="eRF-g7-hz3"/>
<outlet property="region3" destination="eAi-lg-W3O" id="b8g-Jw-xBl"/>
</connections>
</viewController>
<customObject id="rPt-NT-nkU" userLabel="First Responder" customClass="NSResponder" sceneMemberID="firstResponder"/>
Expand Down
38 changes: 38 additions & 0 deletions Magic Drumpad/Drummer.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// Drummer.swift
// Magic Drumpad
//
// Created by Damiaan on 18/06/17.
// Copyright © 2017 Damiaan Dufaux. All rights reserved.
//

import AVFoundation
import AppKit

let drum1 = NSDataAsset(name: .init("drum1"))!.data
let drum2 = NSDataAsset(name: .init("drum2"))!.data
let drum3 = NSDataAsset(name: .init("drum3"))!.data

var drum1players = [AVAudioPlayer]()
var drum2players = [AVAudioPlayer]()
var drum3players = [AVAudioPlayer]()

let count = 100
func initDrummers() {
for _ in 1...count { drum1players.append(try! AVAudioPlayer(data: drum1)) }
for _ in 1...count { drum2players.append(try! AVAudioPlayer(data: drum2)) }
for _ in 1...count { drum3players.append(try! AVAudioPlayer(data: drum3)) }
}

func playDrum1() {
let player = drum1players.first {!$0.isPlaying}
player?.play()
}
func playDrum2() {
let player = drum2players.first {!$0.isPlaying}
player?.play()
}
func playDrum3() {
let player = drum3players.first {!$0.isPlaying}
player?.play()
}
18 changes: 16 additions & 2 deletions Magic Drumpad/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ class ViewController: NSViewController {
var fingerViews: Set<NSBox>!
var visibleFingers = [Int: NSBox]()

@IBOutlet weak var region1: NSBox!
@IBOutlet weak var region2: NSBox!
@IBOutlet weak var region3: NSBox!

override func viewDidLoad() {
super.viewDidLoad()
fingerViews = [fingerView1, fingerView2, fingerView3, fingerView4, fingerView5]
Expand All @@ -37,12 +41,22 @@ class ViewController: NSViewController {

override func touchesBegan(with event: NSEvent) {
for touch in event.touches(matching: .began, in: nil) {
let x = (view.frame.width - fingerView1.frame.width) * touch.normalizedPosition.x
let y = (view.frame.height - fingerView1.frame.height) * touch.normalizedPosition.y
if region1.hitTest(NSPoint(x: x, y: y)) != nil {
playDrum1()
} else if region2.hitTest(NSPoint(x: x, y: y)) != nil {
playDrum2()
} else if region3.hitTest(NSPoint(x: x, y: y)) != nil {
playDrum3()
}

if let fingerView = fingerViews.subtracting(visibleFingers.values).first {
visibleFingers[touch.identity.hash] = fingerView
fingerView.isTransparent = false

fingerView.frame.origin.x = (view.frame.width - fingerView.frame.width) * touch.normalizedPosition.x
fingerView.frame.origin.y = (view.frame.height - fingerView.frame.height) * touch.normalizedPosition.y
fingerView.frame.origin.x = x
fingerView.frame.origin.y = y
}
}
}
Expand Down

0 comments on commit 09034db

Please sign in to comment.