3434import signal
3535import logging
3636import time
37+ import yaml
3738import configparser
3839from pathlib import Path
3940from math import log10 , exp , log
4041from 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
4650def 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+
190242def get_cmdline_arguments ():
191243 parser = argparse .ArgumentParser (description = 'Synchronize MPD volume to CamillaDSP' )
192244
0 commit comments