@@ -60,22 +60,23 @@ func (s {{ .Name }}) Sample(i int) {{ .SampleType }} {
60
60
return {{ .SampleType }}(s.buffer[i])
61
61
}
62
62
63
- // Append appends [0:Length] samples from src to current buffer and returns new
64
- // {{ .Interface }} buffer. Both buffers must have same number of channels and bit depth,
65
- // otherwise function will panic. If current buffer doesn't have enough capacity,
66
- // new buffer will be allocated with capacity of both sources.
63
+ // Append appends [0:Length] samples from src to current buffer and returns
64
+ // new {{ .Interface }} buffer. Both buffers must have same number of channels and
65
+ // bit depth, otherwise function will panic.
67
66
func (s {{ .Name }}) Append(src {{ .Interface }}) {{ .Interface }} {
68
67
mustSameChannels(s.Channels(), src.Channels()){{if ne .Interface "Floating"}}
69
68
mustSameBitDepth(s.BitDepth(), src.BitDepth()){{end}}
69
+ offset := s.Len()
70
70
if s.Cap() < s.Len()+src.Len() {
71
- // allocate and append buffer with cap of both sources capacity;
72
- s.buffer = append(make([]{{ .Builtin }}, 0, s.Cap()+src.Cap()), s.buffer...)
71
+ s.buffer = append(s.buffer, make([]{{ .Builtin }}, src.Len())...)
72
+ } else {
73
+ s.buffer = s.buffer[:s.Len()+src.Len()]
73
74
}
74
- result := {{ .Interface }}(s)
75
75
for i := 0; i < src.Len(); i++ {
76
- result = result.AppendSample( src.Sample(i))
76
+ s.SetSample(i+offset, src.Sample(i))
77
77
}
78
- return result
78
+ alignCapacity(&s.buffer, s.Channels(), s.Cap())
79
+ return s
79
80
}
80
81
81
82
// Slice slices buffer with respect to channels.
@@ -183,10 +184,10 @@ func Test{{ .Name }}(t *testing.T) {
183
184
signal.Allocator{
184
185
Channels: 3,
185
186
Capacity: 2,
186
- }.{{ .Name }}({{if ne .Interface "Floating"}}signal.{{ .MaxBitDepth }}{{end}}).Append(input) .Slice(1, 3),
187
+ }.{{ .Name }}({{if ne .Interface "Floating"}}signal.{{ .MaxBitDepth }}{{end}}).Append(input.Slice(1, 3) ),
187
188
expected{
188
189
length: 2,
189
- capacity: 4 ,
190
+ capacity: 2 ,
190
191
data: [][]{{ .Builtin }}{
191
192
{0, 0},
192
193
{2, 3},
0 commit comments