/
main.go
82 lines (66 loc) · 1.68 KB
/
main.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
/*
*********************************
* GOSFML2
* SFML Example: Sound/Music
* Ported from C++ to Go
*********************************
*/
package main
import (
sf "bitbucket.org/krepa098/gosfml2"
"fmt"
"os"
"time"
)
func playSound() {
// Load a sound buffer from a wav file
buffer, err := sf.NewSoundBufferFromFile("resources/canary.wav")
if err != nil {
panic(err)
}
// Display sound informations
fmt.Println("canary.wav :")
fmt.Println(" ", buffer.GetDuration())
fmt.Println(" ", buffer.GetSampleRate(), " samples/sec")
fmt.Println(" ", buffer.GetChannelCount(), " channels")
// Create a sound instance and play it
sound := sf.NewSound(buffer)
sound.Play()
// Loop while the sound is playing
for sound.GetStatus() == sf.SoundStatusPlaying {
// Leave some CPU time for other processes
time.Sleep(100)
// Display the playing position
fmt.Println("Playing...", sound.GetPlayingOffset())
}
}
func playMusic() {
// Load an ogg music file
music, err := sf.NewMusicFromFile("resources/orchestral.ogg")
if err != nil {
panic(err)
}
// Display sound informations
fmt.Println("orchestral.ogg :")
fmt.Println(" ", music.GetDuration())
fmt.Println(" ", music.GetSampleRate(), " samples/sec")
fmt.Println(" ", music.GetChannelCount(), " channels")
// Play it
music.Play()
// Loop while the music is playing
for music.GetStatus() == sf.SoundStatusPlaying {
// Leave some CPU time for other processes
time.Sleep(100)
// Display the playing position
fmt.Println("Playing...", music.GetPlayingOffset())
}
}
func main() {
// Play a sound
playSound()
// Play a music
playMusic()
fmt.Println("Press enter to exit...")
var buffer [1]byte
os.Stdin.Read(buffer[:])
}