feat: add the ability to use Sointu as a sync-tracker

There is a new "sync" opcode that saves the top-most signal every 256 samples to the new "syncBuffer" output. Additionally, you can enable saving the current fractional row as sync[0], avoiding calculating the beat in the shader, but also calculating the beat correctly when the beat is modulated.
This commit is contained in:
vsariola
2021-03-09 23:47:27 +02:00
parent a3bdf565fd
commit 99dbdfe223
30 changed files with 375 additions and 88 deletions

View File

@ -70,17 +70,18 @@ func Synth(patch sointu.Patch) (*C.Synth, error) {
// time > maxtime, as it is modulated and the time could advance by 2 or more, so the loop
// exit condition would fire when the time is already past maxtime.
// Under no conditions, nsamples >= len(buffer)/2 i.e. guaranteed to never overwrite the buffer.
func (synth *C.Synth) Render(buffer []float32, maxtime int) (int, int, error) {
func (synth *C.Synth) Render(buffer []float32, syncBuffer []float32, maxtime int) (int, int, int, error) {
// TODO: syncBuffer is not getting passed to cgo; do we want to even try to support the syncing with the native bridge
if len(buffer)%1 == 1 {
return -1, -1, errors.New("RenderTime writes stereo signals, so buffer should have even length")
return -1, -1, -1, errors.New("RenderTime writes stereo signals, so buffer should have even length")
}
samples := C.int(len(buffer) / 2)
time := C.int(maxtime)
errcode := int(C.su_render(synth, (*C.float)(&buffer[0]), &samples, &time))
if errcode > 0 {
return int(samples), int(time), &RenderError{errcode: errcode}
return int(samples), 0, int(time), &RenderError{errcode: errcode}
}
return int(samples), int(time), nil
return int(samples), 0, int(time), nil
}
// Trigger is part of C.Synths' implementation of sointu.Synth interface

View File

@ -44,7 +44,7 @@ func TestOscillatSine(t *testing.T) {
if err != nil {
t.Fatalf("Compiling patch failed: %v", err)
}
buffer, err := sointu.Play(synth, song)
buffer, _, err := sointu.Play(synth, song)
if err != nil {
t.Fatalf("Render failed: %v", err)
}
@ -103,7 +103,7 @@ func TestAllRegressionTests(t *testing.T) {
if err != nil {
t.Fatalf("Compiling patch failed: %v", err)
}
buffer, err := sointu.Play(synth, song)
buffer, _, err := sointu.Play(synth, song)
buffer = buffer[:song.Score.LengthInRows()*song.SamplesPerRow()*2] // extend to the nominal length always.
if err != nil {
t.Fatalf("Play failed: %v", err)

View File

@ -18,10 +18,11 @@ type Compiler struct {
OS string
Arch string
Output16Bit bool
RowSync bool
}
// New returns a new compiler using the default .asm templates
func New(os string, arch string, output16Bit bool) (*Compiler, error) {
func New(os string, arch string, output16Bit bool, rowsync bool) (*Compiler, error) {
_, myname, _, _ := runtime.Caller(0)
var subdir string
if arch == "386" || arch == "amd64" {
@ -32,17 +33,17 @@ func New(os string, arch string, output16Bit bool) (*Compiler, error) {
return nil, fmt.Errorf("compiler.New failed, because only amd64, 386 and wasm archs are supported (targeted architecture was %v)", arch)
}
templateDir := filepath.Join(path.Dir(myname), "..", "..", "templates", subdir)
compiler, err := NewFromTemplates(os, arch, output16Bit, templateDir)
compiler, err := NewFromTemplates(os, arch, output16Bit, rowsync, templateDir)
return compiler, err
}
func NewFromTemplates(os string, arch string, output16Bit bool, templateDirectory string) (*Compiler, error) {
func NewFromTemplates(os string, arch string, output16Bit bool, rowsync bool, templateDirectory string) (*Compiler, error) {
globPtrn := filepath.Join(templateDirectory, "*.*")
tmpl, err := template.New("base").Funcs(sprig.TxtFuncMap()).ParseGlob(globPtrn)
if err != nil {
return nil, fmt.Errorf(`could not create template based on directory "%v": %v`, templateDirectory, err)
}
return &Compiler{Template: tmpl, OS: os, Arch: arch, Output16Bit: output16Bit}, nil
return &Compiler{Template: tmpl, OS: os, Arch: arch, RowSync: rowsync, Output16Bit: output16Bit}, nil
}
func (com *Compiler) Library() (map[string]string, error) {

View File

@ -354,15 +354,25 @@ func (p *X86Macros) FmtStack() string {
}
func (p *X86Macros) ExportFunc(name string, params ...string) string {
if !p.Amd64 {
reverseParams := make([]string, len(params))
for i, param := range params {
reverseParams[len(params)-1-i] = param
}
p.Stacklocs = append(reverseParams, "retaddr_"+name) // in 32-bit, we use stdcall and parameters are in the stack
if p.OS == "windows" {
return fmt.Sprintf("%[1]v\nglobal _%[2]v@%[3]v\n_%[2]v@%[3]v:", p.SectText(name), name, len(params)*4)
}
numRegisters := 0 // in 32-bit systems, we use stdcall: everything in stack
switch {
case p.Amd64 && p.OS == "windows":
numRegisters = 4 // 64-bit windows has 4 parameters in registers, rest in stack
case p.Amd64:
numRegisters = 6 // System V ABI has 6 parameters in registers, rest in stack
}
if len(params) > numRegisters {
params = params[numRegisters:]
} else {
params = nil
}
reverseParams := make([]string, len(params))
for i, param := range params {
reverseParams[len(params)-1-i] = param
}
p.Stacklocs = append(reverseParams, "retaddr_"+name) // in 32-bit, we use stdcall and parameters are in the stack
if !p.Amd64 && p.OS == "windows" {
return fmt.Sprintf("%[1]v\nglobal _%[2]v@%[3]v\n_%[2]v@%[3]v:", p.SectText(name), name, len(params)*4)
}
if p.OS == "darwin" {
return fmt.Sprintf("%[1]v\nglobal _%[2]v\n_%[2]v:", p.SectText(name), name)