@blibliki/engine 0.1.22 → 0.1.24

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": "@blibliki/engine",
3
- "version": "0.1.22",
3
+ "version": "0.1.24",
4
4
  "source": "src/index.ts",
5
5
  "main": "dist/main.cjs.js",
6
6
  "module": "dist/main.esm.js",
@@ -68,7 +68,11 @@ export class AudioOutput extends IONode implements IAudioOutput {
68
68
  super.unPlug(io, plugOther);
69
69
  if (io instanceof ForwardInput) return;
70
70
 
71
- this.internalModule.disconnect(io.internalModule);
71
+ try {
72
+ this.internalModule.disconnect(io.internalModule);
73
+ } catch (e) {
74
+ console.error(e);
75
+ }
72
76
  }
73
77
 
74
78
  unPlugAll() {
@@ -0,0 +1,150 @@
1
+ import { now, LFO as LFOInternal, ToneOscillatorType } from "tone";
2
+ import Engine from "../Engine";
3
+ import Module, { PolyModule, Startable } from "../core/Module";
4
+ import { TWave } from "./Oscillator";
5
+ import Note from "../core/Note";
6
+
7
+ export interface LFOInterface {
8
+ wave: TWave;
9
+ frequency: number;
10
+ min: number;
11
+ max: number;
12
+ amount: number;
13
+ }
14
+
15
+ const InitialProps: LFOInterface = {
16
+ wave: "sine",
17
+ frequency: 0.1,
18
+ min: 0,
19
+ max: 1,
20
+ amount: 1,
21
+ };
22
+
23
+ class MonoLFO extends Module<LFOInternal, LFOInterface> implements Startable {
24
+ constructor(params: {
25
+ id?: string;
26
+ name: string;
27
+ props: Partial<LFOInterface>;
28
+ }) {
29
+ const { id, name, props } = params;
30
+
31
+ super(new LFOInternal(), {
32
+ id,
33
+ name,
34
+ props: { ...InitialProps, ...props },
35
+ });
36
+
37
+ this.registerBasicOutputs();
38
+ this.registerInputs();
39
+ this.start(now());
40
+ }
41
+
42
+ get wave() {
43
+ return this._props["wave"];
44
+ }
45
+
46
+ set wave(value: TWave) {
47
+ this._props = { ...this.props, wave: value };
48
+ this.internalModule.type = this.wave as ToneOscillatorType;
49
+ }
50
+
51
+ get min() {
52
+ return this._props["min"];
53
+ }
54
+
55
+ set min(value: number) {
56
+ this._props = { ...this.props, min: value };
57
+ this.internalModule.min = this.min;
58
+ }
59
+
60
+ get max() {
61
+ return this._props["max"];
62
+ }
63
+
64
+ set max(value: number) {
65
+ this._props = { ...this.props, max: value };
66
+ this.internalModule.max = this.max;
67
+ }
68
+
69
+ get frequency() {
70
+ return this._props["frequency"];
71
+ }
72
+
73
+ set frequency(value: number) {
74
+ this._props = { ...this.props, frequency: value };
75
+ this.internalModule.frequency.value = this.frequency;
76
+ }
77
+
78
+ get amount() {
79
+ return this._props["amount"];
80
+ }
81
+
82
+ set amount(value: number) {
83
+ this._props = { ...this.props, amount: value };
84
+ this.internalModule.amplitude.value = this.amount;
85
+ }
86
+
87
+ start(time: number) {
88
+ if (!Engine.isStarted) return;
89
+
90
+ const oscState = this.internalModule.state;
91
+
92
+ if (oscState === "started") {
93
+ this.internalModule.stop(time - 2);
94
+ this.internalModule.start(time);
95
+ } else {
96
+ this.internalModule.start(time);
97
+ }
98
+ }
99
+
100
+ stop(time?: number) {
101
+ this.internalModule.stop(time);
102
+ }
103
+
104
+ triggerAttack = (_: Note, triggeredAt: number) => {
105
+ this.start(triggeredAt);
106
+ };
107
+
108
+ triggerRelease = () => {
109
+ // Do nothing
110
+ };
111
+
112
+ private registerInputs() {
113
+ this.registerDefaultMidiInput();
114
+ this.registerAudioInput({
115
+ name: "amount",
116
+ internalModule: this.internalModule.amplitude,
117
+ });
118
+ }
119
+ }
120
+
121
+ export default class LFO extends PolyModule<MonoLFO, LFOInterface> {
122
+ static moduleName = "LFO";
123
+
124
+ constructor(params: {
125
+ id?: string;
126
+ name: string;
127
+ props: Partial<LFOInterface>;
128
+ }) {
129
+ const { id, name, props } = params;
130
+
131
+ super({
132
+ id,
133
+ name,
134
+ child: MonoLFO,
135
+ props: { ...InitialProps, ...props },
136
+ });
137
+
138
+ this.registerBasicOutputs();
139
+ this.registerInput({ name: "midi in" });
140
+ this.registerInput({ name: "amount" });
141
+ }
142
+
143
+ start(time: number) {
144
+ this.audioModules.forEach((audioModule) => audioModule.start(time));
145
+ }
146
+
147
+ stop(time?: number) {
148
+ this.audioModules.forEach((audioModule) => audioModule.stop(time));
149
+ }
150
+ }
@@ -1,14 +1,16 @@
1
- import { now, Oscillator as Osc, ToneOscillatorType } from "tone";
1
+ import { Multiply, now, Oscillator as Osc, ToneOscillatorType } from "tone";
2
2
  import Engine from "../Engine";
3
3
 
4
4
  import Note from "../core/Note";
5
5
  import Module, { PolyModule, Startable } from "../core/Module";
6
6
 
7
+ export type TWave = "sine" | "triangle" | "square" | "sawtooth";
8
+
7
9
  export interface OscillatorInterface {
8
10
  noteName: string;
9
11
  fine: number;
10
12
  coarse: number;
11
- wave: string;
13
+ wave: TWave;
12
14
  volume: number;
13
15
  range: number;
14
16
  }
@@ -27,6 +29,7 @@ class MonoOscillator
27
29
  implements Startable
28
30
  {
29
31
  private _note: Note;
32
+ private _fineSignal: Multiply;
30
33
 
31
34
  constructor(params: {
32
35
  id?: string;
@@ -41,8 +44,8 @@ class MonoOscillator
41
44
  props: { ...InitialProps, ...props },
42
45
  });
43
46
 
47
+ this.registerInputs();
44
48
  this.registerBasicOutputs();
45
- this.registerDefaultMidiInput();
46
49
  this.start(now());
47
50
  }
48
51
 
@@ -69,13 +72,23 @@ class MonoOscillator
69
72
  this.note = new Note(this.noteName);
70
73
  }
