python获取win10系统声音
程序员文章站
2022-04-19 19:37:53
...
首先是驱动环境这样:
import pyaudio
p = pyaudio.PyAudio()
for i in range(p.get_device_count()):
devInfo = p.get_device_info_by_index(i)
#if devInfo['hostApi'] == 0:
print(devInfo)
# {'index': 0, 'structVersion': 2, 'name': 'Microsoft 声音映射器 - Input', 'hostApi': 0, 'maxInputChannels': 2, 'maxOutputChannels': 0, 'defaultLowInputLatency': 0.09, 'defaultLowOutputLatency': 0.09, 'defaultHighInputLatency': 0.18, 'defaultHighOutputLatency': 0.18, 'defaultSampleRate': 44100.0}
# {'index': 1, 'structVersion': 2, 'name': '麦克风 (Realtek High Definition Au', 'hostApi': 0, 'maxInputChannels': 2, 'maxOutputChannels': 0, 'defaultLowInputLatency': 0.09, 'defaultLowOutputLatency': 0.09, 'defaultHighInputLatency': 0.18, 'defaultHighOutputLatency': 0.18, 'defaultSampleRate': 44100.0}
# {'index': 2, 'structVersion': 2, 'name': 'Microsoft 声音映射器 - Output', 'hostApi': 0, 'maxInputChannels': 0, 'maxOutputChannels': 2, 'defaultLowInputLatency': 0.09, 'defaultLowOutputLatency': 0.09, 'defaultHighInputLatency': 0.18, 'defaultHighOutputLatency': 0.18, 'defaultSampleRate': 44100.0}
# {'index': 3, 'structVersion': 2, 'name': '扬声器 (Realtek High Definition Au', 'hostApi': 0, 'maxInputChannels': 0, 'maxOutputChannels': 2, 'defaultLowInputLatency': 0.09, 'defaultLowOutputLatency': 0.09, 'defaultHighInputLatency': 0.18, 'defaultHighOutputLatency': 0.18, 'defaultSampleRate': 44100.0}
# {'index': 4, 'structVersion': 2, 'name': '主声音捕获驱动程序', 'hostApi': 1, 'maxInputChannels': 2, 'maxOutputChannels': 0, 'defaultLowInputLatency': 0.12, 'defaultLowOutputLatency': 0.0, 'defaultHighInputLatency': 0.24, 'defaultHighOutputLatency': 0.0, 'defaultSampleRate': 44100.0}
# {'index': 5, 'structVersion': 2, 'name': '麦克风 (Realtek High Definition Audio)', 'hostApi': 1, 'maxInputChannels': 2, 'maxOutputChannels': 0, 'defaultLowInputLatency': 0.12, 'defaultLowOutputLatency': 0.0, 'defaultHighInputLatency': 0.24, 'defaultHighOutputLatency': 0.0, 'defaultSampleRate': 44100.0}
# {'index': 6, 'structVersion': 2, 'name': '主声音驱动程序', 'hostApi': 1, 'maxInputChannels': 0, 'maxOutputChannels': 8, 'defaultLowInputLatency': 0.0, 'defaultLowOutputLatency': 0.12, 'defaultHighInputLatency': 0.0, 'defaultHighOutputLatency': 0.24, 'defaultSampleRate': 44100.0}
# {'index': 7, 'structVersion': 2, 'name': '扬声器 (Realtek High Definition Audio)', 'hostApi': 1, 'maxInputChannels': 0, 'maxOutputChannels': 2, 'defaultLowInputLatency': 0.0, 'defaultLowOutputLatency': 0.12, 'defaultHighInputLatency': 0.0, 'defaultHighOutputLatency': 0.24, 'defaultSampleRate': 44100.0}
# {'index': 8, 'structVersion': 2, 'name': 'Realtek ASIO', 'hostApi': 2, 'maxInputChannels': 2, 'maxOutputChannels': 2, 'defaultLowInputLatency': 0.023219954648526078, 'defaultLowOutputLatency': 0.023219954648526078, 'defaultHighInputLatency': 0.023219954648526078, 'defaultHighOutputLatency': 0.023219954648526078, 'defaultSampleRate': 44100.0}
# {'index': 9, 'structVersion': 2, 'name': '扬声器 (Realtek High Definition Audio)', 'hostApi': 3, 'maxInputChannels': 0, 'maxOutputChannels': 8, 'defaultLowInputLatency': 0.0, 'defaultLowOutputLatency': 0.003, 'defaultHighInputLatency': 0.0, 'defaultHighOutputLatency': 0.01, 'defaultSampleRate': 48000.0}
# {'index': 10, 'structVersion': 2, 'name': '麦克风 (Realtek High Definition Audio)', 'hostApi': 3, 'maxInputChannels': 8, 'maxOutputChannels': 0, 'defaultLowInputLatency': 0.003, 'defaultLowOutputLatency': 0.0, 'defaultHighInputLatency': 0.01, 'defaultHighOutputLatency': 0.0, 'defaultSampleRate': 48000.0}
# {'index': 11, 'structVersion': 2, 'name': '立体声混音 (Realtek HD Audio Stereo input)', 'hostApi': 4, 'maxInputChannels': 2, 'maxOutputChannels': 0, 'defaultLowInputLatency': 0.01, 'defaultLowOutputLatency': 0.01, 'defaultHighInputLatency': 0.04, 'defaultHighOutputLatency': 0.04, 'defaultSampleRate': 44100.0}
# {'index': 12, 'structVersion': 2, 'name': 'Speakers (Realtek HD Audio output)', 'hostApi': 4, 'maxInputChannels': 0, 'maxOutputChannels': 2, 'defaultLowInputLatency': 0.01, 'defaultLowOutputLatency': 0.01, 'defaultHighInputLatency': 0.04, 'defaultHighOutputLatency': 0.04, 'defaultSampleRate': 44100.0}
# {'index': 13, 'structVersion': 2, 'name': '麦克风 (Realtek HD Audio Mic input)', 'hostApi': 4, 'maxInputChannels': 2, 'maxOutputChannels': 0, 'defaultLowInputLatency': 0.01, 'defaultLowOutputLatency': 0.01, 'defaultHighInputLatency': 0.04, 'defaultHighOutputLatency': 0.04, 'defaultSampleRate': 44100.0}
# {'index': 14, 'structVersion': 2, 'name': 'SPDIF Out (Realtek HDA SPDIF Out)', 'hostApi': 4, 'maxInputChannels': 0, 'maxOutputChannels': 2, 'defaultLowInputLatency': 0.01, 'defaultLowOutputLatency': 0.01, 'defaultHighInputLatency': 0.04, 'defaultHighOutputLatency': 0.04, 'defaultSampleRate': 44100.0}
这样的话只能接受麦克风声音,还有其他的驱动(2还是4忘了)是哔哔声
然后网上找了一上午,找到了个办法
http://www.naxs.cn/win10/4220.html
也就是扬声器取消独占,并启动立体混响,然后python就能接收到系统的音频信号了
# _*_ coding: utf-8 _*_
# 录音机,用于录制声卡播放的声音(内录)
import os
import pyaudio
import threading
import wave
import time
from datetime import datetime
# 录音类
class Recorder():
def __init__(self, chunk=1024, channels=2, rate=44100):
self.CHUNK = chunk
self.FORMAT = pyaudio.paInt16
self.CHANNELS = channels
self.RATE = rate
self._running = True
self._frames = []
# 获取内录设备序号,在windows操作系统上测试通过,hostAPI = 0 表明是MME设备
def findInternalRecordingDevice(self, p):
# 要找查的设备名称中的关键字
target = '立体声混音'
# 逐一查找声音设备
for i in range(p.get_device_count()):
devInfo = p.get_device_info_by_index(i)
if devInfo['name'].find(target) >= 0 and devInfo['hostApi'] == 0:
# print('已找到内录设备,序号是 ',i)
return i
print('无法找到内录设备!')
return -1
# 开始录音,开启一个新线程进行录音操作
def start(self):
threading._start_new_thread(self.__record, ())
# 执行录音的线程函数
def __record(self):
self._running = True
self._frames = []
p = pyaudio.PyAudio()
# 查找内录设备
dev_idx = self.findInternalRecordingDevice(p)
if dev_idx < 0:
return
# 在打开输入流时指定输入设备
stream = p.open(input_device_index=dev_idx,
format=self.FORMAT,
channels=self.CHANNELS,
rate=self.RATE,
input=True,
frames_per_buffer=self.CHUNK)
# 循环读取输入流
while (self._running):
data = stream.read(self.CHUNK)
self._frames.append(data)
# 停止读取输入流
stream.stop_stream()
# 关闭输入流
stream.close()
# 结束pyaudio
p.terminate()
return
# 停止录音
def stop(self):
self._running = False
# 保存到文件
def save(self, fileName):
# 创建pyAudio对象
p = pyaudio.PyAudio()
# 打开用于保存数据的文件
wf = wave.open(fileName, 'wb')
# 设置音频参数
wf.setnchannels(self.CHANNELS)
wf.setsampwidth(p.get_sample_size(self.FORMAT))
wf.setframerate(self.RATE)
# 写入数据
wf.writeframes(b''.join(self._frames))
# 关闭文件
wf.close()
# 结束pyaudio
p.terminate()
if __name__ == "__main__":
# 检测当前目录下是否有record子目录
if not os.path.exists('record'):
os.makedirs('record')
print("\npython 录音机 ....\n")
print("提示:按 r 键并回车 开始录音\n")
i = input('请输入操作码:')
if i == 'r':
rec = Recorder()
begin = time.time()
print("\n开始录音,按 s 键并回车 停止录音,自动保存到 record 子目录\n")
rec.start()
running = True
while running:
i = input("请输入操作码:")
if i == 's':
running = False
print("录音已停止")
rec.stop()
t = time.time() - begin
print('录音时间为%ds' % t)
# 以当前时间为关键字保存wav文件
rec.save("record/rec_" + datetime.now().strftime("%Y-%m-%d_%H-%M-%S") + ".wav")
代码来源:https://blog.csdn.net/littlezhuhui/article/details/101025305
我现在想学习的是,wallpaper
wallpaper是怎么做到在立体混响禁用的情况下,还能拿到系统声音,我真是服了,知道解决办法,但是我也想跟wallpaper一样,在最上面给出的环境下获取到系统声音。
上一篇: 吃火锅必不可少的菜有哪些
下一篇: html编写购物网站页面练习(一)