Fix audio bug
This commit is contained in:
@@ -100,6 +100,7 @@ func Run() {
|
||||
AssetsDir: dataDir,
|
||||
SystemLevel: "BASIC",
|
||||
SelectedAudioDevice: "default",
|
||||
SelectedAudio: "audio.wav",
|
||||
InitialFontSize: 128,
|
||||
StandardFontSize: 32,
|
||||
LinePattern: "fibonacci",
|
||||
|
||||
+18
-22
@@ -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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
@@ -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()
|
||||
|
||||
+8
-3
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user