Skip to content

Commit

Permalink
implement linear adsr
Browse files Browse the repository at this point in the history
  • Loading branch information
fr3fou committed Jun 28, 2020
1 parent 0c5100f commit 37e92eb
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 9 deletions.
34 changes: 32 additions & 2 deletions a.csv
Original file line number Diff line number Diff line change
@@ -1,2 +1,32 @@
0.000000,0.050625,0.151875,0.303750,0.506250,0.759375,1.063125,1.417500,0.150000,0.150000,0.150000,0.150000,0.150000,0.150000,0.150000,0.150000,0.150000,0.150000,0.150000,0.150000,0.150000,0.150000,0.150000,0.150000,0.150000,0.150000,0.150000,0.150000,0.150000,0.150000,0.150000,0.150000,
0.025313,0.050625,0.101250,0.202500,0.405000,0.810000,1.620000,3.240000,0.150000,0.150000,0.150000,0.150000,0.150000,0.150000,0.150000,0.150000,0.150000,0.150000,0.150000,0.150000,0.150000,0.150000,0.150000,0.150000,0.150000,0.150000,0.150000,0.150000,0.150000,0.150000,0.150000,0.150000,
x,y
0,0.000000
1,0.002531
2,0.005063
3,0.007594
4,0.010125
5,0.012656
6,0.015187
7,0.017719
8,0.020250
9,0.019594
10,0.018937
11,0.018281
12,0.017625
13,0.016969
14,0.016313
15,0.015656
16,0.015000
17,0.015000
18,0.015000
19,0.015000
20,0.015000
21,0.015000
22,0.015000
23,0.015000
24,0.015000
25,0.013125
26,0.011250
27,0.009375
28,0.007500
29,0.005625
30,0.003750
33 changes: 33 additions & 0 deletions b.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
x,y
0,0.000000
1,0.084375
2,0.168750
3,0.253125
4,0.337500
5,0.421875
6,0.506250
7,0.590625
8,0.675000
9,0.653125
10,0.631250
11,0.609375
12,0.587500
13,0.565625
14,0.543750
15,0.521875
16,0.500000
17,0.500000
18,0.500000
19,0.500000
20,0.500000
21,0.500000
22,0.500000
23,0.500000
24,0.500000
25,0.437500
26,0.375000
27,0.312500
28,0.250000
29,0.187500
30,0.125000
31,0.062500
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/fr3fou/gusic

go 1.14

require github.com/davecgh/go-spew v1.1.1
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
20 changes: 13 additions & 7 deletions gusic/adsr.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package gusic

// NewADSR is a constructor for an ADSR
func NewADSR(attack, attackMultiplier, decay, decayMultiplier, sustain, release, releaseMultiplier float64) *ADSR {
return &ADSR{
attackRatio: attack,
Expand All @@ -9,22 +10,25 @@ func NewADSR(attack, attackMultiplier, decay, decayMultiplier, sustain, release,
sustainRatio: sustain,
releaseRatio: release,
releaseMultiplier: releaseMultiplier,
currentMultiplier: 0.0,
}
}

func (a *ADSR) attack(notes []Note) {
multiplier := a.attackMultiplier / float64(len(notes))
step := a.attackMultiplier / float64(len(notes))

for i := range notes {
notes[i].Volume *= multiplier * float64(i)
notes[i].Volume *= a.currentMultiplier
a.currentMultiplier += step
}
}

func (a *ADSR) decay(notes []Note) {
multiplier := a.decayMultiplier / float64(len(notes))
step := a.decayMultiplier / float64(len(notes))

for i := range notes {
notes[i].Volume *= multiplier * float64(i)
notes[i].Volume *= a.currentMultiplier
a.currentMultiplier -= step
}
}

Expand All @@ -35,13 +39,15 @@ func (a *ADSR) sustain(notes []Note) {
}

func (a *ADSR) release(notes []Note) {
multiplier := a.releaseMultiplier / float64(len(notes))
step := a.releaseMultiplier / float64(len(notes))

for i := range notes {
notes[i].Volume *= multiplier * float64(i)
notes[i].Volume *= a.currentMultiplier
a.currentMultiplier -= step
}
}
func (a *ADSR) apply(notes []Note) {

func (a *ADSR) Apply(notes []Note) {
length := len(notes)

attackEnd := int(a.attackRatio * float64(length))
Expand Down
28 changes: 28 additions & 0 deletions gusic/adsr_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package gusic

import (
"fmt"
"testing"
"time"
)

func TestADSR(t *testing.T) {
adsr := NewADSR(0.25, 1.35, 0.25, 0.35, 0.25, 0.25, 1.0)

n := 32

notes := []Note{}

for i := 0; i < n; i++ {
notes = append(notes, A(4, time.Second, 0.015))
}

adsr.Apply(notes)
printVolume(notes)
}

func printVolume(notes []Note) {
for i, note := range notes {
fmt.Printf("%d,%f\n", i, note.Volume)
}
}
2 changes: 2 additions & 0 deletions gusic/gusic.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ type ADSR struct {

releaseRatio float64
releaseMultiplier float64

currentMultiplier float64
}

// Note is a note
Expand Down
24 changes: 24 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package main

import (
"fmt"
"testing"
)

func TestEnvelope(t *testing.T) {
n := 32

notes := []Sample{}

for i := 0; i < n; i++ {
notes = append(notes, 0.5)
}

printVolume(Envelope(notes, 0.25, 0.25, 0.25))
}

func printVolume(notes []Sample) {
for i, note := range notes {
fmt.Printf("%d,%f\n", i, note)
}
}

0 comments on commit 37e92eb

Please sign in to comment.