This commit is contained in:
5684185+vsariola@users.noreply.github.com
2025-10-26 19:08:54 +02:00
parent f92ecb2e99
commit a872bd3340
8 changed files with 353 additions and 33 deletions

View File

@ -13,18 +13,19 @@ import (
)
type Compiler struct {
Template *template.Template
OS string
Arch string
Output16Bit bool
RowSync bool
Template *template.Template
OS string
Arch string
Output16Bit bool
RowSync bool
ForceSingleThread bool
}
//go:embed templates/amd64-386/* templates/wasm/*
var templateFS embed.FS
// New returns a new compiler using the default .asm templates
func New(os string, arch string, output16Bit bool, rowsync bool) (*Compiler, error) {
func New(os string, arch string, output16Bit, rowsync, forceSingleThread bool) (*Compiler, error) {
var subdir string
if arch == "386" || arch == "amd64" {
subdir = "amd64-386"
@ -37,16 +38,16 @@ func New(os string, arch string, output16Bit bool, rowsync bool) (*Compiler, err
if err != nil {
return nil, fmt.Errorf(`could not create templates: %v`, err)
}
return &Compiler{Template: tmpl, OS: os, Arch: arch, RowSync: rowsync, Output16Bit: output16Bit}, nil
return &Compiler{Template: tmpl, OS: os, Arch: arch, RowSync: rowsync, Output16Bit: output16Bit, ForceSingleThread: forceSingleThread}, nil
}
func NewFromTemplates(os string, arch string, output16Bit bool, rowsync bool, templateDirectory string) (*Compiler, error) {
func NewFromTemplates(os string, arch string, output16Bit, rowsync, forceSingleThread 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, RowSync: rowsync, Output16Bit: output16Bit}, nil
return &Compiler{Template: tmpl, OS: os, Arch: arch, RowSync: rowsync, Output16Bit: output16Bit, ForceSingleThread: forceSingleThread}, nil
}
func (com *Compiler) Library() (map[string]string, error) {
@ -75,13 +76,23 @@ 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) (map[string]string, 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)
}
if com.ForceSingleThread {
song = song.Copy()
for i := range song.Patch {
song.Patch[i].ThreadMaskM1 = 0 // clear all ThreadMaskM1 to indicate that all instruments are on Thread 1 i.e. force single threaded rendering
}
}
var templates []string
if com.Arch == "386" || com.Arch == "amd64" {
templates = []string{"player.asm", "player.h", "player.inc"}
if song.Patch.NumThreads() > 1 {
templates = []string{"multithread_player.asm", "player.h", "player.inc"}
} else {
templates = []string{"player.asm", "player.h", "player.inc"}
}
} else if com.Arch == "wasm" {
templates = []string{"player.wat"}
}
@ -91,14 +102,14 @@ func (com *Compiler) Song(song *sointu.Song) (map[string]string, error) {
if err != nil {
return nil, fmt.Errorf(`could not encode patch: %v`, err)
}
patterns, sequences, err := ConstructPatterns(song)
patterns, sequences, err := ConstructPatterns(&song)
if err != nil {
return nil, fmt.Errorf(`could not encode song: %v`, err)
}
for _, templateName := range templates {
compilerMacros := *NewCompilerMacros(*com)
featureSetMacros := FeatureSetMacros{features}
songMacros := *NewSongMacros(song)
songMacros := *NewSongMacros(&song)
var populatedTemplate, extension string
var err error
if com.Arch == "386" || com.Arch == "amd64" {

View File

@ -0,0 +1,286 @@
{{template "structs.asm" .}}
;-------------------------------------------------------------------------------
; Uninitialized data: The synth object
;-------------------------------------------------------------------------------
{{- range $index := .Song.Patch.NumThreads}}
{{.SectBss (print "synth_object" $index)}}
su_synth_obj{{$index}}:
resb su_synthworkspace.size
resb {{.Song.Patch.NumDelayLines}}*su_delayline_wrk.size
{{- end}}
{{- if or .RowSync (.HasOp "sync")}}
{{- if or (and (eq .OS "windows") (not .Amd64)) (eq .OS "darwin")}}
extern _syncBuf
{{- else}}
extern syncBuf
{{- end}}
{{- end}}
;-------------------------------------------------------------------------------
; su_render_songX function(s): the different entry points
;-------------------------------------------------------------------------------
; Has the signature su_render_song(void *ptr), where ptr is a pointer to
; the output buffer. Renders the compile time hard-coded song to the buffer.
; Stack: output_ptr
;-------------------------------------------------------------------------------
{{- range $index := .Song.Patch.NumThreads}}
{{- if gt $index 0 }}
{{- $entrypoint := print "su_render_song" {{add1 $index}} }}
{{- else}}
{{- $entrypoint := print "su_render_song" }}
{{- end}}
{{.ExportFunc $entrypoint "OutputBufPtr"}}
{{- if .Amd64}}
{{- if eq .OS "windows"}}
{{- .PushRegs "rcx" "OutputBufPtr" "rdi" "NonVolatileRsi" "rsi" "NonVolatile" "rbx" "NonVolatileRbx" "rbp" "NonVolatileRbp" | indent 4}} ; rcx = ptr to buf. rdi,rsi,rbx,rbp nonvolatile
{{- else}} ; SystemV amd64 ABI, linux mac or hopefully something similar
{{- .PushRegs "rdi" "OutputBufPtr" "rbx" "NonVolatileRbx" "rbp" "NonVolatileRbp" | indent 4}}
{{- end}}
{{- else}}
{{- .PushRegs | indent 4}}
{{- end}}
{{- $prologsize := len .Stacklocs}}
{{print "su_synth_obj" $index | .Prepare}}
{{.Push (print "su_synth_obj" $index | .Use) "SyncBufPtr"}}
{{print "su_synth_obj" $index | .Prepare}}
{{.Push (print "su_synth_obj" $index | .Use) "SyncBufPtr"}}
{{print "su_synth_obj" $index | .Prepare}}
{{.Push (print "su_synth_obj" $index | .Use) "SyncBufPtr"}}
{{print "su_tracks" $index | .Prepare}}
{{.Push (.print "Tracks+" (.TrackOffset $index) | .Use) "Tracks"}}
{{.Push (.NumVoices $index) "NumVoices"}}
{{.Push (.PolyPhonyBitMask $index) "PolyPhonyBitMask"}}
{{.Push (.VoiceTrackBitmask $index) "VoiceTrackBitmask"}}
{{- if or .RowSync (.HasOp "sync")}}
{{- if or (and (eq .OS "windows") (not .Amd64)) (eq .OS "darwin")}}
{{- print "_syncBuf+" (.SyncOffset $index) | .Prepare}}
{{.Push (print "_syncBuf+" (.SyncOffset $index) | .Use) "SyncBufPtr"}}
{{- else}}
{{- print "syncBuf+" (.SyncOffset $index) | .Prepare}}
{{.Push (print "syncBuf+" (.SyncOffset $index) | .Use) "SyncBufPtr"}}
{{- end}}
{{- end}}
{{.Call "actual_render_song"}}
; rewind the stack the entropy of multiple pop {{.AX}} is probably lower than add
{{- range slice .Stacklocs $prologsize}}
{{$.Pop $.AX}}
{{- end}}
{{- if .Amd64}}
{{- if eq .OS "windows"}}
; Windows64 ABI, rdi rsi rbx rbp non-volatile
{{- .PopRegs "rcx" "rdi" "rsi" "rbx" "rbp" | indent 4}}
{{- else}}
; SystemV64 ABI (linux mac or hopefully something similar), rbx rbp non-volatile
{{- .PopRegs "rdi" "rbx" "rbp" | indent 4}}
{{- end}}
ret
{{- else}}
{{- .PopRegs | indent 4}}
ret 4
{{- end}}
{{- end}}
;-------------------------------------------------------------------------------
; su_render_song function: the entry point for the synth
;-------------------------------------------------------------------------------
; Has the signature su_render_song(void *ptr), where ptr is a pointer to
; the output buffer. Renders the compile time hard-coded song to the buffer.
; Stack: output_ptr
;-------------------------------------------------------------------------------
{{.Func "actual_render_song" "syncBuf" "SyncStride" "VoiceTrackBitMask" "PolyPhonyBitMask" "NumVoices" "Tracks" "Opcodes" "Operands" "SynthObj"}}
xor eax, eax
{{.Push "1" "RandSeed"}}
{{.Push .AX "GlobalTick"}}
su_render_rowloop: ; loop through every row in the song
{{.Push .AX "Row"}}
{{.Call "su_update_voices"}} ; update instruments for the new row
xor eax, eax ; ecx is the current sample within row
su_render_sampleloop: ; loop through every sample in the row
{{.Push .AX "Sample"}}
mov {{.AX}}, {{.PTRWORD}} [{{.Stack "NumVoices"}}]
{{.Push .AX "VoicesRemain"}}
mov {{.DX}}, {{.PTRWORD}} [{{.Stack "SynthObj"}}] ; {{.DX}} points to the synth object
mov {{.COM}}, {{.PTRWORD}} [{{.Stack "Opcodes"}}] ; COM points to vm code
mov {{.VAL}}, {{.PTRWORD}} [{{.Stack "Operands"}}] ; VAL points to unit params
lea {{.CX}}, [{{.DX}} + su_synthworkspace.size - su_delayline_wrk.filtstate]
lea {{.WRK}}, [{{.DX}} + su_synthworkspace.voices] ; WRK points to the first voice
{{.Call "su_run_vm"}} ; run through the VM code
{{.Pop .AX}}
{{- template "output_sound.asm" .}} ; *ptr++ = left, *ptr++ = right
{{.Pop .AX}}
inc dword [{{.Stack "GlobalTick"}}] ; increment global time, used by delays
inc eax
cmp eax, {{.Song.SamplesPerRow}}
jl su_render_sampleloop
{{.Pop .AX}} ; Stack: pushad ptr
inc eax
cmp eax, {{mul .PatternLength .SequenceLength}}
jl su_render_rowloop
ret XXX
;-------------------------------------------------------------------------------
; su_update_voices function: polyphonic & chord implementation
;-------------------------------------------------------------------------------
; Input: eax : current row within song
; Dirty: pretty much everything
;-------------------------------------------------------------------------------
{{.Func "su_update_voices"}}
{{- if ne .VoiceTrackBitmask 0}}
; The more complicated implementation: one track can trigger multiple voices
xor edx, edx
mov ebx, {{.PatternLength}} ; we could do xor ebx,ebx; mov bl,PATTERN_SIZE, but that would limit patternsize to 256...
div ebx ; eax = current pattern, edx = current row in pattern
mov {{.SI}}, {{.PTRWORD}} [{{.Stack "Tracks"}}]
add {{.SI}}, {{.AX}} ; esi points to the pattern data for current track
xor eax, eax ; eax is the first voice of next track
xor ebx, ebx ; ebx is the first voice of current track
mov {{.BP}}, {{.PTRWORD}} [{{.Stack "SynthObj"}}] ; ebp points to the current_voiceno array
su_update_voices_trackloop:
movzx eax, byte [{{.SI}}] ; eax = current pattern
imul eax, {{.PatternLength}} ; eax = offset to current pattern data
{{- .Prepare "su_patterns" .AX | indent 4}}
movzx eax,byte [{{.Use "su_patterns" .AX}} + {{.DX}}] ; eax = note
push {{.DX}} ; Stack: ptrnrow
xor edx, edx ; edx=0
mov ecx, ebx ; ecx=first voice of the track to be done
su_calculate_voices_loop: ; do {
bt dword [{{.Stack "VoiceTrackBitmask"}} + {{.PTRSIZE}}],ecx ; test voicetrack_bitmask// notice that the incs don't set carry
inc edx ; edx++ // edx=numvoices
inc ecx ; ecx++ // ecx=the first voice of next track
jc su_calculate_voices_loop ; } while bit ecx-1 of bitmask is on
push {{.CX}} ; Stack: next_instr ptrnrow
cmp al, {{.Hold}} ; anything but hold causes action
je short su_update_voices_nexttrack
mov cl, byte [{{.BP}}]
mov edi, ecx
add edi, ebx
shl edi, 12 ; each unit = 64 bytes and there are 1<<MAX_UNITS_SHIFT units + small header
{{- .Prepare "su_synth_obj" | indent 4}}
and dword [{{.Use "su_synth_obj"}} + su_synthworkspace.voices + su_voice.sustain + {{.DI}}], 0 ; set the voice currently active to release; notice that it could increment any number of times
cmp al, {{.Hold}} ; if cl < HLD (no new note triggered)
jl su_update_voices_nexttrack ; goto nexttrack
inc ecx ; curvoice++
cmp ecx, edx ; if (curvoice >= num_voices)
jl su_update_voices_skipreset
xor ecx,ecx ; curvoice = 0
su_update_voices_skipreset:
mov byte [{{.BP}}],cl
add ecx, ebx
shl ecx, 12 ; each unit = 64 bytes and there are 1<<6 units + small header
lea {{.DI}},[{{.Use "su_synth_obj"}} + su_synthworkspace.voices + {{.CX}}]
stosd ; save note
stosd ; save release
mov ecx, (su_voice.size - su_voice.inputs)/4
xor eax, eax
rep stosd ; clear the workspace of the new voice, retriggering oscillators
su_update_voices_nexttrack:
pop {{.BX}} ; ebx=first voice of next instrument, Stack: ptrnrow
pop {{.DX}} ; edx=patrnrow
add {{.SI}}, {{.SequenceLength}}
inc {{.BP}}
{{- $addrname := len .Song.Score.Tracks | printf "su_synth_obj + %v"}}
{{- .Prepare $addrname | indent 8}}
cmp {{.BP}},{{.Use $addrname}}
jl su_update_voices_trackloop
ret
{{- else}}
; The simple implementation: each track triggers always the same voice
xor edx, edx
xor ebx, ebx
mov bl, {{.PatternLength}} ; rows per pattern
div ebx ; eax = current pattern, edx = current row in pattern
{{- .Prepare "su_tracks" | indent 4}}
lea {{.SI}}, [{{.Use "su_tracks"}}+{{.AX}}]; esi points to the pattern data for current track
mov {{.DI}}, {{.PTRWORD}} su_synth_obj+su_synthworkspace.voices
mov bl, {{len .Song.Score.Tracks}} ; MAX_TRACKS is always <= 32 so this is ok
su_update_voices_trackloop:
movzx eax, byte [{{.SI}}] ; eax = current pattern
imul eax, {{.PatternLength}} ; multiply by rows per pattern, eax = offset to current pattern data
{{- .Prepare "su_patterns" .AX | indent 8}}
movzx eax, byte [{{.Use "su_patterns" .AX}} + {{.DX}}] ; ecx = note
cmp al, {{.Hold}} ; anything but hold causes action
je short su_update_voices_nexttrack
mov dword [{{.DI}}+su_voice.sustain], eax ; set the voice currently active to release
jb su_update_voices_nexttrack ; if cl < HLD (no new note triggered) goto nexttrack
su_update_voices_retrigger:
stosd ; save note
stosd ; save sustain
mov ecx, (su_voice.size - su_voice.inputs)/4 ; could be xor ecx, ecx; mov ch,...>>8, but will it actually be smaller after compression?
xor eax, eax
rep stosd ; clear the workspace of the new voice, retriggering oscillators
jmp short su_update_voices_skipadd
su_update_voices_nexttrack:
add {{.DI}}, su_voice.size
su_update_voices_skipadd:
add {{.SI}}, {{.SequenceLength}}
dec ebx
jnz short su_update_voices_trackloop
ret
{{- end}}
{{template "patch.asm" .}}
;-------------------------------------------------------------------------------
; Patterns
;-------------------------------------------------------------------------------
{{.Data "su_patterns"}}
{{- range .Patterns}}
db {{. | toStrings | join ","}}
{{- end}}
;-------------------------------------------------------------------------------
; Tracks
;-------------------------------------------------------------------------------
{{- range $index := .Song.Patch.NumThreads}}
{{print "su_tracks" $index | .Data}}
{{- range (.Sequences $index)}}
db {{. | toStrings | join ","}}
{{- end}}
{{- end}}
{{- if gt (.SampleOffsets | len) 0}}
;-------------------------------------------------------------------------------
; Sample offsets
;-------------------------------------------------------------------------------
{{.Data "su_sample_offsets"}}
{{- range .SampleOffsets}}
dd {{.Start}}
dw {{.LoopStart}}
dw {{.LoopLength}}
{{- end}}
{{end}}
{{- if gt (.DelayTimes | len ) 0}}
;-------------------------------------------------------------------------------
; Delay times
;-------------------------------------------------------------------------------
{{.Data "su_delay_times"}}
dw {{.DelayTimes | toStrings | join ","}}
{{end}}
;-------------------------------------------------------------------------------
; The code for this patch, basically indices to vm jump table
;-------------------------------------------------------------------------------
{{- range $index := .Song.Patch.NumThreads}}
{{print "su_patch_opcodes" $index | .Data}}
db {{.Opcodes $index | toStrings | join ","}}
{{- end}}
;-------------------------------------------------------------------------------
; The parameters / inputs to each opcode
;-------------------------------------------------------------------------------
{{- range $index := .Song.Patch.NumThreads}}
{{print "su_patch_operands" $index | .Data}}
db {{.Operands $index | toStrings | join ","}}
{{- end}}
;-------------------------------------------------------------------------------
; Constants
;-------------------------------------------------------------------------------
{{.SectData "constants"}}
{{.Constants}}

View File

@ -13,6 +13,7 @@
#define SU_LENGTH_IN_PATTERNS {{.Song.Score.Length}}
#define SU_LENGTH_IN_ROWS (SU_LENGTH_IN_PATTERNS*SU_PATTERN_SIZE)
#define SU_SAMPLES_PER_ROW (SU_SAMPLE_RATE*60/(SU_BPM*SU_ROWS_PER_BEAT))
#define SU_NUMTHREADS {{.Song.Patch.NumThreads}}
{{- if or .RowSync (.HasOp "sync")}}
{{- if .RowSync}}
@ -54,7 +55,9 @@ extern "C" {
{{- if or .RowSync (.HasOp "sync")}}
#define SU_SYNC
{{- end}}
void SU_CALLCONV su_render_song(SUsample *buffer);
{{- range $index := .Song.Patch.NumThreads}}
void SU_CALLCONV su_render_song{{if gt $index 0}}{{add1 $index}}{{end}}(SUsample *buffer);
{{- end}}
{{- if gt (.SampleOffsets | len) 0}}
void SU_CALLCONV su_load_gmdls();

View File

@ -13,6 +13,8 @@
%define SU_LENGTH_IN_PATTERNS {{.Song.Score.Length}}
%define SU_LENGTH_IN_ROWS (SU_LENGTH_IN_PATTERNS*SU_PATTERN_SIZE)
%define SU_SAMPLES_PER_ROW (SU_SAMPLE_RATE*60/(SU_BPM*SU_ROWS_PER_BEAT))
%define SU_NUMTHREADS {{.Song.Patch.NumThreads}}
{{- if or .RowSync (.HasOp "sync")}}
{{- if .RowSync}}
@ -38,11 +40,14 @@
{{- end}}
_su_symbols:
{{- range $index := .Song.Patch.NumThreads}}
%ifdef MANGLED
extern _su_render_song@4
extern _su_render_song{{if gt $index 0}}{{add1 $index}}{{end}}@4
%else ; MANGLED
extern su_render_song
extern su_render_song{{if gt $index 0}}{{add1 $index}}{{end}}
%endif ; MANGLED
{{- end}}
{{- if gt (.SampleOffsets | len) 0}}
extern _su_load_gmdls

View File

@ -27,7 +27,7 @@ type (
voiceMapping [MAX_THREADS][MAX_VOICES]int
multithreadSynthCommand struct {
core int
thread int
samples int
time int
}
@ -104,7 +104,7 @@ func (s *MultithreadSynth) startProcesses() {
for cmd := range commandCh {
buffer := s.pool.Get().(*sointu.AudioBuffer)
*buffer = append(*buffer, make(sointu.AudioBuffer, cmd.samples)...)
samples, time, renderError := s.synths[cmd.core].Render(*buffer, cmd.time)
samples, time, renderError := s.synths[cmd.thread].Render(*buffer, cmd.time)
resultCh <- multithreadSynthResult{buffer: buffer, samples: samples, time: time, renderError: renderError}
}
}(cmdChan, resultsChan)
@ -124,16 +124,16 @@ func (s *MultithreadSynth) closeSynths() {
}
func (s *MultithreadSynth) Trigger(voiceIndex int, note byte) {
for core, synth := range s.synths {
if ind := s.voiceMapping[core][voiceIndex]; ind >= 0 {
for i, synth := range s.synths {
if ind := s.voiceMapping[i][voiceIndex]; ind >= 0 {
synth.Trigger(ind, note)
}
}
}
func (s *MultithreadSynth) Release(voiceIndex int) {
for core, synth := range s.synths {
if ind := s.voiceMapping[core][voiceIndex]; ind >= 0 {
for i, synth := range s.synths {
if ind := s.voiceMapping[i][voiceIndex]; ind >= 0 {
synth.Release(ind)
}
}
@ -154,7 +154,7 @@ func (s *MultithreadSynth) CPULoad(loads []sointu.CPULoad) (elems int) {
func (s *MultithreadSynth) Render(buffer sointu.AudioBuffer, maxtime int) (samples int, time int, renderError error) {
count := len(s.synths)
for i := 0; i < count; i++ {
s.commands <- multithreadSynthCommand{core: i, samples: len(buffer), time: maxtime}
s.commands <- multithreadSynthCommand{thread: i, samples: len(buffer), time: maxtime}
}
clear(buffer)
samples = math.MaxInt