Rewrite the documentation and add runnable examples
- Bring the README up to date with editing, Apply, the model tags, validation codes, errors, limits, concurrency and development. - Fix the typed-model link and wording in docs/opcodes.md. - Add examples to the package documentation. - Describe the regeneration steps for the registry, and the licence status of the Splendid Grand Piano fixture. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
+140
@@ -0,0 +1,140 @@
|
||||
package sfz_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"strings"
|
||||
"testing/fstest"
|
||||
|
||||
"code.aneur.in/zampler/sfz"
|
||||
)
|
||||
|
||||
// This example reads an instrument and reports what is wrong with it.
|
||||
func ExampleDocument_Validate() {
|
||||
doc, err := sfz.Parse(strings.NewReader("<region> sample=a.wav lokey=c4 hikey=300 volume=loud\n"))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
for _, p := range doc.Validate() {
|
||||
fmt.Printf("%d:%d %s: %s\n", p.Line, p.Col, p.Code, p.Msg)
|
||||
}
|
||||
// Output:
|
||||
// 1:32 value.range: hikey=300 is outside 0 to 127
|
||||
// 1:42 value.type: volume=loud isn't a number
|
||||
}
|
||||
|
||||
// This example reads an instrument whose includes are in a folder, and its
|
||||
// typed regions. The paths of samples are the host's to resolve.
|
||||
func ExampleParseFile() {
|
||||
files := fstest.MapFS{
|
||||
"Piano/Piano.sfz": {Data: []byte("<control> default_path=Samples/\n#include \"regions.sfz\"\n")},
|
||||
"Piano/regions.sfz": {Data: []byte("<group> volume=-3\n<region> sample=C4.wav lokey=c4 hikey=d4\n")},
|
||||
}
|
||||
doc, err := sfz.ParseFile(files, "Piano/Piano.sfz")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
inst := doc.Flatten().Instrument()
|
||||
for _, r := range inst.Regions {
|
||||
fmt.Println(r.Sample, r.LowKey, r.HighKey, r.Volume)
|
||||
}
|
||||
// Output:
|
||||
// Samples/C4.wav 60 62 -3
|
||||
}
|
||||
|
||||
// This example changes a typed region and writes the change to the document.
|
||||
// The change goes in the region's own header, so the group's volume, the
|
||||
// comment and the opcode the library doesn't know are left as they are.
|
||||
func ExampleInstrument_Apply() {
|
||||
doc, err := sfz.Parse(strings.NewReader("<group> volume=-3\n<region> sample=a.wav lokey=c4 // middle C\n vendor_opcode=1\n"))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
inst := doc.Flatten().Instrument()
|
||||
|
||||
region := inst.Regions[0]
|
||||
region.Tune = 25
|
||||
region.Volume = -6
|
||||
if err := inst.Apply(0, region); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Print(doc)
|
||||
// Output:
|
||||
// <group> volume=-3
|
||||
// <region> sample=a.wav lokey=c4 // middle C
|
||||
// vendor_opcode=1
|
||||
// tune=25
|
||||
// volume=-6
|
||||
}
|
||||
|
||||
// This example changes an opcode of a header directly, with the text around it
|
||||
// as it was.
|
||||
func ExampleHeader_Set() {
|
||||
doc, err := sfz.Parse(strings.NewReader("<region>\n sample=a.wav // the sample\n lokey=60\n"))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
h := doc.Headers()[0]
|
||||
if err := h.Set("lokey", "c3"); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
if err := h.Set("hikey", "c5"); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
// A value that isn't one opcode's is refused.
|
||||
fmt.Println(h.Set("pan", "10\n<region>") != nil)
|
||||
fmt.Print(doc)
|
||||
// Output:
|
||||
// true
|
||||
// <region>
|
||||
// sample=a.wav // the sample
|
||||
// lokey=c3
|
||||
// hikey=c5
|
||||
}
|
||||
|
||||
// This example builds a new document from an instrument and writes it as files.
|
||||
func ExampleBuild() {
|
||||
doc, err := sfz.Parse(strings.NewReader("<region> sample=Samples/a.wav lokey=36 hikey=48\n<region> sample=Samples/b.wav lokey=49 hikey=60\n"))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
built, err := sfz.Build(doc.Flatten().Instrument(), sfz.BuildOptions{SplitFiles: true, RegionsPerFile: 1})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
err = built.WriteFiles("Out/Piano.sfz", func(path string, data []byte) error {
|
||||
fmt.Printf("== %s\n%s", path, data)
|
||||
return nil
|
||||
}, sfz.WriteOptions{})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
// Output:
|
||||
// == Out/Piano.sfz
|
||||
// #include "regions/001.sfz"
|
||||
// #include "regions/002.sfz"
|
||||
// == Out/regions/001.sfz
|
||||
// <region>
|
||||
// sample=Samples/a.wav
|
||||
// lokey=36
|
||||
// hikey=48
|
||||
// == Out/regions/002.sfz
|
||||
// <region>
|
||||
// sample=Samples/b.wav
|
||||
// lokey=49
|
||||
// hikey=60
|
||||
}
|
||||
|
||||
// This example looks up what the format says about opcodes, including the
|
||||
// numbered ones and the aliases.
|
||||
func ExampleLookup() {
|
||||
for _, name := range []string{"loopmode", "locc64", "eg1_time2_oncc3", "zampler_gain"} {
|
||||
info, ok := sfz.Lookup(name)
|
||||
fmt.Println(name, ok, info.Name, info.Type)
|
||||
}
|
||||
// Output:
|
||||
// loopmode true loop_mode string
|
||||
// locc64 true loccN integer
|
||||
// eg1_time2_oncc3 true egN_timeX_onccY float
|
||||
// zampler_gain false unspecified
|
||||
}
|
||||
Reference in New Issue
Block a user