Skip to content

Commit

Permalink
implement basic ChaosHarmonizer
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Guo committed May 18, 2015
1 parent 1eb7c31 commit 640ad7f
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="6198" systemVersion="14A297b" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" initialViewController="B8D-0N-5wS">
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="7702" systemVersion="14D136" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" initialViewController="B8D-0N-5wS">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="6198"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="7702"/>
</dependencies>
<scenes>
<!--Application-->
Expand Down Expand Up @@ -148,8 +148,7 @@
<action selector="paste:" target="Ady-hI-5gd" id="UvS-8e-Qdg"/>
</connections>
</menuItem>
<menuItem title="Paste and Match Style" keyEquivalent="V" id="WeT-3V-zwk">
<modifierMask key="keyEquivalentModifierMask" option="YES" command="YES"/>
<menuItem title="Paste and Match Style" keyEquivalent="v" id="WeT-3V-zwk">
<connections>
<action selector="pasteAsPlainText:" target="Ady-hI-5gd" id="cEh-KX-wJQ"/>
</connections>
Expand Down Expand Up @@ -641,16 +640,16 @@
<outlet property="delegate" destination="Voe-Tx-rLC" id="PrD-fu-P6m"/>
</connections>
</application>
<customObject id="Voe-Tx-rLC" customClass="AppDelegate" customModuleProvider="target"/>
<customObject id="Voe-Tx-rLC" customClass="AppDelegate" customModule="ChaosHarmonizer" customModuleProvider="target"/>
<customObject id="Ady-hI-5gd" userLabel="First Responder" customClass="NSResponder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="75" y="0.0"/>
</scene>
<!--Window Controller - Window-->
<!--Window Controller-->
<scene sceneID="R2V-B0-nI4">
<objects>
<windowController id="B8D-0N-5wS" sceneMemberID="viewController">
<window key="window" title="Window" allowsToolTipsWhenApplicationIsInactive="NO" autorecalculatesKeyViewLoop="NO" oneShot="NO" releasedWhenClosed="NO" showsToolbarButton="NO" visibleAtLaunch="NO" animationBehavior="default" id="IQv-IB-iLA">
<window key="window" title="ChaosHarmonizer" allowsToolTipsWhenApplicationIsInactive="NO" autorecalculatesKeyViewLoop="NO" oneShot="NO" releasedWhenClosed="NO" showsToolbarButton="NO" visibleAtLaunch="NO" animationBehavior="default" id="IQv-IB-iLA">
<windowStyleMask key="styleMask" titled="YES" closable="YES" miniaturizable="YES" resizable="YES"/>
<windowPositionMask key="initialPositionMask" leftStrut="YES" rightStrut="YES" topStrut="YES" bottomStrut="YES"/>
<rect key="contentRect" x="196" y="240" width="480" height="270"/>
Expand All @@ -667,7 +666,7 @@
<!--View Controller-->
<scene sceneID="hIz-AP-VOD">
<objects>
<viewController id="XfG-lQ-9wD" customClass="ViewController" customModuleProvider="target" sceneMemberID="viewController">
<viewController id="XfG-lQ-9wD" customClass="ViewController" customModule="ChaosHarmonizer" customModuleProvider="target" sceneMemberID="viewController">
<view key="view" id="m2S-Jp-Qdl">
<rect key="frame" x="0.0" y="0.0" width="480" height="270"/>
<autoresizingMask key="autoresizingMask"/>
Expand Down
44 changes: 36 additions & 8 deletions Examples/ChaosHarmonizer/ChaosHarmonizer/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,47 @@ import MusicKit

class ViewController: NSViewController {

var noteNumberToPitchSet = [UInt : PitchSet]()

override func viewDidLoad() {
super.viewDidLoad()

let midi = MIDI(name: "ChaosHarmonizer")
midi.noteHandler = { messages in
if let first = messages.first {
let possibleChords = ChordQuality.Hexads
let index = arc4random_uniform(UInt32(possibleChords.count))
let quality = possibleChords[Int(index)]
let intervals = quality.intervals
let indices = MKUtil.semitoneIndices(intervals)
let semitones = indices[indices.count - 1]
let harmonizer = Harmony.transpose(Harmony.create(intervals), semitones: semitones)
midi.send([first, first.transpose(2), first.transpose(3)])
if first.on {
let possibleChords = [
ChordQuality.Major,
ChordQuality.Sus2,
ChordQuality.Sus4,
ChordQuality.Minor,
]
let index = arc4random_uniform(UInt32(possibleChords.count))
let quality = possibleChords[Int(index)]
let intervals = quality.intervals
let indices = MKUtil.semitoneIndices(intervals)
let transposition = indices[indices.count - 1]*(-2)
let harmonizer = Harmony.transpose(Harmony.create(intervals), semitones: transposition)
let pitch = Pitch(midi: Float(first.noteNumber))
var pitchSet = harmonizer(pitch)
self.noteNumberToPitchSet[first.noteNumber] = pitchSet
var messages = first.harmonize(harmonizer)
messages.append(first)
midi.send(messages)
}
else {
var offMessages = [MIDINoteMessage]()
if let pitchSet = self.noteNumberToPitchSet[first.noteNumber] {
for pitch in pitchSet {
let message = MIDINoteMessage(on: false,
channel: first.channel,
noteNumber: UInt(pitch.midi), velocity: 0)
offMessages.append(message)
offMessages.append(first)
}
midi.send(offMessages)
}
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion MusicKit/MIDI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class MIDI {
/// Note that messages are always sent on `sourceChannel`.
///
/// :returns: `true` if the message was successfully sent
public func send(messages: [MIDIMessage]) -> Bool {
public func send<T: MIDIMessage>(messages: [T]) -> Bool {
var success = false
var packet = UnsafeMutablePointer<MIDIPacket>.alloc(sizeof(MIDIPacket))
var packetList = UnsafeMutablePointer<MIDIPacketList>.alloc(sizeof(MIDIPacketList))
Expand Down
17 changes: 17 additions & 0 deletions MusicKit/MIDIMessage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,23 @@ public struct MIDINoteMessage : MIDIMessage {
public func copyOnChannel(channel: UInt) -> MIDINoteMessage {
return MIDINoteMessage(on: on, channel: channel, noteNumber: noteNumber, velocity: velocity)
}

/// The message's pitch
public var pitch : Pitch {
return Pitch(midi: Float(noteNumber))
}

/// Applies the given `Harmonizer` to the message.
public func harmonize(harmonizer: Harmonizer) -> [MIDINoteMessage] {
let ps = harmonizer(pitch)
var messages = [MIDINoteMessage]()
// TODO: add a PitchSet -> Array method so this can be mapped
for p in ps {
messages.append(MIDINoteMessage(on: self.on, channel: self.channel,
noteNumber: UInt(p.midi), velocity: self.velocity))
}
return messages
}
}

extension MIDINoteMessage : Printable {
Expand Down

0 comments on commit 640ad7f

Please sign in to comment.