Skip to content

Commit

Permalink
upload save record wav file
Browse files Browse the repository at this point in the history
  • Loading branch information
lloydzhou committed Nov 7, 2024
1 parent 5226278 commit db060d7
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 13 deletions.
39 changes: 27 additions & 12 deletions app/components/realtime-chat/realtime-chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ export function RealtimeChat({

const handleResponse = async (response: RTResponse) => {
for await (const item of response) {
console.log("handleResponse", item);
if (item.type === "message" && item.role === "assistant") {
const botMessage = createMessage({
role: item.role,
Expand Down Expand Up @@ -156,12 +155,16 @@ export function RealtimeChat({
};
await Promise.all([textTask(), audioTask()]);
}
// update message.content
chatStore.updateTargetSession((session) => {
session.messages = session.messages.concat();
});
}
// upload audio get audio_url
const blob = audioHandlerRef.current?.savePlayFile();
uploadImage(blob).then((audio_url) => {
botMessage.audio_url = audio_url;
botMessage.date = new Date().toLocaleString();
// botMessage.date = new Date().toLocaleString();
// update text and audio_url
chatStore.updateTargetSession((session) => {
session.messages = session.messages.concat();
Expand All @@ -174,16 +177,28 @@ export function RealtimeChat({
const handleInputAudio = async (item: RTInputAudioItem) => {
audioHandlerRef.current?.stopStreamingPlayback();
await item.waitForCompletion();
const { audioStartMillis, audioEndMillis } = item;
// TODO, save input audio_url, and update session
console.log("handleInputAudio", item, audioStartMillis, audioEndMillis);
const userMessage = createMessage({
role: "user",
content: item.transcription,
});
chatStore.updateTargetSession(session, (session) => {
session.messages = session.messages.concat([userMessage]);
});
if (item.transcription) {
const userMessage = createMessage({
role: "user",
content: item.transcription,
});
chatStore.updateTargetSession(session, (session) => {
session.messages = session.messages.concat([userMessage]);
});
// save input audio_url, and update session
const { audioStartMillis, audioEndMillis } = item;
// upload audio get audio_url
const blob = audioHandlerRef.current?.saveRecordFile(
audioStartMillis,
audioEndMillis,
);
uploadImage(blob).then((audio_url) => {
userMessage.audio_url = audio_url;
chatStore.updateTargetSession((session) => {
session.messages = session.messages.concat();
});
});
}
};

const toggleRecording = async () => {
Expand Down
18 changes: 18 additions & 0 deletions app/lib/audio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export class AudioHandler {
private workletNode: AudioWorkletNode | null = null;
private stream: MediaStream | null = null;
private source: MediaStreamAudioSourceNode | null = null;
private recordBuffer: Int16Array[] = [];
private readonly sampleRate = 24000;

private nextPlayTime: number = 0;
Expand Down Expand Up @@ -52,6 +53,8 @@ export class AudioHandler {

const uint8Data = new Uint8Array(int16Data.buffer);
onChunk(uint8Data);
// save recordBuffer
this.recordBuffer.push.apply(this.recordBuffer, int16Data);
}
};

Expand Down Expand Up @@ -154,7 +157,22 @@ export class AudioHandler {
savePlayFile() {
return this._saveData(new Int16Array(this.playBuffer));
}
saveRecordFile(
audioStartMillis: number | undefined,
audioEndMillis: number | undefined,
) {
const startIndex = audioStartMillis
? Math.floor((audioStartMillis * this.sampleRate) / 1000)
: 0;
const endIndex = audioEndMillis
? Math.floor((audioEndMillis * this.sampleRate) / 1000)
: this.recordBuffer.length;
return this._saveData(
new Int16Array(this.recordBuffer.slice(startIndex, endIndex)),
);
}
async close() {
this.recordBuffer = [];
this.workletNode?.disconnect();
this.source?.disconnect();
this.stream?.getTracks().forEach((track) => track.stop());
Expand Down
2 changes: 1 addition & 1 deletion app/utils/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export function uploadImage(file: Blob): Promise<string> {
})
.then((res) => res.json())
.then((res) => {
console.log("res", res);
// console.log("res", res);
if (res?.code == 0 && res?.data) {
return res?.data;
}
Expand Down

0 comments on commit db060d7

Please sign in to comment.