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
- func LoadVoices(fsys fs.FS, s *Script) (map[string]*voice.Voice, error)
- type Advance
- type Event
- type EventKind
- type Line
- type Options
- type Phase
- type Player
- func (p *Player) Close()
- func (p *Player) Events() []Event
- func (p *Player) Index() int
- func (p *Player) Line() Line
- func (p *Player) Next()
- func (p *Player) Pause()
- func (p *Player) Paused() bool
- func (p *Player) Phase() Phase
- func (p *Player) Resume()
- func (p *Player) Revealed() int
- func (p *Player) Speaker() Speaker
- func (p *Player) Start(i int)
- func (p *Player) Stop()
- func (p *Player) Update(dt time.Duration)
- func (p *Player) Voices() []SpeakerVoice
- type Script
- type Speaker
- type SpeakerVoice
Constants ¶
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 ¶
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 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 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 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 ¶
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 ¶
Index is the index of the current line (the last one that played, once finished).
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) Revealed ¶
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) Start ¶
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 ¶
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 ¶
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 ¶
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 ¶
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.