Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
18.20.8

26
57 changes: 56 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,62 @@ else.

Feel free, too, to simply use it as a model on which to build your own.

### Stylesheets

Audimance provides default stylesheets for the spatial mixing room and the track list pages. It also provides parts of the Pure CSS framework and a collection of mobile styles. These can be overridden by show-specific templates as needed. For example:

```html
<!-- Audimance's default room styling. -->
<link rel="stylesheet" href="/app/css/room.css">

<!-- Show-specific room styling. -->
<link rel="stylesheet" href="/css/room.css">
```

### Localizing an Audimance Performance

The Audimance server supports shows in multiple languages. Supported languages are defined in `agenda.yaml` as BCP 47 tags. For example:

```yaml
languages: [en, zh-TW]
```

The first language listed is the default, and is used whenever a user's preferred language doesn't match any of the languages supported by the show.

The user's language is resolved in the following order:

1. The `lang` query parameter
2. The browser's `Accept-Language` header
3. The default language.

Localized views are stored in directories for each language code (e.g. `views/zh-TW/index.html`). If a page doesn't have a translated version, the server will fall back to using the default template in `views/`.

All source and point of interest labels are paramterized by language code. The
`name` property serves as both an identifier and the fall-back option when there is no label for the user's current language. For example:

```yaml
sources:
- name: "Choreography"
labels:
zh-TW: "編舞"
```

HTML templates can refer to a label using `{{.Label}}`. For example:

```html
{{ range .Room.Sources }}
<label for="input-{{.ID}}">{{.Label}}</label>
{{ end }}
```

The label in this template renders as `編舞` for on a device set to `zh-TW`, and as `Choreography` for any other language.

The spatial room's play button can be localized in each language-specific template. For example:

```html
<div id="audimance-room" data-play-label="點按播放"></div>
```

### Dependencies

