Sets up the basic project configuration for Cognitive Clarity. This includes: - Initializing `package.json` with necessary dependencies (React, Gemini API, Lucide React) and build scripts for Vite. - Creating a basic `index.html` with Tailwind CSS and Google Fonts integration. - Setting up `index.tsx` as the React application entry point. - Configuring `tsconfig.json` for TypeScript compilation with Vite. - Establishing `vite.config.ts` for development server setup and environment variable handling. - Adding a standard `.gitignore` file. - Updating `README.md` with local run instructions and AI Studio links. - Defining `metadata.json` for application details. - Creating core type definitions in `types.ts`. - Populating `constants.ts` with initial CBT glossary terms. - Adding utility functions for audio processing in `services/audioUtils.ts`.
67 lines
2.2 KiB
TypeScript
67 lines
2.2 KiB
TypeScript
import { LiveServerMessage, Modality } from "@google/genai";
|
|
|
|
// Convert Float32Array (Web Audio API) to PCM Int16 (Gemini API requirement)
|
|
export function floatTo16BitPCM(float32Array: Float32Array): ArrayBuffer {
|
|
const buffer = new ArrayBuffer(float32Array.length * 2);
|
|
const view = new DataView(buffer);
|
|
let offset = 0;
|
|
for (let i = 0; i < float32Array.length; i++) {
|
|
let s = Math.max(-1, Math.min(1, float32Array[i]));
|
|
s = s < 0 ? s * 0x8000 : s * 0x7FFF;
|
|
view.setInt16(offset, s, true); // Little Endian
|
|
offset += 2;
|
|
}
|
|
return buffer;
|
|
}
|
|
|
|
// Base64 encoding for the payload
|
|
export function arrayBufferToBase64(buffer: ArrayBuffer): string {
|
|
let binary = '';
|
|
const bytes = new Uint8Array(buffer);
|
|
const len = bytes.byteLength;
|
|
for (let i = 0; i < len; i++) {
|
|
binary += String.fromCharCode(bytes[i]);
|
|
}
|
|
return btoa(binary);
|
|
}
|
|
|
|
// Convert Base64 string back to Uint8Array
|
|
export function base64ToUint8Array(base64: string): Uint8Array {
|
|
const binaryString = atob(base64);
|
|
const len = binaryString.length;
|
|
const bytes = new Uint8Array(len);
|
|
for (let i = 0; i < len; i++) {
|
|
bytes[i] = binaryString.charCodeAt(i);
|
|
}
|
|
return bytes;
|
|
}
|
|
|
|
// Decode raw PCM data from Gemini to AudioBuffer
|
|
export async function decodeAudioData(
|
|
data: Uint8Array,
|
|
ctx: AudioContext,
|
|
sampleRate: number = 24000,
|
|
numChannels: number = 1,
|
|
): Promise<AudioBuffer> {
|
|
// Ensure the byte length is even for Int16Array
|
|
if (data.byteLength % 2 !== 0) {
|
|
console.warn("Received odd byte length for PCM16 audio, trimming last byte.");
|
|
data = data.subarray(0, data.byteLength - 1);
|
|
}
|
|
|
|
// Use the underlying buffer, but handle offsets if the Uint8Array is a view
|
|
const dataInt16 = new Int16Array(data.buffer, data.byteOffset, data.byteLength / 2);
|
|
|
|
const buffer = ctx.createBuffer(numChannels, dataInt16.length / numChannels, sampleRate);
|
|
|
|
for (let channel = 0; channel < numChannels; channel++) {
|
|
const channelData = buffer.getChannelData(channel);
|
|
// Determine step size if multiple channels (usually 1 for this API)
|
|
for (let i = 0; i < dataInt16.length; i++) {
|
|
// Normalize Int16 to Float32 [-1.0, 1.0]
|
|
channelData[i] = dataInt16[i] / 32768.0;
|
|
}
|
|
}
|
|
return buffer;
|
|
}
|