-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
208 lines (185 loc) · 6.82 KB
/
index.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Hexachord</title>
</head>
<body style="background-color: steelblue; font-family: arial;">
<div id="synth" style="display: flex; background-color: paleturquoise;">
<div id="waveformcontainer" style="padding-left: 20px;">
<p style="margin: 0px;">Waveform</p>
<select id="waveform">
<option value="sawtooth">sawtooth</option>
<option value="sine">sine</option>
<option value="square">square</option>
<option value="triangle">triangle</option>
</select></div>
<div id="filtercontainer" style="padding-left: 20px">
<p style="margin: 0px">Filter freq</p>
<input type="range" min="1" max="100" value="50" class="slider" id="filter">
</div>
<div id="notelenghtcontainer" style="padding-left: 20px;">
<p style="margin: 0px">Note length</p>
<input type="range" min="1" max="100" value="50" class="slider" id="notelength">
</div>
<div id="outputcontainer" style="padding-left: 20px; padding-right: 20px;">
<p style="margin: 0px">Output</p>
<select id="outputselect">
<option value="hexachord">hexachord</option>
</select>
</div>
<button onclick="copyUrlToClipboard()">Copy a link to this instrument</button>
</div>
<div id="elm"></div>
<script src=elm.js></script>
<script src="https://cdn.jsdelivr.net/npm/webmidi"></script>
<script>
var app = Elm.Main.init({
node: document.getElementById('elm'),
})
var audioContext = new AudioContext();
var waveformSelector = document.getElementById('waveform');
var filterSlider = document.getElementById('filter');
var noteLengthSlider = document.getElementById('notelength');
function getNoteLength(){
return 2 * (0.01 * noteLengthSlider.value)
}
function getMidiNoteLength(){
return getNoteLength() * 1000
}
function setFilterFrequency(filter){
var percentage = filterSlider.value / 100.0;
var min = 120;
var filterFreq = Math.pow((audioContext.sampleRate/2.0-min), percentage)+min
filter.type = "lowpass";
filter.frequency.value = filterFreq;
}
function copyUrlToClipboard() {
code = document.getElementById("instrument-code").value;
var tmpElem = document.createElement("input")
tmpElem.style = "position: absolute; left: -1000px; top: -1000px";
// Add the input value to the temp element.
tmpElem.value = window.location.hostname + "/?code=" + code;
document.body.appendChild(tmpElem);
/* Select the text field */
tmpElem.select();
tmpElem.setSelectionRange(0, 99999); /* For mobile devices */
/* Copy the text inside the text field */
document.execCommand("copy");
document.body.removeChild(tmpElem);
}
function start(tone){
var outputSelect = document.getElementById('outputselect')
var selectedOutput = outputSelect.value
if (selectedOutput == 'hexachord'){
start_audio(toneToFreq(tone))
return
}
else {
console.log(tone)
sendMidiMessage(toneToMidiFreq(tone), 100, getMidiNoteLength())
}
}
function toneToFreq(tone) {
[note, octave] = tone.split(':')
octave = parseInt(octave)
noteMapper = {'A': 220.0, 'Bb': 233.08, 'B': 246.94,
'C': 130.81, 'C#': 138.59, 'D': 146.83, 'Eb': 155.56, 'E': 164.81,
'F': 174.61, 'F#': 185.0, 'G': 196.0, 'Ab': 207.65}
return noteMapper[note] * (2 ** (octave -2))
}
function toneToMidiFreq(tone) {
[note, octave] = tone.split(':')
noteMapper = {
'C': 0, 'C#': 1, 'D': 2, 'Eb': 3, 'E': 4,
'F': 5, 'F#': 6, 'G': 7, 'Ab': 8, 'A': 9, 'Bb': 10, 'B': 11}
return noteMapper[note] + (octave*12) + 24
}
function start_audio(frequency) {
var vco = audioContext.createOscillator();
vco.type = waveformSelector.value;
vco.frequency.value = frequency;
var vca = audioContext.createGain();
vca.gain.value = 0.3;
var filter = audioContext.createBiquadFilter();
setFilterFrequency(filter);
// create connections
vco.connect(filter)
filter.connect(vca)
vca.connect(audioContext.destination);
vco.start(0);
var now = audioContext.currentTime;
var noteLength = getNoteLength();
vca.gain.linearRampToValueAtTime(0, audioContext.currentTime + noteLength);
vco.stop(now + noteLength);
}
app.ports.startRaw.subscribe(function (frequency) {
start(frequency);
});
var urlParams = new URLSearchParams(window.location.search);
var code = urlParams.get('code');
if (code){
app.ports.tokenMapFromQuery.send(code);
}
midiOut = []
selectOut = document.getElementById('outputselect')
function connect() {
navigator.requestMIDIAccess()
.then(
(midi) => midiReady(midi),
(err) => console.log('Something went wrong', err)
);
}
function midiReady(midi) {
// Also react to device changes.
midi.addEventListener(
'statechange',
(event) => initDevices(event.target)
);
initDevices(midi); // see the next section!
}
function initDevices(midi) {
// Reset.
midiOut = [];
// MIDI devices that you send data to.
const outputs = midi.outputs.values();
for (let output = outputs.next(); output && !output.done; output = outputs.next()) {
midiOut.push(output.value);
}
displayDevices()
}
function displayDevices() {
console.log('display devices')
selectedValue = String(selectOut.value)
selectOut.innerHTML = '<option>hexachord</option>'
selectOut.innerHTML += midiOut.map(
device => {
if (device.name == selectedValue){
return `<option selected="selected">${device.name}</option>`
}
else return `<option>${device.name}</option>`
}
).join('')
//console.log(selectedValue)
//console.log(selectOut.innerHTML)
}
function sendMidiMessage(pitch, velocity, duration) {
const NOTE_ON = 0x90;
const NOTE_OFF = 0x80;
const device = midiOut[selectOut.selectedIndex - 1];
const msgOn = [NOTE_ON, pitch, velocity];
const msgOff = [NOTE_OFF, pitch, velocity];
// First send the note on;
device.send(msgOn);
console.log(duration)
// Then send the note off. You can send this separately if you want
// (i.e. when the button is released)
setTimeout(() => {
console.log('noteoff');
device.send(msgOff)
}, duration)
}
connect()
</script>
</body>
</html>