Skip to content

Commit

Permalink
Add way to stop playing Sfx with given number
Browse files Browse the repository at this point in the history
User can run Sfx with channel number -2. This stops playing Sfx with number specified as first parameter to Sfx.
  • Loading branch information
elgopher committed Sep 3, 2023
1 parent ecb20cb commit c050cbb
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
6 changes: 4 additions & 2 deletions audio/synth.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,15 @@ func (c *channel) moveToNextNote(sfx SoundEffect) {
func (s *Synthesizer) Sfx(sfxNo int, ch Channel, offset, length int) {
fmt.Println("Sfx is not implemented yet. Sorry...")

s.stopSfx(sfxNo)
if ch >= ChannelStop && ch <= Channel3 {
s.stopSfx(sfxNo)
}

if ch == ChannelAny {
ch = s.findAvailableChannel()
}

if ch < 0 || ch > Channel3 {
if ch < Channel0 || ch > Channel3 {
return
}

Expand Down
42 changes: 42 additions & 0 deletions audio/synth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,48 @@ func TestSynthesizer_Sfx(t *testing.T) {
assert.Equal(t, expectedSignal, signal)
})

t.Run("should stop playing sfx when channel is -2", func(t *testing.T) {
synth := &audio.Synthesizer{}
var e audio.SoundEffect
e.Speed = 1
e.Notes[0].Volume = audio.VolumeLoudest
synth.SetSfx(0, e)

synth.Sfx(0, audio.Channel0, 0, 1)
// when
synth.Sfx(0, audio.ChannelStop, 0, 1)
// then
stat := synth.Stat()
assert.Equal(t, -1, stat.Sfx[0])
// and
assertSilence(t, readSamples(synth, durationOfNoteWhenSpeedIsOne))
})

t.Run("should not stop playing sfx when channel is", func(t *testing.T) {
channels := []audio.Channel{math.MinInt8, -3, 4, math.MaxInt8}

for _, ch := range channels {
testName := fmt.Sprintf("%d", ch)
t.Run(testName, func(t *testing.T) {
synth := &audio.Synthesizer{}
var e audio.SoundEffect
e.Speed = 1
e.Notes[0].Volume = audio.VolumeLoudest
synth.SetSfx(0, e)

synth.Sfx(0, audio.Channel0, 0, 1)
// when
synth.Sfx(0, ch, 0, 1)
// then
stat := synth.Stat()
assert.Equal(t, 0, stat.Sfx[0])
// and
assertNotSilence(t, readSamples(synth, durationOfNoteWhenSpeedIsOne))
})
}

})

sfxOffsetLengthTest(t)
sfxLoopTest(t)
sfxLengthTest(t)
Expand Down

0 comments on commit c050cbb

Please sign in to comment.