mirror of
https://github.com/tuxdotrs/highminded.git
synced 2025-08-22 23:51:03 +05:30
refactor: audio recorder
This commit is contained in:
@@ -3,6 +3,7 @@ using System;
|
||||
using System.ClientModel;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Avalonia.Input;
|
||||
@@ -10,64 +11,55 @@ using highminded.utils;
|
||||
using OpenAI.Chat;
|
||||
using Markdig;
|
||||
using Markdown.ColorCode;
|
||||
using System.Net.Http;
|
||||
|
||||
namespace highminded.ui.controls;
|
||||
|
||||
public partial class ChatUserControl : UserControl
|
||||
{
|
||||
private readonly MarkdownPipeline _pipeline = null!;
|
||||
private AudioCapture.AudioRecorder? _audioRecorder;
|
||||
private const string AudioFilePath = "output.wav";
|
||||
private AudioCapture _audioCapture = null!;
|
||||
|
||||
public ChatUserControl()
|
||||
{
|
||||
InitializeComponent();
|
||||
_pipeline = new MarkdownPipelineBuilder().UseAdvancedExtensions().UseColorCode().Build();
|
||||
_audioCapture = new AudioCapture();
|
||||
}
|
||||
|
||||
public void StartRecord()
|
||||
{
|
||||
_audioRecorder = new AudioCapture.AudioRecorder(AudioFilePath);
|
||||
_audioRecorder.StartRecording();
|
||||
var timestamp = DateTime.Now.ToString("yyyyMMdd_HHmmss");
|
||||
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()
|
||||
{
|
||||
if (_audioRecorder != null)
|
||||
if (_audioCapture != null)
|
||||
{
|
||||
_audioRecorder.StopRecording();
|
||||
OnRecordingStopped(null, EventArgs.Empty);
|
||||
_audioCapture.StopRecording();
|
||||
SendAudio();
|
||||
}
|
||||
}
|
||||
|
||||
private void OnRecordingStopped(object? sender, EventArgs e)
|
||||
{
|
||||
_audioRecorder = null;
|
||||
SendAudio();
|
||||
}
|
||||
|
||||
public async void SendAudio()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!File.Exists("output.wav"))
|
||||
throw new Exception("Audio file not found");
|
||||
|
||||
|
||||
var timestamp = DateTime.Now.ToString("yyyyMMdd_HHmmss");
|
||||
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);
|
||||
var dirPath = Path.Combine(Environment.CurrentDirectory, "audio");
|
||||
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 audioBytes = BinaryData.FromBytes(audioFileBytes);
|
||||
|
||||
List<ChatMessage> messages =
|
||||
[
|
||||
new UserChatMessage(
|
||||
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 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);
|
||||
if (!screenshot) throw new Exception("Failed to capture screenshot");
|
||||
|
@@ -1,8 +1,6 @@
|
||||
using Avalonia.Controls;
|
||||
using System;
|
||||
using System.Net.Quic;
|
||||
using System.Runtime.InteropServices;
|
||||
using Avalonia;
|
||||
using Avalonia.Input;
|
||||
using Avalonia.Interactivity;
|
||||
using Avalonia.Media;
|
||||
@@ -44,6 +42,7 @@ public partial class MainWindow : Window
|
||||
// Global Hotkey
|
||||
_hook.KeyPressed += OnKeyPressed;
|
||||
_hook.RunAsync();
|
||||
_chatUserControl.SendAudio();
|
||||
}
|
||||
|
||||
private void OnPointerPressed(object? sender, PointerPressedEventArgs e)
|
||||
|
@@ -1,58 +1,51 @@
|
||||
using System;
|
||||
using NAudio.CoreAudioApi;
|
||||
using NAudio.Wave;
|
||||
|
||||
namespace highminded.utils;
|
||||
|
||||
public static class AudioCapture
|
||||
public class AudioCapture
|
||||
{
|
||||
private WasapiLoopbackCapture capture;
|
||||
private WaveFileWriter writer;
|
||||
private string outputFilePath;
|
||||
|
||||
public class AudioRecorder
|
||||
public AudioCapture()
|
||||
{
|
||||
private WasapiLoopbackCapture capture;
|
||||
private WaveFileWriter writer;
|
||||
private string outputFilePath;
|
||||
private Action<int> visualCallback;
|
||||
public AudioRecorder(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();
|
||||
}
|
||||
|
||||
public void StopRecording()
|
||||
{
|
||||
capture.StopRecording();
|
||||
}
|
||||
|
||||
public bool IsRecording()
|
||||
{
|
||||
if (capture.CaptureState == CaptureState.Capturing || capture.CaptureState == CaptureState.Starting)
|
||||
{
|
||||
outputFilePath = filePath;
|
||||
}
|
||||
public void StartRecording()
|
||||
{
|
||||
capture = new WasapiLoopbackCapture();
|
||||
writer = new WaveFileWriter(outputFilePath, capture.WaveFormat);
|
||||
capture.DataAvailable += Capture_DataAvailable;
|
||||
capture.RecordingStopped += Capture_RecordingStopped;
|
||||
capture.StartRecording();
|
||||
}
|
||||
public void StopRecording()
|
||||
{
|
||||
capture.StopRecording();
|
||||
}
|
||||
private void Capture_DataAvailable(object sender, WaveInEventArgs e)
|
||||
{
|
||||
// Console.WriteLine($"Received {e.BytesRecorded} bytes");
|
||||
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)
|
||||
{
|
||||
writer.Flush();
|
||||
writer.Dispose();
|
||||
capture.Dispose();
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user