2,640
社区成员
发帖
与我相关
我的任务
分享
#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;
}
#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;
}