-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathworklet-decoder.js
More file actions
165 lines (150 loc) · 4.51 KB
/
Copy pathworklet-decoder.js
File metadata and controls
165 lines (150 loc) · 4.51 KB
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
// defined by worklet context
const SAMPLE_RATE = /** @type {number} */ (sampleRate);
const ZERO_FRAMES = SAMPLE_RATE / 1200;
const ZERO_PULSES = 4;
const ONE_FRAMES = SAMPLE_RATE / 2400;
const ONE_PULSES = 8;
const THRESHOLD = ONE_FRAMES * 1.5;
/** @template T */
class AsyncQueue {
bucket = /** @type {T[]} */ ([]);
resolve = /** @type {((item: T) => void) | undefined} */ (undefined);
/** @param {T} item */
push(item) {
if (this.resolve) {
this.resolve(item);
this.resolve = undefined;
} else {
this.bucket.push(item);
}
}
shift() {
const item = this.bucket.shift();
if (item) {
return Promise.resolve(item);
} else {
const p = new Promise(r => this.resolve = r);
return /** @type {Promise<T>} */ (p);
}
}
}
class WorkletDecoder extends AudioWorkletProcessor {
chunks = /** @type {AsyncQueue<Float32Array>} */ (new AsyncQueue());
chunk = new Float32Array();
chunkIndex = 0;
constructor() {
super()
this.startAsync();
}
process(inputs) {
const channels = inputs[0];
const chunk = channels[0];
if (chunk) {
this.chunks.push(chunk);
}
// true = safe to clean up this node
return !!this.chunk;
}
async * getSamples() {
while (true) {
if (!this.chunk || this.chunkIndex >= this.chunk.length) {
this.chunk = await this.chunks.shift();
this.chunkIndex = 0;
continue;
}
yield this.chunk[this.chunkIndex];
this.chunkIndex += 1;
}
}
/**
* @param {AsyncGenerator<number>} samples
* @returns the time between falling edges, as number of samples
*/
async * getWavelengths(samples) {
let isPositive = false;
let wavelength = 0;
let highTime = 0;
let lowTime = 0;
for await (const sample of samples) {
wavelength += 1;
if (sample > 0) {
lowTime = 0;
highTime += 1;
} else {
lowTime += 1;
highTime = 0;
}
if (!isPositive && highTime >= 4) {
isPositive = true;
}
if (isPositive && lowTime >= 4) {
isPositive = false;
// falling edge detected
yield wavelength;
wavelength = 0;
}
}
}
/** @param {AsyncGenerator<number>} wavelengths */
async * getBytes(wavelengths) {
let idle = true;
let waveCount = 0;
let longCount = 0;
let bitCount = 0;
let byteValue = 0;
let totalBytes = 0;
let streak = 0;
let numStreaks = 1;
for await (const wl of wavelengths) {
// wait for leading zero
idle = idle && wl < THRESHOLD;
if (idle) continue;
// build a bit
waveCount += 1;
if (wl >= THRESHOLD) {
longCount += 1;
}
let bit;
if (waveCount == 8) {
bit = 1;
} else if (waveCount == 4 && longCount >= 2) {
bit = 0;
} else {
continue;
}
bitCount += 1;
waveCount = 0;
longCount = 0;
// build a byte
byteValue = (byteValue >> 1) + (bit << 7);
if (bitCount === 9) {
yield byteValue;
bitCount = 0;
idle = true;
totalBytes += 1;
if (
streak > 16
&& byteValue != 0x0a
&& (byteValue < 0x20 || byteValue > 0x7f)
) {
console.log(`weird byte: ${byteValue}`);
console.log(`streak: ${streak}`);
console.log(`average: ${totalBytes / numStreaks}`);
numStreaks += 1;
streak = 0;
} else {
streak += 1;
}
}
}
}
async startAsync() {
const samples = this.getSamples();
const wavelengths = this.getWavelengths(samples);
const bytes = this.getBytes(wavelengths);
for await (const byte of bytes) {
this.port.postMessage(byte);
}
}
}
registerProcessor('worklet-decoder', WorkletDecoder);