Skip to content

Commit 59b6ac4

Browse files
authored
Merge pull request #5 from ianmunrobot/refactor-view-memory
Refactor view memory
2 parents 892e85f + eb0b543 commit 59b6ac4

File tree

9 files changed

+183
-94
lines changed

9 files changed

+183
-94
lines changed

app/index.js

Lines changed: 38 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,61 @@
11
const Tone = require('tone')
22

33
const socket = require('./socket')
4+
45
const spaceCircle = require('./views/spaceCircle')
56
const coolBlobs = require('./views/coolBlobs')
67

7-
StartAudioContext(Tone.context, '#test').then(function(){
8+
const start = require('./toneCenter').start
9+
const duoSynth = require('./instruments/duoSynth');
810

9-
})
11+
const views = {
12+
spaceCircle,
13+
coolBlobs
14+
}
15+
16+
// main synth memory
17+
const synthesizers = {};
1018

19+
// start with audio defaults
20+
start();
21+
duoSynth(synthesizers)
1122
const drums = require('./basic-beat')
1223

24+
// menu click handler
25+
$('a').on('click', function(e) {
26+
// update menu
27+
$('li').removeClass('active')
28+
$(this).parent().addClass('active')
29+
e.preventDefault()
30+
// reset socket listeners
31+
socket.removeAllListeners('serverDown');
32+
socket.removeAllListeners('serverDrag');
33+
socket.removeAllListeners('serverUp');
34+
35+
// reset and repopulate view
36+
project.clear();
37+
views[this.id]();
38+
39+
// restart synth
40+
duoSynth(synthesizers);
41+
})
1342

43+
// set up canvas
1444
const canvas = document.getElementById('paperCanvas');
1545
paper.setup(canvas);
1646
paper.install(window);
1747

18-
// const drums = require('./basic-beat')
19-
let room = window.location.pathname.slice(1);
20-
21-
// synthesizer memory
22-
let synthesizers = {}
23-
24-
//create a synth and connect it to the master output (your speakers)
25-
var reverb = new Tone.JCReverb(0.25).connect(Tone.Master)
26-
27-
// helper function - change synth frequency
28-
function changeFrequency(id, direction, frequency) {
29-
synthesizers[id][direction].frequency.value = frequency
30-
}
31-
32-
function changeAmplitude(id, amplitude) {
33-
// var newAmp = amplitude * -3
34-
// console.log('newAmp:', newAmp);
35-
// synthesizers[id].x.volume.value = newAmp;
36-
// synthesizers[id].y.volume.value = newAmp;
37-
}
38-
39-
function attack(id) {
40-
synthesizers[id].x.triggerAttack(synthesizers[id].x.frequency.value)
41-
synthesizers[id].y.triggerAttack(synthesizers[id].x.frequency.value * 1.5)
42-
}
48+
// begin view default
49+
spaceCircle();
4350

44-
function release(id) {
45-
synthesizers[id].x.triggerRelease()
46-
synthesizers[id].y.triggerRelease()
47-
}
51+
let room = window.location.pathname.slice(1);
4852

4953
// on connection, emit event to join a specific room
5054
socket.on('connect', () => {
51-
socket.emit('create', room);
55+
socket.emit('create', room);
5256
});
5357

54-
socket.on('populateSynths', (ids) => {
55-
ids.forEach(id => {
56-
let newXSynth1 = new Tone.DuoSynth({harmonicity: 1.5}).chain(reverb)
57-
let newYSynth2 = new Tone.DuoSynth({harmonicity: 1.5}).chain(reverb)
58-
newXSynth1.volume.value = -15;
59-
newYSynth2.volume.value = -15;
60-
synthesizers[id] = {
61-
x: newXSynth1,
62-
y: newYSynth2
63-
}
64-
})
65-
})
66-
67-
socket.on('mouseDown', (event) => {
68-
changeFrequency(event.id, 'x', event.x)
69-
changeFrequency(event.id, 'y', event.x * 1.5)
70-
attack(event.id)
71-
})
72-
73-
socket.on('mouseDrag', (event) => {
74-
// console.log(event);
75-
let amplitude = Math.abs(event.delta[1]) + Math.abs(event.delta[2])
76-
changeAmplitude(event.id, amplitude)
77-
changeFrequency(event.id, 'x', event.x)
78-
changeFrequency(event.id, 'y', event.x * 1.5)
79-
})
80-
81-
socket.on('mouseUp', (event) => {
82-
release(event.id)
83-
})
84-
85-
// drawing tool
86-
58+
// drawing tool emitters
8759
var tool = new Tool()
8860
tool.minDistance = 10;
8961
tool.maxDistance = 30;
@@ -115,6 +87,4 @@ tool.onMouseUp =(event) => {
11587
y: event.point.y,
11688
}
11789
socket.emit('mouseUp', outEvent)
118-
}
119-
120-
coolBlobs();
90+
}

