Skip to content
sgtnastyPublic

About

A terminal UI player for SomaFM internet radio, written in Rust.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

28 Commits

Folders and files

Repository files navigation

somars

License: MIT Version

A terminal UI player for SomaFM internet radio, written in Rust.

Requirements

  • 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

Build & Run

cargo build --release
./target/release/somars

# or
cargo run --release

Key Bindings

Normal mode

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

Filter mode (after pressing /)

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

Architecture

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:

  1. On startup, api::fetch_channels() fetches https://api.somafm.com/channels.json synchronously and sorts stations alphabetically.
  2. The ratatui TUI is initialized with a crossterm backend (alternate screen, raw mode).
  3. The event loop polls for keystrokes every 100 ms and calls app.tick() to detect if mpv has exited.
  4. On Enter, player::Player::play() spawns mpv with --no-video and --input-ipc-server pointing to a per-process UNIX socket at /tmp/somars-<pid>.sock.
  5. Volume changes send JSON commands to the mpv IPC socket (set_property volume).
  6. Every ~2 seconds while a station is playing, app.tick() queries the IPC socket for metadata/by-key/icy-title to 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 the lastPlaying value from the initial API fetch.
  7. Stop kills the mpv child process and removes the socket file.
  8. Filter mode computes filtered_indices() on every keypress — a Vec<usize> of indices into the full channel list that match the query against title, genre, and description. The ListState cursor indexes into this filtered view; now_playing_idx always refers to the full list so the playing indicator survives filter changes.

Flow Diagrams

Event loop

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
Loading

tick internals (~100 ms)

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
Loading

API Schema

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.

About

A terminal UI player for SomaFM internet radio, written in Rust.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages