Ariver
2026-05-18 db6aabfb7aa4460c8858fec0a51534ebe34786f7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package engine
 
import (
    "fmt"
    "os"
    "path/filepath"
    "voicesnap/internal/logger"
    "voicesnap/internal/paths"
)
 
// Engine is the interface for ASR engines.
type Engine interface {
    // Recognize takes float32 PCM samples (16kHz mono) and returns the recognized text.
    Recognize(samples []float32) (string, error)
    // HardwareInfo returns a human-readable description of the hardware backend being used.
    HardwareInfo() string
    // Close releases engine resources.
    Close()
}
 
// ModelDir returns the path to the sensevoice model directory.
func ModelDir() string {
    return paths.ModelDir()
}
 
// ModelPath returns the path to the ONNX model file (prefers int8).
func ModelPath() string {
    dir := ModelDir()
    int8Path := filepath.Join(dir, "model.int8.onnx")
    if _, err := os.Stat(int8Path); err == nil {
        return int8Path
    }
    return filepath.Join(dir, "model.onnx")
}
 
// TokensPath returns the path to the tokens.txt file.
func TokensPath() string {
    return filepath.Join(ModelDir(), "tokens.txt")
}
 
// ModelExists checks if both the model and tokens files exist.
func ModelExists() bool {
    if _, err := os.Stat(ModelPath()); err != nil {
        return false
    }
    if _, err := os.Stat(TokensPath()); err != nil {
        return false
    }
    return true
}
 
// New creates a new platform-specific ASR engine.
// Returns an error if the model files are not found.
func New() (Engine, error) {
    if !ModelExists() {
        return nil, fmt.Errorf("model files not found in %s", ModelDir())
    }
 
    logger.Info("Loading model from %s", ModelPath())
    return newPlatformEngine()
}