-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathSmartMemosAudioRecordModal.ts
More file actions
262 lines (226 loc) · 11 KB
/
Copy pathSmartMemosAudioRecordModal.ts
File metadata and controls
262 lines (226 loc) · 11 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
import { Modal, setIcon } from 'obsidian';
export class SmartMemosAudioRecordModal extends Modal {
private mediaRecorder: MediaRecorder | null = null;
private chunks: BlobPart[] = [];
private resolve: (value: Blob | PromiseLike<Blob>) => void;
private reject: (reason?: any) => void;
private handleAudioRecording: (audioFile: Blob | null, transcribe: boolean, keepAudio: boolean, includeAudioFileLink: boolean) => void;
private isRecording: boolean = false;
private timer: HTMLElement;
private intervalId: number | null = null;
private startTime: number = 0;
private elapsedTime: number = 0; // To keep track of the elapsed time
private redDot: HTMLElement;
private isResetting: boolean = false; // Flag to track reset state
private keepAudioCheckbox: HTMLElement; // Add a property for the checkbox
private includeAudioFileLinkCheckbox: HTMLElement; // Add a property for the checkbox
private settings: any;
constructor(app: any, handleAudioRecording: (audioFile: Blob | null, transcribe: boolean, keepAudio: boolean, includeAudioFileLink: boolean) => void, settings: any) {
super(app);
this.handleAudioRecording = handleAudioRecording;
this.settings = settings; // Initialize settings
}
onOpen() {
const { contentEl, modalEl } = this;
if (!contentEl || !modalEl) {
console.error('contentEl or modalEl is null');
return;
}
// Apply initial recording state
modalEl.addClass('smart-memo-recording');
// Header and timer container
const headerTimerContainer = contentEl.createDiv({ cls: 'smart-memo-header-timer-container' });
const header = headerTimerContainer.createEl('h2', { text: 'Recording...', cls: 'smart-memo-recording-header' });
this.timer = headerTimerContainer.createEl('div', { cls: 'smart-memo-timer', text: '00:00' });
// Add specific class to modal-content
contentEl.addClass('smart-memo-audio-record-modal-content');
// Red dot animation container
const redDotContainer = contentEl.createDiv({ cls: 'smart-memo-red-dot-container' });
this.redDot = redDotContainer.createDiv({ cls: 'smart-memo-red-dot' });
// Control buttons group
const controlGroupWrapper = contentEl.createDiv({ cls: 'smart-memo-control-group-wrapper' });
const controlGroup = controlGroupWrapper.createDiv({ cls: 'smart-memo-modal-button-group' });
const playPauseButton = controlGroup.createEl('button', { cls: 'smart-memo-modal-button smart-memo-flex' });
const stopButton = controlGroup.createEl('button', { cls: 'smart-memo-modal-button smart-memo-flex' });
setIcon(playPauseButton, 'pause'); // Initially set to pause
setIcon(stopButton, 'square'); // Stop icon
stopButton.addEventListener('click', async () => {
const audioFile = await this.stopRecording();
this.handleAudioRecording(audioFile, false, (this.keepAudioCheckbox as HTMLInputElement).checked, (this.includeAudioFileLinkCheckbox as HTMLInputElement).checked);
});
playPauseButton.addEventListener('click', () => {
if (this.isRecording) {
this.pauseRecording();
setIcon(playPauseButton, 'circle');
header.textContent = 'Paused';
modalEl.addClass('smart-memo-paused');
modalEl.removeClass('smart-memo-recording');
} else {
this.resumeOrStartRecording();
setIcon(playPauseButton, 'pause');
header.textContent = 'Recording...';
modalEl.removeClass('smart-memo-paused');
modalEl.addClass('smart-memo-recording');
}
this.isRecording = !this.isRecording;
});
const transcribeButton = controlGroupWrapper.createEl('button', { cls: 'smart-memo-modal-button smart-memo-full-width-button smart-memo-transcribe-button' });
transcribeButton.addEventListener('click', async () => {
const audioFile = await this.stopRecording();
this.handleAudioRecording(audioFile, true, (this.keepAudioCheckbox as HTMLInputElement).checked, (this.includeAudioFileLinkCheckbox as HTMLInputElement).checked);
});
setIcon(transcribeButton, 'file-text'); // Initially set to bulb
// Append text to the button
const buttonText = document.createTextNode(' Smart Transcribe');
transcribeButton.appendChild(buttonText);
// Add margin-right to the SVG element
const svgElement = transcribeButton.querySelector('svg');
if (svgElement) {
svgElement.style.marginRight = '10px';
}
const resetButton = contentEl.createEl('button', { cls: 'smart-memo-modal-button smart-memo-full-width-button smart-memo-reset-button', text: 'Restart' });
resetButton.addEventListener('click', () => {
this.hardReset();
setIcon(playPauseButton, 'circle');
header.textContent = 'Ready to Record';
this.isRecording = false;
modalEl.addClass('smart-memo-paused');
modalEl.removeClass('smart-memo-recording');
// Ensure red dot stops pulsing
this.redDot.classList.remove('smart-memo-pulse-animation');
});
// Add the checkbox
const keepAudioContainer = contentEl.createDiv({ cls: 'smart-memo-keep-audio-container' });
this.keepAudioCheckbox = keepAudioContainer.createEl('input', { type: 'checkbox', cls: 'smart-memo-keep-audio-checkbox' });
(this.keepAudioCheckbox as HTMLInputElement).checked = this.settings.keepAudio // Set checked based on settings;
const keepAudioLabel = keepAudioContainer.createEl('label', { text: 'Keep Audio File', cls: 'smart-memo-keep-audio-label' });
keepAudioLabel.htmlFor = this.keepAudioCheckbox.id;
// Add the checkbox for including audio file link
const includeAudioFileLinkContainer = contentEl.createDiv({ cls: 'smart-memo-include-audio-file-link-container' });
this.includeAudioFileLinkCheckbox = includeAudioFileLinkContainer.createEl('input', { type: 'checkbox', cls: 'smart-memo-include-audio-file-link-checkbox' });
(this.includeAudioFileLinkCheckbox as HTMLInputElement).checked = this.settings.includeAudioFileLink; // Set checked based on settings
const includeAudioFileLinkLabel = includeAudioFileLinkContainer.createEl('label', { text: 'Include Audio File Player', cls: 'smart-memo-include-audio-file-link-label' });
includeAudioFileLinkLabel.htmlFor = this.includeAudioFileLinkCheckbox.id;
// Start recording immediately upon opening the modal
this.startRecording();
this.isRecording = true;
this.redDot.classList.add('smart-memo-pulse-animation');
// Blur any focused element
const activeElement = document.activeElement as HTMLElement;
if (activeElement) {
activeElement.blur();
}
}
startRecording() {
navigator.mediaDevices.getUserMedia({ audio: true })
.then(stream => {
this.mediaRecorder = new MediaRecorder(stream);
this.setupMediaRecorder();
this.mediaRecorder.start(1000);
this.startTime = Date.now();
this.startTimer();
this.mediaRecorder.addEventListener('dataavailable', this.onDataAvailable.bind(this));
})
.catch(error => {
console.error('Error accessing microphone:', error);
this.reject(error);
});
}
setupMediaRecorder() {
if (this.mediaRecorder) {
this.mediaRecorder.addEventListener('stop', this.onStop.bind(this));
}
}
onDataAvailable(event: BlobEvent) {
if (this.isResetting) {
return;
}
this.chunks.push(event.data);
}
onStop() {
if (this.isResetting) {
this.isResetting = false; // Reset the flag after reset
return;
}
const blob = new Blob(this.chunks, { type: 'audio/wav' });
if (this.resolve) {
this.resolve(blob);
this.close();
} else {
console.error('Resolve function is not defined');
}
}
pauseRecording() {
if (this.mediaRecorder && this.mediaRecorder.state === 'recording') {
this.mediaRecorder.pause();
this.stopTimer();
this.elapsedTime += Date.now() - this.startTime; // Accumulate elapsed time
// Ensure red dot stops pulsing
this.redDot.classList.remove('smart-memo-pulse-animation');
}
}
resumeOrStartRecording() {
if (this.mediaRecorder && this.mediaRecorder.state === 'paused') {
this.mediaRecorder.resume();
} else {
this.startRecording();
}
this.startTime = Date.now(); // Reset start time to now
this.startTimer();
// Ensure red dot starts pulsing
this.redDot.classList.add('smart-memo-pulse-animation');
}
hardReset() {
if (this.mediaRecorder) {
this.mediaRecorder.stop();
this.mediaRecorder.onstop = null;
this.mediaRecorder.ondataavailable = null;
this.mediaRecorder = null;
}
this.isResetting = true; // Set the reset flag
this.chunks = []; // Clear the chunks
this.elapsedTime = 0; // Reset elapsed time
this.stopTimer();
this.timer.textContent = '00:00';
// Ensure red dot stops pulsing
this.redDot.classList.remove('smart-memo-pulse-animation'); // Ensure it's removed on reset
}
stopRecording() {
return new Promise<Blob | null>((resolve, reject) => {
this.resolve = resolve;
this.reject = reject;
if (this.mediaRecorder) {
this.mediaRecorder.addEventListener('stop', this.onStop.bind(this));
this.mediaRecorder.stop();
this.stopTimer();
} else {
resolve(null);
}
});
}
startTimer() {
this.stopTimer(); // Clear any existing timer
this.intervalId = window.setInterval(() => {
const elapsedTimeInSeconds = Math.floor(this.elapsedTime / 1000) + Math.floor((Date.now() - this.startTime) / 1000);
const minutes = Math.floor(elapsedTimeInSeconds / 60);
const seconds = elapsedTimeInSeconds % 60;
this.timer.textContent = `${this.padNumber(minutes)}:${this.padNumber(seconds)}`;
}, 1000);
}
stopTimer() {
if (this.intervalId !== null) {
window.clearInterval(this.intervalId);
this.intervalId = null;
}
}
padNumber(num: number): string {
return num.toString().padStart(2, '0');
}
open() {
super.open();
return new Promise<Blob>((resolve, reject) => {
this.resolve = resolve;
this.reject = reject;
});
}
}