vail/static/vail.js

115 lines
2.6 KiB
JavaScript
Raw Normal View History

2020-04-20 22:12:30 -06:00
// jshint asi:true
2020-04-09 23:09:33 -06:00
2020-04-10 08:27:35 -06:00
var short = 80
var long = 200
2020-04-09 23:09:33 -06:00
var audioFreq = 660
var audioFreqMe = audioFreq * 6 / 5 // I think this works out to a minor third
2020-04-20 22:12:30 -06:00
var ac = new AudioContext()
var mygain = ac.createGain()
mygain.connect(ac.destination)
mygain.gain.value = 0
var myosc = ac.createOscillator()
myosc.connect(mygain)
myosc.frequency.value = audioFreqMe
myosc.start()
var theirgain = ac.createGain()
theirgain.connect(ac.destination)
theirgain.gain.value = 0
var theirosc = ac.createOscillator()
theirosc.connect(theirgain)
theirosc.frequency.value = audioFreq
theirosc.start()
var repeatInterval
2020-04-09 23:09:33 -06:00
function message(event) {
2020-04-20 22:12:30 -06:00
let now = ac.currentTime
2020-04-09 23:09:33 -06:00
let duration = Number(event.data) || 0
2020-04-10 08:27:35 -06:00
duration = Math.min(duration, long)
2020-04-09 23:09:33 -06:00
2020-04-20 22:12:30 -06:00
if (now === 0) {
// Audio Context hasn't started, we can't make sound yet
return
}
theirgain.gain.linearRampToValueAtTime(0.1, now + 0.01)
mygain.gain.setValueAtTime(0.1, now + duration/1000)
theirgain.gain.linearRampToValueAtTime(0.0, now + 0.01 + duration/1000)
}
function send(duration) {
let now = ac.currentTime
window.socket.send(duration)
if (now === 0) {
return
}
mygain.gain.linearRampToValueAtTime(0.1, now + 0.01)
mygain.gain.setValueAtTime(0.1, now + duration/1000)
mygain.gain.linearRampToValueAtTime(0.0, now + 0.01 + duration/1000)
2020-04-09 23:09:33 -06:00
}
function key(event) {
2020-04-10 08:27:35 -06:00
let duration = 0
2020-04-20 22:12:30 -06:00
ac.resume()
if (event.repeat) {
// Ignore key repeats generated by the OS, we do this ourselves
return
}
2020-04-10 08:27:35 -06:00
2020-04-20 22:12:30 -06:00
if ((event.button === 0) || (event.code == "Period") || (event.key == "Shift")) {
2020-04-10 08:27:35 -06:00
duration = short
}
2020-04-20 22:12:30 -06:00
if ((event.button === 2) || (event.code == "Slash") || (event.code == "KeyZ")) {
2020-04-10 08:27:35 -06:00
duration = long
}
2020-04-20 22:12:30 -06:00
if (duration === 0) {
2020-04-10 08:27:35 -06:00
return
2020-04-09 23:09:33 -06:00
}
2020-04-20 22:12:30 -06:00
if (repeatInterval) {
clearInterval(repeatInterval)
}
2020-04-10 08:27:35 -06:00
2020-04-20 22:12:30 -06:00
if (event.type.endsWith("down")) {
send(duration)
repeatInterval = setInterval(() => {send(duration)}, duration + short)
}
2020-04-09 23:09:33 -06:00
}
2020-04-10 08:59:15 -06:00
function canWeJustNot(event) {
event.preventDefault()
return false
}
2020-04-09 23:09:33 -06:00
function init() {
2020-04-10 08:27:35 -06:00
let wsUrl = new URL(window.location)
wsUrl.protocol = "ws:"
wsUrl.pathname += "chat"
window.socket = new WebSocket(wsUrl)
2020-04-09 23:09:33 -06:00
window.socket.addEventListener("message", message)
2020-04-10 08:59:15 -06:00
// disable RMB context menu
document.addEventListener("contextmenu", e => canWeJustNot(e))
2020-04-09 23:09:33 -06:00
document.addEventListener("mousedown", e => key(e))
2020-04-20 22:12:30 -06:00
document.addEventListener("mouseup", e => key(e))
2020-04-09 23:09:33 -06:00
document.addEventListener("keydown", e => key(e))
2020-04-20 22:12:30 -06:00
document.addEventListener("keyup", e => key(e))
2020-04-09 23:09:33 -06:00
}
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", init)
} else {
init()
}