forked from cmnybo/nrsc5-gui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnrsc5_gui.py
executable file
·1157 lines (985 loc) · 47.2 KB
/
nrsc5_gui.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# NRSC5 GUI - A graphical interface for nrsc5
# Copyright (C) 2017-2019 Cody Nybo & Clayton Smith
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import glob
import io
import json
import logging
import math
import os
import queue
import re
import sys
import threading
import time
from datetime import datetime, timezone
from PIL import Image, ImageFont, ImageDraw
import pyaudio
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, GObject, Gdk, GdkPixbuf, GLib
import nrsc5
class NRSC5GUI(object):
AUDIO_SAMPLE_RATE = 44100
AUDIO_SAMPLES_PER_FRAME = 2048
MAP_FILE = "map.png"
VERSION = "2.0.0"
log_level = 20 # decrease to 10 to enable debug logs
def __init__(self):
logging.basicConfig(level=self.log_level,
format="%(asctime)s %(levelname)-5s %(filename)s:%(lineno)d: %(message)s",
datefmt="%H:%M:%S")
GObject.threads_init()
self.get_controls() # get controls and windows
self.init_stream_info() # initilize stream info and clear status widgets
self.radio = None
self.audio_queue = queue.Queue(maxsize=64)
self.audio_thread = threading.Thread(target=self.audio_worker)
self.playing = False
self.status_timer = None
self.image_changed = False
self.xhdr_changed = False
self.last_image = ""
self.last_xhdr = ""
self.station_str = "" # current station frequency (string)
self.stream_num = 0
self.bookmarks = []
self.station_logos = {}
self.bookmarked = False
self.map_viewer = None
self.weather_maps = [] # list of current weathermaps sorted by time
self.traffic_map = Image.new("RGB", (600, 600), "white")
self.map_tiles = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
self.map_data = {
"map_mode": 1,
"weather_time": 0,
"weather_pos": [0, 0, 0, 0],
"weather_now": "",
"weather_id": "",
"viewer_config": {
"mode": 1,
"animate": False,
"scale": True,
"window_pos": (0, 0),
"window_size": (782, 632),
"animation_speed": 0.5
}
}
# setup bookmarks listview
name_renderer = Gtk.CellRendererText()
name_renderer.set_property("editable", True)
name_renderer.connect("edited", self.on_bookmark_name_edited)
col_station = Gtk.TreeViewColumn("Station", Gtk.CellRendererText(), text=0)
col_name = Gtk.TreeViewColumn("Name", name_renderer, text=1)
col_station.set_resizable(True)
col_station.set_sort_column_id(2)
col_name.set_resizable(True)
col_name.set_sort_column_id(1)
self.lv_bookmarks.append_column(col_station)
self.lv_bookmarks.append_column(col_name)
self.load_settings()
self.process_weather_maps()
self.audio_thread.start()
def display_logo(self):
if self.station_str in self.station_logos:
# show station logo if it's cached
logo = os.path.join(self.aas_dir, self.station_logos[self.station_str][self.stream_num])
if os.path.isfile(logo):
self.stream_info["logo"] = self.station_logos[self.station_str][self.stream_num]
pixbuf = GdkPixbuf.Pixbuf.new_from_file(logo)
pixbuf = pixbuf.scale_simple(200, 200, GdkPixbuf.InterpType.HYPER)
self.img_cover.set_from_pixbuf(pixbuf)
else:
# add entry in database for the station if it doesn't exist
self.station_logos[self.station_str] = ["", "", "", ""]
def on_btn_play_clicked(self, _btn):
"""start playback"""
if not self.playing:
# update all of the spin buttons to prevent the text from sticking
self.spin_freq.update()
self.spin_stream.update()
self.spin_gain.update()
self.spin_ppm.update()
self.spin_rtl.update()
# start the timer
self.status_timer = threading.Timer(1, self.check_status)
self.status_timer.start()
# disable the controls
self.spin_freq.set_sensitive(False)
self.spin_gain.set_sensitive(False)
self.spin_ppm.set_sensitive(False)
self.spin_rtl.set_sensitive(False)
self.btn_play.set_sensitive(False)
self.btn_stop.set_sensitive(True)
self.cb_auto_gain.set_sensitive(False)
self.playing = True
self.last_xhdr = ""
self.play()
self.station_str = str(self.spin_freq.get_value())
self.stream_num = int(self.spin_stream.get_value())-1
self.display_logo()
# check if station is bookmarked
self.bookmarked = False
freq = int((self.spin_freq.get_value()+0.005)*100) + int(self.spin_stream.get_value())
for bookmark in self.bookmarks:
if bookmark[2] == freq:
self.bookmarked = True
break
self.btn_bookmark.set_sensitive(not self.bookmarked)
if self.notebook_main.get_current_page() != 3:
self.btn_delete.set_sensitive(self.bookmarked)
def on_btn_stop_clicked(self, _btn):
"""stop playback"""
if self.playing:
self.playing = False
# shutdown nrsc5
if self.radio:
self.radio.stop()
self.radio.close()
self.radio = None
# stop timer
self.status_timer.cancel()
self.status_timer = None
# enable controls
if not self.cb_auto_gain.get_active():
self.spin_gain.set_sensitive(True)
self.spin_freq.set_sensitive(True)
self.spin_ppm.set_sensitive(True)
self.spin_rtl.set_sensitive(True)
self.btn_play.set_sensitive(True)
self.btn_stop.set_sensitive(False)
self.btn_bookmark.set_sensitive(False)
self.cb_auto_gain.set_sensitive(True)
# clear stream info
self.init_stream_info()
self.btn_bookmark.set_sensitive(False)
if self.notebook_main.get_current_page() != 3:
self.btn_delete.set_sensitive(False)
def on_btn_bookmark_clicked(self, _btn):
# pack frequency and channel number into one int
freq = int((self.spin_freq.get_value()+0.005)*100) + int(self.spin_stream.get_value())
# create bookmark
bookmark = [
"{:4.1f}-{:1.0f}".format(self.spin_freq.get_value(), self.spin_stream.get_value()),
self.stream_info["callsign"],
freq
]
self.bookmarked = True
self.bookmarks.append(bookmark)
self.ls_bookmarks.append(bookmark)
self.btn_bookmark.set_sensitive(False)
if self.notebook_main.get_current_page() != 3:
self.btn_delete.set_sensitive(True)
def on_btn_delete_clicked(self, _btn):
# select current station if not on bookmarks page
if self.notebook_main.get_current_page() != 3:
station = int((self.spin_freq.get_value()+0.005)*100) + int(self.spin_stream.get_value())
for i in range(len(self.ls_bookmarks)):
if self.ls_bookmarks[i][2] == station:
self.lv_bookmarks.set_cursor(i)
break
# get station of selected row
model, tree_iter = self.lv_bookmarks.get_selection().get_selected()
station = model.get_value(tree_iter, 2)
# remove row
model.remove(tree_iter)
# remove bookmark
for i in range(len(self.bookmarks)):
if self.bookmarks[i][2] == station:
self.bookmarks.pop(i)
break
if self.notebook_main.get_current_page() != 3 and self.playing:
self.btn_bookmark.set_sensitive(True)
self.bookmarked = False
def on_btn_about_activate(self, _btn):
"""sets up and displays about dialog"""
if self.about_dialog:
self.about_dialog.present()
return
authors = [
"Cody Nybo <cmnybo@gmail.com>",
"Clayton Smith <argilo@gmail.com>",
]
nrsc5_gui_license = """
NRSC5 GUI - A graphical interface for nrsc5
Copyright (C) 2017-2019 Cody Nybo & Clayton Smith
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>."""
about_dialog = Gtk.AboutDialog()
about_dialog.set_transient_for(self.main_window)
about_dialog.set_destroy_with_parent(True)
about_dialog.set_name("NRSC5 GUI")
about_dialog.set_version(self.VERSION)
about_dialog.set_copyright("Copyright © 2017-2019 Cody Nybo & Clayton Smith")
about_dialog.set_website("https://github.com/cmnybo/nrsc5-gui")
about_dialog.set_comments("A graphical interface for nrsc5.")
about_dialog.set_authors(authors)
about_dialog.set_license(nrsc5_gui_license)
about_dialog.set_logo(GdkPixbuf.Pixbuf.new_from_file("logo.png"))
# callbacks for destroying the dialog
def close(dialog, _response, editor):
editor.about_dialog = None
dialog.destroy()
def delete_event(_dialog, _event, editor):
editor.about_dialog = None
return True
about_dialog.connect("response", close, self)
about_dialog.connect("delete-event", delete_event, self)
self.about_dialog = about_dialog
about_dialog.show()
def on_spin_stream_value_changed(self, _spin):
self.last_xhdr = ""
self.stream_info["title"] = ""
self.stream_info["album"] = ""
self.stream_info["artist"] = ""
self.stream_info["cover"] = ""
self.stream_info["logo"] = ""
self.stream_info["bitrate"] = 0
self.stream_num = int(self.spin_stream.get_value())-1
if self.playing:
self.display_logo()
def on_cb_auto_gain_toggled(self, btn):
self.spin_gain.set_sensitive(not btn.get_active())
self.lbl_gain.set_visible(btn.get_active())
def on_lv_bookmarks_row_activated(self, treeview, path, _view_column):
if path:
# get station from bookmark row
tree_iter = treeview.get_model().get_iter(path[0])
station = treeview.get_model().get_value(tree_iter, 2)
# set frequency and stream
self.spin_freq.set_value(float(int(station/10)/10.0))
self.spin_stream.set_value(station % 10)
# stop playback if playing
if self.playing:
self.on_btn_stop_clicked(None)
# play bookmarked station
self.on_btn_play_clicked(None)
def on_lv_bookmarks_sel_changed(self, _tree_selection):
# enable delete button if bookmark is selected
_, pathlist = self.lv_bookmarks.get_selection().get_selected_rows()
self.btn_delete.set_sensitive(len(pathlist) != 0)
def on_bookmark_name_edited(self, _cell, path, text, _data=None):
# update name in listview
tree_iter = self.ls_bookmarks.get_iter(path)
self.ls_bookmarks.set(tree_iter, 1, text)
# update name in bookmarks array
for bookmark in self.bookmarks:
if bookmark[2] == self.ls_bookmarks[path][2]:
bookmark[1] = text
break
def on_notebook_main_switch_page(self, _notebook, _page, page_num):
# disable delete button if not on bookmarks page and station is not bookmarked
if page_num != 3 and (not self.bookmarked or not self.playing):
self.btn_delete.set_sensitive(False)
# enable delete button if not on bookmarks page and station is bookmarked
elif page_num != 3 and self.bookmarked:
self.btn_delete.set_sensitive(True)
# enable delete button if on bookmarks page and a bookmark is selected
else:
_, tree_iter = self.lv_bookmarks.get_selection().get_selected()
self.btn_delete.set_sensitive(tree_iter is not None)
def on_rad_map_toggled(self, btn):
if btn.get_active():
if btn == self.rad_map_traffic:
self.map_data["map_mode"] = 0
map_file = os.path.join("map", "traffic_map.png")
if os.path.isfile(map_file):
map_img = Image.open(map_file).resize((200, 200), Image.LANCZOS)
self.img_map.set_from_pixbuf(img_to_pixbuf(map_img))
else:
self.img_map.set_from_stock(Gtk.STOCK_MISSING_IMAGE, Gtk.IconSize.LARGE_TOOLBAR)
elif btn == self.rad_map_weather:
self.map_data["map_mode"] = 1
if os.path.isfile(self.map_data["weather_now"]):
map_img = Image.open(self.map_data["weather_now"]).resize((200, 200), Image.LANCZOS)
self.img_map.set_from_pixbuf(img_to_pixbuf(map_img))
else:
self.img_map.set_from_stock(Gtk.STOCK_MISSING_IMAGE, Gtk.IconSize.LARGE_TOOLBAR)
def on_btn_map_clicked(self, _btn):
"""open map viewer window"""
if self.map_viewer is None:
self.map_viewer = NRSC5Map(self, self.map_viewer_callback, self.map_data)
self.map_viewer.map_window.show()
def map_viewer_callback(self):
"""delete the map viewer"""
self.map_viewer = None
def play(self):
self.radio = nrsc5.NRSC5(lambda type, evt: self.callback(type, evt))
self.radio.open(int(self.spin_rtl.get_value()))
self.radio.set_auto_gain(self.cb_auto_gain.get_active())
self.radio.set_freq_correction(int(self.spin_ppm.get_value()))
# set gain if auto gain is not selected
if not self.cb_auto_gain.get_active():
self.stream_info["gain"] = self.spin_gain.get_value()
self.radio.set_gain(self.stream_info["gain"])
self.radio.set_frequency(self.spin_freq.get_value() * 1e6)
self.radio.start()
def check_status(self):
"""update status information"""
def update():
Gdk.threads_enter()
try:
image_path = ""
image = ""
ber = [self.stream_info["ber"][i]*100 for i in range(4)]
self.txt_title.set_text(self.stream_info["title"])
self.txt_artist.set_text(self.stream_info["artist"])
self.txt_album.set_text(self.stream_info["album"])
self.lbl_bitrate.set_label("{:3.1f} kbps".format(self.stream_info["bitrate"]))
self.lbl_bitrate2.set_label("{:3.1f} kbps".format(self.stream_info["bitrate"]))
self.lbl_error.set_label("{:2.2f}% Error ".format(ber[1]))
self.lbl_callsign.set_label(" " + self.stream_info["callsign"])
self.lbl_name.set_label(self.stream_info["callsign"])
self.lbl_slogan.set_label(self.stream_info["slogan"])
self.lbl_slogan.set_tooltip_text(self.stream_info["slogan"])
self.lbl_mer_lower.set_label("{:1.2f} dB".format(self.stream_info["mer"][0]))
self.lbl_mer_upper.set_label("{:1.2f} dB".format(self.stream_info["mer"][1]))
self.lbl_ber_now.set_label("{:1.3f}% (Now)".format(ber[0]))
self.lbl_ber_avg.set_label("{:1.3f}% (Avg)".format(ber[1]))
self.lbl_ber_min.set_label("{:1.3f}% (Min)".format(ber[2]))
self.lbl_ber_max.set_label("{:1.3f}% (Max)".format(ber[3]))
if self.cb_auto_gain.get_active():
self.spin_gain.set_value(self.stream_info["gain"])
self.lbl_gain.set_label("{:2.1f}dB".format(self.stream_info["gain"]))
if self.last_xhdr == 0:
image_path = os.path.join(self.aas_dir, self.stream_info["cover"])
image = self.stream_info["cover"]
elif self.last_xhdr == 1:
image_path = os.path.join(self.aas_dir, self.stream_info["logo"])
image = self.stream_info["logo"]
if not os.path.isfile(image_path):
self.img_cover.clear()
# resize and display image if it changed and exists
if self.xhdr_changed and self.last_image != image and os.path.isfile(image_path):
self.xhdr_changed = False
self.last_image = image
pixbuf = GdkPixbuf.Pixbuf.new_from_file(image_path)
pixbuf = pixbuf.scale_simple(200, 200, GdkPixbuf.InterpType.HYPER)
self.img_cover.set_from_pixbuf(pixbuf)
logging.debug("Image changed")
finally:
Gdk.threads_leave()
if self.playing:
GObject.idle_add(update)
self.status_timer = threading.Timer(1, self.check_status)
self.status_timer.start()
def process_traffic_map(self, filename, data):
regex = re.compile(r"^TMT_.*_([1-3])_([1-3])_(\d{8}_\d{4}).*$")
match = regex.match(filename)
if match:
tile_x = int(match.group(1))-1
tile_y = int(match.group(2))-1
utc_time = datetime.strptime(match.group(3), "%Y%m%d_%H%M").replace(tzinfo=timezone.utc)
timestamp = int(utc_time.timestamp())
# check if the tile has already been loaded
if self.map_tiles[tile_x][tile_y] == timestamp:
return # no need to recreate the map if it hasn't changed
logging.debug("Got traffic map tile: %s, %s", tile_x, tile_y)
self.map_tiles[tile_x][tile_y] = timestamp
self.traffic_map.paste(Image.open(io.BytesIO(data)), (tile_y*200, tile_x*200))
# check if all of the tiles are loaded
if self.check_tiles(timestamp):
logging.debug("Got complete traffic map")
self.traffic_map.save(os.path.join("map", "traffic_map.png"))
# display on map page
if self.rad_map_traffic.get_active():
img_map = self.traffic_map.resize((200, 200), Image.LANCZOS)
self.img_map.set_from_pixbuf(img_to_pixbuf(img_map))
if self.map_viewer is not None:
self.map_viewer.updated()
def process_weather_overlay(self, filename, data):
regex = re.compile(r"^DWRO_(.*)_.*_(\d{8}_\d{4}).*$")
match = regex.match(filename)
if match:
utc_time = datetime.strptime(match.group(2), "%Y%m%d_%H%M").replace(tzinfo=timezone.utc)
timestamp = int(utc_time.timestamp())
map_id = self.map_data["weather_id"]
if match.group(1) != map_id:
logging.error("Received weather overlay with the wrong ID: %s", match.group(1))
return
if self.map_data["weather_time"] == timestamp:
return # no need to recreate the map if it hasn't changed
logging.debug("Got weather overlay")
self.map_data["weather_time"] = timestamp
weather_map_path = os.path.join("map", "weather_map_{}_{}.png".format(map_id, timestamp))
# create weather map
try:
map_path = os.path.join("map", "base_map_" + map_id + ".png")
if not os.path.isfile(map_path):
self.make_base_map(self.map_data["weather_id"], self.map_data["weather_pos"])
img_map = Image.open(map_path).convert("RGBA")
timestamp_pos = (img_map.size[0]-235, img_map.size[1]-29)
img_ts = self.make_timestamp(utc_time.astimezone(), img_map.size, timestamp_pos)
img_radar = Image.open(io.BytesIO(data)).convert("RGBA")
img_radar = img_radar.resize(img_map.size, Image.LANCZOS)
img_map = Image.alpha_composite(img_map, img_radar)
img_map = Image.alpha_composite(img_map, img_ts)
img_map.save(weather_map_path)
self.map_data["weather_now"] = weather_map_path
# display on map page
if self.rad_map_weather.get_active():
img_map = img_map.resize((200, 200), Image.LANCZOS)
self.img_map.set_from_pixbuf(img_to_pixbuf(img_map))
self.process_weather_maps() # get rid of old maps and add new ones to the list
if self.map_viewer is not None:
self.map_viewer.updated()
except OSError:
logging.error("Error creating weather map")
self.map_data["weather_time"] = 0
def process_weather_info(self, data):
weather_id = None
weather_pos = None
for line in data.decode().split("\n"):
if "DWR_Area_ID=" in line:
regex = re.compile("^DWR_Area_ID=\"(.+)\"$")
match = regex.match(line)
weather_id = match.group(1)
elif "Coordinates=" in line:
regex = re.compile(r"^Coordinates=.*\((.*),(.*)\).*\((.*),(.*)\).*$")
match = regex.match(line)
weather_pos = [float(match.group(i)) for i in range(1, 5)]
if weather_id is not None and weather_pos is not None:
if self.map_data["weather_id"] != weather_id or self.map_data["weather_pos"] != weather_pos:
logging.debug("Got position: (%n, %n) (%n, %n)", *weather_pos)
self.map_data["weather_id"] = weather_id
self.map_data["weather_pos"] = weather_pos
self.make_base_map(weather_id, weather_pos)
self.weather_maps = []
self.process_weather_maps()
def process_weather_maps(self):
number_of_maps = 0
regex = re.compile("^map.weather_map_([a-zA-Z0-9]+)_([0-9]+).png")
now = time.time()
files = glob.glob(os.path.join("map", "weather_map_") + "*.png")
files.sort()
for file in files:
match = regex.match(file)
if match:
map_id = match.group(1)
timestamp = int(match.group(2))
# remove weather maps older than 12 hours
if now - timestamp > 60*60*12:
try:
if file in self.weather_maps:
self.weather_maps.pop(self.weather_maps.index(file))
os.remove(file)
logging.debug("Deleted old weather map: %s", file)
except OSError:
logging.error("Failed to delete old weather map: %s", file)
# skip if not the correct location
elif map_id == self.map_data["weather_id"]:
if file not in self.weather_maps:
self.weather_maps.append(file)
number_of_maps += 1
logging.debug("Found %s weather maps", number_of_maps)
@staticmethod
def map_image_coordinates(lat_degrees, lon_degrees):
"""convert latitude & longitude to x & y cooordinates in the map"""
first_tile_x, first_tile_y = 35, 84
zoom_level = 8
tile_size = 256
map_x = (1 + math.radians(lon_degrees) / math.pi) / 2
map_y = (1 - math.asinh(math.tan(math.radians(lat_degrees))) / math.pi) / 2
tile_x = map_x * (2**zoom_level) - first_tile_x
tile_y = map_y * (2**zoom_level) - first_tile_y
return int(round(tile_x * tile_size)), int(round(tile_y * tile_size))
def make_base_map(self, map_id, pos):
"""crop the map to the area needed for weather radar"""
map_path = os.path.join("map", "base_map_" + map_id + ".png")
if os.path.isfile(self.MAP_FILE):
if not os.path.isfile(map_path):
logging.debug("Creating new map: %s", map_path)
map_upper_left = self.map_image_coordinates(pos[0], pos[1])
map_lower_right = self.map_image_coordinates(pos[2], pos[3])
map_img = Image.open(self.MAP_FILE).crop(map_upper_left + map_lower_right)
map_img.save(map_path)
logging.debug("Finished creating map")
else:
logging.error("Map file not found: %s", self.MAP_FILE)
map_img = Image.new("RGBA", (pos[2]-pos[1], pos[3]-pos[1]), "white")
map_img.save(map_path)
def check_tiles(self, timestamp):
"""check if all the tiles have been received"""
for i in range(3):
for j in range(3):
if self.map_tiles[i][j] != timestamp:
return False
return True
@staticmethod
def make_timestamp(local_time, size, pos):
"""create a timestamp image to overlay on the weathermap"""
pos_x, pos_y = pos
text = datetime.strftime(local_time, "%Y-%m-%d %H:%M")
img_ts = Image.new("RGBA", size, (0, 0, 0, 0))
draw = ImageDraw.Draw(img_ts)
font = ImageFont.truetype("DejaVuSansMono.ttf", 24)
draw.rectangle((pos_x, pos_y, pos_x+231, pos_y+25), outline="black", fill=(128, 128, 128, 96))
draw.text((pos_x+3, pos_y), text, fill="black", font=font)
return img_ts
def audio_worker(self):
audio = pyaudio.PyAudio()
try:
index = audio.get_default_output_device_info()["index"]
stream = audio.open(format=pyaudio.paInt16,
channels=2,
rate=self.AUDIO_SAMPLE_RATE,
output_device_index=index,
output=True)
except OSError:
logging.warning("No audio output device available")
stream = None
while True:
samples = self.audio_queue.get()
if samples is None:
break
if stream:
stream.write(samples)
self.audio_queue.task_done()
if stream:
stream.stop_stream()
stream.close()
audio.terminate()
def update_bitrate(self, bits):
kbps = bits * self.AUDIO_SAMPLE_RATE / self.AUDIO_SAMPLES_PER_FRAME / 1000
if self.stream_info["bitrate"] == 0:
self.stream_info["bitrate"] = kbps
else:
self.stream_info["bitrate"] = 0.99 * self.stream_info["bitrate"] + 0.01 * kbps
def update_ber(self, cber):
ber = self.stream_info["ber"]
if ber[0] == ber[1] == ber[2] == ber[3] == 0:
ber[0] = cber
ber[1] = cber
ber[2] = cber
ber[3] = cber
else:
ber[0] = cber
ber[1] = 0.9 * ber[1] + 0.1 * cber
if cber < ber[2]:
ber[2] = cber
if cber > ber[3]:
ber[3] = cber
def callback(self, evt_type, evt):
if evt_type == nrsc5.EventType.LOST_DEVICE:
pass # TODO: update the GUI?
elif evt_type == nrsc5.EventType.SYNC:
self.stream_info["gain"] = self.radio.get_gain()
# TODO: update the GUI?
elif evt_type == nrsc5.EventType.LOST_SYNC:
pass # TODO: update the GUI?
elif evt_type == nrsc5.EventType.MER:
self.stream_info["mer"] = [evt.lower, evt.upper]
elif evt_type == nrsc5.EventType.BER:
self.update_ber(evt.cber)
elif evt_type == nrsc5.EventType.HDC:
if evt.program == self.stream_num:
self.update_bitrate(len(evt.data) * 8)
elif evt_type == nrsc5.EventType.AUDIO:
if evt.program == self.stream_num:
self.audio_queue.put(evt.data)
elif evt_type == nrsc5.EventType.ID3:
if evt.program == self.stream_num:
if evt.title:
self.stream_info["title"] = evt.title
if evt.artist:
self.stream_info["artist"] = evt.artist
if evt.album:
self.stream_info["album"] = evt.album
if evt.xhdr:
if evt.xhdr.param != self.last_xhdr:
self.last_xhdr = evt.xhdr.param
self.xhdr_changed = True
logging.debug("XHDR changed: %s", evt.xhdr.param)
elif evt_type == nrsc5.EventType.SIG:
for service in evt:
if service.type == nrsc5.ServiceType.AUDIO:
for component in service.components:
if component.type == nrsc5.ComponentType.DATA:
if component.data.mime == nrsc5.MIMEType.PRIMARY_IMAGE:
self.streams[service.number-1]["image"] = component.data.port
elif component.data.mime == nrsc5.MIMEType.STATION_LOGO:
self.streams[service.number-1]["logo"] = component.data.port
elif service.type == nrsc5.ServiceType.DATA:
for component in service.components:
if component.type == nrsc5.ComponentType.DATA:
if component.data.mime == nrsc5.MIMEType.TTN_STM_TRAFFIC:
self.traffic_port = component.data.port
elif component.data.mime == nrsc5.MIMEType.TTN_STM_WEATHER:
self.weather_port = component.data.port
elif evt_type == nrsc5.EventType.LOT:
logging.debug("LOT port=%s", evt.port)
if self.map_dir is not None:
if evt.port == self.traffic_port:
if evt.name.startswith("TMT_"):
self.process_traffic_map(evt.name, evt.data)
elif evt.port == self.weather_port:
if evt.name.startswith("DWRO_"):
self.process_weather_overlay(evt.name, evt.data)
elif evt.name.startswith("DWRI_"):
self.process_weather_info(evt.data)
if self.aas_dir is not None:
path = os.path.join(self.aas_dir, evt.name)
for i, stream in enumerate(self.streams):
if evt.port == stream.get("image"):
logging.debug("Got album cover: %s", evt.name)
with open(path, "wb") as file:
file.write(evt.data)
if i == self.stream_num:
self.stream_info["cover"] = evt.name
elif evt.port == stream.get("logo"):
logging.debug("Got station logo: %s", evt.name)
with open(path, "wb") as file:
file.write(evt.data)
self.station_logos[self.station_str][i] = evt.name
if i == self.stream_num:
self.stream_info["logo"] = evt.name
elif evt_type == nrsc5.EventType.SIS:
if evt.name:
self.stream_info["callsign"] = evt.name
if evt.slogan:
self.stream_info["slogan"] = evt.slogan
def get_controls(self):
# setup gui
builder = Gtk.Builder()
builder.add_from_file("main_form.glade")
builder.connect_signals(self)
# Windows
self.main_window = builder.get_object("main_window")
self.main_window.connect("delete-event", self.shutdown)
self.main_window.connect("destroy", Gtk.main_quit)
self.about_dialog = None
# get controls
self.notebook_main = builder.get_object("notebook_main")
self.img_cover = builder.get_object("img_cover")
self.img_map = builder.get_object("img_map")
self.spin_freq = builder.get_object("spin_freq")
self.spin_stream = builder.get_object("spin_stream")
self.spin_gain = builder.get_object("spin_gain")
self.spin_ppm = builder.get_object("spin_ppm")
self.spin_rtl = builder.get_object("spin_rtl")
self.cb_auto_gain = builder.get_object("cb_auto_gain")
self.btn_play = builder.get_object("btn_play")
self.btn_stop = builder.get_object("btn_stop")
self.btn_bookmark = builder.get_object("btn_bookmark")
self.btn_delete = builder.get_object("btn_delete")
self.rad_map_traffic = builder.get_object("rad_map_traffic")
self.rad_map_weather = builder.get_object("rad_map_weather")
self.txt_title = builder.get_object("txt_title")
self.txt_artist = builder.get_object("txt_artist")
self.txt_album = builder.get_object("txt_album")
self.lbl_name = builder.get_object("lbl_name")
self.lbl_slogan = builder.get_object("lbl_slogan")
self.lbl_callsign = builder.get_object("lbl_callsign")
self.lbl_gain = builder.get_object("lbl_gain")
self.lbl_bitrate = builder.get_object("lbl_bitrate")
self.lbl_bitrate2 = builder.get_object("lbl_bitrate2")
self.lbl_error = builder.get_object("lbl_error")
self.lbl_mer_lower = builder.get_object("lbl_mer_lower")
self.lbl_mer_upper = builder.get_object("lbl_mer_upper")
self.lbl_ber_now = builder.get_object("lbl_ber_now")
self.lbl_ber_avg = builder.get_object("lbl_ber_avg")
self.lbl_ber_min = builder.get_object("lbl_ber_min")
self.lbl_ber_max = builder.get_object("lbl_ber_max")
self.lv_bookmarks = builder.get_object("lv_bookmarks")
self.ls_bookmarks = Gtk.ListStore(str, str, int)
self.lv_bookmarks.set_model(self.ls_bookmarks)
self.lv_bookmarks.get_selection().connect("changed", self.on_lv_bookmarks_sel_changed)
def init_stream_info(self):
self.stream_info = {
"callsign": "",
"slogan": "",
"title": "",
"album": "",
"artist": "",
"cover": "",
"logo": "",
"bitrate": 0,
"mer": [0, 0],
"ber": [0, 0, 0, 0],
"gain": 0
}
self.streams = [{}, {}, {}, {}]
self.traffic_port = -1
self.weather_port = -1
# clear status info
self.lbl_callsign.set_label("")
self.lbl_bitrate.set_label("")
self.lbl_bitrate2.set_label("")
self.lbl_error.set_label("")
self.lbl_gain.set_label("")
self.txt_title.set_text("")
self.txt_artist.set_text("")
self.txt_album.set_text("")
self.img_cover.clear()
self.lbl_name.set_label("")
self.lbl_slogan.set_label("")
self.lbl_slogan.set_tooltip_text("")
self.lbl_mer_lower.set_label("")
self.lbl_mer_upper.set_label("")
self.lbl_ber_now.set_label("")
self.lbl_ber_avg.set_label("")
self.lbl_ber_min.set_label("")
self.lbl_ber_max.set_label("")
def load_settings(self):
try:
with open("station_logos.json", mode="r") as file:
self.station_logos = json.load(file)
except (OSError, json.decoder.JSONDecodeError):
logging.warning("Unable to load station logo database")
# load settings
try:
with open("config.json", mode="r") as file:
config = json.load(file)
if "map_data" in config:
self.map_data = config["map_data"]
if self.map_data["map_mode"] == 0:
self.rad_map_traffic.set_active(True)
self.rad_map_traffic.toggled()
elif self.map_data["map_mode"] == 1:
self.rad_map_weather.set_active(True)
self.rad_map_weather.toggled()
if "width" and "height" in config:
self.main_window.resize(config["width"], config["height"])
self.main_window.move(config["window_x"], config["window_y"])
self.spin_freq.set_value(config["frequency"])
self.spin_stream.set_value(config["stream"])
self.spin_gain.set_value(config["gain"])
self.cb_auto_gain.set_active(config["auto_gain"])
self.spin_ppm.set_value(config["ppm_error"])
self.spin_rtl.set_value(config["rtl"])
self.bookmarks = config["bookmarks"]
for bookmark in self.bookmarks:
self.ls_bookmarks.append(bookmark)
except (OSError, json.decoder.JSONDecodeError, KeyError):
logging.warning("Unable to load config")
# create aas directory
self.aas_dir = os.path.join(sys.path[0], "aas")
if not os.path.isdir(self.aas_dir):
try:
os.mkdir(self.aas_dir)
except OSError:
logging.error("Unable to create AAS directory")
self.aas_dir = None
# create map directory
self.map_dir = os.path.join(sys.path[0], "map")
if not os.path.isdir(self.map_dir):
try:
os.mkdir(self.map_dir)
except OSError:
logging.error("Unable to create map directory")
self.map_dir = None
def shutdown(self, *_args):
# stop map viewer animation if it's running
if self.map_viewer is not None and self.map_viewer.animate_timer is not None:
self.map_viewer.animate_timer.cancel()
self.map_viewer.animate_stop = True
while self.map_viewer.animate_busy:
logging.debug("Animation busy - stopping")
if self.map_viewer.animate_timer is not None:
self.map_viewer.animate_timer.cancel()
time.sleep(0.25)
self.playing = False
# kill nrsc5 if it's running
if self.radio:
self.radio.stop()
self.radio.close()
self.radio = None
# shut down status timer if it's running
if self.status_timer is not None:
self.status_timer.cancel()
self.audio_queue.put(None)
self.audio_thread.join()
# save settings
try:
with open("config.json", mode="w") as file:
window_x, window_y = self.main_window.get_position()
width, height = self.main_window.get_size()
config = {
"config_version": self.VERSION,
"window_x": window_x,
"window_y": window_y,
"width": width,
"height": height,
"frequency": self.spin_freq.get_value(),
"stream": int(self.spin_stream.get_value()),
"gain": self.spin_gain.get_value(),
"auto_gain": self.cb_auto_gain.get_active(),
"ppm_error": int(self.spin_ppm.get_value()),
"rtl": int(self.spin_rtl.get_value()),
"bookmarks": self.bookmarks,
"map_data": self.map_data,
}
# sort bookmarks
config["bookmarks"].sort(key=lambda t: t[2])
json.dump(config, file, indent=2)
with open("station_logos.json", mode="w") as file:
json.dump(self.station_logos, file, indent=2)
except OSError:
logging.error("Unable to save config")
class NRSC5Map(object):
def __init__(self, parent, callback, data):
# setup gui
builder = Gtk.Builder()
builder.add_from_file("map_form.glade")
builder.connect_signals(self)
self.parent = parent
self.callback = callback
self.data = data # map data
self.animate_timer = None
self.animate_busy = False
self.animate_stop = False
self.weather_maps = parent.weather_maps # list of weather maps sorted by time
self.map_index = 0 # the index of the next weather map to display
# get the controls
self.map_window = builder.get_object("map_window")
self.img_map = builder.get_object("img_map")
self.rad_map_weather = builder.get_object("rad_map_weather")