Skip to content

Set of scripts and services to better handle pipe creation for audio streamers

License

Notifications You must be signed in to change notification settings

ale275/audio2pipe

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 

Repository files navigation

audio2pipe

Release GitHub commit activity Issues Contributors project_license Stargazers Forks

bash python

audio2pipe is a set of utilities to pipe sound towards other software

audio2pipe is a set of scripts and services to better handle pipe creation for audio streamers leveraging cpiped under the hood. Recommended in conjunction with OwnTone

Contents

Features

  • systemd service to manage cpiped instances supporting different sample rates
  • Multiple input device supported with dedicated configuration
  • Interlock to avoid out of sequence triggering of per device script
  • Automatic OwnTone output selection on sound detection

Getting Started

Prerequisites

audio2pipe requires

Installation

  1. Clone the repository

    git clone https://github.com/ale275/audio2pipe.git
  2. Install scripts in audio2pipe base dir defined by A2P_HOME env variable and setup needed symlink

    cd audio2pipe
    export A2P_HOME=/home/user
    install  -m 755 -D scripts/* -t "${A2P_HOME}/bin"
    ln -s "${A2P_HOME}/bin/A2PSourceProcess.sh" "${A2P_HOME}/bin/A2PSourceProcessPre"
    ln -s "${A2P_HOME}/bin/A2PSourceProcess.sh" "${A2P_HOME}/bin/A2PSourceProcessDetect"
    ln -s "${A2P_HOME}/bin/A2PSourceProcess.sh" "${A2P_HOME}/bin/A2PSourceProcessSilence"
    ln -s "${A2P_HOME}/bin/A2PSourceProcess.sh" "${A2P_HOME}/bin/A2PSourceProcessStop"
    ln -s "${A2P_HOME}/bin/A2PToggle.sh" "${A2P_HOME}/bin/A2PToggle"

    To configure autmatic output selection on sound detect please refer to scripts/A2POutputselector.py section.

  3. Create per device systemd service file starting from template. More details in configuration section

    export A2P_HOME=/home/user
    export A2P_DEVNAME=Test_Device
    cp "service/cpiped_Template.service" "service/cpiped_${A2P_DEVNAME}.service"
    nano "service/cpiped_${A2P_DEVNAME}.service"
  4. Install and activate the service

    export A2P_HOME=/home/user
    export A2P_DEVNAME=Test_Device
    sudo cp "service/cpiped_${A2P_DEVNAME}.service" "/lib/systemd/system/"
    sudo systemctl daemon-reload
    sudo systemctl enable cpiped_${A2P_DEVNAME}.service
    sudo systemctl start cpiped_${A2P_DEVNAME}

Usage

audio2pipe scripts functionality description

scripts/A2PSourceProcess.sh

Main bash script taking various ENV variable as configuration to make, monitor and clean-up audio pipes, pid monitoring and output selection via callback function.

Script has three execution phases from now on <A2P_ExecPhase>

  • Pre: running all the preparatory check before cpiped is started. Executed only once at service start
  • Detect: Section run at sound detect. It runs python output selector callback script. Script can be replaced at user discretion by replacing the symlinked script
  • Silence: Performs all the files clean-up when audio source is stopped

Lockfile and pipe filling PID checks prevent parallel command execution and multiple pipe fillings resulting in unpleasant white noise blasting from speakers. These features are especially useful when audio is sourced from record players.

On sound detect a python callback script is used, in the repository an OwnTone output selection script is provided.

scripts/A2POutputselector.py

Python script using OwnTone http API(s) to select the output(s) defined in ENV variable at sound detect phase. It's usage is not mandatory

To be invoked script must be located in <A2P_HOME>/bin and named A2P_OT_OUT_SEL_<A2P_DEVNAME>

export A2P_HOME=/home/user
export A2P_DEVNAME=Test_Device
ln -s "${A2P_HOME}/bin/A2POutputSelector.py" "${A2P_HOME}/bin/A2P_OT_OUT_SEL_${A2P_DEVNAME}"

scripts/A2PToggle.sh

Script to toggle the various cpiped services. Must be run as sudo

service/cpiped_Template.service

Systemd service

Configuration

Script configuration are mainly managed though ENV variables

Variable
name
Description Type Default Range/Format Required?
A2P_HOME audio2pipe basedir

Folder structure:
  • bin: contains all the scripts
  • var/pipes: contains all the detect pipes needed by cpiped
  • var/run: contains pid files and lockfiles for audio2pipe and cpiped
string $HOME No
A2P_DEVNAME Device input name

Mnemonic that will define all the specialized file name
  • Audio pipe: <A2P_OT_LIB>/<A2P_DEVNAME>
  • Audio pipe filling PID file: var/run/A2P_<A2P_DEVNAME>.pid
  • cpiped PID file: var/run/A2P_<A2P_DEVNAME>.pid
  • Detect pipe: var/pipes/A2P_<A2P_DEVNAME>.detectpipe
  • Lockfile file: var/run/A2P_<A2P_DEVNAME>.lock.<A2P_ExecPhase>
  • systemd service file: cpiped_<A2P_DEVNAME>.service
string Yes
A2P_CP_SF Audio device sample rate to be passed to cpiped in hz

cpiped will then set env variables CPIPED_SR, CPIPED_SS, CPIPED_CC, respectively for sample rate, sample size and capture channel directly
integer 44100 16000 - 192000 No
A2P_CP_SOUNDCARD Audio capture soundcard to be passed to cpiped in ALSA format string 'default' hw:<card>,<device> No
A2P_CP_SL cpiped silence level integer 100 1 - 32767 No
A2P_OT_LIB OwnTone library folder. Where the audio pipe will be created and filled on sound detection string No
A2P_OT_HOST OwnTone server hostname string localhost No
A2P_OT_OUT_LIST OwnTone output(s) label to be selected at sound detect string
comma separated list
No
A2P_DEBUG Make log more verbose. Accepts True or False values string False True - False No

Usage of following variable is not recommended

Variable
name
Description Type Default Range/Format Required?
A2P_DEVFORMAT Concatenation of sample format and sample frequency separated by two underscores
Example: sample format 16bit Little Endian and sample frequency 48kHz will be S16_LE__48000
Default sample rate 41000Hz
Default sample size 16bit
Default capture channel 2
**Preferred** approach is to read cpiped env variables CPIPED_SR, CPIPED_SS, CPIPED_CC, respectively for sample rate, sample size and capture channel directly
string <Sample format>__<Sample rate> Not recomended

ToDo(s)

  • Sysvinit scripts
  • Move ENV variable setup from Service file to EnvironFile to define monitoring pipe in pre stage. Under evaluation
  • Rework detect pipe section that is prone to config error
  • Create install script
  • Add command line parameters to scripts/A2POutputselector.py
  • Add command line parameters to scripts/A2PToggle.sh
  • Change detect pipe extension from .pipe to .detectpipe
  • Add cpiped existence check in <A2P_ExecPhase>==Pre
  • Improve systemd service ExecStop section of script to stop the device specific instance of cpiped and not all of them
  • Fix systemd service KillMode warning

See the open issues for a full list of proposed features (and known issues).

Acknowledgments

Based on Making an Analog In to Airplay RPi-Transmitter from ownTone wiki

Created to be used with OwnTone

Top contributors:

contrib.rocks image

About

Set of scripts and services to better handle pipe creation for audio streamers

Topics

Resources

License

Stars

Watchers

Forks