Add lossless SFZ document, flattening, typed model and opcode registry
Parse keeps a file exactly as written, with #define and #include unexpanded and each include as its own file in a tree. Flatten resolves directives and header inheritance, and Flat.Instrument reads regions into the typed model ported from Zampler's internal/sfz. The opcode registry is generated from the SFZ format's syntax data and drives Lookup, GetAs and Validate. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
+179
@@ -0,0 +1,179 @@
|
||||
// Package sfz reads, validates and writes SFZ instruments: text files that
|
||||
// map samples to keys, velocities and playback settings. It handles SFZ v1 and
|
||||
// v2 with the ARIA extensions as data. It doesn't play or load samples.
|
||||
//
|
||||
// A [Document] keeps a file exactly as written, including comments, spacing,
|
||||
// unknown opcodes, and #define and #include directives, so that writing an
|
||||
// unchanged document gives the same text back. [Document.Flatten] resolves
|
||||
// directives and header inheritance into regions, and [Flat.Instrument] reads
|
||||
// those into a typed model.
|
||||
package sfz
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Errors that Parse and Flatten wrap.
|
||||
var (
|
||||
// ErrInvalid is returned for text that can't be read as an instrument.
|
||||
ErrInvalid = errors.New("invalid SFZ")
|
||||
// ErrIncludeLoop is returned when a file includes itself, directly or not.
|
||||
ErrIncludeLoop = errors.New("include loop")
|
||||
// ErrUnresolved is returned, with [IncludesFail], for an include that
|
||||
// can't be read.
|
||||
ErrUnresolved = errors.New("unresolved include")
|
||||
)
|
||||
|
||||
// NodeKind is what a [Node] holds.
|
||||
type NodeKind int
|
||||
|
||||
// The kinds of node.
|
||||
const (
|
||||
// KindSpace is a run of whitespace.
|
||||
KindSpace NodeKind = iota
|
||||
// KindComment is a // or /* */ comment.
|
||||
KindComment
|
||||
// KindHeader is a header such as <region>.
|
||||
KindHeader
|
||||
// KindOpcode is an opcode and its value, such as lokey=60.
|
||||
KindOpcode
|
||||
// KindDefine is a #define directive.
|
||||
KindDefine
|
||||
// KindInclude is an #include directive.
|
||||
KindInclude
|
||||
// KindText is text that isn't any of the above, such as a directive that
|
||||
// can't be read.
|
||||
KindText
|
||||
)
|
||||
|
||||
// String returns the kind's name.
|
||||
func (k NodeKind) String() string {
|
||||
switch k {
|
||||
case KindSpace:
|
||||
return "space"
|
||||
case KindComment:
|
||||
return "comment"
|
||||
case KindHeader:
|
||||
return "header"
|
||||
case KindOpcode:
|
||||
return "opcode"
|
||||
case KindDefine:
|
||||
return "define"
|
||||
case KindInclude:
|
||||
return "include"
|
||||
}
|
||||
return "text"
|
||||
}
|
||||
|
||||
// Node is one piece of a file. The raw text of a file's nodes, in order, is
|
||||
// the file's text.
|
||||
type Node struct {
|
||||
Kind NodeKind
|
||||
// Raw is the node's text as written.
|
||||
Raw string
|
||||
// Name is a header's name without its brackets, as written, an opcode's
|
||||
// name, or a define's variable name with its $.
|
||||
Name string
|
||||
// Value is an opcode's or a define's value without surrounding
|
||||
// whitespace, or an include's path as written.
|
||||
Value string
|
||||
// Line and Col are where the node starts, from 1, with Col in bytes.
|
||||
Line, Col int
|
||||
// Resolved is an include's path with its variables substituted, with
|
||||
// forward slashes, relative to the folder of the main file.
|
||||
Resolved string
|
||||
// File is the file an include reads, or nil if it wasn't read.
|
||||
File *File
|
||||
}
|
||||
|
||||
// File is one file of a document: the main file or an included one.
|
||||
type File struct {
|
||||
// Path is the file's path relative to the folder of the main file, with
|
||||
// forward slashes. For a file read with [Parse] and no name, it is empty.
|
||||
Path string
|
||||
Nodes []*Node
|
||||
}
|
||||
|
||||
// String returns the file's text.
|
||||
func (f *File) String() string {
|
||||
var b strings.Builder
|
||||
for _, n := range f.Nodes {
|
||||
b.WriteString(n.Raw)
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
// Document is an instrument as written: a main file and the files it
|
||||
// includes.
|
||||
type Document struct {
|
||||
// Root is the main file.
|
||||
Root *File
|
||||
// problems are those found while parsing.
|
||||
problems []Problem
|
||||
}
|
||||
|
||||
// WriteTo writes the main file's text to w. An included file is a separate
|
||||
// [File] with its own text, which stays in the document as an #include.
|
||||
func (d *Document) WriteTo(w io.Writer) (int64, error) {
|
||||
n, err := io.WriteString(w, d.Root.String())
|
||||
return int64(n), err
|
||||
}
|
||||
|
||||
// String returns the main file's text.
|
||||
func (d *Document) String() string { return d.Root.String() }
|
||||
|
||||
// Files returns the main file and the files it includes, in the order they are
|
||||
// read, each time they are included.
|
||||
func (d *Document) Files() []*File {
|
||||
var out []*File
|
||||
var walk func(f *File)
|
||||
walk = func(f *File) {
|
||||
out = append(out, f)
|
||||
for _, n := range f.Nodes {
|
||||
if n.Kind == KindInclude && n.File != nil {
|
||||
walk(n.File)
|
||||
}
|
||||
}
|
||||
}
|
||||
walk(d.Root)
|
||||
return out
|
||||
}
|
||||
|
||||
// Severity is how serious a [Problem] is.
|
||||
type Severity int
|
||||
|
||||
// The severities.
|
||||
const (
|
||||
// SeverityInfo is something unusual that is allowed.
|
||||
SeverityInfo Severity = iota
|
||||
// SeverityWarning is something that is probably a mistake.
|
||||
SeverityWarning
|
||||
// SeverityError is something that makes the instrument unreadable.
|
||||
SeverityError
|
||||
)
|
||||
|
||||
// String returns the severity's name.
|
||||
func (s Severity) String() string {
|
||||
switch s {
|
||||
case SeverityInfo:
|
||||
return "info"
|
||||
case SeverityWarning:
|
||||
return "warning"
|
||||
}
|
||||
return "error"
|
||||
}
|
||||
|
||||
// Problem is something found in a document.
|
||||
type Problem struct {
|
||||
// File is the path of the file, as in [File.Path].
|
||||
File string
|
||||
// Line and Col are where the problem is, from 1.
|
||||
Line, Col int
|
||||
Severity Severity
|
||||
// Code is a short stable name for the kind of problem, such as
|
||||
// "include.unresolved".
|
||||
Code string
|
||||
Msg string
|
||||
}
|
||||
Reference in New Issue
Block a user