-
Notifications
You must be signed in to change notification settings - Fork 31
/
player.ts
166 lines (153 loc) · 5.4 KB
/
player.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
/// <reference path="api.d.ts" />
/// <reference path="ring_buffer.ts" />
class WebAudioPlayer {
private context: AudioContext;
private node: ScriptProcessorNode;
private resampler: Worker;
private in_writing: boolean = false;
private buffering: boolean = true;
private ringbuf: RingBuffer;
private period_samples: number;
private delay_samples: number;
onneedbuffer: () => void = null;
init(sampling_rate: number, num_of_channels: number,
period_samples: number, delay_periods: number, buffer_periods: number): Promise<any>
{
return new Promise((resolve, reject) => {
this.context = new AudioContext();
this.node = this.context.createScriptProcessor(period_samples, 0, num_of_channels);
this.node.onaudioprocess = (ev) => {
this._onaudioprocess(ev);
};
if (sampling_rate != this.getActualSamplingRate()) {
console.log('enable resampling: ' + sampling_rate + ' -> ' + this.getActualSamplingRate());
this.period_samples = Math.ceil(period_samples * this.getActualSamplingRate() / sampling_rate) * num_of_channels;
this.resampler = new Worker('resampler.js');
} else {
this.period_samples = period_samples * num_of_channels;
}
this.ringbuf = new RingBuffer(new Float32Array(this.period_samples * buffer_periods));
this.delay_samples = this.period_samples * delay_periods;
if (this.resampler) {
this.resampler.onmessage = (ev) => {
if (ev.data.status == 0) {
resolve();
} else {
reject(ev.data);
}
};
this.resampler.postMessage({
channels: num_of_channels,
in_sampling_rate: sampling_rate,
out_sampling_rate: this.getActualSamplingRate()
});
} else {
resolve();
}
});
}
enqueue(buf: IAudioBuffer): Promise<any> {
return new Promise((resolve, reject) => {
if (this.in_writing) {
reject();
return;
}
this.in_writing = true;
var func = (data: Float32Array) => {
this.ringbuf.append(data).then(() => {
this.in_writing = false;
this.check_buffer();
}, (e) => {
this.in_writing = false;
reject(e);
});
};
if (this.resampler) {
var transfer_list = buf.transferable ? [buf.samples.buffer] : [];
this.resampler.onmessage = (ev) => {
if (ev.data.status != 0) {
this.in_writing = false;
reject(ev.data);
return;
}
func(ev.data.result);
};
this.resampler.postMessage({
samples: buf.samples
}, transfer_list);
} else {
func(buf.samples);
}
});
}
private _onaudioprocess(ev): void {
if (this.buffering) {
this.check_buffer();
return;
}
var N = ev.outputBuffer.numberOfChannels;
var buf = new Float32Array(ev.outputBuffer.getChannelData(0).length * N);
var size = this.ringbuf.read_some(buf) / N;
for (var i = 0; i < N; ++i) {
var ch = ev.outputBuffer.getChannelData(i);
for (var j = 0; j < size; ++j)
ch[j] = buf[j * N + i];
}
this.check_buffer(true);
}
private in_requesting_check_buffer = false;
private check_buffer(useTimeOut: boolean = false): void {
if (this.in_requesting_check_buffer || !this.onneedbuffer)
return;
var needbuf = this.check_buffer_internal();
if (!needbuf)
return;
if (useTimeOut) {
this.in_requesting_check_buffer = true;
window.setTimeout(() => {
this.in_requesting_check_buffer = false;
if (this.check_buffer_internal())
this.onneedbuffer();
}, 0);
} else {
this.onneedbuffer();
}
}
private check_buffer_internal(): boolean {
if (this.in_writing) return false;
var avail = this.ringbuf.available();
var size = this.ringbuf.size();
if (size >= this.delay_samples)
this.buffering = false;
if (this.period_samples <= avail)
return true;
return false;
}
start(): void {
if (this.node) {
this.node.connect(this.context.destination);
}
}
stop(): void {
if (this.node) {
this.ringbuf.clear();
this.buffering = true;
this.node.disconnect();
}
}
close(): void {
this.stop();
this.context = null;
this.node = null;
}
getActualSamplingRate(): number {
return this.context.sampleRate;
}
getBufferStatus(): IPlayerBufferStatus {
return {
delay: this.ringbuf.size(),
available: this.ringbuf.available(),
capacity: this.ringbuf.capacity(),
};
}
}