Audio (bugged)

File Already in  use error.
This commit is contained in:
BonzY
2025-07-04 15:30:49 +05:30
parent e9e660807a
commit a5d179d60d
7 changed files with 173 additions and 20 deletions

View File

@@ -17,23 +17,24 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Avalonia" Version="11.3.2"/>
<PackageReference Include="Avalonia.Desktop" Version="11.3.2"/>
<PackageReference Include="Avalonia.HtmlRenderer" Version="11.2.0"/>
<PackageReference Include="Avalonia.Themes.Fluent" Version="11.3.2"/>
<PackageReference Include="Avalonia.Fonts.Inter" Version="11.3.2"/>
<PackageReference Include="Avalonia" Version="11.3.2" />
<PackageReference Include="Avalonia.Desktop" Version="11.3.2" />
<PackageReference Include="Avalonia.HtmlRenderer" Version="11.2.0" />
<PackageReference Include="Avalonia.Themes.Fluent" Version="11.3.2" />
<PackageReference Include="Avalonia.Fonts.Inter" Version="11.3.2" />
<!--Condition below is needed to remove Avalonia.Diagnostics package from build output in Release configuration.-->
<PackageReference Include="Avalonia.Diagnostics" Version="11.3.2">
<IncludeAssets Condition="'$(Configuration)' != 'Debug'">None</IncludeAssets>
<PrivateAssets Condition="'$(Configuration)' != 'Debug'">All</PrivateAssets>
</PackageReference>
<PackageReference Include="IconPacks.Avalonia" Version="1.0.0"/>
<PackageReference Include="IconPacks.Avalonia.Lucide" Version="1.0.0"/>
<PackageReference Include="Markdig" Version="0.41.3"/>
<PackageReference Include="Markdown.ColorCode" Version="3.0.0"/>
<PackageReference Include="OpenAI" Version="2.2.0-beta.4"/>
<PackageReference Include="SharpHook" Version="6.1.2"/>
<PackageReference Include="SharpHook.Reactive" Version="6.1.2"/>
<PackageReference Include="IconPacks.Avalonia" Version="1.0.0" />
<PackageReference Include="IconPacks.Avalonia.Lucide" Version="1.0.0" />
<PackageReference Include="Markdig" Version="0.41.3" />
<PackageReference Include="Markdown.ColorCode" Version="3.0.0" />
<PackageReference Include="NAudio" Version="2.2.1" />
<PackageReference Include="OpenAI" Version="2.2.0-beta.4" />
<PackageReference Include="SharpHook" Version="6.1.2" />
<PackageReference Include="SharpHook.Reactive" Version="6.1.2" />
</ItemGroup>
<ItemGroup>

View File

@@ -10,18 +10,75 @@ 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";
public ChatUserControl()
{
InitializeComponent();
_pipeline = new MarkdownPipelineBuilder().UseAdvancedExtensions().UseColorCode().Build();
}
public void StartRecord()
{
_audioRecorder = new AudioCapture.AudioRecorder(AudioFilePath);
_audioRecorder.StartRecording();
}
public void StopRecord()
{
if (_audioRecorder != null)
{
_audioRecorder.StopRecording();
OnRecordingStopped(null, EventArgs.Empty); // Manually invoke the method after stopping the recording kyuki already in use bata ra hai
}
}
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);
List<ChatMessage> messages =
[
new UserChatMessage(
ChatMessageContentPart.CreateTextPart(InMemoryDb.Obj.SettingsManager.Settings.AudioPrompt),
ChatMessageContentPart.CreateInputAudioPart(audioBytes, "audio/wav")
)
];
await ProcessChatStreamAsync(messages);
}
catch (Exception err)
{
ResultBlock.Text = err.Message;
}
}
public async void SendScreenshot()
{

View File

@@ -21,13 +21,18 @@
<TextBlock Text="API Key" />
<TextBox Name="ApiKeyTextBox" PasswordChar="*" />
</StackPanel>
<StackPanel Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2">
<TextBlock Text="Screenshot Prompt" />
<TextBox Name="ScreenshotPromptTextBox" TextWrapping="Wrap"/>
</StackPanel>
<StackPanel Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="2">
<StackPanel Grid.Row="2" Grid.Column="0">
<TextBlock Text="Screenshot Prompt" />
<TextBox Name="ScreenshotPromptTextBox" TextWrapping="Wrap"/>
</StackPanel>
<StackPanel Grid.Row="2" Grid.Column="1">
<TextBlock Text="Audio Prompt" />
<TextBox Name="AudioPromptTextbox" TextWrapping="Wrap"/>
</StackPanel>
<StackPanel Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="2">
<Button Name="SaveSettingsBtn" Content="Save" Click="SaveSettingsBtn_OnClick"/>
</StackPanel>

View File

@@ -39,6 +39,14 @@
<TextBlock FontSize="12" Text="Screen:" />
<TextBlock FontSize="12" Text="SHIFT + S" />
</StackPanel>
</Border>
<Border Background="Black" BorderBrush="#19ffffff" BorderThickness="2" CornerRadius="5"
Margin="0 0 10 0">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center"
Spacing="5" Margin="10 0">
<TextBlock FontSize="12" Text="Audio:" />
<TextBlock FontSize="12" Text="Alt + A | | Alt + Shift + A" />
</StackPanel>
</Border>
<Button Name="ChatBtn" Background="Transparent" Click="ChatBtnClick">
<iconPacks:PackIconLucide Grid.Column="1" Kind="Brain" Height="16" Width="16" />

View File

@@ -82,6 +82,7 @@ public partial class MainWindow : Window
bool hasShift = (e.RawEvent.Mask & EventMask.Shift) != EventMask.None;
bool hasH = e.Data.KeyCode == KeyCode.VcH;
bool hasBackslash = e.Data.KeyCode == KeyCode.VcBackslash;
bool hasA = e.Data.KeyCode == KeyCode.VcA;
bool hasS = e.Data.KeyCode == KeyCode.VcS;
bool hasQ = e.Data.KeyCode == KeyCode.VcQ;
@@ -94,6 +95,20 @@ public partial class MainWindow : Window
{
Dispatcher.UIThread.Post(() => { _chatUserControl.SendScreenshot(); });
}
if (hasAlt && hasA)
{
Dispatcher.UIThread.Post(() => { _chatUserControl.StartRecord(); });
}
if (hasAlt && hasShift && hasA)
{
Dispatcher.UIThread.Post(() => { _chatUserControl.StopRecord(); });
}
/* if (hasShift && hasA)
{
Dispatcher.UIThread.Post(() => { _chatUserControl.SendAudio(); });
}*/
if (hasAlt && hasShift && hasQ)
{

66
utils/AudioCapture.cs Normal file
View File

@@ -0,0 +1,66 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NAudio.Wave;
using NAudio.CoreAudioApi;
using System.IO;
using System.Windows.Input;
using static System.Net.Mime.MediaTypeNames;
namespace highminded.utils;
public static class AudioCapture
{
public class AudioRecorder
{
private WasapiLoopbackCapture capture;
private WaveFileWriter writer;
private string outputFilePath;
private Action<int> visualCallback;
public AudioRecorder(string filePath)
{
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();
}
}
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();
}
}

View File

@@ -10,7 +10,8 @@ public class AppSettings
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()