Bash Radio Player
Tags: bash, linux, tooling, programming
Published: 2024-03-25 20:40:46

## INTRO

A background playing radio on the `tmux` screen is an essential for anyone performing code at the terminal for extended periods of time. I made the decision to build a terminal radio for this reason.

In the process of figuring out how to accomplish this without using up too many system resources, I found that combining several terminal programs might create a radio player.

This simple project is a nice application project to show what can be done with simple applications for those who are proficient in the 'bash' language and who like to deal with the Linux terminal.

The folder tree of the project is as follows:

```bash 
.
├── extras
│   └── zsh-add.sh
├── install.sh
├── listen.sh
├── README.md
└── src
    ├── foreign_stations.csv
    ├── radyodelisi.csv
    └── requirements.txt
```

# Construction of the Project

## Pre Requirements

Before you start coding, some applications must be installed on your system:

- fd-find: File finder
- fzf: Fuzzy File Finder
- ripgrep: Character and word location finder within the file
- tmux: Terminal multiplexer
- mpv: Terminal video and music player

Additionally, any text editor is required for coding. Personally, I use 'Neovim', but you can do this in any editor. 

### Installing Required Libraries and Applications

We create a file named 'requirements.txt', which contains the necessary libraries. We paste the code below, save and close.

```bash 
fd-find 
fzf
mpv
ripgrep
tmux
```

We will create the code that will automatically install the libraries we added to this file by creating an `install.sh` file as follows.

```bash 
#!/bin/bash
xargs -a ./src/requirements.txt sudo apt-get install
```
The above code will read the contents of the file with `xargs` and transfer it to `sudo apt-get install`. The `-a` statement specifies that the values will be retrieved from a file in the specified path.

We save and close this file.

### Creating Station Records

First of all, the file types with which we can simply list the stations in the system are csv and json file types. Since I like working with csv, I wrote the stations into a csv file and saved them in the following format. Thus, a list of 1000 Turkish radio stations emerged as follows:

```csv 

101 TURHAL FM,https://ssl.radyosfer.com/101turhalfm/stream
103.6 Radyo Light,https://yayin.radiolight.net:8005/live
112 ACiL FM,http://95.173.185.128:9316/;
90'LAR,http://37.247.98.8/stream/166/
90'LAR,https://moondigitalmaster.radyotvonline.net/90lar/playlist.m3u8
A HABER RADYO,https://trkvz-radyolar.ercdn.net/ahaberradyo/playlist.m3u8
A NEWS RADYO,https://trkvz-radyolar.ercdn.net/anewsradyo/playlist.m3u8
A PARA RADYO,https://trkvz-radyolar.ercdn.net/apararadyo/playlist.m3u8
A SPOR RADYO,https://trkvz-radyolar.ercdn.net/asporradyo/playlist.m3u8
...

```

We create the file and save it as csv in the folder named src. Now we carry out the second stage of reading the file.

### Reading the File and Playing the Selected Station

Since I took and edited the compiled links from a source called radiodelisi.com, the name of the file to be read appears as 'radyodelisi.csv'. Type the name of your csv file containing your own radio stations. Now we create a bash file named `listen.sh` and enter the following code into it:

```bash 
selected_station=$(awk -F ',' '{ print $1 }' ./src/radyodelisi.csv | fzf --prompt="Select a radio station: ")
if [ -n "$selected_station" ]; then
    station=$(awk -F ',' -v station="$selected_station" '$1 == station { print $2 }' ./src/radyodelisi.csv)
    mpv "$station"
fi
```

With 'awk', we will display the data in a certain column from our csv file on the screen. With the help of `-F`, we indicate that the columns are separated by the `,` sign and that we will display the data in the first column with `{ print $1 }`. Then we specify the path to the file we created.

We transfer the values here into zf with the pipe operator `|`. We define all these values we wrote into a variable named `selected_station`. The station we chose will be located here.

Now we create an if loop. If we selected a value, we say perform the following operations.

```bash 
if [ -n "$selected_station" ]; then
    ...
fi
```

If the radio station we selected here matches a value in the first column in our file, we say return the 'URL' value in the second column. We search for values in the directory with the Excel 'VLOOKUP' function.

```bash 
if [ -n "$selected_station" ]; then
    station=$(awk -F ',' -v station="$selected_station" '$1 == station { print $2 }' ./src/radyodelisi.csv)
    mpv "$station"
fi
```

Now our interface is ready. Only the testing phase remains.

## Test Stage 


{% embed https://www.youtube.com/watch?v=hldEzlPEAFY %}