-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmusic.go
81 lines (63 loc) · 1.48 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
package audio
import (
"errors"
"fmt"
"io"
"sync"
"time"
)
const (
MusicBufferLength = time.Second // the length of the internal buffer of Music
)
// musicStream satisfies SoundStreamInterface
type musicStream struct {
music *Music
}
func (m musicStream) GetData() []int16 {
m.music.lock.Lock()
defer m.music.lock.Unlock()
read, _ := m.music.file.Read(m.music.buffer)
return m.music.buffer[:read]
}
func (m musicStream) Seek(offset time.Duration) {
m.music.lock.Lock()
defer m.music.lock.Unlock()
m.music.file.Seek(int64(offset.Seconds() * float64(m.music.info.SampleRate) * float64(m.music.info.ChannelCount)))
}
// Music is a streamed sound played from a InputSoundFile.
type Music struct {
SoundStream
file SoundFileReader
lock sync.Mutex
buffer []int16
}
func NewMusic() (m *Music) {
m = &Music{}
return
}
func (m *Music) Open(file io.ReadSeeker) (err error) {
m.file = NewSoundFileReader(file)
if m.file == nil {
return errors.New("Music: cannot open: unknown format")
}
m.info, err = m.file.Open(file)
if err != nil {
return fmt.Errorf("Music: cannot open stream: %s", err.Error())
}
m.init()
return nil
}
// init is called when the music file has changed
func (m *Music) init() {
m.SoundStream.Init(
musicStream{m},
m.info,
)
//if m.buffer == nil {
// allocate a second worth of buffer
m.buffer = make([]int16, int(float64(m.info.SampleRate*m.info.ChannelCount)*MusicBufferLength.Seconds()))
//}
}
func (m *Music) Close() {
m.SoundStream.Close()
}