如何话筒获得声音输入的强度(非声卡输入音量设置)

swmp 2008-05-28 06:38:38

从网上找到一段代码,传说是微软的一个人写的,但调试不能通过,代码如下:
问题处用!!!标明了。


#include "stdafx.h"
#include <windows.h>
#include <mmsystem.h>

// 需要链接winmm.lib
// 希望获得声音输入的强度,而非声卡音量设置
int GetSoundIntensity()
{

HMIXER hMixer; // Mixer handle used in mixer API calls.
MIXERCONTROL mxc; // Holds the mixer control data.
MIXERLINE mxl; // Holds the mixer line data.
MIXERLINECONTROLS mxlc; // Obtains the mixer control.

// Open the mixer. This opens the mixer with a deviceID of 0. If you
// have a single sound card/mixer, then this will open it. If you have
// multiple sound cards/mixers, the deviceIDs will be 0, 1, 2, and
// so on.
MMRESULT result = 0; // Return code.
result = mixerOpen(&hMixer, 0,0,0,0);
if (MMSYSERR_NOERROR != result) {
// Couldn't open the mixer.
return -1;
}

// Initialize MIXERLINE structure.
ZeroMemory(&mxl,sizeof(mxl));
mxl.cbStruct = sizeof(mxl);

// Specify the line you want to get. You are getting the input line
// here. If you want to get the output line, you need to use
// MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT.
// MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE
mxl.dwComponentType = MIXERLINE_COMPONENTTYPE_DST_WAVEIN;

result = mixerGetLineInfo((HMIXEROBJ)hMixer, &mxl,
MIXER_GETLINEINFOF_COMPONENTTYPE);
if (MMSYSERR_NOERROR != result) {
// Couldn't get the mixer line.
mixerClose (hMixer);
return -1;
}

// Get the control.
ZeroMemory(&mxlc, sizeof(mxlc));
mxlc.cbStruct = sizeof(mxlc);
mxlc.dwLineID = mxl.dwLineID;
// !!!!!!此处应该是MIXERCONTROL_CONTROLTYPE_PEAKMETER,但是调用mixerGetLineControls不能通过
mxlc.dwControlType = MIXERCONTROL_CONTROLTYPE_MIXER;//MIXERCONTROL_CONTROLTYPE_PEAKMETER;
mxlc.cControls = 1;
mxlc.cbmxctrl = sizeof(mxc);
mxlc.pamxctrl = &mxc;

result = mixerGetLineControls((HMIXEROBJ)hMixer,&mxlc,
MIXER_GETLINECONTROLSF_ONEBYTYPE|MIXER_OBJECTF_HMIXER);
if (MMSYSERR_NOERROR != result)
{
// !!!!! 使用MIXERCONTROL_CONTROLTYPE_MIXER不能成功
// Couldn't get the control.
// Get the control. Try Mux
ZeroMemory(&mxlc, sizeof(mxlc));
mxlc.cbStruct = sizeof(mxlc);
mxlc.dwLineID = mxl.dwLineID;
mxlc.dwControlType = MIXERCONTROL_CONTROLTYPE_MUX;//MIXERCONTROL_CONTROLTYPE_PEAKMETER;
mxlc.cControls = 1;
mxlc.cbmxctrl = sizeof(mxc);
mxlc.pamxctrl = &mxc;
// 此处调用成功
result = mixerGetLineControls((HMIXEROBJ)hMixer,&mxlc,
MIXER_GETLINECONTROLSF_ONEBYTYPE|MIXER_OBJECTF_HMIXER);
if (MMSYSERR_NOERROR != result)
{
mixerClose (hMixer);
return -1;
}
}

// After successfully getting the peakmeter control, the volume range
// will be specified by mxc.Bounds.lMinimum to mxc.Bounds.lMaximum.

MIXERCONTROLDETAILS mxcd; // Gets the control values.
MIXERCONTROLDETAILS_SIGNED volStruct; // Gets the control values.
long volume; // Holds the final volume value.

// Initialize the MIXERCONTROLDETAILS structure
ZeroMemory(&mxcd, sizeof(mxcd));
mxcd.cbStruct = sizeof(mxcd);
mxcd.cbDetails = sizeof(volStruct);
mxcd.dwControlID = mxc.dwControlID;
mxcd.paDetails = &volStruct;
mxcd.cChannels = 1;

// Get the current value of the peakmeter control.
// !!!!!此处参数错误,可能是因为前面使用了MIXERCONTROL_CONTROLTYPE_MUX的原因
result = mixerGetControlDetails((HMIXEROBJ)hMixer, &mxcd,
MIXER_GETCONTROLDETAILSF_VALUE);
if (MMSYSERR_NOERROR != result) {
// Couldn't get the current volume.
mixerClose (hMixer);
return -1;
}
volume = volStruct.lValue;
return volume;
}



还有一种说法是通过wavein数据转换成声音强度,但没有例子,有没有DX知道

谢谢
...全文
393 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
dtzleg 2008-09-22
  • 打赏
  • 举报
