-
Notifications
You must be signed in to change notification settings - Fork 0
/
sound.go
110 lines (91 loc) · 2.24 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
//go:build !nosound
// +build !nosound
package z3s5
import (
"bytes"
_ "embed"
"io"
"log"
"time"
"github.com/faiface/beep/effects"
"github.com/faiface/beep/speaker"
ogg "github.com/faiface/beep/vorbis"
)
//go:embed sounds/click.ogg
var clickData []byte
//go:embed sounds/confirm.ogg
var confirmData []byte
//go:embed sounds/error.ogg
var errorData []byte
//go:embed sounds/info.ogg
var infoData []byte
//go:embed sounds/okay.ogg
var okayData []byte
//go:embed sounds/ready.ogg
var readyData []byte
//go:embed sounds/start.ogg
var startData []byte
// getSoundData returns the sound data associated with a sound constant.
func getSoundData(snd int) []byte {
switch snd {
case SND_START:
return startData
case SND_CLICK:
return clickData
case SND_CONFIRM:
return confirmData
case SND_INFO:
return infoData
case SND_OKAY:
return okayData
case SND_READY:
return readyData
default:
return errorData
}
}
const supportsSound = true
var soundInited bool
var volume *effects.Volume
func init() {
volume = &effects.Volume{Base: 2}
}
func playSystemSound(snd int) {
reader := bytes.NewReader(getSoundData(snd))
data := io.NopCloser(reader)
streamer, format, err := ogg.Decode(data)
if err != nil {
log.Fatal(err)
}
volume.Streamer = streamer
if !soundInited {
speaker.Init(format.SampleRate, format.SampleRate.N(time.Second/10))
volume.Volume = 0.7
soundInited = true
}
speaker.Play(volume)
}
func setVolume(vol float64) {
if vol == 0.0 {
volume.Silent = true
return
}
vol = vol * 3
if vol > 3.0 {
vol = 3.0 // hearing protection
}
volume.Volume = vol
volume.Silent = false
}
/*
Copyright (c) 2019-2022 Erich Rast
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/