Skip to content

Commit f0997a2

Browse files
committed
Replace audio.Sfx with audio.Play and audio.Stop
1 parent ae20bd0 commit f0997a2

File tree

9 files changed

+232
-139
lines changed

9 files changed

+232
-139
lines changed

audio/audio.go

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,37 @@ package audio
99

1010
import "fmt"
1111

12-
// Sfx starts playing sound effect with given sfxNo on specified channel.
12+
// Play starts playing sound effect with given sfxNo on specified channel.
1313
//
14-
// sfxNo is the number of sound effect to play (0-63). sfxNo=-1 stops
15-
// playing sound on the given channel. sfxNo=-2 releases the sound
16-
// of the given channel from looping.
14+
// sfxNo is the number of sound effect to play (0-63).
1715
//
18-
// channel is the channel number from 0-3. Channel=-1 chooses available
19-
// channel automatically. Channel=-2 stops playing the given sound effect
20-
// on any channel. See pi.Channel.
16+
// channel is the channel number from 0-3. -1 chooses available
17+
// channel automatically.
2118
//
2219
// offset is the note position to start playing (0-31). Offset is clamped to [0,31].
2320
//
2421
// length is the number of notes to play. If length <= 0 and sfx has no loop
2522
// then entire sfx is played. If length < sfx length then only fraction of sfx is
2623
// played. If length <= 0 and sfx has loop then sfx is played infinitely.
27-
func Sfx(sfxNo int, channel Channel, offset, length int) {
28-
system.Sfx(sfxNo, channel, offset, length)
24+
func Play(sfxNo int, channel, offset, length int) {
25+
system.Play(sfxNo, channel, offset, length)
26+
}
27+
28+
// Stop stops playing sound effect with given sfxNo. -1 stops all sounds.
29+
//
30+
// sfxNo is the number of sound effect to stop (0-63).
31+
func Stop(sfxNo int) {
32+
system.Stop(sfxNo)
33+
}
34+
35+
// channel is the channel number from 0-3. -1 stops loop on all channels.
36+
func StopLoop(channel int) {
37+
system.StopLoop(channel)
38+
}
39+
40+
// channel is the channel number from 0-3. -1 stops all channels.
41+
func StopChan(channel int) {
42+
system.StopChan(channel)
2943
}
3044

3145
// Music starts playing music with given patterNo.
@@ -63,17 +77,6 @@ func LoadAudio(b []byte) error {
6377

6478
var system System = &Synthesizer{}
6579

66-
type Channel int8
67-
68-
const (
69-
Channel0 Channel = 0
70-
Channel1 Channel = 1
71-
Channel2 Channel = 2
72-
Channel3 Channel = 3
73-
ChannelAny Channel = -1 // Rename - it means all channels for Sfx(-2, ...)
74-
ChannelStop Channel = -2
75-
)
76-
7780
type Stat struct {
7881
Sfx [4]int // -1 means no sfx on channel
7982
Note [4]int // -1 means no sfx on channel. Not implemented yet.
@@ -253,7 +256,10 @@ func SetSystem(s System) {
253256
}
254257

255258
type System interface {
256-
Sfx(sfxNo int, channel Channel, offset, length int)
259+
Play(sfxNo, channel, offset, length int)
260+
Stop(sfxNo int)
261+
StopChan(channel int)
262+
StopLoop(channel int)
257263
Music(patterNo int, fadeMs int, channelMask byte)
258264
Stat() Stat
259265
// SetSfx updates the sound effect. sfxNo is 0-63. Updating sfx number which

audio/synth.go

Lines changed: 51 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -93,21 +93,21 @@ func (c *channel) moveToNextNote(sfx SoundEffect) {
9393
c.oscillator.FreqHz = pitchToFreq(note.Pitch)
9494
}
9595

96-
func (s *Synthesizer) Sfx(sfxNo int, ch Channel, offset, length int) {
96+
func (s *Synthesizer) Play(sfxNo, ch, offset, length int) {
9797
if sfxNo == -2 {
9898
s.disableLooping(ch)
9999
return
100100
}
101101

102-
if ch >= ChannelStop && ch <= Channel3 {
103-
s.stopSfx(sfxNo)
102+
if ch >= -2 && ch <= 3 {
103+
s.Stop(sfxNo)
104104
}
105105

106-
if ch == ChannelAny {
106+
if ch == -1 {
107107
ch = s.findAvailableChannel()
108108
}
109109

110-
if ch < Channel0 || ch > Channel3 {
110+
if ch < 0 || ch > 3 {
111111
return
112112
}
113113

@@ -135,37 +135,70 @@ func (s *Synthesizer) Sfx(sfxNo int, ch Channel, offset, length int) {
135135
s.channels[ch].oscillator.FreqHz = pitchToFreq(note0.Pitch)
136136
}
137137

138-
func (s *Synthesizer) disableLooping(ch Channel) {
139-
if ch == ChannelAny || ch == ChannelStop {
138+
func (s *Synthesizer) Stop(sfxNo int) {
139+
if sfxNo == -1 {
140+
for i, c := range s.channels {
141+
c.playing = false
142+
s.channels[i] = c
143+
}
144+
return
145+
}
146+
147+
for i, c := range s.channels {
148+
if c.playing && c.sfxNo == sfxNo {
149+
c.playing = false
150+
s.channels[i] = c
151+
return
152+
}
153+
}
154+
}
155+
156+
func (s *Synthesizer) StopChan(channel int) {
157+
if channel == -1 {
158+
s.Stop(-1)
159+
return
160+
}
161+
if channel < 0 || channel > 3 {
162+
return
163+
}
164+
165+
s.channels[channel].playing = false
166+
}
167+
168+
func (s *Synthesizer) StopLoop(channel int) {
169+
if channel == -1 {
140170
for i := range s.channels {
141171
s.channels[i].loopingDisabled = true
142172
}
143173
return
144174
}
145175

146-
if ch >= Channel0 && ch <= Channel3 {
147-
s.channels[ch].loopingDisabled = true
176+
if channel >= 0 && channel <= 3 {
177+
s.channels[channel].loopingDisabled = true
148178
}
149179
}
150180

151-
func (s *Synthesizer) stopSfx(no int) {
152-
for i, c := range s.channels {
153-
if c.playing && c.sfxNo == no {
154-
c.playing = false
155-
s.channels[i] = c
156-
return
181+
func (s *Synthesizer) disableLooping(ch int) {
182+
if ch == -1 || ch == -2 {
183+
for i := range s.channels {
184+
s.channels[i].loopingDisabled = true
157185
}
186+
return
187+
}
188+
189+
if ch >= 0 && ch <= 0 {
190+
s.channels[ch].loopingDisabled = true
158191
}
159192
}
160193

161-
func (s *Synthesizer) findAvailableChannel() Channel {
194+
func (s *Synthesizer) findAvailableChannel() int {
162195
for i, c := range s.channels {
163196
if !c.playing {
164-
return Channel(i)
197+
return i
165198
}
166199
}
167200

168-
return Channel3
201+
return 3
169202
}
170203

171204
func (s *Synthesizer) Music(patterNo int, fadeMs int, channelMask byte) {

0 commit comments

Comments
 (0)