Click to See Complete Forum and Search --> : Muting/unmuting sound question


SnapCrakllPop
September 29th, 2008, 12:54 PM
I was admitted about a week ago into the Help Desk at the high school I go to. The Tech Admin asked me to write him a function that will simply mute system sound from the command line. Basically he wants to be able to go into the command prompt and run my program, which will accept the arguments "mute" and "unmute."

Here's what I have. It's currently set up simply to take the argument and decide whether it says "mute" or "unmute." It was simply for testing purposes, since I haven't messed around with command line arguments in a while.

#include <iostream>
#include <string>
using namespace std;

int main(int argc, char *argv[])
{
char mute[] = "mute";
char unmute[] = "unmute";
if(argc == 0)
{
/* check for mute/unmute and change. This is to accept no arguments. */
}
else
{
if(strcmp(unmute, argv[1]) != 0)
{
cout << "Requested operation: unmute\n";
cout << "argv[1] reads: " << argv[1];
}
else if(strcmp(mute, argv[1]) != 0)
{
cout << "Requested operation: mute\n";
cout << "argv[1] reads: " << argv[1];
}
else
cout << "ERROR";
}
return(0);
}

I've been googling and searching forums for quite a while and have came up empty-handed as to how to control the volume. Can anyone help me figure out how to mute/unmute system sound?

Thanks,
Richard

PS I've attached the .cpp file in case anyone wants to mess around with it :)

EDIT(2:05 EST, Sept 29, 2008): Meant to put this in the C++ Programming category, since I'm not sure if this DOES involve the WinAPI. Sorry.

Marc G
October 1st, 2008, 07:27 AM
Try something like:
void ToggleWindowsAudioMute()
{
// Open the mixer device
HMIXER hmx;
mixerOpen(&hmx, 0, 0, 0, 0);

// Get the line info for the wave in destination line
MIXERLINE mxl;
mxl.cbStruct = sizeof(mxl);
mxl.dwComponentType = MIXERLINE_COMPONENTTYPE_DST_SPEAKERS;
mixerGetLineInfo((HMIXEROBJ)hmx, &mxl, MIXER_GETLINEINFOF_COMPONENTTYPE);

// Find a mute control, if any, of the line out
LPMIXERCONTROL pmxctrl = (LPMIXERCONTROL)malloc(sizeof MIXERCONTROL);
MIXERLINECONTROLS mxlctrl = {sizeof mxlctrl, mxl.dwLineID,MIXERCONTROL_CONTROLTYPE_MUTE, 1, sizeof MIXERCONTROL,
pmxctrl};
if (!mixerGetLineControls((HMIXEROBJ) hmx, &mxlctrl,MIXER_GETLINECONTROLSF_ONEBYTYPE))
{
// Found!
DWORD cChannels = mxl.cChannels;
if (MIXERCONTROL_CONTROLF_UNIFORM & pmxctrl->fdwControl)
cChannels = 1;

LPMIXERCONTROLDETAILS_BOOLEAN pbool = (LPMIXERCONTROLDETAILS_BOOLEAN) malloc(cChannels * sizeof
MIXERCONTROLDETAILS_BOOLEAN);

MIXERCONTROLDETAILS mxcd;
mxcd.cbStruct = sizeof(mxcd);
mxcd.dwControlID = pmxctrl->dwControlID;
mxcd.cChannels = cChannels;
mxcd.hwndOwner = (HWND)0;
mxcd.cbDetails = sizeof(MIXERCONTROLDETAILS_BOOLEAN);
mxcd.paDetails =(LPVOID) pbool;
mixerGetControlDetails((HMIXEROBJ)hmx, &mxcd, MIXER_SETCONTROLDETAILSF_VALUE);
// Mute/Unmute the line (for both channels)
pbool[0].fValue = pbool[cChannels - 1].fValue = (pbool[0].fValue?0:1);
mixerSetControlDetails((HMIXEROBJ)hmx, &mxcd, MIXER_SETCONTROLDETAILSF_VALUE);

free(pbool);
}
free(pmxctrl);
mixerClose(hmx);
}