diff --git a/audio/synth.go b/audio/synth.go index 5e92ded..aeb79ec 100644 --- a/audio/synth.go +++ b/audio/synth.go @@ -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 } diff --git a/audio/synth_test.go b/audio/synth_test.go index fe3c850..0489bba 100644 --- a/audio/synth_test.go +++ b/audio/synth_test.go @@ -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)