# Voice-over


When a video is narrated, the narration sets the pace: a scene lasts as long as its sentences, and things appear on the words that name them. `fx` reads those times from one file, a `timeline.json` with the start and end of every scene, line and word, and gives you three short lookups (`W`, `WE`, `SC`) to build the pictures on. Re-record the voice, replace the file, and the whole video moves with it.

> **Note:** The timeline is read from disk when the project loads. The browser build reads no files, so this page has no live demos.

## The file

`effects` does not make this file, it reads it. Whatever aligns your recording with its script (a forced aligner, a speech-to-text model with word timestamps, a script of your own) writes it in this shape:

```json
{
	"duration": 7.9,
	"scenes": [
		{"id": "intro", "start": 0, "end": 3.4},
		{"id": "dash", "start": 3.4, "end": 7.9}
	],
	"lines": [
		{
			"id": "intro-1",
			"scene": "intro",
			"text": "All your contacts.",
			"start": 0.42,
			"end": 1.71,
			"words": [
				{"w": "All", "s": 0.42, "e": 0.66},
				{"w": "your", "s": 0.66, "e": 0.88},
				{"w": "contacts.", "s": 0.88, "e": 1.71}
			]
		},
		{
			"id": "dash-1",
			"scene": "dash",
			"text": "One dashboard shows every number.",
			"start": 3.71,
			"end": 6.02,
			"words": [
				{"w": "One", "s": 3.71, "e": 3.95},
				{"w": "dashboard", "s": 3.95, "e": 4.58},
				{"w": "shows", "s": 4.58, "e": 4.9},
				{"w": "every", "s": 4.9, "e": 5.26},
				{"w": "number.", "s": 5.26, "e": 6.02}
			]
		}
	]
}
```

All times are seconds of video time. The file maps directly onto four structs:

```odin
Vo :: struct {
	lines:    []Vo_Line,
	scenes:   []Vo_Scene,
	duration: f32,
}

Vo_Scene :: struct {
	id:         string,
	start, end: f32,
}

Vo_Line :: struct {
	id, scene, text: string,
	start, end:      f32,
	words:           []Vo_Word,
}

Vo_Word :: struct {
	w:    string,
	s, e: f32,
}
```

| Field | Meaning |
| --- | --- |
| `Vo.duration` | Length of the voice-over in seconds. A video without a `duration` of its own lasts at least this long. |
| `Vo.scenes` | The sections of the narration: an `id` and the span each one covers. Scenes of the video can take their timing from these. |
| `Vo.lines` | The spoken lines. `id` is how the code refers to a line; `scene` names the section it belongs to; `text` is the whole line; `start` and `end` are when it is spoken. |
| `Vo_Line.words` | The line word by word: `w` is the word as it should be shown, `s` and `e` when it starts and ends. |

Keys the structs do not have are ignored, so a tool can keep its own notes in the file.

## Loading it

```odin
@(export)
effects_video :: proc() -> ^fx.Video {
	return fx.video({
		name = "Spot",
		audio = "audio/mix.wav", // the recording itself
		vo = "timeline.json",    // its timings
		scenes = SCENES[:],
	})
}
```

`Video.vo` is the path of the file, relative to the project directory. `fx.video` reads it and keeps the result in `fx.vo`, which the lookups below search. If the file is missing or malformed, a message is printed and the video loads without a voice-over; scenes that wait for one then keep the `start` and `dur` they were written with, and without a `dur` are not drawn.

