Skip to content

Commit

Permalink
fix: sequentially process messages, restrict share-sustain to just su…
Browse files Browse the repository at this point in the history
…stain messages
  • Loading branch information
aolsenjazz committed Sep 23, 2024
1 parent f798e1d commit 2aa88cf
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 88 deletions.
8 changes: 4 additions & 4 deletions src/main/port-service/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,15 +279,15 @@ export class HardwarePortServiceSingleton {
pair: PortPair,
msg: NumberArrayWithStatus
) {
const loopbackTransport = this.ports.get(config.id)!;
const remoteTransport = VirtualPortService.ports.get(config.id)!;

config.process(msg, loopbackTransport, remoteTransport, {
const message = config.process(msg, {
loopbackTransports: this.ports,
remoteTransports: VirtualPortService.ports,
pluginProvider: Registry,
});

const remoteTransport = VirtualPortService.ports.get(config.id);
if (remoteTransport && message) remoteTransport.send(message);

// TODO: rethink sending input state to frontend
// do we want to just broadcast on message channel? or will that invoke too
// many listeners and be irresponsible usage of cpu cycles?
Expand Down
30 changes: 17 additions & 13 deletions src/plugins/device-plugins/share-sustain/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { MessageProcessorMeta } from '@shared/message-processor';
import { MessageTransport } from '@shared/message-transport';
import { BasePlugin } from '@shared/plugin-core/base-plugin';
import { ShareSustainDTO } from './share-sustain-dto';

const CC = 176;
const SUSTAIN_NUM = 64;

export default class ShareSustainPlugin extends BasePlugin<ShareSustainDTO> {
public sustainTargets: string[] = [];

Expand All @@ -15,20 +17,22 @@ export default class ShareSustainPlugin extends BasePlugin<ShareSustainDTO> {
this.sustainTargets = sustainTargets;
}

public process(
msg: NumberArrayWithStatus,
_loopbackTransport: MessageTransport,
_remoteTransport: MessageTransport,
meta: MessageProcessorMeta
) {
public process(msg: NumberArrayWithStatus, meta: MessageProcessorMeta) {
const { remoteTransports } = meta;

this.sustainTargets
.map((t) => remoteTransports.get(t))
.filter((transport) => transport !== undefined)
.forEach((transport) => {
transport!.send(msg);
});
const statusNibble = msg[0] & 0xf0;
const number = msg[1];

if (statusNibble === CC && number === SUSTAIN_NUM) {
this.sustainTargets
.map((t) => remoteTransports.get(t))
.filter((transport) => transport !== undefined)
.forEach((transport) => {
transport!.send(msg);
});
}

return msg;
}

public toDTO(): ShareSustainDTO {
Expand Down
10 changes: 2 additions & 8 deletions src/plugins/device-plugins/translator/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { MessageProcessorMeta } from '@shared/message-processor';
import { MessageTransport } from '@shared/message-transport';
import { BasePlugin, PluginDTO } from '@shared/plugin-core/base-plugin';
import { MidiEventOverride } from './midi-event-override';

Expand All @@ -18,16 +17,11 @@ export default class TranslatorPlugin extends BasePlugin<TranslatorDTO> {
return ['anonymous'];
}

public process(
msg: NumberArrayWithStatus,
_loopbackTransport: MessageTransport,
remoteTransport: MessageTransport,
_meta: MessageProcessorMeta
) {
public process(msg: NumberArrayWithStatus, _meta: MessageProcessorMeta) {
const override = this.overrides.find(
(o) => JSON.stringify(o.source) === JSON.stringify(msg)
);
remoteTransport.send(override ? override.override : msg);
return override ? override.override : msg;
}

public toDTO(): TranslatorDTO {
Expand Down
11 changes: 2 additions & 9 deletions src/plugins/input-plugins/backlight-control/index.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
import { MessageProcessorMeta } from '@shared/message-processor';
import { MessageTransport } from '@shared/message-transport';
import { BasePlugin, PluginDTO } from '@shared/plugin-core/base-plugin';

// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface BacklightControlIcicle extends PluginDTO {}

export default class BacklightControlPlugin extends BasePlugin<PluginDTO> {
public process(
msg: NumberArrayWithStatus,
_loopbackTransport: MessageTransport,
_remoteTransport: MessageTransport,
_meta: MessageProcessorMeta
) {
// eslint-disable-next-line no-console
console.log(`${this.title} processing ${msg}`);
public process(msg: NumberArrayWithStatus, _meta: MessageProcessorMeta) {
return msg;
}

public toDTO(): PluginDTO {
Expand Down
11 changes: 2 additions & 9 deletions src/plugins/input-plugins/basic-override/index.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
import { MessageProcessorMeta } from '@shared/message-processor';
import { MessageTransport } from '@shared/message-transport';
import { BasePlugin, PluginDTO } from '@shared/plugin-core/base-plugin';

// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface BasicOverrideIcicle extends PluginDTO {}

export default class BasicOverridePlugin extends BasePlugin<BasicOverrideIcicle> {
public process(
msg: NumberArrayWithStatus,
_loopbackTransport: MessageTransport,
_remoteTransport: MessageTransport,
_meta: MessageProcessorMeta
) {
// eslint-disable-next-line no-console
console.log(`${this.title} processing ${msg}`);
public process(msg: NumberArrayWithStatus, _meta: MessageProcessorMeta) {
return msg;
}

public freeze(): BasicOverrideIcicle {
Expand Down
10 changes: 2 additions & 8 deletions src/shared/hardware-config/adapter-device-config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { SupportedDeviceConfig } from './supported-device-config';
import { DeviceConfig } from './device-config';
import { MessageTransport } from '../message-transport';
import { MessageProcessorMeta } from '../message-processor';

export class AdapterDeviceConfig extends DeviceConfig {
Expand Down Expand Up @@ -28,13 +27,8 @@ export class AdapterDeviceConfig extends DeviceConfig {
// return this.child!.bindingAvailable(statusString, number, channel);
// }

process(
msg: NumberArrayWithStatus,
loopbackTransport: MessageTransport,
remoteTransport: MessageTransport,
meta: MessageProcessorMeta
) {
this.child?.process(msg, loopbackTransport, remoteTransport, meta);
process(msg: NumberArrayWithStatus, meta: MessageProcessorMeta) {
return this.child?.process(msg, meta);
}

/**
Expand Down
32 changes: 21 additions & 11 deletions src/shared/hardware-config/device-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import type { BaseIcicle } from '../freezable';
import { Anonymous, getDriver } from '../drivers';

import type { KeyboardDriver } from '../driver-types';
import { MessageTransport } from '../message-transport';
import { MessageProcessor, MessageProcessorMeta } from '../message-processor';

export interface DeviceConfigDTO extends BaseIcicle {
Expand Down Expand Up @@ -105,18 +104,29 @@ export abstract class DeviceConfig<T extends DeviceConfigDTO = DeviceConfigDTO>

public abstract toDTO(): T;

/**
* Processes a MIDI message through a series of plugins, updating the message as it
* passes through each plugin in sequence. If any plugin returns `undefined`, processing
* will be short-circuited, and `undefined` will be returned.
*
* @param {NumberArrayWithStatus} msg - The original MIDI message
* @param {MessageProcessorMeta} meta - Metadata required for processing, including transports and plugin providers.
*/
public process(
msg: NumberArrayWithStatus,
loopbackTransport: MessageTransport,
remoteTransport: MessageTransport,
meta: MessageProcessorMeta
) {
const { pluginProvider } = meta;

this.plugins.forEach((pluginId) => {
pluginProvider
.get(pluginId)
?.process(msg, loopbackTransport, remoteTransport, meta);
});
): NumberArrayWithStatus | undefined {
let message = msg;

for (let i = 0; i < this.plugins.length; i++) {
const plugin = meta.pluginProvider.get(this.plugins[i]);
if (plugin && plugin.on === true) {
const toPropagate = plugin.process(message, meta);
if (toPropagate === undefined) return undefined;
message = toPropagate;
}
}

return message;
}
}
7 changes: 3 additions & 4 deletions src/shared/hardware-config/input-config/base-input-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
MessageProcessor,
MessageProcessorMeta,
} from '@shared/message-processor';
import { MessageTransport } from '@shared/message-transport';
import { BaseIcicle } from '../../freezable';

export interface InputState {}
Expand Down Expand Up @@ -51,8 +50,8 @@ export abstract class BaseInputConfig<T extends InputDTO = InputDTO>

public process(
_msg: NumberArrayWithStatus,
_loopbackTransport: MessageTransport,
_remoteTransport: MessageTransport,
_meta: MessageProcessorMeta
) {}
): NumberArrayWithStatus | undefined {
return _msg;
}
}
19 changes: 4 additions & 15 deletions src/shared/hardware-config/supported-device-config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { MessageProcessorMeta } from '@shared/message-processor';
import { MessageTransport } from '@shared/message-transport';
import { DeviceDriver } from '../driver-types';

import { DeviceConfig, DeviceConfigDTO } from './device-config';
Expand Down Expand Up @@ -59,20 +58,10 @@ export class SupportedDeviceConfig extends DeviceConfig<SupportedDeviceConfigDTO
};
}

public process(
msg: NumberArrayWithStatus,
loopbackTransport: MessageTransport,
remoteTransport: MessageTransport,
meta: MessageProcessorMeta
) {
super.process(msg, loopbackTransport, remoteTransport, meta);

this.getOriginatorInput(msg)?.process(
msg,
loopbackTransport,
remoteTransport,
meta
);
public process(msg: NumberArrayWithStatus, meta: MessageProcessorMeta) {
const message = super.process(msg, meta)!;
const originator = this.getOriginatorInput(msg);
return originator ? originator.process(message, meta) : msg;
}

/**
Expand Down
4 changes: 1 addition & 3 deletions src/shared/message-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ export type MessageProcessorMeta = {
export type MessageProcessor = {
process: (
msg: NumberArrayWithStatus,
loopbackTransport: MessageTransport,
remoteTransport: MessageTransport,
meta: MessageProcessorMeta
) => void;
) => NumberArrayWithStatus | undefined;
};
5 changes: 1 addition & 4 deletions src/shared/plugin-core/base-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import type {
MessageProcessor,
MessageProcessorMeta,
} from '@shared/message-processor';
import type { MessageTransport } from '@shared/message-transport';
import { generateId } from './plugin-utils';

export interface PluginDTO {
Expand Down Expand Up @@ -56,10 +55,8 @@ export abstract class BasePlugin<T extends PluginDTO = PluginDTO>

public abstract process(
msg: NumberArrayWithStatus,
loopbackTransport: MessageTransport,
remoteTransport: MessageTransport,
meta: MessageProcessorMeta
): void;
): NumberArrayWithStatus | undefined;

public abstract get applicableDeviceTypes(): (
| 'supported'
Expand Down

0 comments on commit 2aa88cf

Please sign in to comment.