Thread: Detect the Audio And Microphone Level In Windows

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    2

    Detect the Audio And Microphone Level In Windows

    I am trying to write an equalizer in C, that opens in Windows as a console program. The problem is that I'm stuck on the retrieving data of audio levels from Windows. I've been searching the web for a couple days, and the only thing I found was:

    MMRESULT WINAPI waveOutGetVolume(hwo,pdwVolume);

    I can't seem to get it to work though. Current code just to check that function is:

    Code:
    #include<stdio.h>
    #include<Windows.h>
    
    
    int main()
    {
      HWAVEOUT hwo;
      PDWORD pdwVolume;
      while(6)
      {
        MMRESULT WINAPI waveOutGetVolume(hwo,pdwVolume);
        printf("%d\n",(int) pdwVolume);
        Sleep(100);
      }
      getch();
      return(0);
    }
    I think part of the problem is in the PDWORD to int display, but I'm not sure how to fix it. Currently it displays one number, never changing.

    Also, in case this is needed, I an using Dev-C++ to edit and compile. Thank you for the help.

  2. #2
    Registered User
    Join Date
    Apr 2007
    Posts
    137
    See MSDN samples (SDK) or on Win32 grp Discussions - comp.os.ms-windows.programmer.win32 | Google Groups (C & C++)

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    This belongs more in the Windows programming forum.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    290
    MSDN Windows Multimedia - Audio Mixers

    For whatever reason, it took me about 10 minutes to find a link to the audio mixer page in MSDN. Searches like "win32 mixer api" and even a direct "Bing" search of MSDN for "mixerGetLineInfo" kept redirecting me to pages about Windows Mobile.

    I've used the mixer API before to get system volume and other information about audio devices. It should get you what you need.

  5. #5
    Registered User
    Join Date
    Nov 2009
    Posts
    2
    Thank you all for the help. I'm sorry if it is in the wrong forum, I'm trying to write it in C, so I thought this was the place. Looking through the links provided, I'm finding several functions that are what I need, but I keep getting the save error message. I have try testing code that others have written, and even the finished versions give me the same error, so I'm a bit confused.

    Code:
    #include<stdio.h>
    #include<Windows.h>
    
    
    int main()
    {
      int NumMixers = mixerGetNumDevs();
      int volume;
      volume = auxGetVolume(UINT,PDWORD);
      while(6)
      {
        NumMixers = mixerGetNumDevs();
        printf("%d\n",volume);
        Sleep(100);
      }
      getch();
      return(0);
    }
    I know there is a syntax error with the auxGetVolume, but even with the mixerGetNumDevs, it's saying " [Linker error] undefined reference to `mixerGetNumDevs@0' "

  6. #6
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    I know there is a syntax error with the auxGetVolume, but even with the mixerGetNumDevs, it's saying " [Linker error] undefined reference to `mixerGetNumDevs@0' "
    Link with Winmm.lib

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    290
    Here's a small app I wrote years ago. Some of the comments look a little fishy, and I'm not even sure it's entirely correct, but it does seem to work for me.
    Code:
    #include <windows.h>
    
    /*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
     * A small commandline tool to check the volume set on the speaker out line of
     * the primary/default sound card mixer device.
     * 
     * Basically, it prints out the master audio volume for the system.
     *vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
     */    
    int main(int argc, char ** argv) {
      HMIXER mixerHandle;
      MIXERCAPS mixcaps;
      MIXERLINE mixerline;
      MIXERCONTROL volumeControl;
      MIXERLINECONTROLS mixerLineControls;
      MIXERCONTROLDETAILS_UNSIGNED value;
      MIXERCONTROLDETAILS mixerControlDetails;
      
      // Open the mixer associated with the default sound card in the computer
      if (mixerOpen(&mixerHandle, 0, 0, 0, 0)) {
          printf("Couldn't open mixer\n");
      }
      else {
        // Get info about the first Mixer Device
        if (mixerGetDevCaps((UINT)mixerHandle, &mixcaps, sizeof(MIXERCAPS))) {
          printf("Error calling mixerGetDevCaps()\n");
        }
        else {
          printf("Sound Card:  %s\n", mixcaps.szPname);
          printf("There are %d supported destinations for this card\n", mixcaps.cDestinations);
          printf("\nFetching info about the speaker line out:\n");
          
          mixerline.cbStruct = sizeof(MIXERLINE);
          mixerline.dwComponentType = MIXERLINE_COMPONENTTYPE_DST_SPEAKERS;
            
          // Get info about the speaker destination line
          if (mixerGetLineInfo((HMIXEROBJ)mixerHandle, &mixerline, MIXER_GETLINEINFOF_COMPONENTTYPE)) {
            printf("Error getting speakers line info\n");
          }
          else {
            printf("  Name:         %s\n", mixerline.szShortName);
            printf("  Description:  %s\n", mixerline.szName);
            
            mixerLineControls.cbStruct = sizeof(MIXERLINECONTROLS);
            mixerLineControls.dwLineID = mixerline.dwLineID;
            mixerLineControls.dwControlType = MIXERCONTROL_CONTROLTYPE_VOLUME;
            mixerLineControls.cControls = 1;
            mixerLineControls.pamxctrl = &volumeControl;
            mixerLineControls.cbmxctrl = sizeof(MIXERCONTROL);
            
            // Get info about the speaker line's volume control
            if (mixerGetLineControls((HMIXEROBJ)mixerHandle, &mixerLineControls, MIXER_GETLINECONTROLSF_ONEBYTYPE)) {
              printf("Couldn't get info about the speaker line's volume control\n");
            }
            else {
              mixerControlDetails.cbStruct = sizeof(MIXERCONTROLDETAILS);
              mixerControlDetails.dwControlID = volumeControl.dwControlID;
              mixerControlDetails.cChannels = 1;
              mixerControlDetails.cMultipleItems = volumeControl.cMultipleItems;
              mixerControlDetails.paDetails = &value;
              mixerControlDetails.cbDetails = sizeof(MIXERCONTROLDETAILS_UNSIGNED);
              
              // Get the value of the speaker line's volume control
              if (mixerGetControlDetails((HMIXEROBJ)mixerHandle, &mixerControlDetails, MIXER_GETCONTROLDETAILSF_VALUE)) {
                printf("Couldn't get speaker line's volume control details\n");
              }
              else {
                printf("Speaker line's volume is set to %u (%d%%)\n", value.dwValue,
                         (int)((double)value.dwValue/volumeControl.Bounds.dwMaximum*100));
              }
            }
          }
        }
      }
      
      printf("Closing mixer device's handle\n");
      mixerClose(mixerHandle);
    }
    And here's a sample output running it once on my machine, increasing my system volume a bit, and then running it again:
    Code:
    Z:\>mixer
    Sound Card:  Realtek HD Audio output
    There are 1 supported destinations for this card
    
    Fetching info about the speaker line out:
      Name:         Master Volume
      Description:  Master Volume
    Speaker line's volume is set to 7864 (11%)
    Closing mixer device's handle
    
    Z:\>mixer
    Sound Card:  Realtek HD Audio output
    There are 1 supported destinations for this card
    
    Fetching info about the speaker line out:
      Name:         Master Volume
      Description:  Master Volume
    Speaker line's volume is set to 15728 (23%)
    Closing mixer device's handle
    I know they revamped the audio APIs when Vista came out. Obviously the mixer API is still there, but its been reimplemented on top of whatever it is that Vista uses now. If I remember right, the changes make it harder to get the actual "system volume" since a lot of the volume settings are per application in Vista. I know I wrote a version of my app that works in Vista, so I'll see if I can't dig up an example for that as well.
    Last edited by arpsmack; 11-18-2009 at 12:48 AM. Reason: Split huge line of code that "broke the tables"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Windows Multimedia API - Audio Mixers
    By dit6a9 in forum Windows Programming
    Replies: 4
    Last Post: 09-16-2004, 08:47 PM