/
automation.ts
409 lines (353 loc) · 13.8 KB
/
automation.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
import { Technique, UseContext } from '../fluid-interfaces'
import { AutomationLane, AutomationPoint } from '../FluidAutomation'
import { FluidPlugin } from '../FluidPlugin'
import { FluidSession } from '../FluidSession'
import { FluidTrack, FluidReceive } from '../FluidTrack'
import { Nudge, TechniqueClass } from './basic'
export interface PluginAutoTechniqueClass extends TechniqueClass {
new(options: PluginAutoOptions) : Technique
}
/**
* An automation event on a track
*/
export class TrackAutomation implements Technique {
paramKey : string
value : number = 0
curve : number = 0
constructor (options : AutoOptions) {
this.paramKey = options.paramKey
if (typeof options.paramKey !== 'string') throw new Error('TrackAutomation constructor missing paramKey:'+JSON.stringify(options))
if (typeof options.value === 'number') this.value = options.value
if (typeof options.curve === 'number') this.curve = options.curve
}
use ({ startTimeSeconds, track } : UseContext) {
const point : AutomationPoint = {
startTimeSeconds,
value: (this.value as number),
curve: 0,
}
if (typeof this.curve === 'number') point.curve = this.curve
const automation = track.automation
if (!automation.hasOwnProperty(this.paramKey))
automation[this.paramKey] = new AutomationLane
automation[this.paramKey].points.push(point)
return null
}
}
export interface AutoOptions {
paramKey : string
value? : number
curve? : number
}
export class TrackGainAutomation implements Technique {
gainDb : number
curve : number = 0
constructor(options : { gainDb: number, curve?: number }|number) {
if (typeof options === 'number') {
this.gainDb = options
} else {
this.gainDb = options.gainDb
if (typeof options.curve === 'number') this.curve = options.curve
}
}
use(context : UseContext) {
new TrackAutomation({ paramKey: 'gainDb', value: this.gainDb, curve: this.curve }).use(context)
}
}
export class TrackGainAutomationRamp implements Technique {
gainDb : number
curve : number = 0
constructor(options : { gainDb: number, curve?: number }|number) {
if (typeof options === 'number') options = { gainDb: options }
this.gainDb = options.gainDb
if (typeof options.curve === 'number') this.curve = options.curve
}
use (context : UseContext) {
const currentPoint = context.track.automation.gainDb.getLastPointAt(context.startTimeSeconds)
const currentValue = (typeof currentPoint?.value === 'number') ? currentPoint.value : 0
// Notice that when there are no automation points, the default value is 0,
// not track.gainDb. This is because the track automation and track gain are
// applied in sequence, and we do not want to apply the track's gainDb twice.
new TrackGainAutomation({ gainDb: currentValue, curve: this.curve }).use(context)
const newStartTime = context.startTime + context.duration
const newStartTimeSeconds = context.startTimeSeconds + context.durationSeconds
context.startTime = newStartTime
context.startTimeSeconds = newStartTimeSeconds
new TrackGainAutomation({ gainDb: this.gainDb }).use(context)
}
}
/**
* Identifies a plugin on an arbitrary track
*/
export interface PluginSelector {
pluginName : string
/** ex: 'VST2', 'VST3', 'AudioUnit' */
pluginType : string
/**
* The selected track may have multiple plugins with the same name. Index from
* within those plugins. Most of the time this isn't needed, because it is
* unusual to have more than one plugin with the same name on a particular
* track.
*/
nth? : number
}
/**
* Inserts an automation point for a specific plugin on an arbitrary track
*/
export class PluginAutomation implements Technique {
/**
* There are three possible types for `options.plugin`
* - a pluginSelector object `{ pluginName: string, pluginType: string, nth?: number}`
* - a 'pluginName' string - gets the first instance only
* - a plugin constructor function (instanceof) - gets the first instance only
*
* ```
* // Several different plugin selectors may be used
* const plugin = fluid.plugins.DragonflyHallVst2
* const op1 = { value: 15, paramKey: 'sizeMeters', plugin: plugin }
* const op2 = { value: 10, paramKey: 'sizeMeters', plugin: 'DragonflyHallReverb' }
* const op3 = fluid.plugins.DragonflyHallVst2.makeAutomation.sizeMeters(30)
* op3.plugin.nth = 2 // Select an instance other than the first
*
* const tLibraryVerb = {
* b: new techniques.PluginAutomation(op1),
* c: new techniques.PluginAutomation(op2),
* a: new techniques.PluginAutomation(op3),
* }
* ```
*/
constructor (options : PluginAutoOptions) {
if (!options.plugin) {
console.log(options)
throw new Error('Cannot create PluginAuto technique without a .plugin selector')
}
this.plugin = (typeof options.plugin !== 'string' && typeof options.plugin !== 'function') ? {...options.plugin} : options.plugin
this.paramKey = options.paramKey // ex: 'sizeMeters'
if (typeof options.value === 'number') this.value = options.value
if (typeof options.curve === 'number') this.curve = options.curve
}
// Mandatory members
plugin : PluginSelector|string|any
paramKey : string
// members with default values
value : number = 0
curve : number = 0
use ({ track, startTimeSeconds } : UseContext) {
const point : AutomationPoint = {
startTimeSeconds,
value: this.value,
curve: this.curve,
}
const automationLane = this.resolvePlugin(track).getAutomationLane(this.paramKey)
automationLane.points.push(point)
return null
}
/** @internal */
resolvePlugin(track : FluidTrack) : FluidPlugin {
// We will not cache the plugin, because we may want to re-use the same
// technique on another track
if (typeof this.plugin === 'string') {
for (const p of track.plugins) {
if (p.pluginName === this.plugin) {
return p
}
}
throw new Error('Plugin automation failed to resolve plugins (string)')
} else if (typeof this.plugin === 'function') {
for (const p of track.plugins) {
if (p instanceof this.plugin) {
return p
}
}
throw new Error('Plugin automation failed to resolve plugin (instanceof)')
}
// Assume plugin selector
const nth = this.plugin.nth || 0
const matches = track.plugins.filter(plugin =>
plugin.pluginName === this.plugin.pluginName &&
plugin.pluginType === this.plugin.pluginType);
if (nth >= matches.length) {
const needed = nth - matches.length + 1
if (needed > 0) throw new Error(`${needed} missing ${this.plugin.pluginName} plugins of on ${track.name} track`)
}
return matches[nth]
}
}
export interface PluginAutoOptions extends AutoOptions {
plugin : PluginSelector|string|any
}
/**
* Unlike [[PluginAutomation]], using `PluginAutomationRamp` always inserts two
* automation points (instead of one). The `.use(context)` method tries to find
* the starting value, and then ramps from that value to the value specified by
* `options.value` over the course of the triggering event.
*
* ```
* // Several different plugin selectors may be used
* const plugin = fluid.plugins.DragonflyHallVst2
* const op1 = { value: 15, paramKey: 'sizeMeters', plugin: plugin }
* const op2 = { value: 10, paramKey: 'sizeMeters', plugin: 'DragonflyHallReverb' }
* const op3 = fluid.plugins.DragonflyHallVst2.makeAutomation.sizeMeters(30)
* op3.plugin.nth = 2 // Select an instance other than the first
*
* const tLibraryVerb = {
* b: new techniques.PluginAutomationRamp(op1),
* c: new techniques.PluginAutomationRamp(op2),
* a: new techniques.PluginAutomationRamp(op3),
* }
* ```
*/
export class PluginAutomationRamp implements Technique {
curve : number = 0
rampStartOptions : PluginAutoOptions
rampEndTechnique : PluginAutomation
constructor(options : PluginAutoOptions) {
this.rampStartOptions = {...options}
options = {...options}
if (typeof options.curve === 'number') {
this.curve = options.curve
delete options.curve
}
this.rampEndTechnique = new PluginAutomation(options)
}
use(context : UseContext) {
const paramKey = this.rampStartOptions.paramKey
const plugin = this.rampEndTechnique.resolvePlugin(context.track)
const automationLane = plugin.getAutomationLane(paramKey)
const lastPoint = automationLane.getLastPointAt(context.startTimeSeconds)
const lastValue = (lastPoint && typeof lastPoint.value === 'number')
? lastPoint.value
: (typeof plugin.parameters[paramKey] === 'number')
? plugin.parameters[paramKey]
: this.rampEndTechnique.value
if (lastValue !== null) {
const rampStartOptions = { ...this.rampStartOptions }
rampStartOptions.value = lastValue
new PluginAutomation(rampStartOptions).use(context)
}
new Nudge(context.durationSeconds, this.rampEndTechnique).use(context)
}
}
/**
* SendAutomation inserts a single automation point from the track on which it
* is used to a track of your choice.
*/
export class SendAutomation implements Technique {
/** The destination track */
private destinationTrack? : FluidTrack
private unresolvedTrackName? : string
/** gain in dB */
value : number = 0
curve : number = 0
/**
* Accepts three parameters:
* - `options.to` can be a FluidTrack instance or a track name string. A
* send will be created on the track if it does not exist already. If the
* track does not exist, using this technique will throw an error.
* - `options.value` send level in dBFS
* - `options.curve` optional automation curve, defaults to 0
* ```
* const tLibrarySynth = {
* a: new techniques.SendAutomation({ to: 'verb', value: -6, curve: -0.5 })
* b: new techniques.SendAutomation({ to: 'verb', value: -12, curve: -0.5 })
* }
* ```
*/
constructor(options : SendAutoOptions) {
if (options.to instanceof FluidTrack) this.destinationTrack = options.to
else if (typeof options.to === 'string') this.unresolvedTrackName = options.to
else throw new Error('send automation options.to must be a FluidTrack OR a track name string')
if (typeof options.value === 'number') this.value = options.value
if (typeof options.curve === 'number') this.curve = options.curve
}
use(context : UseContext) {
const receive = this.getOrCreateReceive(context.session, context.track)
// At this point, we have everything we need: sourceTrack, destinationTrack, and receive
receive.automation.gainDb.points.push({
startTimeSeconds: context.startTimeSeconds,
value: this.value,
curve: this.curve
})
}
/**
* Get the destination track, assigning it to this.destionationTrack if
* needed, and throwing if it is unspecified or if it cannot be found.
* @internal
*/
resolveDestinationTrack(session : FluidSession) {
if (this.destinationTrack) return this.destinationTrack
if (!this.unresolvedTrackName) throw new Error('SendAutomation cannot resolve without a target track or target track name')
const track = session.getTrackByName(this.unresolvedTrackName)
if (!track) throw new Error(`Send automation cannot resolve "${this.unresolvedTrackName}" track. Track not found in session.`)
this.destinationTrack = track
return track
}
/** @internal */
getOrCreateReceive(session : FluidSession, sourceTrack : FluidTrack) {
const destinationTrack = this.resolveDestinationTrack(session)
let receive : FluidReceive|undefined
for (const existingReceive of destinationTrack.receives) {
if (existingReceive.from === sourceTrack) {
receive = existingReceive
break
}
}
if (!receive) {
receive = new FluidReceive({ from: sourceTrack })
destinationTrack.receives.push(receive)
}
return receive
}
}
export interface SendAutoOptions {
to: string|FluidTrack
curve? : number
value? : number
}
/**
* Unlike [[SendAutomation]], which always inserts one automation point, using
* SendAutomationRamp always inserts two automation points. The `use` method
* identifies the current value at the beginning of the event, and ramps to the
* new value over the duration of the triggering event.
* ```
* const tLibrarySynth = {
* a: new techniques.SendAutomationRamp({ to: 'verb', value: -6, curve: -0.5 })
* b: new techniques.SendAutomationRamp({ to: 'verb', value: -12, curve: -0.5 })
* }
* ```
*/
export class SendAutomationRamp implements Technique {
curve : number = 0
rampEndTechnique : SendAutomation
/**
* Accepts three parameters:
* - `options.to` can be a FluidTrack instance or a track name string. A
* send will be created on the track if it does not exist already. If the
* track does not exist, using this technique will throw an error.
* - `options.value` send level in dBFS
* - `options.curve` optional automation curve, defaults to 0
*/
constructor(options : SendAutoOptions) {
if (typeof options.curve === 'number') {
this.curve = options.curve
options = {...options}
delete options.curve
}
this.rampEndTechnique = new SendAutomation(options)
}
use (context : UseContext) {
const receiveTrack = this.rampEndTechnique.resolveDestinationTrack(context.session)
const receive = this.rampEndTechnique.getOrCreateReceive(context.session, context.track)
const automationLane = receive.automation.gainDb
const lastPoint = automationLane.getLastPointAt(context.startTimeSeconds)
const lastValue = (lastPoint && typeof lastPoint.value === 'number') ? lastPoint.value : receive.gainDb
// ramp start automation point
new SendAutomation({
to: receiveTrack,
value: lastValue,
curve: this.curve,
}).use(context)
// ramp end automation point
new Nudge(context.durationSeconds, this.rampEndTechnique).use(context)
}
}