Skip to content

Commit

Permalink
mt fix on audio recorderw
Browse files Browse the repository at this point in the history
  • Loading branch information
Fy- committed May 3, 2024
1 parent 8c9b571 commit bc81df8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 41 deletions.
28 changes: 7 additions & 21 deletions packages/klb-vue/src/composables/audioRecorder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ export function useAudioRecorder(
) {
const audioContext = ref<AudioContext | null>(null);
const mediaRecorder = ref<MediaRecorder | null>(null);
const audioChunks = ref<Blob[]>([]);
const analyser = ref<AnalyserNode | null>(null);
const recording = ref(false);
const silenceStart = ref(performance.now());
const isSilent = ref(false);
const hasAudioBeenRecorded = ref(false);
const shouldCallOnAudioAvailable = ref(false);
console.log("init object");

const isRecording = () => {
return mediaRecorder.value && mediaRecorder.value.state === "recording";
Expand Down Expand Up @@ -51,7 +51,7 @@ export function useAudioRecorder(

if (!shouldCallOnAudioAvailable.value) {
shouldCallOnAudioAvailable.value = true;
mediaRecorder.value?.requestData();
mediaRecorder.value?.stop(); // stop() will trigger the dataavailable
}
}
}
Expand All @@ -72,7 +72,6 @@ export function useAudioRecorder(
// Init everything

recording.value = false;
audioChunks.value = [];
hasAudioBeenRecorded.value = false;
isSilent.value = false;
silenceStart.value = performance.now();
Expand All @@ -91,32 +90,20 @@ export function useAudioRecorder(

mediaRecorder.value.addEventListener("dataavailable", (event) => {
if (event.data.size > 0) {
if (shouldCallOnAudioAvailable.value) {
// add the last chunk
audioChunks.value.push(event.data);
onAudioAvailableCallback(
audioChunks.value.length > 1
? new Blob(audioChunks.value)
: audioChunks.value[0],
);
stopRecording();
audioChunks.value = [];
shouldCallOnAudioAvailable.value = false;
silenceStart.value = performance.now();
} else {
audioChunks.value.push(event.data);
}
onAudioAvailableCallback(event.data);
stopRecording();
shouldCallOnAudioAvailable.value = false;
silenceStart.value = performance.now();
}
});

mediaRecorder.value.start(200);
mediaRecorder.value.start();
recording.value = true;

processAudioStream();
};
const stopRecording = async () => {
recording.value = false;
audioChunks.value = [];
hasAudioBeenRecorded.value = false;
isSilent.value = false;
silenceStart.value = performance.now();
Expand All @@ -142,7 +129,6 @@ export function useAudioRecorder(
analyser.value = null;
}
recording.value = false;
audioChunks.value = [];
};

const resumeRecording = () => {
Expand Down
39 changes: 19 additions & 20 deletions packages/klb-vue/src/composables/klbAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ export function useKlbAgent() {
session.value = r.data.HIAgent_Session__;
videoIdleURL.value = r.data.Idle_Url;
bgImage.value = r.data.Background;
if (idleVideoElement.value) {
idleVideoElement.value.muted = true;
idleVideoElement.value.loop = true;
}
setTimeout(() => {
playIdleVideo();
}, 1000);
Expand All @@ -47,32 +43,35 @@ export function useKlbAgent() {
eventBus.emit("main-loading", false);
}
};
const onAudioAvailableCallback = (chunks: Blob) => {
const onAudioAvailableCallback = (blob: Blob) => {
const filename = `audio_${Date.now()}.webm`; // Example filename with timestamp
const file = new File([chunks], filename, { type: "audio/webm" });
const file = new File([blob], filename, { type: "audio/webm" });
upload.append(`HIAgent/Session/${session.value}:input`, file);
upload.run();
};
const playIdleVideo = () => {
if (videoElement.value) {
videoElement.value.classList.add("hidden");
}
if (!videoElement.value) return;
videoElement.value.classList.add("hidden");
if (!audioRecorder.isRecording()) {
audioRecorder.startRecording();
}
if (idleVideoElement.value) {
idleVideoElement.value.muted = true;
idleVideoElement.value.loop = true;
if (idleVideoElement.value.src !== videoIdleURL.value) {
idleVideoElement.value.src = videoIdleURL.value;
}
if (!idleVideoElement.value) return;

idleVideoElement.value.play().catch((e) => {
console.error("Failed to play idle video", e);
});
} else {
console.error("idle video element not found");
// @ts-ignore
if (idleVideoElement.value.src === videoIdleURL.value) {
if (idleVideoElement.value.paused) {
idleVideoElement.value.play().catch((e) => {
console.error("Failed to play idle video", e);
});
}
return;
}
idleVideoElement.value.src = videoIdleURL.value;
idleVideoElement.value.loop = true;
idleVideoElement.value.muted = true;
idleVideoElement.value.play().catch((e) => {
console.error("Failed to play idle video", e);
});
};
const setVideoElement = (stream: MediaStream) => {
if (!stream) return;
Expand Down

0 comments on commit bc81df8

Please sign in to comment.