-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmusic.go
More file actions
145 lines (121 loc) · 2.72 KB
/
music.go
File metadata and controls
145 lines (121 loc) · 2.72 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main
import (
"bytes"
"io"
"sync"
"time"
)
// MusicPlayer manages background procedural audio via Oto.
type MusicPlayer struct {
mu sync.Mutex
player *otoPlayer
playing bool
}
// otoPlayer wraps an Oto player and keeps references to the synth
// readers so they aren't garbage collected while playing.
type otoPlayer struct {
closer io.Closer
}
func (o *otoPlayer) Close() {
if o.closer != nil {
o.closer.Close()
}
}
// Play starts procedural ambient music voiced for the given deck.
func (p *MusicPlayer) Play(deck string) {
p.Stop()
if otoCtx == nil {
return
}
voice := voiceForDeck(deck)
drone := newDroneReader(voice)
melody := newMelodyReader(drone.rootFreq(), voice)
mix := newMixReader(drone, melody)
player := otoCtx.NewPlayer(mix)
player.Play()
p.mu.Lock()
p.player = &otoPlayer{closer: player}
p.playing = true
p.mu.Unlock()
}
// Stop halts any current playback.
func (p *MusicPlayer) Stop() {
p.mu.Lock()
defer p.mu.Unlock()
if !p.playing {
return
}
if p.player != nil {
p.player.Close()
p.player = nil
}
p.playing = false
}
// activeSFX tracks playing SFX players to prevent GC.
var activeSFX struct {
mu sync.Mutex
players []io.Closer
}
// PlaySFX plays a one-shot procedural sound effect.
// Supported names: "card-flip", "paper-slide".
func PlaySFX(name string) {
if otoCtx == nil {
return
}
data := cachedSFX(name)
if data == nil {
return
}
// Create a new bytes.Reader each time (each playback needs its own read position).
r := bytes.NewReader(data)
player := otoCtx.NewPlayer(r)
player.Play()
// Hold reference to prevent GC until done.
activeSFX.mu.Lock()
activeSFX.players = append(activeSFX.players, player)
activeSFX.mu.Unlock()
go func() {
// Wait until playback finishes.
for player.IsPlaying() {
time.Sleep(10 * time.Millisecond)
}
// Remove from active list.
activeSFX.mu.Lock()
for i, p := range activeSFX.players {
if p == player {
activeSFX.players = append(activeSFX.players[:i], activeSFX.players[i+1:]...)
break
}
}
activeSFX.mu.Unlock()
}()
}
// PlayWordChime plays a short tonal ping tuned to the active deck's root.
func PlayWordChime(deck string) {
if otoCtx == nil {
return
}
data := cachedWordChime(deck)
if data == nil {
return
}
r := bytes.NewReader(data)
player := otoCtx.NewPlayer(r)
player.Play()
activeSFX.mu.Lock()
activeSFX.players = append(activeSFX.players, player)
activeSFX.mu.Unlock()
go func() {
for player.IsPlaying() {
time.Sleep(10 * time.Millisecond)
}
activeSFX.mu.Lock()
for i, p := range activeSFX.players {
if p == player {
activeSFX.players = append(activeSFX.players[:i], activeSFX.players[i+1:]...)
break
}
}
activeSFX.mu.Unlock()
}()
}