-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
55 lines (46 loc) · 1.68 KB
/
example_test.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
package mix_test
import (
"github.com/kikht/mix"
"github.com/kikht/mix/sox"
"os"
)
func Example() {
const (
sampleRate = 44100
tempo = 58
bars = 4
whole = sampleRate * 60 * 4 / tempo
length = bars * whole
)
sess := mix.NewSession(sampleRate)
sess.SetOutput(os.Stdout)
// It's only example. Handle your errors properly!
kick, _ := sox.Load("examples/audio/kick.ogg")
snare, _ := sox.Load("examples/audio/snare.ogg")
hat, _ := sox.Load("examples/audio/hat.ogg")
crash, _ := sox.Load("examples/audio/crash.ogg")
guitar, _ := sox.Load("examples/audio/guitar.ogg")
drums := mix.NewSession(sampleRate)
drums.AddRegion(mix.Region{Source: crash, Begin: 0, Volume: 0.7, FadeOut: crash.Length()})
for h := mix.Tz(whole / 16); h < whole; h += whole / 16 {
drums.AddRegion(mix.Region{Source: hat, Begin: h, Volume: 0.5, Pan: -0.3})
}
kickPos := [...]mix.Tz{0, whole * 7 / 16, whole * 9 / 16}
for _, k := range kickPos {
drums.AddRegion(mix.Region{Source: kick, Begin: k, Volume: 1})
}
snarePos := [...]mix.Tz{whole / 4, whole * 3 / 4}
for _, s := range snarePos {
drums.AddRegion(mix.Region{Source: snare, Begin: s, Volume: 1, Pan: 0.1})
}
sess.AddRegion(mix.Region{Source: drums, Begin: 0, Volume: 1, FadeIn: whole})
for b := mix.Tz(whole); b < (bars-1)*whole; b += whole {
sess.AddRegion(mix.Region{Source: drums, Begin: b, Volume: 1})
}
sess.AddRegion(mix.Region{Source: drums, Begin: (bars - 1) * whole, Volume: 1, FadeOut: whole})
sess.AddRegion(mix.Region{Source: guitar, Begin: 0, Volume: 1, FadeIn: whole})
sess.AddRegion(mix.Region{Source: guitar, Begin: 2 * whole, Volume: 1, FadeOut: whole})
for i := 0; i < bars; i++ {
sess.Play(whole)
}
}