Skip to content

Commit 8550b81

Browse files
authored
camilladsp 2.0 support
* Support for volume handling with camilladsp 2.0 * Work arround a issue with cdsp 2.0 volume handling. Sometimes the setpoint isn't the actual (after the ramp period), set the volume for a second time until issue is resolved.
1 parent f025e8e commit 8550b81

File tree

5 files changed

+75
-13
lines changed

5 files changed

+75
-13
lines changed

README.MD

100644100755
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,15 @@ optional arguments:
105105
106106
example:
107107
108+
with camilladsp 1.x
108109
```bash
109110
sudo -H -u mpd bash -c "/usr/local/bin/mpd2cdspvolume --verbose --pid_file /var/run/mpd2cdspvol/mpd2cdspvol.pid --volume_state_file /var/lib/cdsp/camilladsp_volume_state --config /etc/mpd2cdspvolume.config"
110111
```
112+
113+
with camilladsp 2.x
114+
```bash
115+
sudo -H -u mpd bash -c "/usr/local/bin/mpd2cdspvolume --verbose --pid_file /var/run/mpd2cdspvol/mpd2cdspvol.pid --volume_state_file /var/lib/cdsp/statefile.yml --config /etc/mpd2cdspvolume.config"
116+
```
111117
This will run the program as user `mpd` and with `--verbose` show output of the actions.
112118
113119
## alsa_cdsp support

builddep.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ fi
1616

1717
if [ -z "$PKGVERSION" ]
1818
then
19-
PKGVERSION="0.3.1"
19+
PKGVERSION="0.4.0"
2020
fi
2121

2222
if [ -z "$DEBVER" ]
@@ -26,7 +26,7 @@ fi
2626

2727
if [ -z "$DEBLOC" ]
2828
then
29-
DEBLOC="~pre1"
29+
DEBLOC=""
3030
fi
3131

3232
#------------------------------------------------------------

changelog.txt

100644100755
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
0.4.0
2+
* Support for Camilladsp 2.0
3+
4+
0.3.1
5+
* Fixed bug that caused serious processor load
6+
17
0.3.0
28
* Changed volume curve based on https://www.dr-lex.be/info-stuff/volumecontrols.html
39
* Added setting dynamic_range for setting the dynamic range of the volume (default 60dB)
@@ -6,5 +12,3 @@
612
* Fixed uncatched MPC exception ConnectionResetError
713
* Fixed hanging cdsp socket on shutdown of cdsp by closing cdsp socket on sighup
814

9-
0.3.1
10-
* Fixed bug that caused serious processor load

etc/mpd2cdspvolume.service

100644100755
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ After=network-online.target
55
[Service]
66
Type=simple
77
User=mpd
8-
ExecStart=/usr/local/bin/mpd2cdspvolume --pid_file /var/run/mpd2cdspvol/mpd2cdspvol.pid --volume_state_file /var/lib/cdsp/camilladsp_volume_state --config /etc/mpd2cdspvolume.config
8+
ExecStart=/usr/local/bin/mpd2cdspvolume --pid_file /var/run/mpd2cdspvol/mpd2cdspvol.pid --volume_state_file /var/lib/cdsp/statefile.yml --config /etc/mpd2cdspvolume.config
99

1010
[Install]
1111
WantedBy=multi-user.target

mpd2cdspvolume.py

Lines changed: 60 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,18 @@
3434
import signal
3535
import logging
3636
import time
37+
import yaml
3738
import configparser
3839
from pathlib import Path
3940
from math import log10, exp, log
4041
from mpd import MPDClient, ConnectionError
4142

42-
from camilladsp import CamillaConnection
43+
try:
44+
from camilladsp import CamillaClient
45+
except:
46+
from camilladsp import CamillaConnection as CamillaClient
4347

44-
VERSION = "0.3.1"
48+
VERSION = "0.4.0"
4549