回复
我插句话,
不就是声音的频谱分析嘛
源码很多
然后比较数字量的大小

lz google一下
flyer821003 2008-09-03
  • 打赏
  • 举报
回复
等待……
vc8fans 2008-06-13
  • 打赏
  • 举报
回复
做个记号...
swmp 2008-06-02
  • 打赏
  • 举报
回复
自己顶,继续问
swmp 2008-05-30
  • 打赏
  • 举报
回复

#include "stdafx.h"
#include <windows.h>
#include <mmsystem.h>

// 需要链接winmm.lib
// 希望获得声音输入的强度,而非声卡音量设置
int GetSoundIntensity()
{

HMIXER hMixer; // Mixer handle used in mixer API calls.
MIXERCONTROL mxc; // Holds the mixer control data.
MIXERLINE mxl; // Holds the mixer line data.
MIXERLINECONTROLS mxlc; // Obtains the mixer control.

// Open the mixer. This opens the mixer with a deviceID of 0. If you
// have a single sound card/mixer, then this will open it. If you have
// multiple sound cards/mixers, the deviceIDs will be 0, 1, 2, and
// so on.
MMRESULT result = 0; // Return code.
result = mixerOpen(&hMixer, 0,0,0,0);
if (MMSYSERR_NOERROR != result) {
// Couldn't open the mixer.
return -1;
}

// Initialize MIXERLINE structure.
ZeroMemory(&mxl,sizeof(mxl));
mxl.cbStruct = sizeof(mxl);

// Specify the line you want to get. You are getting the input line
// here. If you want to get the output line, you need to use
// MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT.
// MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE
mxl.dwComponentType = MIXERLINE_COMPONENTTYPE_DST_WAVEIN;

result = mixerGetLineInfo((HMIXEROBJ)hMixer, &mxl,
MIXER_GETLINEINFOF_COMPONENTTYPE);
if (MMSYSERR_NOERROR != result) {
// Couldn't get the mixer line.
mixerClose (hMixer);
return -1;
}

// Get the control.
ZeroMemory(&mxlc, sizeof(mxlc));
mxlc.cbStruct = sizeof(mxlc);
mxlc.dwLineID = mxl.dwLineID;
// !!!!!!此处应该是MIXERCONTROL_CONTROLTYPE_PEAKMETER,但是调用mixerGetLineControls不能通过
mxlc.dwControlType = MIXERCONTROL_CONTROLTYPE_MIXER;//MIXERCONTROL_CONTROLTYPE_PEAKMETER;
mxlc.cControls = 1;
mxlc.cbmxctrl = sizeof(mxc);
mxlc.pamxctrl = &mxc;

result = mixerGetLineControls((HMIXEROBJ)hMixer,&mxlc,
MIXER_GETLINECONTROLSF_ONEBYTYPE|MIXER_OBJECTF_HMIXER);
if (MMSYSERR_NOERROR != result)
{
// !!!!! 使用MIXERCONTROL_CONTROLTYPE_MIXER不能成功
// Couldn't get the control.
// Get the control. Try Mux
ZeroMemory(&mxlc, sizeof(mxlc));
mxlc.cbStruct = sizeof(mxlc);
mxlc.dwLineID = mxl.dwLineID;
mxlc.dwControlType = MIXERCONTROL_CONTROLTYPE_MUX;//MIXERCONTROL_CONTROLTYPE_PEAKMETER;
mxlc.cControls = 1;
mxlc.cbmxctrl = sizeof(mxc);
mxlc.pamxctrl = &mxc;
// 此处调用成功
result = mixerGetLineControls((HMIXEROBJ)hMixer,&mxlc,
MIXER_GETLINECONTROLSF_ONEBYTYPE|MIXER_OBJECTF_HMIXER);
if (MMSYSERR_NOERROR != result)
{
mixerClose (hMixer);
return -1;
}
}

// After successfully getting the peakmeter control, the volume range
// will be specified by mxc.Bounds.lMinimum to mxc.Bounds.lMaximum.

MIXERCONTROLDETAILS mxcd; // Gets the control values.
MIXERCONTROLDETAILS_SIGNED volStruct; // Gets the control values.
long volume; // Holds the final volume value.

// Initialize the MIXERCONTROLDETAILS structure
ZeroMemory(&mxcd, sizeof(mxcd));
mxcd.cbStruct = sizeof(mxcd);
mxcd.cbDetails = sizeof(volStruct);
mxcd.dwControlID = mxc.dwControlID;
mxcd.paDetails = &volStruct;
mxcd.cChannels = 1;

// Get the current value of the peakmeter control.
// !!!!!此处参数错误,可能是因为前面使用了MIXERCONTROL_CONTROLTYPE_MUX的原因
result = mixerGetControlDetails((HMIXEROBJ)hMixer, &mxcd,
MIXER_GETCONTROLDETAILSF_VALUE);
if (MMSYSERR_NOERROR != result) {
// Couldn't get the current volume.
mixerClose (hMixer);
return -1;
}
volume = volStruct.lValue;
return volume;
}


