diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..a1e708e --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..3ed824e --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/pxua216mb-exp.iml b/.idea/pxua216mb-exp.iml new file mode 100644 index 0000000..9f89730 --- /dev/null +++ b/.idea/pxua216mb-exp.iml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..bead57e --- /dev/null +++ b/README.md @@ -0,0 +1,17 @@ +# PXUA216MB-DL2-M Python例程 + +## 安装 +本项目通过poetry实现包管理 + +关于poetry的安装及使用方法:[poetry](https://python-poetry.org/docs/) + +本项目依赖于开源项目 [sounddevice](https://python-sounddevice.readthedocs.io/en/0.4.4/installation.html) + +安装poetry后,简单使用```poetry install```即可安装依赖 + +## 使用 +###寻找一个格式的子类型: +``` +>>> import soundfile as sf +>>> sf.available_subtypes('RAW') +``` \ No newline at end of file diff --git a/recorded_audio/testfile.raw b/recorded_audio/testfile.raw new file mode 100644 index 0000000..ae8800a Binary files /dev/null and b/recorded_audio/testfile.raw differ diff --git a/src/__pycache__/settings.cpython-310.pyc b/src/__pycache__/settings.cpython-310.pyc new file mode 100644 index 0000000..5662f11 Binary files /dev/null and b/src/__pycache__/settings.cpython-310.pyc differ diff --git a/src/example.py b/src/example.py new file mode 100644 index 0000000..8f8ccc0 --- /dev/null +++ b/src/example.py @@ -0,0 +1,66 @@ +import sounddevice as sd +import soundfile as sf +import settings +import numpy as np + + +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 \ + 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 \ + and output_channels == device['max_output_channels']: + print("找到一个PawPaw输出设备: " + str(i) + " " + device['name']) + sd.default.device = i # 设置输出设备 + i = i + 1 + + +def play_pcm_raw(): + # path = '../test_audio/test_audio_48k.raw' + path = '../recorded_audio/testfile.raw' # 此处的.raw文件即为pcm格式音频 + # 解析pcm文件并存放到数据流中 + # 需要4个变量:文件路径,音频通道数,采样率,音频子类型 + # 此处的音频位深为16位,故选用PCM_16,关于如何寻找音频的子类型,参考README.md + select_device(device_type='output') + data, fs = sf.read(path, channels=2, samplerate=48000, subtype='PCM_16') + sd.play(data, fs) + sd.wait() + + +def recording(): + duration = 3 # 录制时间 + fs = 48000 # 采样率 + select_device(device_type='input') + my_recording = sd.rec(int(duration * fs), samplerate=fs, channels=18) # 将音频写入输入流 + sd.wait() + + # 提取前两个通道的音频,保存为raw文件供测试播放 + stereo_array = np.empty((len(my_recording), 2)) + flag = 0 + for frame in my_recording: + stereo_array[flag][0] = frame[0] + stereo_array[flag][1] = frame[1] + flag = flag + 1 + print(stereo_array) + sf.write('../recorded_audio/testfile.raw', data=stereo_array, subtype='PCM_16', samplerate=fs) + + +if __name__ == '__main__': + host_api = settings.HOST_API # 不同平台的音频接口不同,例如windows可用WASAPI + input_channel = settings.MAX_INPUT_CHANNEL + output_channels = settings.MAX_OUTPUT_CHANNEL + + device_list = sd.query_devices() # 获取电脑上的所有音频设备 + recording() + while True: + play_pcm_raw() + user_input = input("输入1继续播放,输入2退出测试") + if user_input == '1': + continue + if user_input == '2': + break + print("结束") diff --git a/src/settings.py b/src/settings.py new file mode 100644 index 0000000..5b1f086 --- /dev/null +++ b/src/settings.py @@ -0,0 +1,3 @@ +HOST_API = 'WASAPI' +MAX_INPUT_CHANNEL = 18 +MAX_OUTPUT_CHANNEL = 2 diff --git a/test_audio/Digital Presentation_48000.wav b/test_audio/Digital Presentation_48000.wav new file mode 100644 index 0000000..d0f74e9 Binary files /dev/null and b/test_audio/Digital Presentation_48000.wav differ diff --git a/test_audio/test_audio_16k.raw b/test_audio/test_audio_16k.raw new file mode 100644 index 0000000..64cc195 Binary files /dev/null and b/test_audio/test_audio_16k.raw differ diff --git a/test_audio/test_audio_48k.raw b/test_audio/test_audio_48k.raw new file mode 100644 index 0000000..1d6ae1b Binary files /dev/null and b/test_audio/test_audio_48k.raw differ