Skip to content
This repository was archived by the owner on Mar 18, 2024. It is now read-only.

Commit 7697f68

Browse files
author
Jason Crawford
committed
Provide more optimal in-situ matrix operations
1 parent 91cc9c1 commit 7697f68

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

mixing/mixbuffer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func (m *MixBuffer) MixInSample(d SampleMixIn) {
5454
pos := d.MixPos
5555
for i := 0; i < d.MixLen; i++ {
5656
sdata := d.Sample.GetSample()
57-
samp := d.StaticVol.Apply(sdata...)
57+
samp := sdata.ApplyInSitu(d.StaticVol)
5858
mixed := d.VolMatrix.Apply(samp...)
5959
for c, s := range mixed {
6060
(*m)[c][pos] += s

volume/volume.go

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,34 @@ func (v Volume) Apply(samp ...Volume) Matrix {
6262
return o
6363
}
6464

65-
// Apply takes a volume matrix and multiplies it my incoming volumes
65+
// Apply takes a volume matrix and multiplies it by incoming volumes
6666
func (m Matrix) Apply(samp ...Volume) Matrix {
6767
o := make(Matrix, len(m))
68-
for _, s := range samp {
69-
for i, v := range s.Apply(m...) {
70-
o[i] += v
71-
}
68+
v := Matrix(samp).Sum()
69+
for i, s := range v.Apply(m...) {
70+
o[i] = s
7271
}
7372
return o
7473
}
7574

75+
// ApplyInSitu takes a volume matrix and multiplies it by incoming volumes
76+
func (m Matrix) ApplyInSitu(samp ...Volume) Matrix {
77+
v := Matrix(samp).Sum()
78+
for i, s := range v.Apply(m...) {
79+
m[i] = s
80+
}
81+
return m
82+
}
83+
84+
// Sum sums all the elements of the Matrix and returns the resulting Volume
85+
func (m Matrix) Sum() Volume {
86+
var v Volume
87+
for _, s := range m {
88+
v += s
89+
}
90+
return v
91+
}
92+
7693
func (v Volume) withOverflowProtection() float64 {
7794
val := float64(v)
7895
if math.Abs(val) <= 1.0 {

0 commit comments

Comments
 (0)