检测麦克风是否打开

发布于 2024-08-08 11:16:56 字数 38 浏览 7 评论 0原文

有没有办法以编程方式检测 Windows 上的麦克风是否打开?

Is there a way to programmatically detect whether the microphone is on on Windows?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

橘味果▽酱 2024-08-15 11:16:56

不,麦克风不会告诉您它们是否“打开”,或者特定的声道是否已连接到麦克风设备。您能做的最好的事情就是从您怀疑是麦克风的输入通道(例如Windows默认输入设备/通道)读取音频数据,并查看其上是否有任何信号。

为此,您必须消除任何直流偏移并寻找高于合理本底噪声的任何信号。 (慷慨一点:许多便宜的音频输入设备即使在没有信号输入时也相当嘈杂。中频滤波器/FFT 也可用于仅检测语音中频范围内的信号,而不是低频嗡嗡声和短暂的点击声。)

No, microphones don't tell you whether they're ‘on’ or that a particular sound channel is connected to a microphone device. The best you can do is to read audio data from the input channel you suspect to be a microphone (eg. the Windows default input device/channel), and see if there's any signal on it.

To do that you'd have to remove any DC offset and look for any signal above a reasonable noise floor. (Be generous: many cheap audio input devices are quite noisy even when there is no signal coming in. A mid-band filter/FFT would also be useful to detect only signals in the mid-range of a voice and not low-frequency hum and transient clicks.)

巴黎盛开的樱花 2024-08-15 11:16:56

这没有以任何方式进行测试,但我会尝试阅读一些样本,看看是否有任何变化。如果麦克风打开,那么您应该从环境声音中获得不同的值。如果麦克风关闭,你应该得到一个 0。这又是我想象的事情应该如何工作 - 我不知道它们是否真的那样工作。

This is not tested in any way, but I would try to read some samples and see if there is any variation. If the mike is on then you should get different values from the ambient sounds. If the mike is off you should get a 0. Again this is just how I imagine things should work - I don't know if they actually work that way.

音盲 2024-08-15 11:16:56

由于一次愉快的意外,我可能发现有一种方法可以检测已连接麦克风的存在。

如果您的 Windows“录音设备”显示“无麦克风”,则此方法(使用 Microsoft Speech API)将起作用并确认您没有麦克风。但是,如果 Windows 认为您有麦克风,这不会反对。

#include <sapi.h>
#include <sapiddk.h>
#include <sphelper.h>

CComPtr<ISpRecognizer>  m_cpEngine;
m_cpEngine.CoCreateInstance(CLSID_SpInprocRecognizer);
CComPtr<ISpObjectToken> pAudioToken;
HRESULT hr = SpGetDefaultTokenFromCategoryId(SPCAT_AUDIOIN, &pAudioToken);
if (FAILED(hr))  ::OutputDebugString("no input, aka microphone, detected");

更具体地说,hr 将返回以下结果:

SPERR_NOT_FOUND 0x8004503a  -2147200966
The requested data item (data key, value, etc.) was not found.

Due to a happy accident, I may have discovered that yes there is a way to detect the presence of a connected microphone.

If your windows "recording devices" shows "no microphone", then this approach (using the Microsoft Speech API) will work and confirm you have no mic. If windows however thinks you have a mic, this won't disagree.

#include <sapi.h>
#include <sapiddk.h>
#include <sphelper.h>

CComPtr<ISpRecognizer>  m_cpEngine;
m_cpEngine.CoCreateInstance(CLSID_SpInprocRecognizer);
CComPtr<ISpObjectToken> pAudioToken;
HRESULT hr = SpGetDefaultTokenFromCategoryId(SPCAT_AUDIOIN, &pAudioToken);
if (FAILED(hr))  ::OutputDebugString("no input, aka microphone, detected");

more specifically, hr will return this result:

SPERR_NOT_FOUND 0x8004503a  -2147200966
The requested data item (data key, value, etc.) was not found.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文