-
Notifications
You must be signed in to change notification settings - Fork 0
/
play.html
168 lines (143 loc) · 3.86 KB
/
play.html
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<!--
<script src="http://blog.chrislowis.co.uk/js/AudioContextMonkeyPatch.js"></script>
<script src="http://blog.chrislowis.co.uk/js/raphael-min.js"></script>
<script src="http://blog.chrislowis.co.uk/js/qwerty-hancock.js"></script>
-->
<center>
<div id="keyboard"></div>
<table cellspacing="15">
<tr>
<td>4 = root down</td>
<td>6 = root up</td>
</tr>
<tr>
<td>7 = 3rd down</td>
<td>8 = 3rd up</td>
</tr>
<tr>
<td>9 = 5th down</td>
<td>0 = 5th up</td>
</tr>
<tr>
<td>F1 = Equal Tempered</td>
<td>F2 = Well Tempered</td>
</tr>
<tr>
<td>F3 = Perfect Tempered</td>
<td>F4 = Just Tempered</td>
</tr>
</table>
</center>
<script src="https://stuartmemo.com/qwerty-hancock/qwerty-hancock.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script>
console.log('starting...');
var context,
settings = {
id: 'keyboard',
width: 600,
height: 150,
startNote: 'A4',
whiteNotesColour: 'white',
blackNotesColour: 'black',
hoverColour: 'yellow',
octaves: 2
},
keyboard = new QwertyHancock(settings);
context = new AudioContext();
masterGain = context.createGain(),
nodes = [];
masterGain.gain.value = 0.7; //volume
masterGain.connect(context.destination);
var notes = {
/*
440: {
oscillator: {},
gain: {},
}
*/
};
var activeNotes = {};
// 3 deltas for the test: root, middle, last
var originalFrequencies = [0, 0, 0];
keyboard.keyDown = function (note, frequency) {
if(!notes[frequency]){
var oscillator = context.createOscillator();
oscillator.type = 'sine';
oscillator.frequency.value = frequency;
var noteGain = context.createGain();
noteGain.gain.value = 0.7;
noteGain.connect(context.destination);
oscillator.connect(noteGain);
oscillator.start(0);
//add to notes
notes[frequency] = {
oscillator: oscillator,
gain: noteGain
}
}
else {
var oscillator = notes[frequency].oscillator;
notes[frequency].gain.gain.value = .7;
}
if(!notes[frequency].originalFrequency) notes[frequency].originalFrequency = frequency;
activeNotes[frequency] = notes[frequency];
nodes.push(oscillator);
};
keyboard.keyUp = function (note, frequency) {
notes[frequency].gain.gain.value = 0;
delete activeNotes[frequency];
//nodes[i].stop(0);
//nodes[i].disconnect();
};
// listen to keyboard presses
// keys:
// 9 0 -> root note down up
// - = -> middle note down up
// [ ] -> last note down up
// backspace -> reset all deltas to 0
var increment = 1; //hz
var deltas = [0, 0, 0];
var windowListener = window.addEventListener('keypress', function(evt) {
var code = evt.charCode || evt.keyCode;
//console.log( code );
switch(code) {
case 32:
deltas = [0, 0, 0];
evt.preventDefault();
break;
case 52:
//console.log('1st down');
deltas[0] -= increment;
break;
case 54:
//console.log('1st up');
deltas[0] += increment;
break;
case 55:
//console.log('2nd down');
deltas[1] -= increment;
break;
case 56:
//console.log('2nd up');
deltas[1] += increment;
break;
case 57:
console.log('3rd down');
deltas[2] -= increment;
break;
case 48:
//console.log('3rd up');
deltas[2] += increment;
break;
}
//console.log(deltas, activeNotes);
var count = 0;
for(var i in activeNotes) {
var activeNote = activeNotes[i];
console.log(activeNote.oscillator.frequency.value, count, deltas[count]);
activeNote.oscillator.frequency.value = activeNote.originalFrequency + deltas[count];
++ count;
}
});
</script>