forked from vova616/GarageEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ibxm.go
94 lines (80 loc) · 1.84 KB
/
ibxm.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
package ibxm
import (
"github.com/LaPingvino/GarageEngine/engine/audio"
"github.com/vova616/ibxmgo"
"os"
)
var test = audio.AudioClip(&IBXM{})
type IBXM struct {
ibxm *ibxmgo.IBXM
audioBuffer []int32
format audio.Format
length int
}
func NewClip(path string) (*IBXM, error) {
mr, e := os.Open(path)
if e != nil {
return nil, e
}
mod, e := ibxmgo.Decode(mr)
if e != nil {
return nil, e
}
clip, e := ibxmgo.NewIBXM(mod, 48000)
if e != nil {
return nil, e
}
return &IBXM{clip, nil, audio.Stereo16, clip.Length() * 2}, nil
}
/* Returns the length of the buffer required by NextBuffer(). */
func (this *IBXM) BufferLength() int {
return this.ibxm.AudioBufferLength()
}
func (this *IBXM) SetPosition(pos int) {
this.ibxm.SetSequencePos(pos)
}
func (this *IBXM) NextBuffer(outputBuf []int16, mono bool) (samples int) {
if this.audioBuffer == nil {
this.audioBuffer = make([]int32, this.ibxm.AudioBufferLength())
}
n, _ := this.ibxm.GetAudio(this.audioBuffer)
if mono {
for i := 0; i < n; i++ {
x := int((float64(this.audioBuffer[i*2]) + float64(this.audioBuffer[i*2+1])) / 2)
if x > 32767 {
x = 32767
} else if x < -32768 {
x = -32768
}
outputBuf[i] = int16(x)
}
//n *= 2
this.format = audio.Mono16
} else {
n *= 2
for i := 0; i < n; i++ {
x := this.audioBuffer[i]
if x > 32767 {
x = 32767
} else if x < -32768 {
x = -32768
}
outputBuf[i] = int16(x)
}
this.format = audio.Stereo16
}
return n
}
func (this *IBXM) Clone() (audio.AudioClip, error) {
ib, e := ibxmgo.NewIBXM(this.ibxm.Module(), this.ibxm.SampleRate())
return &IBXM{ib, nil, this.format, this.length}, e
}
func (this *IBXM) AudioFormat() audio.Format {
return this.format
}
func (this *IBXM) Length() int {
return this.length
}
func (this *IBXM) SampleRate() int {
return this.ibxm.SampleRate()
}