类似Win自带麦克风测试的音频监听实现?
搜了下 麦克风音频监听 实现起来没那么容易,火山的摄像头操作库仅可获取到音频设备名称,还无法监听麦克风声音大小,
要实现如win系统自带的麦克风测试效果,一开始说话就能检测到声音大小,
有没有大佬懂得哪个库可以实现这个功能呢
import pyaudio
import numpy as np
# 定义回调函数
def callback(in_data, frame_count, time_info, status):
audio_data = np.frombuffer(in_data, dtype=np.int16)
volume = np.linalg.norm(audio_data) / frame_count
print("音量:", volume)
return (in_data, pyaudio.paContinue)
# 初始化PyAudio对象
p = pyaudio.PyAudio()
# 打开麦克风流
stream = p.open(format=pyaudio.paInt16,
channels=1,
rate=44100,
input=True,
frames_per_buffer=1024,
stream_callback=callback)
# 开始监听
stream.start_stream()
# 保持监听状态,按任意键退出
try:
while stream.is_active():
input("按任意键退出...")
except KeyboardInterrupt:
pass
# 停止监听并关闭流
stream.stop_stream()
stream.close()
# 终止PyAudio对象
p.terminate()
#include <iostream>
#include <windows.h>
#include <mmsystem.h>
#pragma comment(lib, "winmm.lib")
void CALLBACK waveInProc(HWAVEIN hwi, UINT uMsg, DWORD_PTR dwInstance, DWORD_PTR dwParam1, DWORD_PTR dwParam2) {
if (uMsg == WIM_DATA) {
WAVEHDR* header = (WAVEHDR*)dwParam1;
short* buffer = (short*)header->lpData;
int volume = 0;
for (int i = 0; i < header->dwBytesRecorded / sizeof(short); i++) {
volume += abs(buffer);
}
volume /= (header->dwBytesRecorded / sizeof(short));
std::cout << "音量: " << volume << std::endl;
waveInAddBuffer(hwi, header, sizeof(WAVEHDR));
}
}
int main() {
WAVEFORMATEX format;
format.wFormatTag = WAVE_FORMAT_PCM;
format.nChannels = 1;
format.nSamplesPerSec = 44100;
format.wBitsPerSample = 16;
format.nBlockAlign = format.nChannels * format.wBitsPerSample / 8;
format.nAvgBytesPerSec = format.nSamplesPerSec * format.nBlockAlign;
format.cbSize = 0;
HWAVEIN hwi;
MMRESULT result = waveInOpen(&hwi, WAVE_MAPPER, &format, (DWORD_PTR)waveInProc, 0, CALLBACK_FUNCTION);
if (result != MMSYSERR_NOERROR) {
std::cerr << "无法打开音频输入设备" << std::endl;
return 1;
}
WAVEHDR header;
ZeroMemory(&header, sizeof(WAVEHDR));
header.lpData = new char;
header.dwBufferLength = format.nBlockAlign;
header.dwFlags = WHDR_DONE;
result = waveInPrepareHeader(hwi, &header, sizeof(WAVEHDR));
if (result != MMSYSERR_NOERROR) {
std::cerr << "无法准备音频输入头" << std::endl;
return 1;
}
result = waveInAddBuffer(hwi, &header, sizeof(WAVEHDR));
if (result != MMSYSERR_NOERROR) {
std::cerr << "无法添加音频输入缓冲区" << std::endl;
return 1;
}
result = waveInStart(hwi);
if (result != MMSYSERR_NOERROR) {
std::cerr << "无法开始音频输入" << std::endl;
return 1;
}
MSG msg;
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
waveInUnprepareHeader(hwi, &header, sizeof(WAVEHDR));
delete[] header.lpData;
waveInClose(hwi);
return 0;
}
承易 发表于 2024-3-21 20:38
你发的这些是AI生成的吧,不会用,我找到一个在线测试麦克风的网站,完全本地化的,实测可用,我正在将没用的部分主键删除,找出主要功能实现代码,要是解决了就贴出来。 火山的BASS音频库看着能够实现,但对这个库功能不了解怎么用。 99101617 发表于 2024-3-21 21:36
你发的这些是AI生成的吧,不会用,我找到一个在线测试麦克风的网站,完全本地化的,实测可用,我正在将没 ...
现在的时代就是问AI 在修改下 就成了 两个办法,一个用封装的库,一个用JS(第二个网站的JS可以下载到本地使用)
https://bbs.voldp.com/forum.php?mod=viewthread&tid=11085&highlight=BASS
https://themictest.com/zh/
页:
[1]