During lockdown the team decided to do a fun project, Topic of music was brought up and we had an idea - Why not build a radio where everyone can add their tracks and it plays out random tracks.
So we wrote down the finer points of the requirements, it was pretty simple
- Has to be a radio where you can’t skip tracks and everybody is tuned to the same thing at the same time.
- Tracks have to be random enough for everybody to get a chance to hear their own tracks.
- Very simple to operate, just open the url and it should just work.
- Simple to operate and cheap
So we started looking into possible wokflows, The one that stood out the most and easiest to implement was a WebSocket based frontend - backend setup. Where the backend would send out messages over WS at connect and track_change along with playback start time so the player can automatically seek to the part and play. This would enable local playback to act as a radio.
Since we didn’t want to host the mp3 files ourselves, the easiest option was to use youtube as our playback stream. This was pretty easy as a lot of CLI based players like mpd etc used youtube and youtube-dl was an absolute beast of a library. Testing locally and a short visit to youtube-dl documentation we had what we needed pretty quickly, youtube-dl can easily pull out audio URL.
So we had all the information that was needed and had to write the code, It was pretty straightforward. We used the epoch timestamp to avoid timezone mess and was very handy doing seek calculations to sync new user with other users. The flow of the backend was pretty straightforward : Read Random Track from CSV > Lookup Audio URL from the Link > Send to clients and queue up another track once the current track playback is finished.
We used the duration information from youtube-dl to calculate the sleep time for the code and once back it would select a new track and broadcast its information to all the connected users.
If you would like to implement this in your own team, or want to improve / review the code. The project is available here : https://github.com/REAK-INFOTECH-LLP/company-radio/