Skip to content

Commit

Permalink
feat: add preserve audio file
Browse files Browse the repository at this point in the history
  • Loading branch information
nikdanilov committed Jun 18, 2023
1 parent d21e4b3 commit f8517e4
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 34 deletions.
4 changes: 3 additions & 1 deletion main.ts
Expand Up @@ -59,7 +59,9 @@ export default class Whisper extends Plugin {
const extension = this.recorder
.getMimeType()
?.split("/")[1];
const fileName = `audio-${new Date().toISOString()}.${extension}`;
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);
Expand Down
35 changes: 22 additions & 13 deletions src/AudioHandler.ts
@@ -1,6 +1,7 @@
import axios from "axios";
import Whisper from "main";
import { Notice, MarkdownView } from "obsidian";
import * as path from "path";

export class AudioHandler {
private plugin: Whisper;
Expand All @@ -10,6 +11,21 @@ export class AudioHandler {
}

async sendAudioData(blob: Blob, fileName: string): Promise<void> {
// Get the base file name without extension
const baseFileName = path.parse(fileName).name;

const audioFilePath = `${
this.plugin.settings.saveAudioFilePath
? `${this.plugin.settings.saveAudioFilePath}/`
: ""
}${fileName}`;

const noteFilePath = `${
this.plugin.settings.createNewFileAfterRecordingPath
? `${this.plugin.settings.createNewFileAfterRecordingPath}/`
: ""
}${baseFileName}.md`;

new Notice(`Sending audio data size: ${blob.size / 1000} KB`);

if (!this.plugin.settings.apiKey) {
Expand Down Expand Up @@ -49,32 +65,25 @@ export class AudioHandler {
if (this.plugin.settings.saveAudioFile) {
const arrayBuffer = await blob.arrayBuffer();
await this.plugin.app.vault.adapter.writeBinary(
`path/to/save/${fileName}`,
audioFilePath,
new Uint8Array(arrayBuffer)
);
console.log("write to ", audioFilePath);
}

// Determine if a new file should be created
const activeView =
this.plugin.app.workspace.getActiveViewOfType(MarkdownView);
const shouldCreateNewFile =
this.plugin.settings.newFilePostRecording || !activeView;
this.plugin.settings.createNewFileAfterRecording || !activeView;

if (shouldCreateNewFile) {
// Create a new note with the transcribed text
const folderPath = this.plugin.settings.templateFile
? `${this.plugin.settings.templateFile}/`
: "";
const newNoteName = `${folderPath}Transcription-${new Date()
.toISOString()
.replace(/[:.]/g, "-")}.md`;

await this.plugin.app.vault.create(
newNoteName,
response.data.text
noteFilePath,
`![[${audioFilePath}]]\n${response.data.text}`
);
await this.plugin.app.workspace.openLinkText(
newNoteName,
noteFilePath,
"",
true
);
Expand Down
4 changes: 3 additions & 1 deletion src/Controls.ts
Expand Up @@ -76,7 +76,9 @@ export class Controls extends Modal {
this.resetGUI();

const extension = this.plugin.recorder.getMimeType()?.split("/")[1];
const fileName = `audio-${new Date().toISOString()}.${extension}`;
const fileName = `${new Date()
.toISOString()
.replace(/[:.]/g, "-")}.${extension}`;
await this.plugin.audioHandler.sendAudioData(blob, fileName);
this.plugin.statusBar.updateStatus(RecordingStatus.Idle);
}
Expand Down
8 changes: 4 additions & 4 deletions src/SettingsManager.ts
Expand Up @@ -5,21 +5,21 @@ export interface WhisperSettings {
apiUrl: string;
model: string;
language: string;
templateFile: string;
saveAudioFile: boolean;
saveAudioFilePath: string;
newFilePostRecording: boolean;
createNewFileAfterRecording: boolean;
createNewFileAfterRecordingPath: string;
}

export const DEFAULT_SETTINGS: WhisperSettings = {
apiKey: "",
apiUrl: "https://api.openai.com/v1/audio/transcriptions",
model: "whisper-1",
language: "en",
templateFile: "",
saveAudioFile: true,
saveAudioFilePath: "",
newFilePostRecording: true,
createNewFileAfterRecording: true,
createNewFileAfterRecordingPath: "",
};

export class SettingsManager {
Expand Down
29 changes: 14 additions & 15 deletions src/WhisperSettingsTab.ts
@@ -1,17 +1,11 @@
import Whisper from "main";
import {
App,
PluginSettingTab,
Setting,
TextComponent,
TFolder,
} from "obsidian";
import { App, PluginSettingTab, Setting, TFolder } from "obsidian";
import { SettingsManager } from "./SettingsManager";

export class WhisperSettingsTab extends PluginSettingTab {
private plugin: Whisper;
private settingsManager: SettingsManager;
private templateFileInput: Setting;
private createNewFileInput: Setting;
private saveAudioFileInput: Setting;

constructor(app: App, plugin: Whisper) {
Expand Down Expand Up @@ -173,29 +167,34 @@ export class WhisperSettingsTab extends PluginSettingTab {
)
.addToggle((toggle) => {
toggle
.setValue(this.plugin.settings.newFilePostRecording)
.setValue(this.plugin.settings.createNewFileAfterRecording)
.onChange(async (value) => {
this.plugin.settings.newFilePostRecording = value;
this.plugin.settings.createNewFileAfterRecording =
value;
if (!value) {
this.plugin.settings.templateFile = "";
this.plugin.settings.createNewFileAfterRecordingPath =
"";
}
await this.settingsManager.saveSettings(
this.plugin.settings
);
this.templateFileInput.setDisabled(!value);
this.createNewFileInput.setDisabled(!value);
});
});
}

private createNewFilePathSetting(): void {
this.templateFileInput = new Setting(this.containerEl)
this.createNewFileInput = new Setting(this.containerEl)
.setName("Template file location")
.setDesc("Choose a folder for your template file")
.addText((text) => {
text.setPlaceholder("Example: folder/note")
.setValue(this.plugin.settings.templateFile)
.setValue(
this.plugin.settings.createNewFileAfterRecordingPath
)
.onChange(async (value) => {
this.plugin.settings.templateFile = value;
this.plugin.settings.createNewFileAfterRecordingPath =
value;
await this.settingsManager.saveSettings(
this.plugin.settings
);
Expand Down

0 comments on commit f8517e4

Please sign in to comment.