Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions concurrency/map.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package concurrency

import (
"sync"

"github.com/samber/lo"
)

type (
SyncMap[T comparable, E any] struct {
mu sync.Mutex
m map[T]E
}
)

func NewSyncMap[T comparable, E any](m map[T]E) *SyncMap[T, E] {
return &SyncMap[T, E]{
m: m,
}
}

func (m *SyncMap[T, E]) Get(idx T) (E, bool) {
m.mu.Lock()
defer func() {
m.mu.Unlock()
}()
e, found := m.m[idx]
return e, found
}

func (m *SyncMap[T, E]) Set(idx T, e E) {
m.mu.Lock()
defer func() {
m.mu.Unlock()
}()
if m.m == nil {
m.m = make(map[T]E)
}
m.m[idx] = e
}

func (m *SyncMap[T, E]) Delete(idx T) {
m.mu.Lock()
defer func() {
m.mu.Unlock()
}()
delete(m.m, idx)
}

func (m *SyncMap[T, E]) Keys() []T {
return lo.Keys(m.m)
}
4 changes: 2 additions & 2 deletions module/envelope.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ func (e *Envelope) Update(new *Envelope) {
e.initializeFaders()
}

func (e *Envelope) Step(t float64, modules ModuleMap) {
gateValue := getMono(modules[e.Gate])
func (e *Envelope) Step(t float64, modules *ModuleMap) {
gateValue := getMono(modules, e.Gate)

switch {
case e.gateValue <= 0 && gateValue > 0:
Expand Down
18 changes: 9 additions & 9 deletions module/envelope_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ func TestEnvelope_Step(t *testing.T) {
name string
e *Envelope
t float64
modules ModuleMap
modules *ModuleMap
want float64
wantTriggeredAt float64
wantReleasedAt float64
Expand All @@ -250,13 +250,13 @@ func TestEnvelope_Step(t *testing.T) {
Level: 0.5,
},
t: 2,
modules: ModuleMap{
modules: NewModuleMap(map[string]IModule{
"gate": &Module{
current: Output{
Mono: 1,
},
},
},
}),
want: -1,
wantTriggeredAt: 2,
wantGateValue: 1,
Expand All @@ -277,13 +277,13 @@ func TestEnvelope_Step(t *testing.T) {
gateValue: 1,
},
t: 5,
modules: ModuleMap{
modules: NewModuleMap(map[string]IModule{
"gate": &Module{
current: Output{
Mono: -1,
},
},
},
}),
want: 0.5,
wantTriggeredAt: 2,
wantReleasedAt: 5,
Expand All @@ -303,13 +303,13 @@ func TestEnvelope_Step(t *testing.T) {
level: 0.25,
},
t: 8,
modules: ModuleMap{
modules: NewModuleMap(map[string]IModule{
"gate": &Module{
current: Output{
Mono: -1,
},
},
},
}),
want: -1,
wantTriggeredAt: 2,
wantReleasedAt: 5,
Expand All @@ -329,13 +329,13 @@ func TestEnvelope_Step(t *testing.T) {
Level: 0.75,
},
t: 8,
modules: ModuleMap{
modules: NewModuleMap(map[string]IModule{
"gate": &Module{
current: Output{
Mono: 1,
},
},
},
}),
want: 0.5,
wantTriggeredAt: 2,
wantReleasedAt: 0,
Expand Down
18 changes: 5 additions & 13 deletions module/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,6 @@ const (
filterTypeLowPass filterType = "LowPass"
filterTypeHighPass filterType = "HighPass"
filterTypeBandPass filterType = "BandPass"

gain = -50
slope = 0.99
)

var (
amp = math.Pow(10, gain/40)
)

func (m FilterMap) Initialize(sampleRate float64) error {
Expand Down Expand Up @@ -110,15 +103,15 @@ func (f *Filter) Update(new *Filter) {
f.initializeFaders()
}

func (f *Filter) Step(modules ModuleMap) {
func (f *Filter) Step(modules *ModuleMap) {
freq := f.Freq
if f.CV != "" {
freq = cv(freqRange, getMono(modules[f.CV]))
freq = cv(freqRange, getMono(modules, f.CV))
}
freq = modulate(freq, freqRange, getMono(modules[f.Mod]))
freq = modulate(freq, freqRange, getMono(modules, f.Mod))

f.calculateCoeffs(freq)
x := getMono(modules[f.In])
x := getMono(modules, f.In)
y := calc.Limit(f.tap(x, freq), outputRange)

f.current = Output{
Expand Down Expand Up @@ -232,8 +225,7 @@ func getOmega(freq float64, sampleRate float64) float64 {
}

func getAlphaLPHP(omega float64) float64 {
rootArg := (amp+1/amp)*(1/slope-slope) + 2
root := math.Sqrt(rootArg)
root := math.Sqrt(2)
factor := math.Sin(omega) / 2
return factor * root
}
Expand Down
6 changes: 3 additions & 3 deletions module/gate.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (g *Gate) Update(new *Gate) {
}
}

func (g *Gate) Step(modules ModuleMap) {
func (g *Gate) Step(modules *ModuleMap) {
if len(g.Signal) < 1 {
return
}
Expand All @@ -86,10 +86,10 @@ func (g *Gate) Step(modules ModuleMap) {

bpm := g.BPM
if g.CV != "" {
bpm = cv(bpmRange, getMono(modules[g.CV]))
bpm = cv(bpmRange, getMono(modules, g.CV))
}

bpm = modulate(bpm, bpmRange, getMono(modules[g.Mod]))
bpm = modulate(bpm, bpmRange, getMono(modules, g.Mod))
spb := samplesPerBeat(g.sampleRate, bpm)
if spb == 0 {
return
Expand Down
12 changes: 7 additions & 5 deletions module/gate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestGate_Step(t *testing.T) {
tests := []struct {
name string
g *Gate
modules ModuleMap
modules *ModuleMap
want float64
wantIdx float64
}{
Expand All @@ -52,6 +52,7 @@ func TestGate_Step(t *testing.T) {
sampleRate: sampleRate,
idx: 0,
},
modules: &ModuleMap{},
want: 1,
wantIdx: 0,
},
Expand All @@ -63,6 +64,7 @@ func TestGate_Step(t *testing.T) {
sampleRate: sampleRate,
idx: 0,
},
modules: &ModuleMap{},
want: -1,
wantIdx: 1 / sampleRate,
},
Expand All @@ -75,13 +77,13 @@ func TestGate_Step(t *testing.T) {
idx: 1,
CV: "cv",
},
modules: ModuleMap{
modules: NewModuleMap(map[string]IModule{
"cv": &Module{
current: Output{
Mono: calc.Transpose(120, bpmRange, outputRange),
},
},
},
}),
want: 1,
wantIdx: 1 + 2/sampleRate,
},
Expand All @@ -94,13 +96,13 @@ func TestGate_Step(t *testing.T) {
idx: 2,
Mod: "mod",
},
modules: ModuleMap{
modules: NewModuleMap(map[string]IModule{
"mod": &Module{
current: Output{
Mono: -0.03,
},
},
},
}),
want: -1,
wantIdx: 2 + 0.5/sampleRate,
},
Expand Down
Loading