-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwilioSocket.ts
549 lines (476 loc) · 15.8 KB
/
twilioSocket.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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
import { IncomingMessage } from "http";
import WebSocket, { WebSocketServer } from "ws";
import { promises as fs } from "fs";
import {
biquadFilter,
createBiquadBandpassFilter,
createBiquadHighPassFilter,
} from "./filters/biquad";
import { BiquadFilterState } from "./filters/types";
import {
computeAlpha,
computeAlphaLowPass,
highPassFilter,
} from "./filters/singlePole";
import { bandPassFilter } from "./filters/bandPass";
import {
createFIRFilterState,
createFIRLowPassCoefficients,
firFilter,
FIRFilterState,
} from "./filters/fir";
export interface TwilioSocket {
ws: WebSocket;
streamSid: string | null;
callSid: string | null;
phoneNumber: string | null;
prevSampleIn?: number;
prevSampleOut?: number;
recordedSamplesUnfiltered?: Int16Array[];
recordedSamplesFiltered?: Int16Array[];
biquadState?: BiquadFilterState;
firState?: FIRFilterState;
firCoefficients?: number[];
bandPassState?: {
prevSampleInHP: number;
prevSampleOutHP: number;
prevSampleOutLP: number;
};
}
export const twilioWss = new WebSocketServer({ noServer: true });
twilioWss.on("connection", async (ws: WebSocket, request: IncomingMessage) => {
console.log("Twilio WebSocket connected");
const connection: TwilioSocket = {
ws,
streamSid: null,
callSid: null,
phoneNumber: null,
};
let chunkCounter = 0;
ws.on("message", async (message: WebSocket.Data) => {
if (message instanceof Buffer) {
try {
const jsonString = message.toString("utf8");
const jsonMessage = JSON.parse(jsonString);
if (!connection.streamSid && jsonMessage.streamSid) {
connection.streamSid = jsonMessage.streamSid;
console.log(`StreamSid set: ${connection.streamSid}`);
}
switch (jsonMessage.event) {
case "connected":
console.log("Twilio stream connected");
console.log("Connected JSON: ", jsonMessage);
break;
case "start":
console.log("Twilio stream started: ", jsonMessage);
break;
case "media":
// Now we should have connection.aiSocket
if (jsonMessage.media && jsonMessage.media.payload) {
chunkCounter++;
// TODO - Select filter
// highPassFilterAndSaveToWav
// bandPassFilterAndSaveToWav
// biquadBandPassFilterAndSaveToWav
await highPassFilterAndSaveToWav(
jsonMessage.media.payload,
connection,
chunkCounter
);
}
break;
case "stop":
console.log("Twilio stream stopped");
// If we have recorded audio, write them
if (
connection.recordedSamplesUnfiltered &&
connection.recordedSamplesUnfiltered.length > 0
) {
try {
// Merge UNFILTERED
const mergedUnfiltered = mergeInt16Arrays(
connection.recordedSamplesUnfiltered
);
const finalWavBufferUnfiltered = encodeWav(
mergedUnfiltered,
SAMPLE_RATE,
1
);
const unfilteredFilename = `twilio_call_unfiltered.wav`;
await fs.writeFile(
unfilteredFilename,
finalWavBufferUnfiltered
);
console.log(`Wrote UNFILTERED WAV file: ${unfilteredFilename}`);
// Merge FILTERED
const mergedFiltered = mergeInt16Arrays(
connection.recordedSamplesFiltered || []
);
const finalWavBufferFiltered = encodeWav(
mergedFiltered,
SAMPLE_RATE,
1
);
const filteredFilename = `twilio_call_filtered.wav`;
await fs.writeFile(filteredFilename, finalWavBufferFiltered);
console.log(`Wrote FILTERED WAV file: ${filteredFilename}`);
} catch (err) {
console.error("Error writing final WAV files:", err);
}
}
ws.close();
break;
default:
console.log(`Unhandled event type: ${jsonMessage.event}`);
}
} catch (error) {
console.error("Error processing Twilio message:", error);
}
} else {
console.error("Received unexpected message type from Twilio");
}
});
ws.on("error", (error) => {
console.error("WebSocket error:", error);
});
ws.on("close", (code, reason) => {
console.log(`WebSocket closed with code ${code}, reason: ${reason}`);
});
});
function mergeInt16Arrays(chunks: Int16Array[]): Int16Array {
// Calculate total length
let totalLength = 0;
for (let chunk of chunks) {
totalLength += chunk.length;
}
// Create one big Int16Array
const merged = new Int16Array(totalLength);
// Copy each chunk in sequence
let offset = 0;
for (let chunk of chunks) {
merged.set(chunk, offset);
offset += chunk.length;
}
return merged;
}
const SAMPLE_RATE = 8000;
const HP_CUTOFF = 300; // frequency in Hz
const LP_CUTOFF = 3400; // in Hz
async function highPassFilterAndSaveToWav(
audioPayload: string,
connection: TwilioSocket,
chunkNumber: number
) {
try {
// 1) Base64-decode
const muLawBuffer = Buffer.from(audioPayload, "base64");
// 2) Decode µ-law -> Int16 PCM
const pcmSamples = decodeMuLawBuffer(muLawBuffer);
// -- Always store the raw/unfiltered version
if (!connection.recordedSamplesUnfiltered) {
connection.recordedSamplesUnfiltered = [];
}
connection.recordedSamplesUnfiltered.push(pcmSamples);
// -- Now apply the high-pass filter
if (connection.prevSampleIn === undefined) {
connection.prevSampleIn = 0;
}
if (connection.prevSampleOut === undefined) {
connection.prevSampleOut = 0;
}
const alpha = computeAlpha(HP_CUTOFF, SAMPLE_RATE);
const { filtered, newPrevIn, newPrevOut } = highPassFilter(
pcmSamples,
alpha,
connection.prevSampleIn,
connection.prevSampleOut
);
connection.prevSampleIn = newPrevIn;
connection.prevSampleOut = newPrevOut;
// -- Store the filtered version
if (!connection.recordedSamplesFiltered) {
connection.recordedSamplesFiltered = [];
}
connection.recordedSamplesFiltered.push(filtered);
console.log(
`Stored chunk #${chunkNumber} - raw length=${pcmSamples.length}, filtered length=${filtered.length}.`
);
} catch (err) {
console.error("Error filtering + storing WAV data:", err);
}
}
async function bandPassFilterAndSaveToWav(
audioPayload: string,
connection: TwilioSocket,
chunkNumber: number
) {
try {
// 1) Decode base64 & µ-law → Int16
const muLawBuffer = Buffer.from(audioPayload, "base64");
const pcmSamples = decodeMuLawBuffer(muLawBuffer);
// 2) Always store unfiltered
connection.recordedSamplesUnfiltered ||= [];
connection.recordedSamplesUnfiltered.push(pcmSamples);
// 3) If needed, init the bandPassState once
if (!connection.bandPassState) {
connection.bandPassState = {
prevSampleInHP: 0,
prevSampleOutHP: 0,
prevSampleOutLP: 0,
};
}
// 4) Compute alphaHP, alphaLP
const alphaHP = computeAlpha(HP_CUTOFF, SAMPLE_RATE); // e.g. 300 Hz
const alphaLP = computeAlphaLowPass(LP_CUTOFF, SAMPLE_RATE); // e.g. 3400 Hz
// 5) Band-pass filter
const { filtered, newState } = bandPassFilter(
pcmSamples,
alphaHP,
alphaLP,
connection.bandPassState
);
connection.bandPassState = newState; // store updated filter state
// 6) Keep the filtered version
connection.recordedSamplesFiltered ||= [];
connection.recordedSamplesFiltered.push(filtered);
console.log(
`Stored chunk #${chunkNumber} - raw length=${pcmSamples.length}, filtered length=${filtered.length}.`
);
} catch (err) {
console.error("Error filtering + storing WAV data:", err);
}
}
async function biquadBandPassFilterAndSaveToWav(
audioPayload: string,
connection: TwilioSocket,
chunkNumber: number
) {
try {
// 1) Decode base64 & µ-law
const muLawBuffer = Buffer.from(audioPayload, "base64");
const pcmSamples = decodeMuLawBuffer(muLawBuffer);
// 2) Store unfiltered
if (!connection.recordedSamplesUnfiltered) {
connection.recordedSamplesUnfiltered = [];
}
connection.recordedSamplesUnfiltered.push(pcmSamples);
// 3) Initialize biquad filter state if needed
if (!connection.biquadState) {
connection.biquadState = {
x1: 0,
x2: 0,
y1: 0,
y2: 0,
};
}
// 4) Create/get filter coefficients (could be stored as constant)
const centerFreq = 1000; // 1kHz center frequency
const Q = 1.0; // Q factor
const coeffs = createBiquadBandpassFilter(centerFreq, Q, SAMPLE_RATE);
// 5) Apply biquad filter
const { filtered, newState } = biquadFilter(
pcmSamples,
coeffs,
connection.biquadState
);
connection.biquadState = newState;
// 6) Store filtered version
if (!connection.recordedSamplesFiltered) {
connection.recordedSamplesFiltered = [];
}
connection.recordedSamplesFiltered.push(filtered);
console.log(
`Processed chunk #${chunkNumber} - raw length=${pcmSamples.length}, filtered length=${filtered.length}`
);
} catch (err) {
console.error("Error in biquad filtering:", err);
}
}
async function biquadHighPassFilterAndSaveToWav(
audioPayload: string,
connection: TwilioSocket,
chunkNumber: number
) {
try {
// 1) Decode base64 & µ-law → Int16
const muLawBuffer = Buffer.from(audioPayload, "base64");
const pcmSamples = decodeMuLawBuffer(muLawBuffer);
// 2) Always store unfiltered version
connection.recordedSamplesUnfiltered ||= [];
connection.recordedSamplesUnfiltered.push(pcmSamples);
// 3) Initialize biquad state if needed
if (!connection.biquadState) {
connection.biquadState = {
x1: 0,
x2: 0,
y1: 0,
y2: 0,
};
}
// 4) Create the high-pass filter coefficients
const cutoffFreq = 300; // 300 Hz cutoff
const Q = 0.707; // Butterworth response
const coeffs = createBiquadHighPassFilter(cutoffFreq, Q, SAMPLE_RATE);
// 5) Apply the filter
const { filtered, newState } = biquadFilter(
pcmSamples,
coeffs,
connection.biquadState
);
connection.biquadState = newState;
// 6) Store filtered version
connection.recordedSamplesFiltered ||= [];
connection.recordedSamplesFiltered.push(filtered);
console.log(
`Processed chunk #${chunkNumber} - raw length=${pcmSamples.length}, filtered length=${filtered.length}`
);
} catch (err) {
console.error("Error in biquad high-pass filtering:", err);
}
}
async function firFilterAndSaveToWav(
audioPayload: string,
connection: TwilioSocket,
chunkNumber: number
) {
try {
// 1) Decode base64 & µ-law
const muLawBuffer = Buffer.from(audioPayload, "base64");
const pcmSamples = decodeMuLawBuffer(muLawBuffer);
// 2) Store unfiltered
if (!connection.recordedSamplesUnfiltered) {
connection.recordedSamplesUnfiltered = [];
}
connection.recordedSamplesUnfiltered.push(pcmSamples);
// 3) Initialize FIR filter if needed
if (!connection.firState) {
const numTaps = 51; // Odd number for symmetric filter
connection.firState = createFIRFilterState(numTaps);
// Create coefficients (could be stored as constant)
connection.firCoefficients = createFIRLowPassCoefficients(
3400, // cutoff frequency
SAMPLE_RATE,
numTaps
);
}
// 4) Apply FIR filter
const { filtered, newState } = firFilter(
pcmSamples,
connection.firCoefficients ?? [],
connection.firState
);
connection.firState = newState;
// 5) Store filtered version
if (!connection.recordedSamplesFiltered) {
connection.recordedSamplesFiltered = [];
}
connection.recordedSamplesFiltered.push(filtered);
console.log(
`Processed chunk #${chunkNumber} - raw length=${pcmSamples.length}, filtered length=${filtered.length}`
);
} catch (err) {
console.error("Error in FIR filtering:", err);
}
}
/**
* Wrap PCM samples in a standard 44-byte WAV header (mono, 16-bit, 8000Hz).
* Returns a Buffer containing the entire WAV file (header + samples).
*/
function encodeWav(
samples: Int16Array,
sampleRate: number,
numChannels: number
): Buffer {
const bitsPerSample = 16;
// Byte length of the PCM data
const byteRate = sampleRate * numChannels * (bitsPerSample / 8);
const blockAlign = numChannels * (bitsPerSample / 8);
const dataByteLength = samples.length * 2; // 2 bytes per sample (16-bit)
// WAV header size = 44 bytes
// overall file size = 44 + dataByteLength - 8 for chunk size
const fileSize = 44 + dataByteLength;
// Allocate buffer for header + PCM samples
// 44 bytes (header) + dataByteLength
const wavBuffer = Buffer.alloc(fileSize);
// RIFF chunk descriptor
wavBuffer.write("RIFF", 0); // ChunkID
wavBuffer.writeUInt32LE(fileSize - 8, 4); // ChunkSize = fileSize - 8
wavBuffer.write("WAVE", 8); // Format
// 'fmt ' sub-chunk
wavBuffer.write("fmt ", 12); // Subchunk1ID
wavBuffer.writeUInt32LE(16, 16); // Subchunk1Size (16 for PCM)
wavBuffer.writeUInt16LE(1, 20); // AudioFormat (1 = PCM)
wavBuffer.writeUInt16LE(numChannels, 22); // NumChannels
wavBuffer.writeUInt32LE(sampleRate, 24); // SampleRate
wavBuffer.writeUInt32LE(byteRate, 28); // ByteRate
wavBuffer.writeUInt16LE(blockAlign, 32); // BlockAlign
wavBuffer.writeUInt16LE(bitsPerSample, 34); // BitsPerSample
// 'data' sub-chunk
wavBuffer.write("data", 36); // Subchunk2ID
wavBuffer.writeUInt32LE(dataByteLength, 40); // Subchunk2Size
// Write PCM samples (little-endian)
let offset = 44;
for (let i = 0; i < samples.length; i++) {
wavBuffer.writeInt16LE(samples[i], offset);
offset += 2;
}
return wavBuffer;
}
// mediaFormat: { encoding: 'audio/x-mulaw', sampleRate: 8000, channels: 1 },
function muLawDecode(uVal: number): number {
// Flip bits
uVal = ~uVal & 0xff;
const sign = uVal & 0x80 ? -1 : 1;
let exponent = (uVal >> 4) & 0x07;
let mantissa = uVal & 0x0f;
let magnitude = (mantissa << 4) + 0x08;
if (exponent > 0) {
magnitude += 0x100;
}
if (exponent > 1) {
magnitude <<= exponent - 1;
}
return sign * magnitude;
}
function decodeMuLawBuffer(muLawBuffer: Buffer): Int16Array {
const out = new Int16Array(muLawBuffer.length);
for (let i = 0; i < muLawBuffer.length; i++) {
out[i] = muLawDecode(muLawBuffer[i]);
}
return out;
}
/**
* Encodes one 16-bit PCM sample → 8-bit µ-law sample.
* Clamps sample to [-32768..32767].
*/
function muLawEncode(sample: number): number {
// Clamp
if (sample > 32767) sample = 32767;
if (sample < -32768) sample = -32768;
// Get sign bit
const sign = sample < 0 ? 0x80 : 0x00;
if (sign) sample = -sample;
// µ-law uses a logarithmic segment approach.
// We'll find the "exponent" by finding how many times we can shift right
// before the value falls under 128.
let exponent = 0;
let magnitude = sample >> 2; // The bias in µ-law is effectively 4, so shift by 2
while (magnitude > 0x3f) {
magnitude >>= 1;
exponent++;
}
// The mantissa is the lower 6 bits
const mantissa = magnitude & 0x3f;
const ulawByte = ~(sign | (exponent << 4) | (mantissa & 0x0f)) & 0xff;
return ulawByte;
}
/**
* Encodes an Int16Array of PCM → a Buffer of 8-bit µ-law bytes.
*/
function encodeMuLawBuffer(pcmSamples: Int16Array): Buffer {
const out = Buffer.alloc(pcmSamples.length);
for (let i = 0; i < pcmSamples.length; i++) {
out[i] = muLawEncode(pcmSamples[i]);
}
return out;
}