Skip to content

Commit d06db51

Browse files
committed
Add Phaser and Noise wave forms
1 parent 9ba7860 commit d06db51

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

audio/internal.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ func oscillatorFunc(instrument Instrument) func(float64) float64 {
2727
return internal.Pulse
2828
case InstrumentOrgan:
2929
return internal.Organ
30+
case InstrumentNoise:
31+
return internal.Noise()
32+
case InstrumentPhaser:
33+
return internal.Phaser
3034
default:
3135
return silence
3236
}

audio/internal/oscillator.go

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33

44
package internal
55

6-
import "math"
6+
import (
7+
"math"
8+
"math/rand"
9+
)
710

811
type Oscillator struct {
912
Func func(pos float64) float64
@@ -19,13 +22,15 @@ func (o *Oscillator) NextSample() (v float64) {
1922
}
2023

2124
func Triangle(pos float64) float64 {
25+
// Based on https://github.com/picolove
2226
phase := math.Mod(pos, 1)
2327
value := math.Abs(phase*2-1)*2 - 1
2428

2529
return value * 0.45
2630
}
2731

2832
func TiltedSaw(pos float64) float64 {
33+
// Based on https://github.com/picolove
2934
phase := math.Mod(pos, 1)
3035
var v float64
3136
if phase < 0.875 {
@@ -37,11 +42,13 @@ func TiltedSaw(pos float64) float64 {
3742
}
3843

3944
func Saw(pos float64) float64 {
45+
// Based on https://github.com/picolove
4046
phase := math.Mod(pos, 1)
4147
return (phase - 0.5) * 0.65
4248
}
4349

4450
func Square(pos float64) float64 {
51+
// Based on https://github.com/picolove
4552
phase := math.Mod(pos, 1)
4653
v := -1.0
4754
if phase < 0.5 {
@@ -51,6 +58,7 @@ func Square(pos float64) float64 {
5158
}
5259

5360
func Pulse(pos float64) float64 {
61+
// Based on https://github.com/picolove
5462
phase := math.Mod(pos, 1)
5563
v := -1.0
5664
if phase < 0.3125 {
@@ -61,10 +69,38 @@ func Pulse(pos float64) float64 {
6169

6270
// Organ is triangle / 2
6371
func Organ(pos float64) float64 {
72+
// Based on https://github.com/picolove
6473
pos = pos * 4
6574

6675
v := math.Abs(math.Mod(pos, 2)-1) - 0.5 +
6776
(math.Abs((math.Mod(pos*0.5, 2))-1)-0.5)/2 - 0.1
6877

6978
return v * 0.55
7079
}
80+
81+
func Noise() func(pos float64) float64 {
82+
// Based on https://github.com/picolove
83+
lastPos := 0.0
84+
sample := 0.0
85+
lsample := 0.0
86+
const tscale = 2489.0158697766 / SampleRate // 2489.0158697766 is freq for D#5
87+
88+
return func(pos float64) float64 {
89+
scale := (pos - lastPos) / tscale
90+
lsample = sample
91+
sample = (lsample + scale*(rand.Float64()*2-1)) / (1 + scale)
92+
lastPos = pos
93+
v := math.Min(math.Max((lsample+sample)*4/3*(1.75-scale), -1), 1)
94+
return v * 0.5
95+
}
96+
}
97+
98+
func Phaser(pos float64) float64 {
99+
// Based on https://github.com/picolove
100+
pos = pos * 2
101+
102+
v := math.Abs(math.Mod(pos, 2)-1) - 0.5 +
103+
(math.Abs(math.Mod(pos*127/128, 2)-1)-0.5)/2 - 0.25
104+
105+
return v * 0.7
106+
}

examples/audio/audio.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ func main() {
1111
Notes: [32]audio.Note{
1212
{
1313
Pitch: audio.PitchG3,
14-
Instrument: audio.InstrumentOrgan,
14+
Instrument: audio.InstrumentNoise,
1515
Volume: 7,
1616
},
1717
},
@@ -34,7 +34,7 @@ func main() {
3434
audio.SetSfx(0, sfx)
3535
audio.Sfx(0, audio.Channel0, 0, 0)
3636
}
37-
if pi.Btnp(pi.Right) && sfx.Notes[0].Instrument < audio.InstrumentOrgan {
37+
if pi.Btnp(pi.Right) && sfx.Notes[0].Instrument < audio.InstrumentPhaser {
3838
sfx.Notes[0].Instrument += 1
3939
audio.SetSfx(0, sfx)
4040
audio.Sfx(0, audio.Channel0, 0, 0)

0 commit comments

Comments
 (0)