Skip to content

Commit

Permalink
feat: pass context to onNote() events
Browse files Browse the repository at this point in the history
closes #14
  • Loading branch information
arnoson committed Oct 11, 2023
1 parent 6876752 commit f9d00d0
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 10 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,27 @@ myController = midi.input(3).channel(15)
myController.onNote('c1', () => {
osc().out()
})

// or listen to all notes:
myController.onNote('*', ({ note, velocity, channel }) => {
osc().out()
})
```
Or listen to any note:
```js
myController = midi.input(3).channel('*')

scene1 = () => solid(1,0,0).out()
scene2 = () => solid(0,1,0).out()

myController.onNote('*', ({ note, velocity, channel }) => {
switch (note) {
case 36: { scene1(); break; }
case 37: { scene2(); break; }
}
})
```
### Transforms
Expand Down
4 changes: 2 additions & 2 deletions src/hydra-api/onNote.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import state from '../state'
import { ChannelArg, InputArg, NoteArg } from '../types'
import { ChannelArg, InputArg, NoteArg, NoteEventContext } from '../types'
import { getNoteId } from './note'

export const onNote = (
note: NoteArg,
channel: ChannelArg,
input: InputArg,
event: Function
event: (context: NoteEventContext) => void
) => {
const noteId = getNoteId(note, channel, input)
state.noteOnEvents.set(noteId, event)
Expand Down
4 changes: 2 additions & 2 deletions src/midiAccess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ midiAccess.on(MidiAccess.TypeNoteOn, ({ data, channel, input }) => {
const noteId = getMidiId(note, channel, input.id)
playingNotes.set(noteId, velocity)
envelopes.get(noteId)?.trigger()
noteOnEvents.get(noteId)?.()
noteOnEvents.get(noteId)?.({ note, velocity, channel })

getMidiWildcards(note, channel, input.id).forEach(wildcard => {
playingNotes.set(wildcard, velocity)
envelopes.get(wildcard)?.trigger()
noteOnEvents.get(wildcard)?.()
noteOnEvents.get(wildcard)?.({ note, velocity, channel })
})

logMidiMessage({ input, type: 'on', channel, data })
Expand Down
4 changes: 2 additions & 2 deletions src/state.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { CCValues, Defaults } from './types'
import type { CCValues, Defaults, NoteEventContext } from './types'

const ccValues: CCValues = new Map(
JSON.parse(sessionStorage.getItem('hydra-midi_ccValues') || '[]')
Expand All @@ -17,7 +17,7 @@ export default {

playingNotes: new Map<string, number>(),

noteOnEvents: new Map<string, Function>(),
noteOnEvents: new Map<string, (context: NoteEventContext) => void>(),

defaults: {
channel: '*',
Expand Down
4 changes: 2 additions & 2 deletions src/transforms/channel.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { cc } from '../hydra-api/cc'
import { note } from '../hydra-api/note'
import { onNote } from '../hydra-api/onNote'
import { ChannelArg, InputArg, NoteArg } from '../types'
import { ChannelArg, InputArg, NoteArg, NoteEventContext } from '../types'

/**
* Channel is chainable to `midi` and `input()` and provides a channel for all
Expand All @@ -16,6 +16,6 @@ export const channel = (channel: ChannelArg, input?: InputArg) => ({
cc: (_index?: number, _channel?: ChannelArg, _input?: InputArg) =>
cc(_index, _channel ?? channel, _input ?? input),

onNote: (_note: NoteArg, _event: Function) =>
onNote: (_note: NoteArg, _event: (context: NoteEventContext) => void) =>
onNote(_note, channel, input ?? '*', _event),
})
4 changes: 2 additions & 2 deletions src/transforms/input.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { cc, note, onNote } from '../hydra-api'
import { ChannelArg, InputArg, NoteArg } from '../types'
import { ChannelArg, InputArg, NoteArg, NoteEventContext } from '../types'
import { channel } from './channel'

/**
Expand All @@ -15,7 +15,7 @@ export const input = (input: InputArg) => ({
cc: (_index: number, _channel: ChannelArg, _input?: InputArg) =>
cc(_index, _channel, _input ?? input),

onNote: (_note: NoteArg, _event: Function) =>
onNote: (_note: NoteArg, _event: (context: NoteEventContext) => void) =>
onNote(_note, '*', input, _event),

channel: (_channel: ChannelArg) => channel(_channel, input),
Expand Down
6 changes: 6 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ export interface HydraContext {
}
}

export interface NoteEventContext {
note: number
velocity: number
channel: number
}

export interface Defaults {
input: InputArg
channel: ChannelArg
Expand Down

0 comments on commit f9d00d0

Please sign in to comment.