skills/audioservice.py
Go to the documentation of this file.
1 # Copyright 2017 Mycroft AI Inc.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 #
15 import time
16 from os.path import abspath
17 
18 from mycroft.messagebus.message import Message
19 
20 
21 def ensure_uri(s):
22  """
23  Interprete paths as file:// uri's
24 
25  Args:
26  s: string to be checked
27 
28  Returns:
29  if s is uri, s is returned otherwise file:// is prepended
30  """
31  if isinstance(s, str):
32  if '://' not in s:
33  return 'file://' + abspath(s)
34  else:
35  return s
36  elif isinstance(s, (tuple, list)):
37  if '://' not in s[0]:
38  return 'file://' + abspath(s[0]), s[1]
39  else:
40  return s
41  else:
42  raise ValueError('Invalid track')
43 
44 
46  """
47  AudioService class for interacting with the audio subsystem
48 
49  Arguments:
50  bus: Mycroft messagebus connection
51  """
52 
53  def __init__(self, bus):
54  self.bus = bus
55  self.bus.on('mycroft.audio.service.track_info_reply',
56  self._track_info)
57  self.info = None
58 
59  def _track_info(self, message=None):
60  """
61  Handler for catching returning track info
62  """
63  self.info = message.data
64 
65  def queue(self, tracks=None):
66  """ Queue up a track to playing playlist.
67 
68  Args:
69  tracks: track uri or list of track uri's
70  """
71  tracks = tracks or []
72  if isinstance(tracks, str):
73  tracks = [tracks]
74  elif not isinstance(tracks, list):
75  raise ValueError
76  tracks = [ensure_uri(t) for t in tracks]
77  self.bus.emit(Message('mycroft.audio.service.queue',
78  data={'tracks': tracks}))
79 
80  def play(self, tracks=None, utterance=None, repeat=None):
81  """ Start playback.
82 
83  Args:
84  tracks: track uri or list of track uri's
85  Each track can be added as a tuple with (uri, mime)
86  to give a hint of the mime type to the system
87  utterance: forward utterance for further processing by the
88  audio service.
89  repeat: if the playback should be looped
90  """
91  repeat = repeat or False
92  tracks = tracks or []
93  utterance = utterance or ''
94  if isinstance(tracks, (str, tuple)):
95  tracks = [tracks]
96  elif not isinstance(tracks, list):
97  raise ValueError
98  tracks = [ensure_uri(t) for t in tracks]
99  self.bus.emit(Message('mycroft.audio.service.play',
100  data={'tracks': tracks,
101  'utterance': utterance,
102  'repeat': repeat}))
103 
104  def stop(self):
105  """ Stop the track. """
106  self.bus.emit(Message('mycroft.audio.service.stop'))
107 
108  def next(self):
109  """ Change to next track. """
110  self.bus.emit(Message('mycroft.audio.service.next'))
111 
112  def prev(self):
113  """ Change to previous track. """
114  self.bus.emit(Message('mycroft.audio.service.prev'))
115 
116  def pause(self):
117  """ Pause playback. """
118  self.bus.emit(Message('mycroft.audio.service.pause'))
119 
120  def resume(self):
121  """ Resume paused playback. """
122  self.bus.emit(Message('mycroft.audio.service.resume'))
123 
124  def seek(self, seconds=1):
125  """
126  seek X seconds
127 
128  Args:
129  seconds (int): number of seconds to seek, if negative rewind
130  """
131  if seconds < 0:
132  self.seek_backward(abs(seconds))
133  else:
134  self.seek_forward(seconds)
135 
136  def seek_forward(self, seconds=1):
137  """
138  skip ahead X seconds
139 
140  Args:
141  seconds (int): number of seconds to skip
142  """
143  self.bus.emit(Message('mycroft.audio.service.seek_forward',
144  {"seconds": seconds}))
145 
146  def seek_backward(self, seconds=1):
147  """
148  rewind X seconds
149 
150  Args:
151  seconds (int): number of seconds to rewind
152  """
153  self.bus.emit(Message('mycroft.audio.service.seek_backward',
154  {"seconds": seconds}))
155 
156  def track_info(self):
157  """ Request information of current playing track.
158 
159  Returns:
160  Dict with track info.
161  """
162  self.info = None
163  self.bus.emit(Message('mycroft.audio.service.track_info'))
164  wait = 5.0
165  while self.info is None and wait >= 0:
166  time.sleep(0.1)
167  wait -= 0.1
168 
169  return self.info or {}
170 
172  """ Return available audio backends.
173 
174  Returns:
175  dict with backend names as keys
176  """
177  msg = Message('mycroft.audio.service.list_backends')
178  response = self.bus.wait_for_response(msg)
179  return response.data if response else {}
180 
181  @property
182  def is_playing(self):
183  return self.track_info() != {}
def play(self, tracks=None, utterance=None, repeat=None)


mycroft_ros
Author(s):
autogenerated on Mon Apr 26 2021 02:35:40