Skip to content

Commit

Permalink
velocity keys
Browse files Browse the repository at this point in the history
  • Loading branch information
dtinth committed Aug 23, 2023
1 parent a60fe83 commit b50ef6d
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 9 deletions.
22 changes: 21 additions & 1 deletion src/core/MainView.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ import { AppServices } from './AppServices'
import MIDIEmitter from './MIDIEmitter'
import { tw } from 'twind'
import { StatusBar } from './StatusBar'
import { switchOutputChannel, OutputChannelSwitcher } from './switchOutputChannel'
import {
switchOutputChannel,
OutputChannelSwitcher,
} from './switchOutputChannel'
import { showInformationMessage } from './showInformationMessage'

export class MainView extends React.Component {
Expand Down Expand Up @@ -59,10 +62,27 @@ export class MainView extends React.Component {
}
}
if (e.ctrlKey) {
const updateVelocity = (delta) => {
const velocity = Math.round(
Math.max(0, Math.min(127, this.store.noteVelocity + delta * 8))
)
this.store.noteVelocity = velocity
showInformationMessage('Velocity: ' + velocity)
}
if (e.keyCode >= 0x30 && e.keyCode <= 0x39) {
switchOutputChannel(e.keyCode === 0x30 ? 10 : e.keyCode - 0x30)
e.preventDefault()
return
} else if (e.keyCode === 189) {
// Ctrl + -
updateVelocity(-1)
e.preventDefault()
return
} else if (e.keyCode === 187) {
// Ctrl + =
updateVelocity(1)
e.preventDefault()
return
}
}
if (e.metaKey || e.ctrlKey) return
Expand Down
22 changes: 15 additions & 7 deletions src/core/showInformationMessage.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
import { tw } from "twind"
import { tw } from 'twind'

export function showInformationMessage (text: string) {
let clearLastMessage = () => {}

export function showInformationMessage(text: string) {
let cleared = false
clearLastMessage()
const message = document.createElement('div')
message.className = tw`absolute top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2 pointer-events-none bg-black text-white text-xl p-4`
message.className = tw`absolute top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2 pointer-events-none bg-black text-white text-4xl p-4`
message.style.opacity = '1'
message.style.zIndex = '999'
message.style.transition = 'opacity 0.5s ease-in-out'
message.innerText = text
document.body.appendChild(message)
const clear = () => {
if (cleared) return
cleared = true
message.remove()
}
clearLastMessage = clear
setTimeout(() => {
message.style.opacity = '0'
setTimeout(() => {
document.body.removeChild(message)
}, 500)
setTimeout(clear, 500)
}, 1000)
}
}
13 changes: 12 additions & 1 deletion src/midi-keybindings/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { MIDI, createFeature, useConfiguration } from '../core'
import { MIDI, Store, createFeature, useConfiguration } from '../core'
import { observable } from 'mobx'
import { KeyboardEvent, useEffect } from 'react'
import { showInformationMessage } from '../core/showInformationMessage'

const state = observable({
keyCodes: observable.map<number, { altKey: boolean }>({}),
Expand All @@ -25,6 +26,12 @@ let pedalMidiChannel = 1
let chromaticMode = false
let currentPedal: { up: () => void } | null = null

function notifyTranspose(store: Store) {
showInformationMessage(
`Octave: ${store.octave}, Transpose: ${store.transpose}`
)
}

export default createFeature({
name: 'midi-keybindings',
category: 'addons',
Expand Down Expand Up @@ -52,18 +59,22 @@ export default createFeature({
}
if (keyCode === 37) {
store.setTranspose(store.transpose - 1)
notifyTranspose(store)
return
}
if (keyCode === 38) {
store.setOctave(store.octave + 1)
notifyTranspose(store)
return
}
if (keyCode === 39) {
store.setTranspose(store.transpose + 1)
notifyTranspose(store)
return
}
if (keyCode === 40) {
store.setOctave(store.octave - 1)
notifyTranspose(store)
return
}
state.keyCodes.set(keyCode, { altKey: event.altKey })
Expand Down

0 comments on commit b50ef6d

Please sign in to comment.