local obs = obslua -- Default values local audio_file = "" local audio_volume = 1.0 local REPLAY_BUFFER_SAVED_EVENT = 30 -- UI: Define script properties function script_properties() local props = obs.obs_properties_create() obs.obs_properties_add_path(props, "audio_file", "Audio File", obs.OBS_PATH_FILE, "WAV files (*.wav)", nil) obs.obs_properties_add_float_slider(props, "audio_volume", "Volume", 0.0, 1.0, 0.01) return props end -- UI: Define default volume function script_defaults(settings) obs.obs_data_set_default_double(settings, "audio_volume", 0.5) end -- UI: Load settings from UI function script_update(settings) audio_file = obs.obs_data_get_string(settings, "audio_file") audio_volume = obs.obs_data_get_double(settings, "audio_volume") end -- Called when script is loaded function script_load(settings) obs.obs_frontend_add_event_callback(on_event) end -- Called on replay buffer save function on_event(event) if event == REPLAY_BUFFER_SAVED_EVENT then playsound(audio_file, audio_volume) end end function playsound(file, volume) if file == nil or file == "" then print("No audio file selected.") return end if volume < 0.0 then volume = 0.0 end if volume > 1.0 then volume = 1.0 end local play_command -- Check if running inside Flatpak if os.getenv("FLATPAK_ID") ~= nil then -- Use flatpak-spawn to call host binary play_command = string.format("flatpak-spawn --host play -v %.2f \"%s\" &", volume, file) print("Using flatpak-spawn to run play") else -- Call directly play_command = string.format("play -v %.2f \"%s\" &", volume, file) print("Using direct system call to play") end local run = os.execute(play_command) if not run or run ~= 0 then print("SOX Playback failed. Falling back to Pipewire-Pulse Audio (no volume control).") os.execute(string.format("paplay \"%s\" &", file)) end end