Metadata-Version: 2.4
Name: headphone-controller
Version: 0.1.0
Summary: Control Nothing / CMF earbuds and Sony headphones from Linux: battery, noise control, EQ and more
Author-email: X27 <x27@xlabsx27.com>
License-Expression: GPL-3.0-or-later
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: PySide6>=6.5
Requires-Dist: dbus-fast>=2.0
Provides-Extra: dev
Requires-Dist: pytest; extra == "dev"
Dynamic: license-file

# Headphone Controller

A free and open-source Linux app for **Nothing / CMF** earbuds and **Sony** headphones. It shows battery and lets you change noise cancelling, EQ and device settings, with no phone or account needed.

It has a **Material You** interface that takes its colour scheme from your KDE accent colour. It follows Plasma's light/dark mode live and uses your system font and icons. It's built with Python and Qt 6 (PySide6), so it also runs on GNOME, XFCE, Hyprland, Sway and other desktops, on Wayland and X11. It talks to the headphones directly over Bluetooth RFCOMM through BlueZ.

> Independent community project, not affiliated with Nothing Technology or Sony. No warranty.

## Features

The app only shows the controls your device actually answers for. Anything it doesn't answer for stays hidden, so you won't see buttons that do nothing.

**Sony WH-1000XM4**
- **Battery:** level and charging state.
- **Noise control:** Noise cancelling, Wind noise reduction, Ambient sound (level 1–20, Focus on voice) and Off.
- **Equalizer:**
  - Presets: Bright, Excited, Mellow, Relaxed, Vocal, Treble/Bass Boost, Speech, Custom 1/2.
  - Manual 5-band EQ plus Clear Bass.
- **DSEE Extreme.**
- **Speak-to-chat:** on/off, sensitivity and how long it lasts.
- **Device settings:** Pause when taken off, automatic power off, touch panel on/off, and voice guidance.
- **Info:** firmware version and the active codec (SBC/AAC/LDAC).

**Nothing / CMF**
- **Battery:** left, right and case, or a single unit for Headphone (1). Shows charging state and whether each bud is in your ear.
- **Noise control:** Noise cancelling (Adaptive/High/Mid/Low, where the model has them), Transparency and Off.
- **EQ presets:** Balanced, More bass, More treble and Voice.
- **Optional features, where the model supports them:**
  - Audio codec (LDAC/LHDC)
  - Spatial audio
  - Bass enhance
  - Low latency mode
  - Dual connection
  - In-ear detection
  - Personalised sound
- **Find my earbuds:** plays a sound in the left/right bud.
- **Info:** model, firmware and serial number.

**App:**
- **Material 3 design:**
  - Navigation drawer with each device's battery.
  - A header card with battery rings.
  - Segmented buttons, filter chips, switches and EQ sliders.
  - Errors shown in a snackbar.
- **Colours:** dynamic colour from the KDE accent, or pick your own. The theme can be System, Light or Dark.
- **Tray icon:** battery in the tooltip and noise-mode switching from the menu. Closing the window keeps the app in the tray (you can turn this off).
- **Notifications:** when headphones connect, and a low-battery warning at a threshold you choose.
- **Start at login:** an XDG autostart entry that KDE lists under Autostart. Optionally starts minimised.
- **Bluetooth settings:** opens the KDE Bluetooth settings page for pairing.
- **Reconnects automatically:** when the headphones turn on.
- **`hpctl` command-line tool:** for scripting and for debugging new models.

## Supported devices

| Device | Status |
|---|---|
| Sony WH-1000XM4 | Supported (protocol V1) |
| Sony WH-1000XM3 | Experimental (same protocol as XM4) |
| Sony WH-1000XM5/XM6, WF-1000XM4/XM5, LinkBuds | Experimental (protocol V2, only used when the device advertises it) |
| Nothing Ear (3a), CMF Buds Pro 2, Nothing Headphone (1) | Supported |
| Nothing Ear, Ear (1), Ear (2), Ear (a), Ear (stick), CMF Buds | Experimental: shared protocol, needs field testing |

"Supported" means the protocol has been confirmed on that model by the reference projects. "Experimental" ones should work. If yours doesn't, please open an issue and include the output of `hpctl --dump status`.

## Install

### Packages

