VoicePrint: Quality improvements P2-1-5, P3-2 (FRE-5006)

- P2-1: Extract duplicate mock ML logic to modular embedding.service.ts / faiss.index.ts
- P2-2: Weak hashes already fixed via SHA-256 (FRE-5002)
- P2-3: Parallel batch processing with chunked Promise.allSettled
- P2-4: Consistent DI pattern via modular imports
- P2-5: Structured logging via ConsoleLogger
- P3-2: Batch jobId computed/logged, persistence blocked on schema

Approved by CTO review (FRE-5338)

Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
2026-05-14 07:12:31 -04:00
parent 9d4865306c
commit 268889ead4
6 changed files with 396 additions and 249 deletions

View File

@@ -8,6 +8,13 @@ import {
voicePrintFeatureFlags,
} from './voiceprint.config';
import { checkFlag } from './voiceprint.feature-flags';
import { logger } from './logger';
import { EmbeddingService as ModularEmbeddingService } from './embedding.service';
import { FAISSIndex as ModularFAISSIndex } from './faiss.index';
// Alias for backwards compatibility
const EmbeddingService = ModularEmbeddingService;
const FAISSIndex = ModularFAISSIndex;
// Audio preprocessing service
export class AudioPreprocessor {
@@ -292,8 +299,11 @@ export class AnalysisService {
// Batch analysis service
export class BatchAnalysisService {
private readonly maxConcurrency = 5;
/**
* Analyze multiple audio files in a batch.
* Analyze multiple audio files in a batch with parallel processing.
* Uses Promise.allSettled with concurrency control for better performance.
*/
async analyzeBatch(
userId: string,
@@ -321,31 +331,70 @@ export class BatchAnalysisService {
);
}
const jobId = `batch_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
logger.info('Starting batch analysis', {
jobId,
userId,
totalFiles: files.length,
enrollmentId: options?.enrollmentId
});
const analysisService = new AnalysisService();
const results: VoiceAnalysis[] = [];
const errors: Array<{ name: string; error: string }> = [];
let synthetic = 0;
let natural = 0;
let failed = 0;
for (const file of files) {
try {
const result = await analysisService.analyze(userId, file.buffer, {
enrollmentId: options?.enrollmentId,
audioUrl: file.audioUrl,
});
results.push(result);
if (result.isSynthetic) {
synthetic++;
} else {
natural++;
// Process with concurrency control using chunked Promise.allSettled
const processChunk = async (chunk: typeof files) => {
const promises = chunk.map(async (file) => {
try {
const result = await analysisService.analyze(userId, file.buffer, {
enrollmentId: options?.enrollmentId,
audioUrl: file.audioUrl,
});
return { success: true as const, result, name: file.name };
} catch (error) {
const message = error instanceof Error ? error.message : 'Analysis failed';
return { success: false as const, error: message, name: file.name };
}
});
const outcomes = await Promise.allSettled(promises);
for (const outcome of outcomes) {
if (outcome.status === 'fulfilled') {
if (outcome.value.success && outcome.value.result) {
results.push(outcome.value.result);
if (outcome.value.result.isSynthetic) {
synthetic++;
} else {
natural++;
}
} else if (!outcome.value.success) {
errors.push({ name: outcome.value.name, error: outcome.value.error });
}
}
} catch (error) {
console.error(`Batch analysis failed for ${file.name}:`, error);
failed++;
}
};
// Process files in chunks for concurrency control
for (let i = 0; i < files.length; i += this.maxConcurrency) {
const chunk = files.slice(i, i + this.maxConcurrency);
await processChunk(chunk);
}
const jobId = `batch_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
const failed = errors.length;
// TODO: P3-2 - Persist batch jobId to database once schema is fixed
// Schema errors need to be resolved first (AnalysisJob relation issues)
logger.info('Batch analysis completed', {
jobId,
successfulResults: results.length,
failedCount: failed,
synthetic,
natural
});
return {
jobId,
@@ -360,235 +409,11 @@ export class BatchAnalysisService {
}
}
// Embedding service — ECAPA-TDNN inference wrapper
export class EmbeddingService {
private initialized = false;
// Re-export improved modular implementations
export { EmbeddingService } from './embedding.service';
export { FAISSIndex } from './faiss.index';
/**
* Initialize the ECAPA-TDNN model.
*/
async initialize(): Promise<void> {
if (this.initialized) return;
// TODO: Connect to Python ML service for real inference
// const response = await fetch(`${voicePrintEnv.ML_SERVICE_URL}/initialize`, {
// method: 'POST',
// body: JSON.stringify({ modelPath: voicePrintEnv.ECAPA_TDNN_MODEL_PATH }),
// });
this.initialized = true;
console.log('Embedding service initialized (mock model)');
}
/**
* Extract voice embedding from audio.
*/
async extract(audioBuffer: Buffer): Promise<number[]> {
await this.initialize();
// TODO: Call Python ML service
// const response = await fetch(`${voicePrintEnv.ML_SERVICE_URL}/embed`, {
// method: 'POST',
// body: audioBuffer,
// });
// const data = await response.json();
// return data.embedding;
// Mock: generate deterministic embedding based on buffer content
const dims = voicePrintEnv.EMBEDDING_DIMENSIONS;
const embedding: number[] = new Array(dims);
let hash = 0;
for (let i = 0; i < Math.min(audioBuffer.length, 256); i++) {
hash = ((hash << 5) - hash) + audioBuffer[i];
hash |= 0;
}
for (let i = 0; i < dims; i++) {
hash = ((hash << 5) - hash) + i;
hash |= 0;
embedding[i] = (Math.abs(hash) % 1000) / 1000.0;
}
// L2 normalize
const norm = Math.sqrt(embedding.reduce((s, v) => s + v * v, 0));
return embedding.map((v) => v / norm);
}
/**
* Run full analysis: embedding + synthetic detection.
*/
async analyze(audioBuffer: Buffer): Promise<{
confidence: number;
detectionType: DetectionType;
features: Record<string, number>;
embedding: number[];
}> {
const embedding = await this.extract(audioBuffer);
// TODO: Run synthetic voice detection model
// For MVP, use heuristic based on embedding statistics
const confidence = this.estimateSyntheticConfidence(audioBuffer, embedding);
const detectionType =
confidence >= voicePrintEnv.SYNTHETIC_THRESHOLD
? DetectionType.SYNTHETIC_VOICE
: DetectionType.NATURAL;
const features = this.extractAnalysisFeatures(audioBuffer, embedding);
return {
confidence,
detectionType,
features,
embedding,
};
}
private estimateSyntheticConfidence(
buffer: Buffer,
embedding: number[]
): number {
// Heuristic features for synthetic detection
const meanAmplitude =
buffer.reduce((s, v) => s + v, 0) / buffer.length / 255;
const embeddingStdDev =
Math.sqrt(
embedding.reduce((s, v) => s + (v - embedding.reduce((a, b) => a + b) / embedding.length) ** 2, 0) /
embedding.length
) || 0;
// Deterministic buffer variance as alternative to Math.random()
const mean = meanAmplitude * 255;
let variance = 0;
for (let i = 0; i < buffer.length; i++) {
variance += (buffer[i] - mean) ** 2;
}
variance /= buffer.length;
const varianceScore = Math.min(1.0, variance / 16384);
// Combine features into confidence score
const amplitudeScore = Math.abs(meanAmplitude - 0.5) * 2;
const embeddingScore = 1.0 - Math.min(1.0, embeddingStdDev * 2);
return Math.min(
1.0,
amplitudeScore * 0.3 + embeddingScore * 0.4 + varianceScore * 0.3
);
}
private extractAnalysisFeatures(
buffer: Buffer,
embedding: number[]
): Record<string, number> {
const meanAmplitude =
buffer.reduce((s, v) => s + v, 0) / buffer.length / 255;
const zeroCrossings = buffer.reduce((count, v, i, arr) => {
return i > 0 && ((v - 128) * (arr[i - 1] - 128) < 0) ? count + 1 : count;
}, 0);
return {
mean_amplitude: meanAmplitude,
zero_crossing_rate: zeroCrossings / buffer.length,
embedding_energy: embedding.reduce((s, v) => s + v * v, 0),
embedding_entropy: this.calculateEntropy(embedding),
};
}
private calculateEntropy(values: number[]): number {
const bins = 20;
const histogram = new Array(bins).fill(0);
const min = Math.min(...values);
const max = Math.max(...values);
const range = max - min || 1;
for (const v of values) {
const bin = Math.min(bins - 1, Math.floor(((v - min) / range) * bins));
histogram[bin]++;
}
let entropy = 0;
const total = values.length;
for (const count of histogram) {
if (count > 0) {
const p = count / total;
entropy -= p * Math.log2(p);
}
}
return entropy;
}
}
// FAISS index wrapper for voice fingerprint matching
export class FAISSIndex {
private indexPath: string;
private initialized = false;
constructor(path?: string) {
this.indexPath = path ?? voicePrintEnv.FAISS_INDEX_PATH;
}
/**
* Initialize or load the FAISS index.
*/
async initialize(): Promise<void> {
if (this.initialized) return;
// TODO: Load FAISS index from disk
// const faiss = require('faiss-node');
// this.index = faiss.readIndex(this.indexPath);
this.initialized = true;
console.log(`FAISS index initialized at ${this.indexPath}`);
}
/**
* Add an enrollment embedding to the index.
*/
async add(enrollmentId: string, embedding: number[]): Promise<void> {
await this.initialize();
// TODO: Add to FAISS index
// this.index.add([embedding]);
// Store mapping: enrollmentId -> index position
console.log(`Added enrollment ${enrollmentId} to FAISS index`);
}
/**
* Remove an enrollment from the index.
*/
async remove(enrollmentId: string): Promise<void> {
await this.initialize();
// TODO: Remove from FAISS index
console.log(`Removed enrollment ${enrollmentId} from FAISS index`);
}
/**
* Search for similar voice embeddings.
*/
async search(
embedding: number[],
topK: number = 5
): Promise<Array<{ id: string; similarity: number }>> {
await this.initialize();
// TODO: Query FAISS index
// const [distances, indices] = this.index.search([embedding], topK);
// Map indices back to enrollment IDs
// Mock: return empty results
return [];
}
/**
* Save the index to disk.
*/
async save(): Promise<void> {
await this.initialize();
// TODO: Write FAISS index to disk
console.log(`FAISS index saved to ${this.indexPath}`);
}
}
// Export singleton instances
// Export singleton instances for backwards compatibility
export const audioPreprocessor = new AudioPreprocessor();
export const voiceEnrollmentService = new VoiceEnrollmentService();
export const analysisService = new AnalysisService();