-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_diarization_client.js
More file actions
160 lines (137 loc) · 4.82 KB
/
Copy pathtest_diarization_client.js
File metadata and controls
160 lines (137 loc) · 4.82 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
/**
* Test client for the diarization server
* This script connects to the diarization server, sends audio data, and logs the results
*
* Usage: node test_diarization_client.js
*/
const WebSocket = require("ws");
const fs = require("fs");
const path = require("path");
class DiarizationTestClient {
constructor(serverUrl = "ws://localhost:8000/ws/diarize") {
this.serverUrl = serverUrl;
this.socket = null;
this.connected = false;
}
connect() {
return new Promise((resolve, reject) => {
console.log(`Attempting to connect to ${this.serverUrl}...`);
try {
this.socket = new WebSocket(this.serverUrl);
this.socket.on("open", () => {
console.log("Connection established!");
this.connected = true;
resolve();
});
this.socket.on("message", (data) => {
try {
const message = JSON.parse(data);
console.log(`Received message type: ${message.type || "unknown"}`);
console.log("Full message:", JSON.stringify(message, null, 2));
// Check if it's a speaker_transcription_update message
if (
message.type === "speaker_transcription_update" &&
message.segments
) {
// Sort segments by start time to ensure chronological order
const sortedSegments = [...message.segments].sort(
(a, b) => a.start - b.start
);
// Process segments to merge consecutive segments from the same speaker
const mergedSegments = [];
sortedSegments.forEach((segment) => {
const lastSegment =
mergedSegments.length > 0
? mergedSegments[mergedSegments.length - 1]
: null;
// If this segment is from the same speaker as the last one, merge them
if (lastSegment && lastSegment.speaker === segment.speaker) {
lastSegment.text = `${lastSegment.text} ${segment.text}`;
lastSegment.end = segment.end; // Update the end time
} else {
// Otherwise add as a new segment
mergedSegments.push({ ...segment });
}
});
console.log("Processed segments (sorted and merged):");
mergedSegments.forEach((segment) => {
console.log(
` ${segment.speaker}: "${segment.text}" (${segment.start}s - ${segment.end}s)`
);
});
}
} catch (error) {
console.error("Error parsing message:", error);
}
});
this.socket.on("error", (error) => {
console.error("WebSocket error:", error);
reject(error);
});
this.socket.on("close", (code, reason) => {
console.log(`Connection closed: ${code} ${reason}`);
this.connected = false;
});
} catch (error) {
console.error("Failed to create WebSocket connection:", error);
reject(error);
}
});
}
sendAudioFile(filePath) {
return new Promise((resolve, reject) => {
if (!this.connected || !this.socket) {
reject(new Error("WebSocket not connected"));
return;
}
try {
console.log(`Reading audio file: ${filePath}`);
const audioData = fs.readFileSync(filePath);
console.log(
`Successfully read audio file. Size: ${audioData.length} bytes`
);
console.log(`>>> Sending ${audioData.length} bytes of audio...`);
this.socket.send(audioData);
console.log("Audio data sent successfully");
resolve();
} catch (error) {
console.error("Error sending audio file:", error);
reject(error);
}
});
}
disconnect() {
if (this.socket) {
this.socket.close();
this.socket = null;
this.connected = false;
console.log("Disconnected from server");
}
}
}
// Main execution
async function main() {
// Check if audio file path is provided
const audioFilePath = process.argv[2] || "wave_16k.wav";
if (!fs.existsSync(audioFilePath)) {
console.error(`Error: Audio file not found: ${audioFilePath}`);
console.log("Usage: node test_diarization_client.js [path_to_audio_file]");
process.exit(1);
}
const client = new DiarizationTestClient();
try {
await client.connect();
await client.sendAudioFile(audioFilePath);
// Keep the connection open for a while to receive results
console.log("Waiting for diarization results...");
setTimeout(() => {
client.disconnect();
console.log("Test completed");
}, 10000); // Wait 10 seconds for results
} catch (error) {
console.error("Test failed:", error);
client.disconnect();
process.exit(1);
}
}
main();