声音输入中存在声音输出,即使在Linux中没有麦克风

时间:2016-01-25 20:01:15

标签: python linux audio alsa pyaudio

我试图同时播放声音并记录压电传感器的电压输出。为此,我将声板模拟输出连接到扬声器,将声板模拟输入连接到压电传感器。

当我从传感器录制信号但没有播放声音时,它完美无缺。但是,当我在录音过程中播放声音时,我可以看到我在传感器信号中播放的声音,就好像声板的输出和输入已经连接一样。最奇怪的是,即使我拔下传感器,输出信号仍然存在于录音中。

换句话说,问题是:我开始录音,当我开始播放声音时,它会出现在传感器的信号中(即使关闭了扬声器)。

我只使用ALSA,未安装pulseaudio。我使用python(和pyaudio)生成声音信号,将信号写入声板输出并从声板输入读取数据。我也在Audacity中测试过,但结果是一样的。我也用arecord进行了测试:

$ arecord --channel=1 --rate=128000 --format=S32_LE Test.wav

然后:

$ mplayer Audio/Alarms/Annoying_Alien_Buzzer-Kevan-1659966176.mp3 

然后停止了录音。当我播放Test.wav时,播放文件中的所有声音信号都在那里。

我知道这听起来很明显,但我想说在设置的任何地方都没有连接MIC。

以下是一些信息:

====================
框:

$ uname -a
Linux MalfattiTux 4.1.12-gentoo #3 SMP Thu Jan 21 17:24:27 BRST 2016 x86_64 Intel(R) Core(TM) i7-4710HQ CPU @ 2.50GHz GenuineIntel GNU/Linux

音频板:

$ lspci | grep -i audio
00:03.0 Audio device: Intel Corporation Xeon E3-1200 v3/4th Gen Core Processor HD Audio Controller (rev 06)
00:1b.0 Audio device: Intel Corporation 8 Series/C220 Series Chipset High Definition Audio Controller (rev 05)## Heading ##

输出设备:

$ aplay -l
**** List of PLAYBACK Hardware Devices ****
card 0: HDMI [HDA Intel HDMI], device 3: HDMI 0 [HDMI 0]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 0: HDMI [HDA Intel HDMI], device 7: HDMI 1 [HDMI 1]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 0: HDMI [HDA Intel HDMI], device 8: HDMI 2 [HDMI 2]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 1: PCH [HDA Intel PCH], device 0: ALC3239 Analog [ALC3239 Analog]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 1: PCH [HDA Intel PCH], device 1: ALC3239 Digital [ALC3239 Digital]
  Subdevices: 1/1
  Subdevice #0: subdevice #0

输入设备:

$ arecord -l
**** List of CAPTURE Hardware Devices ****
card 1: PCH [HDA Intel PCH], device 0: ALC3239 Analog [ALC3239 Analog]
  Subdevices: 1/1
  Subdevice #0: subdevice #0

要播放和录制的Python代码(最小工作示例):

import array
import pyaudio
import random

Rate = 128000
SoundPulseDur = 5 # in seconds

print('Generating sound pulse...')
SoundNoise = [random.random() for _ in range(round(Rate*SoundPulseDur))]
SoundPulse = [SoundNoise[ElI]*2-1 for ElI,ElV in enumerate(SoundNoise)]
SoundPulse[-1] = 0


print('Interleaving channels...')
SoundList = [0]*(2*len(SoundPulse))
for _ in range(len(SoundPulse)):
    SoundList[_ *2] = SoundPulse[_]
    SoundList[_ *2+1] = 0

Sound = array.array('f')
Sound.fromlist(SoundList)
Sound = bytes(Sound)


print('Generating sound objects...')
# Output
p = pyaudio.PyAudio()
Stimulation = p.open(format=pyaudio.paFloat32,
                channels=2,
                rate=Rate,
                output=True)

# Input
q = pyaudio.PyAudio()
InOn = False
RecStop = True
def InCallBack(in_data, frame_count, time_info, status):
    if InOn:
        global SoundRec
        SoundRec.append(in_data)

    if RecStop:
        InFlag = pyaudio.paComplete
    else:
        InFlag = pyaudio.paContinue
    return(None, InFlag)

Reading = q.open(format=pyaudio.paFloat32,
                     channels=1,
                     rate=Rate,
                     input=True,
                     output=False,
                     stream_callback=InCallBack)

SoundRec = []
Reading.start_stream()
RecStop = False; InOn = True
Stimulation.write(Sound)
InOn = False; RecStop = True
Reading.stop_stream()

1 个答案:

答案 0 :(得分:0)

这听起来像声卡中的电噪声。我认为你使用的是板载卡吗?如果是这样,除非您想购买更好/外置的声卡,否则您无法避免录制噪音。

关于实际的解决方案; 就个人而言,我会录制我的音频输出,并使用该音频数据来消除压电传感器数据中的噪音。

理论上,如果您可以将声音输出读取到压电录音脚本,例如PyAudio

,理论上也可以进行直播