Skip to content

Commit

Permalink
Added keep audio link feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Mossy426 committed Aug 9, 2024
1 parent a6ec2db commit e316c94
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 21 deletions.
17 changes: 13 additions & 4 deletions SmartMemosAudioRecordModal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@ export class SmartMemosAudioRecordModal extends Modal {
private chunks: BlobPart[] = [];
private resolve: (value: Blob | PromiseLike<Blob>) => void;
private reject: (reason?: any) => void;
private handleAudioRecording: (audioFile: Blob | null, transcribe: boolean) => void;
private handleAudioRecording: (audioFile: Blob | null, transcribe: boolean, keepAudio: 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 settings: any;

constructor(app: any, handleAudioRecording: (audioFile: Blob | null, transcribe: boolean) => void) {
constructor(app: any, handleAudioRecording: (audioFile: Blob | null, transcribe: boolean, keepAudio: boolean) => void, settings: any) {
super(app);
this.handleAudioRecording = handleAudioRecording;
this.settings = settings; // Initialize settings
}

onOpen() {
Expand Down Expand Up @@ -53,7 +56,7 @@ export class SmartMemosAudioRecordModal extends Modal {

stopButton.addEventListener('click', async () => {
const audioFile = await this.stopRecording();
this.handleAudioRecording(audioFile, false);
this.handleAudioRecording(audioFile, false, (this.keepAudioCheckbox as HTMLInputElement).checked);
});

playPauseButton.addEventListener('click', () => {
Expand All @@ -77,7 +80,7 @@ export class SmartMemosAudioRecordModal extends Modal {

transcribeButton.addEventListener('click', async () => {
const audioFile = await this.stopRecording();
this.handleAudioRecording(audioFile, true);
this.handleAudioRecording(audioFile, true, (this.keepAudioCheckbox as HTMLInputElement).checked);
});

setIcon(transcribeButton, 'file-text'); // Initially set to bulb
Expand All @@ -103,6 +106,12 @@ export class SmartMemosAudioRecordModal extends Modal {
// 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;

// Start recording immediately upon opening the modal
this.startRecording();
Expand Down
39 changes: 22 additions & 17 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ interface AudioPluginSettings {
prompt: string;
includeTranscript: boolean;
recordingFilePath: string;
keepAudio: boolean;
}

let DEFAULT_SETTINGS: AudioPluginSettings = {
model: 'gpt-4-0613',
apiKey: '',
prompt: 'You are an expert note-making AI for obsidian who specializes in the Linking Your Thinking (LYK) strategy. The following is a transcription of recording of someone talking aloud or people in a conversation. There may be a lot of random things said given fluidity of conversation or thought process and the microphone\'s ability to pick up all audio. Give me detailed notes in markdown language on what was said in the most easy-to-understand, detailed, and conceptual format. Include any helpful information that can conceptualize the notes further or enhance the ideas, and then summarize what was said. Do not mention \"the speaker\" anywhere in your response. The notes your write should be written as if I were writting them. Finally, ensure to end with code for a mermaid chart that shows an enlightening concept map combining both the transcription and the information you added to it. The following is the transcribed audio:\n\n',
includeTranscript: true,
recordingFilePath: ''
recordingFilePath: '',
keepAudio: true
}

const MODELS: string[] = [
Expand All @@ -38,7 +40,6 @@ export default class SmartMemosPlugin extends Plugin {
settings: AudioPluginSettings;
writing: boolean;
transcript: string;

apiKey: string = 'sk-as123mkqwenjasdasdj12...';
model: string = 'gpt-4-0613';

Expand Down Expand Up @@ -68,7 +69,7 @@ export default class SmartMemosPlugin extends Plugin {
name: 'Record smart memo',
editorCallback: async (editor: Editor, view: MarkdownView) => {
// Open the audio recorder and store the recorded audio
this.audioFile = await new SmartMemosAudioRecordModal(this.app, this.handleAudioRecording.bind(this)).open();
this.audioFile = await new SmartMemosAudioRecordModal(this.app, this.handleAudioRecording.bind(this), this.settings).open();

}
});
Expand Down Expand Up @@ -113,7 +114,7 @@ export default class SmartMemosPlugin extends Plugin {
// Update the callback for the audio recorder ribbon
this.addRibbonIcon('microphone', 'Record smart memo', async (evt: MouseEvent) => {
// Open the audio recorder and store the recorded audio
this.audioFile = await new SmartMemosAudioRecordModal(this.app, this.handleAudioRecording.bind(this)).open();
this.audioFile = await new SmartMemosAudioRecordModal(this.app, this.handleAudioRecording.bind(this), this.settings).open();

});

Expand All @@ -122,7 +123,7 @@ export default class SmartMemosPlugin extends Plugin {
}

// Add a new method to handle the audio recording and processing
async handleAudioRecording(audioFile: Blob, transcribe: boolean) {
async handleAudioRecording(audioFile: Blob, transcribe: boolean, keepAudio: boolean) {
try {
console.log('Handling audio recording:', audioFile);

Expand All @@ -137,26 +138,30 @@ export default class SmartMemosPlugin extends Plugin {
const fileName = `recording-${Date.now()}.wav`;
const file = await saveFile(this.app, this.audioFile, fileName, this.settings.recordingFilePath);

// Insert a link to the audio file in the current note
const activeView = this.app.workspace.getActiveViewOfType(MarkdownView);
if (activeView) {
const editor = activeView.editor;
const cursor = editor.getCursor();
const link = `![[${file.path}]]`;
editor.replaceRange(link, cursor);
this.settings.keepAudio = keepAudio;

this.saveSettings();

// Trigger a change in the editor to force Obsidian to re-render the note
editor.replaceRange('', { line: cursor.line, ch: cursor.ch }, { line: cursor.line, ch: cursor.ch });
// Only save the audio file if keepAudio is true
if (keepAudio) {
// Insert a link to the audio file in the current note
const activeView = this.app.workspace.getActiveViewOfType(MarkdownView);
if (activeView) {
const editor = activeView.editor;
const cursor = editor.getCursor();
const link = `![[${file.path}]]`;
editor.replaceRange(link, cursor);

// Trigger a change in the editor to force Obsidian to re-render the note
editor.replaceRange('', { line: cursor.line, ch: cursor.ch }, { line: cursor.line, ch: cursor.ch });
}
}

// Transcribe the audio file if the transcribe parameter is true
if (transcribe) {
this.transcribeRecording(file);
}

// Handle the saved audio file
// You can replace this with your own handling logic
console.log(file);
} catch (error) {
console.error('Error handling audio recording:', error);
new Notice('Failed to handle audio recording');
Expand Down

0 comments on commit e316c94

Please sign in to comment.