From 205b068fbf150ea45ad90859ff98641b0869b21f Mon Sep 17 00:00:00 2001 From: NeilClack Date: Wed, 25 Mar 2026 17:17:22 -0500 Subject: [PATCH] Fix audio bug --- internal/application/application.go | 1 + internal/mixer/mixer.go | 40 +++++++++---------- internal/netreq/netReq_test.go | 26 ------------ internal/page/scrollerPage.go | 12 +++--- .../veandco/go-sdl2/mix/sdl_mixer.go | 11 +++-- 5 files changed, 33 insertions(+), 57 deletions(-) delete mode 100644 internal/netreq/netReq_test.go diff --git a/internal/application/application.go b/internal/application/application.go index d45edd4..aa76523 100644 --- a/internal/application/application.go +++ b/internal/application/application.go @@ -100,6 +100,7 @@ func Run() { AssetsDir: dataDir, SystemLevel: "BASIC", SelectedAudioDevice: "default", + SelectedAudio: "audio.wav", InitialFontSize: 128, StandardFontSize: 32, LinePattern: "fibonacci", diff --git a/internal/mixer/mixer.go b/internal/mixer/mixer.go index 42a82bb..01c4890 100644 --- a/internal/mixer/mixer.go +++ b/internal/mixer/mixer.go @@ -43,32 +43,27 @@ func Init(cfg *db.AppConfig, onUpdate func()) error { refreshCacheCallback = onUpdate - // Let SDL choose the best audio driver for the system - // Remove hardcoded ALSA preference to support PulseAudio, PipeWire, etc. - // Users can set SDL_AUDIODRIVER environment variable if needed - if err := sdl.InitSubSystem(sdl.INIT_AUDIO); err != nil { return fmt.Errorf("SDL audio init failed: %w", err) } - // WAV-only does not require mix.Init(); add when you support OGG/MP3/etc. + // FIX 1: Initialize MP3 and OGG decoders + err := mix.Init(mix.INIT_MP3 | mix.INIT_OGG) + if err != nil { + logger.Error("Failed to initialize MP3/OGG support: %v", err) + } + if err := openDeviceLocked(cfg.SelectedAudioDevice); err != nil { - // Clean up the subsystem on failure so callers can retry later. sdl.QuitSubSystem(sdl.INIT_AUDIO) return err } - // TODO: Check if this logic is still needed; move to application - // if lastVolume < 0 { - // lastVolume = int(mix.MAX_VOLUME) - // currentVolume = lastVolume - // if err := db.SetConfigValue("last_volume", strconv.Itoa(lastVolume)); err != nil { - // } - // } else { - // currentVolume = lastVolume - // } - - // mix.VolumeMusic(currentVolume) + // FIX 2: Actually set a default volume! + currentVolume = 128 // MAX_VOLUME + if cfg.LastVolume > 0 { + currentVolume = cfg.LastVolume + } + mix.VolumeMusic(currentVolume) inited = true return nil @@ -109,9 +104,11 @@ func Play(path string, loopForever bool, cfg *db.AppConfig) error { defer mu.Unlock() if !inited || !deviceOpen { + logger.Error("audio mixer not initialized") return errors.New("audio mixer not initialized") } if path == "" { + logger.Error("empty audio path") return errors.New("empty audio path") } @@ -134,10 +131,7 @@ func Play(path string, loopForever bool, cfg *db.AppConfig) error { currentLoops = 0 } - // Open the device - if err := openDeviceLocked(deviceName); err != nil { - logger.Error("Failed to open device: %w", err) - } + // DELETE the openDeviceLocked block that was here! // Apply remembered volume mix.VolumeMusic(currentVolume) @@ -145,6 +139,7 @@ func Play(path string, loopForever bool, cfg *db.AppConfig) error { if err := currentMusic.Play(currentLoops); err != nil { currentMusic.Free() currentMusic = nil + logger.Error("music.Play failed: %v", err) return fmt.Errorf("music.Play: %w", err) } return nil @@ -331,8 +326,9 @@ func Shutdown() { mix.CloseAudio() deviceOpen = false } - // If you add mix.Init(decoderFlags) in the future, pair it with mix.Quit() here. + if inited { + mix.Quit() // Add this to clean up the decoders sdl.QuitSubSystem(sdl.INIT_AUDIO) inited = false } diff --git a/internal/netreq/netReq_test.go b/internal/netreq/netReq_test.go deleted file mode 100644 index 6f2ee35..0000000 --- a/internal/netreq/netReq_test.go +++ /dev/null @@ -1,26 +0,0 @@ -package netreq - -import ( - "fmt" - "testing" - - "rhemawavetech.com/rhemawave/internal/db" -) - -func TestGet(t *testing.T) { - url := "http://localhost:5173/api/v1/proclamations" - - // Explicitly tell GetJSON to return a slice of db.Proclamation - proclamations, err := GetJSON[db.Proclamation](url) - - if err != nil { - t.Fatalf("GetJSON failed: %v", err) - } - - if len(proclamations) == 0 { - t.Fatal("GetJSON returned no records") - } - - // You can now inspect the actual struct fields instead of raw strings - fmt.Printf("Fetched %d records. First ID: %s\n", len(proclamations), proclamations[0].ID) -} diff --git a/internal/page/scrollerPage.go b/internal/page/scrollerPage.go index d884245..0b39652 100644 --- a/internal/page/scrollerPage.go +++ b/internal/page/scrollerPage.go @@ -301,14 +301,14 @@ func (p *ScrollerPage) Init(ctx *PageCtx) error { } // Start audio playback - selectedAudio := p.Base.Context.CfgPtr.SelectedAudio + + // selectedAudio := p.Base.Context.CfgPtr.SelectedAudio assetsDir := p.Base.Context.CfgPtr.AssetsDir - - if selectedAudio == "" { - } else { - if err := mixer.Play(filepath.Join(assetsDir, selectedAudio), true, p.Base.Context.CfgPtr); err != nil { - } + audioFile := filepath.Join(assetsDir, "audio/audio.wav") + logger.Info("Attempting to play %s", audioFile) + if err := mixer.Play(audioFile, true, p.Base.Context.CfgPtr); err != nil { + logger.Error("Mixer returned an error: %v", err) // ADD THIS } currentDevice := mixer.CurrentDevice() diff --git a/vendor/github.com/veandco/go-sdl2/mix/sdl_mixer.go b/vendor/github.com/veandco/go-sdl2/mix/sdl_mixer.go index 1cfca80..9f8d36f 100644 --- a/vendor/github.com/veandco/go-sdl2/mix/sdl_mixer.go +++ b/vendor/github.com/veandco/go-sdl2/mix/sdl_mixer.go @@ -37,9 +37,12 @@ package mix //} //#endif import "C" -import "unsafe" -import "reflect" -import "github.com/veandco/go-sdl2/sdl" +import ( + "reflect" + "unsafe" + + "github.com/veandco/go-sdl2/sdl" +) // Chunk is the internal format for an audio chunk. // (https://www.libsdl.org/projects/SDL_mixer/docs/SDL_mixer_85.html) @@ -212,6 +215,7 @@ func LoadWAV(file string) (chunk *Chunk, err error) { chunk = (*Chunk)(unsafe.Pointer(C.Mix_LoadWAV_RW(C.SDL_RWFromFile(_file, _rb), 1))) if chunk == nil { err = sdl.GetError() + return nil, err } return } @@ -224,6 +228,7 @@ func LoadMUS(file string) (mus *Music, err error) { mus = (*Music)(unsafe.Pointer(C.Mix_LoadMUS(_file))) if mus == nil { err = sdl.GetError() + return nil, err } return }