Skip to content

Commit 13c0341

Browse files
Implement vol script
Gate a few things in the vol script better
1 parent ce478dc commit 13c0341

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
* Update our spotify provider `go-librespot` to `0.5.2` to accomodate spotify's API update
77
* Web App
88
* Upgraded volume calculations to preserve relative positions when hitting the min or max setting via source volume bar
9+
* Streams:
10+
* Upgraded Spotify to sync Spotify's volume with AmpliPi and vice-versa
911

1012
## 0.4.8
1113
* System

amplipi/streams/spotify_connect.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def _activate(self, vsrc: int):
101101
self.proc2 = subprocess.Popen(args=meta_args, stdout=self._log_file, stderr=self._log_file)
102102

103103
vol_sync = f"{utils.get_folder('streams')}/spotify_volume_handler.py"
104-
vol_args = [sys.executable, vol_sync, str(self._api_port), src_config_folder[-1], "--debug"]
104+
vol_args = [sys.executable, vol_sync, str(self._api_port), str(self.vsrc), "--debug"]
105105
logger.info(f'{self.name}: starting vol synchronizer: {vol_args}')
106106
self.proc3 = subprocess.Popen(args=vol_args, stdout=self._log_file, stderr=self._log_file)
107107

@@ -111,14 +111,19 @@ def _deactivate(self):
111111
logger.info(f'{self.name}: stopping player')
112112
self.proc.terminate()
113113
self.proc2.terminate()
114+
self.proc3.terminate()
114115
if self.proc.wait(1) != 0:
115116
logger.info(f'{self.name}: killing player')
116117
self.proc.kill()
117118
if self.proc2.wait(1) != 0:
118119
logger.info(f'{self.name}: killing metadata reader')
119120
self.proc2.kill()
121+
if self.proc3.wait(1) != 0:
122+
logger.info(f'{self.name}: killing volume synchronizer')
123+
self.proc3.kill()
120124
self.proc.communicate()
121125
self.proc2.communicate()
126+
self.proc3.communicate()
122127
if self.proc and self._log_file: # prevent checking _log_file when it may not exist, thanks validation!
123128
self._log_file.close()
124129
if self.src:
@@ -129,6 +134,7 @@ def _deactivate(self):
129134
self._disconnect()
130135
self.proc = None
131136
self.proc2 = None
137+
self.proc3 = None
132138

133139
def info(self) -> models.SourceInfo:
134140
source = models.SourceInfo(

streams/spotify_volume_handler.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ def __init__(self, source_id: int, callback, debug: bool = False):
6767
self.callback = callback
6868
self.debug = debug
6969
self.status: dict = None
70+
self.last_volume: float = None
7071
self.volume: float = None
7172

7273
self.connected_zones: list = []
@@ -76,15 +77,14 @@ def __init__(self, source_id: int, callback, debug: bool = False):
7677
def get_status(self):
7778
"""Call up the amplipi API and send the output to self.consume_status"""
7879
while True:
79-
last_vol = float(self.volume) if self.volume is not None else None
80-
81-
# with open("/home/pi/.config/amplipi/house.json", "r", encoding="utf-8") as f:
82-
# self.consume_status(json.loads(f))
83-
8480
self.consume_status(requests.get("http://localhost/api", timeout=5).json())
8581

86-
if last_vol != self.volume:
82+
print(f"last: {self.last_volume}, vol: {self.volume}")
83+
84+
if self.last_volume != self.volume:
8785
self.callback("amplipi_volume_changed")
86+
self.last_volume = float(self.volume)
87+
sleep(2)
8888

8989
def consume_status(self, status):
9090
"""Consume an API response into the local object"""
@@ -98,6 +98,8 @@ def get_volume(self):
9898
if self.connected_zones:
9999
total_vol_f = sum([zone["vol_f"] for zone in self.connected_zones]) # Note that accounting for the vol_f overflow variables here would make it impossible to use those overflows while also using this volume bar
100100
return round(total_vol_f / len(self.connected_zones), 2) # Round down to 2 decimals to assist with float accuracy
101+
else:
102+
logger.warning("Could not find any associated zones")
101103
return 0
102104

103105

@@ -169,6 +171,8 @@ def update_spotify_volume(self):
169171
handler = SpotifyVolumeHandler(args.port, args.source, args.debug)
170172
while True:
171173
try:
174+
if handler.spotify.volume is None:
175+
handler.update_spotify_volume()
172176
event = handler.event_queue.get(timeout=2)
173177
if event in "spotify_volume_changed":
174178
handler.update_amplipi_volume()

0 commit comments

Comments
 (0)