mirror of
https://github.com/tuxdotrs/highminded.git
synced 2025-08-23 16:11:02 +05:30
Audio (bugged)
File Already in use error.
This commit is contained in:
@@ -31,6 +31,7 @@
|
|||||||
<PackageReference Include="IconPacks.Avalonia.Lucide" Version="1.0.0" />
|
<PackageReference Include="IconPacks.Avalonia.Lucide" Version="1.0.0" />
|
||||||
<PackageReference Include="Markdig" Version="0.41.3" />
|
<PackageReference Include="Markdig" Version="0.41.3" />
|
||||||
<PackageReference Include="Markdown.ColorCode" Version="3.0.0" />
|
<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="OpenAI" Version="2.2.0-beta.4" />
|
||||||
<PackageReference Include="SharpHook" Version="6.1.2" />
|
<PackageReference Include="SharpHook" Version="6.1.2" />
|
||||||
<PackageReference Include="SharpHook.Reactive" Version="6.1.2" />
|
<PackageReference Include="SharpHook.Reactive" Version="6.1.2" />
|
||||||
|
@@ -10,12 +10,15 @@ 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 const string AudioFilePath = "output.wav";
|
||||||
|
|
||||||
public ChatUserControl()
|
public ChatUserControl()
|
||||||
{
|
{
|
||||||
@@ -23,6 +26,60 @@ public partial class ChatUserControl : UserControl
|
|||||||
_pipeline = new MarkdownPipelineBuilder().UseAdvancedExtensions().UseColorCode().Build();
|
_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()
|
public async void SendScreenshot()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
|
@@ -22,12 +22,17 @@
|
|||||||
<TextBox Name="ApiKeyTextBox" PasswordChar="*" />
|
<TextBox Name="ApiKeyTextBox" PasswordChar="*" />
|
||||||
</StackPanel>
|
</StackPanel>
|
||||||
|
|
||||||
<StackPanel Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2">
|
<StackPanel Grid.Row="2" Grid.Column="0">
|
||||||
<TextBlock Text="Screenshot Prompt" />
|
<TextBlock Text="Screenshot Prompt" />
|
||||||
<TextBox Name="ScreenshotPromptTextBox" TextWrapping="Wrap"/>
|
<TextBox Name="ScreenshotPromptTextBox" TextWrapping="Wrap"/>
|
||||||
</StackPanel>
|
</StackPanel>
|
||||||
|
|
||||||
<StackPanel Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="2">
|
<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"/>
|
<Button Name="SaveSettingsBtn" Content="Save" Click="SaveSettingsBtn_OnClick"/>
|
||||||
</StackPanel>
|
</StackPanel>
|
||||||
|
|
||||||
|
@@ -40,6 +40,14 @@
|
|||||||
<TextBlock FontSize="12" Text="SHIFT + S" />
|
<TextBlock FontSize="12" Text="SHIFT + S" />
|
||||||
</StackPanel>
|
</StackPanel>
|
||||||
</Border>
|
</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">
|
<Button Name="ChatBtn" Background="Transparent" Click="ChatBtnClick">
|
||||||
<iconPacks:PackIconLucide Grid.Column="1" Kind="Brain" Height="16" Width="16" />
|
<iconPacks:PackIconLucide Grid.Column="1" Kind="Brain" Height="16" Width="16" />
|
||||||
</Button>
|
</Button>
|
||||||
|
@@ -82,6 +82,7 @@ public partial class MainWindow : Window
|
|||||||
bool hasShift = (e.RawEvent.Mask & EventMask.Shift) != EventMask.None;
|
bool hasShift = (e.RawEvent.Mask & EventMask.Shift) != EventMask.None;
|
||||||
bool hasH = e.Data.KeyCode == KeyCode.VcH;
|
bool hasH = e.Data.KeyCode == KeyCode.VcH;
|
||||||
bool hasBackslash = e.Data.KeyCode == KeyCode.VcBackslash;
|
bool hasBackslash = e.Data.KeyCode == KeyCode.VcBackslash;
|
||||||
|
bool hasA = e.Data.KeyCode == KeyCode.VcA;
|
||||||
bool hasS = e.Data.KeyCode == KeyCode.VcS;
|
bool hasS = e.Data.KeyCode == KeyCode.VcS;
|
||||||
bool hasQ = e.Data.KeyCode == KeyCode.VcQ;
|
bool hasQ = e.Data.KeyCode == KeyCode.VcQ;
|
||||||
|
|
||||||
@@ -94,6 +95,20 @@ public partial class MainWindow : Window
|
|||||||
{
|
{
|
||||||
Dispatcher.UIThread.Post(() => { _chatUserControl.SendScreenshot(); });
|
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)
|
if (hasAlt && hasShift && hasQ)
|
||||||
{
|
{
|
||||||
|
66
utils/AudioCapture.cs
Normal file
66
utils/AudioCapture.cs
Normal 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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@@ -10,6 +10,7 @@ public class AppSettings
|
|||||||
public string ApiURL { get; set; }
|
public string ApiURL { get; set; }
|
||||||
public string ApiKey { get; set; }
|
public string ApiKey { get; set; }
|
||||||
public string ScreenshotPrompt { get; set; }
|
public string ScreenshotPrompt { get; set; }
|
||||||
|
public string AudioPrompt { get; set; }
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user