不够可加分
swmp 2008-05-29
  • 打赏
  • 举报
回复
#include "stdafx.h"
#include <windows.h>
#include <mmsystem.h>

// 需要链接winmm.lib
// 希望获得声音输入的强度,而非声卡音量设置
int GetSoundIntensity()
{

HMIXER hMixer; // Mixer handle used in mixer API calls.
MIXERCONTROL mxc; // Holds the mixer control data.
MIXERLINE mxl; // Holds the mixer line data.
MIXERLINECONTROLS mxlc; // Obtains the mixer control.

// Open the mixer. This opens the mixer with a deviceID of 0. If you
// have a single sound card/mixer, then this will open it. If you have
// multiple sound cards/mixers, the deviceIDs will be 0, 1, 2, and
// so on.
MMRESULT result = 0; // Return code.
result = mixerOpen(&hMixer, 0,0,0,0);
if (MMSYSERR_NOERROR != result) {
// Couldn't open the mixer.
return -1;
}

// Initialize MIXERLINE structure.
ZeroMemory(&mxl,sizeof(mxl));
mxl.cbStruct = sizeof(mxl);

// Specify the line you want to get. You are getting the input line
// here. If you want to get the output line, you need to use
// MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT.
// MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE
mxl.dwComponentType = MIXERLINE_COMPONENTTYPE_DST_WAVEIN;

result = mixerGetLineInfo((HMIXEROBJ)hMixer, &mxl,
MIXER_GETLINEINFOF_COMPONENTTYPE);
if (MMSYSERR_NOERROR != result) {
// Couldn't get the mixer line.
mixerClose (hMixer);
return -1;
}

// Get the control.
ZeroMemory(&mxlc, sizeof(mxlc));
mxlc.cbStruct = sizeof(mxlc);
mxlc.dwLineID = mxl.dwLineID;
// !!!!!!此处应该是MIXERCONTROL_CONTROLTYPE_PEAKMETER,但是调用mixerGetLineControls不能通过
mxlc.dwControlType = MIXERCONTROL_CONTROLTYPE_MIXER;//MIXERCONTROL_CONTROLTYPE_PEAKMETER;
mxlc.cControls = 1;
mxlc.cbmxctrl = sizeof(mxc);
mxlc.pamxctrl = &mxc;

result = mixerGetLineControls((HMIXEROBJ)hMixer,&mxlc,
MIXER_GETLINECONTROLSF_ONEBYTYPE|MIXER_OBJECTF_HMIXER);
if (MMSYSERR_NOERROR != result)
{
// !!!!! 使用MIXERCONTROL_CONTROLTYPE_MIXER不能成功
// Couldn't get the control.
// Get the control. Try Mux
ZeroMemory(&mxlc, sizeof(mxlc));
mxlc.cbStruct = sizeof(mxlc);
mxlc.dwLineID = mxl.dwLineID;
mxlc.dwControlType = MIXERCONTROL_CONTROLTYPE_MUX;//MIXERCONTROL_CONTROLTYPE_PEAKMETER;
mxlc.cControls = 1;
mxlc.cbmxctrl = sizeof(mxc);
mxlc.pamxctrl = &mxc;
// 此处调用成功
result = mixerGetLineControls((HMIXEROBJ)hMixer,&mxlc,
MIXER_GETLINECONTROLSF_ONEBYTYPE|MIXER_OBJECTF_HMIXER);
if (MMSYSERR_NOERROR != result)
{
mixerClose (hMixer);
return -1;
}
}

// After successfully getting the peakmeter control, the volume range
// will be specified by mxc.Bounds.lMinimum to mxc.Bounds.lMaximum.

MIXERCONTROLDETAILS mxcd; // Gets the control values.
MIXERCONTROLDETAILS_SIGNED volStruct; // Gets the control values.
long volume; // Holds the final volume value.

// Initialize the MIXERCONTROLDETAILS structure
ZeroMemory(&mxcd, sizeof(mxcd));
mxcd.cbStruct = sizeof(mxcd);
mxcd.cbDetails = sizeof(volStruct);
mxcd.dwControlID = mxc.dwControlID;
mxcd.paDetails = &volStruct;
mxcd.cChannels = 1;

// Get the current value of the peakmeter control.
// !!!!!此处参数错误,可能是因为前面使用了MIXERCONTROL_CONTROLTYPE_MUX的原因
result = mixerGetControlDetails((HMIXEROBJ)hMixer, &mxcd,
MIXER_GETCONTROLDETAILSF_VALUE);
if (MMSYSERR_NOERROR != result) {
// Couldn't get the current volume.
mixerClose (hMixer);
return -1;
}
volume = volStruct.lValue;
return volume;
}
CathySun118 2008-05-29
  • 打赏
  • 举报
回复
什么代码?
卡米尔 2008-05-28
  • 打赏
  • 举报
回复
?
没看到!

2,640

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 硬件/系统
社区管理员
  • 硬件/系统社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