@blibliki/engine 0.1.26 → 0.3.1

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.
Files changed (121) hide show
  1. package/README.md +252 -76
  2. package/dist/index.cjs +2 -0
  3. package/dist/index.cjs.map +1 -0
  4. package/dist/index.d.cts +765 -0
  5. package/dist/index.d.ts +765 -0
  6. package/dist/index.js +2 -0
  7. package/dist/index.js.map +1 -0
  8. package/package.json +19 -31
  9. package/src/Engine.ts +158 -177
  10. package/src/core/IO/AudioIO.ts +72 -0
  11. package/src/core/IO/Base.ts +118 -0
  12. package/src/core/IO/Collection.ts +123 -47
  13. package/src/core/IO/MidiIO.ts +43 -0
  14. package/src/core/IO/PolyAudioIO.ts +115 -0
  15. package/src/core/IO/index.ts +7 -61
  16. package/src/core/Note/frequencyTable.ts +144 -144
  17. package/src/core/Note/index.ts +49 -59
  18. package/src/core/Route.ts +79 -0
  19. package/src/core/Timing/Scheduler.ts +37 -0
  20. package/src/core/Timing/Time.ts +103 -0
  21. package/src/core/Timing/Transport.ts +104 -0
  22. package/src/core/Timing/index.ts +16 -0
  23. package/src/core/index.ts +36 -0
  24. package/src/core/midi/{ComputerKeyboardInput.ts → ComputerKeyboardDevice.ts} +31 -11
  25. package/src/core/midi/MidiDevice.ts +38 -31
  26. package/src/core/midi/MidiDeviceManager.ts +54 -55
  27. package/src/core/midi/MidiEvent.ts +36 -60
  28. package/src/core/module/Module.ts +233 -0
  29. package/src/core/module/PolyModule.ts +246 -0
  30. package/src/core/module/VoiceScheduler.ts +121 -0
  31. package/src/core/module/index.ts +3 -0
  32. package/src/core/schema.ts +41 -0
  33. package/src/index.ts +31 -9
  34. package/src/modules/BiquadFilter.ts +162 -0
  35. package/src/modules/Constant.ts +72 -0
  36. package/src/modules/Envelope.ts +178 -0
  37. package/src/modules/Filter.ts +109 -104
  38. package/src/modules/Gain.ts +78 -0
  39. package/src/modules/Inspector.ts +59 -0
  40. package/src/modules/Master.ts +18 -21
  41. package/src/modules/MidiSelector.ts +50 -50
  42. package/src/modules/Oscillator.ts +202 -148
  43. package/src/modules/Scale.ts +79 -0
  44. package/src/modules/StepSequencer.ts +61 -0
  45. package/src/modules/VirtualMidi.ts +33 -49
  46. package/src/modules/index.ts +159 -74
  47. package/src/nodePolyfill.ts +25 -0
  48. package/src/processors/filter-processor.ts +82 -0
  49. package/src/processors/index.ts +28 -0
  50. package/src/processors/scale-processor.ts +81 -0
  51. package/dist/build/Engine.d.ts +0 -82
  52. package/dist/build/core/IO/AudioNode.d.ts +0 -35
  53. package/dist/build/core/IO/Collection.d.ts +0 -13
  54. package/dist/build/core/IO/ForwardNode/Base.d.ts +0 -18
  55. package/dist/build/core/IO/ForwardNode/index.d.ts +0 -27
  56. package/dist/build/core/IO/ForwardNode.d.ts +0 -38
  57. package/dist/build/core/IO/MidiNode.d.ts +0 -30
  58. package/dist/build/core/IO/Node.d.ts +0 -40
  59. package/dist/build/core/IO/index.d.ts +0 -21
  60. package/dist/build/core/Module/MonoModule.d.ts +0 -67
  61. package/dist/build/core/Module/PolyModule.d.ts +0 -61
  62. package/dist/build/core/Module/index.d.ts +0 -6
  63. package/dist/build/core/Note/frequencyTable.d.ts +0 -4
  64. package/dist/build/core/Note/index.d.ts +0 -28
  65. package/dist/build/core/midi/ComputerKeyboardInput.d.ts +0 -11
  66. package/dist/build/core/midi/MidiDevice.d.ts +0 -29
  67. package/dist/build/core/midi/MidiDeviceManager.d.ts +0 -14
  68. package/dist/build/core/midi/MidiEvent.d.ts +0 -21
  69. package/dist/build/core/midi/index.d.ts +0 -5
  70. package/dist/build/index.d.ts +0 -9
  71. package/dist/build/modules/BitCrusher.d.ts +0 -16
  72. package/dist/build/modules/Delay.d.ts +0 -19
  73. package/dist/build/modules/Distortion.d.ts +0 -16
  74. package/dist/build/modules/Effect.d.ts +0 -19
  75. package/dist/build/modules/Envelope/AmpEnvelope.d.ts +0 -18
  76. package/dist/build/modules/Envelope/Base.d.ts +0 -66
  77. package/dist/build/modules/Envelope/FreqEnvelope.d.ts +0 -25
  78. package/dist/build/modules/Envelope/index.d.ts +0 -3
  79. package/dist/build/modules/Filter.d.ts +0 -42
  80. package/dist/build/modules/LFO.d.ts +0 -44
  81. package/dist/build/modules/Master.d.ts +0 -11
  82. package/dist/build/modules/MidiSelector.d.ts +0 -21
  83. package/dist/build/modules/Oscillator.d.ts +0 -55
  84. package/dist/build/modules/Reverb.d.ts +0 -19
  85. package/dist/build/modules/Sequencer.d.ts +0 -42
  86. package/dist/build/modules/VirtualMidi.d.ts +0 -32
  87. package/dist/build/modules/VoiceScheduler.d.ts +0 -44
  88. package/dist/build/modules/Volume.d.ts +0 -26
  89. package/dist/build/modules/index.d.ts +0 -17
  90. package/dist/build/routes.d.ts +0 -18
  91. package/dist/build/types.d.ts +0 -5
  92. package/dist/build/utils.d.ts +0 -1
  93. package/dist/main.cjs.js +0 -25
  94. package/dist/main.cjs.js.map +0 -1
  95. package/dist/main.esm.js +0 -25
  96. package/dist/main.esm.js.map +0 -1
  97. package/src/core/IO/AudioNode.ts +0 -82
  98. package/src/core/IO/ForwardNode/Base.ts +0 -99
  99. package/src/core/IO/ForwardNode/index.ts +0 -60
  100. package/src/core/IO/MidiNode.ts +0 -67
  101. package/src/core/IO/Node.ts +0 -118
  102. package/src/core/Module/MonoModule.ts +0 -219
  103. package/src/core/Module/PolyModule.ts +0 -218
  104. package/src/core/Module/index.ts +0 -15
  105. package/src/core/midi/index.ts +0 -5
  106. package/src/modules/BitCrusher.ts +0 -45
  107. package/src/modules/Delay.ts +0 -53
  108. package/src/modules/Distortion.ts +0 -45
  109. package/src/modules/Effect.ts +0 -46
  110. package/src/modules/Envelope/AmpEnvelope.ts +0 -23
  111. package/src/modules/Envelope/Base.ts +0 -176
  112. package/src/modules/Envelope/FreqEnvelope.ts +0 -64
  113. package/src/modules/Envelope/index.ts +0 -3
  114. package/src/modules/LFO.ts +0 -149
  115. package/src/modules/Reverb.ts +0 -53
  116. package/src/modules/Sequencer.ts +0 -178
  117. package/src/modules/VoiceScheduler.ts +0 -145
  118. package/src/modules/Volume.ts +0 -72
  119. package/src/routes.ts +0 -49
  120. package/src/types.ts +0 -3
  121. package/src/utils.ts +0 -18
