-
Notifications
You must be signed in to change notification settings - Fork 46
/
sound.go
155 lines (119 loc) · 2.83 KB
/
sound.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
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
146
147
148
149
150
151
152
153
154
155
package main
import (
"time"
"github.com/faiface/beep"
"github.com/faiface/beep/effects"
"github.com/faiface/beep/speaker"
)
type Sound struct {
Stream beep.StreamSeeker
Format beep.Format
volume *effects.Volume
control *beep.Ctrl
Empty bool
}
func NewSound(stream beep.StreamSeeker, format beep.Format) *Sound {
sound := &Sound{
Stream: stream,
Format: format,
}
sound.ReloadStream()
speaker.Play(sound.volume)
return sound
}
func (sound *Sound) ReloadStream() {
// if globals.Settings.Get(SettingsCacheAudioBeforePlayback).AsBool() {
// ogStream := sound.Stream
// // globals.EventLog.Log("caching audio", false)
// buffer := beep.NewBuffer(sound.Format)
// buffer.Append(ogStream)
// sound.Stream = buffer.Streamer(0, ogStream.Len())
// // Close the stream if possible before replacing it, we don't need it after buffering
// if stream, ok := ogStream.(beep.StreamSeekCloser); ok {
// stream.Close()
// }
// }
resampled := beep.Resample(3, sound.Format.SampleRate, globals.ChosenAudioSampleRate, sound.Stream)
seq := beep.Seq(resampled, beep.Callback(func() {
sound.Empty = true
}))
sound.control = &beep.Ctrl{
Streamer: seq,
Paused: true,
}
sound.volume = &effects.Volume{
Streamer: sound.control,
Volume: 0,
Base: 100,
Silent: false,
}
sound.UpdateVolume()
}
func (sound *Sound) UpdateVolume() {
speaker.Lock()
v := globals.Settings.Get(SettingsAudioVolume).AsFloat()
if v > 0 {
sound.volume.Silent = false
sound.volume.Volume = (v / 100) - 1
} else {
sound.volume.Silent = true
}
speaker.Unlock()
}
func (sound *Sound) Play() {
if sound.Stream == nil {
sound.ReloadStream()
}
speaker.Lock()
sound.control.Paused = false
speaker.Unlock()
}
func (sound *Sound) Pause() {
speaker.Lock()
sound.control.Paused = true
speaker.Unlock()
}
func (sound *Sound) IsPaused() bool {
return sound.control.Paused
}
func (sound *Sound) Seek(to time.Duration) {
for to > sound.Length() {
to = to - sound.Length()
}
speaker.Lock()
sound.Stream.Seek(sound.Format.SampleRate.N(to))
speaker.Unlock()
}
func (sound *Sound) SeekPercentage(percentage float32) {
speaker.Lock()
sound.Stream.Seek(int(percentage * float32(sound.Stream.Len())))
speaker.Unlock()
}
func (sound *Sound) Length() time.Duration {
speaker.Lock()
d := sound.Format.SampleRate.D(sound.Stream.Len())
speaker.Unlock()
return d
}
func (sound *Sound) Position() time.Duration {
speaker.Lock()
d := sound.Format.SampleRate.D(sound.Stream.Position())
speaker.Unlock()
return d
}
func (sound *Sound) Destroy() {
sound.Pause()
speaker.Lock()
if stream, ok := sound.Stream.(beep.StreamSeekCloser); ok {
stream.Close()
}
sound.Stream = nil
speaker.Unlock()
}
// func (sound *Sound) TogglePause() {
// if sound.Control.Paused {
// sound.Play()
// } else {
// sound.Pause()
// }
// }