Your internet radio

Many of us like to listen to the radio in the morning. And then one fine morning I realized that I did not want to listen to local FM radio stations. Not interested. But the habit turned out to be harmful. And I decided to replace the FM receiver with an Internet receiver. I quickly bought parts on Aliexpress and assembled an Internet receiver.

About the internet receiver. The heart of the receiver is the ESP32 microcontroller. Firmware from KA-radio. Parts cost me $12. The ease of assembly allowed me to assemble it in a couple of days. Works well and stable. For 10 months of work, it hung only a couple of times, and then only because of my experiments. A convenient and thoughtful interface allows you to control it from your smartphone and computer. In a word, this is a wonderful Internet receiver.

Everything's OK. But one early morning I came to the conclusion that with access to tens of thousands of radio stations, there are no interesting stations. I was annoyed by advertising, stupid jokes of the hosts. Constantly jumping from one station to another. I like Spotify and Yandex.Music. But the sad thing is that they do not work in my country. And I would like to listen to them through the Internet receiver.

I remembered my childhood. I had a tape recorder and two dozen cassettes. Cassettes changed with friends. And it was wonderful. I decided that I needed to stream my audio archives only to an Internet receiver. Of course, there is an option to connect an audio player or ipod to the speakers and not worry. But this is not our way! I hate to connect connectors)

I started looking for solutions. There is an offer on the market to create your own Internet radio from Radio-Tochka.com. I tested 5 days. Everything worked fine with my internet receiver. But the price was not attractive to me. Refused this option.

I have paid hosting 10 GB. I decided to write a script on something that would stream the audio stream of my mp3 files. I decided to write in PHP. Quickly wrote and launched. Everything worked. It was cool! But a couple of days later I received a letter from the hosting administration. It talked about exceeding the limit of processor minutes and the need to upgrade to a higher tariff. The script had to be removed and this option abandoned.

How did it happen? I can't live without radio. If they do not allow you to run the script on someone else's hosting, then you need your own server. Where I will do what my soul desires.

I have an ancient netbook without a battery (CPU - 900 MHz, RAM - 512 Mb). The old man is 11 years old. Perfect for a server. I install Ubuntu 12.04. Then I install Apache2 and php 5.3, samba. My server is ready.

Decided to try Icecast. I read a lot of mana on it. But I found it difficult. And I decided to return to the PHP script option. A couple of days were spent debugging this script. And everything worked great. Then I also wrote a script to play podcasts. And I liked it so much that I decided to do a small project. Called it IWScast. Posted on github.

Your internet radio

Everything is very simple. I copy the mp3 files and the index.php file to the Apache root folder /var/www/ and they are randomly played. About 300 songs are enough for the whole day approximately.
The index.php file is the script itself. The script reads all the names of the mp3 files in the directory into an array. Creates an audio stream and substitutes the names of mp3 files. There are moments when you listen to a song and you like it. Who do you think sings it? For such a case, there is a record of the names of the tracks listened to in the log.txt log
Full script code

<?php
set_time_limit(0);
header('Content-type: audio/mpeg');
header("Content-Transfer-Encoding: binary");
header("Pragma: no-cache");
header("icy-br: 128 ");
header("icy-name: your name");
header("icy-description: your description"); 
$files = glob("*.mp3");
shuffle($files); //Random on

for ($x=0; $x < count($files);) {
  $filePath =  $files[$x++];
  $bitrate = 128;
  $strContext=stream_context_create(
   array(
     'http'=>array(
       'method' =>'GET',
       'header' => 'Icy-MetaData: 1',
       'header' =>"Accept-language: enrn"
       )
     )
   );
//Save to log 
  $fl = $filePath; 
  $log = date('Y-m-d H:i:s') . ' Song - ' . $fl;
  file_put_contents('log.txt', $log . PHP_EOL, FILE_APPEND);
  $fpOrigin=fopen($filePath, 'rb', false, $strContext);
  while(!feof($fpOrigin)){
   $buffer=fread($fpOrigin, 4096);
   echo $buffer;
   flush();
 }
 fclose($fpOrigin);
}
?>

If you want the tracks to be played in order, then you need to comment out the line in index.php

shuffle($files); //Random on

For podcasts I use /var/www/podcast/ There is another index.php script. It has a memorization of podcast tracks. The next time you turn on the Internet receiver, the next track of the podcast is played. There is also a log of the tracks being played.
In the counter.dat file, you can specify the track number and podcast playback will start from it.

Wrote parsers for automatic download of podcasts. It takes 4 latest tracks from RSS and downloads them. All this works great on a smartphone, IPTV set-top box, in a browser.

The thought occurred to me recently this morning that it was great to be able to remember the playback position on a track. But I don't know yet how to do it in PHP.

The script can be downloaded github.com/iwsys/IWScast

Source: habr.com

Add a comment