Skip to content

Commit b2a53f9

Browse files
committed
- adding support for playback start, title and volume to TTS
1 parent 44342ed commit b2a53f9

File tree

6 files changed

+555
-236
lines changed

6 files changed

+555
-236
lines changed

pyradio/common.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ class Station(IntEnum):
6868
'press-?': ' Press ? for help',
6969
'error-str': 'error',
7070
'vol_': 'Vol: ',
71+
'volume_set': 'Volume set to ',
7172
'error-403': 'Server returned "Forbidden" (error 403)',
7273
'error-404': 'Station does not exist (error 404)',
7374
'error-503': 'Service not available (error 503)',

pyradio/log.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from importlib_resources import files, as_file # backport για 3.7–3.8
2121
from .common import STATES, M_STRINGS
2222
from .cjkwrap import cjklen, cjkslices
23+
from .tts import Priority
2324

2425
locale.setlocale(locale.LC_ALL, "")
2526

@@ -246,7 +247,8 @@ def __init__(self,
246247
config,
247248
current_player_id,
248249
active_player_id,
249-
get_web_song_title
250+
get_web_song_title,
251+
tts
250252
):
251253
self.program_restart = False
252254
self.error_msg = False
@@ -314,6 +316,7 @@ def __init__(self,
314316
self._stop_thread = False
315317
self.timer = None
316318
self._started_station_name = None
319+
self.tts = tts
317320

318321
def __del__(self):
319322
self._stop_desktop_notification_thread = True
@@ -411,6 +414,20 @@ def write(self,
411414
error_msg=False,
412415
notify_function=None):
413416
with self.lock:
417+
try:
418+
if self.tts and self.tts():
419+
if msg_id == STATES.TITLE \
420+
and msg and msg.startswith(M_STRINGS['title_']):
421+
self.tts().queue_speech(msg.replace(':', ',',1))
422+
pass
423+
elif msg_id == STATES.VOLUME:
424+
logger.error(f'{msg = }')
425+
s_msg = msg.split(']')[0].split(' ')[-1]
426+
logger.error(f'{s_msg = }')
427+
self.tts().queue_speech(M_STRINGS['volume_set'] + s_msg.split('%')[0] + ' percent', Priority.HIGH)
428+
except AttributeError:
429+
pass
430+
414431
current_player_id = self._current_player_id()
415432
active_player_id = self._active_player_id()
416433

pyradio/player.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import socket
1616
from shutil import copyfile as shutil_copy_file
1717
import locale
18+
from .tts import Priority
1819
locale.setlocale(locale.LC_ALL, "")
1920
# this is for windows...
2021
try:
@@ -340,6 +341,8 @@ class Player():
340341

341342
enable_per_station_volume = True
342343

344+
tts = None
345+
343346
def __init__(self,
344347
config,
345348
outputStream,
@@ -2129,6 +2132,7 @@ def play(self,
21292132
self.playback_is_on = False
21302133
self.delay_thread = None
21312134
self.outputStream.write(msg_id=STATES.CONNECT, msg=M_STRINGS['station_'] + name + M_STRINGS['station-open'], counter='')
2135+
self._speak(f'Playing station: {name}', Priority.NORMAL)
21322136
if logger.isEnabledFor(logging.INFO):
21332137
logger.info('Selected Station: ' + name)
21342138
if encoding:
@@ -2307,6 +2311,10 @@ def play(self,
23072311
if referer_file:
23082312
self.handle_old_referer(referer, referer_file)
23092313

2314+
def _speak(self, msg, priority):
2315+
if self.tts and self.tts():
2316+
self.tts().queue_speech(msg, priority)
2317+
23102318
def _sendCommand(self, command):
23112319
''' send keystroke command to player '''
23122320
if [x for x in ('q', 'shutdown') if command.startswith(x)]:
@@ -3923,7 +3931,10 @@ def get_volume(self, repeat=False):
39233931
sleep(1)
39243932
self.get_volume(repeat=True)
39253933
self.actual_volume = self.volume
3926-
self.volume = int(100 * self.actual_volume / self.max_volume)
3934+
try:
3935+
self.volume = int(100 * int(self.actual_volume) / int(self.max_volume))
3936+
except Exception as e:
3937+
logger.error(f'exception: {e}')
39273938
# logger.error('Final')
39283939
# logger.error('self.actual_volume = {}'.format(self.actual_volume))
39293940
# logger.error('self.volume = {}'.format(self.volume))

pyradio/radio.py

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
from .messages_system import PyRadioMessagesSystem
5959
from .server import PyRadioServer, HAS_NETIFACES
6060
from .keyboard import kbkey, get_lkbkey, get_unicode_and_cjk_char, dequeue_input, input_queue, set_kb_letter, check_localized, add_l10n_to_functions_dict, set_kb_cjk
61-
from .tts import TTSManager
61+
from .tts import TTSManager, Priority
6262
HAVE_CHARSET_NORMALIZER = True
6363
try:
6464
from .m3u import parse_m3u
@@ -263,7 +263,6 @@ def keypress(self, char):
263263

264264
class PyRadio():
265265
player = None
266-
ws = Window_Stack()
267266

268267
_i_am_resizing = False
269268
_redisplay_list = []
@@ -512,6 +511,7 @@ def __init__(self, pyradio_config,
512511
in log.write, if _current_player_id != _active_player_id
513512
do not display any message
514513
'''
514+
self.ws = Window_Stack(self._speak_selection)
515515
self.player = None
516516
# player data
517517
self._default_player_name = None
@@ -1047,7 +1047,7 @@ def _set_text_volume(self, vol):
10471047
# self._remote_control_server._send_text('Volume set!')
10481048
self.player.set_volume(vol)
10491049
sleep(.1)
1050-
return f'Volume set to: {vol}'
1050+
return M_STRINGS['volume_set'] + f'{vol}'
10511051
else:
10521052
if self.player.muted:
10531053
return 'Player is Muted!'
@@ -1251,7 +1251,8 @@ def setup(self, stdscr):
12511251
self._cnf,
12521252
lambda: self._current_player_id,
12531253
lambda: self._active_player_id,
1254-
lambda: self._remote_control_server
1254+
lambda: self._remote_control_server,
1255+
lambda: self.tts
12551256
)
12561257
self.log.program_restart = self.program_restart
12571258
self.program_restart = False
@@ -1287,6 +1288,7 @@ def setup(self, stdscr):
12871288
self.player.log = self.log
12881289
self.player.handle_old_referer = self._handle_old_referer
12891290
self.player.update_bitrate = self._update_bitrate
1291+
self.player.tts = lambda: self.tts
12901292
if self._request_recording:
12911293
if not (platform.startswith('win') and \
12921294
self.player.PLAYER_NAME == 'vlc'):
@@ -2217,6 +2219,8 @@ def run(self):
22172219

22182220
self._cnf.setup_mouse()
22192221

2222+
self._speak_selection()
2223+
22202224
if not self._cnf.use_themes \
22212225
and not self._cnf.no_themes_notification_shown \
22222226
and not self._cnf.no_themes_from_command_line:
@@ -4191,7 +4195,8 @@ def _open_playlist(self, a_url=None):
41914195
if not self._cnf._online_browser.initialize():
41924196
''' browser cannot be opened '''
41934197
self._cnf.remove_from_playlist_history()
4194-
self.ws.close_window()
4198+
self.ws.close_window(no_tts=True)
4199+
self._speak_high('Error, cannot connect to Radio Browser')
41954200
self._print_service_connection_error()
41964201
self._cnf.browsing_station_service = False
41974202
self._cnf.online_browser = None
@@ -4202,6 +4207,7 @@ def _open_playlist(self, a_url=None):
42024207

42034208
''' make sure we don't send a wrong click '''
42044209
self._cnf._online_browser.search()
4210+
self._speak_selection()
42054211
else:
42064212
self._cnf.remove_from_playlist_history()
42074213
self._open_simple_message_by_key(
@@ -4635,6 +4641,7 @@ def _open_playlist_from_history(self,
46354641
# self._cnf.save_last_playlist()
46364642
if result:
46374643
self._normal_mode_resize()
4644+
self._speak_selection()
46384645
return result
46394646

46404647
def _get_station_id(self, find):
@@ -6659,6 +6666,7 @@ def _activate_player(self, player_name):
66596666
self.player.update_bitrate = self._update_bitrate
66606667
self.player.log = self.log
66616668
self.player.handle_old_referer = self._handle_old_referer
6669+
self.player.tts = lambda: self.tts
66626670
if not (self.player.PLAYER_NAME == 'vlc' and \
66636671
platform.startswith('win')):
66646672
self.player.recording = to_record
@@ -6747,11 +6755,17 @@ def _exit_program_or_playlist_mode(self):
67476755
self._cnf.EXTERNAL_PLAYER_OPTS = None
67486756
return False
67496757

6758+
def _speak_high(self, msg):
6759+
self.tts.queue_speech(msg, Priority.HIGH)
6760+
6761+
def _speak_normal(self, msg):
6762+
self.tts.queue_speech(msg, Priority.NORMAL)
6763+
67506764
def _speak_selection(self):
67516765
if self.ws.operation_mode == self.ws.NORMAL_MODE:
6752-
self.tts.speak(f'Selected station: {self.stations[self.selection][0]}')
6766+
self.tts.queue_speech(f'{self.selection+1}. {self.stations[self.selection][0]}', Priority.NORMAL)
67536767
elif self.ws.operation_mode == self.ws.PLAYLIST_MODE:
6754-
self.tts.speak(f'Selected playlist: {self.stations[self.selection][0]}')
6768+
self.tts.queue_speech(f'{self.selection+1}. {self.stations[self.selection][0]}', Priority.NORMAL)
67556769

67566770
def keypress(self, char):
67576771
''' PyRadio keypress '''
@@ -9760,6 +9774,7 @@ def keypress(self, char):
97609774
self._update_status_bar_right()
97619775
if self.number_of_items > 0:
97629776
self.setStation(-1)
9777+
self._speak_selection()
97639778
self.refreshBody()
97649779
return
97659780

@@ -9768,6 +9783,7 @@ def keypress(self, char):
97689783
self._jump_to_jumpnr(char)
97699784
self.refreshBody()
97709785
self._reset_status_bar_right()
9786+
self._speak_selection()
97719787
return
97729788

97739789
if char in map(ord, map(str, range(0, 10))):
@@ -9786,6 +9802,7 @@ def keypress(self, char):
97869802
self._update_status_bar_right()
97879803
self.setStation(0)
97889804
self.refreshBody()
9805+
self._speak_selection()
97899806
return
97909807

97919808
if char in (curses.KEY_EXIT, kbkey['q'], 27) or \

0 commit comments

Comments
 (0)