-
-
Notifications
You must be signed in to change notification settings - Fork 30
Add volume sync between Spotify and AmpliPi #1063
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
Open
SteveMicroNova
wants to merge
1
commit into
VolumeDeltaExpansion
Choose a base branch
from
SpotifyVolumeReflection
base: VolumeDeltaExpansion
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| """Script for synchronizing AmpliPi and Spotify volumes""" | ||
| import argparse | ||
| import json | ||
| import logging | ||
| import sys | ||
|
|
||
| import websockets | ||
| import requests | ||
|
|
||
| from volume_synchronizer import VolSyncDispatcher, StreamWatcher, VolEvents | ||
| from spot_connect_meta import Event | ||
|
|
||
|
|
||
| class SpotifyWatcher(StreamWatcher): | ||
| """A class that watches and tracks changes to spotify-side volume""" | ||
|
|
||
| def __init__(self, api_port: int): | ||
| super().__init__() | ||
|
|
||
| self.api_port: int = api_port | ||
| """What port is go-librespot running on? Typically set to 3678 + vsrc.""" | ||
|
|
||
| async def watch_vol(self): | ||
| """Watch the go-librespot websocket endpoint for volume change events and update AmpliPi volume info accordingly""" | ||
| try: | ||
| # pylint: disable=E1101 | ||
| # E1101: Module 'websockets' has no 'connect' member (no-member) | ||
| async with websockets.connect(f"ws://localhost:{self.api_port}/events", open_timeout=5) as websocket: | ||
| while True: | ||
| try: | ||
| msg = await websocket.recv() | ||
| event = Event.from_json(json.loads(msg)) | ||
| if event.event_type == "volume": | ||
| last_volume = float(self.volume) if self.volume is not None else None | ||
| self.volume = event.data.value / 100 # Translate spotify volume (0 - 100) to amplipi volume (0 - 1) | ||
|
|
||
| self.logger.debug(f"Spotify volume changed from {last_volume} to {self.volume}") | ||
| if last_volume is not None and self.volume != last_volume: | ||
| self.schedule_event(VolEvents.CHANGE_AMPLIPI) | ||
| elif event.event_type == "will_play" and self.volume is None: | ||
| self.schedule_event(VolEvents.CHANGE_STREAM) # Intercept the event that occurs when a song starts playing and use that as a trigger for the initial state sync | ||
|
|
||
| except Exception as e: | ||
| self.logger.exception(f"Error: {e}") | ||
| return | ||
| except Exception as e: | ||
| self.logger.exception(f"Error: {e}") | ||
| return | ||
|
|
||
| def set_vol(self, new_vol: float, vol_set_point: float) -> float: | ||
| """Update Spotify's volume slider""" | ||
| try: | ||
| raise Exception("Fake exception that is used to test logging abilities") | ||
| if new_vol is None: | ||
| return vol_set_point | ||
|
|
||
| if abs(new_vol - vol_set_point) <= 0.005 and self.volume is not None: | ||
| self.logger.debug("Ignored minor AmpliPi -> Spotify change") | ||
| return vol_set_point | ||
|
|
||
| url = f"http://localhost:{self.api_port}/player/volume" | ||
| spot_vol = int(new_vol * 100) | ||
| self.logger.debug(f"Setting Spotify volume to {new_vol} from {self.volume}") | ||
| requests.post(url, json={"volume": spot_vol}, timeout=5) | ||
| return new_vol | ||
| except Exception as e: | ||
| self.logger.exception(f"Exception: {e}") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
|
|
||
| parser = argparse.ArgumentParser(description="Read metadata from a given URL and write it to a file.") | ||
|
|
||
| parser.add_argument("port", help="port that go-librespot is running on", type=int) | ||
| parser.add_argument("config_dir", help="The directory of the vsrc config", type=str) | ||
| parser.add_argument("--debug", action="store_true", help="Change log level from WARNING to DEBUG") | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| handler = VolSyncDispatcher(SpotifyWatcher(api_port=args.port), args.config_dir, args.debug) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.