soniox

package
v0.7.1 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2026 License: Apache-2.0 Imports: 18 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package soniox serves OpenAI-style speech-to-text over Soniox's async API: one multipart request becomes upload, create, poll, fetch and delete, and Soniox tokens are grouped into words and speaker segments.

Index

Constants

View Source
const (
	DefaultBaseURL      = "https://api.soniox.com"
	DefaultPollInterval = time.Second
	DefaultDeadline     = 30 * time.Minute
	DefaultPause        = time.Second
	DefaultMaxSegment   = 30 * time.Second
)

Defaults and bounds of Config.

View Source
const (
	StatusQueued     = "queued"
	StatusProcessing = "processing"
	StatusCompleted  = "completed"
	StatusError      = "error"
)

Transcription statuses.

View Source
const DefaultMaxAge = time.Hour

DefaultMaxAge is how old a remora-tagged leftover must be before the sweeper deletes it.

View Source
const RefPrefix = "remora:"

RefPrefix starts the client_reference_id of every file and transcription remora creates; the sweeper only touches objects that carry it.

Variables

This section is empty.

Functions

This section is empty.

Types

type APIError

type APIError struct {
	Op      string // the operation, such as "create transcription"
	Status  int
	Type    string // Soniox error_type
	Message string // Soniox message, redacted
}

APIError is a non-2xx answer from Soniox. Error() never contains the key.

func (*APIError) Error

func (e *APIError) Error() string

Error returns "soniox <op>: <status> <type>: <message>".

type Adapter

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

Adapter serves POST /v1/audio/transcriptions (OpenAI multipart) over the Soniox async API. The Soniox key is the request's bearer token, so the adapter holds no key of its own.

func New

func New(cfg Config) *Adapter

New returns an Adapter with cfg's zero values replaced by the defaults.

func (*Adapter) ServeHTTP

func (a *Adapter) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP runs one transcription: upload, create, poll, fetch, and always delete what it created.

type Client

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

Client calls the Soniox async API. The key is passed per call, so one Client serves every account.

func NewClient

func NewClient(baseURL string, hc *http.Client) *Client

NewClient returns a Client for baseURL (such as "https://api.soniox.com"). A nil hc means http.DefaultClient.

func (*Client) CreateTranscription

func (c *Client) CreateTranscription(ctx context.Context, key string, cr CreateRequest) (Transcription, error)

CreateTranscription starts a transcription.

func (*Client) DeleteFile

func (c *Client) DeleteFile(ctx context.Context, key, id string) error

DeleteFile deletes a file; a missing file counts as deleted.

func (*Client) DeleteTranscription

func (c *Client) DeleteTranscription(ctx context.Context, key, id string) error

DeleteTranscription deletes a transcription; a missing one counts as deleted.

func (*Client) GetTranscript

func (c *Client) GetTranscript(ctx context.Context, key, id string) (Transcript, error)

GetTranscript fetches the transcript of a completed transcription.

func (*Client) GetTranscription

func (c *Client) GetTranscription(ctx context.Context, key, id string) (Transcription, error)

GetTranscription reads a transcription's status.

func (*Client) ListFiles

func (c *Client) ListFiles(ctx context.Context, key string) ([]File, error)

ListFiles lists every file of the key's account, following next_page_cursor to the end.

func (*Client) ListTranscriptions

func (c *Client) ListTranscriptions(ctx context.Context, key string) ([]Transcription, error)

ListTranscriptions lists every transcription of the key's account, following next_page_cursor to the end.

func (*Client) UploadFile

func (c *Client) UploadFile(ctx context.Context, key, ref, filename string, body io.Reader) (File, error)

UploadFile streams body to POST /v1/files as the multipart field "file", after the field client_reference_id = ref.

type Config

type Config struct {
	BaseURL      string        // default "https://api.soniox.com"
	PollInterval time.Duration // default 1s; clamped to [500ms, 60s]
	Deadline     time.Duration // default 30m; whole job from upload to transcript
	Grouping     Grouping      // defaults 1s / 30s
	HTTPClient   *http.Client  // default &http.Client{} (no overall timeout; contexts bound every call)
	Logger       *slog.Logger
}

Config configures the Adapter. Zero values take the defaults.

type CreateRequest

type CreateRequest struct {
	Model                        string          `json:"model"`
	FileID                       string          `json:"file_id"`
	ClientReferenceID            string          `json:"client_reference_id"`
	LanguageHints                []string        `json:"language_hints,omitempty"`
	LanguageHintsStrict          *bool           `json:"language_hints_strict,omitempty"`
	EnableSpeakerDiarization     *bool           `json:"enable_speaker_diarization,omitempty"`
	EnableLanguageIdentification *bool           `json:"enable_language_identification,omitempty"`
	Context                      json.RawMessage `json:"context,omitempty"`     // a JSON string or object
	Translation                  json.RawMessage `json:"translation,omitempty"` // a JSON object
}

