### Bash: Install Dependencies and Build STT CLI Tool Source: https://context7.com/xaionaro-go/speech/llms.txt This section provides bash commands to install necessary development libraries on Ubuntu/Debian systems, download a Whisper model, and build the `stt` command-line tool with CUDA support. The `stt` tool converts PCM audio from stdin to text. ```bash # Install dependencies (Ubuntu/Debian) apt install -y libavcodec-dev libavformat-dev libavfilter-dev \ libavdevice-dev libswscale-dev libsrt-openssl-dev libssl-dev \ libasound2-dev libxxf86vm-dev make cmake nvidia-cuda-toolkit \ cuda-toolkit-12-2 libxcursor-dev libxrandr-dev libxinerama-dev libxi-dev # Download a Whisper model cd thirdparty/whisper.cpp && ./models/download-ggml-model.sh medium # Build the stt tool with CUDA support ENABLE_CUDA=true make stt-linux-amd64 ``` -------------------------------- ### Bash: Build and Run STTD Speech-To-Text Daemon Source: https://context7.com/xaionaro-go/speech/llms.txt This section details the process of building and running the `sttd` command-line tool, which acts as a gRPC server for remote speech-to-text services. It includes steps for enabling CUDA support, downloading a large Whisper model, and starting the server with various configuration options like context management and GPU device selection. ```bash # Build the server with CUDA support ENABLE_CUDA=true make sttd-linux-amd64 # Download a large model for the server cd thirdparty/whisper.cpp && ./models/download-ggml-model.sh large-v3 # Start the server ./build/sttd-linux-amd64 \ --log-level=trace \ --default-model-file=thirdparty/whisper.cpp/models/ggml-large-v3.bin \ --use-gpu=true \ --gpu=0 \ --contexts=4 \ --cache-contexts=2 \ 0.0.0.0:1234 # Server options: # --contexts: Maximum concurrent transcription contexts # --cache-contexts: Number of initialized contexts to cache for reuse # --default-model-file: Model to use when clients don't provide one # --use-gpu: Enable CUDA acceleration # --gpu: GPU device ID to use ``` -------------------------------- ### Bash: Build and Run Subtitleswindow CLI Tool Source: https://context7.com/xaionaro-go/speech/llms.txt Provides bash commands to build and execute the `subtitleswindow` command-line tool. This tool displays live subtitles in a GUI window, useful for OBS integration. Examples cover basic microphone input, using an RTMP stream as input, and utilizing a remote server for speech-to-text processing. ```bash # Build with CUDA support ENABLE_CUDA=true make subtitleswindow-linux-amd64 # Basic usage with microphone input ./build/subtitleswindow-linux-amd64 thirdparty/whisper.cpp/models/ggml-medium.bin # With an RTMP stream as input ./build/subtitleswindow-linux-amd64 \ thirdparty/whisper.cpp/models/ggml-medium.bin \ rtmp://my.server:1935/myapp/mystream # Using remote server for processing ./build/subtitleswindow-linux-amd64 \ --remote-addr=my-server:1234 \ --translate=true \ "" # Empty model path when using remote ``` -------------------------------- ### Local Whisper Speech-To-Text Implementation in Go Source: https://context7.com/xaionaro-go/speech/llms.txt Provides a local speech-to-text implementation using whisper.cpp. This example demonstrates initializing the Whisper engine with model loading, configuration options for GPU acceleration, and processing transcription results from an output channel. It reads audio data from standard input. ```go package main import ( "context" "fmt" "os" "github.com/facebookincubator/go-belt/tool/logger" "github.com/xaionaro-go/speech/pkg/speech" "github.com/xaionaro-go/speech/pkg/speech/speechtotext/implementations/whisper" "github.com/xaionaro-go/speech/pkg/speech/speechtotext/implementations/whisper/types" ) func main() { ctx := context.Background() // Load the Whisper model (e.g., ggml-medium.bin) modelBytes, err := os.ReadFile("thirdparty/whisper.cpp/models/ggml-medium.bin") if err != nil { logger.Fatal(ctx, err) } // Configuration options for GPU acceleration opts := whisper.Options{ whisper.OptionUseGPU(true), // Enable GPU (CUDA) whisper.OptionGPUDeviceID(0), // Use first GPU whisper.OptionFlashAttn(true), // Enable flash attention } // Initialize the speech-to-text engine stt, err := whisper.New( ctx, modelBytes, speech.Language("en-US"), // Source language types.SamplingStrategyGreedy, // Sampling strategy true, // Enable translation to English types.AlignmentAheadsPresetNone, // Alignment heads preset 0.5, // VAD threshold (0 to disable) opts..., ) if err != nil { logger.Fatal(ctx, err) } defer stt.Close() // Get the output channel for transcription results ch, err := stt.OutputChan(ctx) if err != nil { logger.Fatal(ctx, err) } // Process transcription results in a goroutine go func() { for transcript := range ch { variant := transcript.Variants[0] if transcript.IsFinal { fmt.Printf("[FINAL] %s - %s: %s\n", variant.StartTime(), variant.EndTime(), variant.Text) } else { fmt.Printf("[PARTIAL] %s\n", variant.Text) } } }() // Write audio data (PCM Float32LE 16000Hz mono) buf := make([]byte, 1024*1024) for { n, err := os.Stdin.Read(buf) if n == 0 && err != nil { break } if err := stt.WriteAudio(ctx, buf[:n]); err != nil { logger.Fatal(ctx, err) } } } ``` -------------------------------- ### Bash: Use STT CLI Tool for Transcription Source: https://context7.com/xaionaro-go/speech/llms.txt Demonstrates how to use the `stt` command-line tool for speech-to-text transcription. It covers basic microphone input usage and advanced options such as specifying language, translation, GPU usage, VAD threshold, and printing timestamps/confidences. It also shows how to use a remote server for processing. ```bash # Basic usage: transcribe microphone input ./build/stt-linux-amd64 thirdparty/whisper.cpp/models/ggml-medium.bin # With options ./build/stt-linux-amd64 \ --language=en-US \ --translate=true \ --use-gpu=true \ --gpu=0 \ --vad-threshold=0.5 \ --print-timestamps=true \ --print-confidences=true \ --log-level=debug \ thirdparty/whisper.cpp/models/ggml-medium.bin # Use remote server instead of local processing ./build/stt-linux-amd64 \ --remote-addr=my-server:1234 \ --translate=true \ "" # Empty model path when using remote ``` -------------------------------- ### Go: Display Live Subtitles with GUI Window Source: https://context7.com/xaionaro-go/speech/llms.txt This Go program utilizes the `subtitleswindow` package to create a graphical user interface for displaying live subtitles. It captures audio from a microphone, processes it using a local Whisper model, and shows the transcribed text in a window, suitable for streaming applications like OBS. Dependencies include Fyne for the GUI and audio processing libraries. ```go package main import ( "context" "io" "os" "fyne.io/fyne/v2" "fyne.io/fyne/v2/app" "github.com/xaionaro-go/audio/pkg/audio" "github.com/xaionaro-go/speech/pkg/speech" "github.com/xaionaro-go/speech/pkg/speech/speechtotext/implementations/whisper/consts" "github.com/xaionaro-go/speech/pkg/subtitleswindow" ) func main() { ctx := context.Background() // Load Whisper model whisperModel, err := os.ReadFile("thirdparty/whisper.cpp/models/ggml-medium.bin") if err != nil { panic(err) } // Set up audio input from microphone audioEnc := consts.AudioEncoding() // PCM Float32LE 16000Hz audioChannels := consts.AudioChannels // Mono r, w := io.Pipe() recorder := audio.NewRecorderAuto(ctx) defer recorder.Close() stream, err := recorder.RecordPCM(ctx, audioEnc.SampleRate, audioChannels, audioEnc.PCMFormat, w) if err != nil { panic(err) } defer stream.Close() // Create the subtitles window fyneApp := app.New() window, err := subtitleswindow.New( ctx, fyneApp, "Live Subtitles", // Window title fyne.TextAlignCenter, // Text alignment: left, center, right r, // Audio input reader "", // Remote server address (empty for local) 0, // GPU device ID (-1 for CPU) whisperModel, // Whisper model bytes speech.Language("en-US"), // Source language false, // Translate to English nil, // Translate only from specific languages 0.99, // VAD threshold ) if err != nil { panic(err) } window.Show() fyneApp.Run() } ``` -------------------------------- ### Command-Line Execution for Subtitle Generation Source: https://context7.com/xaionaro-go/speech/llms.txt This snippet demonstrates how to execute the subtitleswindow-linux-amd64 binary with various command-line flags to control language, translation, text alignment, VAD threshold, GPU usage, log level, model path, and streaming endpoint. It's useful for setting up live subtitle generation for streams. ```bash ./build/subtitleswindow-linux-amd64 \ --language=ru-RU \ --translate=true \ --translate-only-from=ru-RU,uk-UA \ --text-align=center \ --vad-threshold=0.99 \ --gpu=0 \ --log-level=debug \ thirdparty/whisper.cpp/models/ggml-medium.bin \ rtmp://stream.example.com:1935/live/mystream ``` -------------------------------- ### gRPC Server for Remote Speech-to-Text Processing in Go Source: https://context7.com/xaionaro-go/speech/llms.txt Provides a gRPC server for handling speech-to-text requests. It can load a default model, configure Whisper options (like GPU usage), and manage concurrent contexts. The server listens on a specified TCP address. Dependencies include the go-belt library for logging and Whisper implementation packages. ```go package main import ( "context" "net" "os" "github.com/facebookincubator/go-belt/tool/logger" "github.com/xaionaro-go/speech/pkg/speech/speechtotext/implementations/whisper" "github.com/xaionaro-go/speech/pkg/speech/speechtotext/server" ) func main() { ctx := context.Background() // Load default model for clients that don't provide one defaultModel, err := os.ReadFile("thirdparty/whisper.cpp/models/ggml-large-v3.bin") if err != nil { logger.Fatal(ctx, err) } // Configure Whisper options opts := whisper.Options{ whisper.OptionUseGPU(true), whisper.OptionGPUDeviceID(0), } // Create server with: // - Default model for clients without their own // - Max 4 concurrent contexts // - Cache up to 2 initialized contexts for reuse srv := server.NewServer( defaultModel, 4, // contextsLimit 2, // cacheSize server.OptionWhisperOptions(opts), ) // Start listening listener, err := net.Listen("tcp", "0.0.0.0:1234") if err != nil { logger.Fatal(ctx, err) } logger.Infof(ctx, "Server started at %v", listener.Addr()) if err := srv.Serve(ctx, listener); err != nil { logger.Fatal(ctx, err) } } ``` -------------------------------- ### Remote gRPC Client for Speech-to-Text in Go Source: https://context7.com/xaionaro-go/speech/llms.txt Implements a gRPC client to connect to a remote speech-to-text server. It streams audio input and receives transcribed text. Dependencies include the go-belt library for logging and specific client/server packages from the project. It takes server address and context options as input and outputs transcribed text. ```go package main import ( "context" "fmt" "os" "github.com/facebookincubator/go-belt/tool/logger" "github.com/xaionaro-go/speech/pkg/speech/speechtotext/client" "github.com/xaionaro-go/speech/pkg/speech/speechtotext/implementations/whisper/types" "github.com/xaionaro-go/speech/pkg/speech/speechtotext/server/goconv" "github.com/xaionaro-go/speech/pkg/speech/speechtotext/server/proto/go/speechtotext_grpc" ) func main() { ctx := context.Background() // Connect to remote speech-to-text server stt, err := client.New(ctx, "my-server.example.com:1234", &speechtotext_grpc.NewContextRequest{ ModelBytes: nil, // Use server's default model Language: "en-US", ShouldTranslate: true, VadThreshold: 0.5, Backend: &speechtotext_grpc.NewContextRequest_Whisper{ Whisper: &speechtotext_grpc.WhisperOptions{ SamplingStrategy: goconv.SamplingStrategyToGRPC(types.SamplingStrategyGreedy), AlignmentAheadsPreset: speechtotext_grpc.WhisperAlignmentAheadsPreset_WhisperAlignmentAheadsPresetLargeV3, }, }, }) if err != nil { logger.Fatal(ctx, err) } defer stt.Close() // Get output channel for results ch, err := stt.OutputChan(ctx) if err != nil { logger.Fatal(ctx, err) } // Process results go func() { for t := range ch { fmt.Printf("%s\n", t.Variants[0].Text) } }() // Stream audio to remote server buf := make([]byte, 64*1024) for { n, err := os.Stdin.Read(buf) if n == 0 && err != nil { break } stt.WriteAudio(ctx, buf[:n]) } } ``` -------------------------------- ### gRPC Protocol Definition for Speech-to-Text Source: https://context7.com/xaionaro-go/speech/llms.txt This is the Protocol Buffers definition for the gRPC service used by the Speech library. It defines the messages and services for functionalities like pinging the server, managing audio contexts, writing audio data, and receiving output transcripts. This definition is crucial for developing custom clients or servers that interact with the Speech service. ```protobuf syntax = "proto3"; package speechtotext; service SpeechToText { rpc Ping(PingRequest) returns (PingReply) {} rpc NewContext(NewContextRequest) returns (stream NewContextReply) {} rpc WriteAudio(stream WriteAudioRequest) returns (WriteAudioReply) {} rpc OutputChan(OutputChanRequest) returns (stream OutputChanReply) {} } enum WhisperSamplingStrategy { WhisperSamplingStrategyUndefined = 0; WhisperSamplingStrategyGreedy = 1; WhisperSamplingStrategyBreamSearch = 2; } message WhisperOptions { WhisperSamplingStrategy samplingStrategy = 3; WhisperAlignmentAheadsPreset alignmentAheadsPreset = 5; } message NewContextRequest { bytes modelBytes = 1; string language = 2; bool shouldTranslate = 3; float vadThreshold = 4; oneof Backend { WhisperOptions whisper = 5; }; } message WriteAudioRequest { uint64 contextID = 1; bytes audio = 2; } message Transcript { repeated TranscriptVariant variants = 1; float stability = 2; uint32 audioChannelNum = 3; string language = 4; bool isFinal = 5; } message TranscriptVariant { string text = 1; repeated TranscriptToken transcriptTokens = 2; float confidence = 3; } message TranscriptToken { int64 startTimeNano = 1; int64 endTimeNano = 2; string text = 3; float confidence = 4; string Speaker = 5; } ``` -------------------------------- ### Define Speech-To-Text Core Interface in Go Source: https://context7.com/xaionaro-go/speech/llms.txt Defines the `ToText` interface for speech-to-text operations, including methods for audio input, encoding configuration, and receiving transcription results. It also defines the `Transcript` struct and related types for holding transcription data. ```go package speech import ( "context" "io" "time" "github.com/xaionaro-go/audio/pkg/audio" ) // ToText is the main interface for speech-to-text operations type ToText interface { io.Closer AudioEncoding(context.Context) (audio.Encoding, error) AudioChannels(context.Context) (audio.Channel, error) WriteAudio(context.Context, []byte) error OutputChan(context.Context) (<-chan *Transcript, error) } // Transcript represents the result of speech recognition type Transcript struct { Variants TranscriptVariants Stability float32 NoSpeechProbability float32 AudioChannelNum audio.Channel Language Language IsFinal bool } // TranscriptVariant contains a single transcription hypothesis type TranscriptVariant struct { Text Text TranscriptTokens TranscriptTokens Confidence float32 } // TranscriptToken represents a word or token with timing information type TranscriptToken struct { StartTime time.Duration EndTime time.Duration Text Text Confidence float32 Speaker string } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.