Own 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. 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. The cost of parts cost me $ 12. The ease of assembly allowed me to assemble it in a couple of days. It works well and stably. Over 10 months of work, it hung only a couple of times, and then only because of my experiments. Convenient and thoughtful interface allows you to control from a 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 leading. 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 need to stream my audio archives only to the Internet receiver. Of course, there is an option to connect an audio player or ipod to the speakers and not steam. But this is not our way! I hate connecting connectors)



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



I have a paid hosting of 10 GB. I decided to write a script on something to 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 a letter came from the hosting administration. It talked about exceeding the limit of processor minutes and the need to switch to a higher tariff. The script had to be removed and abandoned this option.



How did it happen? I can’t live without a radio. If you are not allowed 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. For a server, it’s fine. I put Ubuntu 12.04. Then install Apache2 and php 5.3, samba. My server is ready.



I decided to try Icecast. I read a lot of mana on it. But it seemed complicated to me. And I decided to return to the option with a script in PHP. A couple of days were spent debugging this script. And it worked perfectly. Then I also wrote a script to play podcasts. And I liked it so much that I decided to make a small project. Called it IWScast. Posted on github .



image



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 mp3 directory files into an array. Creates an audio stream and substitutes the names of mp3 files. There are times when you listen to a song and you like it. Do you think who sings this? For this case, there is a record of the names of the tracks you listen to in the log log.txt

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: en\r\n" ) ) ); //Save to log $fl = $filePath; $log = date('Ymd 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 play in order, 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 memorizing podcast tracks. The next time you turn on the Internet receiver, the next podcast track is played. There is also a log of reproduced tracks.

You can specify the track number in the counter.dat file and podcast playback starts from it.



He wrote parsers for automatically downloading podcasts. He takes the last 4 tracks from RSS and downloads them. All this works great on a smartphone, IPTV set-top box, in a browser.



Recently in the morning I got the idea that it was great to memorize the playback position on the track. But I don’t know yet how to do this in PHP.



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



All Articles