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

feat/legacy_audio_api #456

Merged
merged 15 commits into from
May 10, 2024
91 changes: 72 additions & 19 deletions ovos_core/intent_services/ocp_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
from threading import RLock
from typing import List, Tuple, Optional

from ovos_classifiers.skovos.classifier import SklearnOVOSClassifier
from ovos_classifiers.skovos.features import ClassifierProbaVectorizer, KeywordFeaturesVectorizer
from padacioso import IntentContainer
from sklearn.pipeline import FeatureUnion

import ovos_core.intent_services
from ovos_bus_client.apis.ocp import OCPInterface, OCPQuery
from ovos_bus_client.apis.ocp import OCPInterface, OCPQuery, ClassicAudioServiceInterface
from ovos_bus_client.message import Message
from ovos_classifiers.skovos.classifier import SklearnOVOSClassifier
from ovos_classifiers.skovos.features import ClassifierProbaVectorizer, KeywordFeaturesVectorizer
from ovos_config import Configuration
from ovos_plugin_manager.ocp import load_stream_extractors
from ovos_utils import classproperty
from ovos_utils.log import LOG
from ovos_utils.messagebus import FakeBus
Expand Down Expand Up @@ -104,6 +106,7 @@ def __init__(self, bus=None, config=None):
resources_dir=f"{dirname(__file__)}")

self.ocp_api = OCPInterface(self.bus)
self.legacy_api = ClassicAudioServiceInterface(self.bus)
JarbasAl marked this conversation as resolved.
Show resolved Hide resolved

self.config = config or {}
self.search_lock = RLock()
Expand All @@ -123,6 +126,15 @@ def __init__(self, bus=None, config=None):
# request available Stream extractor plugins from OCP
self.bus.emit(Message("ovos.common_play.SEI.get"))

@property
def use_legacy_audio(self):
"""when neither ovos-media nor old OCP are available"""
if self.config.get("legacy"):
# explicitly set in pipeline config
return True
cfg = Configuration()
return cfg.get("disable_ocp") and cfg.get("enable_old_audioservice")

def load_classifiers(self):

# warm up the featurizer so intent matches faster (lazy loaded)
Expand Down Expand Up @@ -526,7 +538,10 @@ def handle_play_intent(self, message: Message):
'origin': OCP_ID}))

# ovos-PHAL-plugin-mk1 will display music icon in response to play message
self.ocp_api.play(results, query)
if self.use_legacy_audio:
self.legacy_play(results, query)
else:
self.ocp_api.play(results, query)

def handle_open_intent(self, message: Message):
LOG.info("Requesting OCP homescreen")
Expand All @@ -539,30 +554,54 @@ def handle_like_intent(self, message: Message):
self.bus.emit(message.forward("ovos.common_play.like"))

def handle_stop_intent(self, message: Message):
LOG.info("Requesting OCP to go to stop")
self.ocp_api.stop()
if self.use_legacy_audio:
LOG.info("Requesting Legacy AudioService to stop")
self.legacy_api.stop()
else:
LOG.info("Requesting OCP to stop")
self.ocp_api.stop()

def handle_next_intent(self, message: Message):
LOG.info("Requesting OCP to go to next track")
self.ocp_api.next()
if self.use_legacy_audio:
LOG.info("Requesting Legacy AudioService to go to next track")
self.legacy_api.next()
else:
LOG.info("Requesting OCP to go to next track")
self.ocp_api.next()

def handle_prev_intent(self, message: Message):
LOG.info("Requesting OCP to go to prev track")
self.ocp_api.prev()
if self.use_legacy_audio:
LOG.info("Requesting Legacy AudioService to go to prev track")
self.legacy_api.prev()
else:
LOG.info("Requesting OCP to go to prev track")
self.ocp_api.prev()

def handle_pause_intent(self, message: Message):
LOG.info("Requesting OCP to go to pause")
self.ocp_api.pause()
if self.use_legacy_audio:
LOG.info("Requesting Legacy AudioService to pause")
self.legacy_api.pause()
else:
LOG.info("Requesting OCP to go to pause")
self.ocp_api.pause()

def handle_resume_intent(self, message: Message):
LOG.info("Requesting OCP to go to resume")
self.ocp_api.resume()
if self.use_legacy_audio:
LOG.info("Requesting Legacy AudioService to resume")
self.legacy_api.resume()
else:
LOG.info("Requesting OCP to go to resume")
self.ocp_api.resume()

def handle_search_error_intent(self, message: Message):
self.bus.emit(message.forward("mycroft.audio.play_sound",
{"uri": "snd/error.mp3"}))
LOG.info("Requesting OCP to stop")
self.ocp_api.stop()
if self.use_legacy_audio:
LOG.info("Requesting Legacy AudioService to stop")
self.legacy_api.stop()
else:
LOG.info("Requesting OCP to stop")
self.ocp_api.stop()

def _do_play(self, phrase: str, results, media_type=MediaType.GENERIC):
self.bus.emit(Message('ovos.common_play.reset'))
Expand All @@ -581,7 +620,20 @@ def _do_play(self, phrase: str, results, media_type=MediaType.GENERIC):
'origin': OCP_ID}))

# ovos-PHAL-plugin-mk1 will display music icon in response to play message
self.ocp_api.play(results, phrase)
if self.use_legacy_audio:
self.legacy_play(results, phrase)
else:
self.ocp_api.play(results, phrase)

def legacy_play(self, results: List[MediaEntry], phrase=""):
xtract = load_stream_extractors()
# for legacy audio service we need to do stream extraction here
# we also need to filter video results
results = [xtract.extract_stream(r.uri, video=False)["uri"]
for r in results
if r.playback == PlaybackType.AUDIO
or r.media_type in OCPQuery.cast2audio]
self.legacy_api.play(results, utterance=phrase)

# NLP
@staticmethod
Expand Down Expand Up @@ -751,11 +803,12 @@ def filter_results(self, results: list, phrase: str, lang: str,
video_only = True

# check if user said "play XXX audio only"
if audio_only:
if audio_only or self.use_legacy_audio:
l1 = len(results)
# TODO - also check inside playlists
results = [r for r in results
if isinstance(r, Playlist) or r.playback == PlaybackType.AUDIO]
if (isinstance(r, Playlist) and not self.use_legacy_audio)
or r.playback == PlaybackType.AUDIO]
LOG.debug(f"filtered {l1 - len(results)} non-audio results")

# check if user said "play XXX video only"
Expand Down
Loading