-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhackfm.py
More file actions
1335 lines (1100 loc) · 50.8 KB
/
hackfm.py
File metadata and controls
1335 lines (1100 loc) · 50.8 KB
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
# -*- coding: utf-8 -*-
#
# GNU Radio Python Flow Graph
# Title: FM Playlist Transmitter
# GNU Radio version: 3.10.11.0
from gnuradio import analog
from gnuradio import blocks
import pmt
from gnuradio import filter
from gnuradio.filter import firdes
from gnuradio import gr
from gnuradio.fft import window
import sys
import signal
from argparse import ArgumentParser
from gnuradio.eng_arg import eng_float, intx
from gnuradio import eng_notation
import osmosdr
import time
import threading
import os
import numpy as np
import wave
import shutil
import subprocess
import tempfile
import math # 添加 math 库用于计算 FM 灵敏度
import sys
import msvcrt # Windows控制台输入
from enum import Enum
try:
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
PYQT5_AVAILABLE = True
except ImportError:
PYQT5_AVAILABLE = False
print("PyQt5未安装,请运行: pip install PyQt5")
class PlayMode(Enum):
SEQUENTIAL = "顺序播放"
SHUFFLE = "随机播放"
REPEAT_ONE = "单曲循环"
class PlaybackController:
def __init__(self, playlist_source):
self.playlist_source = playlist_source
self.play_mode = PlayMode.SEQUENTIAL
self.paused = False
self.seek_offset = 0 # 跳转偏移量(秒)
self.current_file_pos = 0 # 当前文件播放位置(秒)
self.last_update_time = time.time()
def set_play_mode(self, mode):
"""设置播放模式"""
if isinstance(mode, PlayMode):
self.play_mode = mode
else:
# 字符串转换
mode_map = {
'1': PlayMode.SEQUENTIAL,
'2': PlayMode.SHUFFLE,
'3': PlayMode.REPEAT_ONE
}
if mode in mode_map:
self.play_mode = mode_map[mode]
# 更新播放源的配置
if self.playlist_source:
if self.play_mode == PlayMode.SHUFFLE:
self.playlist_source.shuffle = True
else:
self.playlist_source.shuffle = False
def toggle_pause(self):
"""暂停/继续播放"""
self.paused = not self.paused
return self.paused
def seek_forward(self, seconds=10):
"""前进指定秒数"""
self.seek_offset += seconds
def seek_backward(self, seconds=10):
"""后退指定秒数"""
self.seek_offset -= seconds
if self.seek_offset < 0:
self.seek_offset = 0
def next_track(self):
"""下一曲"""
if self.playlist_source:
self.playlist_source.next_file()
self.seek_offset = 0
self.current_file_pos = 0
def previous_track(self):
"""上一曲"""
if self.playlist_source and len(self.playlist_source.file_list) > 0:
# 回到上一首或当前歌曲重新开始
if self.current_file_pos > 3: # 如果播放超过3秒,重新开始当前歌曲
self.seek_offset = 0
self.current_file_pos = 0
else: # 否则回到上一首
self.playlist_source.current_file_idx -= 2
if self.playlist_source.current_file_idx < -1:
self.playlist_source.current_file_idx = len(self.playlist_source.file_list) - 2
self.playlist_source.next_file()
self.seek_offset = 0
self.current_file_pos = 0
def update_position(self):
"""更新播放位置"""
if not self.paused:
current_time = time.time()
time_diff = current_time - self.last_update_time
self.current_file_pos += time_diff
self.last_update_time = current_time
# 处理跳转
if abs(self.seek_offset) > 0.1 and self.playlist_source and self.playlist_source.current_file:
try:
# 计算目标位置
current_pos = self.playlist_source.current_file.tell()
bytes_per_second = 44100 * 2 * 2 # 44100Hz, 2声道, 2字节/sample
target_pos = current_pos + int(self.seek_offset * bytes_per_second)
# 确保位置在有效范围内
if target_pos >= 44: # WAV文件头44字节
self.playlist_source.current_file.seek(target_pos)
else:
self.playlist_source.current_file.seek(44)
self.seek_offset = 0
except Exception:
pass # 如果跳转失败,忽略
def get_current_info(self):
"""获取当前播放信息"""
if not self.playlist_source or not self.playlist_source.current_file:
return "无播放信息"
current_file = self.playlist_source.current_file_path
if not current_file:
return "无播放信息"
# 获取文件名
file_name = os.path.basename(current_file)
# 获取播放位置
position = self.current_file_pos + self.seek_offset
if position < 0:
position = 0
# 格式化时间
minutes = int(position // 60)
seconds = int(position % 60)
# 获取播放模式
mode_text = self.play_mode.value
# 获取状态
status = "暂停" if self.paused else "播放中"
return f"[{status}] {file_name} | {minutes:02d}:{seconds:02d} | {mode_text}"
class DisplayManager:
def __init__(self, controller):
self.controller = controller
self.running = True
self.display_thread = None
self.input_thread = None
def start(self):
"""启动显示和输入监控线程"""
self.display_thread = threading.Thread(target=self._display_loop, daemon=True)
self.input_thread = threading.Thread(target=self._input_loop, daemon=True)
self.display_thread.start()
self.input_thread.start()
def stop(self):
"""停止所有线程"""
self.running = False
def _display_loop(self):
"""显示循环,每秒更新一次"""
while self.running:
try:
# 更新播放位置
self.controller.update_position()
# 获取当前信息
info = self.controller.get_current_info()
# 清空当前行并显示信息
print(f"\r{' ' * 100}\r{info}", end='', flush=True)
time.sleep(1) # 每秒更新一次
except Exception as e:
print(f"\r显示错误: {e}", end='', flush=True)
time.sleep(1)
def _input_loop(self):
"""输入监控循环"""
print("\n\n播放控制命令:")
print("空格键 - 暂停/继续")
print("n - 下一曲")
print("p - 上一曲")
print("→ - 前进10秒")
print("← - 后退10秒")
print("1 - 顺序播放")
print("2 - 随机播放")
print("3 - 单曲循环")
print("q - 退出")
print("-" * 50)
while self.running:
try:
if msvcrt.kbhit(): # 检查是否有按键
key = msvcrt.getch().decode('utf-8', errors='ignore').lower()
if key == ' ': # 空格键 - 暂停/继续
paused = self.controller.toggle_pause()
status = "已暂停" if paused else "继续播放"
print(f"\r{status}", end='', flush=True)
elif key == 'n': # 下一曲
self.controller.next_track()
print(f"\r下一曲", end='', flush=True)
elif key == 'p': # 上一曲
self.controller.previous_track()
print(f"\r上一曲", end='', flush=True)
elif key == '\xe0': # 特殊键(方向键)
# 读取第二个字节
key2 = msvcrt.getch().decode('utf-8', errors='ignore')
if key2 == 'M': # 右箭头 - 前进
self.controller.seek_forward()
print(f"\r前进10秒", end='', flush=True)
elif key2 == 'K': # 左箭头 - 后退
self.controller.seek_backward()
print(f"\r后退10秒", end='', flush=True)
elif key in ['1', '2', '3']: # 播放模式
mode_map = {'1': '顺序播放', '2': '随机播放', '3': '单曲循环'}
self.controller.set_play_mode(key)
print(f"\r切换到{mode_map[key]}", end='', flush=True)
elif key == 'q': # 退出
print(f"\r正在退出...")
self.running = False
break
time.sleep(0.1)
except Exception as e:
print(f"\r输入错误: {e}", end='', flush=True)
class FMApplicationGUI(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("FM发射器控制面板")
self.setGeometry(100, 100, 800, 600)
# FM发射器相关
self.fm_console = None
self.controller = None
self.is_playing = False
self.update_timer = None
# 创建界面
self.create_widgets()
# 设置窗口图标(如果有的话)
# self.setWindowIcon(QIcon('icon.png'))
def create_widgets(self):
"""创建界面组件"""
# 创建中央部件
central_widget = QWidget()
self.setCentralWidget(central_widget)
# 主布局
main_layout = QVBoxLayout(central_widget)
main_layout.setSpacing(10)
main_layout.setContentsMargins(10, 10, 10, 10)
# 标题
title_label = QLabel("FM发射器控制面板")
title_label.setAlignment(Qt.AlignCenter)
title_font = QFont("微软雅黑", 16, QFont.Bold)
title_label.setFont(title_font)
main_layout.addWidget(title_label)
# 主要内容区域 - 水平布局
content_layout = QHBoxLayout()
main_layout.addLayout(content_layout)
# 左侧控制面板
control_group = QGroupBox("播放控制")
control_layout = QVBoxLayout(control_group)
# 播放控制按钮
self.play_pause_btn = QPushButton("▶ 开始播放")
self.play_pause_btn.setFont(QFont("微软雅黑", 12, QFont.Bold))
self.play_pause_btn.setStyleSheet("""
QPushButton {
background-color: #4CAF50;
color: white;
border: none;
padding: 10px;
border-radius: 5px;
min-width: 120px;
}
QPushButton:hover {
background-color: #45a049;
}
QPushButton:pressed {
background-color: #3d8b40;
}
""")
self.play_pause_btn.clicked.connect(self.toggle_play_pause)
control_layout.addWidget(self.play_pause_btn, alignment=Qt.AlignCenter)
# 导航按钮
nav_layout = QHBoxLayout()
self.prev_btn = QPushButton("⏮ 上一曲")
self.prev_btn.setFont(QFont("微软雅黑", 10))
self.prev_btn.clicked.connect(self.previous_track)
nav_layout.addWidget(self.prev_btn)
self.next_btn = QPushButton("⏭ 下一曲")
self.next_btn.setFont(QFont("微软雅黑", 10))
self.next_btn.clicked.connect(self.next_track)
nav_layout.addWidget(self.next_btn)
control_layout.addLayout(nav_layout)
# 跳转按钮
seek_layout = QHBoxLayout()
self.seek_back_btn = QPushButton("⏪ 后退10秒")
self.seek_back_btn.setFont(QFont("微软雅黑", 10))
self.seek_back_btn.clicked.connect(self.seek_backward)
seek_layout.addWidget(self.seek_back_btn)
self.seek_forward_btn = QPushButton("⏩ 前进10秒")
self.seek_forward_btn.setFont(QFont("微软雅黑", 10))
self.seek_forward_btn.clicked.connect(self.seek_forward)
seek_layout.addWidget(self.seek_forward_btn)
control_layout.addLayout(seek_layout)
# 播放模式
mode_group = QGroupBox("播放模式")
mode_layout = QVBoxLayout(mode_group)
self.play_mode_group = QButtonGroup()
self.mode_sequential = QRadioButton("顺序播放")
self.mode_sequential.setChecked(True)
self.mode_shuffle = QRadioButton("随机播放")
self.mode_repeat = QRadioButton("单曲循环")
self.play_mode_group.addButton(self.mode_sequential, 1)
self.play_mode_group.addButton(self.mode_shuffle, 2)
self.play_mode_group.addButton(self.mode_repeat, 3)
self.play_mode_group.buttonClicked.connect(self.change_play_mode)
mode_layout.addWidget(self.mode_sequential)
mode_layout.addWidget(self.mode_shuffle)
mode_layout.addWidget(self.mode_repeat)
control_layout.addWidget(mode_group)
# 参数设置
param_group = QGroupBox("发射参数")
param_layout = QFormLayout(param_group)
self.freq_input = QLineEdit("100.0")
self.freq_input.setFont(QFont("微软雅黑", 10))
param_layout.addRow("频率 (MHz):", self.freq_input)
self.power_input = QLineEdit("30")
self.power_input.setFont(QFont("微软雅黑", 10))
param_layout.addRow("功率 (dB):", self.power_input)
control_layout.addWidget(param_group)
# 音乐目录
dir_group = QGroupBox("音乐目录")
dir_layout = QVBoxLayout(dir_group)
self.dir_label = QLabel("未选择目录")
self.dir_label.setFont(QFont("微软雅黑", 10))
self.dir_label.setWordWrap(True)
dir_layout.addWidget(self.dir_label)
self.browse_btn = QPushButton("📁 选择目录")
self.browse_btn.setFont(QFont("微软雅黑", 10))
self.browse_btn.clicked.connect(self.browse_directory)
dir_layout.addWidget(self.browse_btn)
control_layout.addWidget(dir_group)
# 添加弹簧
control_layout.addStretch()
content_layout.addWidget(control_group)
# 右侧信息显示
info_group = QGroupBox("播放信息")
info_layout = QVBoxLayout(info_group)
# 当前播放信息
self.current_song_label = QLabel("当前无播放")
self.current_song_label.setFont(QFont("微软雅黑", 12, QFont.Bold))
self.current_song_label.setWordWrap(True)
info_layout.addWidget(self.current_song_label)
self.time_label = QLabel("时间: 00:00")
self.time_label.setFont(QFont("微软雅黑", 10))
info_layout.addWidget(self.time_label)
self.mode_label = QLabel("模式: 顺序播放")
self.mode_label.setFont(QFont("微软雅黑", 10))
info_layout.addWidget(self.mode_label)
self.status_label = QLabel("状态: 停止")
self.status_label.setFont(QFont("微软雅黑", 10))
info_layout.addWidget(self.status_label)
# 播放列表
playlist_group = QGroupBox("播放列表")
playlist_layout = QVBoxLayout(playlist_group)
self.playlist_widget = QListWidget()
self.playlist_widget.setFont(QFont("微软雅黑", 10))
playlist_layout.addWidget(self.playlist_widget)
info_layout.addWidget(playlist_group)
content_layout.addWidget(info_group)
# 状态栏
self.status_bar = QStatusBar()
self.setStatusBar(self.status_bar)
self.status_bar.showMessage("就绪")
def browse_directory(self):
"""浏览音乐目录"""
directory = QFileDialog.getExistingDirectory(self, "选择音乐目录")
if directory:
self.dir_entry.setText(directory)
self.update_playlist_display(directory)
def update_playlist_display(self, directory):
"""更新播放列表显示"""
self.playlist_widget.clear()
try:
# 查找音频文件
audio_files = []
valid_extensions = ('.wav', '.mp3', '.flac', '.ogg')
for root, dirs, files in os.walk(directory):
files = sorted(files)
for file in files:
if file.lower().endswith(valid_extensions):
audio_files.append(file)
# 添加到列表框
for file in audio_files:
self.playlist_widget.addItem(file)
self.status_bar.showMessage(f"找到 {len(audio_files)} 个音频文件")
except Exception as e:
QMessageBox.critical(self, "错误", f"读取目录失败: {str(e)}")
def toggle_play_pause(self):
"""切换播放/暂停状态"""
if not self.dir_entry.text() or self.dir_entry.text() == "":
QMessageBox.warning(self, "警告", "请先选择音乐目录")
return
try:
if not self.is_playing:
self.start_playback()
else:
self.pause_playback()
except Exception as e:
QMessageBox.critical(self, "错误", f"播放控制失败: {str(e)}")
def start_playback(self):
"""开始播放"""
# 获取参数
freq_mhz = float(self.freq_entry.text())
power_db = int(self.power_entry.text())
directory = self.dir_entry.text()
# 转换为Hz
freq_hz = int(freq_mhz * 1e6)
# 创建FM发射器
self.fm_console = FM_console(music_dir=directory, freq=freq_hz, power=power_db)
self.controller = self.fm_console.controller
# 启动发射器
self.fm_console.start()
self.fm_console.flowgraph_started.set()
self.is_playing = True
self.play_pause_btn.setText("⏸ 暂停播放")
self.status_bar.showMessage("正在播放")
# 开始更新显示
self.update_display()
def pause_playback(self):
"""暂停播放"""
if self.controller:
self.controller.toggle_pause()
if self.controller.paused:
self.play_pause_btn.setText("▶ 继续播放")
self.status_bar.showMessage("已暂停")
else:
self.play_pause_btn.setText("⏸ 暂停播放")
self.status_bar.showMessage("正在播放")
def stop_playback(self):
"""停止播放"""
if self.fm_console:
self.fm_console.stop()
self.fm_console.wait()
self.fm_console = None
self.controller = None
self.is_playing = False
self.play_pause_btn.setText("▶ 开始播放")
self.status_bar.showMessage("已停止")
self.current_song_label.setText("当前无播放")
self.time_label.setText("时间: 00:00")
# 停止更新定时器
if self.update_timer:
self.update_timer.stop()
def next_track(self):
"""下一曲"""
if self.controller:
self.controller.next_track()
self.status_bar.showMessage("切换到下一曲")
def previous_track(self):
"""上一曲"""
if self.controller:
self.controller.previous_track()
self.status_bar.showMessage("切换到上一曲")
def seek_forward(self):
"""前进10秒"""
if self.controller:
self.controller.seek_forward()
self.status_bar.showMessage("前进10秒")
def seek_backward(self):
"""后退10秒"""
if self.controller:
self.controller.seek_backward()
self.status_bar.showMessage("后退10秒")
def change_play_mode(self):
"""改变播放模式"""
if self.controller:
if self.seq_radio.isChecked():
mode = "1"
mode_text = "顺序播放"
elif self.shuffle_radio.isChecked():
mode = "2"
mode_text = "随机播放"
elif self.repeat_radio.isChecked():
mode = "3"
mode_text = "单曲循环"
else:
return
self.controller.set_play_mode(mode)
self.status_bar.showMessage(f"切换到{mode_text}")
def update_display(self):
"""更新显示信息"""
if self.controller and self.is_playing:
try:
# 更新播放位置
self.controller.update_position()
# 获取当前信息
info = self.controller.get_current_info()
# 解析信息
if "无播放信息" not in info:
# 提取文件名
if "]" in info and "|" in info:
parts = info.split("|")
if len(parts) >= 2:
status_file = parts[0].strip()
time_part = parts[1].strip()
mode_part = parts[2].strip() if len(parts) > 2 else ""
# 提取文件名
if "]" in status_file:
file_name = status_file.split("]")[1].strip()
self.current_song_label.setText(file_name)
# 更新时间
self.time_label.setText(f"时间: {time_part}")
# 更新模式
self.mode_label.setText(f"模式: {mode_part}")
# 更新状态
if "暂停" in status_file:
self.status_label.setText("状态: 暂停")
else:
self.status_label.setText("状态: 播放中")
# 继续更新
self.update_timer = QTimer()
self.update_timer.timeout.connect(self.update_display)
self.update_timer.start(1000) # 1秒更新一次
except Exception as e:
self.status_bar.showMessage(f"更新显示错误: {str(e)}")
self.update_timer = QTimer()
self.update_timer.timeout.connect(self.update_display)
self.update_timer.start(1000) # 1秒更新一次
def closeEvent(self, event):
"""窗口关闭事件处理"""
if self.is_playing:
reply = QMessageBox.question(self, '退出', '正在播放中,确定要退出吗?',
QMessageBox.Yes | QMessageBox.No,
QMessageBox.No)
if reply == QMessageBox.Yes:
self.stop_playback()
event.accept()
else:
event.ignore()
else:
event.accept()
def run(self):
"""运行GUI应用"""
self.show() # 显示窗口
# 注意:PyQt5的事件循环将在主程序的app.exec_()中运行
def create_widgets(self):
"""创建界面组件"""
# 创建中央部件
central_widget = QWidget()
self.setCentralWidget(central_widget)
# 主布局
main_layout = QVBoxLayout(central_widget)
main_layout.setSpacing(10)
main_layout.setContentsMargins(10, 10, 10, 10)
# 标题
title_label = QLabel("FM发射器控制面板")
title_label.setAlignment(Qt.AlignCenter)
title_font = QFont("微软雅黑", 16, QFont.Bold)
title_label.setFont(title_font)
main_layout.addWidget(title_label)
# 主要内容区域 - 水平布局
content_layout = QHBoxLayout()
main_layout.addLayout(content_layout)
# 左侧控制面板
control_group = QGroupBox("播放控制")
control_layout = QVBoxLayout(control_group)
# 播放控制按钮
self.play_pause_btn = QPushButton("▶ 开始播放")
self.play_pause_btn.setFont(QFont("微软雅黑", 12, QFont.Bold))
self.play_pause_btn.setStyleSheet("""
QPushButton {
background-color: #4CAF50;
color: white;
border: none;
padding: 10px;
border-radius: 5px;
min-width: 120px;
}
QPushButton:hover {
background-color: #45a049;
}
QPushButton:pressed {
background-color: #3d8b40;
}
""")
self.play_pause_btn.clicked.connect(self.toggle_play_pause)
control_layout.addWidget(self.play_pause_btn, alignment=Qt.AlignCenter)
# 导航按钮(上一曲/下一曲)
nav_layout = QHBoxLayout()
self.prev_btn = QPushButton("⏮ 上一曲")
self.prev_btn.setFont(QFont("微软雅黑", 10))
self.prev_btn.clicked.connect(self.previous_track)
nav_layout.addWidget(self.prev_btn)
self.next_btn = QPushButton("⏭ 下一曲")
self.next_btn.setFont(QFont("微软雅黑", 10))
self.next_btn.clicked.connect(self.next_track)
nav_layout.addWidget(self.next_btn)
control_layout.addLayout(nav_layout)
# 跳转按钮(前进/后退)
seek_layout = QHBoxLayout()
self.seek_back_btn = QPushButton("⏪ 后退10秒")
self.seek_back_btn.setFont(QFont("微软雅黑", 10))
self.seek_back_btn.clicked.connect(self.seek_backward)
seek_layout.addWidget(self.seek_back_btn)
self.seek_forward_btn = QPushButton("⏩ 前进10秒")
self.seek_forward_btn.setFont(QFont("微软雅黑", 10))
self.seek_forward_btn.clicked.connect(self.seek_forward)
seek_layout.addWidget(self.seek_forward_btn)
control_layout.addLayout(seek_layout)
# 播放模式选择
mode_group = QGroupBox("播放模式")
mode_layout = QVBoxLayout(mode_group)
self.play_mode_var = "1" # 默认顺序播放
self.seq_radio = QRadioButton("顺序播放")
self.seq_radio.setChecked(True)
self.seq_radio.clicked.connect(lambda: self.change_play_mode())
mode_layout.addWidget(self.seq_radio)
self.shuffle_radio = QRadioButton("随机播放")
self.shuffle_radio.clicked.connect(lambda: self.change_play_mode())
mode_layout.addWidget(self.shuffle_radio)
self.repeat_radio = QRadioButton("单曲循环")
self.repeat_radio.clicked.connect(lambda: self.change_play_mode())
mode_layout.addWidget(self.repeat_radio)
control_layout.addWidget(mode_group)
# 发射参数设置
param_group = QGroupBox("发射参数")
param_layout = QFormLayout(param_group)
self.freq_var = "100.0"
self.power_var = "30"
self.freq_entry = QLineEdit(self.freq_var)
self.freq_entry.setMaximumWidth(100)
param_layout.addRow("频率 (MHz):", self.freq_entry)
self.power_entry = QLineEdit(self.power_var)
self.power_entry.setMaximumWidth(100)
param_layout.addRow("功率 (dB):", self.power_entry)
control_layout.addWidget(param_group)
# 音乐目录选择
dir_group = QGroupBox("音乐目录")
dir_layout = QVBoxLayout(dir_group)
self.dir_var = ""
self.dir_entry = QLineEdit(self.dir_var)
self.dir_entry.setReadOnly(True)
dir_layout.addWidget(self.dir_entry)
self.browse_btn = QPushButton("📁 选择目录")
self.browse_btn.setFont(QFont("微软雅黑", 10))
self.browse_btn.clicked.connect(self.browse_directory)
dir_layout.addWidget(self.browse_btn)
control_layout.addWidget(dir_group)
# 添加弹簧使控件向上对齐
control_layout.addStretch()
content_layout.addWidget(control_group)
# 右侧信息显示区域
right_panel = QVBoxLayout()
# 播放信息组
info_group = QGroupBox("播放信息")
info_layout = QVBoxLayout(info_group)
self.current_song_label = QLabel("当前无播放")
self.current_song_label.setFont(QFont("微软雅黑", 12, QFont.Bold))
self.current_song_label.setWordWrap(True)
info_layout.addWidget(self.current_song_label)
self.time_label = QLabel("时间: 00:00")
self.time_label.setFont(QFont("微软雅黑", 10))
info_layout.addWidget(self.time_label)
self.mode_label = QLabel("模式: 顺序播放")
self.mode_label.setFont(QFont("微软雅黑", 10))
info_layout.addWidget(self.mode_label)
self.status_label = QLabel("状态: 停止")
self.status_label.setFont(QFont("微软雅黑", 10))
info_layout.addWidget(self.status_label)
right_panel.addWidget(info_group)
# 播放列表组
playlist_group = QGroupBox("播放列表")
playlist_layout = QVBoxLayout(playlist_group)
self.playlist_widget = QListWidget()
self.playlist_widget.setMaximumHeight(200)
playlist_layout.addWidget(self.playlist_widget)
right_panel.addWidget(playlist_group)
content_layout.addLayout(right_panel)
# 状态栏
self.status_bar = QStatusBar()
self.setStatusBar(self.status_bar)
self.status_bar.showMessage("就绪")
class playlist_file_source(gr.sync_block):
"""
Stream PCM data from all WAV/MP3 files in a directory (playlist) sequentially as shorts.
For MP3 (and for WAVs with incorrect sample rate), auto-convert on the fly into WAV with 44100Hz, original channels/sample format.
Uses ./temp folder for transient data. Does not downmix or change bit depth.
Loops through all files repeatedly if repeat=True.
音频自动归一化到满幅(-1~1区间),避免响度太小或炸音
[Update for Stereo]
Force conversion to 2 channels (Stereo). Outputs two streams (L, R).
"""
def __init__(self, dir_path, repeat=True, dtype=np.int16, chunk_size=4096, target_headroom=0.98, shuffle=False):
gr.sync_block.__init__(self,
name="playlist_file_source",
in_sig=None,
# [Stereo] 输出两个 int16 端口:左声道,右声道
out_sig=[np.int16, np.int16])
self.dir_path = dir_path
self.repeat = repeat
self.shuffle = shuffle
self.dtype = dtype
self.chunk_size = chunk_size
self.target_headroom = target_headroom # 保证最大值不会爆 1.0,防削波,典型取0.98~0.99
self.temp_dir = os.path.abspath('./temp')
os.makedirs(self.temp_dir, exist_ok=True)
self._clear_temp_dir()
self.file_list = self._find_audio_files()
if not self.file_list:
raise RuntimeError(f"No audio files found in {self.dir_path}")
# 随机播放模式:打乱文件列表
if self.shuffle:
import random
random.shuffle(self.file_list)
print(f"随机播放模式已启用,共 {len(self.file_list)} 首歌曲")
# 保存原始文件列表用于避免重复
self._played_indices = set()
self._original_file_list = self.file_list.copy()
self.current_file_idx = 0
self.current_file = None
self.current_file_path = None
self.current_gain = 1.0 # 会在 open_current_file 时更新
self.open_current_file()
def _clear_temp_dir(self):
for f in os.listdir(self.temp_dir):
path = os.path.join(self.temp_dir, f)
if os.path.isfile(path):
os.remove(path)
def _find_audio_files(self):
audio_files = []
# 添加更多支持的格式:.flac, .ogg, .mp3, .wav
valid_extensions = ('.wav', '.mp3', '.flac', '.ogg')
for root, dirs, files in os.walk(self.dir_path):
files = sorted(files)
for file in files:
if file.lower().endswith(valid_extensions):
audio_files.append(os.path.join(root, file))
return audio_files
def _get_wav_info(self, fname):
try:
with wave.open(fname, 'rb') as w:
sr = w.getframerate()
ch = w.getnchannels()
sw = w.getsampwidth()
return sr, ch, sw
except Exception:
return None, None, None
def _needs_conversion(self, fname):
# 检查是否为非 WAV 格式 (MP3, FLAC, OGG 等)
if fname.lower().endswith(('.mp3', '.flac', '.ogg')):
return True
sr, ch, sw = self._get_wav_info(fname)
# 关键修复:除了检查采样率,还必须检查声道数
# [Stereo] 如果不是 44100Hz 或者不是双声道(ch!=2),则需要转换
if sr != 44100 or ch != 2:
return True
return False
def _make_temp_wav(self, source_path):
base = os.path.basename(source_path)
name, _ext = os.path.splitext(base)
temp_wav = os.path.join(self.temp_dir, f"{name}_{int(time.time()*1e6)%1000000}.wav")
cmd = [
'ffmpeg', '-hide_banner', '-loglevel', 'error',
'-y',
'-i', source_path,
'-ar', '44100',
'-ac', '2', # 关键修复:[Stereo] 强制转换为双声道 (-ac 2)
temp_wav
]
ret = subprocess.call(cmd)
if ret != 0:
raise RuntimeError(f"ffmpeg failed to convert {source_path}")
return temp_wav
def _wav_max_abs(self, fpath):
# 只取最大幅度,避免读取全部造成内存溢出
try:
with wave.open(fpath, 'rb') as w:
sample_width = w.getsampwidth()
channels = w.getnchannels()
dtype = np.int16 if sample_width == 2 else None
if dtype is None:
return 32767 # 不支持其他类型,返回最大
max_abs = 0
buffer_size = 4096 * channels
while True:
data = w.readframes(buffer_size)
if not data:
break
arr = np.frombuffer(data, dtype=dtype)
if arr.size == 0:
continue
m = np.abs(arr).max()
if m > max_abs:
max_abs = m
return max_abs
except Exception:
return 32767
def open_current_file(self):
self._cleanup_old_temp()
real_path = self.file_list[self.current_file_idx]
# 检查是否需要转换(MP3/FLAC/OGG, 错误的采样率, 或非立体声)
if self._needs_conversion(real_path):
temp_wav = self._make_temp_wav(real_path)
self.current_file_path = temp_wav
else:
self.current_file_path = real_path
self.current_file = open(self.current_file_path, 'rb')
self.current_file.seek(44)
print(f"Now playing: {real_path}")
# 计算最大幅度,用于归一化
max_abs = self._wav_max_abs(self.current_file_path)
if max_abs == 0:
self.current_gain = 1.0
else:
self.current_gain = float(self.target_headroom * 32767.0 / max_abs)
# 不要超过2倍,过大说明采样值异常
if self.current_gain > 2.0:
self.current_gain = 2.0
print(f"Auto gain factor: {self.current_gain:.3f} (file max abs sample={max_abs})")
def _cleanup_old_temp(self):
if hasattr(self, 'current_file_path') and self.current_file_path:
if os.path.abspath(self.current_file_path).startswith(self.temp_dir) and os.path.exists(self.current_file_path):
try:
os.remove(self.current_file_path)
except Exception:
pass
def next_file(self):
if self.shuffle:
# 随机播放模式:随机选择下一首歌,避免重复播放同一首歌
import random
if len(self._played_indices) >= len(self.file_list):
# 所有歌曲都已播放过,重置播放记录
self._played_indices.clear()