Skip to content

Commit

Permalink
Merge pull request #201 from egor-denysenko/chore/update-examples-to-…
Browse files Browse the repository at this point in the history
…use-loop2-fn

Update examples to use Loop2 instead of deprecated Loop method
  • Loading branch information
MarkKremer authored Jan 5, 2025
2 parents 9b913f5 + 3e55b7b commit e1593eb
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
16 changes: 12 additions & 4 deletions examples/speedy-player/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,16 @@ type audioPanel struct {
volume *effects.Volume
}

func newAudioPanel(sampleRate beep.SampleRate, streamer beep.StreamSeeker) *audioPanel {
ctrl := &beep.Ctrl{Streamer: beep.Loop(-1, streamer)}
func newAudioPanel(sampleRate beep.SampleRate, streamer beep.StreamSeeker) (*audioPanel, error) {
loopStreamer, err := beep.Loop2(streamer)
if err != nil {
return nil, err
}

ctrl := &beep.Ctrl{Streamer: loopStreamer}
resampler := beep.ResampleRatio(4, 1, ctrl)
volume := &effects.Volume{Streamer: resampler, Base: 2}
return &audioPanel{sampleRate, streamer, ctrl, resampler, volume}
return &audioPanel{sampleRate, streamer, ctrl, resampler, volume}, nil
}

func (ap *audioPanel) play() {
Expand Down Expand Up @@ -172,7 +177,10 @@ func main() {
}
defer screen.Fini()

ap := newAudioPanel(format.SampleRate, streamer)
ap, err := newAudioPanel(format.SampleRate, streamer)
if err != nil {
report(err)
}

screen.Clear()
ap.draw(screen)
Expand Down
7 changes: 5 additions & 2 deletions examples/tutorial/2-composing-and-controlling/b/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ func main() {

speaker.Init(format.SampleRate, format.SampleRate.N(time.Second/10))

loop := beep.Loop(3, streamer)
fast := beep.ResampleRatio(4, 5, loop)
loopStreamer, err := beep.Loop2(streamer, beep.LoopTimes(2))
if err != nil {
log.Fatal(err)
}
fast := beep.ResampleRatio(4, 5, loopStreamer)

done := make(chan bool)
speaker.Play(beep.Seq(fast, beep.Callback(func() {
Expand Down
7 changes: 6 additions & 1 deletion examples/tutorial/2-composing-and-controlling/c/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ func main() {

speaker.Init(format.SampleRate, format.SampleRate.N(time.Second/10))

ctrl := &beep.Ctrl{Streamer: beep.Loop(-1, streamer), Paused: false}
loopStreamer, err := beep.Loop2(streamer)
if err != nil {
log.Fatal(err)
}

ctrl := &beep.Ctrl{Streamer: loopStreamer, Paused: false}
speaker.Play(ctrl)

for {
Expand Down
6 changes: 5 additions & 1 deletion examples/tutorial/2-composing-and-controlling/d/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@ func main() {
defer streamer.Close()

speaker.Init(format.SampleRate, format.SampleRate.N(time.Second/10))
loopStreamer, err := beep.Loop2(streamer)
if err != nil {
log.Fatal(err)
}

ctrl := &beep.Ctrl{Streamer: beep.Loop(-1, streamer), Paused: false}
ctrl := &beep.Ctrl{Streamer: loopStreamer, Paused: false}
volume := &effects.Volume{
Streamer: ctrl,
Base: 2,
Expand Down

0 comments on commit e1593eb

Please sign in to comment.