473,652 Members | 3,123 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Audio Mixer Set sound level

Here is the code :

public class AudioMixerHelpe r
{

public const int MMSYSERR_NOERRO R = 0;
public const int MAXPNAMELEN = 32;
public const int MIXER_LONG_NAME _CHARS = 64;
public const int MIXER_SHORT_NAM E_CHARS = 16;
public const int MIXER_GETLINEIN FOF_COMPONENTTY PE = 0x3;
public const int MIXER_GETCONTRO LDETAILSF_VALUE = 0x0;
public const int MIXER_GETLINECO NTROLSF_ONEBYTY PE = 0x2;
public const int MIXER_SETCONTRO LDETAILSF_VALUE = 0x0;

public const int MIXERLINE_COMPO NENTTYPE_DST_FI RST = 0x0;
public const int MIXERLINE_COMPO NENTTYPE_SRC_FI RST = 0x1000;

public const int MIXERLINE_COMPO NENTTYPE_DST_SP EAKERS =
(MIXERLINE_COMP ONENTTYPE_DST_F IRST + 4);

public const int MIXERLINE_COMPO NENTTYPE_SRC_MI CROPHONE =
(MIXERLINE_COMP ONENTTYPE_SRC_F IRST + 3);

public const int MIXERLINE_COMPO NENTTYPE_SRC_LI NE =
(MIXERLINE_COMP ONENTTYPE_SRC_F IRST + 2);

public const int MIXERCONTROL_CT _CLASS_FADER = 0x50000000;
public const int MIXERCONTROL_CT _UNITS_UNSIGNED = 0x30000;

public const int MIXERCONTROL_CO NTROLTYPE_FADER =
(MIXERCONTROL_C T_CLASS_FADER | MIXERCONTROL_CT _UNITS_UNSIGNED );

public const int MIXERCONTROL_CO NTROLTYPE_VOLUM E =
(MIXERCONTROL_C ONTROLTYPE_FADE R + 1);

[ DllImport( "winmm.dll" , CharSet=CharSet .Ansi )]
private static extern int mixerClose (int hmx);
[ DllImport( "winmm.dll" , CharSet=CharSet .Ansi )]
private static extern int mixerGetControl DetailsA (int hmxobj,ref
MIXERCONTROLDET AILS pmxcd , int fdwDetails);
[ DllImport( "winmm.dll" , CharSet=CharSet .Ansi )]
private static extern int mixerGetDevCaps A( int uMxId, MIXERCAPS
pmxcaps, int cbmxcaps) ;
[ DllImport( "winmm.dll" , CharSet=CharSet .Ansi )]
private static extern int mixerGetID (int hmxobj, int pumxID, int
fdwId );
[ DllImport( "winmm.dll" , CharSet=CharSet .Ansi )]
private static extern int mixerGetLineCon trolsA (int hmxobj,ref
MIXERLINECONTRO LS pmxlc, int fdwControls);
[ DllImport( "winmm.dll" , CharSet=CharSet .Ansi )]
private static extern int mixerGetLineInf oA (int hmxobj,ref
MIXERLINE pmxl , int fdwInfo);
[ DllImport( "winmm.dll" , CharSet=CharSet .Ansi )]
private static extern int mixerGetNumDevs ();
[ DllImport( "winmm.dll" , CharSet=CharSet .Ansi )]
private static extern int mixerMessage(in t hmx , int uMsg , int
dwParam1 , int dwParam2);
[ DllImport( "winmm.dll" , CharSet=CharSet .Ansi )]
private static extern int mixerOpen (out int phmx , int uMxId ,
int dwCallback , int dwInstance , int fdwOpen);
[ DllImport( "winmm.dll" , CharSet=CharSet .Ansi )]
private static extern int mixerSetControl Details(int hmxobj ,ref
MIXERCONTROLDET AILS pmxcd , int fdwDetails);

public struct MIXERCAPS{
public int wMid ; // manufacturer id
public int wPid ; // product id
public int vDriverVersion ; // version of the driver
[ MarshalAs( UnmanagedType.B yValTStr, SizeConst=MAXPN AMELEN)]
public string szPname ; // product name
public int fdwSupport ; // misc. support bits
public int cDestinations ; // count of destinations
}

public struct MIXERCONTROL{
public int cbStruct ; // size in Byte of MIXERCONTROL
public int dwControlID ; // unique control id for mixer
device
public int dwControlType ; // MIXERCONTROL_CO NTROLpublic
enum _xxx
public int fdwControl ; // MIXERCONTROL_CO NTROLF_xxx
public int cMultipleItems ; // if
MIXERCONTROL_CO NTROLF_MULTIPLE
[ MarshalAs( UnmanagedType.B yValTStr,
SizeConst=MIXER _SHORT_NAME_CHA RS )]
public string szShortName ; // short name of
[ MarshalAs( UnmanagedType.B yValTStr,
SizeConst=MIXER _LONG_NAME_CHAR S )]
public string szName ; // long name of
public int lMinimum ; // Minimum value
public int lMaximum ; // Maximum value
// [ MarshalAs( UnmanagedType.B yValArray, SizeConst=10 )]
[ MarshalAs( UnmanagedType.U 4, SizeConst=10 )]
public int reserved; // replaced // reserved structure space
}

public struct MIXERCONTROLDET AILS{
public int cbStruct ; // size in Byte of MIXERCONTROLDET AILS
public int dwControlID ; // control id to get/set details on
public int cChannels ; // number of channels in paDetails
array
public int item ; // hwndOwner or cMultipleItems
public int cbDetails ; // size of _one_ details_XX struct
public IntPtr paDetails ; // pointer to array of details_XX
structs
}

public struct MIXERCONTROLDET AILS_UNSIGNED{
public int dwValue ; // value of the control
}

public struct MIXERLINE{
public int cbStruct ; // size of MIXERLINE structure
public int dwDestination ; // zero based destination
index
public int dwSource ; // zero based source index (if
// source)
public int dwLineID ; // unique line id for mixer
device
public int fdwLine ; // state/information about
line
public int dwUser ; // driver specific information
public int dwComponentType ; // component public enum
line connects to
public int cChannels ; // number of channels line
supports
public int cConnections ; // number of connections
(possible)
public int cControls ;
[ MarshalAs( UnmanagedType.B yValTStr,
SizeConst=MIXER _SHORT_NAME_CHA RS )] // number of controls at this
line
public string szShortName;
[ MarshalAs( UnmanagedType.B yValTStr,
SizeConst=MIXER _LONG_NAME_CHAR S )]
public string szName;
public int dwType ;
public int dwDeviceID ;
public int wMid ;
public int wPid ;
public int vDriverVersion ;
[ MarshalAs( UnmanagedType.B yValTStr, SizeConst=MAXPN AMELEN)]
public string szPname ;
}

public struct MIXERLINECONTRO LS{
public int cbStruct ; // size in Byte of MIXERLINECONTRO LS
public int dwLineID ; // line id (from MIXERLINE.dwLin eID)
// MIXER_GETLINECO NTROLSF_ONEBYID or
public int dwControl ; // MIXER_GETLINECO NTROLSF_ONEBYpu blic
enum
public int cControls ; // count of controls pmxctrl points to
public int cbmxctrl ; // size in Byte of _one_ MIXERCONTROL
public IntPtr pamxctrl ; // pointer to first MIXERCONTROL
array
}

private static bool GetVolumeContro l(int hmixer ,int componentType,
int ctrlType, out MIXERCONTROL mxc, out int vCurrentVol)
{

// This function attempts to obtain a mixer control.
// Returns True if successful.
MIXERLINECONTRO LS mxlc = new MIXERLINECONTRO LS();
MIXERLINE mxl = new MIXERLINE();
MIXERCONTROLDET AILS pmxcd = new MIXERCONTROLDET AILS();
MIXERCONTROLDET AILS_UNSIGNED du = new
MIXERCONTROLDET AILS_UNSIGNED() ;
mxc = new MIXERCONTROL();
int rc ;
bool retValue;
vCurrentVol = -1;

//mxl.szShortName = new string(' ', MIXER_SHORT_NAM E_CHARS);
//mxl.szName = new string(' ', MIXER_LONG_NAME _CHARS);
mxl.cbStruct = Marshal.SizeOf( mxl);
mxl.dwComponent Type = componentType ;

// Obtain a line corresponding to the component public enum
rc = mixerGetLineInf oA(hmixer,ref mxl,
MIXER_GETLINEIN FOF_COMPONENTTY PE );

if(MMSYSERR_NOE RROR == rc)
{
int sizeofMIXERCONT ROL = 152;
//Marshal.SizeOf( typeof(MIXERCON TROL))
int ctrl = Marshal.SizeOf( typeof(MIXERCON TROL));
mxlc.pamxctrl = Marshal.AllocCo TaskMem(sizeofM IXERCONTROL) ;//new
MIXERCONTROL();
mxlc.cbStruct = Marshal.SizeOf( mxlc);
mxlc.dwLineID = mxl.dwLineID;
mxlc.dwControl = ctrlType;
mxlc.cControls = 1;
mxlc.cbmxctrl = sizeofMIXERCONT ROL;

// Allocate a buffer for the control
mxc.cbStruct = sizeofMIXERCONT ROL;

// Get the control
rc = mixerGetLineCon trolsA(hmixer,r ef mxlc,
MIXER_GETLINECO NTROLSF_ONEBYTY PE);

if(MMSYSERR_NOE RROR == rc)
{
retValue = true;

// Copy the control into the destination structure
mxc = (MIXERCONTROL)M arshal.PtrToStr ucture(mxlc.pam xctrl,typeof(MI XERCONTROL));

}
else
{
retValue = false;
}
int sizeofMIXERCONT ROLDETAILS =
Marshal.SizeOf( typeof(MIXERCON TROLDETAILS));
int sizeofMIXERCONT ROLDETAILS_UNSI GNED =
Marshal.SizeOf( typeof(MIXERCON TROLDETAILS_UNS IGNED));
pmxcd.cbStruct = sizeofMIXERCONT ROLDETAILS;
pmxcd.dwControl ID = mxc.dwControlID ;
pmxcd.paDetails =
Marshal.AllocCo TaskMem(sizeofM IXERCONTROLDETA ILS_UNSIGNED) ;
pmxcd.cChannels = 1;
pmxcd.item = 0;
pmxcd.cbDetails = sizeofMIXERCONT ROLDETAILS_UNSI GNED;

rc = mixerGetControl DetailsA(hmixer ,ref pmxcd,
MIXER_GETCONTRO LDETAILSF_VALUE );

du = (MIXERCONTROLDE TAILS_UNSIGNED) Marshal.PtrToSt ructure(pmxcd.p aDetails,typeof (MIXERCONTROLDE TAILS_UNSIGNED) );

vCurrentVol = du.dwValue;

return retValue;
}

retValue = false;
return retValue;
}

private static bool SetVolumeContro l(int hmixer , MIXERCONTROL mxc ,
int volume )
{
// This function sets the value for a volume control.
// Returns True if successful

bool retValue;
int rc;
MIXERCONTROLDET AILS mxcd = new MIXERCONTROLDET AILS();
MIXERCONTROLDET AILS_UNSIGNED vol = new
MIXERCONTROLDET AILS_UNSIGNED() ;

mxcd.item = 0;
mxcd.dwControlI D = mxc.dwControlID ;
mxcd.cbStruct = Marshal.SizeOf( mxcd);
mxcd.cbDetails = Marshal.SizeOf( vol);

// Allocate a buffer for the control value buffer
mxcd.cChannels = 1;
vol.dwValue = volume;

// Copy the data into the control value buffer
//mxcd.paDetails =
vol;//(MIXERCONTROL)M arshal.PtrToStr ucture(mxlc.pam xctrl,typeof(MI XERCONTROL));
mxcd.paDetails = Marshal.AllocCo TaskMem(Marshal .SizeOf(typeof( MIXERCONTROLDET AILS_UNSIGNED)) );
Marshal.Structu reToPtr(vol, mxcd.paDetails, false);
// Set the control value
rc = mixerSetControl Details(hmixer, ref mxcd,
MIXER_SETCONTRO LDETAILSF_VALUE );

if(MMSYSERR_NOE RROR == rc)
{
retValue = true;
}
else
{
retValue = false;
}
return retValue;
}

public static int GetVolume()
{
int mixer;
MIXERCONTROL volCtrl = new MIXERCONTROL();
int currentVol;
mixerOpen(out mixer,0 ,0 ,0, 0);
int type = MIXERCONTROL_CO NTROLTYPE_VOLUM E;
GetVolumeContro l(mixer,
MIXERLINE_COMPO NENTTYPE_DST_SP EAKERS,type,out volCtrl, out
currentVol);
mixerClose(mixe r);

return currentVol;
}
public static void SetVolume(int vVolume)
{
int mixer;
MIXERCONTROL volCtrl = new MIXERCONTROL();
int currentVol;
mixerOpen(out mixer,0 ,0 ,0, 0);
int type = MIXERCONTROL_CO NTROLTYPE_VOLUM E;
GetVolumeContro l(mixer,
MIXERLINE_COMPO NENTTYPE_DST_SP EAKERS,type,out volCtrl, out
currentVol);
if(vVolume>volC trl.lMaximum) vVolume = volCtrl.lMaximu m;
if(vVolume<volC trl.lMinimum) vVolume = volCtrl.lMinimu m;
SetVolumeContro l(mixer, volCtrl, vVolume);
GetVolumeContro l(mixer,
MIXERLINE_COMPO NENTTYPE_DST_SP EAKERS,type,out volCtrl, out
currentVol);
if(vVolume!=cur rentVol)
{
throw new Exception("Cann ot Set Volume");
}
mixerClose(mixe r);
}

}
Nov 15 '05 #1
2 11612
Wow. This is exactly something that I was looking for. Thanks for
posting this. Have there been any improvements since Christmas?

