-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.go
110 lines (94 loc) · 2.01 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package xm
import (
"math"
)
type numeric interface {
uint8 | int | float64
}
func slideTowards[T numeric](v, goal, delta T) T {
if v > goal {
return clampMin(v-delta, goal)
}
if v < goal {
return clampMax(v+delta, goal)
}
return v
}
func lerp(u, v, t float64) float64 {
return u + t*(v-u)
}
func clampMin[T numeric](v, min T) T {
if v < min {
return min
}
return v
}
func clampMax[T numeric](v, max T) T {
if v > max {
return max
}
return v
}
func clamp[T numeric](v, min, max T) T {
if v < min {
return min
}
if v > max {
return max
}
return v
}
func abs(x float64) float64 {
if x < 0 {
return -x
}
return x
}
func calcSecondsPerRow(ticksPerRow int, bpm float64) float64 {
ticksPerSecond := bpm * 0.4
return 1 / (ticksPerSecond / float64(ticksPerRow))
}
func calcSamplesPerTick(sampleRate, bpm float64) (samplesPerTick float64, bytesPerTick int) {
samplesPerTick = math.Round(sampleRate / (bpm * 0.4))
const (
channels = 2
bytesPerSample = 2
)
bytesPerTick = int(samplesPerTick) * channels * bytesPerSample
return samplesPerTick, bytesPerTick
}
func waveform(step uint8) float64 {
return -math.Sin(2 * 3.141592 * float64(step) / 0x40)
}
func calcRealNote(fnote float64, inst *instrument) float64 {
var frelativeNote float64
var ffinetune float64
if inst != nil {
frelativeNote = float64(inst.relativeNote)
ffinetune = float64(inst.finetune)
}
return (fnote + frelativeNote + ffinetune/128) - 1
}
func linearPeriod(note float64) float64 {
return 7680.0 - note*64.0
}
func linearFrequency(period float64) float64 {
return 8363.0 * math.Pow(2, (4608-period)/768)
}
func envelopeLerp(a, b envelopePoint, frame int) float64 {
if frame <= a.frame {
return a.value
}
if frame >= b.frame {
return b.value
}
p := float64(frame-a.frame) / float64(b.frame-a.frame)
return a.value*(1-p) + b.value*p
}
func putPCM(buf []byte, left, right uint16) {
_ = buf[3] // Early bound check
buf[0] = byte(left)
buf[1] = byte(left >> 8)
buf[2] = byte(right)
buf[3] = byte(right >> 8)
}