A terminal UI player for SomaFM internet radio, written in Rust.
- mpv — used for audio decoding and playback (routes through PipeWire automatically on modern Linux)
- Network access to
api.somafm.com
Install mpv on Arch Linux:
sudo pacman -S mpv
cargo build --release
./target/release/somars
# or
cargo run --release| Key | Action |
|---|---|
↑ / k |
Move selection up |
↓ / j |
Move selection down |
Enter |
Play selected station |
s |
Stop playback |
← / h |
Volume down 5% |
→ / l |
Volume up 5% |
/ |
Enter search/filter mode |
q |
Quit |
| Key | Action |
|---|---|
| type | Narrow station list (matches title, genre, description) |
↑ / ↓ |
Navigate filtered results |
Enter |
Play highlighted station and exit filter |
Esc |
Clear filter and return to full list |
src/
├── main.rs — entry point: mpv check, channel fetch, TUI event loop
├── api.rs — HTTP fetch + JSON deserialization of channels.json
├── app.rs — application state and input actions
├── player.rs — mpv subprocess management + IPC socket control
└── ui.rs — ratatui rendering (station list + detail panel + status bar)
Flow:
- On startup,
api::fetch_channels()fetcheshttps://api.somafm.com/channels.jsonsynchronously and sorts stations alphabetically. - The ratatui TUI is initialized with a crossterm backend (alternate screen, raw mode).
- The event loop polls for keystrokes every 100 ms and calls
app.tick()to detect if mpv has exited. - On
Enter,player::Player::play()spawnsmpvwith--no-videoand--input-ipc-serverpointing to a per-process UNIX socket at/tmp/somars-<pid>.sock. - Volume changes send JSON commands to the mpv IPC socket (
set_property volume). - Every ~2 seconds while a station is playing,
app.tick()queries the IPC socket formetadata/by-key/icy-titleto get the current song from the stream's ICY metadata. This updates the Now Playing field in the detail panel in real time. Before the first successful poll, the field falls back to thelastPlayingvalue from the initial API fetch. - Stop kills the mpv child process and removes the socket file.
- Filter mode computes
filtered_indices()on every keypress — aVec<usize>of indices into the full channel list that match the query against title, genre, and description. TheListStatecursor indexes into this filtered view;now_playing_idxalways refers to the full list so the playing indicator survives filter changes.
flowchart TD
A([Start]) --> B{mpv in PATH?}
B -- No --> C([Exit with error])
B -- Yes --> D["Fetch channels\napi.somafm.com"]
D --> E[Init ratatui TUI]
E --> F[Render frame]
F --> G[app.tick]
G --> H{Key event\nwithin 100 ms?}
H -- No --> F
H -- "Yes · filter mode" --> I{Key}
H -- "Yes · normal mode" --> J{Key}
I -- "Esc" --> ClearFilter[clear_filter] --> F
I -- "Enter" --> PlayExit[exit_filter + play_selected] --> F
I -- "Backspace" --> PopChar[pop_filter_char] --> F
I -- "char" --> PushChar[push_filter_char] --> F
I -- "↑ / ↓" --> NavF[prev / next] --> F
J -- "q" --> K([Restore TUI · exit])
J -- "/" --> EnterFilter[enter_filter] --> F
J -- "↑↓ / jk" --> Nav[prev / next] --> F
J -- "Enter" --> Play[play_selected] --> F
J -- "s" --> Stop[stop] --> F
J -- "← → / hl" --> Vol[volume_down / up] --> F
flowchart TD
A([tick]) --> B[player.check_alive]
B --> C{mpv exited\non its own?}
C -- Yes --> D["Clear now_playing_idx\nstatus ← 'Stream ended'"]
C -- No --> E{"is_playing &\ntick % 20 == 0?"}
D --> Z([done])
E -- No --> Z
E -- Yes --> G["Query mpv IPC\nmetadata/by-key/icy-title"]
G --> H{Title received?}
H -- No --> Z
H -- Yes --> I[Update now_playing_title] --> Z
Station data comes from the unofficial SomaFM JSON endpoint:
GET https://api.somafm.com/channels.json
Response shape:
{
"channels": [
{
"id": "groovesalad",
"title": "Groove Salad",
"description": "A nicely chilled plate of ambient/downtempo beats and grooves.",
"dj": "SomaFM",
"djmail": "dj@somafm.com",
"genre": "ambient",
"listeners": 1899,
"lastPlaying": "Artist Name - Track Title", // shown until live ICY metadata arrives
"image": "https://somafm.com/img3/groovesalad-400.jpg",
"largeimage": "https://somafm.com/img3/groovesalad-400.jpg",
"xlimage": "https://somafm.com/img3/groovesalad-1500.jpg",
"updated": "1234567890",
"playlists": [
{
"url": "https://somafm.com/groovesalad256.pls",
"format": "mp3",
"quality": "highest"
},
{
"url": "https://somafm.com/groovesalad.pls",
"format": "mp3",
"quality": "high"
},
{
"url": "https://somafm.com/groovesalad32.pls",
"format": "aac",
"quality": "low"
}
]
}
]
}Stream quality values in priority order: highest → high → low. The playlists[].url is a .pls playlist file pointing to the actual stream URL; mpv handles .pls files natively.
Note: This endpoint is unofficial. SomaFM discontinued third-party API access. It works as of mid-2026 but may change without notice. Consider supporting SomaFM at somafm.com.