app/instruments/duoSynth.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
const Tone = require('tone')
2+
3+
const reverb = require('../toneCenter').reverb
4+
// const synthesizers = require('../toneCenter')
5+
// console.log('in duo', synthesizers);
6+
7+
const socket = require('../socket')
8+
9+
module.exports = function(synthesizers) {
10+
11+
// helper function - change synth frequency
12+
function changeFrequency(id, direction, frequency) {
13+
synthesizers[id][direction].frequency.value = frequency
14+
}
15+
16+
function changeAmplitude(id, amplitude) {
17+
// var newAmp = amplitude * -3
18+
// console.log('newAmp:', newAmp);
19+
// synthesizers[id].x.volume.value = newAmp;
20+
// synthesizers[id].y.volume.value = newAmp;
21+
}
22+
23+
function attack(id) {
24+
synthesizers[id].x.triggerAttack(synthesizers[id].x.frequency.value)
25+
synthesizers[id].y.triggerAttack(synthesizers[id].x.frequency.value * 1.5)
26+
}
27+
28+
function release(id) {
29+
synthesizers[id].x.triggerRelease()
30+
synthesizers[id].y.triggerRelease()
31+
}
32+
33+
socket.on('populateSynths', (ids) => {
34+
ids.forEach(id => {
35+
let newXSynth1 = new Tone.DuoSynth({harmonicity: 1.5}).chain(reverb)
36+
let newYSynth2 = new Tone.DuoSynth({harmonicity: 1.5}).chain(reverb)
37+
newXSynth1.volume.value = -15;
38+
newYSynth2.volume.value = -15;
39+
synthesizers[id] = {
40+
x: newXSynth1,
41+
y: newYSynth2,
42+
random: Math.floor(Math.random() * 100)
43+
}
44+
})
45+
})
46+
47+
socket.on('serverDown', (event) => {
48+
changeFrequency(event.id, 'x', event.x)
49+
changeFrequency(event.id, 'y', event.x * 1.5)
50+
attack(event.id)
51+
})
52+
53+
socket.on('serverDrag', (event) => {
54+
let amplitude = Math.abs(event.delta[1]) + Math.abs(event.delta[2])
55+
changeAmplitude(event.id, amplitude)
56+
changeFrequency(event.id, 'x', event.x)
57+
changeFrequency(event.id, 'y', event.x * 1.5)
58+
})
59+
60+
socket.on('serverUp', (event) => {
61+
release(event.id)
62+
})
63+
64+
}

app/toneCenter.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const Tone = require('tone')
2+
3+
// const synthesizers = {};
4+
5+
//create a main reverb element
6+
const reverb = new Tone.JCReverb(0.25).connect(Tone.Master);
7+
8+
var start = function () {
9+
StartAudioContext(Tone.context, '#test').then(function(){
10+
11+
})
12+
}
13+
14+
module.exports = {
15+
start,
16+
reverb,
17+
}

app/views/coolBlobs.js

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
const socket = require('../socket')
2+
const paths = require('./index')
23

