platform

package
v0.14.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 5, 2026 License: GPL-3.0 Imports: 23 Imported by: 0

Documentation

Overview

pkg/platform/imgui.go Copyright(c) 2022-2024 vice contributors, licensed under the GNU Public License, Version 3. SPDX: GPL-3.0-only

Index

Constants

View Source
const (
	MouseButtonPrimary imgui.MouseButton = iota
	MouseButtonSecondary
	MouseButtonTertiary
	MouseButtonCount
)
View Source
const AudioInputSampleRate = 16000 // Whisper's native sample rate
View Source
const AudioSampleRate = 44100
View Source
const PrerollDuration = 200 // milliseconds

PrerollDuration is how much audio to buffer before PTT for capturing transmission starts.

Variables

View Source
var ErrCurrentlyPlayingSpeech = errors.New("Speech is currently playing")

Functions

func DecodeSpeechMP3 added in v0.14.0

func DecodeSpeechMP3(mp3 []byte) ([]int16, error)

DecodeSpeechMP3 decodes MP3 audio to PCM samples ready for playback. This can be called from any goroutine to pre-decode audio.

func GetAudioInputDevices added in v0.14.0

func GetAudioInputDevices() []string

GetAudioInputDevices returns a list of available audio input devices

func GetImGuiKeyName added in v0.14.0

func GetImGuiKeyName(key imgui.Key) string

func RequestMicrophoneAccess added in v0.14.0

func RequestMicrophoneAccess()

RequestMicrophoneAccess triggers the microphone permission dialog. On non-macOS platforms, this is a no-op since no permission is required.

Types

type AudioRecorder added in v0.14.0

type AudioRecorder struct {
	// contains filtered or unexported fields
}

AudioRecorder handles microphone recording

func NewAudioRecorder added in v0.14.0

func NewAudioRecorder(lg *log.Logger) *AudioRecorder

NewAudioRecorder creates a new audio recorder

func (*AudioRecorder) Close added in v0.14.0

func (ar *AudioRecorder) Close()

Close closes the audio recording device. Should be called when the application exits.

func (*AudioRecorder) GetPreroll added in v0.14.0

func (ar *AudioRecorder) GetPreroll() []int16

GetPreroll returns a copy of the current preroll buffer contents. This is useful for feeding preroll samples to a transcriber when starting recording.

func (*AudioRecorder) IsCapturing added in v0.14.0

func (ar *AudioRecorder) IsCapturing() bool

IsCapturing returns true if background capture is active.

func (*AudioRecorder) IsRecording added in v0.14.0

func (ar *AudioRecorder) IsRecording() bool

IsRecording returns true if currently recording

func (*AudioRecorder) SetStreamCallback added in v0.14.0

func (ar *AudioRecorder) SetStreamCallback(cb func([]int16))

SetStreamCallback sets a callback function that receives audio samples as they are recorded. This enables streaming audio to a transcriber. Pass nil to disable the callback.

func (*AudioRecorder) StartCapture added in v0.14.0

func (ar *AudioRecorder) StartCapture() error

StartCapture starts continuous background audio capture to the preroll buffer. This should be called when the app is ready to accept PTT input.

func (*AudioRecorder) StartCaptureWithDevice added in v0.14.0

func (ar *AudioRecorder) StartCaptureWithDevice(deviceName string) error

StartCaptureWithDevice starts continuous background audio capture from the specified device.

func (*AudioRecorder) StartRecording added in v0.14.0

func (ar *AudioRecorder) StartRecording() error

StartRecording starts recording audio from the default microphone. If capture is already active, includes the preroll buffer.

func (*AudioRecorder) StartRecordingWithDevice added in v0.14.0

func (ar *AudioRecorder) StartRecordingWithDevice(deviceName string) error

StartRecordingWithDevice starts recording audio from the specified microphone. If capture is already active on this device, includes the preroll buffer. Otherwise, opens the device and starts fresh (no preroll).

func (*AudioRecorder) StopCapture added in v0.14.0

func (ar *AudioRecorder) StopCapture()

StopCapture stops background audio capture.

func (*AudioRecorder) StopRecording added in v0.14.0

func (ar *AudioRecorder) StopRecording() ([]int16, error)

StopRecording stops recording and returns the recorded audio data. If capture was active, it continues running for future preroll.

type Config

type Config struct {
	InitialWindowSize     [2]int
	InitialWindowPosition [2]int

	EnableMSAA bool

	StartInFullScreen bool
	FullScreenMonitor int
}

