Pull some JS functionality into classes

This commit is contained in:
Neale Pickett 2020-04-26 13:08:43 -06:00
parent 7ba5ee836e
commit 8fcbb05183
3 changed files with 267 additions and 97 deletions

17
main.go
View File

@ -4,6 +4,7 @@ import (
"log" "log"
"net/http" "net/http"
"golang.org/x/net/websocket" "golang.org/x/net/websocket"
"time"
) )
var book Book var book Book
@ -17,13 +18,23 @@ func (c Client) Handle(ws *websocket.Conn) {
book.Join(c.repeaterName, ws) book.Join(c.repeaterName, ws)
defer book.Part(c.repeaterName, ws) defer book.Part(c.repeaterName, ws)
offset := int64{0}
for { for {
var m Message
buf := make([]byte, ws.MaxPayloadBytes) buf := make([]byte, ws.MaxPayloadBytes)
n, err := ws.Read(buf)
if err != nil { if n, err := ws.Read(buf); err != nil {
break break
} } else {
buf = buf[:n] buf = buf[:n]
}
if err := m.UnmarshalBinary(buf); err != nil {
log("Unmarshal error:", err)
continue
}
book.Send(c.repeaterName, buf) book.Send(c.repeaterName, buf)
} }
} }

View File

@ -68,12 +68,12 @@
<table style="width: 100%; text-align: center;"> <table style="width: 100%; text-align: center;">
<tr> <tr>
<td> <td>
<button class="key mdl-button mdl-js-button mdl-button--raised mdl-button--colored"> <button id="dit" class="key mdl-button mdl-js-button mdl-button--raised mdl-button--colored">
dit dit
</button> </button>
</td> </td>
<td> <td>
<button class="key mdl-button mdl-js-button mdl-button--raised mdl-button--colored"> <button id="dah" class="key mdl-button mdl-js-button mdl-button--raised mdl-button--colored">
dah dah
</button> </button>
</td> </td>
@ -87,6 +87,17 @@
</td> </td>
</tr> </tr>
</table> </table>
<p>
dit length
<input
id="duration"
class="mdl-slider mdl-js-slider"
type="range"
min="40"
max="255"
value="80">
<output id="duration-value"></output>
</p>
</div> </div>
<div class="mdl-tabs__panel" id="straight"> <div class="mdl-tabs__panel" id="straight">
<button class="key mdl-button mdl-js-button mdl-button--raised mdl-button--colored"> <button class="key mdl-button mdl-js-button mdl-button--raised mdl-button--colored">

View File

