This repository has been archived by the owner on Oct 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use SessionShowContext everywhere, sustainInverted
- Loading branch information
1 parent
b945f2b
commit c81651c
Showing
11 changed files
with
236 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/** | ||
* Processes MIDI data received from local MIDI input device. Plays the local | ||
* peer keyboard and sends data to the remote peer. | ||
* | ||
* @see https://newt.phys.unsw.edu.au/jw/notes.html | ||
* @see https://www.midi.org/specifications-old/item/table-1-summary-of-midi-message | ||
* */ | ||
import { | ||
InputEventControlchange, | ||
InputEventNoteoff, | ||
InputEventNoteon, | ||
InputEvents, | ||
} from "webmidi"; | ||
import { store } from "./store"; | ||
import { PeerConnection } from "./rtc/PeerConnection"; | ||
import { playKeyboard } from "./playKeyboard"; | ||
import { Session } from "@midishare/common"; | ||
import { | ||
buildSessionShowContext, | ||
ISessionShowContext, | ||
} from "../views/pages/Sessions/Show/SessionShowContext"; | ||
import { queryClient } from "./queryClient"; | ||
import { queryKey as currentUserQueryKey } from "./queries/getCurrentUser"; | ||
import { queryKey as sessionQueryKey } from "./queries/getSession"; | ||
|
||
// Restrict allowable event types to prevent registering something new that | ||
// we don't yet know how to handle. Let's goooo strong types! | ||
export type AllowedInputEventTypes = "noteon" | "noteoff" | "controlchange"; | ||
|
||
export function createMidiInputHandler( | ||
context: ISessionShowContext | ||
): ( | ||
event: InputEventNoteon | InputEventNoteoff | InputEventControlchange | ||
) => void { | ||
if (!context.session) { | ||
throw new Error("Cannot create MIDI input handler without session"); | ||
} | ||
|
||
const sessionId = context.session.id; | ||
|
||
return (event) => { | ||
// Ignore events from inactive MIDI devices, but don't unregister | ||
// the listeners. | ||
if (store.getState().activeMidiInputDeviceId !== event.target.id) { | ||
return; | ||
} | ||
|
||
const eventType: Extract<keyof InputEvents, AllowedInputEventTypes> = | ||
event.type; | ||
const timestamp = event.timestamp; | ||
const data = event.data; | ||
|
||
const runtime = store.getState().runtime?.localKeyboardRuntime; | ||
if (!runtime) { | ||
throw new Error("Runtime not initialized"); | ||
} | ||
playKeyboard( | ||
"local", | ||
eventType, | ||
timestamp, | ||
data, | ||
// TODO this is a tight coupling, but it might be fine tbh | ||
buildSessionShowContext({ | ||
currentUser: queryClient.getQueryData(currentUserQueryKey()), | ||
session: queryClient.getQueryData(sessionQueryKey(sessionId)), | ||
}) | ||
); | ||
PeerConnection.sendMidiData(data, timestamp); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 5 additions & 2 deletions
7
projects/client/src/views/pages/Sessions/Show/PeerLane/AttachMidiPrompt.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
projects/client/src/views/pages/Sessions/Show/SessionShowContext.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { createContext } from "react"; | ||
import { Session, SessionRuntimeOptions, UserProfile } from "@midishare/common"; | ||
import { Runtime } from "@midishare/keyboard"; | ||
import { store } from "../../../../lib/store"; | ||
|
||
export type ISessionShowContext = { | ||
currentUser?: UserProfile | null; | ||
session?: Session | null; | ||
isHost?: boolean; | ||
localRuntime?: Runtime; | ||
remoteRuntime?: Runtime; | ||
localRuntimeOptions?: SessionRuntimeOptions; | ||
remoteRuntimeOptions?: SessionRuntimeOptions; | ||
}; | ||
|
||
export const SessionShowContext = createContext<ISessionShowContext>({}); | ||
|
||
/** | ||
* @note Calling this without the currentUser and/or session keys will still | ||
* create a context, one | ||
* | ||
* @note Do not use React hooks in this function for now, as it is also used | ||
* outside of the React context. Maybe that is a huge smell anyway, might | ||
* revisit this approach. | ||
* */ | ||
export function buildSessionShowContext( | ||
options: Pick<ISessionShowContext, "currentUser" | "session"> | ||
): ISessionShowContext { | ||
const isHost = (() => { | ||
// If we know user is not logged in, they are always the guest | ||
if (options.currentUser === null) { | ||
return false; | ||
} | ||
|
||
// Waiting for currentUser and/or session to resolve, indeterminate | ||
if (!options.currentUser || !options.session) { | ||
return undefined; | ||
} | ||
|
||
return options.currentUser.sub === options.session.participants.host; | ||
})(); | ||
|
||
const localRuntime = store.getState().runtime?.localKeyboardRuntime; | ||
const remoteRuntime = store.getState().runtime?.remoteKeyboardRuntime; | ||
|
||
const localRuntimeOptions = (() => { | ||
if (!options.session || isHost === undefined) { | ||
return undefined; | ||
} | ||
return options.session.runtimeOptions[isHost ? "host" : "guest"]; | ||
})(); | ||
|
||
const remoteRuntimeOptions = (() => { | ||
if (!options.session || isHost === undefined) { | ||
return undefined; | ||
} | ||
return options.session.runtimeOptions[isHost ? "guest" : "host"]; | ||
})(); | ||
|
||
return { | ||
currentUser: options.currentUser, | ||
session: options.session, | ||
localRuntime, | ||
remoteRuntime, | ||
localRuntimeOptions, | ||
remoteRuntimeOptions, | ||
isHost, // computed | ||
}; | ||
} |
Oops, something went wrong.