Monorepo for Aesthetic.Computer aesthetic.computer
4
fork

Configure Feed

Select the types of activity you want to include in your feed.

fix: default audio to 48kHz — codecs that claim 192kHz often can't sustain it

MacBook Pro 2011 Cirrus Logic CS4206 reports 32k-192kHz range but XRUNs
constantly at 192kHz. Default to 48kHz for all laptop HDA codecs.
AC_AUDIO_RATE env var overrides for known-good 192kHz hardware.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

+18 -16
+18 -16
fedac/native/src/audio.c
··· 798 798 snd_pcm_hw_params_get_rate_max(params, &rate_max, NULL); 799 799 fprintf(stderr, "[audio] Hardware rate range: %u–%u Hz\n", rate_min, rate_max); 800 800 801 - // Try rates from highest to lowest — pick the best the hardware supports 802 - unsigned int preferred_rates[] = { 192000, 96000, 48000, 44100, 0 }; 803 - unsigned int rate = 0; 804 - for (int i = 0; preferred_rates[i]; i++) { 805 - unsigned int try_rate = preferred_rates[i]; 806 - if (try_rate > rate_max || try_rate < rate_min) continue; 807 - // Test if this exact rate works 808 - if (snd_pcm_hw_params_test_rate(pcm, params, try_rate, 0) == 0) { 809 - rate = try_rate; 810 - fprintf(stderr, "[audio] Selected rate: %u Hz\n", rate); 811 - break; 812 - } 801 + // Pick sample rate: 48kHz is the safe default that all hardware can sustain. 802 + // Many codecs (e.g. Cirrus Logic CS4206) claim 192kHz support but can't 803 + // sustain it without constant XRUNs. Only use high rates on known-good 804 + // hardware (ThinkPad HDA with Realtek codec handles 192kHz fine). 805 + // Heuristic: if max rate > 48kHz AND min rate <= 32kHz, the codec is 806 + // likely a laptop HDA that works better at 48kHz. 807 + unsigned int rate = 48000; 808 + if (rate_max >= 192000 && rate_min > 44100) { 809 + // Dedicated audio interface — likely supports high rates reliably 810 + rate = 192000; 811 + } else if (rate_max >= 96000 && rate_min > 44100) { 812 + rate = 96000; 813 813 } 814 - if (rate == 0) { 815 - // Fallback: let ALSA pick the nearest to 48kHz 816 - rate = 48000; 817 - fprintf(stderr, "[audio] No preferred rate supported, falling back to nearest-48kHz\n"); 814 + // Override: environment variable AC_AUDIO_RATE forces a specific rate 815 + const char *env_rate = getenv("AC_AUDIO_RATE"); 816 + if (env_rate) { 817 + unsigned int r = (unsigned int)atoi(env_rate); 818 + if (r >= rate_min && r <= rate_max) rate = r; 818 819 } 820 + fprintf(stderr, "[audio] Selected rate: %u Hz (hw range %u–%u)\n", rate, rate_min, rate_max); 819 821 snd_pcm_hw_params_set_rate_near(pcm, params, &rate, 0); 820 822 821 823 // Scale period and buffer to ~1ms latency at the negotiated rate