smartpi 1.1.0__py3-none-any.whl → 1.1.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- smartpi/__init__.py +3 -3
- smartpi/audio.py +125 -0
- {smartpi-1.1.0.dist-info → smartpi-1.1.2.dist-info}/METADATA +7 -1
- {smartpi-1.1.0.dist-info → smartpi-1.1.2.dist-info}/RECORD +6 -5
- {smartpi-1.1.0.dist-info → smartpi-1.1.2.dist-info}/WHEEL +0 -0
- {smartpi-1.1.0.dist-info → smartpi-1.1.2.dist-info}/top_level.txt +0 -0
smartpi/__init__.py
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
from ._gui import gui
|
|
2
2
|
from .base_driver import P1, P2, P3, P4, P5, P6, M1, M2, M3, M4, M5, M6
|
|
3
3
|
|
|
4
|
-
__all__ = ["base_driver","gui","ultrasonic","touch_sensor","temperature","humidity","light_sensor","color_sensor","motor","servo","led","flash",
|
|
5
|
-
"P1", "P2", "P3", "P4", "P5", "P6", "M1", "M2", "M3", "M4", "M5", "M6"]
|
|
4
|
+
__all__ = ["base_driver","gui","audio","ultrasonic","touch_sensor","temperature","humidity","light_sensor","color_sensor","motor","servo","led","flash",
|
|
5
|
+
"trace","move","local_model","P1", "P2", "P3", "P4", "P5", "P6", "M1", "M2", "M3", "M4", "M5", "M6"]
|
|
6
6
|
|
|
7
|
-
__version__ = "1.1.
|
|
7
|
+
__version__ = "1.1.2"
|
|
8
8
|
|
smartpi/audio.py
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
import os
|
|
3
|
+
import sys
|
|
4
|
+
import subprocess
|
|
5
|
+
import numpy as np
|
|
6
|
+
import math
|
|
7
|
+
|
|
8
|
+
# 全局导入,避免每次调用都重复导入
|
|
9
|
+
import warnings
|
|
10
|
+
warnings.filterwarnings('ignore')
|
|
11
|
+
|
|
12
|
+
# 在导入sounddevice前设置环境变量抑制警告
|
|
13
|
+
os.environ['PYTHONWARNINGS'] = 'ignore'
|
|
14
|
+
|
|
15
|
+
# 导入sounddevice
|
|
16
|
+
import sounddevice as sd
|
|
17
|
+
|
|
18
|
+
def record(seconds, filename="recording.wav"):
|
|
19
|
+
"""
|
|
20
|
+
录音函数
|
|
21
|
+
示例: record(10, "voice.wav")
|
|
22
|
+
"""
|
|
23
|
+
cmd = f"sudo record.sh -d {seconds} {filename}"
|
|
24
|
+
print(f"录音{seconds}秒 -> {filename}")
|
|
25
|
+
result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
|
|
26
|
+
if result.returncode == 0:
|
|
27
|
+
print(f"录音成功: {filename}")
|
|
28
|
+
return True
|
|
29
|
+
else:
|
|
30
|
+
print(f"录音失败: {result.stderr}")
|
|
31
|
+
return False
|
|
32
|
+
|
|
33
|
+
def get_db(sample_time=0.1):
|
|
34
|
+
"""
|
|
35
|
+
检测麦克风分贝值
|
|
36
|
+
返回: 0-100的整数分贝值
|
|
37
|
+
"""
|
|
38
|
+
try:
|
|
39
|
+
# 获取可用设备
|
|
40
|
+
devices = sd.query_devices()
|
|
41
|
+
|
|
42
|
+
# 选择第一个可用的输入设备
|
|
43
|
+
input_device = None
|
|
44
|
+
for i, dev in enumerate(devices):
|
|
45
|
+
if dev['max_input_channels'] > 0:
|
|
46
|
+
input_device = i
|
|
47
|
+
break
|
|
48
|
+
|
|
49
|
+
if input_device is None:
|
|
50
|
+
return 0
|
|
51
|
+
|
|
52
|
+
# 录音参数
|
|
53
|
+
samplerate = 44100
|
|
54
|
+
channels = 1
|
|
55
|
+
duration = sample_time
|
|
56
|
+
|
|
57
|
+
# 录音
|
|
58
|
+
recording = sd.rec(
|
|
59
|
+
int(duration * samplerate),
|
|
60
|
+
samplerate=samplerate,
|
|
61
|
+
channels=channels,
|
|
62
|
+
device=input_device,
|
|
63
|
+
blocking=True,
|
|
64
|
+
dtype='int16'
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
# 转换为numpy数组
|
|
68
|
+
audio_data = recording.flatten()
|
|
69
|
+
|
|
70
|
+
# 过滤静音
|
|
71
|
+
audio_data = audio_data[np.abs(audio_data) > 50]
|
|
72
|
+
|
|
73
|
+
if len(audio_data) == 0:
|
|
74
|
+
return 0
|
|
75
|
+
|
|
76
|
+
# 计算RMS
|
|
77
|
+
rms = np.sqrt(np.mean(np.square(audio_data.astype(np.float32))))
|
|
78
|
+
|
|
79
|
+
# 计算分贝值
|
|
80
|
+
if rms > 0:
|
|
81
|
+
db = 20 * math.log10(rms / 32767.0)
|
|
82
|
+
# 转换为0-100范围并取整
|
|
83
|
+
db_value = max(0, min(100, (db + 60) * (100/60)))
|
|
84
|
+
return int(db_value)
|
|
85
|
+
|
|
86
|
+
return 0
|
|
87
|
+
|
|
88
|
+
except Exception:
|
|
89
|
+
# 发生任何异常返回0
|
|
90
|
+
return 0
|
|
91
|
+
|
|
92
|
+
def play(audio_file):
|
|
93
|
+
"""
|
|
94
|
+
播放音频文件
|
|
95
|
+
示例: play("music.wav")
|
|
96
|
+
"""
|
|
97
|
+
if not os.path.exists(audio_file):
|
|
98
|
+
print(f"文件不存在: {audio_file}")
|
|
99
|
+
return False
|
|
100
|
+
|
|
101
|
+
try:
|
|
102
|
+
subprocess.run(f"aplay {audio_file}", shell=True, check=True)
|
|
103
|
+
return True
|
|
104
|
+
except subprocess.CalledProcessError as e:
|
|
105
|
+
print(f"播放失败: {str(e)}")
|
|
106
|
+
return False
|
|
107
|
+
except Exception as e:
|
|
108
|
+
print(f"播放错误: {str(e)}")
|
|
109
|
+
return False
|
|
110
|
+
|
|
111
|
+
def volume(level=None):
|
|
112
|
+
"""
|
|
113
|
+
调节音量百分比 (0-100)
|
|
114
|
+
示例:
|
|
115
|
+
volume(80) # 设置音量到80%
|
|
116
|
+
current = volume() # 获取当前音量
|
|
117
|
+
"""
|
|
118
|
+
|
|
119
|
+
try:
|
|
120
|
+
level = max(0, min(100, level))
|
|
121
|
+
subprocess.run(['amixer', '-q', 'sset', 'Master', f'{level}%'], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, check=True)
|
|
122
|
+
return level
|
|
123
|
+
except Exception as e:
|
|
124
|
+
print(f"音量操作错误: {str(e)}")
|
|
125
|
+
return 0 if level is None else level
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: smartpi
|
|
3
|
-
Version: 1.1.
|
|
3
|
+
Version: 1.1.2
|
|
4
4
|
Summary: A library use for H2-RCU
|
|
5
5
|
Author: ZMROBO
|
|
6
6
|
Classifier: Programming Language :: Python :: 3
|
|
@@ -21,3 +21,9 @@ V0.1.40
|
|
|
21
21
|
3、增加电源按键检测机制;
|
|
22
22
|
4、增加可调用模型和相关功能函数;
|
|
23
23
|
|
|
24
|
+
V1.1.1
|
|
25
|
+
1、修复块传输bug;
|
|
26
|
+
2、更新gui函数功能;
|
|
27
|
+
|
|
28
|
+
V1.1.2
|
|
29
|
+
1、增加了音频函数库;
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
smartpi/__init__.py,sha256=
|
|
1
|
+
smartpi/__init__.py,sha256=NAAJlU1Q3y8old45KZ79n3Gycbqet_HM1Q7bvANRCZc,392
|
|
2
2
|
smartpi/_gui.py,sha256=tUHNamQSvXUrYFNgWI5GexjL9mTy_qeW0xbIExkTGoM,2290
|
|
3
3
|
smartpi/ai_asr.py,sha256=wxh_1Klh8vJottAt19jq3qpOmM_Cw4DHQEPKpsHGhmE,40063
|
|
4
4
|
smartpi/ai_llm.py,sha256=-khBK2PMwbYUDvbaCTRCktE4dFloqfai4mHI-V8GEXM,36751
|
|
5
5
|
smartpi/ai_tts.py,sha256=OjAJs3XOykiufXuEDiaD0coKsWjknPdhwRpRzGLDZKU,36061
|
|
6
6
|
smartpi/ai_vad.py,sha256=9J1xxuKF8GvojkL_JuXv7_xE8bQAo5D49Xasi8piKK8,3630
|
|
7
|
+
smartpi/audio.py,sha256=Ca5rs-MhKNyNjvwVDyh4hpG4ssYG-MSO_I99MRsh8t0,3475
|
|
7
8
|
smartpi/base_driver.py,sha256=BwqgcYbQ9Rg8KiBJV2xy--NMNsMIg_zL-nnsAilCHJk,22687
|
|
8
9
|
smartpi/camera.py,sha256=AVpZsMpW-24CP3BOfarjmRawMJdTOZY7Crq7FeLOqb4,3292
|
|
9
10
|
smartpi/color_sensor.py,sha256=ckIXD81YnqPo6nENAnipNp3gY12FJ235QKj0e8Cul9E,521
|
|
@@ -70,7 +71,7 @@ smartpi/text_gte_model/config/tokenizer_config.json,sha256=w5RiDifbeIYy6vyGX5v94
|
|
|
70
71
|
smartpi/text_gte_model/config/vocab.txt,sha256=oi9hP3uz_8h8XoHNh6rgLnVdJbIEm75zKoSKM8HzsC8,84758
|
|
71
72
|
smartpi/text_gte_model/gte/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
72
73
|
smartpi/text_gte_model/gte/gte_model.onnx,sha256=XXYg6TUhzOx1SqAhp6ePDU0QgeK6DQEqHATMuQQJCNE,30468366
|
|
73
|
-
smartpi-1.1.
|
|
74
|
-
smartpi-1.1.
|
|
75
|
-
smartpi-1.1.
|
|
76
|
-
smartpi-1.1.
|
|
74
|
+
smartpi-1.1.2.dist-info/METADATA,sha256=4q1OkRMX3hTS4ZLB59J7gwvkHUCakadIeS0O94IjtvI,715
|
|
75
|
+
smartpi-1.1.2.dist-info/WHEEL,sha256=oiQVh_5PnQM0E3gPdiz09WCNmwiHDMaGer_elqB3coM,92
|
|
76
|
+
smartpi-1.1.2.dist-info/top_level.txt,sha256=PoLhUCmWAiQUg5UeN2fS-Y1iQyBbF2rdUlizXtpHGRQ,8
|
|
77
|
+
smartpi-1.1.2.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|