-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathmain.ts
107 lines (95 loc) · 2.98 KB
/
main.ts
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
import { Plugin } from "obsidian";
import { Timer } from "src/Timer";
import { Controls } from "src/Controls";
import { AudioHandler } from "src/AudioHandler";
import { WhisperSettingsTab } from "src/WhisperSettingsTab";
import { SettingsManager, WhisperSettings } from "src/SettingsManager";
import { NativeAudioRecorder } from "src/AudioRecorder";
import { RecordingStatus, StatusBar } from "src/StatusBar";
export default class Whisper extends Plugin {
settings: WhisperSettings;
settingsManager: SettingsManager;
timer: Timer;
recorder: NativeAudioRecorder;
audioHandler: AudioHandler;
controls: Controls | null = null;
statusBar: StatusBar;
async onload() {
this.settingsManager = new SettingsManager(this);
this.settings = await this.settingsManager.loadSettings();
this.addRibbonIcon("activity", "Open recording controls", (evt) => {
if (!this.controls) {
this.controls = new Controls(this);
}
this.controls.open();
});
this.addSettingTab(new WhisperSettingsTab(this.app, this));
this.timer = new Timer();
this.audioHandler = new AudioHandler(this);
this.recorder = new NativeAudioRecorder();
this.statusBar = new StatusBar(this);
this.addCommands();
}
onunload() {
if (this.controls) {
this.controls.close();
}
this.statusBar.remove();
}
addCommands() {
this.addCommand({
id: "start-stop-recording",
name: "Start/stop recording",
callback: async () => {
if (this.statusBar.status !== RecordingStatus.Recording) {
this.statusBar.updateStatus(RecordingStatus.Recording);
await this.recorder.startRecording();
} else {
this.statusBar.updateStatus(RecordingStatus.Processing);
const audioBlob = await this.recorder.stopRecording();
const extension = this.recorder
.getMimeType()
?.split("/")[1];
const fileName = `${new Date()
.toISOString()
.replace(/[:.]/g, "-")}.${extension}`;
// Use audioBlob to send or save the recorded audio as needed
await this.audioHandler.sendAudioData(audioBlob, fileName);
this.statusBar.updateStatus(RecordingStatus.Idle);
}
},
hotkeys: [
{
modifiers: ["Alt"],
key: "Q",
},
],
});
this.addCommand({
id: "upload-audio-file",
name: "Upload audio file",
callback: () => {
// Create an input element for file selection
const fileInput = document.createElement("input");
fileInput.type = "file";
fileInput.accept = "audio/*"; // Accept only audio files
// Handle file selection
fileInput.onchange = async (event) => {
const files = (event.target as HTMLInputElement).files;
if (files && files.length > 0) {
const file = files[0];
const fileName = file.name;
const audioBlob = file.slice(0, file.size, file.type);
// Use audioBlob to send or save the uploaded audio as needed
await this.audioHandler.sendAudioData(
audioBlob,
fileName
);
}
};
// Programmatically open the file dialog
fileInput.click();
},
});
}
}