If I want to create a signal flow graph using small processing blocks connected via pipes, how could I most nicely implement a flow where I i.e. split the 2 channels of a stereo stream into 2 seperate pipes? In pseudocode I'd
genpcm-stereo | split -c 2 ..
[ ( lowpass | reverb ),
( highpass | reverb )
]
.. merge -c 2
Where split
should create 2 output streams where one channel would be
processed with lowpass pipe, while the other with highpass pipe. After that merge
would interleave the 2 streams again.
Is there some elegant simple way to write this in bash?
One option is to use named pipes. This Shellcheck-clean Bash code demonstrates one way to do it:
#! /bin/bash -p
mkfifo pipe{1..4}
genpm-stereo | splitchannels pipe1 pipe2 &
lowpass <pipe1 | reverb >pipe3 &
highpass <pipe2 | reverb >pipe4 &
mergechannels pipe3 pipe4 &
wait
rm pipe{1..4}
... &
) to prevent deadlocks due to pipe buffers being full or empty.splitchannels
is assumed to split its standard input into channels and write them to its two filename arguments.mergechannels
is assumed to read its two filename arguments, interleave the contents, and write the result to standard output.