109 lines
3.3 KiB
Go
109 lines
3.3 KiB
Go
// Copyright (c) 2026 Storm Dragon
|
|
// Authors: Storm Dragon, Angus Kola
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
package speechdispatcher
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
// Abort interrupts blocked I/O and permanently prevents reconnection. Close
|
|
// subsequently releases state. It may run concurrently with any operation.
|
|
func (client *Client) Abort() {
|
|
client.aborted.Store(true)
|
|
if pair := client.sockets.Load(); pair != nil {
|
|
pair.data.Close()
|
|
pair.control.Close()
|
|
}
|
|
}
|
|
|
|
// ensureConnected runs under the speaking lock. Never replay a failed request:
|
|
// its acknowledgement may have been lost after the daemon accepted it.
|
|
func (client *Client) ensureConnected(ctx context.Context) error {
|
|
if err := ctx.Err(); err != nil {
|
|
return err
|
|
}
|
|
if client.closed || client.aborted.Load() {
|
|
return errors.New("speech client is closed")
|
|
}
|
|
client.cancelMutex.Lock()
|
|
defer client.cancelMutex.Unlock()
|
|
if client.conn != nil && (client.cancelConn != nil || !client.initialized) {
|
|
return nil
|
|
}
|
|
if !client.initialized {
|
|
return errors.New("speech client is not connected")
|
|
}
|
|
if client.conn != nil {
|
|
client.conn.Close()
|
|
}
|
|
if client.cancelConn != nil {
|
|
client.cancelConn.Close()
|
|
}
|
|
client.conn, client.reader = nil, nil
|
|
client.cancelConn, client.cancelReader = nil, nil
|
|
client.clientID = 0
|
|
return client.connectLocked(ctx)
|
|
}
|
|
|
|
// SpeakText preserves line breaks and whitespace for readers that prepare their
|
|
// own text. Speak provides the desktop reader's whitespace normalization.
|
|
func (client *Client) SpeakText(ctx context.Context, text string) error {
|
|
client.mutex.Lock()
|
|
defer client.mutex.Unlock()
|
|
if err := client.ensureConnected(ctx); err != nil {
|
|
return err
|
|
}
|
|
text = strings.ToValidUTF8(strings.ReplaceAll(text, "\x00", ""), "�")
|
|
text = strings.ReplaceAll(text, "\r\n", "\n")
|
|
text = strings.ReplaceAll(text, "\r", "\n")
|
|
return client.speak_block(ctx, strings.ReplaceAll(text, "\n", "\r\n"))
|
|
}
|
|
|
|
// Configure applies numeric settings on the current speaking connection and
|
|
// remembers successful changes for recovery. Nil leaves a parameter unchanged.
|
|
func (client *Client) Configure(ctx context.Context, rate, pitch, volume *int) error {
|
|
for _, value := range []*int{rate, pitch, volume} {
|
|
if value != nil && (*value < -100 || *value > 100) {
|
|
return errors.New("speech parameters must be between -100 and 100")
|
|
}
|
|
}
|
|
client.mutex.Lock()
|
|
defer client.mutex.Unlock()
|
|
if err := client.ensureConnected(ctx); err != nil {
|
|
return err
|
|
}
|
|
for _, item := range []struct {
|
|
name string
|
|
value *int
|
|
target **int
|
|
}{
|
|
{"RATE", rate, &client.settings.Rate}, {"PITCH", pitch, &client.settings.Pitch}, {"VOLUME", volume, &client.settings.Volume},
|
|
} {
|
|
if item.value == nil {
|
|
continue
|
|
}
|
|
if err := client.sendCommand(ctx, []string{"SET", "SELF", item.name, strconv.Itoa(*item.value)}); err != nil {
|
|
return err
|
|
}
|
|
value := *item.value
|
|
*item.target = &value
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Shutdown interrupts pending sends, cancels only this client's queued output,
|
|
// and closes both sockets. Readers that already cancel at shutdown can use Close.
|
|
func (client *Client) Shutdown(ctx context.Context) error {
|
|
client.aborted.Store(true)
|
|
if pair := client.sockets.Load(); pair != nil {
|
|
pair.data.Close()
|
|
}
|
|
err := client.Cancel(ctx)
|
|
return errors.Join(err, client.Close(ctx))
|
|
}
|