Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print the actual channel being filtered, fix documentation #72

Merged
merged 6 commits into from
Dec 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions contrib/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Hifiberry DSP External Conributions

## What is this?

This directory contains external contributions (like useful scripts, snippets, etc.) for Hifiberry DSP.

## Disclaimer

This is not an official part of Hifiberry Products. The code included here wasn't thoroughly reviewed and may contain security issues. Use at your own risk!

# Overview

* install_hbdsp
install Hifiberry Dsp on piCorePlayer

* set-loudness
for profiles that do not support loudness this script 'fakes' loudness by using tonecontrol.

* hifiberry_config.toml
`cp hifiberry_config.toml ~/.config/hifiberry_config.toml`
This config file allows to customise the filterdefinitions

** Please do not report any issues with these scripts to hifiberry! *+
issues can be reported here https://github.com/rawdlite/hifiberry-dsp/issues
2 changes: 2 additions & 0 deletions contrib/hifiberry_config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[contrib]
loudness="{'1': {'hs': {'freq': '7000Hz', 'vol': '3dB'},'ls': {'freq': '100Hz', 'vol': '3dB'}},'2': {'hs': {'freq': '7000Hz', 'vol': '4dB'},'ls': {'freq': '150Hz', 'vol': '6dB'}},'3': {'hs': {'freq': '7000Hz', 'vol': '6dB'},'ls': {'freq': '200Hz', 'vol': '9dB'}}}"
157 changes: 157 additions & 0 deletions contrib/install_hbdsp.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
#!/bin/sh -e

### Exit, if not enough free space
requiredSpaceInMB=25
availableSpaceInMB=$(/bin/df -m /dev/mmcblk0p2 | awk 'NR==2 { print $4 }')
if [[ $availableSpaceInMB -le $requiredSpaceInMB ]]; then
>&2 echo "Not enough free space"
>&2 echo "Increase SD-Card size: Main Page > Additional functions > Resize FS"
exit 1
fi

### Abort, if piCoreCDSP extension is already installed
if [ -f "/etc/sysconfig/tcedir/optional/HifiBerryDSP.tcz" ]; then
>&2 echo "Uninstall the HifiBerryDSP Extension and reboot, before installing it again"
>&2 echo "In Main Page > Extensions > Installed > select 'HifiBerry.tcz' and press 'Delete'"
exit 1
fi


if [ -d "/tmp/hbdsp" ]; then
>&2 echo "Reboot before running the script again."
exit 1
fi
mkdir -p /tmp/hbdsp

# Installs a module from the piCorePlayer repository - if not already installed.
# Call like this: install_if_missing module_name
install_if_missing(){
if ! tce-status -i | grep -q "$1" ; then
pcp-load -wil "$1"
fi
}

# Installs a module from the piCorePlayer repository, at least until the next reboot - if not already installed.
# Call like this: install_temporarily_if_missing module_name
install_temporarily_if_missing(){
if ! tce-status -i | grep -q "$1" ; then
pcp-load -wil -t /tmp "$1" # Downloads to /tmp/optional and loads extensions temporarily
fi
}

set -v

### Create hifiberry data folder

cd /mnt/mmcblk0p2/tce
[[ -d hifiberry ]] || mkdir hifiberry
mkdir -p /tmp/hbdsp/var/lib
ln -s /mnt/mmcblk0p2/tce/hifiberry /tmp/hbdsp/var/lib/hifiberry

install_if_missing python3.11
install_if_missing libxslt-dev
install_if_missing libxml2-dev
install_temporarily_if_missing python3.11-pip
#install_temporarily_if_missing binutils
install_temporarily_if_missing git
install_temporarily_if_missing compiletc
install_temporarily_if_missing libasound-dev

cd /tmp

### Install HifiBerryDSP
mkdir -p /tmp/hbdsp/usr/local/hifiberry
python3 -m venv /tmp/hbdsp/usr/local/hifiberry/environment
cd /tmp/hbdsp/usr/local/hifiberry/
(tr -d '\r' < environment/bin/activate) > environment/bin/activate_new # Create fixed version of the activate script. See https://stackoverflow.com/a/44446239
mv -f environment/bin/activate_new environment/bin/activate
source environment/bin/activate # activate custom python environment
python3 -m pip install --upgrade pip
python3 -m pip install --upgrade "hifiberrydsp==0.21"
#cd /tmp
#git clone https://github.com/hifiberry/hifiberry-dsp
#cd hifiberry-dsp
#python3 ./setup.py install
deactivate # deactivate custom python environment


