Skip to content

Commit

Permalink
Remove unused code from Synthesizer.Play
Browse files Browse the repository at this point in the history
After recent refactorings some code was no longer needed.
  • Loading branch information
elgopher committed Sep 7, 2023
1 parent 2391487 commit bb09f9b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 26 deletions.
31 changes: 5 additions & 26 deletions audio/synth.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,28 +94,20 @@ func (c *channel) moveToNextNote(sfx SoundEffect) {
}

func (s *Synthesizer) Play(sfxNo, ch, offset, length int) {
if sfxNo == -2 {
s.disableLooping(ch)
if ch < -1 || ch > 3 {
return
}

if ch >= -2 && ch <= 3 {
s.Stop(sfxNo)
if sfxNo < 0 || sfxNo > maxSfxNo {
return
}

s.Stop(sfxNo)

if ch == -1 {
ch = s.findAvailableChannel()
}

if ch < 0 || ch > 3 {
return
}

if sfxNo == -1 {
s.channels[ch].playing = false
return
}

offset = pi.MidInt(offset, 0, 31)

s.channels[ch].playing = true
Expand Down Expand Up @@ -178,19 +170,6 @@ func (s *Synthesizer) StopLoop(channel int) {
}
}

func (s *Synthesizer) disableLooping(ch int) {
if ch == -1 || ch == -2 {
for i := range s.channels {
s.channels[i].loopingDisabled = true
}
return
}

if ch >= 0 && ch <= 0 {
s.channels[ch].loopingDisabled = true
}
}

func (s *Synthesizer) findAvailableChannel() int {
for i, c := range s.channels {
if !c.playing {
Expand Down
24 changes: 24 additions & 0 deletions audio/synth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,30 @@ func TestSynthesizer_PlayStop(t *testing.T) {
}
})

t.Run("should not stop playing sfx when sfx is invalid", func(t *testing.T) {
sfxNumbers := []int{math.MinInt, -1, maxSfxNo + 1, math.MaxInt}

for _, sfxNo := range sfxNumbers {
testName := fmt.Sprintf("%d", sfxNo)
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.Play(0, 0, 0, 1)
// when
synth.Play(sfxNo, 0, 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 bb09f9b

Please sign in to comment.