forked from whichlight/mobile-synth-test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
130 lines (93 loc) · 3.2 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
console.log('test');
var context;
try{
window.AudioContext = window.AudioContext||window.webkitAudioContext;
context = new AudioContext();
}
catch (err){
alert('web audio not supported');
}
if (window.DeviceMotionEvent) {
console.log("DeviceMotionEvent supported");
}
if (window.DeviceMotionEvent) {
window.addEventListener('devicemotion', deviceMotionHandler, false);
} else {
document.getElementById("dmEvent").innerHTML = "Not supported."
}
if (window.DeviceOrientationEvent) {
// Listen for the event and handle DeviceOrientationEvent object
window.addEventListener('deviceorientation', devOrientHandler, false);
}
function map_range(value, low1, high1, low2, high2) {
return low2 + (high2 - low2) * (value - low1) / (high1 - low1);
}
var accelControl;
oscillator = context.createOscillator(); // Create sound source
oscillator.type = 2; // Square wave
oscillator.frequency.value = 400
var filter = context.createBiquadFilter();
gainNode = context.createGainNode();
gainNode.gain.value = 0;
filter.type = 0;
filter.frequency.value = 440;
oscillator.connect(filter); // Connect sound to output
filter.connect(gainNode);
gainNode.connect(context.destination);
oscillator.active = false;
$fun = $("#fun");
var playSound = function(e){
e.preventDefault();
console.log(e);
if(!oscillator.active){
oscillator.noteOn(0); // Play instantly
}
oscillator.active = true;
gainNode.gain.value = 0.5;
$fun.css("background-color","lime");
return false;
}
var stopSound = function(e){
e.preventDefault();
gainNode.gain.value = 0;
$fun.css("background-color","white");
return false;
}
$fun.bind("mousedown", playSound);
$fun.bind("mouseup",stopSound);
$fun.bind("touchstart", playSound);
$fun.bind("touchend", stopSound);
function deviceMotionHandler(eventData) {
var info, xyz = "[X, Y, Z]";
// Grab the acceleration from the results
var acceleration = eventData.acceleration;
info = xyz.replace("X", acceleration.x);
info = info.replace("Y", acceleration.y);
info = info.replace("Z", acceleration.z);
// Grab the acceleration including gravity from the results
acceleration = eventData.accelerationIncludingGravity;
info = xyz.replace("X", acceleration.x);
info = info.replace("Y", acceleration.y);
info = info.replace("Z", acceleration.z);
// Grab the rotation rate from the results
var rotation = eventData.rotationRate;
info = xyz.replace("X", rotation.alpha);
info = info.replace("Y", rotation.beta);
info = info.replace("Z", rotation.gamma);
// // Grab the refresh interval from the results
info = eventData.interval;
var accelControl = acceleration.x;
oscillator.frequency.value = 200+ accelControl*50;
}
function devOrientHandler(eventData) {
// gamma is the left-to-right tilt in degrees, where right is positive
var tiltLR = eventData.gamma;
// beta is the front-to-back tilt in degrees, where front is positive
var tiltFB = eventData.beta;
var filterval = map_range(tiltFB, -90, 90, 10000, 0);
filter.frequency.value = filterval;
// alpha is the compass direction the device is facing in degrees
var dir = eventData.alpha;
// call our orientation event handler
deviceOrientationHandler(tiltLR, tiltFB, dir);
}