Enable buffering by default on beefy hardware
authorMarcel van der Veldt <m.vanderveldt@outlook.com>
Sat, 25 Oct 2025 20:16:33 +0000 (22:16 +0200)
committerMarcel van der Veldt <m.vanderveldt@outlook.com>
Sat, 25 Oct 2025 20:16:33 +0000 (22:16 +0200)
music_assistant/controllers/streams.py
music_assistant/helpers/util.py

index 9ade5ea5af28bbb95c0d4029b6dc9be0f8ad1171..5774005e173ca9e2f456205adc50673560df8602 100644 (file)
@@ -74,7 +74,7 @@ from music_assistant.helpers.smart_fades import (
     SmartFadesMixer,
     SmartFadesMode,
 )
-from music_assistant.helpers.util import get_ip_addresses, select_free_port
+from music_assistant.helpers.util import get_ip_addresses, get_total_system_memory, select_free_port
 from music_assistant.helpers.webserver import Webserver
 from music_assistant.models.core_controller import CoreController
 from music_assistant.models.music_provider import MusicProvider
@@ -95,6 +95,9 @@ isfile = wrap(os.path.isfile)
 CONF_ALLOW_BUFFER: Final[str] = "allow_buffering"
 CONF_ALLOW_CROSSFADE_SAME_ALBUM: Final[str] = "allow_crossfade_same_album"
 
+# Calculate total system memory once at module load time
+TOTAL_SYSTEM_MEMORY_GB: Final[float] = get_total_system_memory()
+
 
 def parse_pcm_info(content_type: str) -> tuple[int, int, int]:
     """Parse PCM info from a codec/content_type string."""
@@ -181,7 +184,7 @@ class StreamsController(CoreController):
             ConfigEntry(
                 key=CONF_ALLOW_BUFFER,
                 type=ConfigEntryType.BOOLEAN,
-                default_value=False,
+                default_value=TOTAL_SYSTEM_MEMORY_GB >= 8.0,
                 label="Allow (in-memory) buffering of (track) audio",
                 description="By default, Music Assistant tries to be as resource "
                 "efficient as possible when streaming audio, especially considering "
index 212fe78e4606c0ae4a0c23824c52ac983fb19c46..1b0f4d65ddbdfa4b459a25ca45cb544165e8e674 100644 (file)
@@ -50,6 +50,18 @@ T = TypeVar("T")
 CALLBACK_TYPE = Callable[[], None]
 
 
+def get_total_system_memory() -> float:
+    """Get total system memory in GB."""
+    try:
+        # Works on Linux and macOS
+        total_memory_bytes = os.sysconf("SC_PAGE_SIZE") * os.sysconf("SC_PHYS_PAGES")
+        return total_memory_bytes / (1024**3)  # Convert to GB
+    except (AttributeError, ValueError):
+        # Fallback if sysconf is not available (e.g., Windows)
+        # Return a conservative default to disable buffering by default
+        return 0.0
+
+
 keyword_pattern = re.compile("title=|artist=")
 title_pattern = re.compile(r"title=\"(?P<title>.*?)\"")
 artist_pattern = re.compile(r"artist=\"(?P<artist>.*?)\"")