The timeline holds times only. The voice itself is a sound like any other: `Video.audio` when it is already mixed with the music, or a [`Sound`](/docs/sound/) of its own when you mix in code and want the music to [step back under it](/docs/sound/#automation).

In the editor a video with a voice-over gets a "voice" lane showing every word at its time, and scenes, cues and sounds snap to word starts when dragged. Saving `timeline.json` reloads the project like saving a source file does.

## Scenes that follow the voice

```odin
SCENES := [?]fx.Scene {
	{name = "intro", vo = "intro", draw = intro},
	{name = "dash", vo = "dash", draw = dash},
	{name = "outro", start = 7.9, dur = 2, draw = outro},
}
```

A scene with `vo` set takes its `start` from the voice-over scene of that id, and its length too unless it has a `dur` of its own. Give it a `dur` when the picture should hold longer than the words, and overlap is fine: scenes may overlap, and are drawn in list order. Scenes without `vo` are timed as written, and both kinds can be mixed.

If the video has no `duration`, it ends where the last scene ends or where the voice-over does, whichever is later.

```odin
SC :: proc(id: string) -> Vo_Scene
```

`SC` returns a voice-over scene's span for anything else that should follow it: `fx.SC("dash").start` is a time like any other.

## Cutting to words

```odin
W  :: proc(id: string, i: int = 0) -> f32
WE :: proc(id: string, i: int = -1) -> f32
```

`W` is the time word `i` of line `id` starts, `WE` the time word `i` ends. Words count from 0; negative numbers count from the end, so `-1` is the last word. The defaults give the two times you want most: `fx.W("dash-1")` is when the line begins, `fx.WE("dash-1")` when it has been spoken. An index past either end means the first or last word.

They return video time, so they go wherever a time goes:

```odin
dash :: proc(s: fx.Shot) {
	fx.backdrop(.Night)
	// The window slides in as the line begins …
	in_t := fx.W("dash-1")
	if fx.group({o = fx.ep(s.t, in_t, in_t + 0.5), y = 40 * (1 - fx.ep(s.t, in_t, in_t + 0.5))}) {
		dashboard()
	}
	// … and the figure is boxed from the word "number." (word 4) until shortly after the line.
	fx.highlight([]fx.Hl_Key{{t = fx.W("dash-1", 4), r = KPI_RECT}, {t = fx.WE("dash-1") + 0.8, off = true}})
}
```

Write the indices against the words in the file. When the voice is recorded again with the same script, the indices still fit and every time moves to where the word now is.

A line id that does not exist prints `fx: no voice-over line <id>` and gives 0, so a typo shows up in the terminal and as something happening at the very start of the video.

## The line as text

```odin
vo_text :: proc(id: string, x, y: f32, f: Font, c: Color, accents: []int = nil, breaks: []int = nil, max_w: f32 = 0) -> Rect
```

`vo_text` sets a spoken line as a headline whose words rise in as they are said: each word of line `id` comes up as it starts (rising 26 px out of a blur over 0.38 s, beginning 0.06 s ahead of the word). The text is the words' `w`, drawn from `(x, y)` in font `f` and color `c`.

| Argument | Meaning |
| --- | --- |
| `accents` | Indices of words to draw in the theme's `red` instead of `c`. |
| `breaks` | Indices of words after which the line breaks. |
| `max_w` | A width in px to wrap at, 0 for none. Use it or `breaks`, or both. |

It returns the rectangle the whole block will occupy, also before all words are in, so other things can be placed under it.

```odin
intro :: proc(s: fx.Shot) {
	fx.backdrop(.Night)
	if fx.group({o = fx.fade(s)}) {
		// "All your" / "contacts.", the last word in the theme's red
		r := fx.vo_text("intro-1", 160, 380, fx.headline(140), fx.theme.nt, accents = []int{2}, breaks = []int{1})
		fx.eyebrow("CRM", {160, r.y - 60}, fx.theme.nm)
	}
}
```

It is [`fx.kinetic`](/docs/ui/) with the words and times taken from the line; call `kinetic` yourself with a `[]fx.Word` when you need a different accent color or rise time.

## The data itself

```odin
vo: Vo

vo_line :: proc(id: string) -> ^Vo_Line
load_vo :: proc(path: string, allocator := context.allocator) -> (v: Vo, ok: bool)
```

`fx.vo` is the loaded timeline, for everything the lookups do not cover. Subtitles are a loop over it:

```odin
subtitles :: proc(t: f32) {
	for l in fx.vo.lines {
		if t < l.start || t >= l.end + 0.3 do continue
		fx.label(l.text, {960, 980}, fx.sans(40, 500), fx.theme.nt, {0.5, 0.5})
	}
}
```

Call it from `Video.overlay` and it sits above every scene.

`vo_line` returns one line by id, or `nil` (with a message) if there is none. `load_vo` is the loader `fx.video` uses: it reads and parses a timeline file from a full path and reports whether that worked. In the browser build it returns nothing.

## Reference

| Name | What it is |
| --- | --- |
| `Video.vo` | Path of the `timeline.json`, relative to the project. |
| `Scene.vo` | Id of the voice-over scene this scene takes its start (and, without a `dur`, its length) from. |
| `Vo`, `Vo_Scene`, `Vo_Line`, `Vo_Word` | The timeline: scenes, lines and words with their times. |
| `vo` | The loaded timeline. |
| `W(id, i)` | Start of word `i` of a line; default the first word. |
| `WE(id, i)` | End of word `i` of a line; default the last word. |
| `SC(id)` | A voice-over scene's `start` and `end`. |
| `vo_line(id)` | A line by id, or `nil`. |
| `vo_text(id, x, y, f, c, accents, breaks, max_w)` | The line as text whose words rise in when spoken. |
| `load_vo(path, allocator)` | Reads a timeline file. |
