Nothing Special   »   [go: up one dir, main page]

DEV Community

Cover image for Bash Radio Player
gokayburuc.dev
gokayburuc.dev

Posted on

Bash Radio Player

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:

.
├── extras
│   └── zsh-add.sh
├── install.sh
├── listen.sh
├── README.md
└── src
    ├── foreign_stations.csv
    ├── radyodelisi.csv
    └── requirements.txt
Enter fullscreen mode Exit fullscreen mode

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.

fd-find 
fzf
mpv
ripgrep
tmux
Enter fullscreen mode Exit fullscreen mode

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

#!/bin/bash
xargs -a ./src/requirements.txt sudo apt-get install
Enter fullscreen mode Exit fullscreen mode

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:


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
...

Enter fullscreen mode Exit fullscreen mode

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:

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
Enter fullscreen mode Exit fullscreen mode

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.

if [ -n "$selected_station" ]; then
    ...
fi
Enter fullscreen mode Exit fullscreen mode

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.

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

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

Test Stage

Top comments (4)

Collapse
 
moopet profile image
Ben Sinclair

Looks cool. Do you have a link to it?

Collapse
 
gokayburuc profile image
gokayburuc.dev
Collapse
 
gokayburuc profile image
gokayburuc.dev

Updated Video :

Radio


New Features

With New Dashboard i added these features given below:

  1. Add Radio Station
  2. Remove Radio Station
  3. List All Stations
  4. Listen Radio
Collapse
 
gokayburuc profile image
gokayburuc.dev

BTW : as New Feature i added dashboard. You can add,delete,view, listen radio stations in a dashboard screen.