Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add speaker.Suspend and speaker.Resume #144

Merged
merged 1 commit into from
Jan 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion speaker/speaker.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func Init(sampleRate beep.SampleRate, bufferSize int) error {

var err error
var readyChan chan struct{}
context, readyChan, err := oto.NewContext(&oto.NewContextOptions{
context, readyChan, err = oto.NewContext(&oto.NewContextOptions{
SampleRate: int(sampleRate),
ChannelCount: channelCount,
Format: otoFormat,
Expand Down Expand Up @@ -95,6 +95,27 @@ func Play(s ...beep.Streamer) {
mu.Unlock()
}

// Suspend suspends the entire audio play.
//
// This function is intended to save resources when no audio is playing.
// To suspend individual streams, use the beep.Ctrl.
func Suspend() error {
err := context.Suspend()
if err != nil {
return errors.Wrap(err, "failed to suspend the speaker")
}
return nil
}

// Resume resumes the entire audio play, which was suspended by Suspend.
func Resume() error {
err := context.Resume()
if err != nil {
return errors.Wrap(err, "failed to resume the speaker")
}
return nil
}

// Clear removes all currently playing Streamers from the speaker.
// Previously buffered samples may still be played.
func Clear() {
Expand Down