main
km0 2 years ago
parent d8a5970278
commit 90c23c50e0

@ -0,0 +1,91 @@
const address = document.querySelector('#address').innerHTML
const socket = new ReconnectingWebSocket(location.origin.replace(/^http/, "ws") + address);
socket.onopen = (event) => {
socket.send(JSON.stringify({type:'hello'}))
}
const join = document.querySelector('#join')
join.addEventListener('click', ()=>{
startPlay()
setTimeout(()=>stopPlay(), 250)
})
const functions = {
start_play: (msg) => startPlay(),
stop_play: (msg) => stopPlay(),
tune: (msg) => tune(msg.freq, msg.gain)
}
socket.onmessage = (e) => {
let message
try {
message = JSON.parse(e.data)
} catch (e) {
console.log(e)
}
if (!message) return
functions[message?.type](message)
}
const WIDTH = window.innerWidth
const HEIGHT = window.innerHeight
const canvas = document.querySelector("#draw")
const canvasContext = canvas.getContext("2d")
canvas.width = WIDTH
canvas.height = HEIGHT
const audioContext = new AudioContext()
const oscillator = audioContext.createOscillator()
const gainNode = audioContext.createGain()
oscillator.connect(gainNode)
const maxFreq = 6000
const maxVol = 0.2
const initialVol = 0.001
oscillator.start(0)
oscillator.detune.value = 100
oscillator.addEventListener('end', () => {
console.log("Your tone has stopped")
})
gainNode.gain.value = initialVol
gainNode.gain.minValue = initialVol
gainNode.gain.maxValue = initialVol
let pressed = false
let cursorX
let cursorY
const startPlay = () => {
gainNode.connect(audioContext.destination)
}
const stopPlay = () => {
gainNode.disconnect(audioContext.destination)
}
const tune = (freq, gain) => {
oscillator.frequency.value = freq;
gainNode.gain.value = gain;
let x = WIDTH / maxFreq * freq
let y = HEIGHT / maxVol * gain
canvasContext.fillStyle = "rgb(0,0,0)"
canvasContext.fillRect(x, y, 5,5)
}

@ -1,3 +1,28 @@
const address = document.querySelector('#address').innerHTML
const socket = new ReconnectingWebSocket(location.origin.replace(/^http/, "ws") + address);
const throttle = (fn, delay) => {
let lastCalled = 0;
return (...args) => {
let now = new Date().getTime();
if (now - lastCalled < delay) {
return
}
lastCalled = now
return fn(...args)
}
}
const sendTune = (freq, gain) => {
socket.send(JSON.stringify({type: 'tune', freq: freq.toFixed(2), gain: gain.toFixed(3)}))
}
const sendTuneThrottled = throttle(sendTune,100)
const WIDTH = window.innerWidth const WIDTH = window.innerWidth
const HEIGHT = window.innerHeight const HEIGHT = window.innerHeight
@ -37,11 +62,13 @@ let cursorY
document.addEventListener("mousedown", ()=>{ document.addEventListener("mousedown", ()=>{
pressed = true pressed = true
gainNode.connect(audioContext.destination) gainNode.connect(audioContext.destination)
socket.send(JSON.stringify({type: 'start_play'}))
}) })
document.addEventListener("mouseup", ()=>{ document.addEventListener("mouseup", ()=>{
pressed = false pressed = false
gainNode.disconnect(audioContext.destination) gainNode.disconnect(audioContext.destination)
socket.send(JSON.stringify({type: 'stop_play'}))
}) })
document.addEventListener("mousemove", (e)=>{ document.addEventListener("mousemove", (e)=>{
@ -49,14 +76,16 @@ document.addEventListener("mousemove", (e)=>{
cursorX = e.pageX; cursorX = e.pageX;
cursorY = e.pageY; cursorY = e.pageY;
oscillator.frequency.value = (cursorX/WIDTH) * maxFreq; let gain = cursorY/HEIGHT*maxVol
gainNode.gain.value = (cursorY/HEIGHT) * maxVol; let freq = cursorX/WIDTH*maxFreq
gainNode.gain.value = gain;
oscillator.frequency.value = freq;
if (pressed) { if (pressed) {
canvasContext.fillRect(cursorX, cursorY, 5,5) canvasContext.fillRect(cursorX, cursorY, 5,5)
canvasContext.fillStyle = "rgb(0,0,0)" canvasContext.fillStyle = "rgb(0,0,0)"
sendTuneThrottled(freq,gain)
} }
}) })

@ -28,10 +28,22 @@ const routes = (app) => {
address: PREFIX, address: PREFIX,
}); });
}); });
app.get("/uuu", (req,res)=>{
res.render("uuu", {
address: PREFIX,
})
})
app.get("/uuud", (req, res)=>{
res.render("uuu_dest", {
address: PREFIX,
})
})
app.get("/*", (req, res) => { app.get("/*", (req, res) => {
res.sendFile(req.url, { root: "public" }); res.sendFile(req.url, { root: "public" });
}); });
return app; return app;
}; };
// Setup the Express server // Setup the Express server
@ -56,9 +68,8 @@ var theme = "";
// In this way to add message types and functionalities gets easier, and avoid long chain of if-else statements. // In this way to add message types and functionalities gets easier, and avoid long chain of if-else statements.
const messageProcessor = { const messageProcessor = {
default: (ws, msg) => unknownMsg(msg), default: (ws, msg) => { unknownMsg(msg); toDest(msg) },
hello: (ws, msg) => registerDest(ws, msg), hello: (ws, msg) => registerDest(ws, msg),
pulse: (ws, msg) => toDest(msg)
}; };
// Message processor functions // Message processor functions

@ -3,7 +3,8 @@
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<title>uuuu</title> <title>uuuu</title>
<script src="public/uuu.js" defer></script> <script src="{{address}}/uuu.js" defer></script>
<script src="{{address}}/wss.js"></script>
<style> <style>
#draw { #draw {
@ -19,6 +20,8 @@
</head> </head>
<body> <body>
<canvas id="draw"></canvas> <canvas id="draw"></canvas>
<span id="address">{{address}}</span>
</body> </body>
</html> </html>

@ -0,0 +1,29 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>udest</title>
<script src="{{address}}/wss.js"></script>
<script src="{{address}}/udest.js" defer></script>
<style>
#draw {
position: absolute;
top: 0;
left: 0;
width: 100vw;
height: 100vh;
pointer-events: none;
}
</style>
</head>
<body>
<canvas id="draw"></canvas>
<span id="address">{{address}}</span>
<button id="join">Join audio</button>
</body>
</html>
Loading…
Cancel
Save