mirror of
https://github.com/tuxdotrs/highminded.git
synced 2025-08-23 08:01:03 +05:30
refactor: cleaup and implemented mvvm
This commit is contained in:
@@ -1,51 +1,27 @@
|
||||
using NAudio.CoreAudioApi;
|
||||
using NAudio.Wave;
|
||||
using System.IO;
|
||||
using SoundFlow.Backends.MiniAudio;
|
||||
using SoundFlow.Components;
|
||||
using SoundFlow.Enums;
|
||||
|
||||
namespace highminded.utils;
|
||||
|
||||
public class AudioCapture
|
||||
{
|
||||
private WasapiLoopbackCapture capture;
|
||||
private WaveFileWriter writer;
|
||||
private string outputFilePath;
|
||||
private readonly MiniAudioEngine _audioEngine = new(48000, Capability.Loopback);
|
||||
private Stream? _fileStream;
|
||||
private Recorder? _recorder;
|
||||
|
||||
public AudioCapture()
|
||||
public void StartRecording(string filePath)
|
||||
{
|
||||
capture = new WasapiLoopbackCapture();
|
||||
}
|
||||
|
||||
public void StartRecording(string outPath)
|
||||
{
|
||||
writer = new WaveFileWriter(outPath, capture.WaveFormat);
|
||||
capture.DataAvailable += Capture_DataAvailable;
|
||||
capture.RecordingStopped += Capture_RecordingStopped;
|
||||
capture.StartRecording();
|
||||
_fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None);
|
||||
_recorder = new Recorder(_fileStream, sampleRate: 48000);
|
||||
_recorder.StartRecording();
|
||||
}
|
||||
|
||||
public void StopRecording()
|
||||
{
|
||||
capture.StopRecording();
|
||||
}
|
||||
|
||||
public bool IsRecording()
|
||||
{
|
||||
if (capture.CaptureState == CaptureState.Capturing || capture.CaptureState == CaptureState.Starting)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private void Capture_DataAvailable(object sender, WaveInEventArgs e)
|
||||
{
|
||||
writer.Write(e.Buffer, 0, e.BytesRecorded);
|
||||
}
|
||||
|
||||
private void Capture_RecordingStopped(object sender, StoppedEventArgs e)
|
||||
{
|
||||
writer.Flush();
|
||||
writer.Dispose();
|
||||
capture.Dispose();
|
||||
_recorder?.StopRecording();
|
||||
_recorder?.Dispose();
|
||||
_fileStream?.Dispose();
|
||||
}
|
||||
}
|
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.ClientModel;
|
||||
using highminded.models;
|
||||
using OpenAI;
|
||||
using OpenAI.Chat;
|
||||
|
||||
@@ -7,37 +8,45 @@ namespace highminded.utils;
|
||||
|
||||
public class InMemoryDb
|
||||
{
|
||||
internal ChatClient ChatClient;
|
||||
internal SettingsManager<AppSettings> SettingsManager;
|
||||
public static readonly InMemoryDb Obj = new InMemoryDb();
|
||||
|
||||
internal readonly SettingsManager<SettingsViewModel> SettingsManager;
|
||||
internal readonly MainViewModel MainViewModel;
|
||||
internal readonly ChatViewModel ChatViewModel;
|
||||
internal readonly SettingsViewModel SettingsViewModel;
|
||||
internal ChatClient? ChatClient;
|
||||
|
||||
// Initialize Singleton Class
|
||||
private InMemoryDb()
|
||||
{
|
||||
SettingsManager = new SettingsManager<AppSettings>();
|
||||
if (SettingsManager.Settings.ApiKey != null)
|
||||
SettingsManager = new SettingsManager<SettingsViewModel>();
|
||||
|
||||
MainViewModel = new MainViewModel();
|
||||
ChatViewModel = new ChatViewModel();
|
||||
SettingsViewModel = new SettingsViewModel();
|
||||
SettingsViewModel = SettingsManager.Settings;
|
||||
|
||||
if (SettingsManager.Settings.ApiKey != string.Empty)
|
||||
{
|
||||
InitOpenAIClient();
|
||||
InitOpenAiClient();
|
||||
}
|
||||
}
|
||||
|
||||
public void InitOpenAIClient()
|
||||
public void SaveSettings()
|
||||
{
|
||||
SettingsManager.Save();
|
||||
InitOpenAiClient();
|
||||
}
|
||||
|
||||
private void InitOpenAiClient()
|
||||
{
|
||||
ChatClient = new ChatClient(
|
||||
model: SettingsManager.Settings.Model,
|
||||
credential: new ApiKeyCredential(SettingsManager.Settings.ApiKey),
|
||||
options: new OpenAIClientOptions
|
||||
{
|
||||
Endpoint = new Uri(SettingsManager.Settings.ApiURL)
|
||||
Endpoint = new Uri(SettingsManager.Settings.ApiUrl)
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
public void SaveSettings(AppSettings settings)
|
||||
{
|
||||
SettingsManager.Settings = settings;
|
||||
SettingsManager.Save();
|
||||
InitOpenAIClient();
|
||||
}
|
||||
|
||||
public static readonly InMemoryDb Obj = new InMemoryDb();
|
||||
}
|
@@ -4,27 +4,17 @@ using System.Text.Json;
|
||||
|
||||
namespace highminded.utils;
|
||||
|
||||
public class AppSettings
|
||||
{
|
||||
public string Model { get; set; }
|
||||
public string ApiURL { get; set; }
|
||||
public string ApiKey { get; set; }
|
||||
public string ScreenshotPrompt { get; set; }
|
||||
public string AudioPrompt { get; set; }
|
||||
|
||||
}
|
||||
|
||||
public class SettingsManager<T> where T : class, new()
|
||||
{
|
||||
private readonly string _settingsPath;
|
||||
public T Settings { get; internal set; }
|
||||
|
||||
public SettingsManager(string appName = "highminded")
|
||||
public SettingsManager()
|
||||
{
|
||||
_settingsPath = Path.Combine(Environment.CurrentDirectory, "settings.json");
|
||||
Settings = Load();
|
||||
}
|
||||
|
||||
_settingsPath = Path.Combine(Environment.CurrentDirectory, "settings.json");
|
||||
Settings = Load();
|
||||
}
|
||||
|
||||
private T Load()
|
||||
{
|
||||
if (!File.Exists(_settingsPath))
|
||||
|
Reference in New Issue
Block a user