fix(vm/compiler)!: don't define SU_BUFFER_LENGTH when speed is used

BREAKING CHANGE: If song uses a speed unit, SU_LENGTH_IN_SAMPLES,
SU_BUFFER_LENGTH and SU_SYNCBUFFER_LENGTH won't be defined in the
generated header file, as we cannot know their exact values during
compile time without rendering the entire song. When using a speed
unit, the user has to take responsibility of allocating large enough
buffers.

Closes #241
This commit is contained in:
2026-08-15 13:49:40 +03:00
parent f3a8377c56
commit 06b1c86c3a
5 changed files with 43 additions and 9 deletions
+10 -7
View File
@@ -75,9 +75,9 @@ func (com *Compiler) Library() (map[string]string, error) {
return retmap, nil
}
func (com *Compiler) Song(song *sointu.Song) (map[string]string, error) {
func (com *Compiler) Song(song *sointu.Song) (retmap map[string]string, warnings []string, err error) {
if com.Arch != "386" && com.Arch != "amd64" && com.Arch != "wasm" {
return nil, fmt.Errorf(`compiling a song player is supported only on 386, amd64 and wasm architectures (targeted architecture was %v)`, com.Arch)
return nil, nil, fmt.Errorf(`compiling a song player is supported only on 386, amd64 and wasm architectures (targeted architecture was %v)`, com.Arch)
}
var templates []string
if com.Arch == "386" || com.Arch == "amd64" {
@@ -86,14 +86,17 @@ func (com *Compiler) Song(song *sointu.Song) (map[string]string, error) {
templates = []string{"player.wat"}
}
features := vm.NecessaryFeaturesFor(song.Patch)
retmap := map[string]string{}
if _, ok := features.Opcode("speed"); ok {
warnings = append(warnings, fmt.Sprintf(`song uses the speed unit, so SU_LENGTH_IN_SAMPLES, SU_BUFFER_LENGTH, and SU_SYNCBUFFER_LENGTH cannot be known without rendering the entire song. They won't be defined in the generated header file. You have to take responsibility for allocating large enough audio buffer and syncBuf.`))
}
retmap = map[string]string{}
encodedPatch, err := vm.NewBytecode(song.Patch, features, song.BPM)
if err != nil {
return nil, fmt.Errorf(`could not encode patch: %v`, err)
return nil, nil, fmt.Errorf(`could not encode patch: %v`, err)
}
patterns, sequences, err := ConstructPatterns(song)
if err != nil {
return nil, fmt.Errorf(`could not encode song: %v`, err)
return nil, nil, fmt.Errorf(`could not encode song: %v`, err)
}
for _, templateName := range templates {
compilerMacros := *NewCompilerMacros(*com)
@@ -133,11 +136,11 @@ func (com *Compiler) Song(song *sointu.Song) (map[string]string, error) {
populatedTemplate, extension, err = com.compile(templateName, &data)
}
if err != nil {
return nil, fmt.Errorf(`could not execute template "%v": %v`, templateName, err)
return nil, nil, fmt.Errorf(`could not execute template "%v": %v`, templateName, err)
}
retmap[extension] = populatedTemplate
}
return retmap, nil
return retmap, warnings, nil
}
func (com *Compiler) compile(templateName string, data interface{}) (string, string, error) {
+13 -1
View File
@@ -3,15 +3,22 @@
#define SU_RENDER_H
#define SU_CHANNEL_COUNT 2
{{- if .HasOp "speed" -}}
// warning: song uses speed unit, so SU_LENGTH_IN_SAMPLES, SU_BUFFER_LENGTH, and
// SU_SYNCBUFFER_LENGTH cannot be known without rendering the entire song. They
// are not defined in this generated header file. You have to take
// responsibility for allocating large enough audio buffer and syncBuf.
{{- else}}
#define SU_LENGTH_IN_SAMPLES {{.MaxSamples}}
#define SU_BUFFER_LENGTH (SU_LENGTH_IN_SAMPLES*SU_CHANNEL_COUNT)
{{- end}}
#define SU_SAMPLE_RATE 44100
#define SU_BPM {{.Song.BPM}}
#define SU_ROWS_PER_BEAT {{.Song.RowsPerBeat}}
#define SU_ROWS_PER_PATTERN {{.Song.Score.RowsPerPattern}}
#define SU_LENGTH_IN_PATTERNS {{.Song.Score.Length}}
#define SU_LENGTH_IN_ROWS (SU_LENGTH_IN_PATTERNS*SU_PATTERN_SIZE)
#define SU_LENGTH_IN_ROWS (SU_LENGTH_IN_PATTERNS*SU_ROWS_PER_PATTERN)
#define SU_SAMPLES_PER_ROW (SU_SAMPLE_RATE*60/(SU_BPM*SU_ROWS_PER_BEAT))
{{- if or .RowSync (.HasOp "sync")}}
@@ -20,8 +27,13 @@
{{- else}}
#define SU_NUMSYNCS {{.Song.Patch.NumSyncs}}
{{- end}}
{{- if .HasOp "speed" -}}
// Patch uses the speed unit, cannot safely define SU_SYNCBUFFER_LENGTH here.
// Normally it would be round_up(SU_LENGTH_IN_SAMPLES/256)*SU_NUMSYNCS.
{{- else}}
#define SU_SYNCBUFFER_LENGTH ((SU_LENGTH_IN_SAMPLES+255)>>8)*SU_NUMSYNCS
{{- end}}
{{- end}}
#include <stdint.h>
#if UINTPTR_MAX == 0xffffffff