Upload utils.ts
Browse files
utils.ts
ADDED
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/**
|
2 |
+
* @license
|
3 |
+
* SPDX-License-Identifier: Apache-2.0
|
4 |
+
*/
|
5 |
+
import {Blob} from '@google/genai';
|
6 |
+
|
7 |
+
function encode(bytes) {
|
8 |
+
let binary = '';
|
9 |
+
const len = bytes.byteLength;
|
10 |
+
for (let i = 0; i < len; i++) {
|
11 |
+
binary += String.fromCharCode(bytes[i]);
|
12 |
+
}
|
13 |
+
return btoa(binary);
|
14 |
+
}
|
15 |
+
|
16 |
+
function decode(base64) {
|
17 |
+
const binaryString = atob(base64);
|
18 |
+
const len = binaryString.length;
|
19 |
+
const bytes = new Uint8Array(len);
|
20 |
+
for (let i = 0; i < len; i++) {
|
21 |
+
bytes[i] = binaryString.charCodeAt(i);
|
22 |
+
}
|
23 |
+
return bytes;
|
24 |
+
}
|
25 |
+
|
26 |
+
function createBlob(data: Float32Array): Blob {
|
27 |
+
const l = data.length;
|
28 |
+
const int16 = new Int16Array(l);
|
29 |
+
for (let i = 0; i < l; i++) {
|
30 |
+
// convert float32 -1 to 1 to int16 -32768 to 32767
|
31 |
+
int16[i] = data[i] * 32768;
|
32 |
+
}
|
33 |
+
|
34 |
+
return {
|
35 |
+
data: encode(new Uint8Array(int16.buffer)),
|
36 |
+
mimeType: 'audio/pcm;rate=16000',
|
37 |
+
};
|
38 |
+
}
|
39 |
+
|
40 |
+
async function decodeAudioData(
|
41 |
+
data: Uint8Array,
|
42 |
+
ctx: AudioContext,
|
43 |
+
sampleRate: number,
|
44 |
+
numChannels: number,
|
45 |
+
): Promise<AudioBuffer> {
|
46 |
+
const buffer = ctx.createBuffer(
|
47 |
+
numChannels,
|
48 |
+
data.length / 2 / numChannels,
|
49 |
+
sampleRate,
|
50 |
+
);
|
51 |
+
|
52 |
+
const dataInt16 = new Int16Array(data.buffer);
|
53 |
+
const l = dataInt16.length;
|
54 |
+
const dataFloat32 = new Float32Array(l);
|
55 |
+
for (let i = 0; i < l; i++) {
|
56 |
+
dataFloat32[i] = dataInt16[i] / 32768.0;
|
57 |
+
}
|
58 |
+
// Extract interleaved channels
|
59 |
+
if (numChannels === 0) {
|
60 |
+
buffer.copyToChannel(dataFloat32, 0);
|
61 |
+
} else {
|
62 |
+
for (let i = 0; i < numChannels; i++) {
|
63 |
+
const channel = dataFloat32.filter(
|
64 |
+
(_, index) => index % numChannels === i,
|
65 |
+
);
|
66 |
+
buffer.copyToChannel(channel, i);
|
67 |
+
}
|
68 |
+
}
|
69 |
+
|
70 |
+
return buffer;
|
71 |
+
}
|
72 |
+
|
73 |
+
export {createBlob, decode, decodeAudioData, encode};
|