-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
326 lines (282 loc) · 8.95 KB
/
main.ts
File metadata and controls
326 lines (282 loc) · 8.95 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
import {
App,
Editor,
MarkdownView,
Modal,
Notice,
Plugin,
PluginSettingTab,
Setting,
EditorPosition,
} from "obsidian";
import { createClient, Deepgram, LiveTranscriptionEvents } from "@deepgram/sdk";
interface DeepGramPluginSettings {
apiKey: string;
summarize: boolean;
topic_detection: boolean;
intent_detection: boolean;
sentiment: boolean;
smart_format: boolean;
punctuation: boolean;
paragraphs: boolean;
utterances: boolean;
filler_words: boolean;
}
const DEFAULT_SETTINGS: DeepGramPluginSettings = {
apiKey: "",
summarize: false,
topic_detection: false,
intent_detection: false,
sentiment: false,
smart_format: true,
punctuation: true,
paragraphs: false,
utterances: false,
filler_words: false,
};
export default class DeepgramPlugin extends Plugin {
settings: DeepGramPluginSettings;
isRecording: boolean = false;
statusBarItem: HTMLElement;
initialCursorPosition: EditorPosition;
deepgram: Deepgram; // Add this line to declare the deepgram property
mediaRecorder: MediaRecorder; // Add this line to declare the mediaRecorder property
async onload() {
console.log("Loading Deepgram plugin");
await this.loadSettings();
this.addCommand({
id: "toggle-transcription",
name: "Toggle Transcription",
callback: () => {
if (this.isRecording) {
this.stopTranscription();
} else {
this.startTranscription();
}
},
});
this.statusBarItem = this.addStatusBarItem();
this.updateStatusBarItem();
this.addSettingTab(new DeepgramSettingTab(this.app, this));
console.log("Deepgram plugin loaded");
}
async onunload() {
console.log("Unloading Deepgram plugin");
if (this.isRecording) {
await this.stopTranscription();
}
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
updateStatusBarItem() {
this.statusBarItem.setText(
this.isRecording ? "Recording" : "Not Recording"
);
}
async startTranscription() {
console.log("Starting transcription");
const options = {
model: "nova-2",
language: "en-US",
smart_format: this.settings.smart_format,
summarize: this.settings.summarize,
topic_detection: this.settings.topic_detection,
intent_detection: this.settings.intent_detection,
sentiment: this.settings.sentiment,
punctuation: this.settings.punctuation,
paragraphs: this.settings.paragraphs,
utterances: this.settings.utterances,
filler_words: this.settings.filler_words,
interim_results: true,
endpointing:2000
};
const deepgramClient = createClient(this.settings.apiKey);
const deepgram = deepgramClient.listen.live(options);
this.deepgram = deepgram;
console.log("Deepgram client created", deepgram);
this.deepgram.addListener(LiveTranscriptionEvents.Open, () => {
console.log("Deepgram connection opened");
this.isRecording = true;
this.updateStatusBarItem();
this.deepgram.addListener(LiveTranscriptionEvents.Transcript, (data) => {
const transcript = data.channel.alternatives[0].transcript;
const isFinal = data.is_final;
const activeView = this.app.workspace.getActiveViewOfType(MarkdownView);
if (activeView) {
const editor = activeView.editor;
const currentPosition = editor.getCursor();
console.log("Transcript: ", transcript);
if (isFinal) {
console.log("Final transcript:", transcript);
const formatted_transcript = transcript + " ";
const startPosition = currentPosition;
editor.replaceRange(formatted_transcript, startPosition);
const endPosition = {
line: currentPosition.line,
ch: currentPosition.ch + formatted_transcript.length,
};
editor.setCursor(endPosition); // Set the cursor at the end of the inserted text
} else {
console.log("Partial transcript:", transcript);
}
}
});
});
this.deepgram.addListener(LiveTranscriptionEvents.Error, (error) => {
console.error("Deepgram error:", error);
this.stopTranscription();
});
const mediaStream = await navigator.mediaDevices.getUserMedia({
audio: true,
});
const mediaRecorder = new MediaRecorder(mediaStream, {
mimeType: "audio/webm",
});
this.mediaRecorder = mediaRecorder;
mediaRecorder.addEventListener("dataavailable", (event) => {
if (event.data.size > 0) {
deepgram.send(event.data);
}
});
mediaRecorder.start(1000);
}
async stopTranscription() {
console.log("Stopping transcription");
this.isRecording = false;
this.updateStatusBarItem();
this.deepgram.finish();
// Stop the MediaRecorder
if (this.mediaRecorder && this.mediaRecorder.state !== "inactive") {
this.mediaRecorder.stop();
}
}
}
class DeepgramSettingTab extends PluginSettingTab {
plugin: DeepgramPlugin;
constructor(app: App, plugin: DeepgramPlugin) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
let { containerEl } = this;
containerEl.empty();
new Setting(containerEl)
.setName("Deepgram API Key")
.setDesc("Enter your Deepgram API key")
.addText((text) =>
text
.setPlaceholder("API Key")
.setValue(this.plugin.settings.apiKey)
.onChange(async (value) => {
console.log("Deepgram API key updated");
this.plugin.settings.apiKey = value;
await this.plugin.saveSettings();
})
);
new Setting(containerEl)
.setName("Summarize")
.setDesc("Enable summarization")
.addToggle((toggle) =>
toggle
.setValue(this.plugin.settings.summarize)
.onChange(async (value) => {
this.plugin.settings.summarize = value;
await this.plugin.saveSettings();
})
);
new Setting(containerEl)
.setName("Topic Detection")
.setDesc("Enable topic detection")
.addToggle((toggle) =>
toggle
.setValue(this.plugin.settings.topic_detection)
.onChange(async (value) => {
this.plugin.settings.topic_detection = value;
await this.plugin.saveSettings();
})
);
new Setting(containerEl)
.setName("Intent Detection")
.setDesc("Enable intent detection")
.addToggle((toggle) =>
toggle
.setValue(this.plugin.settings.intent_detection)
.onChange(async (value) => {
this.plugin.settings.intent_detection = value;
await this.plugin.saveSettings();
})
);
new Setting(containerEl)
.setName("Sentiment")
.setDesc("Enable sentiment analysis")
.addToggle((toggle) =>
toggle
.setValue(this.plugin.settings.sentiment)
.onChange(async (value) => {
this.plugin.settings.sentiment = value;
await this.plugin.saveSettings();
})
);
new Setting(containerEl)
.setName("Smart Formatting")
.setDesc("Enable smart formatting")
.addToggle((toggle) =>
toggle
.setValue(this.plugin.settings.smart_format)
.onChange(async (value) => {
this.plugin.settings.smart_format = value;
await this.plugin.saveSettings();
})
);
new Setting(containerEl)
.setName("Punctuation")
.setDesc("Enable punctuation")
.addToggle((toggle) =>
toggle
.setValue(this.plugin.settings.punctuation)
.onChange(async (value) => {
this.plugin.settings.punctuation = value;
await this.plugin.saveSettings();
})
);
new Setting(containerEl)
.setName("Paragraphs")
.setDesc("Enable paragraph detection")
.addToggle((toggle) =>
toggle
.setValue(this.plugin.settings.paragraphs)
.onChange(async (value) => {
this.plugin.settings.paragraphs = value;
await this.plugin.saveSettings();
})
);
new Setting(containerEl)
.setName("Utterances")
.setDesc("Enable utterance detection")
.addToggle((toggle) =>
toggle
.setValue(this.plugin.settings.utterances)
.onChange(async (value) => {
this.plugin.settings.utterances = value;
await this.plugin.saveSettings();
})
);
new Setting(containerEl)
.setName("Filler Words")
.setDesc("Include filler words")
.addToggle((toggle) =>
toggle
.setValue(this.plugin.settings.filler_words)
.onChange(async (value) => {
this.plugin.settings.filler_words = value;
await this.plugin.saveSettings();
})
);
// Add settings for other transcription options
// ...
}
}