type Cursor added in v0.14.0

type Cursor struct {
	// contains filtered or unexported fields
}

Cursor is a platform cursor handle created from a .cur file.

type KeyboardState

type KeyboardState struct {
	Input string
	// A key shows up here once each time it is pressed (though repeatedly
	// if key repeat kicks in.)
	Pressed   map[imgui.Key]any
	HeldFKeys map[imgui.Key]any
}

func (*KeyboardState) IsFKeyHeld

func (k *KeyboardState) IsFKeyHeld(key imgui.Key) bool

func (*KeyboardState) KeyAlt

func (k *KeyboardState) KeyAlt() bool

func (*KeyboardState) KeyControl

func (k *KeyboardState) KeyControl() bool

func (*KeyboardState) KeyShift

func (k *KeyboardState) KeyShift() bool

func (*KeyboardState) KeySuper

func (k *KeyboardState) KeySuper() bool

func (*KeyboardState) WasPressed

func (k *KeyboardState) WasPressed(key imgui.Key) bool

type MicrophoneAuthStatus added in v0.14.0

type MicrophoneAuthStatus int

MicrophoneAuthStatus represents the authorization status for microphone access

const (
	// MicAuthNotDetermined - User has not yet made a choice
	MicAuthNotDetermined MicrophoneAuthStatus = 0
	// MicAuthRestricted - Access restricted by system policy
	MicAuthRestricted MicrophoneAuthStatus = 1
	// MicAuthDenied - User explicitly denied access
	MicAuthDenied MicrophoneAuthStatus = 2
	// MicAuthAuthorized - User granted access
	MicAuthAuthorized MicrophoneAuthStatus = 3
)

func GetMicrophoneAuthorizationStatus added in v0.14.0

func GetMicrophoneAuthorizationStatus() MicrophoneAuthStatus

GetMicrophoneAuthorizationStatus returns the current microphone authorization status. On non-macOS platforms, this always returns MicAuthAuthorized since there's no system-level permission required.

func (MicrophoneAuthStatus) String added in v0.14.0

func (s MicrophoneAuthStatus) String() string

type MouseState

type MouseState struct {
	Pos           [2]float32
	DeltaPos      [2]float32
	Down          [MouseButtonCount]bool
	Clicked       [MouseButtonCount]bool
	Released      [MouseButtonCount]bool
	DoubleClicked [MouseButtonCount]bool
	Dragging      [MouseButtonCount]bool
	DragDelta     [2]float32
	Wheel         [2]float32
}

func (*MouseState) SetCursor

func (ms *MouseState) SetCursor(id imgui.MouseCursor)

type Platform

