@djodjonx/x32-simulator 0.0.1 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@djodjonx/x32-simulator",
3
- "version": "0.0.1",
3
+ "version": "0.0.3",
4
4
  "description": "X32 Simulator",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -27,6 +27,10 @@ function getLocalIp(): string {
27
27
  return '127.0.0.1';
28
28
  }
29
29
 
30
+ /**
31
+ * The core service that manages the X32 simulation, including networking,
32
+ * state management, and OSC message handling.
33
+ */
30
34
  export class SimulationService {
31
35
  private subscriptionManager: SubscriptionManager;
32
36
  private messageHandler: OscMessageHandler;
@@ -43,6 +47,17 @@ export class SimulationService {
43
47
  private updateInterval: NodeJS.Timeout | null = null;
44
48
  private cleanupInterval: NodeJS.Timeout | null = null;
45
49
 
50
+ /**
51
+ * Creates a new SimulationService instance.
52
+ * @param gateway - The network gateway for OSC communication.
53
+ * @param logger - The logger service.
54
+ * @param stateRepo - The repository managing the mixer state.
55
+ * @param schemaRegistry - The registry for X32 OSC schema.
56
+ * @param port - UDP port to listen on (default 10023).
57
+ * @param ip - IP address to bind to (default '0.0.0.0').
58
+ * @param name - Reported console name.
59
+ * @param model - Reported console model.
60
+ */
46
61
  constructor(
47
62
  private gateway: INetworkGateway,
48
63
  private logger: ILogger,
@@ -96,6 +111,9 @@ export class SimulationService {
96
111
  this.gateway.onPacket((packet, source) => this.processPacket.execute(packet, source));
97
112
  }
98
113
 
114
+ /**
115
+ * Starts the simulator server and internal loops.
116
+ */
99
117
  public async start(): Promise<void> {
100
118
  await this.gateway.start(this.port, this.ip);
101
119
 
@@ -110,12 +128,18 @@ export class SimulationService {
110
128
  }, 100);
111
129
  }
112
130
 
131
+ /**
132
+ * Stops the simulator server and stops all internal loops.
133
+ */
113
134
  public async stop(): Promise<void> {
114
135
  if (this.updateInterval) clearInterval(this.updateInterval);
115
136
  if (this.cleanupInterval) clearInterval(this.cleanupInterval);
116
137
  await this.gateway.stop();
117
138
  }
118
139
 
140
+ /**
141
+ * Resets the mixer state to default values.
142
+ */
119
143
  public resetState() {
120
144
  this.stateRepo.reset();
121
145
  }
@@ -3,7 +3,7 @@ import { X32Node } from '../models/X32Node';
3
3
 
4
4
  /**
5
5
  * Manages the internal "Digital Twin" state of the X32 console.
6
- * acts as a single source of truth for all parameters.
6
+ * It acts as a single source of truth for all parameters.
7
7
  * Emits 'change' events whenever a value is updated.
8
8
  */
9
9
  export class X32State extends EventEmitter {
@@ -1,6 +1,5 @@
1
1
  /**
2
- * Represents an X32 OSC Address path.
3
- * parsing logic and component extraction.
2
+ * Represents an X32 OSC Address path, providing parsing logic and component extraction.
4
3
  */
5
4
  export class X32Address {
6
5
  private readonly _path: string;
@@ -24,11 +23,6 @@ export class X32Address {
24
23
  return this._path;
25
24
  }
26
25
 
27
- /**
28
- * Gets the full string representation of the path.
29
- return this._path;
30
- }
31
-
32
26
  /**
33
27
  * Gets the root category (e.g., "ch", "bus", "config").
34
28
  * @returns The first segment of the path.
@@ -75,4 +69,4 @@ export class X32Address {
75
69
  }
76
70
  return pattern.test(this._path);
77
71
  }
78
- }
72
+ }
@@ -43,16 +43,6 @@ export interface OscMsg {
43
43
  args: OscArgumentValue[];
44
44
  }
45
45
 
46
- /**
47
- * Metadata for a single state node.
48
- */
49
- export interface X32Node {
50
- /** Value type (float, int, or string). */
51
- type: 'f' | 'i' | 's';
52
- /** Default value for reset. */
53
- default: number | string;
54
- }
55
-
56
46
  /**
57
47
  * Represents an active subscription from a client.
58
48
  */
@@ -3,17 +3,33 @@ import { X32State } from '../../domain/entities/X32State';
3
3
  import { ILogger, LogCategory } from '../../domain/ports/ILogger';
4
4
  import { SchemaRegistry } from '../../domain/services/SchemaRegistry';
5
5
 
6
+ /**
7
+ * In-memory implementation of the state repository.
8
+ * Stores the mixer state in volatile memory during the simulator's execution.
9
+ */
6
10
  export class InMemoryStateRepository implements IStateRepository {
7
11
  private state: X32State;
8
12
 
13
+ /**
14
+ * Creates a new InMemoryStateRepository.
15
+ * @param logger - Logger service.
16
+ * @param schemaRegistry - Registry providing the initial state schema.
17
+ */
9
18
  constructor(private logger: ILogger, schemaRegistry: SchemaRegistry) {
10
19
  this.state = new X32State(schemaRegistry.getSchema());
11
20
  }
12
21
 
22
+ /**
23
+ * Returns the current mixer state instance.
24
+ * @returns The X32State entity.
25
+ */
13
26
  public getState(): X32State {
14
27
  return this.state;
15
28
  }
16
29
 
30
+ /**
31
+ * Resets the entire state to its default values.
32
+ */
17
33
  public reset(): void {
18
34
  this.logger.info(LogCategory.SYSTEM, 'Resetting state to defaults');
19
35
  this.state.reset();
@@ -4,19 +4,38 @@ import { OscPacket, RemoteClient } from '../../domain/models/types';
4
4
  import { OscCodec } from '../mappers/OscCodec';
5
5
  import { ILogger, LogCategory } from '../../domain/ports/ILogger';
6
6
 
7
+ /**
8
+ * Node.js UDP implementation of the network gateway.
9
+ * Handles the low-level socket communication and OSC packet routing.
10
+ */
7
11
  export class UdpNetworkGateway implements INetworkGateway {
8
12
  private socket: dgram.Socket;
9
13
  private isRunning = false;
10
14
  private packetCallback: ((packet: OscPacket, source: RemoteClient) => void) | null = null;
11
15
 
16
+ /**
17
+ * Creates a new UdpNetworkGateway.
18
+ * @param logger - Logger service.
19
+ * @param codec - OSC codec for encoding/decoding.
20
+ */
12
21
  constructor(private logger: ILogger, private codec: OscCodec) {
13
22
  this.socket = dgram.createSocket('udp4');
14
23
  }
15
24
 
25
+ /**
26
+ * Registers a callback to be executed whenever a new OSC packet arrives.
27
+ * @param callback - Function called with decoded packet and source info.
28
+ */
16
29
  public onPacket(callback: (packet: OscPacket, source: RemoteClient) => void): void {
17
30
  this.packetCallback = callback;
18
31
  }
19
32
 
33
+ /**
34
+ * Starts the UDP server on the specified port and IP.
35
+ * @param port - UDP port.
36
+ * @param ip - IP address to bind to.
37
+ * @returns Promise resolving when the socket is bound.
38
+ */
20
39
  public start(port: number, ip: string): Promise<void> {
21
40
  return new Promise((resolve, reject) => {
22
41
  this.socket.on('error', (err) => {
@@ -44,6 +63,10 @@ export class UdpNetworkGateway implements INetworkGateway {
44
63
  });
45
64
  }
46
65
 
66
+ /**
67
+ * Shuts down the UDP server.
68
+ * @returns Promise resolving when the socket is closed.
69
+ */
47
70
  public stop(): Promise<void> {
48
71
  return new Promise((resolve) => {
49
72
  if (!this.isRunning) return resolve();
@@ -55,6 +78,12 @@ export class UdpNetworkGateway implements INetworkGateway {
55
78
  });
56
79
  }
57
80
 
81
+ /**
82
+ * Sends an OSC message to a specific remote client.
83
+ * @param target - Target client info (IP/port).
84
+ * @param address - OSC address pattern.
85
+ * @param args - Array of arguments.
86
+ */
58
87
  public send(target: RemoteClient, address: string, args: any[]): void {
59
88
  const buf = this.codec.encode(address, args);
60
89
  const cat = address.startsWith('/meters') ? LogCategory.METER : LogCategory.OSC_OUT;
@@ -1,9 +1,139 @@
1
+ /**
2
+ * @module x32-simulator
3
+ * @description
4
+ * The main entry point for the X32 Simulator library.
5
+ * This module exports all the necessary classes, interfaces, and types required to
6
+ * integrate the X32 simulator into your own Node.js applications or testing frameworks.
7
+ *
8
+ * It provides access to:
9
+ * - The core `SimulationService` for running the simulator.
10
+ * - Domain entities like `X32State` and `SubscriptionManager`.
11
+ * - Infrastructure components for networking and logging.
12
+ * - All OSC-related models and types.
13
+ *
14
+ * @example
15
+ * ```typescript
16
+ * import { SimulationService, ConsoleLogger, InMemoryStateRepository, SchemaRegistry, SchemaFactory, UdpNetworkGateway, OscCodec } from '@djodjonx/x32-simulator';
17
+ *
18
+ * // 1. Setup Dependencies
19
+ * const logger = ConsoleLogger.getInstance();
20
+ * const schemaRegistry = new SchemaRegistry(new SchemaFactory());
21
+ * const codec = new OscCodec(schemaRegistry);
22
+ * const gateway = new UdpNetworkGateway(logger, codec);
23
+ * const repository = new InMemoryStateRepository(logger, schemaRegistry);
24
+ *
25
+ * // 2. Initialize Service
26
+ * const simulator = new SimulationService(
27
+ * gateway,
28
+ * logger,
29
+ * repository,
30
+ * schemaRegistry,
31
+ * 10023, // Port
32
+ * '127.0.0.1' // Host
33
+ * );
34
+ *
35
+ * // 3. Start
36
+ * await simulator.start();
37
+ * ```
38
+ */
39
+
40
+ // Application layer
41
+ /**
42
+ * The main service class that orchestrates the simulation logic.
43
+ */
1
44
  export { SimulationService } from '../../application/use-cases/SimulationService';
45
+
46
+ // Domain entities
47
+ /**
48
+ * Represents the complete state of the X32 console, including all parameters and faders.
49
+ */
50
+ export { X32State } from '../../domain/entities/X32State';
51
+
52
+ /**
53
+ * Manages client subscriptions to OSC updates (e.g. /xremote, /subscribe).
54
+ */
55
+ export { SubscriptionManager } from '../../domain/entities/SubscriptionManager';
56
+
57
+ // Domain models
58
+ /**
59
+ * Represents a parsed OSC message.
60
+ */
61
+ export { OscMessage } from '../../domain/models/OscMessage';
62
+
63
+ /**
64
+ * Utility class for parsing and manipulating X32 OSC address strings.
65
+ */
66
+ export { X32Address } from '../../domain/models/X32Address';
67
+
68
+ /**
69
+ * Represents a single parameter node (fader, button, knob) in the X32 state.
70
+ */
71
+ export { X32Node } from '../../domain/models/X32Node';
72
+
73
+ /**
74
+ * Handles meter data generation and binary blob creation.
75
+ */
76
+ export { MeterData } from '../../domain/models/MeterData';
77
+
78
+ /**
79
+ * Configuration constant defining the number of meters per meter bank.
80
+ */
81
+ export { METER_COUNTS } from '../../domain/models/MeterConfig';
82
+
83
+ /**
84
+ * Exports all shared TypeScript types and interfaces used across the domain.
85
+ */
86
+ export * from '../../domain/models/types';
87
+
88
+ // Domain ports (interfaces)
89
+ /**
90
+ * Interface for the network gateway (UDP communication).
91
+ */
2
92
  export type { INetworkGateway } from '../../domain/ports/INetworkGateway';
93
+
94
+ /**
95
+ * Interface for the logger.
96
+ */
3
97
  export type { ILogger, LogData } from '../../domain/ports/ILogger';
98
+
99
+ /**
100
+ * Enum for log categories.
101
+ */
4
102
  export { LogCategory } from '../../domain/ports/ILogger';
103
+
104
+ /**
105
+ * Interface for the state repository (persistence/storage).
106
+ */
5
107
  export type { IStateRepository } from '../../domain/ports/IStateRepository';
108
+
109
+ // Infrastructure layer
110
+ /**
111
+ * In-memory implementation of the state repository.
112
+ */
6
113
  export { InMemoryStateRepository } from '../../infrastructure/repositories/InMemoryStateRepository';
114
+
115
+ /**
116
+ * Console-based implementation of the logger.
117
+ */
7
118
  export { ConsoleLogger } from '../../infrastructure/services/ConsoleLogger';
119
+
120
+ /**
121
+ * Node.js UDP implementation of the network gateway.
122
+ */
8
123
  export { UdpNetworkGateway } from '../../infrastructure/services/UdpNetworkGateway';
9
- export * from '../../domain/models/types';
124
+
125
+ /**
126
+ * OSC Codec for encoding and decoding X32-specific OSC packets.
127
+ */
128
+ export { OscCodec } from '../../infrastructure/mappers/OscCodec';
129
+
130
+ // Domain services
131
+ /**
132
+ * Factory service for creating the initial X32 state schema.
133
+ */
134
+ export { SchemaFactory } from '../../domain/services/SchemaFactory';
135
+
136
+ /**
137
+ * Registry service for looking up X32 nodes and definitions.
138
+ */
139
+ export { SchemaRegistry } from '../../domain/services/SchemaRegistry';