CreateRequest is the body of POST /v1/transcriptions.

type File

type File struct {
	ID                string    `json:"id"`
	Filename          string    `json:"filename"`
	ClientReferenceID string    `json:"client_reference_id"`
	CreatedAt         time.Time `json:"created_at"`
}

File is an uploaded Soniox file.

type Grouping

type Grouping struct {
	Pause      time.Duration // default 1s
	MaxSegment time.Duration // default 30s
}

Grouping bounds segments: a pause of at least Pause or a segment longer than MaxSegment starts a new one, but only at a word boundary.

type KeySource

type KeySource func(ctx context.Context) ([]string, error)

KeySource returns the Soniox keys to sweep, such as the enabled Soniox provider keys in the store.

type ParamError

type ParamError struct{ Field, Reason string }

ParamError is a client mistake; the handler answers 400 with Error() as the message.

func (*ParamError) Error

func (e *ParamError) Error() string

Error returns `unsupported parameter "<field>"` for an unknown field, else `invalid parameter "<field>": <reason>`.

type Segment

type Segment struct {
	ID      int     `json:"id"`
	Speaker string  `json:"speaker,omitempty"`
	Start   float64 `json:"start"`
	End     float64 `json:"end"`
	Text    string  `json:"text"`
}

Segment is one speaker segment of the OpenAI verbose_json answer.

type SweepResult

type SweepResult struct{ Transcriptions, Files int }

SweepResult counts what one sweep deleted.

type Sweeper

type Sweeper struct {
	Client *Client
	Keys   KeySource
	MaxAge time.Duration    // 1h, from the spec
	Now    func() time.Time // default time.Now
	Logger *slog.Logger
}

Sweeper deletes Soniox files and transcriptions that remora created (their client_reference_id starts with RefPrefix) and that outlived MaxAge, such as leftovers of a crash. Objects without the prefix are never touched. An owned job still queued or processing past MaxAge loses its file, which makes it fail; a later sweep then deletes the failed transcription.

func (*Sweeper) Run

func (s *Sweeper) Run(ctx context.Context, every time.Duration)

Run sweeps at once, then every `every`, until ctx is done.

func (*Sweeper) SweepOnce

func (s *Sweeper) SweepOnce(ctx context.Context) (SweepResult, error)

SweepOnce sweeps every key. A key that cannot be listed is logged and its error joined into the result; the other keys still run. A single object that fails to delete is logged and skipped.

type Token

type Token struct {
	Text              string  `json:"text"`
	StartMs           int64   `json:"start_ms"`
	EndMs             int64   `json:"end_ms"`
	Confidence        float64 `json:"confidence"`
	Speaker           string  `json:"speaker"`
	Language          string  `json:"language"`
	TranslationStatus string  `json:"translation_status"`
}

Token is one (often sub-word) transcript token.

type Transcript

type Transcript struct {
	ID     string  `json:"id"`
	Text   string  `json:"text"`
	Tokens []Token `json:"tokens"`
}

Transcript is the result of a completed transcription.

type Transcription

type Transcription struct {
	ID                string    `json:"id"`
	Status            string    `json:"status"` // queued|processing|completed|error
	FileID            string    `json:"file_id"`
	ClientReferenceID string    `json:"client_reference_id"`
	ErrorType         string    `json:"error_type"`
	ErrorMessage      string    `json:"error_message"`
	AudioDurationMs   int64     `json:"audio_duration_ms"`
	CreatedAt         time.Time `json:"created_at"`
}

Transcription is a Soniox async transcription job.

type Usage

type Usage struct {
	Type    string  `json:"type"` // "duration"
	Seconds float64 `json:"seconds"`
}

Usage is the billed audio duration.

type VerboseJSON

type VerboseJSON struct {
	Task     string    `json:"task"` // "transcribe"
	Language string    `json:"language,omitempty"`
	Duration float64   `json:"duration"`
	Text     string    `json:"text"`
	Words    []Word    `json:"words"`
	Segments []Segment `json:"segments"`
	Usage    Usage     `json:"usage"`
}

VerboseJSON is the OpenAI verbose_json transcription answer.

type Word

type Word struct {
	Word    string  `json:"word"`
	Start   float64 `json:"start"`
	End     float64 `json:"end"`
	Speaker string  `json:"speaker,omitempty"`
}

Word is one word of the OpenAI verbose_json answer.

Jump to

Keyboard shortcuts

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