Category Archive Technology

EZSTREAM for streaming

ezstream is a command line source client software that can stream audio with/without reconding to an icecast or shoutcast server.

In its basic mode of operation, it streams media files or data from standard input without reencoding and thus requires only very little CPU resources. It can also use various external decoders and encoders to reencode from one format to another, and then stream the result to an Icecast server. I’m using it to stream a list of mp3 files to a webradio server. My use case is:

I want to stream the podcasts that I host in a folder, with some auto-update mechanism, so that If a new podcast is added, then it’s played (sooner or later).

ezstream can be used for streaming with a configuration file and it can be launched with:

ezstream -c mystream.xml

where mystream.xml is the configuration file

EZSTREAM configuration

ezstream use an xml file for configuration. Mine is very simple and I’ve used a modified version of the example that ezstream itself provides. Here’s the configuration I’m using:

<format>MP3</format>
<filename>/myfolder/playlist.txt</filename>
<stream_once>0</stream_once>

format: is the file format

filename: it’s the name and the path of a text file that contains a path of every single podcast, one per line

stream_once: zero (0) stream and then restart the list, one (1) stream the list once.

In the file there are other configurations: <url>, <sourcepassword>, <svrinfobitrate> but I think these are trivial.

The playlist

ezstream provide a very usefull feature to shuffle a list of file, it’s documented so you can check it with -h flag, and you can run it with:

ezstream -s playlist.txt

this command gets the playlist.txt input file, shuffles it, and sends it to the stdoutput, so it writes the shuffled list to the console. You can redirect the stdoutput to a file so that the list is shuffled. But, to be honest, I need something similar… but different. I want a shuffled version of the list but every X podcast, I want to insert two sounds, two musical cuts. So I’ve used a scripting language to generate the playlist. Unfortunately for you, I’ve used php, but let’s just analyze the logic:

<?php
$baseFolder = "/music/";
$cutArray = array("jingle.mp3","spot.mp3");
$playlistFile = "/myfolder/playlist.txt";
$everySong =3;

if (file_exists($playlistFile)){
    unlink($playlistFile);
}

foreach (glob($baseFolder . '*.mp3', GLOB_NOSORT) as $file) {
     $tmp = basename($file);
     if (strpos($tmp, 'news-automatiche') === false) {
         $allfile[] = $baseFolder . basename($tmp);
     }
}
shuffle($allfile);

$f = fopen($playlistFile, "w+");
foreach ($allfile as $index => $file) {
    if ($index % $everySong == 0) {
        foreach ($cutArray as $sng) {
            fwrite($f, $sng . PHP_EOL);
        }
     }
     fwrite($f, $file . PHP_EOL);
}
fclose($f);

I check if the file exists and, if yes, I delete it. Then I scan the folder $baseFolder for mp3 files and store them in an Array (with full path).

The array is shuffled with shuffle($allfile)

Then I write the playlist file line by line, and with % (mod mathematic operation), every 3 podcast files (defined with the variable $everySong), I write the two musical cut sounds.

Shuffle and update playlist

How can I reload the playlist with the new added podcasts? I’ve written a bash script that I run with cron:

Every 12 hours the cronjob runs the php script to write a new playlist file. This generated playlist file includes all the podcasts, including the added ones. Then, with the usage of sig -HUP signal mechanism of ezstream, I can reload this new playlist.

#!/bin/bash

SERVICE="ezstream"
COMMAND="pidof $SERVICE"
EZSTREAMPID=$(eval $COMMAND)

# Randomize playlist and insert sounds
php createPlaylist.php

 # Rereads the playlist file
kill -HUP $EZSTREAMPID

The main point here is kill -HUP $EZSTREAMPID: this command sends signal -HUP to the running ezstream. This signal:

  1. Rereads the playlist file
  2. Checks, in the new playlist file, where is the current playing podcast file
  3. If the current playing podcast file exists, the next song will be the one below it. Otherwise the next song will be the first of the playlist file

Conclusion

The need for an icecast client player that can stream podcast files with an update functionality is solved with ezstream and some lines of code, easy to read and to configure for your needs.

From audio to video with ffmpeg

A video content is something that social media and website used widely. Also a Video content it’s some more engaging and with a better reaction than a simple audio. Working on a webradio, I’m always looking for some solution that can engage people in order to attract people so my Idea was to modify audio content to create a video content.

