@9c5s/node-tcnet 0.5.1 → 0.6.0

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/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } var _class; var _class2; var _class3; var _class4; var _class5; var _class6; var _class7;var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } var _class; var _class2; var _class3; var _class4; var _class5; var _class6; var _class7; var _class8; var _class9; var _class10; var _class11; var _class12; var _class13;var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
2
2
  get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
3
3
  }) : x)(function(x) {
4
4
  if (typeof require !== "undefined") return require.apply(this, arguments);
@@ -360,6 +360,163 @@ var TCNetDataPacketMetadata = (_class5 = class extends TCNetDataPacket {construc
360
360
  return 548;
361
361
  }
362
362
  }, _class5);
363
+ var TCNetDataPacketCUE = (_class6 = class extends TCNetDataPacket {constructor(...args5) { super(...args5); _class6.prototype.__init7.call(this); }
364
+ __init7() {this.data = null}
365
+ read() {
366
+ const loopInTime = this.buffer.readUInt32LE(42);
367
+ const loopOutTime = this.buffer.readUInt32LE(46);
368
+ const cues = [];
369
+ const cueStart = 50;
370
+ for (let i = 0; i < 18; i++) {
371
+ const offset = cueStart + i * 22;
372
+ if (offset + 22 > this.buffer.length) break;
373
+ const type = this.buffer.readUInt8(offset);
374
+ if (type === 0) continue;
375
+ cues.push({
376
+ index: i + 1,
377
+ type,
378
+ inTime: this.buffer.readUInt32LE(offset + 2),
379
+ outTime: this.buffer.readUInt32LE(offset + 6),
380
+ color: {
381
+ r: this.buffer.readUInt8(offset + 11),
382
+ g: this.buffer.readUInt8(offset + 12),
383
+ b: this.buffer.readUInt8(offset + 13)
384
+ }
385
+ });
386
+ }
387
+ this.data = { loopInTime, loopOutTime, cues };
388
+ }
389
+ write() {
390
+ throw new Error("not supported!");
391
+ }
392
+ length() {
393
+ return 436;
394
+ }
395
+ }, _class6);
396
+ function parseWaveformBars(source, dataStart, maxBytes) {
397
+ const bars = [];
398
+ const end = maxBytes !== void 0 ? Math.min(dataStart + maxBytes, source.length) : source.length;
399
+ const safeEnd = dataStart + (end - dataStart & ~1);
400
+ for (let i = dataStart; i < safeEnd; i += 2) {
401
+ bars.push({
402
+ level: source.readUInt8(i),
403
+ color: source.readUInt8(i + 1)
404
+ });
405
+ }
406
+ return bars;
407
+ }
408
+ var TCNetDataPacketSmallWaveForm = (_class7 = class extends TCNetDataPacket {constructor(...args6) { super(...args6); _class7.prototype.__init8.call(this); }
409
+ __init8() {this.data = null}
410
+ read() {
411
+ this.data = { bars: parseWaveformBars(this.buffer, 42, 2400) };
412
+ }
413
+ write() {
414
+ throw new Error("not supported!");
415
+ }
416
+ length() {
417
+ return 2442;
418
+ }
419
+ }, _class7);
420
+ var TCNetDataPacketMixer = (_class8 = class extends TCNetDataPacket {constructor(...args7) { super(...args7); _class8.prototype.__init9.call(this); }
421
+ __init9() {this.data = null}
422
+ read() {
423
+ if (this.buffer.length < 259) {
424
+ return;
425
+ }
426
+ const parseChannel = (offset) => ({
427
+ sourceSelect: this.buffer.readUInt8(offset),
428
+ audioLevel: this.buffer.readUInt8(offset + 1),
429
+ faderLevel: this.buffer.readUInt8(offset + 2),
430
+ trimLevel: this.buffer.readUInt8(offset + 3),
431
+ compLevel: this.buffer.readUInt8(offset + 4),
432
+ eqHi: this.buffer.readUInt8(offset + 5),
433
+ eqHiMid: this.buffer.readUInt8(offset + 6),
434
+ eqLowMid: this.buffer.readUInt8(offset + 7),
435
+ eqLow: this.buffer.readUInt8(offset + 8),
436
+ filterColor: this.buffer.readUInt8(offset + 9),
437
+ send: this.buffer.readUInt8(offset + 10),
438
+ cueA: this.buffer.readUInt8(offset + 11),
439
+ cueB: this.buffer.readUInt8(offset + 12),
440
+ crossfaderAssign: this.buffer.readUInt8(offset + 13)
441
+ });
442
+ this.data = {
443
+ mixerId: this.buffer.readUInt8(25),
444
+ mixerType: this.buffer.readUInt8(26),
445
+ mixerName: this.buffer.slice(29, 45).toString("ascii").replace(/\0.*$/g, ""),
446
+ masterAudioLevel: this.buffer.readUInt8(61),
447
+ masterFaderLevel: this.buffer.readUInt8(62),
448
+ masterFilter: this.buffer.readUInt8(69),
449
+ masterIsolatorOn: this.buffer.readUInt8(74) === 1,
450
+ masterIsolatorHi: this.buffer.readUInt8(75),
451
+ masterIsolatorMid: this.buffer.readUInt8(76),
452
+ masterIsolatorLow: this.buffer.readUInt8(77),
453
+ filterHpf: this.buffer.readUInt8(79),
454
+ filterLpf: this.buffer.readUInt8(80),
455
+ filterResonance: this.buffer.readUInt8(81),
456
+ crossFader: this.buffer.readUInt8(99),
457
+ crossFaderCurve: this.buffer.readUInt8(98),
458
+ channelFaderCurve: this.buffer.readUInt8(97),
459
+ beatFxOn: this.buffer.readUInt8(100) === 1,
460
+ beatFxSelect: this.buffer.readUInt8(103),
461
+ beatFxLevelDepth: this.buffer.readUInt8(101),
462
+ beatFxChannelSelect: this.buffer.readUInt8(102),
463
+ headphonesALevel: this.buffer.readUInt8(108),
464
+ headphonesBLevel: this.buffer.readUInt8(110),
465
+ boothLevel: this.buffer.readUInt8(112),
466
+ channels: [125, 149, 173, 197, 221, 245].map(parseChannel)
467
+ };
468
+ }
469
+ write() {
470
+ throw new Error("not supported!");
471
+ }
472
+ length() {
473
+ return 270;
474
+ }
475
+ }, _class8);
476
+ var TCNetDataPacketBeatGrid = (_class9 = class extends TCNetDataPacket {constructor(...args8) { super(...args8); _class9.prototype.__init10.call(this); }
477
+ __init10() {this.data = null}
478
+ read() {
479
+ this.readFromOffset(42);
480
+ }
481
+ // アセンブル済みバッファからのパース用
482
+ readAssembled(assembled) {
483
+ this.readFromOffset(0, assembled);
484
+ }
485
+ readFromOffset(dataStart, buf) {
486
+ const source = _nullishCoalesce(buf, () => ( this.buffer));
487
+ const entries = [];
488
+ for (let offset = dataStart; offset + 8 <= source.length; offset += 8) {
489
+ const beatNumber = source.readUInt16LE(offset);
490
+ const beatType = source.readUInt8(offset + 2);
491
+ const timestampMs = source.readUInt32LE(offset + 4);
492
+ if (beatNumber === 0 && timestampMs === 0) continue;
493
+ entries.push({ beatNumber, beatType, timestampMs });
494
+ }
495
+ this.data = { entries };
496
+ }
497
+ write() {
498
+ throw new Error("not supported!");
499
+ }
500
+ length() {
501
+ return 2442;
502
+ }
503
+ }, _class9);
504
+ var TCNetDataPacketBigWaveForm = (_class10 = class extends TCNetDataPacket {constructor(...args9) { super(...args9); _class10.prototype.__init11.call(this); }
505
+ __init11() {this.data = null}
506
+ read() {
507
+ this.data = { bars: parseWaveformBars(this.buffer, 42) };
508
+ }
509
+ // アセンブル済みバッファからのパース用
510
+ readAssembled(assembled) {
511
+ this.data = { bars: parseWaveformBars(assembled, 0) };
512
+ }
513
+ write() {
514
+ throw new Error("not supported!");
515
+ }
516
+ length() {
517
+ return -1;
518
+ }
519
+ }, _class10);
363
520
  var TCNetPackets = {
364
521
  [2 /* OptIn */]: TCNetOptInPacket,
365
522
  [3 /* OptOut */]: TCNetOptOutPacket,
@@ -385,54 +542,80 @@ var TCNetPackets = {
385
542
  var TCNetDataPackets = {
386
543
  [2 /* MetricsData */]: TCNetDataPacketMetrics,
387
544
  [4 /* MetaData */]: TCNetDataPacketMetadata,
388
- [8 /* BeatGridData */]: null,
389
- // not yet implemented
390
- [12 /* CUEData */]: null,
391
- // not yet implemented
392
- [16 /* SmallWaveFormData */]: null,
393
- // not yet implemented
394
- [32 /* BigWaveFormData */]: null,
395
- // not yet implemented
396
- [150 /* MixerData */]: null
397
- // not yet implemented
545
+ [8 /* BeatGridData */]: TCNetDataPacketBeatGrid,
546
+ [12 /* CUEData */]: TCNetDataPacketCUE,
547
+ [16 /* SmallWaveFormData */]: TCNetDataPacketSmallWaveForm,
548
+ [32 /* BigWaveFormData */]: TCNetDataPacketBigWaveForm,
549
+ [150 /* MixerData */]: TCNetDataPacketMixer
398
550
  };
399
551
 
552
+ // src/multi-packet.ts
553
+ var MultiPacketAssembler = (_class11 = class {constructor() { _class11.prototype.__init12.call(this);_class11.prototype.__init13.call(this); }
554
+ __init12() {this.packets = /* @__PURE__ */ new Map()}
555
+ __init13() {this.totalPackets = 0}
556
+ // パケットを追加し、全パケットが揃ったら true を返す
557
+ add(buffer) {
558
+ if (buffer.length < 42) return false;
559
+ const newTotalPackets = buffer.readUInt32LE(30);
560
+ if (newTotalPackets === 0) return false;
561
+ if (this.totalPackets > 0 && newTotalPackets !== this.totalPackets) return false;
562
+ this.totalPackets = newTotalPackets;
563
+ const packetNo = buffer.readUInt32LE(34);
564
+ const clusterSize = buffer.readUInt32LE(38);
565
+ const dataStart = 42;
566
+ if (dataStart + clusterSize > buffer.length) return false;
567
+ this.packets.set(packetNo, Buffer.from(buffer.slice(dataStart, dataStart + clusterSize)));
568
+ return this.packets.size >= this.totalPackets;
569
+ }
570
+ // packetNo 順にソートしてデータを結合する
571
+ assemble() {
572
+ const sorted = [...this.packets.entries()].sort((a, b) => a[0] - b[0]);
573
+ return Buffer.concat(sorted.map(([, buf]) => buf));
574
+ }
575
+ // 状態をリセットする
576
+ reset() {
577
+ this.packets.clear();
578
+ this.totalPackets = 0;
579
+ }
580
+ }, _class11);
581
+
400
582
  // src/tcnet.ts
401
583
  var EventEmitter = __require("events");
402
584
  var TCNET_BROADCAST_PORT = 6e4;
403
585
  var TCNET_TIMESTAMP_PORT = 60001;
404
- var TCNetConfiguration = (_class6 = class {constructor() { _class6.prototype.__init7.call(this);_class6.prototype.__init8.call(this);_class6.prototype.__init9.call(this);_class6.prototype.__init10.call(this);_class6.prototype.__init11.call(this);_class6.prototype.__init12.call(this);_class6.prototype.__init13.call(this);_class6.prototype.__init14.call(this);_class6.prototype.__init15.call(this);_class6.prototype.__init16.call(this);_class6.prototype.__init17.call(this); }
405
- __init7() {this.logger = null}
406
- __init8() {this.unicastPort = 65023}
407
- __init9() {this.applicationCode = 65535}
408
- __init10() {this.nodeId = Math.floor(Math.random() * 65535)}
409
- __init11() {this.nodeName = "TCNET.JS"}
410
- __init12() {this.vendorName = "CHDXD1"}
411
- __init13() {this.appName = "NODE-TCNET"}
412
- __init14() {this.broadcastInterface = null}
413
- __init15() {this.broadcastAddress = "255.255.255.255"}
414
- __init16() {this.broadcastListeningAddress = ""}
415
- __init17() {this.requestTimeout = 2e3}
416
- }, _class6);
586
+ var MULTI_PACKET_TYPES = /* @__PURE__ */ new Set([32 /* BigWaveFormData */, 8 /* BeatGridData */]);
587
+ var TCNetConfiguration = (_class12 = class {constructor() { _class12.prototype.__init14.call(this);_class12.prototype.__init15.call(this);_class12.prototype.__init16.call(this);_class12.prototype.__init17.call(this);_class12.prototype.__init18.call(this);_class12.prototype.__init19.call(this);_class12.prototype.__init20.call(this);_class12.prototype.__init21.call(this);_class12.prototype.__init22.call(this);_class12.prototype.__init23.call(this);_class12.prototype.__init24.call(this); }
588
+ __init14() {this.logger = null}
589
+ __init15() {this.unicastPort = 65023}
590
+ __init16() {this.applicationCode = 65535}
591
+ __init17() {this.nodeId = Math.floor(Math.random() * 65535)}
592
+ __init18() {this.nodeName = "TCNET.JS"}
593
+ __init19() {this.vendorName = "CHDXD1"}
594
+ __init20() {this.appName = "NODE-TCNET"}
595
+ __init21() {this.broadcastInterface = null}
596
+ __init22() {this.broadcastAddress = "255.255.255.255"}
597
+ __init23() {this.broadcastListeningAddress = ""}
598
+ __init24() {this.requestTimeout = 2e3}
599
+ }, _class12);
417
600
  var closeSocket = (socket) => new Promise((resolve) => socket.close(() => resolve()));
418
- var TCNetClient = (_class7 = class extends EventEmitter {
601
+ var TCNetClient = (_class13 = class extends EventEmitter {
419
602
 
420
603
 
421
604
 
422
605
 
423
606
 
424
- __init18() {this.seq = 0}
425
- __init19() {this.uptime = 0}
426
- __init20() {this.connected = false}
427
- __init21() {this.connectedHandler = null}
428
- __init22() {this.requests = /* @__PURE__ */ new Map()}
607
+ __init25() {this.seq = 0}
608
+ __init26() {this.uptime = 0}
609
+ __init27() {this.connected = false}
610
+ __init28() {this.connectedHandler = null}
611
+ __init29() {this.requests = /* @__PURE__ */ new Map()}
429
612
 
430
613
  /**
431
614
  *
432
615
  * @param config configuration for TCNet access
433
616
  */
434
617
  constructor(config) {
435
- super();_class7.prototype.__init18.call(this);_class7.prototype.__init19.call(this);_class7.prototype.__init20.call(this);_class7.prototype.__init21.call(this);_class7.prototype.__init22.call(this);;
618
+ super();_class13.prototype.__init25.call(this);_class13.prototype.__init26.call(this);_class13.prototype.__init27.call(this);_class13.prototype.__init28.call(this);_class13.prototype.__init29.call(this);;
436
619
  this.config = config || new TCNetConfiguration();
437
620
  if (this.config.broadcastInterface && this.config.broadcastAddress == "255.255.255.255") {
438
621
  this.config.broadcastAddress = interfaceAddress(this.config.broadcastInterface);
@@ -589,15 +772,44 @@ var TCNetClient = (_class7 = class extends EventEmitter {
589
772
  dataPacket.dataType = packet.dataType;
590
773
  dataPacket.layer = packet.layer;
591
774
  dataPacket.read();
592
- if (this.connected) {
593
- this.emit("data", dataPacket);
594
- }
595
775
  const key = `${dataPacket.dataType}-${dataPacket.layer}`;
596
776
  const pendingRequest = this.requests.get(key);
597
- if (pendingRequest) {
598
- this.requests.delete(key);
599
- clearTimeout(pendingRequest.timeout);
600
- pendingRequest.resolve(dataPacket);
777
+ if (pendingRequest && pendingRequest.assembler) {
778
+ const complete = pendingRequest.assembler.add(msg);
779
+ if (complete) {
780
+ const assembled = pendingRequest.assembler.assemble();
781
+ const finalPacket = new dataPacketClass();
782
+ finalPacket.buffer = msg;
783
+ finalPacket.header = mgmtHeader;
784
+ finalPacket.dataType = dataPacket.dataType;
785
+ finalPacket.layer = dataPacket.layer;
786
+ if ("readAssembled" in finalPacket && typeof finalPacket.readAssembled === "function") {
787
+ finalPacket.readAssembled(assembled);
788
+ }
789
+ this.requests.delete(key);
790
+ clearTimeout(pendingRequest.timeout);
791
+ if (this.connected) {
792
+ this.emit("data", finalPacket);
793
+ }
794
+ pendingRequest.resolve(finalPacket);
795
+ } else {
796
+ clearTimeout(pendingRequest.timeout);
797
+ pendingRequest.timeout = setTimeout(() => {
798
+ if (this.requests.delete(key)) {
799
+ _optionalChain([pendingRequest, 'access', _23 => _23.assembler, 'optionalAccess', _24 => _24.reset, 'call', _25 => _25()]);
800
+ pendingRequest.reject(new Error("Timeout while requesting data"));
801
+ }
802
+ }, this.config.requestTimeout);
803
+ }
804
+ } else {
805
+ if (this.connected) {
806
+ this.emit("data", dataPacket);
807
+ }
808
+ if (pendingRequest) {
809
+ this.requests.delete(key);
810
+ clearTimeout(pendingRequest.timeout);
811
+ pendingRequest.resolve(dataPacket);
812
+ }
601
813
  }
602
814
  }
603
815
  } else if (packet instanceof TCNetOptInPacket) {
@@ -625,7 +837,7 @@ var TCNetClient = (_class7 = class extends EventEmitter {
625
837
  const mgmtHeader = new TCNetManagementHeader(msg);
626
838
  mgmtHeader.read();
627
839
  if (mgmtHeader.messageType !== 254 /* Time */) {
628
- _optionalChain([this, 'access', _23 => _23.log, 'optionalAccess', _24 => _24.debug, 'call', _25 => _25("Received non Time packet on Time port")]);
840
+ _optionalChain([this, 'access', _26 => _26.log, 'optionalAccess', _27 => _27.debug, 'call', _28 => _28("Received non Time packet on Time port")]);
629
841
  return;
630
842
  }
631
843
  const packet = this.parsePacket(mgmtHeader);
@@ -725,11 +937,18 @@ var TCNetClient = (_class7 = class extends EventEmitter {
725
937
  request.layer = layer + 1;
726
938
  const key = `${dataType}-${layer}`;
727
939
  const timeout = setTimeout(() => {
728
- if (this.requests.delete(key)) {
940
+ const req = this.requests.get(key);
941
+ if (req && this.requests.delete(key)) {
942
+ _optionalChain([req, 'access', _29 => _29.assembler, 'optionalAccess', _30 => _30.reset, 'call', _31 => _31()]);
729
943
  reject(new Error("Timeout while requesting data"));
730
944
  }
731
945
  }, this.config.requestTimeout);
732
- this.requests.set(key, { resolve, timeout });
946
+ this.requests.set(key, {
947
+ resolve,
948
+ reject,
949
+ timeout,
950
+ assembler: MULTI_PACKET_TYPES.has(dataType) ? new MultiPacketAssembler() : void 0
951
+ });
733
952
  this.sendServer(request).catch((err) => {
734
953
  if (this.requests.delete(key)) {
735
954
  clearTimeout(timeout);
@@ -738,7 +957,13 @@ var TCNetClient = (_class7 = class extends EventEmitter {
738
957
  });
739
958
  });
740
959
  }
741
- }, _class7);
960
+ }, _class13);
961
+
962
+
963
+
964
+
965
+
966
+
742
967
 
743
968
 
744
969
 
@@ -761,4 +986,4 @@ var TCNetClient = (_class7 = class extends EventEmitter {
761
986
 
762
987
 
763
988
 
764
- exports.NodeType = NodeType; exports.TCNetClient = TCNetClient; exports.TCNetConfiguration = TCNetConfiguration; exports.TCNetDataPacket = TCNetDataPacket; exports.TCNetDataPacketMetadata = TCNetDataPacketMetadata; exports.TCNetDataPacketMetrics = TCNetDataPacketMetrics; exports.TCNetDataPacketType = TCNetDataPacketType; exports.TCNetDataPackets = TCNetDataPackets; exports.TCNetLayerStatus = TCNetLayerStatus; exports.TCNetLayerSyncMaster = TCNetLayerSyncMaster; exports.TCNetManagementHeader = TCNetManagementHeader; exports.TCNetMessageType = TCNetMessageType; exports.TCNetOptInPacket = TCNetOptInPacket; exports.TCNetOptOutPacket = TCNetOptOutPacket; exports.TCNetPacket = TCNetPacket; exports.TCNetPackets = TCNetPackets; exports.TCNetRequestPacket = TCNetRequestPacket; exports.TCNetStatusPacket = TCNetStatusPacket; exports.TCNetTimePacket = TCNetTimePacket; exports.TCNetTimecode = TCNetTimecode; exports.TCNetTimecodeState = TCNetTimecodeState;
989
+ exports.MultiPacketAssembler = MultiPacketAssembler; exports.NodeType = NodeType; exports.TCNetClient = TCNetClient; exports.TCNetConfiguration = TCNetConfiguration; exports.TCNetDataPacket = TCNetDataPacket; exports.TCNetDataPacketBeatGrid = TCNetDataPacketBeatGrid; exports.TCNetDataPacketBigWaveForm = TCNetDataPacketBigWaveForm; exports.TCNetDataPacketCUE = TCNetDataPacketCUE; exports.TCNetDataPacketMetadata = TCNetDataPacketMetadata; exports.TCNetDataPacketMetrics = TCNetDataPacketMetrics; exports.TCNetDataPacketMixer = TCNetDataPacketMixer; exports.TCNetDataPacketSmallWaveForm = TCNetDataPacketSmallWaveForm; exports.TCNetDataPacketType = TCNetDataPacketType; exports.TCNetDataPackets = TCNetDataPackets; exports.TCNetLayerStatus = TCNetLayerStatus; exports.TCNetLayerSyncMaster = TCNetLayerSyncMaster; exports.TCNetManagementHeader = TCNetManagementHeader; exports.TCNetMessageType = TCNetMessageType; exports.TCNetOptInPacket = TCNetOptInPacket; exports.TCNetOptOutPacket = TCNetOptOutPacket; exports.TCNetPacket = TCNetPacket; exports.TCNetPackets = TCNetPackets; exports.TCNetRequestPacket = TCNetRequestPacket; exports.TCNetStatusPacket = TCNetStatusPacket; exports.TCNetTimePacket = TCNetTimePacket; exports.TCNetTimecode = TCNetTimecode; exports.TCNetTimecodeState = TCNetTimecodeState;