mirror of
https://github.com/vsariola/sointu.git
synced 2025-06-04 01:28:45 -04:00
feat(sointu): add copy methods to structs
This commit is contained in:
parent
cbf9d34738
commit
b1df5bb4d5
50
sointu.go
50
sointu.go
@ -13,6 +13,16 @@ type Unit struct {
|
|||||||
VarArgs []int `yaml:",flow,omitempty"`
|
VarArgs []int `yaml:",flow,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (u *Unit) Copy() Unit {
|
||||||
|
parameters := make(map[string]int)
|
||||||
|
for k, v := range u.Parameters {
|
||||||
|
parameters[k] = v
|
||||||
|
}
|
||||||
|
varArgs := make([]int, len(u.VarArgs))
|
||||||
|
copy(varArgs, u.VarArgs)
|
||||||
|
return Unit{Type: u.Type, Parameters: parameters, VarArgs: varArgs}
|
||||||
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
Sine = iota
|
Sine = iota
|
||||||
Trisaw = iota
|
Trisaw = iota
|
||||||
@ -27,11 +37,27 @@ type Instrument struct {
|
|||||||
Units []Unit
|
Units []Unit
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (instr *Instrument) Copy() Instrument {
|
||||||
|
units := make([]Unit, len(instr.Units))
|
||||||
|
for i, u := range instr.Units {
|
||||||
|
units[i] = u.Copy()
|
||||||
|
}
|
||||||
|
return Instrument{NumVoices: instr.NumVoices, Units: units}
|
||||||
|
}
|
||||||
|
|
||||||
// Patch is simply a list of instruments used in a song
|
// Patch is simply a list of instruments used in a song
|
||||||
type Patch struct {
|
type Patch struct {
|
||||||
Instruments []Instrument
|
Instruments []Instrument
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *Patch) Copy() Patch {
|
||||||
|
instruments := make([]Instrument, len(p.Instruments))
|
||||||
|
for i, instr := range p.Instruments {
|
||||||
|
instruments[i] = instr.Copy()
|
||||||
|
}
|
||||||
|
return Patch{Instruments: instruments}
|
||||||
|
}
|
||||||
|
|
||||||
func (p Patch) TotalVoices() int {
|
func (p Patch) TotalVoices() int {
|
||||||
ret := 0
|
ret := 0
|
||||||
for _, i := range p.Instruments {
|
for _, i := range p.Instruments {
|
||||||
@ -60,6 +86,22 @@ type Track struct {
|
|||||||
Patterns [][]byte `yaml:",flow"`
|
Patterns [][]byte `yaml:",flow"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *Track) Copy() Track {
|
||||||
|
sequence := make([]byte, len(t.Sequence))
|
||||||
|
copy(sequence, t.Sequence)
|
||||||
|
patterns := make([][]byte, len(t.Patterns))
|
||||||
|
for i, oldPat := range t.Patterns {
|
||||||
|
newPat := make([]byte, len(oldPat))
|
||||||
|
copy(newPat, oldPat)
|
||||||
|
patterns[i] = newPat
|
||||||
|
}
|
||||||
|
return Track{
|
||||||
|
NumVoices: t.NumVoices,
|
||||||
|
Sequence: sequence,
|
||||||
|
Patterns: patterns,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type Synth interface {
|
type Synth interface {
|
||||||
Render(buffer []float32, maxtime int) (int, int, error)
|
Render(buffer []float32, maxtime int) (int, int, error)
|
||||||
Trigger(voice int, note byte)
|
Trigger(voice int, note byte)
|
||||||
@ -217,6 +259,14 @@ type Song struct {
|
|||||||
Patch Patch
|
Patch Patch
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Song) Copy() Song {
|
||||||
|
tracks := make([]Track, len(s.Tracks))
|
||||||
|
for i, t := range s.Tracks {
|
||||||
|
tracks[i] = t.Copy()
|
||||||
|
}
|
||||||
|
return Song{BPM: s.BPM, Tracks: tracks, Patch: s.Patch.Copy()}
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Song) PatternRows() int {
|
func (s *Song) PatternRows() int {
|
||||||
return len(s.Tracks[0].Patterns[0])
|
return len(s.Tracks[0].Patterns[0])
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user