#create executables
mkdir /tmp/hbdsp/usr/local/bin
echo "/usr/local/hifiberry/environment/bin/python3.11 /usr/local/hifiberry/environment/bin/dsptoolkit" > /tmp/hbdsp/usr/local/bin/dsptoolkit
chmod 755 /tmp/hbdsp/usr/local/bin/dsptoolkit

echo "/usr/local/hifiberry/environment/bin/python3.11 /usr/local/hifiberry/environment/bin/sigmatcpserver" > /tmp/hbdsp/usr/local/bin/sigmatcpserver
chmod 755 /tmp/hbdsp/usr/local/bin/sigmatcpserver

mkdir -p /tmp/hbdsp/usr/local/etc/init.d
echo "#!/bin/sh
# Version: 1.1.0

PNAME='sigmatcpserver'
DESC='SigmaTCP Server for HiFiBerry DSP'
PIDFILE=/var/run/sigmatcpserver/sigmatcpserver.pid

# Set DAEMON to the actual binary
DAEMON=\"/usr/local/bin/sigmatcpserver\"

case \"\$1\" in
start)
echo \"Starting \$DESC...\"
if [ -e \$PIDFILE ]; then
rm \$PIDFILE
fi

start-stop-daemon --start --quiet --exec \$DAEMON \
-- --daemon
;;
stop)
echo \"Stopping \$DESC...\"
start-stop-daemon --stop --quiet --exec \$DAEMON

;;
restart)
echo \"Restarting \$DESC...\"
\$0 stop
sleep 1
\$0 start
;;
status)
# Check if sigmatcpserver daemon is running
PID=\$(pgrep -f \$DAEMON)
if [ 0\$PID -gt 0 ]; then
echo \"\$PNAME is running.\"
exit 0
else
echo \"\$PNAME not running.\"
exit 1
fi
;;
*)
echo
echo -e \"Usage: \$0 [start|stop|restart|status]\"
echo
exit 1
;;
esac

exit 0" > /tmp/hbdsp/usr/local/etc/init.d/sigmatcpserver
chmod 755 /tmp/hbdsp/usr/local/etc/init.d/sigmatcpserver

mkdir -p /tmp/hbdsp/usr/local/tce.installed
echo "#!/bin/sh

/usr/local/etc/init.d/sigmatcpserver start
" >> /tmp/hbdsp/usr/local/tce.installed/HifiBerryDSP
### Create and install HifiBerryDSP.tcz

install_temporarily_if_missing squashfs-tools
mksquashfs /tmp/hbdsp /etc/sysconfig/tcedir/optional/HifiBerryDSP.tcz
echo "python3.11.tcz" > /etc/sysconfig/tcedir/optional/HifiBerryDSP.tcz.dep
echo "libxslt-dev.tcz" > /etc/sysconfig/tcedir/optional/HifiBerryDSP.tcz.dep
echo "libxml2-dev.tcz" > /etc/sysconfig/tcedir/optional/HifiBerryDSP.tcz.dep
echo HifiBerryDSP.tcz >> /etc/sysconfig/tcedir/onboot.lst

### Save Changes

pcp backup
pcp reboot
62 changes: 62 additions & 0 deletions contrib/set-loudness
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Author: [email protected]
# Created: 2024-12-21
# License: MIT License
from hifiberrydsp.dsptoolkit import DSPToolkit,CommandLine
from hifiberrydsp.filtering.biquad import Biquad
from hifiberrydsp.datatools import parse_int, parse_frequency, parse_decibel
import tomllib
from pathlib import Path
import argparse


try:
with open(Path.home() / ".config" / "hifiberry_config.toml", mode="rb") as fp:
settings = tomllib.load(fp)
print(settings)
LOUDNESS = eval(settings['contrib']['loudness'])
except:
LOUDNESS = {
'1': {'hs': {'freq': '7000Hz', 'vol': '3dB'},
'ls': {'freq': '100Hz', 'vol': '3dB'}},
'2': {'hs': {'freq': '7000Hz', 'vol': '4dB'},
'ls': {'freq': '150Hz', 'vol': '6dB'}},
'3': {'hs': {'freq': '7000Hz', 'vol': '6dB'},
'ls': {'freq': '200Hz', 'vol': '9dB'}}}

