mirror of
https://github.com/tuxdotrs/highminded.git
synced 2025-08-23 08:01:03 +05:30
refactor: audio recorder
This commit is contained in:
@@ -3,6 +3,7 @@ using System;
|
|||||||
using System.ClientModel;
|
using System.ClientModel;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Avalonia.Input;
|
using Avalonia.Input;
|
||||||
@@ -10,64 +11,55 @@ using highminded.utils;
|
|||||||
using OpenAI.Chat;
|
using OpenAI.Chat;
|
||||||
using Markdig;
|
using Markdig;
|
||||||
using Markdown.ColorCode;
|
using Markdown.ColorCode;
|
||||||
using System.Net.Http;
|
|
||||||
|
|
||||||
namespace highminded.ui.controls;
|
namespace highminded.ui.controls;
|
||||||
|
|
||||||
public partial class ChatUserControl : UserControl
|
public partial class ChatUserControl : UserControl
|
||||||
{
|
{
|
||||||
private readonly MarkdownPipeline _pipeline = null!;
|
private readonly MarkdownPipeline _pipeline = null!;
|
||||||
private AudioCapture.AudioRecorder? _audioRecorder;
|
private AudioCapture _audioCapture = null!;
|
||||||
private const string AudioFilePath = "output.wav";
|
|
||||||
|
|
||||||
public ChatUserControl()
|
public ChatUserControl()
|
||||||
{
|
{
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
_pipeline = new MarkdownPipelineBuilder().UseAdvancedExtensions().UseColorCode().Build();
|
_pipeline = new MarkdownPipelineBuilder().UseAdvancedExtensions().UseColorCode().Build();
|
||||||
|
_audioCapture = new AudioCapture();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void StartRecord()
|
public void StartRecord()
|
||||||
{
|
{
|
||||||
_audioRecorder = new AudioCapture.AudioRecorder(AudioFilePath);
|
var timestamp = DateTime.Now.ToString("yyyyMMdd_HHmmss");
|
||||||
_audioRecorder.StartRecording();
|
var fileName = $"audio_{timestamp}.wav";
|
||||||
|
var dirPath = Path.Combine(Environment.CurrentDirectory, "audio");
|
||||||
|
var filePath = Path.Combine(dirPath, fileName);
|
||||||
|
Directory.CreateDirectory(dirPath);
|
||||||
|
_audioCapture.StartRecording(filePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void StopRecord()
|
public void StopRecord()
|
||||||
{
|
{
|
||||||
if (_audioRecorder != null)
|
if (_audioCapture != null)
|
||||||
{
|
{
|
||||||
_audioRecorder.StopRecording();
|
_audioCapture.StopRecording();
|
||||||
OnRecordingStopped(null, EventArgs.Empty);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void OnRecordingStopped(object? sender, EventArgs e)
|
|
||||||
{
|
|
||||||
_audioRecorder = null;
|
|
||||||
SendAudio();
|
SendAudio();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public async void SendAudio()
|
public async void SendAudio()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (!File.Exists("output.wav"))
|
var dirPath = Path.Combine(Environment.CurrentDirectory, "audio");
|
||||||
throw new Exception("Audio file not found");
|
var latestAudio = new DirectoryInfo(dirPath).GetFiles().OrderByDescending(f => f.LastWriteTime).First();
|
||||||
|
var filePath = Path.Combine(dirPath, latestAudio.Name);
|
||||||
|
var audioFileBytes = await File.ReadAllBytesAsync(filePath);
|
||||||
var timestamp = DateTime.Now.ToString("yyyyMMdd_HHmmss");
|
var audioBytes = BinaryData.FromBytes(audioFileBytes);
|
||||||
var fileName = "output.wav";
|
|
||||||
var destPath = Path.Combine(Environment.CurrentDirectory, fileName);
|
|
||||||
File.Copy("output.wav", destPath, true);
|
|
||||||
|
|
||||||
await using Stream audioStream = File.OpenRead("output.wav");
|
|
||||||
var audioBytes = await BinaryData.FromStreamAsync(audioStream);
|
|
||||||
|
|
||||||
List<ChatMessage> messages =
|
List<ChatMessage> messages =
|
||||||
[
|
[
|
||||||
new UserChatMessage(
|
new UserChatMessage(
|
||||||
ChatMessageContentPart.CreateTextPart(InMemoryDb.Obj.SettingsManager.Settings.AudioPrompt),
|
ChatMessageContentPart.CreateTextPart(InMemoryDb.Obj.SettingsManager.Settings.AudioPrompt),
|
||||||
ChatMessageContentPart.CreateInputAudioPart(audioBytes, "audio/wav")
|
ChatMessageContentPart.CreateInputAudioPart(audioBytes, ChatInputAudioFormat.Wav)
|
||||||
)
|
)
|
||||||
];
|
];
|
||||||
|
|
||||||
@@ -85,7 +77,9 @@ public partial class ChatUserControl : UserControl
|
|||||||
{
|
{
|
||||||
var timestamp = DateTime.Now.ToString("yyyyMMdd_HHmmss");
|
var timestamp = DateTime.Now.ToString("yyyyMMdd_HHmmss");
|
||||||
var fileName = $"screenshot_{timestamp}.png";
|
var fileName = $"screenshot_{timestamp}.png";
|
||||||
var filePath = Path.Combine(Environment.CurrentDirectory, fileName);
|
var dirPath = Path.Combine(Environment.CurrentDirectory, "images");
|
||||||
|
var filePath = Path.Combine(dirPath, fileName);
|
||||||
|
Directory.CreateDirectory(dirPath);
|
||||||
|
|
||||||
var screenshot = await ScreenCapture.CaptureScreenAsync(filePath);
|
var screenshot = await ScreenCapture.CaptureScreenAsync(filePath);
|
||||||
if (!screenshot) throw new Exception("Failed to capture screenshot");
|
if (!screenshot) throw new Exception("Failed to capture screenshot");
|
||||||
|
@@ -1,8 +1,6 @@
|
|||||||
using Avalonia.Controls;
|
using Avalonia.Controls;
|
||||||
using System;
|
using System;
|
||||||
using System.Net.Quic;
|
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
using Avalonia;
|
|
||||||
using Avalonia.Input;
|
using Avalonia.Input;
|
||||||
using Avalonia.Interactivity;
|
using Avalonia.Interactivity;
|
||||||
using Avalonia.Media;
|
using Avalonia.Media;
|
||||||
@@ -44,6 +42,7 @@ public partial class MainWindow : Window
|
|||||||
// Global Hotkey
|
// Global Hotkey
|
||||||
_hook.KeyPressed += OnKeyPressed;
|
_hook.KeyPressed += OnKeyPressed;
|
||||||
_hook.RunAsync();
|
_hook.RunAsync();
|
||||||
|
_chatUserControl.SendAudio();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnPointerPressed(object? sender, PointerPressedEventArgs e)
|
private void OnPointerPressed(object? sender, PointerPressedEventArgs e)
|
||||||
|
@@ -1,58 +1,51 @@
|
|||||||
using System;
|
using NAudio.CoreAudioApi;
|
||||||
using NAudio.Wave;
|
using NAudio.Wave;
|
||||||
|
|
||||||
namespace highminded.utils;
|
namespace highminded.utils;
|
||||||
|
|
||||||
public static class AudioCapture
|
public class AudioCapture
|
||||||
{
|
|
||||||
|
|
||||||
public class AudioRecorder
|
|
||||||
{
|
{
|
||||||
private WasapiLoopbackCapture capture;
|
private WasapiLoopbackCapture capture;
|
||||||
private WaveFileWriter writer;
|
private WaveFileWriter writer;
|
||||||
private string outputFilePath;
|
private string outputFilePath;
|
||||||
private Action<int> visualCallback;
|
|
||||||
public AudioRecorder(string filePath)
|
public AudioCapture()
|
||||||
{
|
|
||||||
outputFilePath = filePath;
|
|
||||||
}
|
|
||||||
public void StartRecording()
|
|
||||||
{
|
{
|
||||||
capture = new WasapiLoopbackCapture();
|
capture = new WasapiLoopbackCapture();
|
||||||
writer = new WaveFileWriter(outputFilePath, capture.WaveFormat);
|
}
|
||||||
|
|
||||||
|
public void StartRecording(string outPath)
|
||||||
|
{
|
||||||
|
writer = new WaveFileWriter(outPath, capture.WaveFormat);
|
||||||
capture.DataAvailable += Capture_DataAvailable;
|
capture.DataAvailable += Capture_DataAvailable;
|
||||||
capture.RecordingStopped += Capture_RecordingStopped;
|
capture.RecordingStopped += Capture_RecordingStopped;
|
||||||
capture.StartRecording();
|
capture.StartRecording();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void StopRecording()
|
public void StopRecording()
|
||||||
{
|
{
|
||||||
capture.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)
|
private void Capture_DataAvailable(object sender, WaveInEventArgs e)
|
||||||
{
|
{
|
||||||
// Console.WriteLine($"Received {e.BytesRecorded} bytes");
|
writer.Write(e.Buffer, 0, e.BytesRecorded);
|
||||||
writer.Write(e.Buffer, 0, e.BytesRecorded); //dont cram random stuff in this function unless you want delay.
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void Capture_RecordingStopped(object sender, StoppedEventArgs e)
|
private void Capture_RecordingStopped(object sender, StoppedEventArgs e)
|
||||||
{
|
{
|
||||||
writer.Flush();
|
writer.Flush();
|
||||||
writer.Dispose();
|
writer.Dispose();
|
||||||
capture.Dispose();
|
capture.Dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
public static void start_record()
|
|
||||||
{
|
|
||||||
string filePath = "output.wav";
|
|
||||||
AudioRecorder recorder = new AudioRecorder(filePath);
|
|
||||||
recorder.StartRecording();
|
|
||||||
}
|
|
||||||
public static void stop_record()
|
|
||||||
{
|
|
||||||
string filePath = "output.wav";
|
|
||||||
AudioRecorder recorder = new AudioRecorder(filePath);
|
|
||||||
recorder.StopRecording();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user