Extract device name to settings.py
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
# PXUA216MB-DL2-M Python例程
|
||||
|
||||
本项目是结合木瓜电子16麦板使用的python例程,请结合项目内注释使用
|
||||
|
||||
## 安装
|
||||
|
||||
本项目通过poetry实现包管理
|
||||
|
||||
关于poetry的安装及使用方法:[poetry](https://python-poetry.org/docs/)
|
||||
|
||||
2
poetry.lock
generated
2
poetry.lock
generated
@@ -55,7 +55,7 @@ numpy = ["numpy"]
|
||||
|
||||
[metadata]
|
||||
lock-version = "1.1"
|
||||
python-versions = "^3.10"
|
||||
python-versions = "^3.7"
|
||||
content-hash = "5c2b83e7e719e6d1491460d44a0dcf4161bca6d52bfd58df0f16654f8a40417e"
|
||||
|
||||
[metadata.files]
|
||||
|
||||
@@ -5,7 +5,7 @@ description = ""
|
||||
authors = ["VergilWang15 <s644906481@gmail.com>"]
|
||||
|
||||
[tool.poetry.dependencies]
|
||||
python = "^3.10"
|
||||
python = "^3.7"
|
||||
|
||||
[tool.poetry.dev-dependencies]
|
||||
sounddevice = "0.4.4"
|
||||
|
||||
@@ -8,11 +8,11 @@ def select_device(device_type='output'):
|
||||
i = 0
|
||||
for device in device_list: # 选择设备
|
||||
host_name = sd.query_hostapis(device['hostapi'])['name']
|
||||
if device_type == 'input' and 'PawPaw Microphone Array' in device['name'] and host_api in host_name \
|
||||
if device_type == 'input' and settings.DEVICE_NAME in device['name'] and host_api in host_name \
|
||||
and input_channel == device['max_input_channels']:
|
||||
print("找到一个PawPaw输入设备: " + str(i) + " " + device['name'])
|
||||
sd.default.device = i # 设置输入设备
|
||||
if device_type == 'output' and 'PawPaw Microphone Array' in device['name'] and host_api in host_name \
|
||||
if device_type == 'output' and settings.DEVICE_NAME in device['name'] and host_api in host_name \
|
||||
and output_channels == device['max_output_channels']:
|
||||
print("找到一个PawPaw输出设备: " + str(i) + " " + device['name'])
|
||||
sd.default.device = i # 设置输出设备
|
||||
@@ -20,8 +20,9 @@ def select_device(device_type='output'):
|
||||
|
||||
|
||||
def play_pcm_raw():
|
||||
# path = '../test_audio/test_audio_48k.raw' # 测试播放48k音频
|
||||
path = '../recorded_audio/testfile.raw' # 此处的.raw文件即为pcm格式音频
|
||||
# 去除不同的注释以播放不同的音频
|
||||
path = '../test_audio/test_audio_48k.raw' # 测试播放48k音频
|
||||
# path = '../recorded_audio/testfile.raw' # 此处的.raw文件即为pcm格式音频
|
||||
# 解析pcm文件并存放到数据流中
|
||||
# 需要4个变量:文件路径,音频通道数,采样率,音频子类型
|
||||
# 此处的音频位深为16位,故选用PCM_16,关于如何寻找音频的子类型,参考README.md
|
||||
@@ -36,6 +37,9 @@ def recording():
|
||||
fs = 48000 # 采样率
|
||||
select_device(device_type='input')
|
||||
my_recording = sd.rec(int(duration * fs), samplerate=fs, channels=18) # 将音频写入输入流
|
||||
# -------------------------------------
|
||||
# my_recording是一个二维数组,在此示例中存放了18个通道的sample数据
|
||||
# -------------------------------------
|
||||
sd.wait()
|
||||
|
||||
# 提取前两个通道的音频,保存为raw文件供测试播放
|
||||
@@ -46,6 +50,7 @@ def recording():
|
||||
stereo_array[flag][1] = frame[1]
|
||||
flag = flag + 1
|
||||
print(stereo_array)
|
||||
# 存储到文件 testfile.raw
|
||||
sf.write('../recorded_audio/testfile.raw', data=stereo_array, subtype='PCM_16', samplerate=fs)
|
||||
|
||||
|
||||
@@ -55,9 +60,10 @@ if __name__ == '__main__':
|
||||
output_channels = settings.MAX_OUTPUT_CHANNEL
|
||||
|
||||
device_list = sd.query_devices() # 获取电脑上的所有音频设备
|
||||
recording() # 播放音频
|
||||
print(device_list)
|
||||
recording() # 录制
|
||||
while True:
|
||||
play_pcm_raw()
|
||||
play_pcm_raw() # 播放pcm
|
||||
user_input = input("输入1继续播放,输入2退出测试")
|
||||
if user_input == '1':
|
||||
continue
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
HOST_API = 'WASAPI'
|
||||
MAX_INPUT_CHANNEL = 18
|
||||
MAX_OUTPUT_CHANNEL = 2
|
||||
DEVICE_NAME = 'USB Audio Device (UAC2.0)'
|
||||
|
||||
Reference in New Issue
Block a user