4650
def lin_vol_curve(perc: int, dynamic_range: float= 60.0) -> float:
4751
'''
@@ -145,11 +149,12 @@ class CamillaDSPVolumeUpdater:
145149
"""
146150
def __init__(self, volume_state_file: Optional[Path] = None, host: str='127.0.0.1', port:int=1234):
147151
self._volume_state_file: Optional[Path]= volume_state_file
148-
self._cdsp = CamillaConnection(host, port)
152+
self._cdsp = CamillaClient(host, port)
149153
if volume_state_file:
150154
logging.info('volume state file: "%s"', volume_state_file )
151155

152156
def update_alsa_cdsp_volume_file(self, volume_db: float, mute: int=0):
157+
""" Store state in own mpd2cdspvolume statefile. Required for CamillaDSP < 2.x"""
153158
if self._volume_state_file and self._volume_state_file.exists():
154159
logging.info('update volume state file : %.2f dB, mute: %d', volume_db,mute)
155160
try:
@@ -158,35 +163,82 @@ def update_alsa_cdsp_volume_file(self, volume_db: float, mute: int=0):
158163
logging.error('Couldn\'t create state file "%s", prob basedir doesn\'t exists.', self._volume_state_file)
159164
except PermissionError as e:
160165
logging.error('Couldn\'t write state to "%s", prob incorrect owner rights of dir.', self._volume_state_file)
166+
161167
def update_cdsp_volume(self, volume_db: float):
162168
try:
163169
if self._cdsp.is_connected() is False:
164170
self._cdsp.connect()
165171

166-
self._cdsp.set_volume(volume_db)
172+
173+
if hasattr(self._cdsp, "volume"):
174+
self._cdsp.volume.set_main(volume_db)
175+
time.sleep(0.2)
176+
cdsp_actual_volume = self._cdsp.volume.main()
177+
logging.info('volume set to %.2f [readback = %.2f] dB', volume_db, cdsp_actual_volume)
178+
179+
# correct issue when volume is not the required one (issue with cdsp 2.0)
180+
if abs(cdsp_actual_volume-volume_db) > .2:
181+
# logging.info('volume incorrect !')
182+
self._cdsp.volume.set_main(volume_db)
183+
else:
184+
self._cdsp.set_volume(volume_db)
167185
return True
168186
except (ConnectionRefusedError, IOError) as e:
169187
logging.info('no cdsp')
170-
self.update_alsa_cdsp_volume_file(volume_db)
188+
if hasattr(self._cdsp, "volume"):
189+
self.update_alsa_cdsp_volume_file(volume_db)
190+
else:
191+
self.update_cdsp_statefile(volume_db)
171192
return False
172193

173194
def store_volume(self):
174195
try:
175196
if self._cdsp.is_connected() is False:
176197
self._cdsp.connect()
177198

178-
volume_db = float(self._cdsp.get_volume())
179-
mute = 1 if self._cdsp.get_mute() else 0
199+
if hasattr(self._cdsp, "volume"):
200+
volume_db = float(self._cdsp.volume.main())
201+
mute = 1 if self._cdsp.mute.main() else 0
202+
else:
203+
volume_db = float(self._cdsp.get_volume())
204+
mute = 1 if self._cdsp.get_mute() else 0
180205
self.update_alsa_cdsp_volume_file(volume_db, mute)
206+
181207
except (ConnectionRefusedError, IOError) as e:
182208
logging.warning('store volume: no cdsp')
183209

184210
def sig_hup(self, signum, frame):
185-
self.store_volume()
211+
if hasattr(self._cdsp, "volume") is False:
212+
self.store_volume()
186213
# force disconnect to prevent a 'hanging' socket during close down of cdsp
187214
if self._cdsp.is_connected():
188215
self._cdsp.disconnect()
189216

217+
def update_cdsp_statefile(self, main_volume: float=-6.0, main_mute:bool = False):
218+
""" Update statefile from camilladsp. Used for CamillaDSP 2.x and higher."""
219+
logging.info('update volume state file : %.2f dB, mute: %d', main_volume ,main_mute)
220+
cdsp_state = {
221+
'config_path': '/usr/share/camilladsp/working_config.yml',
222+
'mute': [ False,False, False,False,False],
223+
'volume': [ -6.0, -6.0, -6.0, -6.0, -6.0]
224+
}
225+
if self._volume_state_file:
226+
try:
227+
if self._volume_state_file.exists():
228+
cdsp_state = yaml.load(self._volume_state_file.read_text(), Loader=yaml.Loader)
229+
else:
230+
logging.info('no state file present, create one')
231+
232+
cdsp_state['volume'][0] = main_volume
233+
cdsp_state['mute'][0] = main_mute
234+
235+
self._volume_state_file.write_text(yaml.dump(data, indent=8, explicit_start=True))
236+
except FileNotFoundError as e:
237+
logging.error('Couldn\'t create state file "%s", prob basedir doesn\'t exists.', self._volume_state_file)
238+
except PermissionError as e:
239+
logging.error('Couldn\'t write state to "%s", prob incorrect owner rights of dir.', self._volume_state_file)
240+
241+
190242
def get_cmdline_arguments():
191243
parser = argparse.ArgumentParser(description = 'Synchronize MPD volume to CamillaDSP')
192244

0 commit comments

Comments
 (0)