Because Audimance is intended to be run at venues with unreliable internet, all
Expand All @@ -139,4 +195,3 @@ Audimance accepts cues on a UDP port (9001, by default) to be fed from external
synchronization sources, such as from Figure53's
[QLab](https://figure53.com/qlab/). It expects the cue label to be received in
case-sensitive plain text on the UDP port to trigger the cue.

132 changes: 129 additions & 3 deletions agenda/agenda.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,40 @@
package agenda

import (
"bytes"
"crypto/md5"
"encoding/hex"
"errors"
"fmt"
"io"
"os"
"slices"
"strings"
"time"

"github.com/gofrs/uuid"
"golang.org/x/text/language"
yaml "gopkg.in/yaml.v3"
)

var fileFormats = []string{"mp3", "m4a", "webm"}

// New attempts to load an agenda from the given filename
func New(filename string) (*Agenda, error) {
// Load reads an agenda from a YAML file.
func Load(filename string) (*Agenda, error) {
data, err := os.ReadFile(filename)
if err != nil {
return nil, fmt.Errorf("failed to read agenda from file: %w", err)
}

return New(bytes.NewReader(data))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you use os.Open() instead of os.ReadFile(), you get an io.Reader back (a file handle), which can be passed directly to New(). Just make sure you include a defer f.Close() on the returned file handle, so it gets closed after reading. That is handled automatically by os.ReadFile(), but you have to do it yourself for os.Open().

}

// New reads an agenda from a YAML stream reader.
func New(src io.Reader) (*Agenda, error) {
a := new(Agenda)

if err := yaml.Unmarshal(data, a); err != nil {
err := yaml.NewDecoder(src).Decode(a)
if err != nil {
return nil, fmt.Errorf("failed to read YAML: %w", err)
}

Expand Down Expand Up @@ -58,6 +68,13 @@ func New(filename string) (*Agenda, error) {
return nil, fmt.Errorf("failed to generate announcement %s: %w", ann.Name, err)
}
}

if err = a.validateLanguages(); err != nil {
return nil, err
}

a.localizeLabels(a.defaultLanguage())

return a, err
}

Expand Down Expand Up @@ -97,6 +114,87 @@ type Agenda struct {
// server, and so not validation should be performed, and no modifications
// of the prefix be made. This is not recommended.
RemoteMedia bool `json:"remoteMedia" yaml:"remoteMedia"`

// BCP 47 tags. First entry is the default language.
Languages []string `json:"languages" yaml:"languages"`
}

func (a *Agenda) defaultLanguage() string {
if len(a.Languages) == 0 {
return ""
}

return a.Languages[0]
}

// Localized returns a copy of the agenda with each source and point of interest labeled in the specified language.
func (a *Agenda) Localized(lang string) *Agenda {
out := *a

out.Rooms = make([]*Room, len(a.Rooms))
for i, r := range a.Rooms {
room := *r

room.PointsOfInterest = make([]*PointOfInterest, len(r.PointsOfInterest))
for j, p := range r.PointsOfInterest {
poi := *p
room.PointsOfInterest[j] = &poi
}

room.Sources = make([]*Source, len(r.Sources))
for j, s := range r.Sources {
src := *s
room.Sources[j] = &src
}

out.Rooms[i] = &room
}

out.localizeLabels(lang)

return &out
}

func (a *Agenda) localizeLabels(lang string) {
for _, p := range a.allPointsOfInterest() {
p.Label = p.labelFor(lang)
}
}

func (a *Agenda) allPointsOfInterest() (out []*PointOfInterest) {
for _, r := range a.Rooms {
out = append(out, r.PointsOfInterest...)

for _, s := range r.Sources {
out = append(out, &s.PointOfInterest)
}
}

return out
}

func (a *Agenda) validateLanguages() error {
if err := validateLanguageTags(a.Languages); err != nil {
return err
}

for _, p := range a.allPointsOfInterest() {
if err := p.validateLabels(a.Languages); err != nil {
return err
}
}

return nil
}

func validateLanguageTags(languages []string) error {
for _, lang := range languages {
if _, err := language.Parse(lang); err != nil {
return fmt.Errorf("invalid language %q: %w", lang, err)
}
}

return nil
}

// AllTracks returns the list of all tracks for all rooms and announcements so
Expand Down Expand Up @@ -342,11 +440,39 @@ type PointOfInterest struct {
// Name is the unique, human-friendly name for this source
Name string `json:"name" yaml:"name"`

// Labels contains display text in each supported language, keyed by language code.
Labels map[string]string `json:"-" yaml:"labels"`

// Label is the display text in the language the agenda was localized to, or the name if there is no label.
Label string `json:"label" yaml:"-"`

// Location indicates a specific 3-dimensional coordinate in the room from
// which the audio of this source emanates
Location Point `json:"location" yaml:"location"`
}

func (p *PointOfInterest) labelFor(lang string) string {
if label := p.Labels[lang]; label != "" {
return label
}

return p.Name
}

// ErrUnlistedLanguage is returned when an agenda contains a label in a language it doesn't support.
var ErrUnlistedLanguage = errors.New("label is in a language that is not specified in the agenda's list of supported languages")

// validateLabels checks that all POI labels are in one of the specified languages.
func (p *PointOfInterest) validateLabels(languages []string) error {
for lang := range p.Labels {
if !slices.Contains(languages, lang) {
return fmt.Errorf("%w: %q is labeled in %q, but the agenda lists %v", ErrUnlistedLanguage, p.Name, lang, languages)
}
}

return nil
}

func (p *PointOfInterest) generateID() error {
// If we don't have a name, generate one
if p.Name == "" {
Expand Down
44 changes: 44 additions & 0 deletions agenda/agenda_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package agenda_test

import (
"testing"

"github.com/CyCoreSystems/audimance/agenda"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestLabelsFallBackToNames(t *testing.T) {
a, err := agenda.Load("testdata/no-languages.yaml")
require.NoError(t, err)

room := a.Rooms[0]
assert.Equal(t, "Downstage center", room.PointsOfInterest[0].Label)
assert.Equal(t, "Choreography", room.Sources[0].Label)
}

func TestLocalizedLabels(t *testing.T) {
a, err := agenda.Load("testdata/bilingual.yaml")
require.NoError(t, err)

zh := a.Localized("zh-TW")
source, original := zh.Rooms[0].Sources[0], a.Rooms[0].Sources[0]

assert.Equal(t, "編舞", source.Label)
assert.Equal(t, "Downstage center", zh.Rooms[0].PointsOfInterest[0].Label, "untranslated labels fall back to the name")
assert.Equal(t, original.ID, source.ID)
assert.Equal(t, original.Name, source.Name)
assert.Equal(t, "Choreography", original.Label, "localizing changed the original")
}

func TestLabelsDefaultToFirstLanguage(t *testing.T) {
a, err := agenda.Load("testdata/chinese-first.yaml")
require.NoError(t, err)

assert.Equal(t, "編舞", a.Rooms[0].Sources[0].Label)
}

func TestRejectsLabelInUnlistedLanguage(t *testing.T) {
_, err := agenda.Load("testdata/label-in-unlisted-language.yaml")
assert.ErrorIs(t, err, agenda.ErrUnlistedLanguage)
}
9 changes: 9 additions & 0 deletions agenda/testdata/bilingual.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
languages: [en, zh-TW]
rooms:
- name: stage
pointsOfInterest:
- name: Downstage center
sources:
- name: Choreography
labels:
zh-TW: 編舞
9 changes: 9 additions & 0 deletions agenda/testdata/chinese-first.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
languages: [zh-TW, en]
rooms:
- name: stage
pointsOfInterest:
- name: Downstage center
sources:
- name: Choreography
labels:
zh-TW: 編舞
9 changes: 9 additions & 0 deletions agenda/testdata/label-in-unlisted-language.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
languages: [en, zh-TW]
rooms:
- name: stage
pointsOfInterest:
- name: Downstage center
sources:
- name: Choreography
labels:
zh-Hant: 編舞
6 changes: 6 additions & 0 deletions agenda/testdata/no-languages.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
rooms:
- name: stage
pointsOfInterest:
- name: Downstage center
sources:
- name: Choreography
16 changes: 9 additions & 7 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

buildInputs = with pkgs; [
gci
git
gofumpt
golangci-lint
pkgLint
Expand All @@ -35,12 +36,13 @@
];

pkgLint = pkgs.writeScriptBin "lint" ''
${pkgs.nodejs_26}/bin/npm run build &&
golangci-lint run ./
'';
${pkgs.nodejs_26}/bin/npm run build &&
golangci-lint run ./
'';
in
{
devShells.default = pkgs.mkShell {
# On macOS, mkShell breaks /usr/bin/git.
devShells.default = (if pkgs.stdenv.hostPlatform.isDarwin then pkgs.mkShellNoCC else pkgs.mkShell) {
inherit
buildInputs
nativeBuildInputs
Expand All @@ -61,7 +63,7 @@
;

vendorHash = "sha256-Up6pjR9C2Z5wX6yLnEytdqjGCqKMCbLUvEzTZIMN8sU=";
npmDepsHash = "sha256-6dDY4qR40LYpcVA8lkhSc3McupBpkYAOK8eDS35/Fvo=";
npmDepsHash = "sha256-u2MkAOFI0qVjii6z2Mbv4R7P3HbYfh0sYECuhSeK3rw=";
npmDepsFetcherVersion = 2;
};

Expand All @@ -76,10 +78,10 @@

preBuild = ''
mkdir app
cp ${self.packages.${system}.app}/lib/node_modules/audimance-app/app/* app/
cp -r ${self.packages.${system}.app}/lib/node_modules/audimance-app/app/* app/
'';

vendorHash = "sha256-O03GmLuBuWegNMQJ3PMsojgFR/6bPRER4bjvxHxdUBw=";
vendorHash = "sha256-lpmE99NjL0WaheoLfs2KCdhSuUF1yz/w9/Y5Ev9h7+0=";
};

};
Expand Down
Loading
Loading