Documentation / Programming / SPSQ
SPSQ package
Versioned API documentation and examples, mirrored from pkg.go.dev. Open the canonical package page
package spsq
Package spsq provides a programmatic builder for SynapSeq .spsq sequence text.
Overview
This package is a Go API for constructing SynapSeq sequences dynamically. It uses a fluent Builder that records options and timeline entries, plus Preset builders that record tracks and effects, then renders them as .spsq text.
Load validates the generated text through the core package and returns the loaded sequence context. The spsq package does not dump, stream, or render audio itself; those responsibilities remain in core and the internal sequence and audio packages.
Example Usage
package main
import (
"log"
"time"
synapseq "github.com/synapseq-foundation/synapseq/v4/core"
"github.com/synapseq-foundation/synapseq/v4/spsq"
)
func main() {
ctx := synapseq.NewAppContext()
builder, err := spsq.New(ctx)
if err != nil {
log.Fatal(err)
}
builder.SampleRate(44100).Volume(100)
alpha := builder.NewPreset("alpha")
alpha.Pink(0).Amplitude(30)
alpha.Tone(300).Binaural(10).Amplitude(15)
loaded, err := builder.
SilenceAt(0).
PresetAt(15*time.Second, alpha).
SilenceAt(time.Minute).
Load()
if err != nil {
log.Fatal(err)
}
if err := loaded.WAV("output.wav"); err != nil {
log.Fatal(err)
}
}
Verbose Output
New receives a core AppContext. Configure that context with WithVerbose when you want progress output from later operations such as WAV, MP3, or Stream:
ctx := synapseq.NewAppContext().WithVerbose(os.Stderr, true)
builder, err := spsq.New(ctx)
if err != nil {
log.Fatal(err)
}
alpha := builder.NewPreset("alpha")
alpha.Pink(0).Amplitude(30)
loaded, err := builder.
SilenceAt(0).
PresetAt(15*time.Second, alpha).
SilenceAt(time.Minute).
Load()
if err != nil {
log.Fatal(err)
}
if err := loaded.WAV("output.wav"); err != nil {
log.Fatal(err)
}
Builder Flow
Typical builder usage follows the .spsq document shape:
- add sequence options such as sample rate, volume, ambiance, or music;
- create presets and add tracks with track modifiers;
- add timeline entries that select presets or silence at specific times;
- call Load to validate and load the generated .spsq content.
Noise tracks are added with White, Pink, or Brown. Each method receives the noise smoothness percentage and returns the preset so modifiers such as Amplitude can be chained:
preset.White(0).Amplitude(20)
preset.Pink(10).Amplitude(20)
preset.Brown(15).Amplitude(20)
preset.Pink(10).Amplitude(20, 12)
Amplitude accepts one percentage for matching channels or explicit left/right percentages with `.Amplitude(left, right)`.
Builder methods return the same Builder so calls can be chained. Methods that modify the last track or timeline entry are no-ops when there is no matching target. New requires a non-nil core AppContext. Load returns a core LoadedContext or parser and validation errors produced by core.
More Information
For the loading, rendering, streaming, and dump API, see the core package: https://pkg.go.dev/github.com/synapseq-foundation/synapseq/v4/core@v4.43.1
For complete project documentation and examples, see: https://github.com/synapseq-foundation/synapseq
Types
type Builder struct {
// contains filtered or unexported fields
}
Builder is responsible for building a sequence from a string sequence content
func New(ctx *synapseq.AppContext) (*Builder, error)
New creates a new Builder using ctx to load generated content.
Example
ctx := synapseq.NewAppContext()
builder, err := spsq.New(ctx)
if err != nil {
panic(err)
}
builder.SampleRate(44100).Volume(100)
alpha := builder.NewPreset("alpha")
alpha.Pink(0).Amplitude(30)
alpha.Tone(300).Binaural(10).Amplitude(15)
loaded, err := builder.
SilenceAt(0).
PresetAt(15*time.Second, alpha).
SilenceAt(time.Minute).
Load()
if err != nil {
panic(err)
}
_ = loaded
fmt.Println("SPSQ content built and loaded successfully")
Output:
SPSQ content built and loaded successfully
func (b *Builder) Ambiance(name, path string) *Builder
Ambiance registers an ambiance source for the sequence.
func (b *Builder) EaseIn() *Builder
EaseIn sets the transition of the last timeline entry to ease-in.
func (b *Builder) EaseOut() *Builder
EaseOut sets the transition of the last timeline entry to ease-out.
func (b *Builder) Load() (*synapseq.LoadedContext, error)
Load renders and loads the generated .spsq content.
Example (verbose)
ctx := synapseq.NewAppContext().WithVerbose(io.Discard, false)
builder, err := spsq.New(ctx)
if err != nil {
panic(err)
}
alpha := builder.NewPreset("alpha")
alpha.Pink(0).Amplitude(30)
loaded, err := builder.
SilenceAt(0).
PresetAt(15*time.Second, alpha).
SilenceAt(time.Minute).
Load()
if err != nil {
panic(err)
}
_ = loaded
fmt.Println("SPSQ content built with verbose context")
Output:
SPSQ content built with verbose context
func (b *Builder) Music(name, path string) *Builder
Music registers a music source for the sequence.
func (b *Builder) NewPreset(name string) *Preset
NewPreset creates a new preset with the given name.
func (b *Builder) PresetAt(at time.Duration, preset *Preset) *Builder
PresetAt adds a preset timeline entry at the given time.
func (b *Builder) SampleRate(sampleRate int) *Builder
SampleRate sets the sample rate for the sequence.
func (b *Builder) SilenceAt(at time.Duration) *Builder
SilenceAt adds a silence timeline entry at the given time.
func (b *Builder) Smooth() *Builder
Smooth sets the transition of the last timeline entry to smooth.
func (b *Builder) Steady() *Builder
Steady sets the transition of the last timeline entry to steady.
func (b *Builder) Step(s int) *Builder
Step sets the step count of the last timeline entry.
func (b *Builder) Transition(name string, points ...float64) *Builder
Transition registers a custom timeline transition for the sequence.
func (b *Builder) UseTransition(name string) *Builder
UseTransition sets the transition of the last timeline entry by custom name.
func (b *Builder) Volume(volume int) *Builder
Volume sets the volume for the sequence.
func (b *Builder) Waveform(name string, points ...float64) *Builder
Waveform registers a custom waveform for the sequence.
type Preset struct {
// contains filtered or unexported fields
}
Preset builds the tracks for one named .spsq preset.
func (p *Preset) Ambiance(name string) *Preset
Ambiance adds an ambiance track.
func (p *Preset) Amplitude(left float64, right ...float64) *Preset
Amplitude sets the left and optional right amplitude of the last track.
func (p *Preset) Binaural(beat float64) *Preset
Binaural converts the last tone track to a binaural beat.
func (p *Preset) Brown(smooth float64) *Preset
Brown adds a brown noise track.
func (p *Preset) Doppler(value float64) *Preset
Doppler adds a doppler effect to the last track.
func (p *Preset) Intensity(value float64) *Preset
Intensity sets the effect intensity on the last track.
func (p *Preset) Isochronic(beat float64) *Preset
Isochronic converts the last tone track to an isochronic beat.
func (p *Preset) LeftRight() *Preset
LeftRight places the higher binaural frequency on the left (the default). It leaves the preset unchanged unless its last track is binaural.
func (p *Preset) Modulation(value float64) *Preset
Modulation adds a modulation effect to the last track.
func (p *Preset) Monaural(beat float64) *Preset
Monaural converts the last tone track to a monaural beat.
func (p *Preset) Music(name string) *Preset
Music adds a music track.
func (p *Preset) Pan(value float64) *Preset
Pan adds a pan effect to the last track.
func (p *Preset) Pink(smooth float64) *Preset
Pink adds a pink noise track.
func (p *Preset) RightLeft() *Preset
RightLeft places the higher binaural frequency on the right. It leaves the preset unchanged unless its last track is binaural.
Example
builder, err := spsq.New(synapseq.NewAppContext())
if err != nil {
panic(err)
}
focus := builder.NewPreset("focus")
focus.Tone(220).Binaural(10).RightLeft().Amplitude(10)
loaded, err := builder.PresetAt(0, focus).PresetAt(time.Minute, focus).Load()
if err != nil {
panic(err)
}
for _, line := range strings.Split(string(loaded.RawContent()), "\n") {
if strings.Contains(line, "binaural") {
fmt.Println(strings.TrimSpace(line))
}
}
Output:
waveform sine tone 220.00 binaural right-left 10.00 amplitude left 10.00 right 10.00
func (p *Preset) Sawtooth() *Preset
Sawtooth sets the waveform of the last track to sawtooth.
func (p *Preset) Shift(value float64) *Preset
Shift adds a symmetric stereo frequency shift effect to the last track.
func (p *Preset) Short() *Preset
Short selects half-width pulses at 75 percent envelope gain. Missing or incompatible tracks are left unchanged.
Example
builder, err := spsq.New(synapseq.NewAppContext())
if err != nil {
panic(err)
}
focus := builder.NewPreset("focus")
focus.Tone(220).Isochronic(10).Short().Amplitude(15)
loaded, err := builder.PresetAt(0, focus).PresetAt(time.Minute, focus).Load()
if err != nil {
panic(err)
}
for _, line := range strings.Split(string(loaded.RawContent()), "\n") {
if strings.Contains(line, "isochronic") {
fmt.Println(strings.TrimSpace(line))
}
}
Output:
waveform sine tone 220.00 isochronic short 10.00 amplitude left 15.00 right 15.00
func (p *Preset) Sine() *Preset
Sine sets the waveform of the last track to sine.
func (p *Preset) Square() *Preset
Square sets the waveform of the last track to square.
func (p *Preset) Standard() *Preset
Standard selects the original envelope on the last isochronic track. Missing or incompatible tracks are left unchanged.
func (p *Preset) Tone(carrier float64) *Preset
Tone adds a pure tone track.
func (p *Preset) Triangle() *Preset
Triangle sets the waveform of the last track to triangle.
func (p *Preset) Waveform(name string) *Preset
Waveform sets the waveform of the last track by name.
func (p *Preset) White(smooth float64) *Preset
White adds a white noise track.
func (p *Preset) Wide() *Preset
Wide selects a fixed envelope over 75 percent of each cycle at full gain. Missing or incompatible tracks are left unchanged.
Example
builder, err := spsq.New(synapseq.NewAppContext())
if err != nil {
panic(err)
}
focus := builder.NewPreset("focus")
focus.Tone(220).Isochronic(10).Wide().Amplitude(15)
loaded, err := builder.PresetAt(0, focus).PresetAt(time.Minute, focus).Load()
if err != nil {
panic(err)
}
for _, line := range strings.Split(string(loaded.RawContent()), "\n") {
if strings.Contains(line, "isochronic") {
fmt.Println(strings.TrimSpace(line))
}
}
Output:
waveform sine tone 220.00 isochronic wide 10.00 amplitude left 15.00 right 15.00