Skip to content

Commit

Permalink
Do not call speaker.{Resume(),Suspend()} in sound.Play* anymore
Browse files Browse the repository at this point in the history
This is causing more issues than it needed to, and the CPU usage while
it is unfortunate it is not that bad.

We will still call speaker.Suspend() when the sound is disabled and
speaker.Resume() when the sound is re-enabled. This results in 0% CPU
usage when this feature is not used, and doesn't need to bring back the
complicated initialisation code we had before.
  • Loading branch information
thiagokokada committed Jan 25, 2024
1 parent 890c5d3 commit ecdf3f2
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 22 deletions.
39 changes: 19 additions & 20 deletions internal/sound/sound.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ import (

const Enabled bool = true

// Maximum sound notification lag, 1000ms / 10 = 100ms
const lag time.Duration = time.Second / 10
// Maximum lag, good enough for this use case and will use lower CPU, but need
// to compesate the lag with time.Sleep() to not feel "strange" (e.g.: "floaty"
// notifications because the sound comes too late).
const lag time.Duration = time.Second

var (
buffer1 *beep.Buffer
Expand All @@ -26,45 +28,40 @@ var (
notifications embed.FS
)

func speakerResume() {
func Resume() {
err := speaker.Resume()
if err != nil {
log.Printf("Error while resuming speaker: %v\n", err)
}
}

func speakerSuspend() {
func Suspend() {
speaker.Clear()
err := speaker.Suspend()
if err != nil {
log.Printf("Error while suspending speaker: %v\n", err)
}
}

func PlaySendNotification(endCallback func()) {
speakerResume()

speaker.Play(beep.Seq(
buffer1.Streamer(0, buffer1.Len()),
// https://github.com/gopxl/beep/issues/137#issuecomment-1908845253
beep.Callback(func() { time.Sleep(lag) }),
beep.Callback(speakerSuspend),
beep.Callback(endCallback),
))
// compesate the lag
time.Sleep(lag)
}

func PlayCancelNotification(callback func()) {
speakerResume()

func PlayCancelNotification(endCallback func()) {
speaker.Play(beep.Seq(
buffer2.Streamer(0, buffer2.Len()),
// https://github.com/gopxl/beep/issues/137#issuecomment-1908845253
beep.Callback(func() { time.Sleep(lag) }),
beep.Callback(speakerSuspend),
beep.Callback(callback),
beep.Callback(endCallback),
))
// compesate the lag
time.Sleep(lag)
}

func Init() (err error) {
func Init(suspend bool) (err error) {
var format beep.Format

buffer1, format, err = loadSound("assets/notification_1.ogg")
Expand All @@ -81,9 +78,11 @@ func Init() (err error) {
if err != nil {
return fmt.Errorf("speaker init: %w", err)
}
err = speaker.Suspend()
if err != nil {
return fmt.Errorf("speaker suspend: %w", err)
if suspend {
err = speaker.Suspend()
if err != nil {
return fmt.Errorf("speaker suspend: %w", err)
}
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion internal/sound/sound_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestPlaySendAndCancelNotification(t *testing.T) {
t.Skip("Skipping testing in CI environment")
}

err := Init()
err := Init(false)
if err != nil {
t.Fatalf("Error while initialising sound: %v\n", err)
}
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func main() {
settings = core.ParseFlags(os.Args[0], os.Args[1:], version, optional)

if optional.Sound {
err := sound.Init()
err := sound.Init(!settings.Sound)
if err != nil {
log.Printf("Error while initialising sound: %v.\n", err)
log.Println("Disabling sound...")
Expand Down
3 changes: 3 additions & 0 deletions systray.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"fyne.io/systray"

"github.com/thiagokokada/twenty-twenty-twenty/internal/core"
"github.com/thiagokokada/twenty-twenty-twenty/internal/sound"
)

const systrayEnabled bool = true
Expand Down Expand Up @@ -94,10 +95,12 @@ func onReady() {
}
case <-mSound.ClickedCh:
if mSound.Checked() {
sound.Suspend()
settings.Sound = false

withMutex(&mu, func() { mSound.Uncheck() })
} else {
sound.Resume()
settings.Sound = true

withMutex(&mu, func() { mSound.Check() })
Expand Down

0 comments on commit ecdf3f2

Please sign in to comment.