71
74
 
75
+ get fineSingal() {
76
+ if (this._fineSignal) return this._fineSignal;
77
+
78
+ this._fineSignal = new Multiply(100);
79
+ this._fineSignal.connect(this.internalModule.detune);
80
+
81
+ return this._fineSignal;
82
+ }
83
+
72
84
  get fine() {
73
85
  return this._props["fine"];
74
86
  }
75
87
 
76
88
  set fine(value: number) {
77
- this._props = { ...this.props, fine: Math.floor(value) };
78
- this.internalModule.detune.value = this.fine;
89
+ this._props = { ...this.props, fine: value };
90
+
91
+ this.fineSingal.value = this.fine;
79
92
  }
80
93
 
81
94
  get coarse() {
@@ -92,7 +105,7 @@ class MonoOscillator
92
105
  return this._props["wave"];
93
106
  }
94
107
 
95
- set wave(value: string) {
108
+ set wave(value: TWave) {
96
109
  this._props = { ...this.props, wave: value };
97
110
  this.internalModule.type = this.wave as ToneOscillatorType;
98
111
  }
@@ -156,6 +169,14 @@ class MonoOscillator
156
169
  private getNote(note: Note | string): Note {
157
170
  return note instanceof Note ? note : new Note(note);
158
171
  }
172
+
173
+ private registerInputs() {
174
+ this.registerDefaultMidiInput();
175
+ this.registerAudioInput({
176
+ name: "fine",
177
+ internalModule: this.fineSingal,
178
+ });
179
+ }
159
180
  }
160
181
 
161
182
  export default class Oscillator extends PolyModule<
@@ -180,6 +201,7 @@ export default class Oscillator extends PolyModule<
180
201
 
181
202
  this.registerBasicOutputs();
182
203
  this.registerInput({ name: "midi in" });
204
+ this.registerInput({ name: "fine" });
183
205
  }
184
206
 
185
207
  start(time: number) {
@@ -1,4 +1,3 @@
1
- import { camelCase, upperFirst } from "lodash";
2
1
  import { Envelope, AmpEnvelope, FreqEnvelope } from "./Envelope";
3
2
  import Oscillator from "./Oscillator";
4
3
  import Filter from "./Filter";
@@ -12,6 +11,7 @@ import Delay from "./Delay";
12
11
  import Distortion from "./Distortion";
13
12
  import BitCrusher from "./BitCrusher";
14
13
  import Sequencer from "./Sequencer";
14
+ import LFO from "./LFO";
15
15
  import { AudioModule } from "../core/Module";
16
16
 
17
17
  export { default as Master } from "./Master";
@@ -45,8 +45,6 @@ export function createModule(params: ICreateModule): AudioModule {
45
45
  }
46
46
 
47
47
  function moduleClassFromType(type: string) {
48
- type = upperFirst(camelCase(type));
49
-
50
48
  switch (type) {
51
49
  case Oscillator.moduleName:
52
50
  return Oscillator;
@@ -78,6 +76,8 @@ function moduleClassFromType(type: string) {
78
76
  return BitCrusher;
79
77
  case Sequencer.moduleName:
80
78
  return Sequencer;
79
+ case LFO.moduleName:
80
+ return LFO;
81
81
  default:
82
82
  throw Error(`Unknown module type ${type}`);
83
83
  }