Categories
Dirk Jan Buter

Liquidsoap Pipe Filter

I was looking for a system in Liquidsoap to schedule a talking clock at the whole hour for our web radio station, but I could not find any satisfying solution. That is the reason I post this example of how to make a pass through filter in the c-language.

Liquidsoap is a language for describing audio and video streams. It offers a collection of operators that you can combine at will, for creating or transforming (radio) streams.

You can uses this filter with the following code in a Liquidsoap script:

s = pipe(process="/home/dirkjan/astradio/src/pipefilter/pipefilter", s)

This is the c code:

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdint.h>

typedef struct WAVEHEADER {
	uint8_t chunkID[4];
	uint32_t chunkSize;
	uint8_t format[4];
	uint8_t subchunk1ID[4];
	uint32_t subchunk1Size;
	uint16_t audioFormat;
	uint16_t numChannels;
	uint32_t sampleRate;
	uint32_t byteRate;
	uint16_t blockAlign;
	uint16_t bitsPerSample;
	uint8_t subchunk2ID[4];
	uint32_t subchunk2Size;
} WAVEHEADER;

int main(int argc,char * argv[])
{
    WAVEHEADER waveHeader;
    uint16_t sample;

    // Read wave header from stdin
    fread(&waveHeader, sizeof(WAVEHEADER), 1, stdin);

    // Write wave header to stdout
    fwrite(&waveHeader, sizeof(WAVEHEADER), 1, stdout);

    while(!feof(stdin))
    {
	fread(&sample, sizeof(uint16_t), 1, stdin);
	if(waveHeader.bitsPerSample == 16)
	{
		double f = (double)sample * 1.0 / (double)0x7FFF;

		// Manupulate audio here

		sample = (uint16_t)(f * (double)0x7FFF / 1.0);
	}
	fwrite(&sample, sizeof(uint16_t), 1, stdout);
    }

    return 0;
}

Compile it with gcc in the following way:

gcc pipefilter.c -o pipefilter

Thank you and I hope it will be useful for you!

Dirk Jan Buter