Skip to content

Commit

Permalink
pull signal.go parts out into separate pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
dskinner committed Oct 23, 2022
1 parent 54fd236 commit be9e1ef
Show file tree
Hide file tree
Showing 17 changed files with 118 additions and 487 deletions.
49 changes: 15 additions & 34 deletions envel.go
Original file line number Diff line number Diff line change
@@ -1,49 +1,30 @@
package snd

import (
"math"
"time"
)

func ExpDecayFunc(t float64) float64 {
return math.Exp(twopi * -t)
}

func ExpDecay() Discrete {
sig := make(Discrete, 1024)
sig.Sample(ExpDecayFunc, 1./1024, 0)
return sig
}
"dasa.cc/signal"
)

func ExpDrive() Discrete {
sig := ExpDecay()
func ExpDrive() signal.Discrete {
sig := signal.ExpDecay()
sig.Reverse()
return sig
}

func LinearDecayFunc(t float64) float64 {
return 1 - t
}

func LinearDecay() Discrete {
sig := make(Discrete, 1024)
sig.Sample(LinearDecayFunc, 1./1024, 0)
return sig
}

func LinearDrive() Discrete {
sig := LinearDecay()
sig.Reverse()
func LinearDrive() signal.Discrete {
sig := make(signal.Discrete, 1024)
sig.Sample(func(t float64) float64 { return t }, 1./1024, 0)
return sig
}

// TODO rename ...
type timed struct {
sig Discrete
sig signal.Discrete
nfr float64
}

func newtimed(sig Discrete, nfr int) *timed {
func newtimed(sig signal.Discrete, nfr int) *timed {
return &timed{sig, float64(nfr)}
}

Expand Down Expand Up @@ -103,13 +84,13 @@ func NewADSR(attack, decay, sustain, release time.Duration, susamp, maxamp float
atk := newtimed(atksig, Dtof(attack, sr))

// dcysig := LinearDecay()
dcysig := ExpDecay()
dcysig := signal.ExpDecay()
dcysig.NormalizeRange(maxamp, susamp)
dcy := newtimed(dcysig, Dtof(decay, sr))

sus := newtimed(Discrete{susamp, susamp}, Dtof(sustain, sr))
sus := newtimed(signal.Discrete{susamp, susamp}, Dtof(sustain, sr))

relsig := ExpDecay()
relsig := signal.ExpDecay()
relsig.NormalizeRange(susamp, 0)
rel := newtimed(relsig, Dtof(release, sr))

Expand Down Expand Up @@ -146,15 +127,15 @@ func (adsr *ADSR) Release() (ok bool) {

type Damp struct {
*mono
sig Discrete
sig signal.Discrete
i, n float64
}

func NewDamp(d time.Duration, in Sound) *Damp {
sd := newmono(in)
return &Damp{
mono: sd,
sig: ExpDecay(),
sig: signal.ExpDecay(),
n: float64(Dtof(d, sd.SampleRate())),
}
}
Expand All @@ -177,7 +158,7 @@ func (dmp *Damp) Prepare(uint64) {

type Drive struct {
*mono
sig Discrete
sig signal.Discrete
i, n float64
}

Expand Down
3 changes: 2 additions & 1 deletion example/bass/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"math"
"time"

"dasa.cc/signal"
"dasa.cc/snd"
"dasa.cc/snd/al"
)
Expand All @@ -27,7 +28,7 @@ func main() {
}
al.Start(master)

sine := snd.Sawtooth()
sine := signal.Sawtooth()

freq0 = notes[15] * math.Pow(2, 30.0/1200)
freq2 = notes[15] * math.Pow(2, -30.0/1200)
Expand Down
7 changes: 4 additions & 3 deletions example/exp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"log"
"time"

"dasa.cc/signal"
"dasa.cc/snd"
"dasa.cc/snd/al"
)
Expand Down Expand Up @@ -36,11 +37,11 @@ func main() {

// osc := snd.NewOscil(src, 1/t, nil)

tri := make(snd.Discrete, 1024)
tri.Sample(snd.TriangleFunc, 1./1024, 1./8)
tri := make(signal.Discrete, 1024)
tri.Sample(signal.TriangleFunc, 1./1024, 1./8)
mod := snd.NewOscil(tri, 0.25, nil)

sine := snd.Sine()
sine := signal.Sine()
osc := snd.NewOscil(sine, 440, nil)
allpass := snd.NewOscil(sine, 440, nil)
allpass.SetPhase(mod)
Expand Down
3 changes: 2 additions & 1 deletion example/oscil/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"log"
"time"

"dasa.cc/signal"
"dasa.cc/snd"
"dasa.cc/snd/al"
)
Expand All @@ -16,7 +17,7 @@ func main() {
}
al.Start(master)

sine := snd.Sine()
sine := signal.Sine()
// // mod is a modulator; try replacing the nil argument to the oscillator with this.
// // mod := snd.NewOscil(sine, 2, nil)
osc := snd.NewOscil(sine, 440, nil) // oscillator
Expand Down
15 changes: 8 additions & 7 deletions example/piano/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@ import (
"math"
"time"

"dasa.cc/signal"
"dasa.cc/snd"
"dasa.cc/snd/al"
)

var (
sawtooth = snd.Sawtooth()
sawsine = snd.SawtoothSynthesis(8)
square = snd.Square()
sqsine = snd.SquareSynthesis(49)
sine = snd.Sine()
triangle = snd.Triangle()
sawtooth = signal.Sawtooth()
sawsine = signal.SawtoothSynthesis(8)
square = signal.Square()
sqsine = signal.SquareSynthesis(49)
sine = signal.Sine()
triangle = signal.Triangle()
notes = snd.EqualTempermant(12, 440, 48)

keys [12]Key
Expand Down Expand Up @@ -117,7 +118,7 @@ type ReeseKey struct {
}

func NewReeseKey(idx int) Key {
sine := snd.Sawtooth()
sine := signal.Sawtooth()
freq := notes[idx-36]
osc0 := snd.NewOscil(sine, freq*math.Pow(2, 30.0/1200), nil)
osc0.SetAmp(snd.Decibel(-3).Amp(), nil)
Expand Down
5 changes: 3 additions & 2 deletions example/plugin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"plugin"
"time"

"dasa.cc/signal"
"dasa.cc/snd"
"dasa.cc/snd/al"
)
Expand Down Expand Up @@ -38,8 +39,8 @@ func main() {
al.Start(gain)

dur := snd.BPM(80).Dur()
mod := snd.NewOscil(snd.Square(), 40, nil)
osc := snd.NewOscil(snd.Sine(), 440, mod)
mod := snd.NewOscil(signal.Square(), 40, nil)
osc := snd.NewOscil(signal.Sine(), 440, mod)
dmp := snd.NewDamp(dur, osc)

loop := snd.NewLoop(dur, dmp)
Expand Down
5 changes: 3 additions & 2 deletions example/rhythm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import (
"log"
"time"

"dasa.cc/signal"
"dasa.cc/snd"
"dasa.cc/snd/al"
)

var (
notes = snd.EqualTempermant(12, 440, 48)
sawsine = snd.SawtoothSynthesis(8)
triangle = snd.Triangle()
sawsine = signal.SawtoothSynthesis(8)
triangle = signal.Triangle()
bpm = snd.BPM(80)
)

Expand Down
6 changes: 4 additions & 2 deletions freeze.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package snd
import (
"math"
"time"

"dasa.cc/signal"
)

type Freeze struct {
*mono
sig, prv Discrete
sig, prv signal.Discrete
r int
}

Expand All @@ -20,7 +22,7 @@ func NewFreeze(d time.Duration, in Sound) *Freeze {
n = int(math.Ldexp(1, e))
}

frz := &Freeze{mono: newmono(nil), prv: make(Discrete, n)}
frz := &Freeze{mono: newmono(nil), prv: make(signal.Discrete, n)}
frz.sig = frz.prv[:f]

inps := GetInputs(in)
Expand Down
6 changes: 4 additions & 2 deletions freeze_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package snd
import (
"testing"
"time"

"dasa.cc/signal"
)

func TestRingcopy(t *testing.T) {
Expand Down Expand Up @@ -34,8 +36,8 @@ func TestRingcopy(t *testing.T) {

func BenchmarkRingcopy(b *testing.B) {
// src := []float64(ExpDecay())
src := make(Discrete, 32)
src.Sample(ExpDecayFunc, 1./32, 0)
src := make(signal.Discrete, 32)
src.Sample(signal.ExpDecayFunc, 1./32, 0)
dst := make([]float64, 256)
r := 0
b.ReportAllocs()
Expand Down
10 changes: 5 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
module dasa.cc/snd

require (
dasa.cc/material v0.0.0-20181002233133-bf1ace7fb445
github.com/fsnotify/fsnotify v1.4.7 // indirect
golang.org/x/exp v0.0.0-20200228211341-fcea875c7e85 // indirect
golang.org/x/image v0.0.0-20200119044424-58c23975cae1 // indirect
golang.org/x/mobile v0.0.0-20200222142934-3c8601c510d0
golang.org/x/sys v0.0.0-20200301040627-c5d0d7b4ec88 // indirect
golang.org/x/exp/shiny v0.0.0-20221012211006-4de253d81b95 // indirect
golang.org/x/image v0.0.0-20221017200508-ffcb3fe7d1bf // indirect
golang.org/x/mobile v0.0.0-20221012134814-c746ac228303
golang.org/x/sys v0.1.0 // indirect
gonum.org/v1/netlib v0.0.0-20180930160340-e150bd5bba73 // indirect
gonum.org/v1/plot v0.0.0-20180905080458-5f3c436ce602
)

go 1.13
go 1.19
Loading

0 comments on commit be9e1ef

Please sign in to comment.