-
Notifications
You must be signed in to change notification settings - Fork 36
/
offline-gain-node.ts
147 lines (106 loc) · 4.62 KB
/
offline-gain-node.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
import { IAudioNode, IGainNode, IOfflineAudioContext, IOfflineAudioNodeFaker } from '../interfaces';
import { OfflineAudioNodeProxy } from '../offline-audio-node';
import { TUnpatchedOfflineAudioContext } from '../types';
export interface IOfflineGainNodeFakerProxyOptions {
fakeNodeStore: WeakMap<IAudioNode, IOfflineAudioNodeFaker>;
offlineAudioContext: IOfflineAudioContext;
}
export class OfflineGainNodeFakerProxy extends OfflineAudioNodeProxy implements IGainNode {
constructor ({ fakeNodeStore, offlineAudioContext }: IOfflineGainNodeFakerProxyOptions) {
super({
channelCountMode: 'max',
channelInterpretation: 'speakers',
fakeNodeStore,
numberOfInputs: 1,
numberOfOutputs: 1,
offlineAudioContext
});
}
public get gain (): AudioParam {
// @todo Fake a proper AudioParam.
const audioParam = {
cancelScheduledValues: (startTime: number) => {
startTime; // tslint:disable-line:no-unused-expression
return audioParam;
},
defaultValue: 1,
exponentialRampToValueAtTime: (value: number, endTime: number) => {
endTime; // tslint:disable-line:no-unused-expression
value; // tslint:disable-line:no-unused-expression
return audioParam;
},
linearRampToValueAtTime: (value: number, endTime: number) => {
endTime; // tslint:disable-line:no-unused-expression
value; // tslint:disable-line:no-unused-expression
return audioParam;
},
setTargetAtTime: (target: number, startTime: number, timeConstant: number) => {
startTime; // tslint:disable-line:no-unused-expression
target; // tslint:disable-line:no-unused-expression
timeConstant; // tslint:disable-line:no-unused-expression
return audioParam;
},
setValueAtTime: (value: number, startTime: number) => {
startTime; // tslint:disable-line:no-unused-expression
value; // tslint:disable-line:no-unused-expression
return audioParam;
},
setValueCurveAtTime: (values: Float32Array, startTime: number, duration: number) => {
duration; // tslint:disable-line:no-unused-expression
startTime; // tslint:disable-line:no-unused-expression
values; // tslint:disable-line:no-unused-expression
return audioParam;
},
value: 1
};
return audioParam;
}
}
export interface IOfflineGainNodeFakerOptions {
fakeNodeStore: WeakMap<IAudioNode, IOfflineAudioNodeFaker>;
offlineAudioContext: IOfflineAudioContext;
}
export class OfflineGainNodeFaker implements IOfflineAudioNodeFaker {
private _node: null | GainNode;
private _proxy: OfflineGainNodeFakerProxy;
private _sources: Map<IOfflineAudioNodeFaker, { input: number, output: number }>;
constructor ({ fakeNodeStore, offlineAudioContext }: IOfflineGainNodeFakerOptions) {
this._node = null;
this._proxy = new OfflineGainNodeFakerProxy({ fakeNodeStore, offlineAudioContext });
this._sources = new Map();
fakeNodeStore.set(this._proxy, this);
}
public get proxy () {
return this._proxy;
}
public render (offlineAudioContext: TUnpatchedOfflineAudioContext) {
if (this._node !== null) {
return Promise.resolve(this._node);
}
this._node = offlineAudioContext.createGain();
const promises = Array
.from(this._sources)
.map(([ source, { input, output } ]) => source
.render(offlineAudioContext)
.then((node) => node.connect(<GainNode> this._node, output, input)));
return Promise
.all(promises)
.then(() => this._node);
}
public wire (source: IOfflineAudioNodeFaker, output: number, input: number) {
this._sources.set(source, { input, output });
return this._proxy;
}
public unwire (source: IOfflineAudioNodeFaker) {
this._sources.delete(source);
}
}
export interface IOfflineGainNodeFakerFactoryOptions {
fakeNodeStore: WeakMap<IAudioNode, IOfflineAudioNodeFaker>;
offlineAudioContext: IOfflineAudioContext;
}
export class OfflineGainNodeFakerFactory {
public create ({ fakeNodeStore, offlineAudioContext }: IOfflineGainNodeFakerFactoryOptions) {
return new OfflineGainNodeFaker({ fakeNodeStore, offlineAudioContext });
}
}