Initial commit from agent
This commit is contained in:
0
utils/__init__.py
Normal file
0
utils/__init__.py
Normal file
49
utils/audio_utils.py
Normal file
49
utils/audio_utils.py
Normal file
@@ -0,0 +1,49 @@
|
||||
"""
|
||||
Audio utilities — microphone capture helpers using PyAudio.
|
||||
Captures audio from the default input device and returns WAV bytes.
|
||||
"""
|
||||
import io
|
||||
import wave
|
||||
import pyaudio
|
||||
|
||||
CHUNK = 1024
|
||||
FORMAT = pyaudio.paInt16
|
||||
CHANNELS = 1
|
||||
RATE = 16000
|
||||
|
||||
def record_audio(duration: float = 5.0) -> bytes:
|
||||
"""
|
||||
Record audio from default microphone for the given duration.
|
||||
Returns raw WAV bytes compatible with Whisper.
|
||||
"""
|
||||
p = pyaudio.PyAudio()
|
||||
stream = p.open(
|
||||
format=FORMAT, channels=CHANNELS,
|
||||
rate=RATE, input=True, frames_per_buffer=CHUNK
|
||||
)
|
||||
frames = []
|
||||
for _ in range(int(RATE / CHUNK * duration)):
|
||||
data = stream.read(CHUNK, exception_on_overflow=False)
|
||||
frames.append(data)
|
||||
stream.stop_stream()
|
||||
stream.close()
|
||||
p.terminate()
|
||||
|
||||
buf = io.BytesIO()
|
||||
with wave.open(buf, 'wb') as wf:
|
||||
wf.setnchannels(CHANNELS)
|
||||
wf.setsampwidth(p.get_sample_size(FORMAT))
|
||||
wf.setframerate(RATE)
|
||||
wf.writeframes(b''.join(frames))
|
||||
return buf.getvalue()
|
||||
|
||||
def list_audio_devices() -> list[dict]:
|
||||
"""List all available audio input devices."""
|
||||
p = pyaudio.PyAudio()
|
||||
devices = []
|
||||
for i in range(p.get_device_count()):
|
||||
info = p.get_device_info_by_index(i)
|
||||
if info['maxInputChannels'] > 0:
|
||||
devices.append({'index': i, 'name': info['name']})
|
||||
p.terminate()
|
||||
return devices
|
||||
43
utils/window_utils.py
Normal file
43
utils/window_utils.py
Normal file
@@ -0,0 +1,43 @@
|
||||
"""
|
||||
Windows window utilities using win32gui.
|
||||
Enumerate and manage application windows.
|
||||
"""
|
||||
try:
|
||||
import win32gui
|
||||
import win32con
|
||||
|
||||
def get_active_window() -> dict:
|
||||
hwnd = win32gui.GetForegroundWindow()
|
||||
return {
|
||||
'hwnd': hwnd,
|
||||
'title': win32gui.GetWindowText(hwnd)
|
||||
}
|
||||
|
||||
def list_windows() -> list[dict]:
|
||||
windows = []
|
||||
def _enum(hwnd, _):
|
||||
if win32gui.IsWindowVisible(hwnd):
|
||||
title = win32gui.GetWindowText(hwnd)
|
||||
if title:
|
||||
windows.append({'hwnd': hwnd, 'title': title})
|
||||
win32gui.EnumWindows(_enum, None)
|
||||
return windows
|
||||
|
||||
def focus_window(title_substring: str) -> bool:
|
||||
"""Bring a window to foreground by partial title match."""
|
||||
def _enum(hwnd, _):
|
||||
if title_substring.lower() in win32gui.GetWindowText(hwnd).lower():
|
||||
win32gui.SetForegroundWindow(hwnd)
|
||||
return False
|
||||
win32gui.EnumWindows(_enum, None)
|
||||
return True
|
||||
|
||||
except ImportError:
|
||||
def get_active_window():
|
||||
return {'hwnd': None, 'title': 'Unknown (pywin32 not installed)'}
|
||||
|
||||
def list_windows():
|
||||
return []
|
||||
|
||||
def focus_window(title_substring: str):
|
||||
return False
|
||||
Reference in New Issue
Block a user