mirror of
https://github.com/vsariola/sointu.git
synced 2025-07-22 06:54:34 -04:00
feat(go4k): Implement .asm exporting.
This commit is contained in:
@ -4,15 +4,14 @@ import (
|
||||
"bufio"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func ParseAsm(reader io.Reader) (*Song, error) {
|
||||
func DeserializeAsm(asmcode string) (*Song, error) {
|
||||
var bpm int
|
||||
scanner := bufio.NewScanner(reader)
|
||||
scanner := bufio.NewScanner(strings.NewReader(asmcode))
|
||||
patterns := make([][]byte, 0)
|
||||
tracks := make([]Track, 0)
|
||||
var patch Patch
|
||||
@ -186,3 +185,227 @@ func ParseAsm(reader io.Reader) (*Song, error) {
|
||||
s := Song{BPM: bpm, Patterns: patterns, Tracks: tracks, Patch: patch, SongLength: -1}
|
||||
return &s, nil
|
||||
}
|
||||
|
||||
func SerializeAsm(song *Song) (string, error) {
|
||||
paramorder := map[string][]string{
|
||||
"add": []string{},
|
||||
"addp": []string{},
|
||||
"pop": []string{},
|
||||
"loadnote": []string{},
|
||||
"mul": []string{},
|
||||
"mulp": []string{},
|
||||
"push": []string{},
|
||||
"xch": []string{},
|
||||
"distort": []string{"drive"},
|
||||
"hold": []string{"holdfreq"},
|
||||
"crush": []string{"resolution"},
|
||||
"gain": []string{"gain"},
|
||||
"invgain": []string{"invgain"},
|
||||
"filter": []string{"frequency", "resonance", "lowpass", "bandpass", "highpass", "negbandpass", "neghighpass"},
|
||||
"clip": []string{},
|
||||
"pan": []string{"panning"},
|
||||
"delay": []string{"pregain", "dry", "feedback", "damp", "delay", "count", "notetracking"},
|
||||
"compressor": []string{"attack", "release", "invgain", "threshold", "ratio"},
|
||||
"speed": []string{},
|
||||
"out": []string{"gain"},
|
||||
"outaux": []string{"outgain", "auxgain"},
|
||||
"aux": []string{"gain", "channel"},
|
||||
"send": []string{"amount", "voice", "unit", "port", "sendpop"},
|
||||
"envelope": []string{"attack", "decay", "sustain", "release", "gain"},
|
||||
"noise": []string{"shape", "gain"},
|
||||
"oscillator": []string{"transpose", "detune", "phase", "color", "shape", "gain", "type", "lfo", "unison"},
|
||||
"loadval": []string{"value"},
|
||||
"receive": []string{},
|
||||
"in": []string{"channel"},
|
||||
}
|
||||
indentation := 0
|
||||
indent := func() string {
|
||||
return strings.Repeat(" ", indentation*4)
|
||||
}
|
||||
var b strings.Builder
|
||||
println := func(format string, params ...interface{}) {
|
||||
if len(format) > 0 {
|
||||
fmt.Fprintf(&b, "%v", indent())
|
||||
fmt.Fprintf(&b, format, params...)
|
||||
}
|
||||
fmt.Fprintf(&b, "\n")
|
||||
}
|
||||
align := func(table [][]string, format string) [][]string {
|
||||
var maxwidth []int
|
||||
// find the maximum width of each column
|
||||
for _, row := range table {
|
||||
for k, elem := range row {
|
||||
l := len(elem)
|
||||
if len(maxwidth) <= k {
|
||||
maxwidth = append(maxwidth, l)
|
||||
} else {
|
||||
if maxwidth[k] < l {
|
||||
maxwidth[k] = l
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// align each column, depending on the specified formatting
|
||||
for _, row := range table {
|
||||
for k, elem := range row {
|
||||
l := len(elem)
|
||||
var f byte
|
||||
if k >= len(format) {
|
||||
f = format[len(format)-1] // repeat the last format specifier for all remaining columns
|
||||
} else {
|
||||
f = format[k]
|
||||
}
|
||||
switch f {
|
||||
case 'n': // no alignment
|
||||
row[k] = elem
|
||||
case 'l': // left align
|
||||
row[k] = elem + strings.Repeat(" ", maxwidth[k]-l)
|
||||
case 'r': // right align
|
||||
row[k] = strings.Repeat(" ", maxwidth[k]-l) + elem
|
||||
}
|
||||
}
|
||||
}
|
||||
return table
|
||||
}
|
||||
printTable := func(table [][]string) {
|
||||
indentation++
|
||||
for _, row := range table {
|
||||
println("%v %v", row[0], strings.Join(row[1:], ","))
|
||||
}
|
||||
indentation--
|
||||
}
|
||||
delayTable, delayIndices := ConstructDelayTimeTable(song.Patch)
|
||||
sampleTable, sampleIndices := ConstructSampleOffsetTable(song.Patch)
|
||||
// The actual printing starts here
|
||||
println("%%define BPM %d", song.BPM)
|
||||
// delay modulation is pretty much the only %define that the asm preprocessor cannot figure out
|
||||
// as the preprocessor has no clue if a SEND modulates a delay unit. So, unfortunately, for the
|
||||
// time being, we need to figure during export if INCLUDE_DELAY_MODULATION needs to be defined.
|
||||
delaymod := false
|
||||
for i, instrument := range song.Patch {
|
||||
for j, unit := range instrument.Units {
|
||||
if unit.Type == "send" {
|
||||
targetInstrument := i
|
||||
if unit.Parameters["voice"] > 0 {
|
||||
v, err := song.Patch.InstrumentForVoice(unit.Parameters["voice"] - 1)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("INSTRUMENT #%v / SEND #%v targets voice %v, which does not exist", i, j, unit.Parameters["voice"])
|
||||
}
|
||||
targetInstrument = v
|
||||
}
|
||||
if unit.Parameters["unit"] < 0 || unit.Parameters["unit"] >= len(song.Patch[targetInstrument].Units) {
|
||||
return "", fmt.Errorf("INSTRUMENT #%v / SEND #%v target unit %v out of range", i, j, unit.Parameters["unit"])
|
||||
}
|
||||
if song.Patch[targetInstrument].Units[unit.Parameters["unit"]].Type == "delay" && unit.Parameters["port"] == 5 {
|
||||
delaymod = true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if delaymod {
|
||||
println("%%define INCLUDE_DELAY_MODULATION")
|
||||
}
|
||||
println("")
|
||||
println("%%include \"sointu/header.inc\"\n")
|
||||
var patternTable [][]string
|
||||
for _, pattern := range song.Patterns {
|
||||
row := []string{"PATTERN"}
|
||||
for _, v := range pattern {
|
||||
if v == 1 {
|
||||
row = append(row, "HLD")
|
||||
} else {
|
||||
row = append(row, strconv.Itoa(int(v)))
|
||||
}
|
||||
}
|
||||
patternTable = append(patternTable, row)
|
||||
}
|
||||
println("BEGIN_PATTERNS")
|
||||
printTable(align(patternTable, "lr"))
|
||||
println("END_PATTERNS\n")
|
||||
var trackTable [][]string
|
||||
for _, track := range song.Tracks {
|
||||
row := []string{"TRACK", fmt.Sprintf("VOICES(%d)", track.NumVoices)}
|
||||
for _, v := range track.Sequence {
|
||||
row = append(row, strconv.Itoa(int(v)))
|
||||
}
|
||||
trackTable = append(trackTable, row)
|
||||
}
|
||||
println("BEGIN_TRACKS")
|
||||
printTable(align(trackTable, "lr"))
|
||||
println("END_TRACKS\n")
|
||||
println("BEGIN_PATCH")
|
||||
indentation++
|
||||
for i, instrument := range song.Patch {
|
||||
var instrTable [][]string
|
||||
for j, unit := range instrument.Units {
|
||||
stereomono := "MONO"
|
||||
if unit.Stereo {
|
||||
stereomono = "STEREO"
|
||||
}
|
||||
row := []string{fmt.Sprintf("SU_%v", strings.ToUpper(unit.Type)), stereomono}
|
||||
for _, parname := range paramorder[unit.Type] {
|
||||
if unit.Type == "oscillator" && unit.Parameters["type"] == Sample && parname == "color" {
|
||||
row = append(row, fmt.Sprintf("COLOR(%v)", strconv.Itoa(sampleIndices[i][j])))
|
||||
} else if unit.Type == "delay" && parname == "count" {
|
||||
count := len(unit.DelayTimes)
|
||||
if unit.Stereo {
|
||||
count /= 2
|
||||
}
|
||||
row = append(row, fmt.Sprintf("COUNT(%v)", strconv.Itoa(count)))
|
||||
} else if unit.Type == "delay" && parname == "delay" {
|
||||
row = append(row, fmt.Sprintf("DELAY(%v)", strconv.Itoa(delayIndices[i][j])))
|
||||
} else if unit.Type == "oscillator" && parname == "type" {
|
||||
switch unit.Parameters["type"] {
|
||||
case Sine:
|
||||
row = append(row, "TYPE(SINE)")
|
||||
case Trisaw:
|
||||
row = append(row, "TYPE(TRISAW)")
|
||||
case Pulse:
|
||||
row = append(row, "TYPE(PULSE)")
|
||||
case Gate:
|
||||
row = append(row, "TYPE(GATE)")
|
||||
case Sample:
|
||||
row = append(row, "TYPE(SAMPLE)")
|
||||
}
|
||||
} else if v, ok := unit.Parameters[parname]; ok {
|
||||
row = append(row, fmt.Sprintf("%v(%v)", strings.ToUpper(parname), strconv.Itoa(int(v))))
|
||||
} else {
|
||||
return "", fmt.Errorf("The parameter map for unit %v does not contain %v, even though it should", unit.Type, parname)
|
||||
}
|
||||
}
|
||||
instrTable = append(instrTable, row)
|
||||
}
|
||||
println("BEGIN_INSTRUMENT VOICES(%d)", instrument.NumVoices)
|
||||
printTable(align(instrTable, "ln"))
|
||||
println("END_INSTRUMENT")
|
||||
}
|
||||
indentation--
|
||||
println("END_PATCH\n")
|
||||
if len(delayTable) > 0 {
|
||||
var delStrTable [][]string
|
||||
for _, v := range delayTable {
|
||||
row := []string{"DELTIME", strconv.Itoa(int(v))}
|
||||
delStrTable = append(delStrTable, row)
|
||||
}
|
||||
println("BEGIN_DELTIMES")
|
||||
printTable(align(delStrTable, "lr"))
|
||||
println("END_DELTIMES\n")
|
||||
}
|
||||
if len(sampleTable) > 0 {
|
||||
var samStrTable [][]string
|
||||
for _, v := range sampleTable {
|
||||
samStrTable = append(samStrTable, []string{
|
||||
"SAMPLE_OFFSET",
|
||||
fmt.Sprintf("START(%d)", v.Start),
|
||||
fmt.Sprintf("LOOPSTART(%d)", v.LoopStart),
|
||||
fmt.Sprintf("LOOPLENGTH(%d)", v.LoopLength),
|
||||
})
|
||||
}
|
||||
println("BEGIN_SAMPLE_OFFSETS")
|
||||
printTable(align(samStrTable, "r"))
|
||||
println("END_SAMPLE_OFFSETS\n")
|
||||
}
|
||||
println("%%include \"sointu/footer.inc\"")
|
||||
ret := b.String()
|
||||
return ret, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user