Skip to content

Commit db060d7

Browse files
committed
upload save record wav file
1 parent 5226278 commit db060d7

File tree

3 files changed

+46
-13
lines changed

3 files changed

+46
-13
lines changed

app/components/realtime-chat/realtime-chat.tsx

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ export function RealtimeChat({
127127

128128
const handleResponse = async (response: RTResponse) => {
129129
for await (const item of response) {
130-
console.log("handleResponse", item);
131130
if (item.type === "message" && item.role === "assistant") {
132131
const botMessage = createMessage({
133132
role: item.role,
@@ -156,12 +155,16 @@ export function RealtimeChat({
156155
};
157156
await Promise.all([textTask(), audioTask()]);
158157
}
158+
// update message.content
159+
chatStore.updateTargetSession((session) => {
160+
session.messages = session.messages.concat();
161+
});
159162
}
160163
// upload audio get audio_url
161164
const blob = audioHandlerRef.current?.savePlayFile();
162165
uploadImage(blob).then((audio_url) => {
163166
botMessage.audio_url = audio_url;
164-
botMessage.date = new Date().toLocaleString();
167+
// botMessage.date = new Date().toLocaleString();
165168
// update text and audio_url
166169
chatStore.updateTargetSession((session) => {
167170
session.messages = session.messages.concat();
@@ -174,16 +177,28 @@ export function RealtimeChat({
174177
const handleInputAudio = async (item: RTInputAudioItem) => {
175178
audioHandlerRef.current?.stopStreamingPlayback();
176179
await item.waitForCompletion();
177-
const { audioStartMillis, audioEndMillis } = item;
178-
// TODO, save input audio_url, and update session
179-
console.log("handleInputAudio", item, audioStartMillis, audioEndMillis);
180-
const userMessage = createMessage({
181-
role: "user",
182-
content: item.transcription,
183-
});
184-
chatStore.updateTargetSession(session, (session) => {
185-
session.messages = session.messages.concat([userMessage]);
186-
});
180+
if (item.transcription) {
181+
const userMessage = createMessage({
182+
role: "user",
183+
content: item.transcription,
184+
});
185+
chatStore.updateTargetSession(session, (session) => {
186+
session.messages = session.messages.concat([userMessage]);
187+
});
188+
// save input audio_url, and update session
189+
const { audioStartMillis, audioEndMillis } = item;
190+
// upload audio get audio_url
191+
const blob = audioHandlerRef.current?.saveRecordFile(
192+
audioStartMillis,
193+
audioEndMillis,
194+
);
195+
uploadImage(blob).then((audio_url) => {
196+
userMessage.audio_url = audio_url;
197+
chatStore.updateTargetSession((session) => {
198+
session.messages = session.messages.concat();
199+
});
200+
});
201+
}
187202
};
188203

189204
const toggleRecording = async () => {

app/lib/audio.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export class AudioHandler {
33
private workletNode: AudioWorkletNode | null = null;
44
private stream: MediaStream | null = null;
55
private source: MediaStreamAudioSourceNode | null = null;
6+
private recordBuffer: Int16Array[] = [];
67
private readonly sampleRate = 24000;
78

89
private nextPlayTime: number = 0;
@@ -52,6 +53,8 @@ export class AudioHandler {
5253

5354
const uint8Data = new Uint8Array(int16Data.buffer);
5455
onChunk(uint8Data);
56+
// save recordBuffer
57+
this.recordBuffer.push.apply(this.recordBuffer, int16Data);
5558
}
5659
};
5760

@@ -154,7 +157,22 @@ export class AudioHandler {
154157
savePlayFile() {
155158
return this._saveData(new Int16Array(this.playBuffer));
156159
}
160+
saveRecordFile(
161+
audioStartMillis: number | undefined,
162+
audioEndMillis: number | undefined,
163+
) {
164+
const startIndex = audioStartMillis
165+
? Math.floor((audioStartMillis * this.sampleRate) / 1000)
166+
: 0;
167+
const endIndex = audioEndMillis
168+
? Math.floor((audioEndMillis * this.sampleRate) / 1000)
169+
: this.recordBuffer.length;
170+
return this._saveData(
171+
new Int16Array(this.recordBuffer.slice(startIndex, endIndex)),
172+
);
173+
}
157174
async close() {
175+
this.recordBuffer = [];
158176
this.workletNode?.disconnect();
159177
this.source?.disconnect();
160178
this.stream?.getTracks().forEach((track) => track.stop());

app/utils/chat.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ export function uploadImage(file: Blob): Promise<string> {
138138
})
139139
.then((res) => res.json())
140140
.then((res) => {
141-
console.log("res", res);
141+
// console.log("res", res);
142142
if (res?.code == 0 && res?.data) {
143143
return res?.data;
144144
}

0 commit comments

Comments
 (0)