But how you can create a video content from a video content?

ffmpeg has a huge collection of functions, one of these is waveform that can generate a video from an audio, the video simply shows the waveform of the audio. It’s good, but for me this is just a starting point

ffmpeg -i input_audio.mp3 -filter_complex \
"[0:a]showwaves=s=1280x100:colors=Red:mode=cline:rate=25:scale=sqrt[outputwave]" \
output.mp4

I’m using here filter_complex because I want a chain of filters, and this is only one of the steps. Let me explain the parameters:

[0:a] this means: from the stream 0 take the audio…

showwaves=s=1280×100 this is the filter, showwave, and the output format 1280×100

colors=Red the color of the waveform….

mode=cline the type of the waveform, line…..

rate=25 … how many frame

scale=sqrt …the scale. sqrt is good

[outputwave] this is a placeholder….we will use it later in the chain.

But what I want is a video, where the waveform it’s only an overlay level on it. The base video could be a pre-created video that can be used for all the audio content. It could be a slideshow video, something created in graphic, a static foto or with simple animation. So let’s suppose that this video exists. But How can this video fit the audio length, that can be variable?

How can, a pre-created video, fit the audio length of an audio file with a variable length?

My idea is to loop the pre-created video for the length of the audio and overlay on it, the waveform created from the audio file!

And I want this in a single ffmpeg command! And so…

ffmpeg -stream_loop -1 -i video.mp4 \
-i input_audio.mp3 -filter_complex \
"[1:a]showwaves=s=1280x100:colors=Red:mode=cline:rate=25:scale=sqrt[outputwave]; \
[0:v][outputwave] overlay=0:main_h-overlay_h:shortest=1 [out]" \
-map '[out]' -map '1:a' -c:a copy -y \
output.mp4

-stream_loop -1 -i video.mp4 This loops the input video, video.mp4, idefinetly*
-i input_audio.mp3….outputwave] we already covered this….
[0:v][outputwave] overlay=0:main_h-overlay_h:shortest=1 [out] this get the video of the first video, with [0:v] and the video of the waveform, [outputwave] as the input for the overlay filter, and put it in the position bottom with 0:main_h-overlay_h. The magic happens with shortest=1 that means: do this overlay for the length of the shorter. *The loop is infinitely long but the waveform no! And this means: for the length of the waveform!

-map ‘[out]’ -map ‘1:a’ -c:a copy -y this map the output as a video and copy the audio of the

This is a frame of the final video:

Listen microphone with your computer speaker: Audio monitor (for Mac)

How to listen your microphone with your speaker? How can you direct, or redirect, input from microphone to a different audio device?

Using your mac there is a freeware that you can download, it’s called Audio Monitor. The MTCoreAudio framework is a Cocoa-flavored Objective-C wrapping around the Hardware Abstraction Layer (HAL) of Apple’s CoreAudio library.

As you can see here you can send audio from your microphone to your speakers, as you can imagine this means also that there’ll be a lot of echo, but another things that you can do is send audio from your microphone to another output device. In conjunction with a setup like the one described here: Audio virtual device for MAC you can enable a lot of configuration like send your microphone audio and music to another listener connected with you with skype.

 

Audio virtual device for MAC

If you play with streaming, podcast, mixing, webradio or you want to setup an audio recording studio at home you may find difficult to get audio from some sources and record or use in your live set. 

While in a real analog world, this can be solved with cables and mixers, by splitting some output to other inputs and similar stuff, in a digital world this seems difficult and it’s complicated, for example, record the audio from the browser, or get the audio from skype or similar. There are some solution that I want to share with you, 2 out of 3 of them are completely free:

Soundflower

FREEWARE. MacOS system extension that allows applications to pass audio to other applications. Soundflower works on macOS Catalina. Follow the instruction at this link (official link) after a restart you’ll see 2 new output device available on your menu. You can set the output to soundflower and set soundflower as input, in your record software and done!

 

 

 

 

BlackHole

FREEWARE. BlackHole is a modern macOS virtual audio driver that allows applications to pass audio to other applications with zero additional latency. Follow the instruction at this link (official link). It works just like Soundflower: it adds a new virtual device Blackhole 16ch and you can use the input of Blackhole 16ch to get the audio of every app, just setting the output to Blackhole 16ch.


 

 

 

 

 

 

 

LOOPBACK

