-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.go
47 lines (40 loc) · 1 KB
/
player.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
package main
import (
"math"
)
// player is like io.Reader/Writer, but for the underlying frequency type,
// but there aren't any meaningful errors. See `playableSample`
// for the canonical example
//
// TODO players are assumed responsible, and should do
// something (even zero) the entire buffer they are given.
type player interface {
play([]tf)
}
type stereoPlayer interface {
stereoPlay([]tf, []tf)
}
// sample is a discrete chunk of audio data
type sample []tf
// createSample returns a slice of values sampled from
// a sine wave of frequency f, at points s/Hz apart
func sineSample(f, hz freq) sample {
smp := make(sample, int(hz/f))
for i := range smp {
smp[i] = tf(math.Sin(2 * math.Pi * float64(i) * float64(f/hz)))
}
return smp
}
// playableSample wraps a sample with some state to allow implementing the
// player interface
type playableSample struct {
*mint
sample
}
// implements player
func (s playableSample) play(buf []tf) {
for i := range buf {
buf[i] = s.sample[s.val()]
s.inc()
}
}