@ -1,62 +1,219 @@
// jshint asi:true // jshint asi:true
var short = 80 var recFreq = 660
var long = 200 var sendFreq = recFreq * 6 / 5 // Perfect minor third
var audioFreq = 660
var audioFreqMe = audioFreq * 6 / 5 // I think this works out to a minor third
var ac = new AudioContext() const DIT = 1
const DAH = 3
var mygain = ac.createGain() class Iambic {
mygain.connect(ac.destination) constructor(BeginTxFunc, endTxFunc) {
mygain.gain.value = 0 this.beginTxFunc = beginTxFunc
this.endTxFunc = endTxFunc
this.interval = null
this.state = this.space
this.keyFunc = null
}
var myosc = ac.createOscillator() // Set a new interval (transmission rate)
myosc.connect(mygain) setInterval(duration) {
myosc.frequency.value = audioFreqMe clearInterval(this.interval)
myosc.start() this.interval = setInterval(e => this.pulse(), duration)
}
var theirgain = ac.createGain() // An interval has passed, call whatever the current state function is
theirgain.connect(ac.destination) pulse(event) {
theirgain.gain.value = 0 this.state()
}
var theirosc = ac.createOscillator() // Don't transmit for one interval.
theirosc.connect(theirgain) // If a transmission was requested, start transmitting at the next interval
theirosc.frequency.value = audioFreq space() {
theirosc.start() if (this.keyFunc) {
this.state = this.keyFunc
}
}
var repeatInterval // Send a dit
dit() {
this.beginTxFunc()
this.state = this.end
}
// Send a dah
dah() {
this.beginTxFunc()
this.state = this.dah2
}
dah2() {
this.state = this.dah3
}
dah3() {
this.state = this.end
}
// Stop sending
end() {
this.endTxFunc()
this.state = this.space
this.state()
}
// Edge trigger on key press
KeyDown(key) {
if (key == DIT) {
this.keyFunc = this.dit
} else if (key == DAH) {
this.keyFunc = this.dah
}
}
// Edge trigger on key release
KeyUp() {
// Only clear the keyFunc if the key released is the same one that we think is pressed
if ((key == DIT) && (this.keyFunc == this.dit)) {
this.keyFunc = null
} else if ((key == DAH) && (this.keyFunc = this.dah)) {
this.keyFunc = null
}
}
}
class Buzzer {
constructor(txGain=0.1) {
this.txGain = txGain
this.ac = new AudioContext()
this.lowGain = this.ac.createGain()
this.lowGain.connect(ac.destination)
this.lowGain.gain.value = 0
this.lowOsc = this.ac.createOscillator()
this.lowOsc.connect(recGain)
this.lowOsc.frequency.value = recFreq
this.highGain = this.ac.createGain()
this.highGain.connect(ac.destination)
this.highGain.gain.value = 0
this.highOsc = this.ac.createOscillator()
this.highOsc.connect(sendGain)
this.highOsc.frequency.value = recFreq
}
gain(high) {
if (high) {
return this.highGain
} else {
return this.lowGain
}
}
// Begin buzzing at time (null = now)
Buzz(high=false, when=null) {
if (when === null) {
when = this.ac.currentTime
}
this.gain(high).linearRampToValueAtTime(this.txGain, when + 0.1)
}
// End buzzing at time (null = now)
Silence(high=false, when=null) {
if (when === null) {
when = this.ac.currentTime
}
this.gain(high).linearRampToValueAtTime(0, when + 0.1)
}
// Buzz for a duration at time
BuzzDuration(high, when, duration) {
let gain = this.gain(high)
gain.setValueAtTime(0, when)
gain.linearRampToValueAtTime(this.txGain, when+0.1)
gain.setValueAtTime(this.txGain, when+duration)
gain.linearRampToValueAtTime(this.txGain, when+duration+0.1)
}
}
class Vail {
constructor() {
this.beginTxTime = null // Time when we began transmitting
// Set up WebSocket
let wsUrl = new URL(window.location)
wsUrl.protocol = "ws:"
wsUrl.pathname += "chat"
window.socket = new WebSocket(wsUrl)
window.socket.addEventListener("message", this.wsMessage)
// Listen for right clicks on dit button
let dit = document.querySelector("#dit")
dit.addEventListener("contextmenu", e => this.canWeJustNot(e))
dit.addEventListener("mousedown", e => this.ditMouse(e))
dit.addEventListener("mouseup", e => this.ditMouse(e))
// Listen for keystrokes
document.addEventListener("keydown", e => this.key(e))
document.addEventListener("keyup", e => this.key(e))
// Make an Iambic input device
this.iambic = new Iambic(() => this.beginTx(), () => this.endTx())
function message(event) { let durationElement = document.querySelector("#duration")
durationElement.addEventListener("input", e => this.changeDuration(e))
this.durationInterval = setInterval(e => this.durationElapsed(e), durationElement.value)
this.keyDownTime = 0
this.durationsLeft = 0
}
keyDown() {
this.keyDownTime = new Date()
this.sendGain.linearRampToValueAtTime(0.1, this.ac.currentTime + 0.1)
}
keyUp() {
let keyUpTime = new Date()
let duration = keyUpTime - keyDownTime
this.sendGain.linearRampToValueAtTime(0.0, this.ac.currentTime + 0.1)
this.send(keyDownTime, [duration])
}
durationElapsed(e) {
if (this.durationsLeft === 0) {
this.durationsLeft = this.keyDown + 1
}
if (this.keyDown == 2) {
return
}
}
beep(gain, duration) {
let now = ac.currentTime let now = ac.currentTime
let end = now + duration
if (now === 0) {
return
}
// Ramping in and out prevents square wave overtones
gain.linearRampToValueAtTime(0.1, now + 0.1)
gain.setValueAtTime(0.1, end - 0.1)
gain.linearRampToValueAtTime(0.0, end)
}
message(event) {
let duration = Number(event.data) || 0 let duration = Number(event.data) || 0
duration = Math.min(duration, long) duration = Math.min(duration, long)
beep(this.recGain.gain, duration)
if (now === 0) {
// Audio Context hasn't started, we can't make sound yet
return
} }
theirgain.gain.linearRampToValueAtTime(0.1, now + 0.01) send(duration) {
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) window.socket.send(duration)
beep(this.sendGain.gain, duration)
if (now === 0) {
return
} }
mygain.gain.linearRampToValueAtTime(0.1, now + 0.01) key(event) {
mygain.gain.setValueAtTime(0.1, now + duration/1000)
mygain.gain.linearRampToValueAtTime(0.0, now + 0.01 + duration/1000)
}
function key(event) {
let duration = 0 let duration = 0
ac.resume() ac.resume()
@ -86,29 +243,20 @@ function key(event) {
} }
} }
function canWeJustNot(event) { canWeJustNot(event) {
event.preventDefault() event.preventDefault()
return false return false
} }
function init() { }
let wsUrl = new URL(window.location)
wsUrl.protocol = "ws:"
wsUrl.pathname += "chat"
window.socket = new WebSocket(wsUrl)
window.socket.addEventListener("message", message)
// disable RMB context menu function vailInit() {
document.addEventListener("contextmenu", e => canWeJustNot(e)) window.app = new Vail()
document.addEventListener("mousedown", e => key(e))
document.addEventListener("mouseup", e => key(e))
document.addEventListener("keydown", e => key(e))
document.addEventListener("keyup", e => key(e))
} }
if (document.readyState === "loading") { if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", init) document.addEventListener("DOMContentLoaded", vailInit)
} else { } else {
init() vailInit()
} }