99$, 14 days trial. It creates virtual audio devices to take the sound from apps and audio input devices, then pass it to any audio processing software. With an easy-to-use wire-based interface, Loopback gives you the power of a high-end studio mixing board, right inside your computer. Details here while Loopback is not a free software, it has the power of super easy to understand user interface where you can configure multiple sources as a new virtual device.

And then you can use this new created virtual device in your recording software

My use case it’s something a little bit complicated than record from an internal source in my computer: I want to stream my webradio show from home, and have someone else talking with me in the show, but he/she is at home. What I’m using is MIXX a free software that can easily stream audio to shoutcast or icecast server, you can add multiple microphones (up to 4 microphones) so I want that Skype output is seen as a microphone, to do this I’ve installed Soundflower and used as output device in Skype

And set Soundflower 2ch as mic in MIXX

And it works! This is just a use case, but you can solve a lot of problem with these solutions. Then you probably want also and additional feature like: sending a source to two different device (for example real speakers and one of the virtual device) so that you can listen to what you are recording, or using as a source (the skype virtual microphone in my use case). This can be done using the built in Audio device of the Apple MAC, just open Audio device click on + button on the left bottom corner and select “create multi output device”

You can then select something like Built-in output (the speakers) and Soundflower output (set Drift correction, better result)

When Multi-output device is selected from the sound menu….

..everything that is sent to the Built-in output will also be sent to the Soundflower.

Automatic check for unbalanced audio, or bad level

I have a radio show and I’m also one of the people much involved in all the tech-related stuff in the Radio. There are multiple system that automatically, and manually, record the podcast of a show, upload it online and similar stuff.

Sometimes the recorded podcasts have some problems, typically:

  • The audio of the left channel is unbalanced with the respect of the right channel, or viceversa (Unbalanced volume level)
  • The audio is too high
  • The audio is too low

It may happen because the analogic audio system, before the audio card sampler, was misconfigured, or someone move some knobs, or maybe the cable are too long and of bad quality. Anyway, when this happen, there is no way I can understand it other than listen to all the recorded files. 

We are around 40 speakers, we broadcast liver from 8:00 to 24:00… it’s not something that a human want (and can) do. So for this type of problem computer systems is the way. I need

something that automatically can understand if a file is unbalanced, too high or too low in volume

I think that Linux is a super operating system and it’s full of library, tool, script and community that can help you find the right solution, but sometimes it’s not so simple. The siwss-knife I was looking for is called sox. Sox is a command line tool that can do tons of stuff (see here for the documentation). What I used for this is the stats option

sox FILE_NAME.mp3 -n stats

stats returns a lot of info, and what I was looking at was the RMS db level, global and for each channel (left right). To filter out these result we can use a simple grep command:

sox FILE_NAME.mp3 -n stats | grep 'RMS lev dB'

And with some ‘magic’ get the numeric info about TOTAL level, LEFT and RIGHT level:

LINE=$(sox FILE_NAME.mp3 -n stats | grep 'RMS lev dB')

TOTAL=$(echo $LINE | tr -s ' ' | cut -d ' ' -f 4)
LEFT=$(echo $LINE | tr -s ' ' | cut -d ' ' -f 5)
RIGHT=$(echo $LINE | tr -s ' ' | cut -d ' ' -f 6)
BAL_DIFF=$(echo "($LEFT)-($RIGHT)" | bc)

What we have done here? Saved the output of the sox command to the variable LINE:

LINE=$(sox FILE_NAME.mp3 -n stats | grep 'RMS lev dB')

and then we’ve parsed it 3 times:

TOTAL=$(echo $LINE | tr -s ' ' | cut -d ' ' -f 4)
LEFT=$(echo $LINE | tr -s ' ' | cut -d ' ' -f 5)
RIGHT=$(echo $LINE | tr -s ' ' | cut -d ' ' -f 6)

and stored the LEFT/RIGHT difference to a variable BAL_DIFF

BAL_DIFF=$(echo "($LEFT)-($RIGHT)" | bc)

from now on what you need to do with these value is up to you, you can check if values are less then a threshold and send an email, store the value somewhere, push an alert.

 

WebRadio, FM Radio, streaming and similiar stuff. A Webinair

Mirobarsa, speaker at https://www.radiocittaperta.it with a 30 minutes Webinair about: WebRadio, FM Radio, streaming and similiar stuff. Also some usefull resources on How to start your webradio.