style(vm): group public/private member types; delete unused types

This commit is contained in:
5684185+vsariola@users.noreply.github.com 2023-10-19 13:07:24 +03:00
parent fdad626279
commit 43707e5fd6

View File

@ -6,41 +6,75 @@ import (
"github.com/vsariola/sointu" "github.com/vsariola/sointu"
) )
// FeatureSet defines what opcodes / parameters are included in the compiled virtual machine type (
// It is used by the compiler to decide how to encode opcodes // FeatureSet defines what opcodes / parameters are included in the compiled virtual machine
type FeatureSet interface { // It is used by the compiler to decide how to encode opcodes
Opcode(unitType string) (int, bool) FeatureSet interface {
TransformCount(unitType string) int Opcode(unitType string) (int, bool)
Instructions() []string TransformCount(unitType string) int
InputNumber(unitType string, paramName string) int Instructions() []string
SupportsParamValue(unitType string, paramName string, value int) bool InputNumber(unitType string, paramName string) int
SupportsParamValueOtherThan(unitType string, paramName string, value int) bool SupportsParamValue(unitType string, paramName string, value int) bool
SupportsModulation(unitType string, paramName string) bool SupportsParamValueOtherThan(unitType string, paramName string, value int) bool
SupportsPolyphony() bool SupportsModulation(unitType string, paramName string) bool
SupportsGlobalSend() bool SupportsPolyphony() bool
} SupportsGlobalSend() bool
}
type Instruction struct { // AllFeatures is used by the library compilation / bridging to configure a virtual machine
Name string // that supports every conceivable parameter, so it needs no members and just returns "true" to all
TransformCount int // queries about what it supports. Contrast this NecessaryFeatures that only returns true if the patch
} // needs support for that feature
AllFeatures struct {
}
// NecessaryFeatures returns true only if the patch actually needs the support for the feature
NecessaryFeatures struct {
opcodes map[string]int
instructions []string
supportsParamValue map[paramKey](map[int]bool)
supportsModulation map[paramKey]bool
globalSend bool
polyphony bool
}
)
type paramKey struct { type paramKey struct {
Unit string Unit string
Param string Param string
} }
type paramValueKey struct { var allOpcodes map[string]int
Unit string var allInstructions []string
Param string var allInputs map[paramKey]int
Value int var allTransformCounts map[string]int
}
// AllFeatures is used by the library compilation / bridging to configure a virtual machine func init() {
// that supports every conceivable parameter, so it needs no members and just returns "true" to all allInstructions = make([]string, len(sointu.UnitTypes))
// queries about what it supports. Contrast this NecessaryFeatures that only returns true if the patch allOpcodes = map[string]int{}
// needs support for that feature allTransformCounts = map[string]int{}
type AllFeatures struct { allInputs = map[paramKey]int{}
i := 0
for k, v := range sointu.UnitTypes {
inputCount := 0
transformCount := 0
for _, t := range v {
if t.CanModulate {
allInputs[paramKey{k, t.Name}] = inputCount
inputCount++
}
if t.CanModulate && t.CanSet {
transformCount++
}
}
allInstructions[i] = k // Opcode 0 is reserved for instrument advance, so opcodes start from 1
allTransformCounts[k] = transformCount
i++
}
sort.Strings(allInstructions) // sort the opcodes to have predictable ordering, as maps don't guarantee the order the items
for i, instruction := range allInstructions {
allOpcodes[instruction] = (i + 1) * 2 // make a map to find out the opcode number based on the type
}
} }
func (_ AllFeatures) SupportsParamValue(unit string, paramName string, value int) bool { func (_ AllFeatures) SupportsParamValue(unit string, paramName string, value int) bool {
@ -80,49 +114,6 @@ func (_ AllFeatures) InputNumber(unitType string, paramName string) int {
return allInputs[paramKey{unitType, paramName}] return allInputs[paramKey{unitType, paramName}]
} }
var allOpcodes map[string]int
var allInstructions []string
var allInputs map[paramKey]int
var allTransformCounts map[string]int
func init() {
allInstructions = make([]string, len(sointu.UnitTypes))
allOpcodes = map[string]int{}
allTransformCounts = map[string]int{}
allInputs = map[paramKey]int{}
i := 0
for k, v := range sointu.UnitTypes {
inputCount := 0
transformCount := 0
for _, t := range v {
if t.CanModulate {
allInputs[paramKey{k, t.Name}] = inputCount
inputCount++
}
if t.CanModulate && t.CanSet {
transformCount++
}
}
allInstructions[i] = k // Opcode 0 is reserved for instrument advance, so opcodes start from 1
allTransformCounts[k] = transformCount
i++
}
sort.Strings(allInstructions) // sort the opcodes to have predictable ordering, as maps don't guarantee the order the items
for i, instruction := range allInstructions {
allOpcodes[instruction] = (i + 1) * 2 // make a map to find out the opcode number based on the type
}
}
// NecessaryFeatures returns true only if the patch actually needs the support for the feature
type NecessaryFeatures struct {
opcodes map[string]int
instructions []string
supportsParamValue map[paramKey](map[int]bool)
supportsModulation map[paramKey]bool
globalSend bool
polyphony bool
}
func NecessaryFeaturesFor(patch sointu.Patch) NecessaryFeatures { func NecessaryFeaturesFor(patch sointu.Patch) NecessaryFeatures {
features := NecessaryFeatures{opcodes: map[string]int{}, supportsParamValue: map[paramKey](map[int]bool){}, supportsModulation: map[paramKey]bool{}} features := NecessaryFeatures{opcodes: map[string]int{}, supportsParamValue: map[paramKey](map[int]bool){}, supportsModulation: map[paramKey]bool{}}
for instrIndex, instrument := range patch { for instrIndex, instrument := range patch {