1
1
var state = 0 ,
2
2
ws ,
3
3
context ,
4
- oscillator ;
4
+ oscillator ,
5
+ gainer ;
5
6
6
7
// Support both the WebSocket and MozWebSocket objects
7
8
if ( ( typeof ( WebSocket ) == 'undefined' ) &&
@@ -24,15 +25,24 @@ function init() {
24
25
var obj = JSON . parse ( event . data ) ;
25
26
if ( state > 0 ) {
26
27
if ( obj . pointables . length > 0 ) {
27
- var value = obj . pointables [ 0 ] . tipPosition [ 1 ] ;
28
+ var value = obj . pointables [ 0 ] . tipPosition [ 0 ] ;
28
29
var intensity = $ ( '#intensity' ) . val ( ) ;
29
- var modified = ( 575 - value ) * intensity ;
30
+ var modified = ( 350 - value ) * intensity ;
30
31
if ( modified <= 0 ) {
31
32
modified = 5 ;
32
33
}
33
34
modified = modified . toFixed ( 2 ) ;
34
35
36
+ var gainval = ( obj . pointables [ 0 ] . tipPosition [ 1 ] - 25 ) / 600 ;
37
+ if ( gainval < 0 ) {
38
+ gainval = 0 ;
39
+ }
40
+ if ( gainval > 1 ) {
41
+ gainval = 1 ;
42
+ }
43
+
35
44
oscillator . frequency . value = modified ;
45
+ gainer . gain . value = gainval ;
36
46
$ ( '#frequency' ) . html ( modified + ' Hz' ) ;
37
47
}
38
48
}
@@ -67,8 +77,10 @@ $(function() {
67
77
state = 1 ;
68
78
context = new webkitAudioContext ( ) ,
69
79
oscillator = context . createOscillator ( )
70
- oscillator . connect ( context . destination ) ;
71
- oscillator . frequency . value = 900 ;
80
+ gainer = context . createGain ( ) ;
81
+ gainer . connect ( context . destination ) ;
82
+ oscillator . connect ( gainer ) ;
83
+ oscillator . frequency . value = 901 ;
72
84
oscillator . start ( 0 ) ;
73
85
74
86
$ ( this ) . addClass ( 'active' ) ;
0 commit comments