-
Notifications
You must be signed in to change notification settings - Fork 4
/
splash_darwin.go
130 lines (101 loc) · 3.3 KB
/
splash_darwin.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
package main
import (
"bytes"
"github.com/ebitengine/oto/v3"
"github.com/hajimehoshi/go-mp3"
"math/rand"
"time"
)
var otoCtx *oto.Context
var readyChan chan struct{}
func init() {
var err error
op := &oto.NewContextOptions{}
// Usually 44100 or 48000. Other values might cause distortions in Oto
op.SampleRate = 44100
// Number of channels (aka locations) to play sounds from. Either 1 or 2.
// 1 is mono sound, and 2 is stereo (most speakers are stereo).
op.ChannelCount = 2
// Format of the source. go-mp3's format is signed 16bit integers.
op.Format = oto.FormatSignedInt16LE
// Remember that you should **not** create more than one context
otoCtx, readyChan, err = oto.NewContext(op)
if err != nil {
panic("oto.NewContext failed: " + err.Error())
}
}
func Splash() {
flip := rand.Intn(2)
var fileBytesReader *bytes.Reader
if flip == 1 {
fileBytesReader = bytes.NewReader(splashSound1)
} else {
fileBytesReader = bytes.NewReader(splashSound2)
}
// Decode file
decodedMp3, err := mp3.NewDecoder(fileBytesReader)
if err != nil {
panic("mp3.NewDecoder failed: " + err.Error())
}
// It might take a bit for the hardware audio devices to be ready, so we wait on the channel.
<-readyChan
// Create a new 'player' that will handle our sound. Paused by default.
player := otoCtx.NewPlayer(decodedMp3)
// Play starts playing the sound and returns without waiting for it (Play() is async).
player.Play()
// We can wait for the sound to finish playing using something like this
for player.IsPlaying() {
time.Sleep(time.Millisecond)
}
// Now that the sound finished playing, we can restart from the beginning (or go to any location in the sound) using seek
// newPos, err := player.(io.Seeker).Seek(0, io.SeekStart)
// if err != nil{
// panic("player.Seek failed: " + err.Error())
// }
// println("Player is now at position:", newPos)
// player.Play()
// If you don't want the player/sound anymore simply close
err = player.Close()
if err != nil {
panic("player.Close failed: " + err.Error())
}
}
func Solution() {
if mute {
return
}
flip := rand.Intn(2)
var fileBytesReader *bytes.Reader
if flip == 1 {
fileBytesReader = bytes.NewReader(solution1)
} else {
fileBytesReader = bytes.NewReader(solution2)
}
// Decode file
decodedMp3, err := mp3.NewDecoder(fileBytesReader)
if err != nil {
panic("mp3.NewDecoder failed: " + err.Error())
}
// It might take a bit for the hardware audio devices to be ready, so we wait on the channel.
<-readyChan
// Create a new 'player' that will handle our sound. Paused by default.
player := otoCtx.NewPlayer(decodedMp3)
// Play starts playing the sound and returns without waiting for it (Play() is async).
player.Play()
// We can wait for the sound to finish playing using something like this
for player.IsPlaying() {
time.Sleep(time.Millisecond)
}
// Now that the sound finished playing, we can restart from the beginning (or go to any location in the sound) using seek
// newPos, err := player.(io.Seeker).Seek(0, io.SeekStart)
// if err != nil{
// panic("player.Seek failed: " + err.Error())
// }
// println("Player is now at position:", newPos)
// player.Play()
// If you don't want the player/sound anymore simply close
err = player.Close()
if err != nil {
panic("player.Close failed: " + err.Error())
}
}