@@ -1,76 +1,152 @@
1
- import MonoModule, { AudioModule, PolyModule } from "../Module";
2
- import IO, { IOType } from "./Node";
3
- import { AudioInput, AudioOutput } from "./AudioNode";
4
- import { MidiInput, MidiOutput } from "./MidiNode";
5
- import { IAnyIO } from ".";
6
- import { ForwardAudioInput, ForwardAudioOutput } from "./ForwardNode";
7
-
8
- export default class IOCollection<AnyIO extends IO> {
9
- plugableModule: AudioModule;
10
- collection: AnyIO[] = [];
11
-
12
- constructor(plugableModule: AudioModule) {
13
- this.plugableModule = plugableModule;
1
+ import { assertNever } from "@blibliki/utils";
2
+ import { ModuleType } from "@/modules";
3
+ import { Module } from "../module";
4
+ import { PolyModule } from "../module/PolyModule";
5
+ import {
6
+ AudioInput,
7
+ AudioInputProps,
8
+ AudioOutput,
9
+ AudioOutputProps,
10
+ } from "./AudioIO";
11
+ import { Base, IOType } from "./Base";
12
+ import {
13
+ MidiInput,
14
+ MidiInputProps,
15
+ MidiOutput,
16
+ MidiOutputProps,
17
+ } from "./MidiIO";
18
+ import {
19
+ PolyAudioInput,
20
+ PolyAudioInputProps,
21
+ PolyAudioOutput,
22
+ PolyAudioOutputProps,
23
+ } from "./PolyAudioIO";
24
+
25
+ export enum CollectionType {
26
+ Input = "Input",
27
+ Output = "Output",
28
+ }
29
+
30
+ type IMappedIOProps = {
31
+ [CollectionType.Input]:
32
+ | AudioInputProps
33
+ | PolyAudioInputProps
34
+ | MidiInputProps;
35
+ [CollectionType.Output]:
36
+ | AudioOutputProps
37
+ | PolyAudioOutputProps
38
+ | MidiOutputProps;
39
+ };
40
+
41
+ type IIOTypeTOClass = {
42
+ [IOType.AudioInput]: AudioInput;
43
+ [IOType.AudioOutput]: AudioOutput;
44
+ [IOType.PolyAudioInput]: PolyAudioInput;
45
+ [IOType.PolyAudioOutput]: PolyAudioOutput;
46
+ [IOType.MidiInput]: MidiInput;
47
+ [IOType.MidiOutput]: MidiOutput;
48
+ };
49
+
50
+ export default abstract class IOCollection<T extends CollectionType> {
51
+ module: Module<ModuleType> | PolyModule<ModuleType>;
52
+ collection: Base[] = [];
53
+ collectionType: T;
54
+
55
+ constructor(
56
+ collectionType: T,
57
+ module: Module<ModuleType> | PolyModule<ModuleType>,
58
+ ) {
59
+ this.collectionType = collectionType;
60
+ this.module = module;
14
61
  }
15
- add<CurrentIO extends AnyIO>(props: IAnyIO): CurrentIO {
16
- let io;
62
+
63
+ add<TT extends IMappedIOProps[T]>(props: TT): IIOTypeTOClass[TT["ioType"]] {
64
+ let io:
65
+ | AudioInput
66
+ | AudioOutput
67
+ | PolyAudioInput
68
+ | PolyAudioOutput
69
+ | MidiInput
70
+ | MidiOutput;
71
+ this.validateUniqName(props.name);
17
72
 
18
73
  switch (props.ioType) {
19
74
  case IOType.AudioInput:
20
- if (this.plugableModule instanceof PolyModule) {
21
- throw Error("PolyModule is not supported");
22
- }
23
-
24
- io = new AudioInput(this.plugableModule, props);
75
+ if (this.module instanceof PolyModule) throw Error("Not compatible");
76
+ io = new AudioInput(this.module, props);
25
77
  break;
26
78
  case IOType.AudioOutput:
27
- if (this.plugableModule instanceof PolyModule) {
28
- throw Error("PolyModule is not supported");
29
- }
30
-
31
- io = new AudioOutput(this.plugableModule, props);
79
+ if (this.module instanceof PolyModule) throw Error("Not compatible");
80
+ io = new AudioOutput(this.module, props);
32
81
  break;
33
- case IOType.MidiInput:
34
- io = new MidiInput(this.plugableModule, props);
82
+ case IOType.PolyAudioInput:
83
+ if (this.module instanceof Module) throw Error("Not compatible");
84
+ io = new PolyAudioInput(this.module, props);
35
85
  break;
36
- case IOType.MidiOutput:
37
- io = new MidiOutput(this.plugableModule, props);
86
+ case IOType.PolyAudioOutput:
87
+ if (this.module instanceof Module) throw Error("Not compatible");
88
+ io = new PolyAudioOutput(this.module, props);
38
89
  break;
39
- case IOType.ForwardAudioInput:
40
- if (this.plugableModule instanceof MonoModule) {
41
- throw Error("MonoModule is not supported");
42
- }
43
-
44
- io = new ForwardAudioInput(this.plugableModule, props);
90
+ case IOType.MidiInput:
91
+ io = new MidiInput(this.module, props);
45
92
  break;
46
- case IOType.ForwardAudioOutput:
47
- if (this.plugableModule instanceof MonoModule) {
48
- throw Error("MonoModule is not supported");
49
- }
50
-
51
- io = new ForwardAudioOutput(this.plugableModule, props);
93
+ case IOType.MidiOutput:
94
+ io = new MidiOutput(this.module, props);
52
95
  break;
53
96
  default:
54
- throw Error("Unknown IOType");
97
+ assertNever(props);
55
98
  }
56
99
 
57
- this.collection.push(io as unknown as CurrentIO);
58
- return io as unknown as CurrentIO;
100
+ this.collection.push(io);
101
+
102
+ return io as IIOTypeTOClass[TT["ioType"]];
59
103
  }
60
104
 
61
105
  unPlugAll() {
62
- this.collection.forEach((io) => io.unPlugAll());
106
+ this.collection.forEach((io) => {
107
+ io.unPlugAll();
108
+ });
109
+ }
110
+
111
+ rePlugAll(callback?: () => void) {
112
+ this.collection.forEach((io) => {
113
+ io.rePlugAll(callback);
114
+ });
63
115
  }
64
116
 
65
117
  find(id: string) {
66
- return this.collection.find((io) => io.id === id);
118
+ const io = this.collection.find((io) => io.id === id);
119
+ if (!io) throw Error(`The io with id ${id} is not exists`);
120
+
121
+ return io;
67
122
  }
68
123
 
69
124
  findByName(name: string) {
70
- return this.collection.find((io) => io.name === name);
125
+ const io = this.collection.find((io) => io.name === name);
126
+ if (!io) throw Error(`The io with name ${name} is not exists`);
127
+
128
+ return io;
71
129
  }
72
130
 
73
131
  serialize() {
74
132
  return this.collection.map((io) => io.serialize());
75
133
  }
134
+
135
+ private validateUniqName(name: string) {
136
+ if (this.collection.some((io) => io.name === name)) {
137
+ throw Error(`An io with name ${name} is already exists`);
138
+ }
139
+ }
140
+ }
141
+
142
+ export class InputCollection extends IOCollection<CollectionType.Input> {
143
+ constructor(module: Module<ModuleType> | PolyModule<ModuleType>) {
144
+ super(CollectionType.Input, module);
145
+ }
146
+ }
147
+
148
+ export class OutputCollection extends IOCollection<CollectionType.Output> {
149
+ constructor(module: Module<ModuleType> | PolyModule<ModuleType>) {
150
+ super(CollectionType.Output, module);
151
+ }
76
152
  }
@@ -0,0 +1,43 @@
1
+ import { ModuleType } from "@/modules";
2
+ import MidiEvent from "../midi/MidiEvent";
3
+ import { Module } from "../module";
4
+ import { PolyModule } from "../module/PolyModule";
5
+ import IO, { IOProps, IOType } from "./Base";
6
+
7
+ export type MidiIO = MidiInput | MidiOutput;
8
+
9
+ export type MidiInputProps = IOProps & {
10
+ ioType: IOType.MidiInput;
11
+ onMidiEvent: (event: MidiEvent) => void;
12
+ };
13
+
14
+ export type MidiOutputProps = IOProps & {
15
+ ioType: IOType.MidiOutput;
16
+ };
17
+
18
+ export class MidiInput extends IO<MidiOutput> implements MidiInputProps {
19
+ declare ioType: IOType.MidiInput;
20
+ onMidiEvent: MidiInputProps["onMidiEvent"];
21
+
22
+ constructor(
23
+ module: Module<ModuleType> | PolyModule<ModuleType>,
24
+ props: MidiInputProps,
25
+ ) {
26
+ super(module, props);
27
+ this.onMidiEvent = props.onMidiEvent;
28
+ }
29
+ }
30
+
31
+ export class MidiOutput extends IO<MidiInput> implements MidiOutputProps {
32
+ declare ioType: IOType.MidiOutput;
33
+
34
+ onMidiEvent = (event: MidiEvent) => {
35
+ this.midiConnections.forEach((input) => {
36
+ input.onMidiEvent(event);
37
+ });
38
+ };
39
+
40
+ private get midiConnections() {
41
+ return this.connections.filter((input) => input instanceof MidiInput);
42
+ }
43
+ }
@@ -0,0 +1,115 @@
1
+ import { ModuleType } from "@/modules";
2
+ import { PolyModule } from "../module/PolyModule";
3
+ import { AudioInput, AudioOutput } from "./AudioIO";
4
+ import IO, { IOProps, IOType } from "./Base";
5
+
6
+ export type PolyAudioIO = PolyAudioInput | PolyAudioOutput;
7
+
8
+ export type PolyAudioInputProps = IOProps & {
9
+ ioType: IOType.PolyAudioInput;
10
+ };
11
+
12
+ export type PolyAudioOutputProps = IOProps & {
13
+ ioType: IOType.PolyAudioOutput;
14
+ };
15
+
16
+ export class PolyAudioInput
17
+ extends IO<PolyAudioOutput | AudioOutput>
18
+ implements PolyAudioInputProps
19
+ {
20
+ declare ioType: IOType.PolyAudioInput;
21
+ declare module: PolyModule<ModuleType>;
22
+
23
+ plug(io: PolyAudioOutput | AudioOutput, plugOther = true) {
24
+ super.plug(io, plugOther);
25
+ if (!plugOther && io instanceof PolyAudioOutput) return;
26
+
27
+ plugOrUnplug(this, io, true);
28
+ }
29
+
30
+ unPlug(io: PolyAudioOutput | AudioOutput, plugOther = true) {
31
+ super.unPlug(io, plugOther);
32
+ if (!plugOther && io instanceof PolyAudioOutput) return;
33
+
34
+ plugOrUnplug(this, io, false);
35
+ }
36
+
37
+ findIOByVoice(voice: number): AudioInput {
38
+ return this.module
39
+ .findVoice(voice)
40
+ .inputs.findByName(this.name) as AudioInput;
41
+ }
42
+ }
43
+
44
+ export class PolyAudioOutput
45
+ extends IO<PolyAudioInput | AudioInput>
46
+ implements PolyAudioOutputProps
47
+ {
48
+ declare ioType: IOType.PolyAudioOutput;
49
+ declare module: PolyModule<ModuleType>;
50
+
51
+ plug(io: PolyAudioInput | AudioInput, plugOther = true) {
52
+ super.plug(io, plugOther);
53
+ if (!plugOther && io instanceof PolyAudioInput) return;
54
+
55
+ plugOrUnplug(this, io, true);
56
+ }
57
+
58
+ unPlug(io: PolyAudioInput | AudioInput, plugOther = true) {
59
+ super.unPlug(io, plugOther);
60
+ if (!plugOther && io instanceof PolyAudioInput) return;
61
+
62
+ plugOrUnplug(this, io, false);
63
+ }
64
+
65
+ findIOByVoice(voice: number): AudioOutput {
66
+ return this.module
67
+ .findVoice(voice)
68
+ .outputs.findByName(this.name) as AudioOutput;
69
+ }
70
+ }
71
+
72
+ function plugOrUnplug(
73
+ thisIO: PolyAudioInput,
74
+ otherIO: PolyAudioOutput | AudioOutput,
75
+ isPlug: boolean,
76
+ ): void;
77
+ function plugOrUnplug(
78
+ thisIO: PolyAudioOutput,
79
+ otherIO: PolyAudioInput | AudioInput,
80
+ isPlug: boolean,
81
+ ): void;
82
+ function plugOrUnplug(
83
+ thisIO: PolyAudioInput | PolyAudioOutput,
84
+ otherIO: PolyAudioOutput | AudioOutput | PolyAudioInput | AudioInput,
85
+ isPlug: boolean,
86
+ ) {
87
+ if (otherIO instanceof PolyAudioInput || otherIO instanceof PolyAudioOutput) {
88
+ const maxVoices = Math.max(thisIO.module.voices, otherIO.module.voices);
89
+
90
+ for (let voice = 0; voice < maxVoices; voice++) {
91
+ const thisMonoIO = thisIO.findIOByVoice(voice % thisIO.module.voices);
92
+ const otherMonoIO = otherIO.findIOByVoice(voice % otherIO.module.voices);
93
+
94
+ if (isPlug) {
95
+ // @ts-expect-error: temp solution until guard this input plug to output
96
+ thisMonoIO.plug(otherMonoIO);
97
+ } else {
98
+ // @ts-expect-error: temp solution until guard this input plug to output
99
+ thisMonoIO.unPlug(otherMonoIO);
100
+ }
101
+ }
102
+ } else {
103
+ for (let voice = 0; voice < thisIO.module.voices; voice++) {
104
+ const thisMonoIO = thisIO.findIOByVoice(voice);
105
+
106
+ if (isPlug) {
107
+ // @ts-expect-error: temp solution until guard this input plug to output
108
+ thisMonoIO.plug(otherIO);
109
+ } else {
110
+ // @ts-expect-error: temp solution until guard this input plug to output
111
+ thisMonoIO.unPlug(otherIO);
112
+ }
113
+ }
114
+ }
115
+ }
@@ -1,62 +1,8 @@
1
- import { default as IOCollection } from "./Collection";
2
- import { IIONode, IOType, IIOSerialize } from "./Node";
3
- import {
4
- AudioInput,
5
- AudioOutput,
6
- IAudioInput,
7
- IAudioOutput,
8
- } from "./AudioNode";
9
- import { IMidiInput, IMidiOutput, MidiInput, MidiOutput } from "./MidiNode";
10
- import {
11
- ForwardAudioInput,
12
- ForwardAudioOutput,
13
- IForwardAudioInput,
14
- IForwardAudioOutput,
15
- } from "./ForwardNode";
1
+ export { InputCollection, OutputCollection } from "./Collection";
2
+ export { AudioInput, AudioOutput } from "./AudioIO";
3
+ export { MidiInput, MidiOutput } from "./MidiIO";
16
4
 
17
- type AnyAudioInput = AudioInput | ForwardAudioInput;
18
- type AnyMidiInput = MidiInput;
19
- type AnyAudioOuput = AudioOutput | ForwardAudioOutput;
20
- type AnyMidiOuput = MidiOutput;
21
- type AnyInput = AnyAudioInput | AnyMidiInput;
22
- type AnyOuput = AnyAudioOuput | AnyMidiOuput;
23
- type AnyIO = AnyInput | AnyOuput;
24
-
25
- type IAnyAudioInput = IAudioInput | IForwardAudioInput;
26
- type IAnyMidiInput = IMidiInput;
27
- type IAnyAudioOuput = IAudioOutput | IForwardAudioOutput;
28
- type IAnyMidiOuput = IMidiOutput;
29
- type IAnyInput = IAnyAudioInput | IAnyMidiInput;
30
- type IAnyOutput = IAnyAudioOuput | IAnyMidiOuput;
31
- type IAnyIO = IAnyInput | IAnyOutput;
32
-
33
- export {
34
- IOCollection,
35
- IOType,
36
- AudioInput,
37
- AudioOutput,
38
- MidiInput,
39
- MidiOutput,
40
- ForwardAudioInput,
41
- ForwardAudioOutput,
42
- };
43
- export type {
44
- IAnyIO,
45
- IIONode,
46
- IAudioInput,
47
- IAudioOutput,
48
- IMidiInput,
49
- IMidiOutput,
50
- IForwardAudioInput,
51
- IForwardAudioOutput,
52
- IAnyInput,
53
- IAnyOutput,
54
- IIOSerialize,
55
- AnyAudioInput,
56
- AnyMidiInput,
57
- AnyAudioOuput,
58
- AnyMidiOuput,
59
- AnyInput,
60
- AnyOuput,
61
- AnyIO,
62
- };
5
+ export { IOType } from "./Base";
6
+ export type { IIOSerialize } from "./Base";
7
+ export type { AudioInputProps, AudioOutputProps } from "./AudioIO";
8
+ export type { MidiInputProps, MidiOutputProps } from "./MidiIO";