summaryrefslogtreecommitdiff
path: root/client
diff options
context:
space:
mode:
authorJules Laplace <julescarbon@gmail.com>2018-03-23 01:58:16 +0100
committerJules Laplace <julescarbon@gmail.com>2018-03-23 01:58:16 +0100
commit1926101e77699bd2b5bd5e8c500bae1019ba8655 (patch)
treed9f593a1b931c12a2756ef897c61224c812b0287 /client
parent8dab62fdfafb24e792bee09a04b5592704f33e92 (diff)
arrow keys move pattern around
Diffstat (limited to 'client')
-rw-r--r--client/index.js59
-rw-r--r--client/lib/organ.js12
2 files changed, 49 insertions, 22 deletions
diff --git a/client/index.js b/client/index.js
index d6dad6a..edc5281 100644
--- a/client/index.js
+++ b/client/index.js
@@ -25,7 +25,7 @@ let notes = []
requestAudioContext( () => {
for (var i = 0; i < ws; i++) {
- notes[i] = []
+ notes[i] = []
for (var j = 0; j < hs; j++) {
notes[i][j] = add(i, j)
}
@@ -34,6 +34,7 @@ requestAudioContext( () => {
})
function play(freq) {
+ if (freq.playing) return
freq.playing = true
instrument.play(freq.frequency)
if (instrument === organ || hash || life.isRunning()) {
@@ -42,6 +43,7 @@ function play(freq) {
life.assign_item(freq, true)
}
function pause(freq) {
+ if (!freq.playing) return
freq.playing = false
instrument.pause(freq.frequency)
freq.div.classList.remove('playing')
@@ -55,7 +57,7 @@ function assign(freq, state) {
}
}
function toggle(freq) {
- assign(freq, freq.playing = !freq.playing)
+ assign(freq, !freq.playing)
}
const gliderShape = [
[0,0,0,0,0],
@@ -74,7 +76,6 @@ const gliderShapes = [
function glider() {
const x = Math.floor(Math.random() * ws)
const y = Math.floor(Math.random() * hs)
- console.log("glider at", x, y)
const shape = choice(gliderShapes)
weave(x,y,shape)
}
@@ -95,44 +96,58 @@ function forEach(f){
for (i = 0; i < ws; i++) {
for (j = 0; j < hs; j++) {
note = notes[i][j]
- s = f(i,j,note.playing)
+ s = f(i, j, note.playing)
assign(note, s)
}
}
}
-function clear(){
+function clone(){
+ let i, j;
+ let a = []
+ for (i = 0; i < ws; i++) {
+ a[i] = []
+ for (j = 0; j < hs; j++) {
+ a[i][j] = notes[i][j].playing
+ }
+ }
+ return a
+}
+function move(dx,dy){
+ let a = clone()
forEach((x,y,state) => {
- return false
+ x = (x+dx+ws)%ws
+ y = (y+dy+hs)%hs
+ return a[x][y]
})
}
-function white(){
- forEach((x,y,state) => {
+function clear(){
+ forEach(() => {
return false
})
}
function stripex(odd){
odd = !! odd
- forEach((x,y,state) => {
+ forEach((x) => {
return x % 2 ? odd : !odd
})
}
function stripey(odd){
odd = !! odd
- forEach((x,y,state) => {
+ forEach((x,y) => {
return y % 2 ? odd : !odd
})
}
function checker(odd, n){
odd = !! odd
n = n || 1
- forEach((x,y,state) => {
+ forEach((x,y) => {
return ((Math.floor(x/n)%2) ^ (Math.floor(y/n)%2)) ? odd : !odd
})
}
function noise(n){
n = n || 0.5
n = n * n
- forEach((x,y,state) => {
+ forEach(() => {
return Math.random() < n
})
}
@@ -229,21 +244,33 @@ function swap_instrument(){
let life_bpm = 50
window.addEventListener("keydown", keydown, true)
function keydown(e){
- console.log(e.keyCode)
+ // console.log(e.keyCode)
if (e.altKey || e.ctrlKey || e.metaKey) return
switch (e.keyCode){
case 32: // space
life.toggle()
break
- case 38: // up
+ case 188: // comma
life_bpm += e.shiftKey ? 1 : 5
life.setTempo(life_bpm)
break
- case 40: // down
+ case 190: // period
life_bpm -= e.shiftKey ? 1 : 5
life_bpm = Math.max(1, life_bpm)
life.setTempo(life_bpm)
break
+ case 37: // left
+ move(1, 0)
+ break
+ case 38: // up
+ move(0, 1)
+ break
+ case 39: // right
+ move(-1, 0)
+ break
+ case 40: // down
+ move(0, -1)
+ break
case 71: // g
glider()
break
@@ -254,7 +281,7 @@ function keydown(e){
clear()
break
case 87: // w
- white()
+ clear()
break
case 78: // n
noise(0.5)
diff --git a/client/lib/organ.js b/client/lib/organ.js
index fe2315c..0048abf 100644
--- a/client/lib/organ.js
+++ b/client/lib/organ.js
@@ -1,24 +1,24 @@
import Tone from 'tone'
-import { choice } from './util'
import output from './output'
-const player_count = 4
-
const oscillators = {}
+let lastPlayed
function play (freq) {
const osc = oscillators[freq] = oscillators[freq] || {}
if (!osc.el) {
- osc.el = new Tone.Oscillator(freq , "sine")
+ osc.el = new Tone.Oscillator(freq, "sine")
osc.el.connect(output)
}
osc.el.start()
osc.playing = true
+ lastPlayed = osc
return osc
}
-function pause(freq) {
+function pause (freq) {
+ if (!oscillators[freq]) return
const osc = oscillators[freq] = oscillators[freq] || {}
- osc.el && osc.el.stop()
+ if (osc.el) osc.el.stop()
osc.playing = false
return osc
}