type Platform interface {
	// NewFrame marks the begin of a render pass; it forwards all current state to imgui IO.
	NewFrame()

	// ProcessEvents handles all pending window events. Returns true if
	// there were any events and false otherwise.
	ProcessEvents() bool

	// PostRender performs the buffer swap.
	PostRender()

	// Dispose is called when the application is shutting down and is when
	// resources are be freed.
	Dispose()

	// ShouldStop returns true if the window is to be closed.
	ShouldStop() bool

	// CancelShouldStop cancels a user's request to close the window.
	CancelShouldStop()

	// SetWindowTitle sets the title of the appllication window.
	SetWindowTitle(text string)

	// InputCharacters returns a string of all the characters (generally at most one!) that have
	// been entered since the last call to ProcessEvents.
	InputCharacters() string

	// EnableVSync specifies whether v-sync should be used when rendering;
	// v-sync is on by default and should only be disabled for benchmarking.
	EnableVSync(sync bool)

	// EnableFullScreen switches between the application running in windowed and fullscreen mode.
	EnableFullScreen(fullscreen bool)

	// IsFullScreen() returns true if the application is in full-screen mode.
	IsFullScreen() bool

	// GetAllMonitorNames() returns an array of all available monitors' names.
	GetAllMonitorNames() []string

	// DisplaySize returns the dimension of the display.
	DisplaySize() [2]float32

	// WindowSize returns the size of the window.
	WindowSize() [2]int

	// WindowSize returns the position of the window on the screen.
	WindowPosition() [2]int

	// FramebufferSize returns the dimension of the framebuffer.
	FramebufferSize() [2]float32

	// GetClipboard() returns an object that implements the imgui.Clipboard
	// interface so that copy and paste can be supported.
	GetClipboard() imgui.ClipboardHandler

	// Enables a mode where the mouse is constrained to be within the
	// specified pixel extent, specified in window coordinates.
	StartCaptureMouse(e math.Extent2D)

	// Disable mouse capture.
	EndCaptureMouse()

	// Enter/leave a mouse capture mode where the change in mouse position
	// is returned each frame through MouseState DeltaPos. The cursor is
	// hidden and the returned mouse position is kept fixed.
	StartMouseDeltaMode()
	StopMouseDeltaMode()

	// Moves the mouse cursor to the given position specified in window coordinates.
	SetMousePosition([2]float32)

	// Scaling factor to account for Retina-style displays
	DPIScale() float32

	// GetMouse returns a MouseState object that encapsulates the current state
	// of the mouse (position, buttons pressed, mouse wheel motion, etc.)
	GetMouse() *MouseState

	// GetKeyboard returns a KeyboardState object that stores keyboard input
	// and which keys are currently down.
	GetKeyboard() *KeyboardState

	// Cursor overrides. LoadCursorFromFile parses a .cur file and creates a
	// cursor handle that can be set as the active cursor via SetCursorOverride.
	LoadCursorFromFile(path string) (*Cursor, error)
	// SetCursorOverride replaces the OS cursor until cleared.
	SetCursorOverride(cursor *Cursor)
	// ClearCursorOverride removes any cursor override.
	ClearCursorOverride()

	// AddPCM registers an audio effect encoded via pulse code modulation.
	// It is assumed to be one channel audio sampled at AudioSampleRate.
	// The integer return value identifies the effect and can be passed to
	// the audio playing entrypoints.
	AddPCM(pcm []byte, rate int) (int, error)

	// Registers an MP3-based audio effect. As with AddPCM, assumes one
	// channel sampled at AudioSampleRate. The integer return value
	// identifies the effect and can be passed to the audio playing
	// entrypoints.
	AddMP3(mp3 []byte) (int, error)

	// TryEnqueueSpeechPCM queues pre-decoded PCM speech audio for playback.
	// If speech is currently being played, ErrCurrentlyPlayingSpeech is returned.
	// If non-nil, the provided callback function is called after the speech has finished.
	// Use DecodeSpeechMP3 to decode MP3 to PCM ahead of time.
	TryEnqueueSpeechPCM(pcm []int16, finished func()) error

	// SetSpeechGarbled enables or disables garbling of speech audio.
	// When enabled, speech is ducked and static noise is added.
	SetSpeechGarbled(garbled bool)

	// IsPlayingSpeech returns true if speech audio is currently playing.
	IsPlayingSpeech() bool

	// SetAudioVolume sets the volume for audio playback; the value passed
	// should be between 0 and 10.
	SetAudioVolume(vol int)

	// PlayAudioOnce plays the audio effect identified by the given identifier
	// once. Multiple audio effects may be played simultaneously.
	PlayAudioOnce(id int)

	// StartPlayAudioContinuous	starts playing the specified audio effect
	// continuously, until StopPlayAudioContinuous is called.
	StartPlayAudioContinuous(id int)

	// StopPlayAudio stops playback of the audio effect specified
	// by the given identifier.
	StopPlayAudio(id int)

	// Audio capture methods for continuous background capture with preroll buffer.
	// StartAudioCapture begins capturing to the preroll buffer (200ms ring buffer).
	// This allows recording to include audio from before PTT was pressed.
	StartAudioCapture() error
	StartAudioCaptureWithDevice(deviceName string) error
	StopAudioCapture()
	IsAudioCapturing() bool
	// GetAudioPreroll returns the current preroll buffer (200ms of audio before now).
	// Returns nil if not capturing.
	GetAudioPreroll() []int16

	// Audio recording methods. If capture is active, recording includes preroll.
	StartAudioRecording() error
	StartAudioRecordingWithDevice(deviceName string) error
	StopAudioRecording() ([]int16, error)
	IsAudioRecording() bool
	GetAudioInputDevices() []string

	// SetAudioStreamCallback sets a callback that receives audio samples
	// as they are recorded. This enables streaming audio to a transcriber.
	// Pass nil to disable the callback.
	SetAudioStreamCallback(cb func([]int16))

	// GetGPUInfo returns the GPU vendor and renderer strings from OpenGL.
	// Should be called after OpenGL is initialized.
	GetGPUInfo() (vendor, renderer string)
}

Platform is the interface that abstracts platform-specific features like creating windows, mouse and keyboard handling, etc.

func New

func New(config *Config, lg *log.Logger) (Platform, error)

New returns a new instance of a Platform implemented with a window of the specified size open at the specified position on the screen.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL