-
Notifications
You must be signed in to change notification settings - Fork 0
/
music.go
122 lines (100 loc) · 2.24 KB
/
music.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
package main
import (
"log"
"os"
"time"
"github.com/faiface/beep"
"github.com/faiface/beep/effects"
"github.com/faiface/beep/mp3"
"github.com/faiface/beep/speaker"
)
var (
currentSong = 0
musicCounter = 0 //used for seting up the audio
//musicAddr [20]Music //addresses for music files
//musicName [20]MusicName //names of the music files
music []Music //literally music
//ARBITRARILY SET ARRAY LENGTH, CAN ADJUST LATER
)
//Music ... Music for the game in a simple-to-use system, must be mp3 for now
type Music struct {
location string
name string
}
//MusicName ... Names for the music files
type MusicName struct {
name string
}
func (m *Music) play() {
song, err := os.Open(m.location)
if err != nil {
log.Fatal(err)
}
streamer, format, err := mp3.Decode(song)
if err != nil {
log.Fatal(err)
}
defer streamer.Close()
speaker.Init(format.SampleRate, format.SampleRate.N(time.Second/10))
loop := beep.Loop(-1, streamer) //will indefinitley loop the song selected
volume := &effects.Volume{
Streamer: loop,
Base: 2,
Volume: gameReference.settings.Audio.MusicVolume,
Silent: false,
}
done := make(chan bool)
speaker.Play(beep.Seq(volume, beep.Callback(func() {
done <- true
})))
<-done
}
func loadMusic() {
dirname := "./Assets/Music"
f, err := os.Open(dirname)
if err != nil {
log.Fatal(err)
}
files, err := f.Readdir(-1)
f.Close()
if err != nil {
log.Fatal(err)
}
//set up flag, name, and address
for _, file := range files {
name := file.Name()
if name[len(name)-3:] != "mp3" {
continue
}
location := dirname + "/" + file.Name()
music = append(music, Music{
location,
name,
})
//musicName[musicCounter].name = file.Name()
//musicAddr[musicCounter].location = dirname + "/" + file.Name()
musicCounter++
}
}
func closeSong() {
speaker.Close()
}
//literally just change the song 4head
func switchSong(i int) {
if i == currentSong {
return //literally just don't do anything
}
currentSong = i
closeSong()
music[i].play()
}
//pass in file name, index for file returned
func searchMusic(s string) int {
for i := 0; i < musicCounter; i++ {
if music[i].name == s {
return i //returns the index for the desired song
}
}
//no filename found
return -1
}