34
module.exports = function() {
4-
var paths = {}
5+
56
var count = 20
67
var radiusSize = 100;
78
var shadowBlur = 100;
@@ -65,56 +66,58 @@ module.exports = function() {
6566
}
6667

6768
view.onFrame = (event) => {
68-
for (var i = 0; i < count; i++) {
69-
var item = project.activeLayer.children[i];
70-
item.position.y = item.position.y + item.speed
71-
if (item.position.y > view.size.height + 60 || item.position.y < -60) item.speed *= -1
72-
}
69+
for (var i = 0; i < count; i++) {
70+
var item = project.activeLayer.children[i];
71+
item.position.y = item.position.y + item.speed
72+
if (item.position.y > view.size.height + 60 || item.position.y < -60) item.speed *= -1
73+
}
7374
}
7475

7576
// Space Circle Path:
7677

77-
socket.on('mouseDown', function(event) {
78+
socket.on('serverDown', function(event) {
7879
var circlePath = new Path.Circle({
7980
center: [event.x, event.y],
8081
radius: 20,
8182
fillColor: 'cyan',
8283
});
8384
// create memory queue
8485
if (!paths[event.id]) {
85-
paths[event.id] = [];
86+
paths[event.id] = {
87+
past: []
88+
};
8689
}
87-
paths[event.id].push(circlePath)
90+
paths[event.id].past.push(circlePath)
8891
window.setTimeout(function() {
8992
itemTimeout(circlePath, event.id);
9093
}, 300);
9194
})
9295

93-
socket.on('mouseDrag', function(event) {
94-
var last = paths[event.id][paths[event.id].length - 1]
96+
socket.on('serverDrag', function(event) {
97+
var last = paths[event.id].past[paths[event.id].past.length - 1]
9598
var circlePath = new Path.Circle({
9699
center: [event.x, event.y],
97100
radius: 20 + event.delta[1] + event.delta[2],
98101
fillColor: last.fillColor,
99102
});
100103
circlePath.fillColor.hue += Math.floor(Math.random() * 4)
101-
paths[event.id].push(circlePath)
104+
paths[event.id].past.push(circlePath)
102105
window.setTimeout(function() {
103106
itemTimeout(circlePath, event.id)
104107
}, 300)
105108
});
106109

107-
socket.on('mouseUp', function() {
110+
socket.on('serverUp', function() {
108111

109112
})
110113

111114
var itemTimeout = function(pathToRemove, id) {
112115
var alpha = 1.0
113116
var fadeOut = window.setInterval(function() {
114-
if (alpha === 0) {
115-
window.clearInterval(fadeOut)
117+
if (alpha <= 0) {
116118
pathToRemove.remove()
117-
paths[id].unshift()
119+
paths[id].past.unshift()
120+
window.clearInterval(fadeOut)
118121
} else {
119122
pathToRemove.fillColor.alpha -= 0.1
120123
alpha -= 0.1

app/views/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// main paths memory
2+
var paths = {}
3+
4+
module.exports = paths;

app/views/spaceCircle.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const socket = require('../socket')
2+
const paths = require('./index')
23

34
module.exports = function() {
4-
var paths = {}
55
// The amount of circles we want to make:
66
var count = 100;
77

@@ -59,7 +59,7 @@ module.exports = function() {
5959

6060

6161
// create drawing events
62-
socket.on('mouseDown', function(event) {
62+
socket.on('serverDown', function(event) {
6363
var shadow = new Path();
6464
var path = new Path();
6565
path.fillColor = {
@@ -85,7 +85,7 @@ module.exports = function() {
8585
};
8686
})
8787

88-
socket.on('mouseDrag', function(event) {
88+
socket.on('serverDrag', function(event) {
8989
var middlePoint = new Point(event.middlePoint[1], event.middlePoint[2])
9090
var step = new Point(event.delta[1] / 2, event.delta[2] / 2)
9191
step.angle += 90;
@@ -109,7 +109,7 @@ module.exports = function() {
109109

110110
})
111111

112-
socket.on('mouseUp', function(event) {
112+
socket.on('serverUp', function(event) {
113113
var current = paths[event.id].currentPath.path;
114114
var shadow = paths[event.id].currentPath.shadow;
115115
current.add(new Point(event.x, event.y));
@@ -139,6 +139,7 @@ module.exports = function() {
139139
paths[id].past.unshift()
140140
} else {
141141
pathToRemove.fillColor.alpha -= 0.05
142+
alpha -= 0.05
142143
}
143144
}, 50)
144145
}

index.html

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,38 @@
66
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
77
<meta name="description" content="">
88
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
9+
<link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.min.css">
910
<link rel="stylesheet" type="text/css" href="css/style.css">
11+
<script type="text/javascript" src="jquery/jquery.min.js"></script>
1012
<script type="text/javascript" src="socket/socket.io.js"></script>
13+
<script type="text/javascript" src="bootstrap/js/bootstrap.min.js"></script>
1114
<script type="text/javascript" src="paper/paper-full.js"></script>
1215
<script type="text/javascript" src="startaudiocontext/StartAudioContext.js"></script>
1316
</head>
1417
<body>
1518
<div id="test">
1619
<canvas id="paperCanvas" resize="true"></canvas>
1720
</div>
21+
<nav class="navbar navbar-inverse">
22+
<div class="container-fluid">
23+
<!--<div class="navbar-header">
24+
<a class="navbar-brand" href="#">WebSiteName</a>
25+
</div>-->
26+
<ul class="nav navbar-nav">
27+
<!--<li class="dropdown">
28+
<a class="dropdown-toggle" data-toggle="dropdown" href="#">Page 1
29+
<span class="caret"></span></a>
30+
<ul class="dropdown-menu">
31+
<li><a href="#">Page 1-1</a></li>
32+
<li><a href="#">Page 1-2</a></li>
33+
<li><a href="#">Page 1-3</a></li>
34+
</ul>
35+
</li>-->
36+
<li class="active"><a id="spaceCircle" href="#">circles</a></li>
37+
<li><a id="coolBlobs" href="#">blobs</a></li>
38+
</ul>
39+
</div>
40+
</nav>
1841
<script type="text/javascript" src="js/bundle.js" charset="utf-8"></script>
1942
</body>
2043
</html>

0 commit comments

Comments
 (0)