Download the latest `.rpm` or `.deb` from [Releases](https://git.xlabsx27.com/X27-Apps/Headphones-Controller/releases):

```sh
sudo dnf install ./headphone-controller-*.noarch.rpm   # Fedora 44
sudo apt install ./headphone-controller_*_all.deb       # Debian 13+, Ubuntu 24.10+
```

Both pull in PySide6, dbus-fast and BlueZ from your distro.

### From source

**Requirements:**
- Python 3.10+
- BlueZ (`bluetoothd` running)
- PySide6
- dbus-fast

On Fedora:

```sh
sudo dnf install python3-pyside6 python3-dbus-fast
./install.sh
```

On Debian/Ubuntu (24.04+) or Arch, install `python3-pyside6` / `pyside6` the same way. If dbus-fast isn't packaged for your distro, pip installs it.

`./install.sh` installs into `~/.local`, adds **Headphone Controller** to your app menu and puts `headphone-controller` and `hpctl` on your PATH. To remove it, run `./install.sh --uninstall`.

To run from the source tree without installing:

```sh
PYTHONPATH=src python3 -m hpc          # GUI (add --hidden to start in the tray)
PYTHONPATH=src python3 -m hpc.cli list
```

## Usage

1. Pair the headphones once in your system's Bluetooth settings.
2. Connect them as usual.
3. Open Headphone Controller. It finds them and connects by itself.

To start the app in the tray at login, add `headphone-controller --hidden` to your desktop's autostart.

### Command line

```sh
hpctl list                               # paired supported devices
hpctl status                             # everything the device reports
hpctl noise anc                          # noise cancelling
hpctl noise ambient --ambient 15 --voice # Sony: ambient level 15, focus on voice
hpctl noise anc --level 4                # Nothing: adaptive ANC
hpctl eq "Bass Boost"
hpctl set dsee on
hpctl set auto_power_off taken_off
hpctl -d AC:80:0A:DA:59:4C status        # pick a device by address
hpctl --dump status                      # log raw frames (for bug reports)
```

## Troubleshooting

- **"Couldn't talk to the headphones":** only one app can hold the control channel at a time. Close other controllers such as nadamas, Gadgetbridge-style bridges or Sony's app on a connected phone, then click **Retry now**.
- **The device shows but never connects:** make sure it's connected for audio first. The app only opens its control channel when BlueZ reports the device as connected.
- **Debug output:** `headphone-controller --debug` or `hpctl --dump status` prints every frame sent and received.

## How it works

The app registers a BlueZ `Profile1` for the vendor's RFCOMM service UUID and asks BlueZ to connect it. BlueZ does the SDP lookup and hands over the socket, so it needs no `sdptool`, PyBluez or root. If a device doesn't advertise the UUID, the app falls back to probing RFCOMM channels.

| | Sony | Nothing / CMF |
|---|---|---|
| Service UUID | `96CC203E-…` (V1), `956C7B26-…` (V2) | `AEAC4A03-DFF5-498F-843A-34487CF133EB` |
| Frame | `3E` type seq len:4 payload sum `3C`, escaped with `3D` | `55` ctrl:2 cmd:2 len:2 fsn payload CRC-16/MODBUS, little-endian |
| Flow | Stop-and-wait with ACK, seq alternates 0/1 | Request `0xC0xx`/`0xF0xx`, reply with bit 15 cleared, events `0xE0xx` |

For safety, on Sony V1 devices (XM3/XM4) opcode `0x22` means *power off*, so the session refuses to send it.

Code layout:
- `src/hpc/protocols/`: framing, sessions and drivers.
- `src/hpc/manager.py`: device detection and reconnects.
- `src/hpc/ui/`: the Qt interface.
  - `theme.py`: the Material You colour scheme, built from the accent colour in OKLCH.
  - `widgets.py`: the Material 3 components.
- `tests/`: the codec tests and full driver runs against simulated devices (`python -m pytest`).

## Credits

The protocol knowledge comes from these projects:

- [ezvk/nadamas](https://github.com/ezvk/nadamas) and [chukfinley/nada](https://github.com/chukfinley/nada) for Nothing / CMF.
- [LZmx/sony-device-center](https://github.com/LZmx/sony-device-center) and [Gadgetbridge](https://codeberg.org/Freeyourgadget/Gadgetbridge) for Sony.

This app's code is an independent implementation.

## Building packages

`packaging/build-packages.sh` builds the `.rpm` (in a Fedora container) and the `.deb` (in a Debian container) from the committed tree into `dist/`. It needs podman or docker.

## License

GPL-3.0-or-later. See [LICENSE](LICENSE).