parser = argparse.ArgumentParser(description='set loudness via tonecontrol')
parser.add_argument("-v", "--verbose", action="store_true",
help="be verbose about whats going on")
parser.add_argument('loudness_value', choices=LOUDNESS.keys(),help="loudness intensity value")
args = parser.parse_args()
loudness = LOUDNESS[args.loudness_value]
hs_cmd = f"dsptoolkit tone-control hs {loudness['hs']['freq']} {loudness['hs']['vol']}"
ls_cmd = f"dsptoolkit tone-control hs {loudness['ls']['freq']} {loudness['ls']['vol']}"


if args.verbose:
print(f"got loudness defined as {loudness}")
print(f"hs-cmd: {hs_cmd}")
print(f"ls-cmd: {ls_cmd}")

dsptoolkit = DSPToolkit()
cmd = CommandLine()

hs_frequency = parse_frequency(loudness['hs']['freq'])
hs_dbgain = parse_decibel(loudness['hs']['vol'])
ls_frequency = parse_frequency(loudness['ls']['freq'])
ls_dbgain = parse_decibel(loudness['ls']['vol'])
hs_filterdef = f"hs:{hs_frequency}:{hs_dbgain}"
ls_filterdef = f"ls:{ls_frequency}:{ls_dbgain}"
lowshelffilter = Biquad.create_filter(ls_filterdef,dsptoolkit.get_samplerate())
highshelffilter = Biquad.create_filter(hs_filterdef,dsptoolkit.get_samplerate())
dsptoolkit.hibernate()
try:
dsptoolkit.set_tonecontrol_filters(lowshelf=lowshelffilter, highshelf=highshelffilter)
except Exception:
print("check filerdefinition")
exit
finally:
dsptoolkit.hibernate(False)

4 changes: 2 additions & 2 deletions doc/dsptoolkit.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ The following commands are supported. Note that some command need specific param
For negative db values, you need to prefix these with `--`, e.g.
`dsptoolkit set-limit -- -3db`

* `set-rew-filters|set-rew-filters-left|set-rew-filters-right filename`
* `apply-rew-filters|apply-rew-filters-left|apply-rew-filters-right filename`

Deploys parametric equaliser settings calculated by REW to the equaliser filter banks (left, right or both).
Not all DSP profiles will support this setting.
To make sure the filters are still active after a system reboot, make sure you use the store command.

* `set-fir-filters|set-fir-filters-left|set-fir-filters-right`
* `apply-fir-filters|apply-fir-filters-left|apply-fir-filters-right`

Deploys a FIR (finite impulse response) filter to the left, right or both FIR filter banks.
A FIR filter file is a simple text file with one real number per line.
Expand Down
9 changes: 7 additions & 2 deletions hifiberrydsp/dsptoolkit.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@
MODE_BOTH = 0
MODE_LEFT = 1
MODE_RIGHT = 2
MODE_DESCRIPTION = {
0: 'both channels',
1: 'left channel',
2: 'right channel'
}

DISPLAY_FLOAT = 0
DISPLAY_INT = 1
Expand Down Expand Up @@ -473,7 +478,7 @@ def __init__(self):
to 2 (only right channel)
at balance=1 the volume setting on both channels is equal

set-rew-filters|set-rew-filters-left|set-rew-filters-right <filename>
apply-rew-filters|apply-rew-filters-left|apply-rew-filters-right <filename>
Deploys parametric equaliser settings calculated by REW to the
equaliser filter banks (left, right or both)

Expand Down Expand Up @@ -683,7 +688,7 @@ def set_iir_filters(self, mode=MODE_BOTH, format=GENERIC):
self.dsptk.clear_iir_filters(mode)
try:
self.dsptk.set_filters(filters, mode)
print("Filters configured on both channels:")
print(f"Filters configured on {MODE_DESCRIPTION[mode]}:")
for f in filters:
print(f.description)
except DSPError as e:
Expand Down
Loading