BlueALSA

repository·master·Indexed 21 days ago

https://github.com/arkq/bluez-alsa

A lightweight Bluetooth audio backend providing A2DP, HFP, HSP, and BLE-MIDI support via ALSA plug-ins. Designed for low-power, dedicated audio systems, it acts as a proxy between BlueZ (bluetoothd) and ALSA, avoiding the overhead of full audio servers like PulseAudio or PipeWire. Includes the bluealsad daemon, bluealsactl management utility, bluealsa-aplay for creating Bluetooth speakers, and a2dpconf for decoding codec capability strings.

Tokens
13.5K
Snippets
47
Records
69
Agent score
73%

What's inside bluez-alsa

  1. Use bluealsactl to manage BlueALSA via D-Bus

    master

    bluealsactl is a command-line interface for the BlueALSA D-Bus API (org.bluealsa.Manager1 and org.bluealsa.PCM1). It allows you to inspect the service status, list available Bluetooth services and PCMs, and control PCM properties like volume, mute, and codecs while they are running.

    Key Workflow:

    1. Use list-services to find running BlueALSA services.
    2. Use list-pcms to find valid PCM_PATH values.
    3. Use info <PCM_PATH> to inspect a specific PCM's properties (channels, volume, codecs).
    4. Use specific commands (codec, volume, mute, etc.) to modify the PCM state.
    bluealsactl list-pcms
  2. Manage client-side delay with ClientDelay

    master

    The ClientDelay property (measured in 1/10 of a millisecond) allows a client to account for its own processing latency.

    • For PCM sources: Set this to a value reported by your playback subsystem to account for playback delay.
    • For PCM sinks: Use this to adjust the Delay property to compensate for Bluetooth devices that do not report accurate delay values.
  3. How BlueALSA works

    master

    BlueALSA is a low-level Bluetooth audio backend designed for small, low-powered, dedicated audio/visual systems. It acts as a proxy between BlueZ (the Bluetooth daemon bluetoothd) and ALSA.

    Unlike high-level audio servers like PipeWire or PulseAudio, BlueALSA is designed for systems where applications interface directly with ALSA and only one application uses each Bluetooth audio stream at a time. It provides support for:

    • Classic Bluetooth audio profiles: A2DP, HFP, and HSP.
    • BLE-MIDI: Creates a MIDI port directly in the ALSA MIDI sequencer.

    Core Components:

    • bluealsad (daemon): The heart of the system. It interfaces with BlueZ and handles profile connection/configuration logic, presenting audio streams via D-Bus.
    • ALSA plug-ins: Hide D-Bus complexities, allowing standard ALSA applications to use Bluetooth devices as if they were local sound cards.
    • bluealsactl: Command-line utility for managing the BlueALSA system.
    • bluealsa-aplay: A utility to simplify building Bluetooth speakers by forwarding audio from a BlueALSA capture PCM to a local playback PCM.
    • bluealsa-rfcomm: Provides access to the RFCOMM terminal for HFP/HSP devices.
  4. Manage Bluetooth audio volume with BlueALSA CTL

    master

    The BlueALSA CTL plugin provides mixer controls for Bluetooth devices. Since these are not hardware soundcards, they won't appear in the standard alsamixer F6 menu. You must select them explicitly.

    Operating Modes:

    1. Default Mode: Dynamically creates/removes controls as devices connect/disconnect. Use alsamixer -D bluealsa.
    2. Single Device Mode: Presents controls only for one specific device. This is more stable for applications that cannot handle dynamic control changes. Use alsamixer -D bluealsa:XX:XX:XX:XX:XX:XX.

    Special Address: Using alsamixer -D bluealsa:00:00:00:00:00:00 opens a mixer for the most recently connected device. It will not switch to a new device if a different one connects later.

    Extended Controls (EXT parameter): By default, extended controls are off. Set EXT=yes or specify individual controls (codec, mode, sync, battery) to enable:

    • codec: Switch between supported codecs.
    • mode: Select volume mode (software or pass-through).
    • sync: Adjust client delay (latency) in ms (range -3275 to +3275).
    • battery: View battery level (if supported).
    # Open mixer for all devices (Default Mode)
    alsamixer -D bluealsa
    
    # Open mixer for a specific device (Single Device Mode)
    alsamixer -D bluealsa:00:11:22:33:44:55
  5. Manage audio latency and timer drift with bluealsa-aplay

    master

    Buffer and Period Settings

    • --pcm-period-time=SEC: Sets the playback PCM period duration in microseconds.
      • Default: 50000 for A2DP, 20000 for SCO.
      • Increasing this can help with underruns/overruns but increases latency.
      • Tip: To avoid rounding errors with the ALSA rate plugin, choose a period time that results in an exact integer number of frames for both source and sink rates (e.g., 10000 µs).
    • --pcm-buffer-time=SEC: Sets the playback PCM buffer duration in microseconds.
      • Recommended: An exact multiple of the period time, and at least 3x the period time for reliability.

    Adaptive Resampling (Timer Drift Compensation)

    If bluealsa-aplay was built with libsamplerate support, you can use --resampler=METHOD to compensate for timer drift between the Bluetooth and ALSA clocks. This prevents audio "blips" caused by buffer exhaustion or overflow.

    Available methods via --resampler=:

    • sinc-best: Highest quality, highest CPU usage.
    • sinc-medium: High quality, moderate CPU usage.
    • sinc-fastest: Good quality, lower CPU usage (recommended compromise).
    • linear: Lower quality, low CPU usage. Not bandwidth limited.
    • zero-order-hold: Lowest quality, very low CPU usage (for embedded processors). Not bandwidth limited.
    • none (Default): No resampling; relies on ALSA device for rate conversion.
  6. Understand BlueALSA volume control

    master

    BlueALSA provides a consistent interface for volume control via D-Bus or ALSA plugins. It supports two modes:

    1. Native Volume Control (Default): Links PCM volume to Bluetooth AVRCP (for A2DP) or HFP/HSP volume control. Local clients and remote devices can both modify it. Local clients are notified of changes made by the remote device.
    2. Soft-Volume: Scales PCM samples locally before encoding/after decoding. It does not interact with Bluetooth volume properties. Only local clients can modify it.

    Volume Initialization Priority: When a device connects, volume is set in this order:

    1. Saved value from previous connection.
    2. Value set by --initial-volume.
    3. 100%.

    Note: For reliable native A2DP volume control, BlueZ version 5.65 or later is recommended.

  7. Configure bluealsad profiles and roles

    master

    The daemon supports several Bluetooth profiles. Note that some profiles (like asha-source, hfp-ofono, and midi) require specific compilation support.

    Available Profile Names:

    • a2dp-source: Advanced Audio Source (streaming audio to connected device)
    • a2dp-sink: Advanced Audio Sink (receiving audio from connected device)
    • asha-source: Audio Streaming for Hearing Aids Source
    • asha-sink: Audio Streaming for Hearing Aids Sink
    • hfp-ofono: Hands-Free AG/HF handled by oFono
    • hfp-ag: Hands-Free Audio Gateway
    • hfp-hf: Hands-Free
    • hsp-ag: Headset Audio Gateway
    • hsp-hs: Headset
    • midi: Bluetooth LE MIDI

    Important Notes:

    • Enabling hfp-ofono automatically disables hfp-hf and hfp-ag.
    • Only one service can register the HFP profile per BlueZ instance; it is automatically registered with every HCI device.
  8. Using BlueALSA alongside PulseAudio or PipeWire

    master

    Running BlueALSA simultaneously with PulseAudio or PipeWire (with their Bluetooth modules enabled) is not recommended and can cause conflicts.

    Symptoms: BlueALSA may log warnings about UUIDs already being registered in BlueZ: bluealsad: W: UUID already registered in BlueZ [hci0]: ...

    Recommendation: For a deterministic setup, disable Bluetooth in PulseAudio or PipeWire. If integration is required, refer to the PulseAudio integration wiki.

  9. Configure dmix for bluealsa-aplay

    master

    The ALSA dmix plugin may ignore requested period and buffer times, which can cause rounding errors when used with the ALSA rate plugin. To ensure stability, explicitly define the hardware period size and buffer size in your ALSA configuration.

    Example: Global dmix configuration

    To set a period time of 50000 µs and 4 periods for an Intel 'PCH' card, add this to your ALSA configuration:

    defaults.dmix.PCH.period_time 50000
    defaults.dmix.PCH.periods 4

    Example: Custom PCM definition

    Alternatively, define a specific PCM (e.g., dmix_rate_fix) that uses dmix with fixed parameters:

    pcm.dmix_rate_fix {
        type plug
        slave.pcm {
            type dmix
            ipc_key 12345
            slave {
                pcm "hw:0,0"
                period_time 50000
                periods 4
            }
        }
    }
  10. Use bluealsa-aplay to create a Bluetooth speaker

    master

    The bluealsa-aplay utility simplifies forwarding audio from a Bluetooth device (capture PCM) to a local playback device (e.g., a built-in sound card).

    Play audio from a specific device

    bluealsa-aplay XX:XX:XX:XX:XX:XX

    List available BlueALSA PCMs

    To see a list of all connected Bluetooth devices with audio capabilities:

    bluealsa-aplay -L
  11. Set up the BlueALSA runtime environment without systemd

    master

    If you are not using systemd or did not use the --enable-systemd flag, you must manually create the persistent state storage directory.

    1. Create a directory named bluealsa under the system local state directory (typically /var/lib).
    2. Set the owner to the user account running the bluealsad daemon.
    3. Set permissions to rwx------ (0700) to prevent corruption.

    Example for a daemon running as user bluealsa:

    sudo mkdir /var/lib/bluealsa
    sudo chown bluealsa /var/lib/bluealsa
    sudo chmod 0700 /var/lib/bluealsa