dialogue

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2026 License: MIT Imports: 12 Imported by: 1 module, 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 dialogue loads and plays a conversation: a list of lines, each said by one speaker, each speaker with its own voice, with optional timing per line. A conversation is a *.dialogue.json file; Load reads one into a Script, LoadVoices resolves the voices it names, and a Player speaks it on a voice.Sink one line after the other.

The package uses the standard library and the voice package only.

Index

Constants

View Source
const (
	DefaultHold  = 1.0 // seconds a finished line stays before an auto advance
	MaxDelay     = 10  // seconds, the most a line's delay or hold can be
	MinLineSpeed = 0.25
	MaxLineSpeed = 4
)

The defaults and limits of the optional fields.

Variables

This section is empty.

Functions

func LoadVoices

func LoadVoices(fsys fs.FS, s *Script) (map[string]*voice.Voice, error)

LoadVoices resolves the voice of every speaker of s: a starter name through voice.Preset, a file name through voice.LoadVoiceFS relative to the dialogue's folder (Script.Dir) in fsys. The result is keyed by the speaker's voice reference (Speaker.Voice), so speakers that share a reference share the loaded voice; NewPlayer copies it per speaker.

A voice that cannot be loaded is a voice.FieldError at "speakers[i].voice"; every problem is returned together (errors.Join).

Types

type Advance

type Advance string

Advance says how a finished line gives way to the next one.

const (
	// AdvanceInput waits for [Player.Next] (a click or a key).
	AdvanceInput Advance = "input"
	// AdvanceAuto goes on by itself after the line's hold time.
	AdvanceAuto Advance = "auto"
)

type Event

type Event struct {
	Kind EventKind
	// Line is the index of the line (for DialogueFinished, the last one).
	Line int
	// At is the player's clock when it happened: the sum of the Update
	// times so far.
	At time.Duration
}

Event is something that happened in the dialogue.

type EventKind

type EventKind uint8

EventKind is what happened in a dialogue.

const (
	LineStarted      EventKind = iota // the line's delay passed and it began to speak
	LineDone                          // the line completed or was skipped; its text is complete
	DialogueFinished                  // the last line is done and gave way
)

func (EventKind) String

func (k EventKind) String() string

type Line

type Line struct {
	// Speaker is the ID of a speaker of the script.
	Speaker string
	Text    string
	// Delay is the time in seconds before the line starts, 0 to 10.
	Delay float32
	// Speed is [voice.SayOptions].Speed, 0.25 to 4.
	Speed   float32
	Instant bool
	// Advance and Hold: see [Script].
	Advance Advance
	Hold    float32
}

Line is one line of a dialogue.

type Options

type Options struct {
	// Languages resolves a voice's language name (voice.Voice.Language) to
	// a language. Nil, or a nil result, means the default language.
	Languages func(name string) *voice.Language
	// AutoAdvance goes on after every line by itself, whatever the script
	// says, using each line's hold time.
	AutoAdvance bool
	// Start is the index of the first line to play.
	Start int
}

Options tune a Player.

type Phase

type Phase uint8

Phase is where the player is in the current line.

const (
	// Waiting is the line's delay, before it starts.
	Waiting Phase = iota
	// Speaking is the line typing out and sounding.
	Speaking
	// Holding is the finished line, until Next or its hold time passes.
	Holding
	// Finished is after the last line, or after Stop.
	Finished
)

func (Phase) String

func (p Phase) String() string

type Player

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

Player speaks a Script on a voice.Sink: it runs each line's delay, the line itself and the hold, and tells the caller what to show.

Drive it from one goroutine: call Update with the frame time every frame, Next on a click or key press, show Line().Text[:Revealed()], and read Events. Every note it starts is ended: Next (skipping), Start, Pause, Stop and Close release the notes of the speaker involved. Steady-state Update allocates nothing; starting a line allocates.

func NewPlayer

func NewPlayer(sink voice.Sink, s *Script, voices map[string]*voice.Voice, opts Options) (*Player, error)

NewPlayer returns a player for s on sink. Speaker i is owner i + 1. voices maps each speaker's voice reference to its voice (see LoadVoices); the player works on copies, with the speaker's pan applied. It starts the line opts.Start at once. It returns an error for a missing voice or an invalid voice or start.

The sink needs the speakers' instruments before the first note: for each entry of Player.Voices call the sink's own SetVoice (synth.Stream has one) with the owner and voice.

func (*Player) Close

func (p *Player) Close()

