-
Notifications
You must be signed in to change notification settings - Fork 1
/
__init__.py
189 lines (171 loc) · 8.58 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# NEON AI (TM) SOFTWARE, Software Development Kit & Application Framework
# All trademark and other rights reserved by their respective owners
# Copyright 2008-2022 Neongecko.com Inc.
# Contributors: Daniel McKnight, Guy Daniels, Elon Gasper, Richard Leeds,
# Regina Bloomstine, Casimiro Ferreira, Andrii Pernatii, Kirill Hrymailo
# BSD-3 License
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# 3. Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from this
# software without specific prior written permission.
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
# OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
from threading import Thread, Event
from typing import List, Optional
from os.path import join, dirname, expanduser, isdir
from random import sample
from ovos_plugin_common_play import MediaType, PlaybackType
from ovos_workshop.skills.common_play import OVOSCommonPlaybackSkill, \
ocp_search
from ovos_utils import classproperty
from ovos_utils.log import LOG
from ovos_utils.process_utils import RuntimeRequirements
from ovos_utils.xdg_utils import xdg_cache_home
from skill_local_music.util import MusicLibrary, Track
class LocalMusicSkill(OVOSCommonPlaybackSkill):
def __init__(self, **kwargs):
self.supported_media = [MediaType.MUSIC,
MediaType.AUDIO,
MediaType.GENERIC]
self.library_update_event = Event()
self._music_library = None
self._image_url = join(dirname(__file__), 'ui/music-solid.svg')
self._demo_dir = join(expanduser(xdg_cache_home()), "neon",
"demo_music")
OVOSCommonPlaybackSkill.__init__(self, **kwargs)
@classproperty
def runtime_requirements(self):
return RuntimeRequirements(network_before_load=False,
internet_before_load=False,
gui_before_load=False,
requires_internet=False,
requires_network=False,
requires_gui=False,
no_internet_fallback=True,
no_network_fallback=True,
no_gui_fallback=True)
@property
def demo_url(self) -> Optional[str]:
return self.settings.get("demo_url")
@property
def music_dir(self) -> str:
return expanduser(self.settings.get('music_dir') or "~/Music")
@property
def music_library(self):
if not self._music_library:
LOG.info(f"Initializing music library at: {self.music_dir}")
self._music_library = MusicLibrary(self.music_dir,
self.file_system.path)
return self._music_library
# TODO: Move to __init__ after ovos-workshop stable release
def initialize(self):
# TODO: add intent to update library?
Thread(target=self.update_library, daemon=True).start()
def update_library(self):
self.library_update_event.clear()
if self.music_dir and isdir(self.music_dir):
LOG.debug(f"Load configured directory: {self.music_dir}")
self.music_library.update_library(self.music_dir)
user_dir = expanduser("~/Music")
if isdir(user_dir):
LOG.debug(f"Load default directory: {self.music_dir}")
self.music_library.update_library(user_dir)
if self.demo_url and not isdir(self._demo_dir):
LOG.info(f"Downloading Demo Music from: {self.demo_url}")
self._download_demo_tracks()
elif isdir(self._demo_dir):
self.music_library.update_library(self._demo_dir)
self.library_update_event.set()
@ocp_search()
def search_music(self, phrase, media_type=MediaType.GENERIC):
if not self.library_update_event.wait(5):
LOG.warning("Library update in progress; results may be limited")
results = self.search_artist(phrase, media_type) + \
self.search_album(phrase, media_type) + \
self.search_genre(phrase, media_type) + \
self.search_track(phrase, media_type)
if not results and self.voc_match(phrase, 'local.voc'):
score = 60
if media_type == MediaType.MUSIC:
score += 20
else:
LOG.debug("No media type requested")
all_songs = self.music_library.all_songs
non_demo = [s for s in all_songs
if not s.path.startswith(self._demo_dir)]
if non_demo:
LOG.debug("Using non-demo tracks")
all_songs = non_demo
if len(all_songs) > 50:
all_songs = sample(self.music_library.all_songs, 50)
results = self._tracks_to_search_results(all_songs, score)
LOG.info(f"Returning all songs with score={score}")
LOG.info(f"Returning {len(results)} results")
return results
def search_artist(self, phrase, media_type=MediaType.GENERIC) -> List[dict]:
score = 65
if media_type == MediaType.MUSIC:
score += 20
if self.voc_match(phrase, 'local.voc'):
score += 20
tracks = self.music_library.search_songs_for_artist(phrase)
LOG.debug(f"Found {len(tracks)} artist results")
return self._tracks_to_search_results(tracks, score)
def search_album(self, phrase, media_type=MediaType.GENERIC) -> List[dict]:
score = 70
if media_type == MediaType.MUSIC:
score += 20
if self.voc_match(phrase, 'local.voc'):
score += 20
tracks = self.music_library.search_songs_for_album(phrase)
LOG.debug(f"Found {len(tracks)} album results")
return self._tracks_to_search_results(tracks, score)
def search_genre(self, phrase, media_type=MediaType.GENERIC) -> List[dict]:
score = 50
if media_type == MediaType.MUSIC:
score += 20
if self.voc_match(phrase, 'local.voc'):
score += 20
tracks = self.music_library.search_songs_for_genre(phrase)
LOG.debug(f"Found {len(tracks)} genre results")
return self._tracks_to_search_results(tracks, score)
def search_track(self, phrase, media_type=MediaType.GENERIC) -> List[dict]:
score = 75
if media_type == MediaType.MUSIC:
score += 20
if self.voc_match(phrase, 'local.voc'):
score += 20
tracks = self.music_library.search_songs_for_track(phrase)
LOG.debug(f"Found {len(tracks)} track results")
return self._tracks_to_search_results(tracks, score)
def _tracks_to_search_results(self, tracks: List[Track], score: int = 20):
# TODO: Lower confidence if path is in demo dir
tracks = [{'media_type': MediaType.MUSIC,
'playback': PlaybackType.AUDIO,
'image': track.artwork if track.artwork else None,
'skill_icon': self._image_url,
'uri': track.path,
'title': track.title,
'artist': track.artist,
'length': track.duration_ms,
'match_confidence': score} for track in tracks]
return tracks
def _download_demo_tracks(self):
from ovos_skill_installer import download_extract_zip
download_extract_zip(self.demo_url, self._demo_dir)
self.music_library.update_library(self._demo_dir)