Eric
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Nov 15 '05 #2
Great Code!!!

But can somebody help me to read Recorder Mixer Line.
What I must do to read Level of Line Input line
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2386
by: Tal Sharfi | last post by:
hi all I need to playback two or more audio files ( .wav files) together. let's say it's a song and a person speaking. I need to control the volume of the two channels, so when the speaking file is playing, the song volume is lowered. can someone point me to a simple example in c-sharp (or other .net language) thats implements some kind of audio mixer?
4
3472
by: Robert Gravereaux | last post by:
I'm putting together a C# .Net forms project on win2k. The application requires some sort of horn sound. I've never implemented any audio in .Net, so I'm not sure how best to accomplish this. It's an application that works with a wireless barcode scanner, in a warehouse environment. The user could be potentially 20 or 30 ft. from the PC. If the user scans something improperly, the app should sound a 'horn' to notify the user. I'm trying...
0
1328
by: arkam | last post by:
Hi, Can someone tell me how to mute the line in entry of the mixer in csharp ? Where can I find docs on this ? Thanks, Arkam
0
1785
by: daniel123456 | last post by:
Hi, I'm searching for an audio mixer class that can change the balance o the lines too. I already found the AudioMixerHelper (e.g http://forums.hostrocket.com/archive/index.php/t-16820.html )class, bu this one can only change the volume for both channels. In other sources i saw that vol was an array like vol.dwvalue=rigthvol vol.dwvalue=leftvol
1
10677
by: Andrea V.F. | last post by:
I need to control (Get and Set) this parameters of Windows Audio Mixer: - Master Volume Mute - Master Volume Level In addition, but not mandatory, I need to control also the PC Speaker Mute and Volume. PC Speaker control is aviable only in some Systems such as laptops, in others systems there is no control of PC speaker in Windows Mixer.
0
1314
by: hzgt9b | last post by:
Using VB.NET 2003, QUESTION How do I set up my program so that it will be notified of any/all audio changes on the machine? I.e. if someone adjusts the machine’s volume outside of my app, I want my app to be notified so that I can update the app’s volume track bar or mute check box appropriately. BACKGROUND I have a windows app that plays some media (sounds, audio).... I cobbled
2
2705
by: BarryX | last post by:
Hey, I am trying to write a section of code in VB that gives me the audio level of the output from a sound card. If I am using a Soundblaster this is very easy as I can use the MIXERCONTROL_CONTROLTYPE_PEAKMETER. But not all sound cards have this. I have seen some code which looks at the level on the input by checking samples and I can see that this makes the concept independent of the H/W which is what I need.
0
1062
by: midmukesh | last post by:
Hi, I am Mukesh, I have prepared an web application in ASP.NET 2003, And I want that, when user / client will open the link of my site, the system of that user will automatically be checked and will be detected weather sound is installed or not on that system. And if it will, then sound level of that system will also be checked, and will be informed to the user that sound is mute or not. I hope, This breifing would have made picture cleare...
0
8279
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8811
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8703
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8467
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5619
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4145
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4291
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1914
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1591
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.