mirror of
https://github.com/jahruz67/wisp-open.git
synced 2026-08-08 18:14:08 +00:00
DUMP
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
using System;
|
||||
using Windows.Media.Control;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
class MediaCheck
|
||||
{
|
||||
static async Task<int> Main()
|
||||
{
|
||||
try
|
||||
{
|
||||
var sessionManager = await GlobalSystemMediaTransportControlsSessionManager.RequestAsync();
|
||||
var session = sessionManager.GetCurrentSession();
|
||||
|
||||
if (session == null)
|
||||
{
|
||||
return 0; // No media session
|
||||
}
|
||||
|
||||
var playbackInfo = session.GetPlaybackInfo();
|
||||
var status = playbackInfo.PlaybackStatus;
|
||||
|
||||
// PlaybackStatus.Playing = 4
|
||||
if (status == GlobalSystemMediaTransportControlsSessionPlaybackStatus.Playing)
|
||||
{
|
||||
return 1; // Playing
|
||||
}
|
||||
|
||||
return 0; // Not playing (paused, stopped, etc.)
|
||||
}
|
||||
catch
|
||||
{
|
||||
return 0; // Error = assume not playing
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net6.0-windows10.0.19041.0</TargetFramework>
|
||||
<UseWindowsForms>false</UseWindowsForms>
|
||||
<Nullable>enable</Nullable>
|
||||
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||
</PropertyGroup>
|
||||
</Project>
|
||||
@@ -0,0 +1,33 @@
|
||||
# Check if media is playing using Windows SMTC
|
||||
Add-Type -AssemblyName System.Runtime.WindowsRuntime
|
||||
$asTaskGeneric = ([System.WindowsRuntimeSystemExtensions].GetMethods() | ? { $_.Name -eq 'AsTask' -and $_.GetParameters().Count -eq 1 -and $_.GetParameters()[0].ParameterType.Name -eq 'IAsyncOperation`1' })[0]
|
||||
|
||||
function Await($WinRtTask, $ResultType) {
|
||||
$asTask = $asTaskGeneric.MakeGenericMethod($ResultType)
|
||||
$netTask = $asTask.Invoke($null, @($WinRtTask))
|
||||
$netTask.Wait(-1) | Out-Null
|
||||
$netTask.Result
|
||||
}
|
||||
|
||||
try {
|
||||
[Windows.Media.Control.GlobalSystemMediaTransportControlsSessionManager,Windows.Media,ContentType=WindowsRuntime] | Out-Null
|
||||
$sessionManager = Await ([Windows.Media.Control.GlobalSystemMediaTransportControlsSessionManager]::RequestAsync()) ([Windows.Media.Control.GlobalSystemMediaTransportControlsSessionManager])
|
||||
|
||||
$session = $sessionManager.GetCurrentSession()
|
||||
if ($null -eq $session) {
|
||||
exit 0 # No session = not playing
|
||||
}
|
||||
|
||||
$playbackInfo = $session.GetPlaybackInfo()
|
||||
$status = $playbackInfo.PlaybackStatus
|
||||
|
||||
# 4 = Playing
|
||||
if ($status -eq 4) {
|
||||
exit 1 # Playing
|
||||
}
|
||||
|
||||
exit 0 # Not playing
|
||||
}
|
||||
catch {
|
||||
exit 0 # Error = not playing
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
package media
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"syscall"
|
||||
"wis-free-v3/internal/logger"
|
||||
)
|
||||
|
||||
var (
|
||||
user32 = syscall.NewLazyDLL("user32.dll")
|
||||
keybd_event = user32.NewProc("keybd_event")
|
||||
)
|
||||
|
||||
//go:embed check-media.ps1
|
||||
var checkMediaScript []byte
|
||||
|
||||
const (
|
||||
KEYEVENTF_KEYUP = 0x0002
|
||||
VK_MEDIA_PLAY_PAUSE = 0xB3
|
||||
)
|
||||
|
||||
// sendKey sends a Windows virtual key code
|
||||
func sendKey(key byte) {
|
||||
keybd_event.Call(
|
||||
uintptr(key),
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
)
|
||||
keybd_event.Call(
|
||||
uintptr(key),
|
||||
0,
|
||||
uintptr(KEYEVENTF_KEYUP),
|
||||
0,
|
||||
)
|
||||
}
|
||||
|
||||
// IsPlaying checks if media is currently playing using PowerShell SMTC query
|
||||
func IsPlaying() bool {
|
||||
// Write script to temp file
|
||||
tempDir := os.TempDir()
|
||||
scriptPath := filepath.Join(tempDir, "wis_check_media.ps1")
|
||||
|
||||
// Always write to ensure latest version
|
||||
err := os.WriteFile(scriptPath, checkMediaScript, 0644)
|
||||
if err != nil {
|
||||
logger.Error("Failed to write media check script: %v", err)
|
||||
return false
|
||||
}
|
||||
|
||||
cmd := exec.Command("powershell.exe",
|
||||
"-NoProfile",
|
||||
"-NonInteractive",
|
||||
"-WindowStyle", "Hidden",
|
||||
"-ExecutionPolicy", "Bypass",
|
||||
"-File", scriptPath,
|
||||
)
|
||||
|
||||
// Hide window completely
|
||||
cmd.SysProcAttr = &syscall.SysProcAttr{
|
||||
HideWindow: true,
|
||||
CreationFlags: 0x08000000, // CREATE_NO_WINDOW
|
||||
}
|
||||
|
||||
err = cmd.Run()
|
||||
|
||||
// Exit code 0 = not playing, Exit code 1 = playing
|
||||
if err == nil {
|
||||
return false // Exit code 0
|
||||
}
|
||||
|
||||
if exitErr, ok := err.(*exec.ExitError); ok {
|
||||
return exitErr.ExitCode() == 1
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// TogglePlayPause sends the play/pause toggle key
|
||||
func TogglePlayPause() {
|
||||
sendKey(VK_MEDIA_PLAY_PAUSE)
|
||||
}
|
||||
|
||||
// PauseMedia checks if playing, pauses if so, returns whether we paused
|
||||
func PauseMedia() bool {
|
||||
wasPlaying := IsPlaying()
|
||||
if wasPlaying {
|
||||
TogglePlayPause()
|
||||
}
|
||||
return wasPlaying
|
||||
}
|
||||
|
||||
// ResumeMedia resumes only if we paused it
|
||||
func ResumeMedia(wasPaused bool) {
|
||||
if wasPaused {
|
||||
TogglePlayPause()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,294 @@
|
||||
// Package recorder provides audio capture functionality using the miniaudio library.
|
||||
// It records audio from the system's default capture device to WAV files.
|
||||
package recorder
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"os"
|
||||
"sync"
|
||||
|
||||
"math"
|
||||
"wis-free-v3/internal/logger"
|
||||
|
||||
"github.com/gen2brain/malgo"
|
||||
)
|
||||
|
||||
// Audio recording configuration
|
||||
const (
|
||||
SampleRate = 16000 // Hz - optimal for speech recognition
|
||||
NumChannels = 1 // Mono audio
|
||||
BitsPerSample = 16 // 16-bit PCM
|
||||
BytesPerSample = BitsPerSample / 8
|
||||
)
|
||||
|
||||
// MicrophoneInfo contains information about an available audio capture device.
|
||||
type MicrophoneInfo struct {
|
||||
ID string
|
||||
Name string
|
||||
IsDefault bool
|
||||
}
|
||||
|
||||
// VolumeCallback is called when new volume levels are calculated
|
||||
type VolumeCallback func(level float64)
|
||||
|
||||
// AudioRecorder handles audio capture from the system microphone.
|
||||
type AudioRecorder struct {
|
||||
ctx *malgo.AllocatedContext
|
||||
deviceConfig malgo.DeviceConfig
|
||||
device *malgo.Device
|
||||
outputFile *os.File
|
||||
dataSize uint32
|
||||
isRecording bool
|
||||
deviceID *string
|
||||
OnVolume VolumeCallback
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
// NewRecorder creates a new audio recorder instance.
|
||||
// Returns an error if the audio context cannot be initialized.
|
||||
func NewRecorder() (*AudioRecorder, error) {
|
||||
ctx, err := malgo.InitContext(nil, malgo.ContextConfig{}, nil)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to initialize audio context: %w", err)
|
||||
}
|
||||
|
||||
deviceConfig := malgo.DefaultDeviceConfig(malgo.Capture)
|
||||
deviceConfig.Capture.Format = malgo.FormatS16
|
||||
deviceConfig.Capture.Channels = NumChannels
|
||||
deviceConfig.SampleRate = SampleRate
|
||||
deviceConfig.Alsa.NoMMap = 1
|
||||
|
||||
return &AudioRecorder{
|
||||
ctx: ctx,
|
||||
deviceConfig: deviceConfig,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// GetMicrophones returns a list of available audio capture devices.
|
||||
func (r *AudioRecorder) GetMicrophones() ([]MicrophoneInfo, error) {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
|
||||
if r.ctx == nil {
|
||||
return nil, fmt.Errorf("audio context not initialized")
|
||||
}
|
||||
|
||||
devices, err := r.ctx.Context.Devices(malgo.Capture)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to enumerate devices: %w", err)
|
||||
}
|
||||
|
||||
mics := []MicrophoneInfo{
|
||||
{ID: "", Name: "System Default", IsDefault: true},
|
||||
}
|
||||
|
||||
for _, info := range devices {
|
||||
name := info.Name()
|
||||
if name == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
mics = append(mics, MicrophoneInfo{
|
||||
ID: fmt.Sprintf("%v", info.ID),
|
||||
Name: name,
|
||||
IsDefault: false,
|
||||
})
|
||||
|
||||
logger.Info("Found microphone: %s", name)
|
||||
}
|
||||
|
||||
return mics, nil
|
||||
}
|
||||
|
||||
// SetDevice configures the recorder to use a specific device.
|
||||
// Pass an empty string to use the system default device.
|
||||
func (r *AudioRecorder) SetDevice(deviceID string) {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
|
||||
if deviceID == "" {
|
||||
r.deviceID = nil
|
||||
} else {
|
||||
r.deviceID = &deviceID
|
||||
}
|
||||
}
|
||||
|
||||
// Start begins recording audio to the specified WAV file.
|
||||
// Returns an error if recording is already in progress or initialization fails.
|
||||
func (r *AudioRecorder) Start(filename string) error {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
|
||||
if r.isRecording {
|
||||
return fmt.Errorf("recording already in progress")
|
||||
}
|
||||
|
||||
// Create output file
|
||||
file, err := os.Create(filename)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create output file: %w", err)
|
||||
}
|
||||
r.outputFile = file
|
||||
r.dataSize = 0
|
||||
|
||||
// Write placeholder WAV header
|
||||
if err := r.writeWAVHeader(0); err != nil {
|
||||
file.Close()
|
||||
return fmt.Errorf("failed to write WAV header: %w", err)
|
||||
}
|
||||
|
||||
// Configure audio callback
|
||||
callbacks := malgo.DeviceCallbacks{
|
||||
Data: r.onAudioData,
|
||||
}
|
||||
|
||||
// Use custom device if specified
|
||||
if r.deviceID != nil {
|
||||
// Enumerate devices to find the matching pointer
|
||||
devices, err := r.ctx.Context.Devices(malgo.Capture)
|
||||
if err == nil {
|
||||
for _, info := range devices {
|
||||
if fmt.Sprintf("%v", info.ID) == *r.deviceID {
|
||||
r.deviceConfig.Capture.DeviceID = info.ID.Pointer()
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
r.deviceConfig.Capture.DeviceID = nil
|
||||
}
|
||||
|
||||
// Initialize device
|
||||
device, err := malgo.InitDevice(r.ctx.Context, r.deviceConfig, callbacks)
|
||||
if err != nil {
|
||||
file.Close()
|
||||
return fmt.Errorf("failed to initialize audio device: %w", err)
|
||||
}
|
||||
r.device = device
|
||||
|
||||
// Start capturing
|
||||
if err := device.Start(); err != nil {
|
||||
device.Uninit()
|
||||
file.Close()
|
||||
return fmt.Errorf("failed to start audio capture: %w", err)
|
||||
}
|
||||
|
||||
r.isRecording = true
|
||||
logger.Info("Recording started: %s", filename)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Stop ends the current recording and finalizes the WAV file.
|
||||
func (r *AudioRecorder) Stop() error {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
|
||||
if !r.isRecording {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Stop and cleanup device
|
||||
if r.device != nil {
|
||||
r.device.Uninit()
|
||||
r.device = nil
|
||||
}
|
||||
|
||||
// Finalize WAV file
|
||||
if r.outputFile != nil {
|
||||
// Rewrite header with correct data size
|
||||
r.outputFile.Seek(0, 0)
|
||||
if err := r.writeWAVHeader(r.dataSize); err != nil {
|
||||
logger.Error("Failed to update WAV header: %v", err)
|
||||
}
|
||||
r.outputFile.Close()
|
||||
r.outputFile = nil
|
||||
}
|
||||
|
||||
r.isRecording = false
|
||||
logger.Info("Recording stopped: %d bytes captured", r.dataSize)
|
||||
return nil
|
||||
}
|
||||
|
||||
// IsRecording returns true if recording is currently in progress.
|
||||
func (r *AudioRecorder) IsRecording() bool {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
return r.isRecording
|
||||
}
|
||||
|
||||
// Cleanup releases all audio resources.
|
||||
func (r *AudioRecorder) Cleanup() {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
|
||||
if r.device != nil {
|
||||
r.device.Uninit()
|
||||
r.device = nil
|
||||
}
|
||||
|
||||
if r.ctx != nil {
|
||||
r.ctx.Free()
|
||||
r.ctx = nil
|
||||
}
|
||||
|
||||
logger.Info("Audio recorder cleaned up")
|
||||
}
|
||||
|
||||
// onAudioData is called by miniaudio when audio data is available.
|
||||
func (r *AudioRecorder) onAudioData(_, inputSamples []byte, _ uint32) {
|
||||
if r.outputFile != nil && len(inputSamples) > 0 {
|
||||
n, _ := r.outputFile.Write(inputSamples)
|
||||
r.dataSize += uint32(n)
|
||||
|
||||
// Calculate volume if callback is set
|
||||
if r.OnVolume != nil {
|
||||
// S16LE: 2 bytes per sample
|
||||
samples := len(inputSamples) / 2
|
||||
var maxAmplitude float64
|
||||
for i := 0; i < samples; i++ {
|
||||
// Read as int16
|
||||
val := int16(binary.LittleEndian.Uint16(inputSamples[i*2 : i*2+2]))
|
||||
absVal := math.Abs(float64(val))
|
||||
if absVal > maxAmplitude {
|
||||
maxAmplitude = absVal
|
||||
}
|
||||
}
|
||||
|
||||
// Normalize to 0.0 - 1.0 (max for int16 is 32767)
|
||||
level := maxAmplitude / 32767.0
|
||||
r.OnVolume(level)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// writeWAVHeader writes a standard RIFF WAV header to the output file.
|
||||
func (r *AudioRecorder) writeWAVHeader(dataSize uint32) error {
|
||||
sampleRate := uint32(SampleRate)
|
||||
numChannels := uint16(NumChannels)
|
||||
bitsPerSample := uint16(BitsPerSample)
|
||||
|
||||
byteRate := sampleRate * uint32(numChannels) * uint32(bitsPerSample/8)
|
||||
blockAlign := numChannels * (bitsPerSample / 8)
|
||||
|
||||
// RIFF chunk
|
||||
r.outputFile.Write([]byte("RIFF"))
|
||||
binary.Write(r.outputFile, binary.LittleEndian, uint32(36+dataSize))
|
||||
r.outputFile.Write([]byte("WAVE"))
|
||||
|
||||
// fmt sub-chunk
|
||||
r.outputFile.Write([]byte("fmt "))
|
||||
binary.Write(r.outputFile, binary.LittleEndian, uint32(16)) // Subchunk1Size
|
||||
binary.Write(r.outputFile, binary.LittleEndian, uint16(1)) // AudioFormat (PCM)
|
||||
binary.Write(r.outputFile, binary.LittleEndian, numChannels)
|
||||
binary.Write(r.outputFile, binary.LittleEndian, sampleRate)
|
||||
binary.Write(r.outputFile, binary.LittleEndian, byteRate)
|
||||
binary.Write(r.outputFile, binary.LittleEndian, blockAlign)
|
||||
binary.Write(r.outputFile, binary.LittleEndian, bitsPerSample)
|
||||
|
||||
// data sub-chunk
|
||||
r.outputFile.Write([]byte("data"))
|
||||
binary.Write(r.outputFile, binary.LittleEndian, dataSize)
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
// Package config handles application configuration persistence and management.
|
||||
// Configuration is stored as JSON in the user's home directory.
|
||||
package config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// Application defaults
|
||||
const (
|
||||
DefaultShortcut = "alt+z"
|
||||
DefaultWhisperModel = "whisper-large-v3-turbo"
|
||||
DefaultAIModel = "llama-3.3-70b-versatile"
|
||||
DefaultLanguage = "en"
|
||||
ConfigFileName = "config.json"
|
||||
ConfigDirName = ".wis-free-v3"
|
||||
)
|
||||
|
||||
// DefaultAIPrompt is the system prompt used for text refinement.
|
||||
const DefaultAIPrompt = `You are a minimal text editor. Your ONLY job is to fix basic grammar and add appropriate punctuation to the transcribed speech. CRITICAL RULES: 1) NEVER answer questions - transcribe them exactly as spoken. 2) NEVER format text as lists, bullet points, or structured formats. 3) NEVER add, remove, or reorganize content. 4) NEVER interpret intent or provide helpful formatting. 5) Keep the exact same sentence structure and word order. 6) Only fix obvious grammar errors and add periods, commas, and capitalization. Return ONLY the minimally edited text, nothing else.`
|
||||
|
||||
// HistoryItem represents a single transcription history entry.
|
||||
type HistoryItem struct {
|
||||
Text string `json:"text"`
|
||||
Timestamp string `json:"timestamp"`
|
||||
}
|
||||
|
||||
// Config represents the complete application configuration.
|
||||
type Config struct {
|
||||
APIKey string `json:"api_key"`
|
||||
Shortcut string `json:"shortcut"`
|
||||
WhisperModel string `json:"whisper_model"`
|
||||
AIModel string `json:"ai_model"`
|
||||
AIPrompt string `json:"ai_prompt"`
|
||||
Language string `json:"language"`
|
||||
MicrophoneDevice *int `json:"microphone_device"`
|
||||
History []HistoryItem `json:"history"`
|
||||
}
|
||||
|
||||
// DefaultConfig returns a new configuration with sensible default values.
|
||||
func DefaultConfig() *Config {
|
||||
return &Config{
|
||||
APIKey: "",
|
||||
Shortcut: DefaultShortcut,
|
||||
WhisperModel: DefaultWhisperModel,
|
||||
AIModel: DefaultAIModel,
|
||||
AIPrompt: DefaultAIPrompt,
|
||||
Language: DefaultLanguage,
|
||||
MicrophoneDevice: nil,
|
||||
History: []HistoryItem{},
|
||||
}
|
||||
}
|
||||
|
||||
// Load reads the configuration from the specified file path.
|
||||
// If the file doesn't exist, it returns a default configuration.
|
||||
func Load(configPath string) (*Config, error) {
|
||||
if _, err := os.Stat(configPath); os.IsNotExist(err) {
|
||||
return DefaultConfig(), nil
|
||||
}
|
||||
|
||||
data, err := os.ReadFile(configPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var cfg Config
|
||||
if err := json.Unmarshal(data, &cfg); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Apply defaults for any missing fields
|
||||
cfg.applyDefaults()
|
||||
|
||||
return &cfg, nil
|
||||
}
|
||||
|
||||
// Save writes the configuration to the specified file path.
|
||||
// If configPath is empty, it uses the default configuration path.
|
||||
func Save(c *Config, configPath string) error {
|
||||
if configPath == "" {
|
||||
var err error
|
||||
configPath, err = GetConfigPath()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// Ensure the directory exists
|
||||
dir := filepath.Dir(configPath)
|
||||
if err := os.MkdirAll(dir, 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
data, err := json.MarshalIndent(c, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return os.WriteFile(configPath, data, 0644)
|
||||
}
|
||||
|
||||
// GetConfigPath returns the default configuration file path.
|
||||
func GetConfigPath() (string, error) {
|
||||
homeDir, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
configDir := filepath.Join(homeDir, ConfigDirName)
|
||||
return filepath.Join(configDir, ConfigFileName), nil
|
||||
}
|
||||
|
||||
// applyDefaults sets default values for any empty fields.
|
||||
func (c *Config) applyDefaults() {
|
||||
if c.Shortcut == "" {
|
||||
c.Shortcut = DefaultShortcut
|
||||
}
|
||||
if c.WhisperModel == "" {
|
||||
c.WhisperModel = DefaultWhisperModel
|
||||
}
|
||||
if c.AIModel == "" {
|
||||
c.AIModel = DefaultAIModel
|
||||
}
|
||||
if c.AIPrompt == "" {
|
||||
c.AIPrompt = DefaultAIPrompt
|
||||
}
|
||||
if c.Language == "" {
|
||||
c.Language = DefaultLanguage
|
||||
}
|
||||
if c.History == nil {
|
||||
c.History = []HistoryItem{}
|
||||
}
|
||||
}
|
||||
|
||||
// AddHistoryItem adds a new transcription to the history.
|
||||
func (c *Config) AddHistoryItem(text, timestamp string) {
|
||||
c.History = append(c.History, HistoryItem{
|
||||
Text: text,
|
||||
Timestamp: timestamp,
|
||||
})
|
||||
}
|
||||
|
||||
// ClearHistory removes all history items.
|
||||
func (c *Config) ClearHistory() {
|
||||
c.History = []HistoryItem{}
|
||||
}
|
||||
@@ -0,0 +1,160 @@
|
||||
// Package hotkey provides global keyboard shortcut detection and handling.
|
||||
// It uses the gohook library to capture system-wide key events.
|
||||
package hotkey
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"wis-free-v3/internal/logger"
|
||||
|
||||
hook "github.com/robotn/gohook"
|
||||
)
|
||||
|
||||
// Listener handles global hotkey events and triggers callbacks when the
|
||||
// configured shortcut is pressed and released.
|
||||
type Listener struct {
|
||||
startCallback func()
|
||||
stopCallback func()
|
||||
isListening bool
|
||||
stopChan chan struct{}
|
||||
triggerKeys []uint16
|
||||
modifiers [][]uint16
|
||||
mu sync.RWMutex
|
||||
}
|
||||
|
||||
// NewListener creates a new hotkey listener with the specified shortcut and callbacks.
|
||||
// The shortcut should be in format like "ctrl+k" or "alt+shift+space".
|
||||
// onStart is called when the shortcut is pressed, onStop when it's released.
|
||||
func NewListener(shortcut string, onStart, onStop func()) *Listener {
|
||||
trigger, mods := ParseShortcut(shortcut)
|
||||
|
||||
logger.Info("Hotkey listener created: shortcut=%s, trigger=%d, modifiers=%v",
|
||||
shortcut, trigger, mods)
|
||||
|
||||
return &Listener{
|
||||
startCallback: onStart,
|
||||
stopCallback: onStop,
|
||||
stopChan: make(chan struct{}),
|
||||
triggerKeys: trigger,
|
||||
modifiers: mods,
|
||||
}
|
||||
}
|
||||
|
||||
// UpdateShortcut changes the shortcut without stopping the listener.
|
||||
// This allows hot-swapping the shortcut while the application is running.
|
||||
func (l *Listener) UpdateShortcut(shortcut string) {
|
||||
trigger, mods := ParseShortcut(shortcut)
|
||||
|
||||
l.mu.Lock()
|
||||
l.triggerKeys = trigger
|
||||
l.modifiers = mods
|
||||
l.mu.Unlock()
|
||||
|
||||
logger.Info("Hotkey updated: shortcut=%s, trigger=%d", shortcut, trigger)
|
||||
}
|
||||
|
||||
// Start begins listening for the configured shortcut in a background goroutine.
|
||||
// It's safe to call Start multiple times; subsequent calls are ignored.
|
||||
func (l *Listener) Start() {
|
||||
if l.isListening {
|
||||
return
|
||||
}
|
||||
l.isListening = true
|
||||
|
||||
go l.eventLoop()
|
||||
}
|
||||
|
||||
// Stop terminates the hotkey listener.
|
||||
// It's safe to call Stop multiple times.
|
||||
func (l *Listener) Stop() {
|
||||
if !l.isListening {
|
||||
return
|
||||
}
|
||||
l.isListening = false
|
||||
close(l.stopChan)
|
||||
logger.Info("Hotkey listener stopped")
|
||||
}
|
||||
|
||||
// eventLoop runs the main keyboard event processing loop.
|
||||
func (l *Listener) eventLoop() {
|
||||
logger.Info("Hotkey listener started")
|
||||
|
||||
evChan := hook.Start()
|
||||
defer hook.End()
|
||||
|
||||
var isRecording bool
|
||||
pressedKeys := make(map[uint16]bool)
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-l.stopChan:
|
||||
return
|
||||
|
||||
case ev := <-evChan:
|
||||
l.handleKeyEvent(ev, pressedKeys, &isRecording)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// handleKeyEvent processes a single keyboard event.
|
||||
func (l *Listener) handleKeyEvent(ev hook.Event, pressedKeys map[uint16]bool, isRecording *bool) {
|
||||
// Update pressed keys state
|
||||
switch ev.Kind {
|
||||
case hook.KeyDown, hook.KeyHold:
|
||||
pressedKeys[ev.Rawcode] = true
|
||||
case hook.KeyUp:
|
||||
delete(pressedKeys, ev.Rawcode)
|
||||
default:
|
||||
return
|
||||
}
|
||||
|
||||
// Read current configuration
|
||||
l.mu.RLock()
|
||||
triggerVariants := l.triggerKeys
|
||||
modGroups := l.modifiers
|
||||
l.mu.RUnlock()
|
||||
|
||||
if len(triggerVariants) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
// 1. Check trigger key first (fast path)
|
||||
triggerPressed := false
|
||||
for _, t := range triggerVariants {
|
||||
if pressedKeys[t] {
|
||||
triggerPressed = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// 2. State transition handling
|
||||
active := false
|
||||
if triggerPressed {
|
||||
active = true
|
||||
for _, group := range modGroups {
|
||||
groupPressed := false
|
||||
for _, m := range group {
|
||||
if pressedKeys[m] {
|
||||
groupPressed = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !groupPressed {
|
||||
active = false
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if active && !*isRecording {
|
||||
logger.Info("Shortcut activated: starting recording")
|
||||
l.startCallback()
|
||||
*isRecording = true
|
||||
} else if !active && *isRecording {
|
||||
logger.Info("Shortcut released: stopping recording")
|
||||
l.stopCallback()
|
||||
*isRecording = false
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,112 @@
|
||||
// Package hotkey provides global keyboard shortcut detection and handling.
|
||||
package hotkey
|
||||
|
||||
import "strings"
|
||||
|
||||
// Windows Virtual Key Codes
|
||||
// See: https://docs.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes
|
||||
const (
|
||||
VK_LCTRL = 162 // Left Control
|
||||
VK_RCTRL = 163 // Right Control
|
||||
VK_LSHIFT = 160 // Left Shift
|
||||
VK_RSHIFT = 161 // Right Shift
|
||||
VK_LALT = 164 // Left Alt (Menu)
|
||||
VK_RALT = 165 // Right Alt (Menu)
|
||||
VK_LWIN = 91 // Left Windows key
|
||||
VK_RWIN = 92 // Right Windows key
|
||||
)
|
||||
|
||||
// keyMap maps human-readable key names to Windows Virtual Key Codes.
|
||||
// Each key may have multiple valid codes (e.g., left and right variants).
|
||||
var keyMap = map[string][]uint16{
|
||||
// Modifier keys
|
||||
"ctrl": {VK_LCTRL, VK_RCTRL, 17},
|
||||
"control": {VK_LCTRL, VK_RCTRL, 17},
|
||||
"shift": {VK_LSHIFT, VK_RSHIFT, 16},
|
||||
"alt": {VK_LALT, VK_RALT, 18},
|
||||
"win": {VK_LWIN, VK_RWIN, 91, 92},
|
||||
"windows": {VK_LWIN, VK_RWIN, 91, 92},
|
||||
"meta": {VK_LWIN, VK_RWIN, 91, 92},
|
||||
"super": {VK_LWIN, VK_RWIN, 91, 92},
|
||||
|
||||
// Special keys
|
||||
"backspace": {8},
|
||||
"tab": {9},
|
||||
"enter": {13},
|
||||
"escape": {27},
|
||||
"esc": {27},
|
||||
"space": {32},
|
||||
"left": {37},
|
||||
"up": {38},
|
||||
"right": {39},
|
||||
"down": {40},
|
||||
"delete": {46},
|
||||
"del": {46},
|
||||
|
||||
// Number keys (top row)
|
||||
"0": {48}, "1": {49}, "2": {50}, "3": {51}, "4": {52},
|
||||
"5": {53}, "6": {54}, "7": {55}, "8": {56}, "9": {57},
|
||||
|
||||
// Letter keys
|
||||
"a": {65}, "b": {66}, "c": {67}, "d": {68}, "e": {69},
|
||||
"f": {70}, "g": {71}, "h": {72}, "i": {73}, "j": {74},
|
||||
"k": {75}, "l": {76}, "m": {77}, "n": {78}, "o": {79},
|
||||
"p": {80}, "q": {81}, "r": {82}, "s": {83}, "t": {84},
|
||||
"u": {85}, "v": {86}, "w": {87}, "x": {88}, "y": {89},
|
||||
"z": {90},
|
||||
|
||||
// Function keys
|
||||
"f1": {112}, "f2": {113}, "f3": {114}, "f4": {115},
|
||||
"f5": {116}, "f6": {117}, "f7": {118}, "f8": {119},
|
||||
"f9": {120}, "f10": {121}, "f11": {122}, "f12": {123},
|
||||
}
|
||||
|
||||
// modifierKeys identifies which key names are modifiers.
|
||||
var modifierKeys = map[string]bool{
|
||||
"ctrl": true, "control": true,
|
||||
"shift": true,
|
||||
"alt": true,
|
||||
"win": true, "windows": true, "meta": true, "super": true,
|
||||
}
|
||||
|
||||
// ParseShortcut parses a shortcut string into a trigger key and modifier keys.
|
||||
// The shortcut format is "modifier+modifier+key" (e.g., "ctrl+shift+k").
|
||||
//
|
||||
// Returns:
|
||||
// - trigger: the primary key code that activates the shortcut
|
||||
// - modifiers: slice of modifier key code variants that must be held
|
||||
func ParseShortcut(shortcut string) (trigger []uint16, modifiers [][]uint16) {
|
||||
parts := strings.Split(strings.ToLower(strings.TrimSpace(shortcut)), "+")
|
||||
if len(parts) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// Last part is always the trigger key
|
||||
triggerName := strings.TrimSpace(parts[len(parts)-1])
|
||||
if codes, ok := keyMap[triggerName]; ok && len(codes) > 0 {
|
||||
trigger = codes
|
||||
}
|
||||
|
||||
// Preceding parts are modifiers
|
||||
for i := 0; i < len(parts)-1; i++ {
|
||||
modName := strings.TrimSpace(parts[i])
|
||||
if codes, ok := keyMap[modName]; ok {
|
||||
modifiers = append(modifiers, codes)
|
||||
}
|
||||
}
|
||||
|
||||
return trigger, modifiers
|
||||
}
|
||||
|
||||
// IsModifier returns true if the given key name is a modifier key.
|
||||
func IsModifier(keyName string) bool {
|
||||
return modifierKeys[strings.ToLower(keyName)]
|
||||
}
|
||||
|
||||
// GetKeyCode returns the virtual key code(s) for a given key name.
|
||||
// Returns nil if the key name is not recognized.
|
||||
func GetKeyCode(keyName string) []uint16 {
|
||||
return keyMap[strings.ToLower(keyName)]
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,140 @@
|
||||
// Package logger provides a simple file-based logging system for the application.
|
||||
// Logs are written to date-stamped files in the user's config directory.
|
||||
package logger
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Log level constants
|
||||
const (
|
||||
logFileMode = 0644
|
||||
logDirMode = 0755
|
||||
)
|
||||
|
||||
var (
|
||||
logFile *os.File
|
||||
logMutex sync.Mutex
|
||||
isInitialized bool
|
||||
)
|
||||
|
||||
// Init initializes the logger with a new log file for today's date.
|
||||
// It creates the log directory if it doesn't exist.
|
||||
func Init() error {
|
||||
logMutex.Lock()
|
||||
defer logMutex.Unlock()
|
||||
|
||||
if isInitialized {
|
||||
return nil
|
||||
}
|
||||
|
||||
logPath, err := getLogPath()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get log path: %w", err)
|
||||
}
|
||||
|
||||
// Ensure log directory exists
|
||||
logDir := filepath.Dir(logPath)
|
||||
if err := os.MkdirAll(logDir, logDirMode); err != nil {
|
||||
return fmt.Errorf("failed to create log directory: %w", err)
|
||||
}
|
||||
|
||||
// Open or create log file
|
||||
logFile, err = os.OpenFile(logPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, logFileMode)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to open log file: %w", err)
|
||||
}
|
||||
|
||||
isInitialized = true
|
||||
Info("Logger initialized")
|
||||
return nil
|
||||
}
|
||||
|
||||
// Close closes the log file handle.
|
||||
func Close() {
|
||||
logMutex.Lock()
|
||||
defer logMutex.Unlock()
|
||||
|
||||
if logFile != nil {
|
||||
logFile.Close()
|
||||
logFile = nil
|
||||
}
|
||||
isInitialized = false
|
||||
}
|
||||
|
||||
// Info logs an informational message with timestamp.
|
||||
// Modified to do nothing so only errors are logged.
|
||||
func Info(format string, args ...interface{}) {
|
||||
// Do nothing
|
||||
}
|
||||
|
||||
// Error logs an error message with timestamp.
|
||||
func Error(format string, args ...interface{}) {
|
||||
log("ERROR", format, args...)
|
||||
}
|
||||
|
||||
// log writes a formatted log message to the log file.
|
||||
func log(level, format string, args ...interface{}) {
|
||||
logMutex.Lock()
|
||||
defer logMutex.Unlock()
|
||||
|
||||
// Auto-initialize if needed
|
||||
if !isInitialized {
|
||||
if err := initWithoutLock(); err != nil {
|
||||
// Fall back to stderr if logging fails
|
||||
fmt.Fprintf(os.Stderr, "[%s] %s: %s\n", level, time.Now().Format(time.RFC3339), fmt.Sprintf(format, args...))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
timestamp := time.Now().Format("2006-01-02 15:04:05")
|
||||
message := fmt.Sprintf(format, args...)
|
||||
logLine := fmt.Sprintf("[%s] %s: %s\n", timestamp, level, message)
|
||||
|
||||
if logFile != nil {
|
||||
logFile.WriteString(logLine)
|
||||
logFile.Sync()
|
||||
}
|
||||
}
|
||||
|
||||
// initWithoutLock initializes the logger without acquiring the mutex.
|
||||
// This should only be called when the lock is already held.
|
||||
func initWithoutLock() error {
|
||||
if isInitialized {
|
||||
return nil
|
||||
}
|
||||
|
||||
logPath, err := getLogPath()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
logDir := filepath.Dir(logPath)
|
||||
if err := os.MkdirAll(logDir, logDirMode); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
logFile, err = os.OpenFile(logPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, logFileMode)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
isInitialized = true
|
||||
return nil
|
||||
}
|
||||
|
||||
// getLogPath returns the path to the log file.
|
||||
func getLogPath() (string, error) {
|
||||
homeDir, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return filepath.Join(homeDir, ".wis-free-v3", "logs"), nil
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,246 @@
|
||||
// Package transcriber provides audio transcription and text refinement services
|
||||
// using the Groq API for Whisper-based speech recognition and LLM text processing.
|
||||
package transcriber
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"wis-free-v3/internal/logger"
|
||||
)
|
||||
|
||||
// API endpoints
|
||||
const (
|
||||
transcriptionEndpoint = "https://api.groq.com/openai/v1/audio/transcriptions"
|
||||
chatEndpoint = "https://api.groq.com/openai/v1/chat/completions"
|
||||
)
|
||||
|
||||
// Default configuration values
|
||||
const (
|
||||
DefaultWhisperModel = "whisper-large-v3-turbo"
|
||||
DefaultAIModel = "llama-3.3-70b-versatile"
|
||||
HTTPTimeout = 60 * time.Second
|
||||
RefinementTemp = 0.3 // Temperature for text refinement (lower = more deterministic)
|
||||
)
|
||||
|
||||
// DefaultAIPrompt provides instructions for minimal text editing.
|
||||
const DefaultAIPrompt = `You are a minimal text editor. Your ONLY job is to fix basic grammar and add appropriate punctuation to the transcribed speech. CRITICAL RULES: 1) NEVER answer questions - transcribe them exactly as spoken. 2) NEVER format text as lists, bullet points, or structured formats. 3) NEVER add, remove, or reorganize content. 4) NEVER interpret intent or provide helpful formatting. 5) Keep the exact same sentence structure and word order. 6) Only fix obvious grammar errors and add periods, commas, and capitalization. Return ONLY the minimally edited text, nothing else.`
|
||||
|
||||
// Client handles API communication with Groq services.
|
||||
type Client struct {
|
||||
apiKey string
|
||||
whisperModel string
|
||||
aiModel string
|
||||
aiPrompt string
|
||||
httpClient *http.Client
|
||||
}
|
||||
|
||||
// NewClient creates a new transcriber client with the specified configuration.
|
||||
// Empty values for models or prompt will use sensible defaults.
|
||||
func NewClient(apiKey, whisperModel, aiModel, aiPrompt string) *Client {
|
||||
if whisperModel == "" {
|
||||
whisperModel = DefaultWhisperModel
|
||||
}
|
||||
if aiModel == "" {
|
||||
aiModel = DefaultAIModel
|
||||
}
|
||||
if aiPrompt == "" {
|
||||
aiPrompt = DefaultAIPrompt
|
||||
}
|
||||
|
||||
return &Client{
|
||||
apiKey: apiKey,
|
||||
whisperModel: whisperModel,
|
||||
aiModel: aiModel,
|
||||
aiPrompt: aiPrompt,
|
||||
httpClient: &http.Client{
|
||||
Timeout: HTTPTimeout,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// TranscribeAudio converts an audio file to text using Whisper.
|
||||
// Returns the transcribed text or an error if the operation fails.
|
||||
func (c *Client) TranscribeAudio(audioFilePath, language string) (string, error) {
|
||||
if c.apiKey == "" {
|
||||
return "", fmt.Errorf("API key is missing - please configure it in Settings")
|
||||
}
|
||||
|
||||
// Validate file exists and get size for logging
|
||||
fileInfo, err := os.Stat(audioFilePath)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("audio file not found: %w", err)
|
||||
}
|
||||
logger.Info("Transcribing audio: %s (%.2f KB) in %s", audioFilePath, float64(fileInfo.Size())/1024, language)
|
||||
|
||||
// Prepare the multipart request
|
||||
body, contentType, err := c.prepareAudioRequest(audioFilePath, language)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// Create and send request
|
||||
req, err := http.NewRequest(http.MethodPost, transcriptionEndpoint, body)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to create request: %w", err)
|
||||
}
|
||||
req.Header.Set("Authorization", "Bearer "+c.apiKey)
|
||||
req.Header.Set("Content-Type", contentType)
|
||||
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("API request failed: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
// Handle response
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return "", c.handleAPIError(resp, "transcription")
|
||||
}
|
||||
|
||||
var result struct {
|
||||
Text string `json:"text"`
|
||||
}
|
||||
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
|
||||
return "", fmt.Errorf("failed to parse response: %w", err)
|
||||
}
|
||||
|
||||
logger.Info("Transcription complete: %d characters", len(result.Text))
|
||||
return result.Text, nil
|
||||
}
|
||||
|
||||
// RefineText uses an LLM to clean up and correct transcribed text.
|
||||
// If the AI model is set to "None" or the API key is missing, returns the original text.
|
||||
func (c *Client) RefineText(text string) (string, error) {
|
||||
if c.apiKey == "" || c.aiModel == "None" {
|
||||
return text, nil
|
||||
}
|
||||
|
||||
payload := map[string]interface{}{
|
||||
"model": c.aiModel,
|
||||
"messages": []map[string]string{
|
||||
{"role": "system", "content": c.aiPrompt},
|
||||
{"role": "user", "content": text},
|
||||
},
|
||||
"temperature": RefinementTemp,
|
||||
}
|
||||
|
||||
// Add special parameters for OpenAI reasoning models
|
||||
if strings.Contains(c.aiModel, "gpt-oss") || strings.Contains(c.aiModel, "openai/") {
|
||||
payload["max_completion_tokens"] = 8192
|
||||
payload["top_p"] = 1
|
||||
|
||||
// Set reasoning effort based on model name
|
||||
effort := "low"
|
||||
actualModel := c.aiModel
|
||||
if strings.HasSuffix(c.aiModel, "-high") {
|
||||
effort = "high"
|
||||
actualModel = strings.TrimSuffix(c.aiModel, "-high")
|
||||
}
|
||||
payload["reasoning_effort"] = effort
|
||||
payload["model"] = actualModel
|
||||
}
|
||||
|
||||
payloadBytes, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
logger.Error("Failed to marshal refinement request: %v", err)
|
||||
return text, nil // Return original text on error
|
||||
}
|
||||
|
||||
req, err := http.NewRequest(http.MethodPost, chatEndpoint, bytes.NewBuffer(payloadBytes))
|
||||
if err != nil {
|
||||
logger.Error("Failed to create refinement request: %v", err)
|
||||
return text, nil
|
||||
}
|
||||
req.Header.Set("Authorization", "Bearer "+c.apiKey)
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
logger.Error("Refinement request failed: %v", err)
|
||||
return text, nil
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
c.handleAPIError(resp, "refinement")
|
||||
return text, nil
|
||||
}
|
||||
|
||||
var result struct {
|
||||
Choices []struct {
|
||||
Message struct {
|
||||
Content string `json:"content"`
|
||||
} `json:"message"`
|
||||
} `json:"choices"`
|
||||
}
|
||||
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
|
||||
logger.Error("Failed to parse refinement response: %v", err)
|
||||
return text, nil
|
||||
}
|
||||
|
||||
if len(result.Choices) > 0 && result.Choices[0].Message.Content != "" {
|
||||
logger.Info("Text refinement complete")
|
||||
return result.Choices[0].Message.Content, nil
|
||||
}
|
||||
|
||||
return text, nil
|
||||
}
|
||||
|
||||
// prepareAudioRequest creates a multipart form request body for audio transcription.
|
||||
func (c *Client) prepareAudioRequest(audioFilePath, language string) (*bytes.Buffer, string, error) {
|
||||
file, err := os.Open(audioFilePath)
|
||||
if err != nil {
|
||||
return nil, "", fmt.Errorf("failed to open audio file: %w", err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
body := &bytes.Buffer{}
|
||||
writer := multipart.NewWriter(body)
|
||||
|
||||
// Add audio file
|
||||
part, err := writer.CreateFormFile("file", "audio.wav")
|
||||
if err != nil {
|
||||
return nil, "", fmt.Errorf("failed to create form file: %w", err)
|
||||
}
|
||||
if _, err := io.Copy(part, file); err != nil {
|
||||
return nil, "", fmt.Errorf("failed to copy audio data: %w", err)
|
||||
}
|
||||
|
||||
// Add model parameter
|
||||
if err := writer.WriteField("model", c.whisperModel); err != nil {
|
||||
return nil, "", fmt.Errorf("failed to write model field: %w", err)
|
||||
}
|
||||
|
||||
// Add language parameter
|
||||
if language != "" {
|
||||
if err := writer.WriteField("language", language); err != nil {
|
||||
return nil, "", fmt.Errorf("failed to write language field: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Add temperature=0 for more consistent results
|
||||
if err := writer.WriteField("temperature", "0"); err != nil {
|
||||
return nil, "", fmt.Errorf("failed to write temperature field: %w", err)
|
||||
}
|
||||
|
||||
if err := writer.Close(); err != nil {
|
||||
return nil, "", fmt.Errorf("failed to finalize request: %w", err)
|
||||
}
|
||||
|
||||
return body, writer.FormDataContentType(), nil
|
||||
}
|
||||
|
||||
// handleAPIError logs and formats API error responses.
|
||||
func (c *Client) handleAPIError(resp *http.Response, operation string) error {
|
||||
bodyBytes, _ := io.ReadAll(resp.Body)
|
||||
logger.Error("API %s error: status=%d body=%s", operation, resp.StatusCode, string(bodyBytes))
|
||||
return fmt.Errorf("%s failed with status %d", operation, resp.StatusCode)
|
||||
}
|
||||
@@ -0,0 +1,452 @@
|
||||
// Package whisper provides local offline transcription using whisper.cpp
|
||||
package whisper
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"wis-free-v3/internal/logger"
|
||||
)
|
||||
|
||||
// Download URLs
|
||||
const (
|
||||
// Whisper.cpp Windows binary from ggml-org GitHub releases
|
||||
// Using Vulkan build to support Intel iGPU, AMD, and Nvidia
|
||||
whisperBinaryURL = "https://github.com/jerryshell/whisper.cpp-windows-vulkan-bin/releases/download/v1.0.0/whisper.cpp-windows-vulkan.zip"
|
||||
// Model URLs from Hugging Face
|
||||
modelBaseURL = "https://huggingface.co/ggerganov/whisper.cpp/resolve/main"
|
||||
)
|
||||
|
||||
// Available models
|
||||
var Models = map[string]struct {
|
||||
Filename string
|
||||
Size string
|
||||
}{
|
||||
"tiny": {"ggml-tiny.bin", "75 MB"},
|
||||
"base": {"ggml-base.bin", "150 MB"},
|
||||
"small": {"ggml-small.bin", "500 MB"},
|
||||
"medium": {"ggml-medium.bin", "1.5 GB"},
|
||||
}
|
||||
|
||||
// InstalledInfo stores information about the installed whisper
|
||||
type InstalledInfo struct {
|
||||
Version string `json:"version"`
|
||||
Model string `json:"model"`
|
||||
InstallPath string `json:"install_path"`
|
||||
}
|
||||
|
||||
// Manager handles whisper installation and execution
|
||||
type Manager struct {
|
||||
installDir string
|
||||
}
|
||||
|
||||
// NewManager creates a new whisper manager
|
||||
func NewManager() (*Manager, error) {
|
||||
homeDir, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get home directory: %w", err)
|
||||
}
|
||||
|
||||
installDir := filepath.Join(homeDir, ".wis-free-v3", "whisper")
|
||||
|
||||
return &Manager{
|
||||
installDir: installDir,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// IsInstalled checks if whisper is installed
|
||||
func (m *Manager) IsInstalled() bool {
|
||||
infoPath := filepath.Join(m.installDir, "installed.json")
|
||||
if _, err := os.Stat(infoPath); os.IsNotExist(err) {
|
||||
return false
|
||||
}
|
||||
|
||||
// Check if model exists
|
||||
info, err := m.GetInstalledInfo()
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
modelPath := filepath.Join(m.installDir, Models[info.Model].Filename)
|
||||
if _, err := os.Stat(modelPath); os.IsNotExist(err) {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// GetInstalledInfo returns information about the installed whisper
|
||||
func (m *Manager) GetInstalledInfo() (*InstalledInfo, error) {
|
||||
infoPath := filepath.Join(m.installDir, "installed.json")
|
||||
data, err := os.ReadFile(infoPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var info InstalledInfo
|
||||
if err := json.Unmarshal(data, &info); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &info, nil
|
||||
}
|
||||
|
||||
// Install downloads and installs whisper with the specified model
|
||||
// It opens a terminal window to show progress
|
||||
func (m *Manager) Install(model string) error {
|
||||
if _, ok := Models[model]; !ok {
|
||||
return fmt.Errorf("unknown model: %s", model)
|
||||
}
|
||||
|
||||
// Create install directory
|
||||
if err := os.MkdirAll(m.installDir, 0755); err != nil {
|
||||
return fmt.Errorf("failed to create install directory: %w", err)
|
||||
}
|
||||
|
||||
// Create install script
|
||||
scriptPath := filepath.Join(m.installDir, "install.bat")
|
||||
script := m.generateInstallScript(model)
|
||||
if err := os.WriteFile(scriptPath, []byte(script), 0755); err != nil {
|
||||
return fmt.Errorf("failed to write install script: %w", err)
|
||||
}
|
||||
|
||||
// Run install script in a visible terminal
|
||||
cmd := exec.Command("cmd", "/c", "start", "cmd", "/k", scriptPath)
|
||||
if err := cmd.Start(); err != nil {
|
||||
return fmt.Errorf("failed to start installer: %w", err)
|
||||
}
|
||||
|
||||
logger.Info("Whisper installation started in terminal")
|
||||
return nil
|
||||
}
|
||||
|
||||
// generateInstallScript creates a batch script for installation
|
||||
func (m *Manager) generateInstallScript(model string) string {
|
||||
modelInfo := Models[model]
|
||||
modelURL := fmt.Sprintf("%s/%s", modelBaseURL, modelInfo.Filename)
|
||||
modelPath := filepath.Join(m.installDir, modelInfo.Filename)
|
||||
infoPath := filepath.Join(m.installDir, "installed.json")
|
||||
zipPath := filepath.Join(m.installDir, "whisper.zip")
|
||||
|
||||
script := fmt.Sprintf(`@echo off
|
||||
title wis-free-v3 - Installing Offline Whisper
|
||||
color 0A
|
||||
echo.
|
||||
echo ===============================================
|
||||
echo wis-free-v3 - Offline Whisper Installer
|
||||
echo ===============================================
|
||||
echo.
|
||||
echo Install directory: %s
|
||||
echo Model: %s (%s)
|
||||
echo.
|
||||
|
||||
echo Checking prerequisites...
|
||||
if not exist "C:\Windows\System32\msvcp140.dll" (
|
||||
echo.
|
||||
echo ERROR: Microsoft Visual C++ Redistributable is missing.
|
||||
echo This is required for the offline model to run.
|
||||
echo.
|
||||
echo Please download and install it from:
|
||||
echo https://aka.ms/vs/17/release/vc_redist.x64.exe
|
||||
echo.
|
||||
echo After installing, restart wis-free-v3 and try again.
|
||||
echo.
|
||||
pause
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
echo.
|
||||
echo [1/4] Creating directories...
|
||||
mkdir "%s" 2>nul
|
||||
|
||||
echo.
|
||||
echo [2/4] Downloading whisper.cpp binary...
|
||||
echo URL: %s
|
||||
curl -L --retry 3 --progress-bar -o "%s" "%s"
|
||||
if %%ERRORLEVEL%% neq 0 (
|
||||
echo.
|
||||
echo ERROR: Failed to download whisper.cpp
|
||||
pause
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
echo.
|
||||
echo [3/4] Extracting whisper.cpp...
|
||||
powershell -Command "Expand-Archive -Path '%s' -DestinationPath '%s' -Force"
|
||||
if %%ERRORLEVEL%% neq 0 (
|
||||
echo ERROR: Failed to extract whisper.cpp
|
||||
pause
|
||||
exit /b 1
|
||||
)
|
||||
del "%s"
|
||||
|
||||
echo.
|
||||
echo [4/4] Downloading Whisper %s model (%s)...
|
||||
echo URL: %s
|
||||
echo.
|
||||
echo This may take several minutes depending on your internet speed...
|
||||
echo.
|
||||
curl -L --retry 3 --progress-bar -o "%s" "%s"
|
||||
if %%ERRORLEVEL%% neq 0 (
|
||||
echo.
|
||||
echo ERROR: Failed to download model
|
||||
pause
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
echo.
|
||||
echo Verifying installation...
|
||||
if not exist "%s" (
|
||||
echo ERROR: Model file not found
|
||||
pause
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
echo.
|
||||
echo Creating installation info...
|
||||
echo {"version":"1.8.2","model":"%s","install_path":"%s"} > "%s"
|
||||
|
||||
echo.
|
||||
echo ===============================================
|
||||
echo Installation Complete!
|
||||
echo ===============================================
|
||||
echo.
|
||||
echo Whisper.cpp and %s model installed successfully!
|
||||
echo.
|
||||
echo NEXT STEPS:
|
||||
echo 1. Close this window
|
||||
echo 2. Restart wis-free-v3
|
||||
echo 3. Select "Local Whisper" in settings
|
||||
echo.
|
||||
pause
|
||||
exit
|
||||
`,
|
||||
m.installDir,
|
||||
model, modelInfo.Size,
|
||||
m.installDir,
|
||||
whisperBinaryURL,
|
||||
zipPath, whisperBinaryURL,
|
||||
zipPath, m.installDir,
|
||||
zipPath,
|
||||
model, modelInfo.Size,
|
||||
modelURL,
|
||||
modelPath, modelURL,
|
||||
modelPath,
|
||||
model, strings.ReplaceAll(m.installDir, `\`, `\\`), infoPath,
|
||||
model,
|
||||
)
|
||||
|
||||
return script
|
||||
}
|
||||
|
||||
// Uninstall removes whisper installation
|
||||
func (m *Manager) Uninstall() error {
|
||||
if err := os.RemoveAll(m.installDir); err != nil {
|
||||
return fmt.Errorf("failed to remove whisper directory: %w", err)
|
||||
}
|
||||
logger.Info("Whisper uninstalled")
|
||||
return nil
|
||||
}
|
||||
|
||||
// Transcribe runs local whisper transcription
|
||||
func (m *Manager) Transcribe(audioPath string) (string, error) {
|
||||
if !m.IsInstalled() {
|
||||
return "", fmt.Errorf("whisper is not installed")
|
||||
}
|
||||
|
||||
info, err := m.GetInstalledInfo()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to get installation info: %w", err)
|
||||
}
|
||||
|
||||
binaryPath := m.getBinaryPath()
|
||||
modelFilename := Models[info.Model].Filename
|
||||
|
||||
// Check for model in Release directory first, then main directory
|
||||
modelPath := filepath.Join(m.installDir, "Release", modelFilename)
|
||||
if _, err := os.Stat(modelPath); os.IsNotExist(err) {
|
||||
modelPath = filepath.Join(m.installDir, modelFilename)
|
||||
}
|
||||
|
||||
logger.Info("Running whisper: %s -m %s -f %s", binaryPath, modelPath, audioPath)
|
||||
|
||||
// Run whisper with simple arguments: ./main -m model.bin -f audio.wav
|
||||
// Vulkan build uses GPU by default, no need for -ngl
|
||||
cmd := exec.Command(binaryPath, "-m", modelPath, "-f", audioPath)
|
||||
|
||||
// Set working directory to the binary's location so it can find DLLs
|
||||
cmd.Dir = filepath.Dir(binaryPath)
|
||||
|
||||
// Hide the console window
|
||||
cmd.SysProcAttr = &syscall.SysProcAttr{
|
||||
HideWindow: true,
|
||||
CreationFlags: 0x08000000, // CREATE_NO_WINDOW
|
||||
}
|
||||
|
||||
output, err := cmd.CombinedOutput()
|
||||
outputStr := string(output)
|
||||
|
||||
if err != nil {
|
||||
logger.Error("Whisper failed: %v, output: %s", err, outputStr)
|
||||
return "", fmt.Errorf("transcription failed: %w", err)
|
||||
}
|
||||
|
||||
// Parse the output - whisper.cpp outputs transcribed text to stdout
|
||||
lines := strings.Split(outputStr, "\n")
|
||||
var textLines []string
|
||||
|
||||
for _, line := range lines {
|
||||
line = strings.TrimSpace(line)
|
||||
|
||||
// Skip empty lines
|
||||
if line == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
// Skip all system/debug info
|
||||
if strings.Contains(line, "whisper_") ||
|
||||
strings.Contains(line, "main:") ||
|
||||
strings.Contains(line, "system_info:") ||
|
||||
strings.Contains(line, "ld_") ||
|
||||
strings.Contains(line, "cuda_") ||
|
||||
strings.Contains(line, "ggml_vulkan") {
|
||||
continue
|
||||
}
|
||||
|
||||
// Handle timestamped lines like: [00:00:00.000 --> 00:00:07.280] Text
|
||||
if strings.HasPrefix(line, "[") && strings.Contains(line, "]") {
|
||||
// Extract text after the timestamp
|
||||
parts := strings.SplitN(line, "]", 2)
|
||||
if len(parts) > 1 {
|
||||
text := strings.TrimSpace(parts[1])
|
||||
if text != "" {
|
||||
textLines = append(textLines, text)
|
||||
}
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
// Fallback for non-prefixed text lines (if any)
|
||||
textLines = append(textLines, line)
|
||||
}
|
||||
|
||||
result := strings.Join(textLines, " ")
|
||||
result = strings.TrimSpace(result)
|
||||
|
||||
if result == "" {
|
||||
// If still no text, return simple fallback or raw output if short
|
||||
if len(outputStr) < 200 {
|
||||
return strings.TrimSpace(outputStr), nil
|
||||
}
|
||||
// Return empty string rather than spamming user with massive logs
|
||||
return "", nil
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// getBinaryPath returns the path to the whisper binary
|
||||
func (m *Manager) getBinaryPath() string {
|
||||
// whisper.cpp extracts to a Release subdirectory
|
||||
releaseDir := filepath.Join(m.installDir, "Release")
|
||||
|
||||
// Try different possible binary names
|
||||
// whisper-cli.exe is the new standard (main.exe is deprecated)
|
||||
possibleNames := []string{
|
||||
"whisper-cli.exe",
|
||||
"main.exe",
|
||||
}
|
||||
|
||||
// First check in Release subdirectory
|
||||
for _, name := range possibleNames {
|
||||
path := filepath.Join(releaseDir, name)
|
||||
if _, err := os.Stat(path); err == nil {
|
||||
return path
|
||||
}
|
||||
}
|
||||
|
||||
// Then check in main install directory
|
||||
for _, name := range possibleNames {
|
||||
path := filepath.Join(m.installDir, name)
|
||||
if _, err := os.Stat(path); err == nil {
|
||||
return path
|
||||
}
|
||||
}
|
||||
|
||||
// Default to Release/main.exe
|
||||
return filepath.Join(releaseDir, "main.exe")
|
||||
}
|
||||
|
||||
// CheckOnline checks if internet is available
|
||||
func CheckOnline() bool {
|
||||
client := &http.Client{
|
||||
Timeout: 3 * time.Second,
|
||||
}
|
||||
|
||||
resp, err := client.Get("https://api.groq.com")
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// DownloadProgress represents download progress
|
||||
type DownloadProgress struct {
|
||||
Downloaded int64
|
||||
Total int64
|
||||
Percent float64
|
||||
}
|
||||
|
||||
// downloadFile downloads a file with progress reporting
|
||||
func downloadFile(url, dest string, progress chan<- DownloadProgress) error {
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
out, err := os.Create(dest)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer out.Close()
|
||||
|
||||
total := resp.ContentLength
|
||||
var downloaded int64
|
||||
|
||||
buf := make([]byte, 32*1024)
|
||||
for {
|
||||
n, err := resp.Body.Read(buf)
|
||||
if n > 0 {
|
||||
out.Write(buf[:n])
|
||||
downloaded += int64(n)
|
||||
if progress != nil && total > 0 {
|
||||
progress <- DownloadProgress{
|
||||
Downloaded: downloaded,
|
||||
Total: total,
|
||||
Percent: float64(downloaded) / float64(total) * 100,
|
||||
}
|
||||
}
|
||||
}
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
// Package startup manages Windows startup registry entries for the application.
|
||||
// It allows the application to automatically start when the user logs in.
|
||||
package startup
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"golang.org/x/sys/windows/registry"
|
||||
)
|
||||
|
||||
// Windows Registry constants
|
||||
const (
|
||||
registryPath = `SOFTWARE\Microsoft\Windows\CurrentVersion\Run`
|
||||
appName = "WISNative"
|
||||
)
|
||||
|
||||
// AddToStartup adds the current executable to Windows startup.
|
||||
// The application will start automatically when the user logs in.
|
||||
func AddToStartup() error {
|
||||
exePath, err := getExecutablePath()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get executable path: %w", err)
|
||||
}
|
||||
|
||||
key, err := registry.OpenKey(
|
||||
registry.CURRENT_USER,
|
||||
registryPath,
|
||||
registry.SET_VALUE,
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to open registry key: %w", err)
|
||||
}
|
||||
defer key.Close()
|
||||
|
||||
if err := key.SetStringValue(appName, exePath); err != nil {
|
||||
return fmt.Errorf("failed to set registry value: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// RemoveFromStartup removes the application from Windows startup.
|
||||
func RemoveFromStartup() error {
|
||||
key, err := registry.OpenKey(
|
||||
registry.CURRENT_USER,
|
||||
registryPath,
|
||||
registry.SET_VALUE,
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to open registry key: %w", err)
|
||||
}
|
||||
defer key.Close()
|
||||
|
||||
if err := key.DeleteValue(appName); err != nil {
|
||||
// Ignore error if value doesn't exist
|
||||
if err != registry.ErrNotExist {
|
||||
return fmt.Errorf("failed to delete registry value: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// IsInStartup checks if the application is configured to start with Windows.
|
||||
func IsInStartup() bool {
|
||||
key, err := registry.OpenKey(
|
||||
registry.CURRENT_USER,
|
||||
registryPath,
|
||||
registry.QUERY_VALUE,
|
||||
)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
defer key.Close()
|
||||
|
||||
_, _, err = key.GetStringValue(appName)
|
||||
return err == nil
|
||||
}
|
||||
|
||||
// getExecutablePath returns the absolute path to the current executable.
|
||||
func getExecutablePath() (string, error) {
|
||||
exe, err := os.Executable()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return filepath.Abs(exe)
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,455 @@
|
||||
package overlay
|
||||
|
||||
import (
|
||||
"math"
|
||||
"runtime"
|
||||
"strings"
|
||||
"sync"
|
||||
"syscall"
|
||||
"time"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
var (
|
||||
user32 = syscall.NewLazyDLL("user32.dll")
|
||||
gdi32 = syscall.NewLazyDLL("gdi32.dll")
|
||||
procCreateWindowEx = user32.NewProc("CreateWindowExW")
|
||||
procDefWindowProc = user32.NewProc("DefWindowProcW")
|
||||
procDispatchMessage = user32.NewProc("DispatchMessageW")
|
||||
procPeekMessage = user32.NewProc("PeekMessageW")
|
||||
procRegisterClassEx = user32.NewProc("RegisterClassExW")
|
||||
procTranslateMessage = user32.NewProc("TranslateMessage")
|
||||
procShowWindow = user32.NewProc("ShowWindow")
|
||||
procUpdateWindow = user32.NewProc("UpdateWindow")
|
||||
procGetSystemMetrics = user32.NewProc("GetSystemMetrics")
|
||||
procSetLayeredWindowAttributes = user32.NewProc("SetLayeredWindowAttributes")
|
||||
procBeginPaint = user32.NewProc("BeginPaint")
|
||||
procEndPaint = user32.NewProc("EndPaint")
|
||||
procCreateSolidBrush = gdi32.NewProc("CreateSolidBrush")
|
||||
procCreateFontW = gdi32.NewProc("CreateFontW")
|
||||
procSelectObject = gdi32.NewProc("SelectObject")
|
||||
procDeleteObject = gdi32.NewProc("DeleteObject")
|
||||
procSetBkMode = gdi32.NewProc("SetBkMode")
|
||||
procSetTextColor = gdi32.NewProc("SetTextColor")
|
||||
procDrawTextW = user32.NewProc("DrawTextW")
|
||||
procPostMessage = user32.NewProc("PostMessageW")
|
||||
procInvalidateRect = user32.NewProc("InvalidateRect")
|
||||
procLoadCursor = user32.NewProc("LoadCursorW")
|
||||
procCreatePen = gdi32.NewProc("CreatePen")
|
||||
procEllipse = gdi32.NewProc("Ellipse")
|
||||
procCreateRoundRectRgn = gdi32.NewProc("CreateRoundRectRgn")
|
||||
procSetWindowRgn = user32.NewProc("SetWindowRgn")
|
||||
procFillRgn = gdi32.NewProc("FillRgn")
|
||||
procCreateCompatibleDC = gdi32.NewProc("CreateCompatibleDC")
|
||||
procCreateCompatibleBitmap = gdi32.NewProc("CreateCompatibleBitmap")
|
||||
procBitBlt = gdi32.NewProc("BitBlt")
|
||||
procDeleteDC = gdi32.NewProc("DeleteDC")
|
||||
)
|
||||
|
||||
const (
|
||||
WS_POPUP = 0x80000000
|
||||
WS_EX_LAYERED = 0x00080000
|
||||
WS_EX_TOPMOST = 0x00000008
|
||||
WS_EX_TOOLWINDOW = 0x00000080
|
||||
WS_EX_TRANSPARENT = 0x00000020
|
||||
SW_SHOW = 5
|
||||
SW_HIDE = 0
|
||||
LWA_ALPHA = 0x00000002
|
||||
SM_CXSCREEN = 0
|
||||
SM_CYSCREEN = 1
|
||||
TRANSPARENT_BK = 1
|
||||
DT_CENTER = 0x00000001
|
||||
DT_VCENTER = 0x00000004
|
||||
DT_SINGLELINE = 0x00000020
|
||||
WM_PAINT = 0x000F
|
||||
WM_DESTROY = 0x0002
|
||||
WM_CLOSE = 0x0010
|
||||
IDC_ARROW = 32512
|
||||
PM_REMOVE = 0x0001
|
||||
PS_SOLID = 0
|
||||
WM_ERASEBKGND = 0x0014
|
||||
)
|
||||
|
||||
const (
|
||||
COLOR_BG_DARK = 0x1A0F0B // Deep dark background (BGR for #0b0f1a)
|
||||
COLOR_MIC_RED = 0x4545FF // Red for recording (BGR)
|
||||
COLOR_MIC_GREEN = 0x50C850 // Green for ready
|
||||
COLOR_MIC_ORANGE = 0x00A5FF // Orange for processing
|
||||
COLOR_WHITE = 0xFFFFFF
|
||||
COLOR_GRAY = 0x808080
|
||||
)
|
||||
|
||||
// Overlay manages a transparent overlay window
|
||||
type Overlay struct {
|
||||
hwnd syscall.Handle
|
||||
text string
|
||||
isShowing bool
|
||||
running bool
|
||||
mu sync.RWMutex
|
||||
stopCh chan struct{}
|
||||
bgBrush syscall.Handle
|
||||
hFont syscall.Handle
|
||||
volume float64 // Current raw volume (0.0 - 1.0)
|
||||
smoothedVolume float64 // Moving average volume for smoother animations
|
||||
}
|
||||
|
||||
var globalOverlay *Overlay
|
||||
|
||||
// NewOverlay creates a new overlay instance
|
||||
func NewOverlay() *Overlay {
|
||||
o := &Overlay{
|
||||
text: "Ready",
|
||||
stopCh: make(chan struct{}),
|
||||
}
|
||||
globalOverlay = o
|
||||
go o.run()
|
||||
return o
|
||||
}
|
||||
|
||||
// Show displays the overlay with the given message
|
||||
func (o *Overlay) Show(message string) {
|
||||
o.mu.Lock()
|
||||
o.text = message
|
||||
o.isShowing = true
|
||||
o.mu.Unlock()
|
||||
|
||||
if o.hwnd != 0 {
|
||||
procInvalidateRect.Call(uintptr(o.hwnd), 0, 1)
|
||||
procShowWindow.Call(uintptr(o.hwnd), SW_SHOW)
|
||||
}
|
||||
}
|
||||
|
||||
// Hide hides the overlay
|
||||
func (o *Overlay) Hide() {
|
||||
o.mu.Lock()
|
||||
o.isShowing = false
|
||||
o.mu.Unlock()
|
||||
|
||||
if o.hwnd != 0 {
|
||||
procShowWindow.Call(uintptr(o.hwnd), SW_HIDE)
|
||||
}
|
||||
}
|
||||
|
||||
// SetVolume updates the current audio volume level
|
||||
func (o *Overlay) SetVolume(level float64) {
|
||||
o.mu.Lock()
|
||||
o.volume = level
|
||||
// Stronger smoothing: 10% new value, 90% old value to reduce jitter/flicker
|
||||
o.smoothedVolume = (o.smoothedVolume * 0.9) + (level * 0.1)
|
||||
o.mu.Unlock()
|
||||
}
|
||||
|
||||
// Close stops the overlay
|
||||
func (o *Overlay) Close() {
|
||||
if o.hwnd != 0 {
|
||||
procPostMessage.Call(uintptr(o.hwnd), WM_CLOSE, 0, 0)
|
||||
}
|
||||
if o.bgBrush != 0 {
|
||||
procDeleteObject.Call(uintptr(o.bgBrush))
|
||||
}
|
||||
if o.hFont != 0 {
|
||||
procDeleteObject.Call(uintptr(o.hFont))
|
||||
}
|
||||
close(o.stopCh)
|
||||
}
|
||||
|
||||
func (o *Overlay) run() {
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
o.running = true
|
||||
defer func() { o.running = false }()
|
||||
|
||||
className := syscall.StringToUTF16Ptr("wis-free-v3Overlay")
|
||||
|
||||
var wc wndClassEx
|
||||
wc.cbSize = uint32(unsafe.Sizeof(wc))
|
||||
wc.lpfnWndProc = syscall.NewCallback(overlayWndProc)
|
||||
wc.lpszClassName = className
|
||||
|
||||
// Pre-create resources
|
||||
o.bgBrush = syscall.Handle(uintptr(0))
|
||||
brushRec, _, _ := procCreateSolidBrush.Call(COLOR_BG_DARK)
|
||||
o.bgBrush = syscall.Handle(brushRec)
|
||||
|
||||
fontName := syscall.StringToUTF16Ptr("Segoe UI")
|
||||
fontRec, _, _ := procCreateFontW.Call(
|
||||
18, 0, 0, 0, 600,
|
||||
0, 0, 0, 0, 0, 0, 0, 0,
|
||||
uintptr(unsafe.Pointer(fontName)),
|
||||
)
|
||||
o.hFont = syscall.Handle(fontRec)
|
||||
|
||||
cursor, _, _ := procLoadCursor.Call(0, uintptr(IDC_ARROW))
|
||||
wc.hCursor = syscall.Handle(cursor)
|
||||
wc.hbrBackground = o.bgBrush
|
||||
|
||||
procRegisterClassEx.Call(uintptr(unsafe.Pointer(&wc)))
|
||||
|
||||
// Get screen dimensions
|
||||
screenWidth, _, _ := procGetSystemMetrics.Call(SM_CXSCREEN)
|
||||
screenHeight, _, _ := procGetSystemMetrics.Call(SM_CYSCREEN)
|
||||
|
||||
// Pill dimensions
|
||||
width := 120
|
||||
height := 46
|
||||
x := (int(screenWidth) - width) / 2
|
||||
y := int(screenHeight) - height - 80 // 80px from bottom
|
||||
|
||||
hwnd, _, _ := procCreateWindowEx.Call(
|
||||
WS_EX_LAYERED|WS_EX_TOPMOST|WS_EX_TOOLWINDOW|WS_EX_TRANSPARENT,
|
||||
uintptr(unsafe.Pointer(className)),
|
||||
0,
|
||||
WS_POPUP,
|
||||
uintptr(x), uintptr(y), uintptr(width), uintptr(height),
|
||||
0, 0, 0, 0,
|
||||
)
|
||||
|
||||
if hwnd == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
o.hwnd = syscall.Handle(hwnd)
|
||||
|
||||
// Create a rounded region to clip the window (true pill shape, no corners)
|
||||
// The corner radius should be half the height for a perfect pill
|
||||
rgn, _, _ := procCreateRoundRectRgn.Call(0, 0, uintptr(width+1), uintptr(height+1), uintptr(height), uintptr(height))
|
||||
procSetWindowRgn.Call(hwnd, rgn, 1)
|
||||
|
||||
// Set window transparency
|
||||
procSetLayeredWindowAttributes.Call(uintptr(hwnd), 0, 240, LWA_ALPHA)
|
||||
procShowWindow.Call(uintptr(hwnd), SW_HIDE)
|
||||
procUpdateWindow.Call(uintptr(hwnd))
|
||||
|
||||
// Message loop
|
||||
ticker := time.NewTicker(16 * time.Millisecond) // ~60 FPS
|
||||
defer ticker.Stop()
|
||||
|
||||
var msg msg
|
||||
for {
|
||||
select {
|
||||
case <-o.stopCh:
|
||||
return
|
||||
case <-ticker.C:
|
||||
if o.hwnd != 0 && o.isShowing {
|
||||
// Only invalidate if we are in a state that needs animation
|
||||
o.mu.RLock()
|
||||
needsAnimation := strings.HasPrefix(o.text, "Recording") || strings.HasPrefix(o.text, "Transcribing") || strings.HasPrefix(o.text, "Processing")
|
||||
o.mu.RUnlock()
|
||||
|
||||
if needsAnimation {
|
||||
procInvalidateRect.Call(uintptr(o.hwnd), 0, 0)
|
||||
}
|
||||
}
|
||||
default:
|
||||
ret, _, _ := procPeekMessage.Call(
|
||||
uintptr(unsafe.Pointer(&msg)),
|
||||
0, 0, 0,
|
||||
PM_REMOVE,
|
||||
)
|
||||
|
||||
if ret != 0 {
|
||||
if msg.message == WM_CLOSE {
|
||||
return
|
||||
}
|
||||
procTranslateMessage.Call(uintptr(unsafe.Pointer(&msg)))
|
||||
procDispatchMessage.Call(uintptr(unsafe.Pointer(&msg)))
|
||||
} else {
|
||||
// Higher resolution sleep for better responsiveness while keeping CPU low
|
||||
time.Sleep(2 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func overlayWndProc(hwnd syscall.Handle, msg uint32, wParam, lParam uintptr) uintptr {
|
||||
switch msg {
|
||||
case WM_ERASEBKGND:
|
||||
return 1 // Prevent white flash by handling erase ourselves
|
||||
|
||||
case WM_PAINT:
|
||||
var ps paintStruct
|
||||
hdc, _, _ := procBeginPaint.Call(uintptr(hwnd), uintptr(unsafe.Pointer(&ps)))
|
||||
|
||||
// Double buffering: Create a memory DC to draw off-screen first
|
||||
memDC, _, _ := procCreateCompatibleDC.Call(hdc)
|
||||
memBitmap, _, _ := procCreateCompatibleBitmap.Call(hdc, 120, 46)
|
||||
oldBitmap, _, _ := procSelectObject.Call(memDC, memBitmap)
|
||||
|
||||
// Access global state safely
|
||||
var text string
|
||||
var bgBrush syscall.Handle
|
||||
var hFont syscall.Handle
|
||||
var volume float64
|
||||
|
||||
if globalOverlay != nil {
|
||||
globalOverlay.mu.RLock()
|
||||
text = globalOverlay.text
|
||||
bgBrush = globalOverlay.bgBrush
|
||||
hFont = globalOverlay.hFont
|
||||
volume = globalOverlay.smoothedVolume
|
||||
globalOverlay.mu.RUnlock()
|
||||
}
|
||||
|
||||
// 1. Clear memory DC with background
|
||||
rgn, _, _ := procCreateRoundRectRgn.Call(0, 0, 121, 47, 46, 46)
|
||||
if bgBrush != 0 {
|
||||
procFillRgn.Call(memDC, rgn, uintptr(bgBrush))
|
||||
}
|
||||
procDeleteObject.Call(rgn)
|
||||
|
||||
// 2. Draw content to memory DC
|
||||
if strings.HasPrefix(text, "Recording") {
|
||||
// Draw animated waves for recording
|
||||
// Slowed down from 10.0 to 6.0
|
||||
t := float64(time.Now().UnixNano()) / 1e9 * 6.0
|
||||
|
||||
barCount := 7 // Reduced from 9 for shorter pill
|
||||
barWidth := 4
|
||||
barGap := 4
|
||||
totalWidth := barCount*barWidth + (barCount-1)*barGap
|
||||
startX := (120 - totalWidth) / 2
|
||||
centerY := 46 / 2
|
||||
|
||||
// Use colors for waves? The user said "instead of... the color... it's just like waves"
|
||||
// But maybe a subtle red pulse is nice? Let's use WHITE as requested.
|
||||
barBrush, _, _ := procCreateSolidBrush.Call(uintptr(COLOR_WHITE))
|
||||
|
||||
for i := 0; i < barCount; i++ {
|
||||
// Different phases for each bar
|
||||
phase := float64(i) * 0.8
|
||||
|
||||
// Base height from animation
|
||||
animH := 4.0 * math.Abs(math.Sin(t+phase))
|
||||
|
||||
// Scale height based on real-time volume
|
||||
// Boosted sensitivity from 40.0 to 120.0 for better response to normal speech
|
||||
volH := volume * 120.0
|
||||
|
||||
// Total height: slow background wave + responsive voice spikes
|
||||
h := 6.0 + animH + volH
|
||||
|
||||
// Cap height to 38 (max pill center space)
|
||||
if h > 38 {
|
||||
h = 38
|
||||
}
|
||||
|
||||
x := int32(startX + i*(barWidth+barGap))
|
||||
y1 := int32(float64(centerY) - h/2)
|
||||
y2 := int32(float64(centerY) + h/2)
|
||||
|
||||
barRgn, _, _ := procCreateRoundRectRgn.Call(uintptr(x), uintptr(y1), uintptr(x+int32(barWidth)), uintptr(y2), 4, 4)
|
||||
procFillRgn.Call(memDC, barRgn, barBrush)
|
||||
procDeleteObject.Call(barRgn)
|
||||
}
|
||||
procDeleteObject.Call(barBrush)
|
||||
|
||||
} else if strings.HasPrefix(text, "Transcribing") || strings.HasPrefix(text, "Processing") {
|
||||
// Different animation for processing/transcribing (more uniform, pulsing)
|
||||
// Slowed down from 5.0 to 3.0
|
||||
t := float64(time.Now().UnixNano()) / 1e9 * 3.0
|
||||
|
||||
barCount := 5 // Reduced from 7 for shorter pill
|
||||
barWidth := 4
|
||||
barGap := 6
|
||||
totalWidth := barCount*barWidth + (barCount-1)*barGap
|
||||
startX := (120 - totalWidth) / 2
|
||||
centerY := 46 / 2
|
||||
|
||||
barBrush, _, _ := procCreateSolidBrush.Call(uintptr(COLOR_MIC_ORANGE)) // Orange for processing
|
||||
|
||||
for i := 0; i < barCount; i++ {
|
||||
// Subtler oscillation
|
||||
h := 10.0 + 10.0*math.Abs(math.Sin(t+float64(i)*0.3))
|
||||
|
||||
x := int32(startX + i*(barWidth+barGap))
|
||||
y1 := int32(float64(centerY) - h/2)
|
||||
y2 := int32(float64(centerY) + h/2)
|
||||
|
||||
barRgn, _, _ := procCreateRoundRectRgn.Call(uintptr(x), uintptr(y1), uintptr(x+int32(barWidth)), uintptr(y2), 4, 4)
|
||||
procFillRgn.Call(memDC, barRgn, barBrush)
|
||||
procDeleteObject.Call(barRgn)
|
||||
}
|
||||
procDeleteObject.Call(barBrush)
|
||||
} else {
|
||||
// Show text for errors or "Ready" (though Ready is rarely shown in overlay)
|
||||
// This keeps the user informed if something went wrong
|
||||
procSetBkMode.Call(memDC, TRANSPARENT_BK)
|
||||
|
||||
textColor := COLOR_WHITE
|
||||
if text == "Error" || strings.Contains(text, "Key") || strings.Contains(text, "Err") {
|
||||
textColor = COLOR_MIC_RED
|
||||
}
|
||||
|
||||
procSetTextColor.Call(memDC, uintptr(textColor))
|
||||
|
||||
if hFont != 0 {
|
||||
oldFont, _, _ := procSelectObject.Call(memDC, uintptr(hFont))
|
||||
// Centered text across the whole pill since there's no circle anymore
|
||||
textRect := rect{0, 0, 120, 46}
|
||||
textPtr := syscall.StringToUTF16Ptr(text)
|
||||
procDrawTextW.Call(memDC, uintptr(unsafe.Pointer(textPtr)), ^uintptr(0), uintptr(unsafe.Pointer(&textRect)), DT_CENTER|DT_VCENTER|DT_SINGLELINE)
|
||||
procSelectObject.Call(memDC, oldFont)
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Copy everything from memory DC to screen (prevents flicker)
|
||||
procBitBlt.Call(hdc, 0, 0, 120, 46, memDC, 0, 0, 0x00CC0020) // SRCCOPY
|
||||
|
||||
// Cleanup memory DC
|
||||
procSelectObject.Call(memDC, oldBitmap)
|
||||
procDeleteObject.Call(memBitmap)
|
||||
procDeleteDC.Call(memDC)
|
||||
|
||||
procEndPaint.Call(uintptr(hwnd), uintptr(unsafe.Pointer(&ps)))
|
||||
return 0
|
||||
|
||||
case WM_DESTROY:
|
||||
return 0
|
||||
}
|
||||
|
||||
ret, _, _ := procDefWindowProc.Call(uintptr(hwnd), uintptr(msg), wParam, lParam)
|
||||
return ret
|
||||
}
|
||||
|
||||
// Structs for Windows API
|
||||
type wndClassEx struct {
|
||||
cbSize uint32
|
||||
style uint32
|
||||
lpfnWndProc uintptr
|
||||
cbClsExtra int32
|
||||
cbWndExtra int32
|
||||
hInstance syscall.Handle
|
||||
hIcon syscall.Handle
|
||||
hCursor syscall.Handle
|
||||
hbrBackground syscall.Handle
|
||||
lpszMenuName *uint16
|
||||
lpszClassName *uint16
|
||||
hIconSm syscall.Handle
|
||||
}
|
||||
|
||||
type msg struct {
|
||||
hwnd syscall.Handle
|
||||
message uint32
|
||||
wParam uintptr
|
||||
lParam uintptr
|
||||
time uint32
|
||||
pt point
|
||||
}
|
||||
|
||||
type point struct {
|
||||
x, y int32
|
||||
}
|
||||
|
||||
type paintStruct struct {
|
||||
hdc syscall.Handle
|
||||
fErase int32
|
||||
rcPaint rect
|
||||
fRestore int32
|
||||
fIncUpdate int32
|
||||
rgbReserved [32]byte
|
||||
}
|
||||
|
||||
type rect struct {
|
||||
left, top, right, bottom int32
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package settings
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"wis-free-v3/internal/config"
|
||||
)
|
||||
|
||||
// ShowSettings displays current settings
|
||||
func ShowSettings(cfg *config.Config) {
|
||||
fmt.Println("\n=== wis-free-v3 Settings ===")
|
||||
fmt.Printf("API Key: %s...%s\n", cfg.APIKey[:8], cfg.APIKey[len(cfg.APIKey)-4:])
|
||||
fmt.Printf("Whisper Model: %s\n", cfg.WhisperModel)
|
||||
fmt.Printf("AI Model: %s\n", cfg.AIModel)
|
||||
fmt.Printf("Shortcut: %s\n", cfg.Shortcut)
|
||||
fmt.Println("===========================")
|
||||
}
|
||||
|
||||
// EditSettings provides a simple way to modify settings
|
||||
func EditSettings(cfg *config.Config) error {
|
||||
// For now, just return the config
|
||||
// In a full implementation, this would open a dialog or prompt
|
||||
fmt.Println("To edit settings, modify: ~/.wis-free-v3/config.json")
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 66 KiB |
@@ -0,0 +1,131 @@
|
||||
// Package tray provides system tray functionality for the application.
|
||||
// It displays an icon in the Windows notification area with a context menu.
|
||||
package tray
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"os"
|
||||
|
||||
"wis-free-v3/internal/config"
|
||||
"wis-free-v3/internal/logger"
|
||||
"wis-free-v3/internal/system/startup"
|
||||
|
||||
"github.com/getlantern/systray"
|
||||
)
|
||||
|
||||
//go:embed icon.ico
|
||||
var iconData []byte
|
||||
|
||||
// App defines the interface required by the tray package to interact with the main application.
|
||||
type App interface {
|
||||
Quit()
|
||||
GetConfig() *config.Config
|
||||
ShowSettings()
|
||||
}
|
||||
|
||||
// Menu item references for dynamic updates
|
||||
var statusMenuItem *systray.MenuItem
|
||||
|
||||
// Start initializes and runs the system tray.
|
||||
// This function blocks until the tray is terminated.
|
||||
func Start(app App) {
|
||||
systray.Run(
|
||||
func() { onReady(app) },
|
||||
onExit,
|
||||
)
|
||||
}
|
||||
|
||||
// onReady is called when the system tray is ready to be configured.
|
||||
func onReady(app App) {
|
||||
// Configure tray icon and tooltip
|
||||
systray.SetIcon(iconData)
|
||||
systray.SetTitle("wis-free-v3")
|
||||
systray.SetTooltip(buildTooltip(app))
|
||||
|
||||
// Build menu structure
|
||||
statusMenuItem = systray.AddMenuItem("Status: Ready", "Current application status")
|
||||
statusMenuItem.Disable()
|
||||
|
||||
systray.AddSeparator()
|
||||
|
||||
menuSettings := systray.AddMenuItem("Settings", "Open settings window")
|
||||
menuStartup := systray.AddMenuItemCheckbox(
|
||||
"Start with Windows",
|
||||
"Automatically start when Windows boots",
|
||||
startup.IsInStartup(),
|
||||
)
|
||||
|
||||
systray.AddSeparator()
|
||||
|
||||
menuExit := systray.AddMenuItem("Exit", "Close the application")
|
||||
|
||||
// Handle menu events in background
|
||||
go handleMenuEvents(app, menuSettings, menuStartup, menuExit)
|
||||
}
|
||||
|
||||
// handleMenuEvents processes menu item click events.
|
||||
func handleMenuEvents(app App, settings, startupItem, exit *systray.MenuItem) {
|
||||
for {
|
||||
select {
|
||||
case <-settings.ClickedCh:
|
||||
app.ShowSettings()
|
||||
|
||||
case <-startupItem.ClickedCh:
|
||||
toggleStartup(startupItem)
|
||||
|
||||
case <-exit.ClickedCh:
|
||||
handleExit(app)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// toggleStartup handles the startup toggle menu item.
|
||||
func toggleStartup(item *systray.MenuItem) {
|
||||
if item.Checked() {
|
||||
if err := startup.RemoveFromStartup(); err != nil {
|
||||
logger.Error("Failed to remove from startup: %v", err)
|
||||
} else {
|
||||
item.Uncheck()
|
||||
logger.Info("Removed from Windows startup")
|
||||
}
|
||||
} else {
|
||||
if err := startup.AddToStartup(); err != nil {
|
||||
logger.Error("Failed to add to startup: %v", err)
|
||||
} else {
|
||||
item.Check()
|
||||
logger.Info("Added to Windows startup")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// handleExit cleanly shuts down the application.
|
||||
func handleExit(app App) {
|
||||
logger.Info("User requested application exit")
|
||||
app.Quit()
|
||||
systray.Quit()
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
// buildTooltip creates the tray icon tooltip text.
|
||||
func buildTooltip(app App) string {
|
||||
shortcut := "Ctrl+K"
|
||||
if cfg := app.GetConfig(); cfg != nil && cfg.Shortcut != "" {
|
||||
shortcut = cfg.Shortcut
|
||||
}
|
||||
return "wis-free-v3 - " + shortcut + " to record"
|
||||
}
|
||||
|
||||
// UpdateStatus updates the status text displayed in the tray menu.
|
||||
func UpdateStatus(status string) {
|
||||
if statusMenuItem != nil {
|
||||
statusMenuItem.SetTitle("Status: " + status)
|
||||
systray.SetTooltip("wis-free-v3 - " + status)
|
||||
}
|
||||
}
|
||||
|
||||
// onExit is called when the system tray is shutting down.
|
||||
func onExit() {
|
||||
logger.Info("System tray terminated")
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user