Skip to content
Snippets Groups Projects
Commit 58d14801 authored by Serene H's avatar Serene H
Browse files

output key names and reduce computations

parent 6da0a6e3
No related branches found
No related tags found
No related merge requests found
.DS_Store .DS_Store
*.pyc *.pyc
*.swp
*.swo
...@@ -12,23 +12,18 @@ ...@@ -12,23 +12,18 @@
# Left hand -> Low frequency oscillators (wobble) # Left hand -> Low frequency oscillators (wobble)
# Right hand -> Particles, shifting in color and pointiness # Right hand -> Particles, shifting in color and pointiness
# #
# 2014, Micah Elizabeth Scott & ~keroserene # 2014, Micah Elizabeth Scott & Keroserene
# #
flags = require 'flags'
flags.defineString 'layout', '../layouts/grid32x16z.json', 'LED board layout'
flags.defineInteger 'midi', 0, 'MIDI input port'
flags.parse()
# Prepare MIDI input. # Default MIDI input
# TODO: Intelligently find the right midi port instead of default to 0.
midi = require 'midi' midi = require 'midi'
input = new midi.input input = new midi.input
input.openPort flags.get('midi') input.openPort 1
input.ignoreTypes false, false, false input.ignoreTypes false, false, false
# Default OPC output # Default OPC output
OPC = new require './opc' OPC = new require './opc'
model = OPC.loadModel flags.get('layout') model = OPC.loadModel process.argv[2] || '../layouts/grid32x16z.json'
client = new OPC 'localhost', 7890 client = new OPC 'localhost', 7890
# Live particles # Live particles
...@@ -49,12 +44,15 @@ wobbleAmount = 24.0 ...@@ -49,12 +44,15 @@ wobbleAmount = 24.0
origin = [0, 0, 0] origin = [0, 0, 0]
# Physics # Physics
numPhysicsTimesteps = 1 # numPhysicsTimesteps = 20 TODO: Re-enable when things become more complex
frameDelay = 5 frameDelay = 5
timestepSize = 0.010 timestepSize = 0.010
gain = 0.1 gain = 0.1
previousNow = 0 # Derived values
particleDecay = timestepSize / particleLifetime
# Controlled by the Pitch Transpose Knob
spinAngle = 0 spinAngle = 0
# Time clock in seconds # Time clock in seconds
...@@ -66,20 +64,30 @@ midiToHz = (key) -> 440 * Math.pow 2, (key - 69) / 12 ...@@ -66,20 +64,30 @@ midiToHz = (key) -> 440 * Math.pow 2, (key - 69) / 12
# Midi note to angle, one rev per octave # Midi note to angle, one rev per octave
midiToAngle = (key) -> (2 * Math.PI / 24) * key midiToAngle = (key) -> (2 * Math.PI / 24) * key
# Musical Constants
# Boundary between the left-hand and right-hand patterns. # Boundary between the left-hand and right-hand patterns.
LIMINAL_KEY = 40 LIMINAL_KEY = 40
MAX_VELOCITY = 100
SHARP_NAMES = ['A', 'A#', 'B', 'C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#']
FLAT_NAMES = ['A', 'Bb', 'B', 'C', 'Db', 'D', 'Eb', 'E', 'F', 'Gb', 'G', 'Ab']
LOWEST_A = 1
HIGHEST_C = 124
getKeyName = (key) -> FLAT_NAMES[(key - LOWEST_A) % 12]
input.on 'message', (deltaTime, message) -> input.on 'message', (deltaTime, message) ->
console.log message logMsg = message
msgType = Math.floor parseInt(message[0]) / 16 # Examine the 0xF0 byte.
switch message[0] switch msgType
when 0x80 # Voice 0, note off when 0x8 # Voice 0, note off
key = message[1] key = message[1]
logMsg += ' : ' + getKeyName key
delete lfoNotes[key] delete lfoNotes[key]
delete particleNotes[key] delete particleNotes[key]
when 0x90 # Voice 0, note on when 0x9 # Voice 0, note on
key = message[1] key = message[1]
logMsg += ' : ' + getKeyName key
info = info =
key: key key: key
velocity: message[2] velocity: message[2]
...@@ -91,7 +99,7 @@ input.on 'message', (deltaTime, message) -> ...@@ -91,7 +99,7 @@ input.on 'message', (deltaTime, message) ->
else else
lfoNotes[key] = info lfoNotes[key] = info
when 0xb0 # Voice 0, Control Change when 0xb # Voice 0, Control Change
switch message[1] switch message[1]
when 7 # "Data entry" slider, brightness when 7 # "Data entry" slider, brightness
brightness = message[2] * 2.0 / 127 brightness = message[2] * 2.0 / 127
...@@ -99,12 +107,12 @@ input.on 'message', (deltaTime, message) -> ...@@ -99,12 +107,12 @@ input.on 'message', (deltaTime, message) ->
when 1 # "Modulation" slider, particle speed when 1 # "Modulation" slider, particle speed
particleLifetime = 0.1 + message[2] * 2.0 / 127 particleLifetime = 0.1 + message[2] * 2.0 / 127
when 0xe0 # Voice 0, Pitch Bend when 0xe # Voice 0, Pitch Bend
# Default spin 1.0, but allow forward/backward # Default spin 1.0, but allow forward/backward
spinRate = (message[2] - 64) * 10.0 / 64 spinRate = (message[2] - 64) * 10.0 / 64
console.log logMsg
draw = () ->
draw = ->
# Time delta calculations # Time delta calculations
now = clock() now = clock()
...@@ -141,12 +149,13 @@ draw = -> ...@@ -141,12 +149,13 @@ draw = ->
# Hop around between almost-opposing colors, eventually going # Hop around between almost-opposing colors, eventually going
# around the rainbow. These ratios control what kinds of color # around the rainbow. These ratios control what kinds of color
# schemes we get for different chords. # schemes we get for different chords. This operates by the circle of
# fifths.
hue = (p.note.key - LIMINAL_KEY + 0.1) * (7 / 12.0) hue = (p.note.key - LIMINAL_KEY + 0.1) * (7 / 12.0)
p.color = OPC.hsv hue, 0.5, 0.8 p.color = OPC.hsv hue, 0.5, 0.8
# Intensity mapped to velocity, nonlinear # Intensity mapped to velocity, nonlinear
p.intensity = Math.pow(p.note.velocity / 100, 2.0) * 0.2 * brightness p.intensity = Math.pow(p.note.velocity / MAX_VELOCITY, 2.0) * 0.2 * brightness
# Fade with age # Fade with age
noteAge = now - p.note.timestamp noteAge = now - p.note.timestamp
...@@ -162,7 +171,7 @@ draw = -> ...@@ -162,7 +171,7 @@ draw = ->
lfoAngle = midiToAngle key lfoAngle = midiToAngle key
# Amplitude starts with left hand velocity # Amplitude starts with left hand velocity
wobbleAmp = Math.pow(note.velocity / 100, 2.0) * wobbleAmount wobbleAmp = Math.pow(note.velocity / MAX_VELOCITY, 2.0) * wobbleAmount
# Scale based on particle fuzziness # Scale based on particle fuzziness
wobbleAmp /= p.falloff wobbleAmp /= p.falloff
...@@ -178,16 +187,16 @@ draw = -> ...@@ -178,16 +187,16 @@ draw = ->
y += wobbleAmp * Math.sin lfoAngle y += wobbleAmp * Math.sin lfoAngle
# Update velocity; use the XZ plane # Update velocity; use the XZ plane
p.velocity[0] += (x - p.point[0]) * (gain / numPhysicsTimesteps) p.velocity[0] += (x - p.point[0]) * (gain) # / numPhysicsTimesteps)
p.velocity[2] += (y - p.point[2]) * (gain / numPhysicsTimesteps) p.velocity[2] += (y - p.point[2]) * (gain) # / numPhysicsTimesteps)
# Fixed timestep physics # Fixed timestep physics
for i in [1 .. numPhysicsTimesteps] # TODO: Re-enable this when it's not just re-adding the delta.
p.point[0] += p.velocity[0] # for i in [1 .. numPhysicsTimesteps]
p.point[1] += p.velocity[1] p.point[0] += p.velocity[0]
p.point[2] += p.velocity[2] p.point[1] += p.velocity[1]
p.point[2] += p.velocity[2]
p.life -= timestepSize / particleLifetime p.life -= particleDecay
# Filter out dead particles # Filter out dead particles
particles = particles.filter (p) -> p.life > 0 particles = particles.filter (p) -> p.life > 0
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment