Skip to content
This repository was archived by the owner on Apr 3, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion static/web/game/keyboard/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { right } from "./keys/right"
import { space } from "./keys/space"
import { up } from "./keys/up"
import { w } from "./keys/w"
import { i } from "./keys/i"

import { IKeyboardAction } from './i-actions'

Expand All @@ -32,4 +33,5 @@ p(actions)
right(actions)
space(actions)
up(actions)
w(actions)
w(actions)
i(actions)
27 changes: 27 additions & 0 deletions static/web/game/keyboard/keys/i.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { config, setParam } from "../../../config/config"
import { Song } from "../../song/song"
import { toggle } from "../tools"
import { IKeyboardAction } from "../i-actions"
import { webMidi } from "../../../web-midi/web-midi"
import { FromDevice } from "../../player/from-device"

export function i(keyboardActions: { [key: string]: IKeyboardAction }) {
keyboardActions["i"] = {
title: "Change device",
description: "Alternate through midi devices",
triggerAction: (song: Song) => {
var midi = webMidi.midi
const inputs = Array.from(midi.inputs.values())
var device = parseInt(config.p_deviceIn) + 1
if (device >= inputs.length)
device = 0

setParam("p_deviceIn", device.toString())
webMidi.refresh()
},
getCurrentState: () => {
const inputs = Array.from(webMidi.midi.inputs.values())
return inputs[config.p_deviceIn].name
}
}
}
35 changes: 31 additions & 4 deletions static/web/web-midi/web-midi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class WebMidi {
private midi: any
private output: any
private input: any
private cb: any

constructor() {
const o = this
Expand All @@ -30,19 +31,45 @@ class WebMidi {

inOpen = (callback: (timestamp: number, data1: number, data2: number, data3: number) => void) => {
const o = this
o.input = o.midi.inputs.get('input-' + config.p_deviceIn)
o.cb = callback
o.refresh()
}

refresh = () => {
const o = this
// First try to get by configured device ID
o.midi.inputs.forEach((value, key) => {
console.log("key:"+key)
console.log("id: "+value.id)
console.log("name: "+value.name)
})
o.input = o.midi.inputs.get(config.p_deviceIn)

// If not found, get the first available input
if (!o.input) {
const inputs = Array.from(o.midi.inputs.values())
o.input = inputs[config.p_deviceIn]
if (!o.input) {
o.input = inputs[0]
}
}

if (o.input) {
o.input.onmidimessage = (e: any) => {
callback(e.timeStamp, e.data[0], e.data[1], e.data[2])
o.cb(e.timeStamp, e.data[0], e.data[1], e.data[2])
}
}
}

outOpen = () => {
const o = this
o.output = o.midi.outputs.get('output-' + config.p_deviceOut)
// First try to get by configured device ID
o.output = o.midi.outputs.get(config.p_deviceOut)

// If not found, get the first available output
if (!o.output) {
o.output = o.midi.outputs.get(0)
const outputs = Array.from(o.midi.outputs.values())
o.output = outputs[0]
}
}

Expand Down