-
Notifications
You must be signed in to change notification settings - Fork 347
/
Copy pathAudioTypes.hx
184 lines (160 loc) · 4.73 KB
/
AudioTypes.hx
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
package hxd.snd.webaudio;
#if (js && !useal)
import js.html.audio.*;
class BufferHandle {
public var inst : AudioBuffer;
public var isEnd : Bool;
public var samples : Int;
public function new() { }
}
@:allow(hxd.snd.webaudio.Driver)
class SourceHandle {
public var sampleOffset : Int;
public var playing : Bool;
public var driver : Driver;
public var lowPass : BiquadFilterNode;
public var panner : PannerNode;
public var gain : GainNode;
public var destination : AudioNode;
public var buffers : Array<BufferPlayback>;
public var pitch : Float;
public var firstPlay : Bool;
public function new() {
buffers = [];
sampleOffset = 0;
pitch = 1;
firstPlay = true;
}
public function updateDestination() {
destination = gain;
if ( lowPass != null ) {
lowPass.connect(destination);
destination = lowPass;
}
if ( panner != null ) {
panner.connect(destination);
destination = panner;
}
gain.connect(driver.destination);
for (b in buffers) {
if ( b.node != null ) {
b.restart(this);
}
}
}
public function applyPitch() {
// BUG: Because pitch is k-rate parameter, it applies it once per 128 sample block, which throws timings off and creates audio skips.
// Noticeable mainly with low pitch values, so it's not particularly usable to reduce pitch gradually.
var t = 0.;
for ( b in buffers ) {
t = b.readjust(t, this);
}
}
}
class BufferPlayback {
public var buffer : BufferHandle;
public var node : AudioBufferSourceNode;
public var offset : Float; // Buffer offset. Modified when applying effects.
public var dirty : Bool; // Playback was started - node no longer usable.
public var consumed : Bool; // Node was played completely (ended event fired)
public var starts : Float;
public var ends : Float;
public var currentSample(get, never):Int;
static inline var FADE_SAMPLES = 10; // Click prevent at the start.
var lastSamples:Int;
var lastTime:Float;
public function new()
{
}
function get_currentSample ( ):Int {
if ( consumed ) return buffer.samples;
if ( node == null || !dirty || node.context.currentTime < lastTime ) return 0;
lastSamples += Math.floor((node.context.currentTime - lastTime) * buffer.inst.sampleRate * node.playbackRate.value);
lastTime = node.context.currentTime;
return lastSamples;
}
public function set(buf : BufferHandle, grainOffset : Float) {
buffer = buf;
offset = Math.isNaN(grainOffset) ? 0 : grainOffset;
dirty = false;
consumed = false;
starts = 0;
ends = 0;
}
public function start( ctx : AudioContext, source : SourceHandle, time : Float) {
dirty = true;
consumed = false;
if (node != null) {
stop();
}
if ( source.firstPlay && buffer.samples > FADE_SAMPLES ) {
source.firstPlay = false;
var channels = [for (i in 0...buffer.inst.numberOfChannels) buffer.inst.getChannelData(i)];
var j = 0, fade = 0.;
while ( j < FADE_SAMPLES ) {
var i = 0;
while ( i < channels.length ) {
channels[i][j] *= fade;
i++;
}
j++;
fade += 1 / FADE_SAMPLES;
if (fade > 1) fade = 1;
}
}
node = ctx.createBufferSource();
node.buffer = buffer.inst;
node.addEventListener("ended", onBufferConsumed);
node.connect(source.destination);
node.playbackRate.value = source.pitch;
node.start(time, offset);
lastSamples = 0;
lastTime = time;
starts = time;
return ends = time + (buffer.inst.duration - offset) / source.pitch;
}
public function readjust( time : Float, source : SourceHandle ) {
if (consumed || node == null) return ends;
var ctx = source.driver.ctx;
var shiftTime = ctx.currentTime;// + 16 / buffer.inst.sampleRate;
node.playbackRate.setValueAtTime(source.pitch, shiftTime);
var elapsed = shiftTime - starts;
if ( elapsed < 0 ) {
// Queued node that haven't started yet: Requeue.
return start(ctx, source, time == 0 ? shiftTime : time);
}
// Stretch start position relative to new pitch.
starts = shiftTime - (elapsed / source.pitch);
return ends = starts + (buffer.inst.duration - offset) / source.pitch;
}
public function restart( source : SourceHandle ) {
if ( consumed || node == null ) return;
var ctx = hxd.snd.webaudio.Context.get();
if ( ctx.currentTime > starts ) {
offset += (ctx.currentTime - starts) * source.pitch;
start(ctx, source, ctx.currentTime);
} else {
start(ctx, source, starts);
}
}
public function stop( immediate : Bool = true ) {
if ( node != null ) {
node.removeEventListener("ended", onBufferConsumed);
if (immediate) node.disconnect();
else node.stop();
node = null;
}
}
function onBufferConsumed ( e : js.html.Event ) {
node.removeEventListener("ended", onBufferConsumed);
node.disconnect();
node = null;
consumed = true;
}
public function clear()
{
buffer = null;
node = null;
}
}
#end