mirror of
				https://github.com/vsariola/sointu.git
				synced 2025-10-30 23:45:53 -04:00 
			
		
		
		
	feat(sointu): add copy methods to structs
This commit is contained in:
		
							
								
								
									
										50
									
								
								sointu.go
									
									
									
									
									
								
							
							
						
						
									
										50
									
								
								sointu.go
									
									
									
									
									
								
							| @ -13,6 +13,16 @@ type Unit struct { | ||||
| 	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 ( | ||||
| 	Sine   = iota | ||||
| 	Trisaw = iota | ||||
| @ -27,11 +37,27 @@ type Instrument struct { | ||||
| 	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 | ||||
| type Patch struct { | ||||
| 	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 { | ||||
| 	ret := 0 | ||||
| 	for _, i := range p.Instruments { | ||||
| @ -60,6 +86,22 @@ type Track struct { | ||||
| 	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 { | ||||
| 	Render(buffer []float32, maxtime int) (int, int, error) | ||||
| 	Trigger(voice int, note byte) | ||||
| @ -217,6 +259,14 @@ type Song struct { | ||||
| 	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 { | ||||
| 	return len(s.Tracks[0].Patterns[0]) | ||||
| } | ||||
|  | ||||
		Reference in New Issue
	
	Block a user