Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve recorder #1001

Merged
merged 4 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions enjoy/src/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"durationTooShort": "Duration too short",
"failedToSave": "Failed to save recording",
"notFound": "Recording not found",
"cannotDetectAnySound": "Cannot detect any sound"
"cannotDetectAnySound": "Cannot detect any sound in the recording, please check your microphone"
},
"conversation": {
"name": "Name",
Expand Down Expand Up @@ -737,5 +737,6 @@
"waveforms": "Contains all waveforms decoded from audio/videos. They're for caching. It's save to delete them.",
"logs": "Contains some logs helpful for debugging.",
"cache": "Contains cached files. They will be cleaned up automatically."
}
},
"recordingIsTooLongToAssess": "Recording is too long to assess. The maximum duration is 60 seconds."
}
5 changes: 3 additions & 2 deletions enjoy/src/i18n/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"durationTooShort": "录音时长太短",
"failedToSave": "保存录音失败",
"notFound": "未找到录音",
"cannotDetectAnySound": "未检测到任何声音"
"cannotDetectAnySound": "录音中未检测到任何声音,请检查您的录音设备是否正常后重试"
},
"conversation": {
"name": "对话标题",
Expand Down Expand Up @@ -737,5 +737,6 @@
"waveforms": "波形文件,用于显示音频波形。用作缓存,可以删除",
"logs": "日志文件,帮助开发者调试问题",
"cache": "缓存文件,会自动清理。"
}
},
"recordingIsTooLongToAssess": "录音时长过长,无法评估。最长支持 1 分钟。"
}
9 changes: 5 additions & 4 deletions enjoy/src/main/db/models/recording.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,10 +254,9 @@ export class Recording extends Model<Recording> {
) {
const { targetId, targetType, referenceId, referenceText, language } =
params;
let { duration } = params;

if (blob.arrayBuffer.byteLength === 0) {
throw new Error("Empty recording");
throw new Error(t("models.recording.cannotDetectAnySound"));
}

let rawAudio = await echogarden.ensureRawAudio(
Expand All @@ -270,10 +269,12 @@ export class Recording extends Model<Recording> {
0,
-50
);
trimmedSamples = echogarden.trimAudioEnd(trimmedSamples, 0, -50);
trimmedSamples = echogarden.trimAudioEnd(trimmedSamples, 0, -100);
rawAudio.audioChannels[0] = trimmedSamples;

duration = Math.round(echogarden.getRawAudioDuration(rawAudio) * 1000);
const duration = Math.round(
echogarden.getRawAudioDuration(rawAudio) * 1000
);

if (duration === 0) {
throw new Error(t("models.recording.cannotDetectAnySound"));
Expand Down
59 changes: 15 additions & 44 deletions enjoy/src/renderer/components/medias/media-current-recording.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -693,57 +693,27 @@ export const MediaCurrentRecording = () => {
};

export const MediaRecordButton = () => {
const {
media,
recordingBlob,
isRecording,
startRecording,
stopRecording,
recordingTime,
transcription,
currentSegmentIndex,
} = useContext(MediaPlayerProviderContext);
const { media, isRecording, startRecording, stopRecording, recordingTime } =
useContext(MediaPlayerProviderContext);
const { EnjoyApp } = useContext(AppSettingsProviderContext);
const [access, setAccess] = useState(true);
const [active, setActive] = useState(false);
const ref = useRef(null);

const createRecording = async (blob: Blob) => {
const currentSegment =
transcription?.result?.timeline?.[currentSegmentIndex];
if (!currentSegment) return;

EnjoyApp.recordings
.create({
targetId: media.id,
targetType: media.mediaType,
blob: {
type: recordingBlob.type.split(";")[0],
arrayBuffer: await blob.arrayBuffer(),
},
referenceId: currentSegmentIndex,
referenceText: currentSegment.text,
})
.then(() =>
toast.success(t("recordingSaved"), { position: "bottom-right" })
)
.catch((err) =>
toast.error(t("failedToSaveRecording" + " : " + err.message))
);
const askForMediaAccess = () => {
EnjoyApp.system.preferences.mediaAccess("microphone").then((access) => {
if (access) {
setAccess(true);
} else {
setAccess(false);
toast.warning(t("noMicrophoneAccess"));
}
});
};

/*
* Save recording
* when recording is stopped
* And only when record button is active
*/
useEffect(() => {
if (!media) return;
if (!transcription) return;
if (!active) return;
if (!recordingBlob) return;

createRecording(recordingBlob);
}, [recordingBlob, media, transcription, active]);
askForMediaAccess();
}, [media]);

useEffect(() => {
if (!active) return;
Expand All @@ -769,6 +739,7 @@ export const MediaRecordButton = () => {
<Button
ref={ref}
variant="ghost"
disabled={!access}
onClick={() => {
if (isRecording) {
stopRecording();
Expand Down
Loading
Loading