Close stops like Stop and makes every later call do nothing. The player stays readable.

func (*Player) Events

func (p *Player) Events() []Event

Events returns what happened since the previous Update began: the events of Next and Start calls since then and those of the Update itself. The slice is reused: it is valid until the next Update.

func (*Player) Index

func (p *Player) Index() int

Index is the index of the current line (the last one that played, once finished).

func (*Player) Line

func (p *Player) Line() Line

Line is the current line.

func (*Player) Next

func (p *Player) Next()

Next is the player's input: a click or key press. While a line types out it shows the rest of it at once (the line is done); while a finished line holds it goes on to the next line (or finishes); during a delay and after the end it does nothing.

func (*Player) Pause

func (p *Player) Pause()

Pause holds time everywhere; the sounding notes are released, as with voice.Speaker.Pause. Resume continues.

func (*Player) Paused

func (p *Player) Paused() bool

Paused reports whether Pause was called and not Resume.

func (*Player) Phase

func (p *Player) Phase() Phase

Phase is where the player is in the current line.

func (*Player) Resume

func (p *Player) Resume()

Resume continues a paused dialogue.

func (*Player) Revealed

func (p *Player) Revealed() int

Revealed is how many bytes of Line().Text to show: none during the delay, the speaker's reveal while the line speaks, all of it afterwards.

func (*Player) Speaker

func (p *Player) Speaker() Speaker

Speaker is the speaker of the current line.

func (*Player) Start

func (p *Player) Start(i int)

Start jumps to line i, stopping the current one, and plays it from its delay. It also resumes a paused or finished dialogue. It panics for an index outside the script.

func (*Player) Stop

func (p *Player) Stop()

Stop ends the dialogue where it is: the notes are released, the phase is Finished, and no event is emitted. Start plays it again.

func (*Player) Update

func (p *Player) Update(dt time.Duration)

Update advances the dialogue by dt: it passes the delay, moves the current line and passes the hold of an auto-advancing one, going on to the next line when due. Time left over when a phase ends goes to the next phase, except that a line that completes inside dt uses all of it. Events from the previous Update are dropped; Events has those of this Update and of calls since the previous one. Allocates nothing in steady state.

func (*Player) Voices

func (p *Player) Voices() []SpeakerVoice

Voices lists the owner and voice of every speaker, in script order. The caller gives them to the sink before the first Update.

type Script

type Script struct {
	// Name is the dialogue's name (may be empty).
	Name string
	// Advance and Hold are the dialogue's defaults for its lines.
	Advance Advance
	Hold    float32
	// Speakers are in file order; speaker i plays as owner i + 1.
	Speakers []Speaker
	Lines    []Line
	// Dir is the folder of the file inside the file system it was loaded
	// from ("." for [Load]); [LoadVoices] resolves voice file names there.
	Dir string
}

Script is a loaded dialogue. Load fills in every default, so the values are what the player uses: a line's Advance and Hold are its own or else the script's, Speed is 1 unless the file says otherwise.

func Load

func Load(r io.Reader) (*Script, error)

Load reads a *.dialogue.json file. Malformed JSON and unknown fields are reported on their own; otherwise every validation error is returned together (errors.Join) as a voice.FieldError with File "dialogue" and its JSON path. Voices are not resolved, see LoadVoices.

func LoadFS

func LoadFS(fsys fs.FS, name string) (*Script, error)

LoadFS reads the *.dialogue.json file name from fsys, see Load. The script remembers name's folder for LoadVoices. Errors are prefixed with the file name.

func (*Script) SpeakerIndex

func (s *Script) SpeakerIndex(id string) int

SpeakerIndex returns the index of the speaker with this ID, or -1.

type Speaker

type Speaker struct {
	// ID is unique in the script and matches [a-z0-9_-]+.
	ID string
	// Name is what a dialogue box shows; it defaults to the ID.
	Name string
	// Voice is a starter voice name or a *.voice.json file name relative to
	// the dialogue file.
	Voice string
	// Pan overrides the voice's pan for this speaker, -1 to 1; nil keeps
	// the voice's own.
	Pan *float32
}

Speaker is one character of a dialogue.

type SpeakerVoice

type SpeakerVoice struct {
	Owner   voice.Owner
	Speaker string // the speaker's ID
	// Voice is a copy of the loaded voice with the speaker's pan applied.
	Voice *voice.Voice
}

SpeakerVoice is the voice a speaker plays with.

Jump to

Keyboard shortcuts

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