Skip to content

Commit

Permalink
add onnote to trigger custom events on note on
Browse files Browse the repository at this point in the history
  • Loading branch information
geikha committed Feb 4, 2023
1 parent bde5e65 commit de716e2
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/hydra-api/note.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { getMidiId, resolveNote, resolveInput } from '../midiAccess'

const noteIsPlaying = noteId => state.playingNotes.has(noteId)

const getNoteId = (note, channel, input) =>
export const getNoteId = (note, channel, input) =>
getMidiId(
resolveNote(note),
channel ?? state.defaults.channel,
Expand Down
9 changes: 9 additions & 0 deletions src/hydra-api/onnote.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// @ts-check

import state from '../state'
import { getNoteId } from './note'

export const onnote = (note, channel, input, event) => {
const noteId = getNoteId(note, channel, input)
noteOnEvents[noteId] = event;
}
1 change: 1 addition & 0 deletions src/midiAccess.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ midiAccess.on(MidiAccess.TypeNoteOn, ({ data, channel, input }) => {
const noteId = getMidiId(note, channel, input.id)
playingNotes.add(noteId)
envelopes[noteId]?.trigger()
noteOnEvents[noteId]?.call()

getMidiWildcards(note, channel, input.id).forEach(wildcard => {
playingNotes.add(wildcard)
Expand Down
3 changes: 3 additions & 0 deletions src/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ export default {
/** @type {Set<string>} */
playingNotes: new Set(),

/** @type {Record<string, function>} */
noteOnEvents: {},

initialDefaults: {
channel: 0,
input: 0,
Expand Down
6 changes: 5 additions & 1 deletion src/transforms/channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import { cc } from '../hydra-api/cc'
import { note } from '../hydra-api/note'
import { onnote } from '../hydra-api/onnote'

/**
* Channel is chainable to `midi` and `input()` and provides a channel for all
Expand All @@ -17,5 +18,8 @@ export const channel = (channel, input = null) => ({
note(_note, _channel ?? channel, _input ?? input),

cc: (_index, _channel, _input) =>
cc(_index, _channel ?? channel, _input ?? input)
cc(_index, _channel ?? channel, _input ?? input),

onnote: (_note, _event) =>
onnote(_note, channel, input, _event)
})
3 changes: 2 additions & 1 deletion src/transforms/input.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @ts-check

import { cc, note } from '../hydra-api'
import { cc, note, onnote } from '../hydra-api'
import { channel } from './channel'

/**
Expand All @@ -14,5 +14,6 @@ import { channel } from './channel'
export const input = input => ({
note: (_note, _channel, _input) => note(_note, _channel, _input ?? input),
cc: (_index, _channel, _input) => cc(_index, _channel, _input ?? input),
onnote: (_note, _event) => onnote(_note, '*', input, _event),
channel: _channel => channel(_channel, input)
})

0 comments on commit de716e2

Please sign in to comment.