通过API选择声卡输入通道

网上此类贴子很多,工作原理就不用介绍了,请看代码:

 

  1. // AudioInputChannel.cpp : 定义控制台应用程序的入口点。
  2. //
  3. #include "stdafx.h"
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <tchar.h>
  7. #include <windows.h>
  8. #ifndef _INC_MMSYSTEM
  9. #include <mmsystem.h>
  10. #pragma comment(lib, "winmm.lib")
  11. #endif
  12. void PrintMixerLinesInfo(void);
  13. bool ActiveMixerInputChannel(UINTUINT);
  14. int _tmain(int argc, _TCHAR* argv[])
  15. {
  16.     UINT di, li;
  17.     PrintMixerLinesInfo ();
  18.     _tcprintf( TEXT("输入设备编号与录音通道编号(例:0 1)") ); 
  19.     _tscanf ( TEXT("%d %d"), &di, &li );
  20.     if ( !ActiveMixerInputChannel ( di, li ) ){
  21.         _tcprintf( TEXT("设置失败!/n") ); 
  22.     } else {
  23.         _tcprintf( TEXT("设置成功!/n") ); 
  24.         PrintMixerLinesInfo ();
  25.     }
  26.     system("pause");
  27.     return 0;
  28. }
  29. void PrintMixerLinesInfo(void)
  30. {
  31.     MIXERCAPS mxcaps        = {0};
  32.     HMIXER hMixer           = NULL;
  33.     for ( UINT i = 0; i < mixerGetNumDevs (); i++ ){
  34.         if ( MMSYSERR_NOERROR != mixerGetDevCaps ( i, &mxcaps, sizeof ( MIXERCAPS ) ) ){ continue; }    
  35.         if ( MMSYSERR_NOERROR != mixerOpen ( &hMixer, i, NULL, NULL, 0 ) ){ continue; }
  36.         MIXERLINE mxl           = { sizeof ( MIXERLINE ), 0, 0, 0, 0, 0, MIXERLINE_COMPONENTTYPE_DST_WAVEIN, 0};
  37.         if ( MMSYSERR_NOERROR != mixerGetLineInfo ( (HMIXEROBJ)hMixer, &mxl, MIXER_GETLINEINFOF_COMPONENTTYPE ) ){ 
  38.             mixerClose ( hMixer );
  39.             continue
  40.         }
  41.         _tcprintf( TEXT("%d: %s   /t[%s]/n"), i, mxcaps.szPname, mxl.szName ); 
  42.         MIXERCONTROL mxc        = { sizeof ( MIXERCONTROL ), 0 }; 
  43.         MIXERLINECONTROLS mxlc  = { sizeof ( MIXERLINECONTROLS ), mxl.dwLineID, MIXERCONTROL_CONTROLTYPE_MIXER, 1, mxc.cbStruct, &mxc }; 
  44.         if ( MMSYSERR_NOERROR != mixerGetLineControls ( (HMIXEROBJ)hMixer, &mxlc, MIXER_GETLINECONTROLSF_ONEBYTYPE ) ){ 
  45.             mxlc.dwControlType  = MIXERCONTROL_CONTROLTYPE_MUX;
  46.             if ( MMSYSERR_NOERROR != mixerGetLineControls ( (HMIXEROBJ)hMixer, &mxlc, MIXER_GETLINECONTROLSF_ONEBYTYPE ) ){ 
  47.                 mixerClose ( hMixer );
  48.                 continue
  49.             }
  50.         }
  51.         if ( mxc.cMultipleItems == 0 ){
  52.             mixerClose ( hMixer );
  53.             continue;
  54.         }
  55.         MIXERCONTROLDETAILS mxcd    = { sizeof ( MIXERCONTROLDETAILS ), mxc.dwControlID, 1, 0 };        
  56.         PMIXERCONTROLDETAILS_LISTTEXT mxcdlts   = (PMIXERCONTROLDETAILS_LISTTEXT)malloc ( sizeof ( MIXERCONTROLDETAILS_LISTTEXT ) * mxc.cMultipleItems );
  57.         mxcd.cbDetails          = sizeof ( MIXERCONTROLDETAILS_LISTTEXT );
  58.         mxcd.cMultipleItems     = mxc.cMultipleItems;
  59.         mxcd.paDetails          = mxcdlts;
  60.         if ( MMSYSERR_NOERROR != mixerGetControlDetails ( (HMIXEROBJ)hMixer, &mxcd, MIXER_GETCONTROLDETAILSF_LISTTEXT ) ){ 
  61.             free ( mxcdlts );
  62.             mixerClose ( hMixer );
  63.             continue
  64.         }
  65.         PMIXERCONTROLDETAILS_BOOLEAN mxcdbls    = (PMIXERCONTROLDETAILS_BOOLEAN)malloc ( sizeof ( MIXERCONTROLDETAILS_BOOLEAN ) * mxc.cMultipleItems );
  66.         mxcd.cbDetails          = sizeof ( MIXERCONTROLDETAILS_BOOLEAN );
  67.         mxcd.cMultipleItems     = mxc.cMultipleItems;
  68.         mxcd.paDetails          = mxcdbls;
  69.         if ( MMSYSERR_NOERROR != mixerGetControlDetails ( (HMIXEROBJ)hMixer, &mxcd, MIXER_GETCONTROLDETAILSF_VALUE ) ){ 
  70.             free ( mxcdbls );
  71.             mixerClose ( hMixer );
  72.             continue
  73.         }
  74.         
  75.         DWORD dwConnections     = mxl.cConnections;
  76.         DWORD dwDestination     = mxl.dwDestination;
  77.         for ( DWORD j = 0; j < dwConnections; j++ ){ 
  78.             mxl.dwDestination   = dwDestination;
  79.             mxl.dwSource        = j;
  80.             if ( MMSYSERR_NOERROR != mixerGetLineInfo ( (HMIXEROBJ)hMixer, &mxl, MIXER_GETLINEINFOF_DESTINATION | MIXER_GETLINEINFOF_SOURCE ) ){ continue; }
  81.             for ( DWORD k = 0; k < mxc.cMultipleItems; k++ ){
  82.                 if ( mxcdlts[k].dwParam1 == mxl.dwLineID ){
  83.                     _tcprintf( TEXT("/t%d %s   /t%s/n"), j, mxl.szName, mxcdbls[k].fValue ? TEXT("Enabled") : TEXT("Disabled") );
  84.                     break;
  85.                 }
  86.             }
  87.         }
  88.         _tcprintf( TEXT("/n") ); 
  89.         free ( mxcdbls );
  90.         free ( mxcdlts ); 
  91.         mixerClose ( hMixer );
  92.     }
  93. }
  94. bool ActiveMixerInputChannel(UINT nMixerDeviceIndex, UINT nMixerLineIndex)
  95. {
  96.     bool result             = false;
  97.     HMIXER hMixer           = NULL;
  98.     MIXERCONTROL mxc        = { sizeof ( MIXERCONTROL ), 0 }; 
  99.     MIXERLINE mxl           = { sizeof ( MIXERLINE ), 0, 0, 0, 0, 0, MIXERLINE_COMPONENTTYPE_DST_WAVEIN, 0};
  100.     if ( MMSYSERR_NOERROR != mixerOpen ( &hMixer, nMixerDeviceIndex, NULL, NULL, 0 ) ){ return false; }
  101.     if ( MMSYSERR_NOERROR != mixerGetLineInfo ( (HMIXEROBJ)hMixer, &mxl, MIXER_GETLINEINFOF_COMPONENTTYPE ) ){ 
  102.         mixerClose ( hMixer ); 
  103.         return false
  104.     }
  105.     MIXERLINECONTROLS mxlc  = { sizeof ( MIXERLINECONTROLS ), mxl.dwLineID, MIXERCONTROL_CONTROLTYPE_MIXER, 1, mxc.cbStruct, &mxc }; 
  106.     mxl.dwSource            = nMixerLineIndex;
  107.     if ( MMSYSERR_NOERROR != mixerGetLineInfo ( (HMIXEROBJ)hMixer, &mxl, MIXER_GETLINEINFOF_DESTINATION | MIXER_GETLINEINFOF_SOURCE ) ){ 
  108.         mixerClose ( hMixer ); 
  109.         return false
  110.     }
  111.     if ( MMSYSERR_NOERROR != mixerGetLineControls ( (HMIXEROBJ)hMixer, &mxlc, MIXER_GETLINECONTROLSF_ONEBYTYPE ) ){ 
  112.         mxlc.dwControlType  = MIXERCONTROL_CONTROLTYPE_MUX;
  113.         if ( MMSYSERR_NOERROR != mixerGetLineControls ( (HMIXEROBJ)hMixer, &mxlc, MIXER_GETLINECONTROLSF_ONEBYTYPE ) ){ 
  114.             mixerClose ( hMixer ); 
  115.             return false
  116.         }
  117.     }
  118.     if ( mxc.cMultipleItems == 0 ){ mixerClose ( hMixer ); return false; }
  119.     PMIXERCONTROLDETAILS_BOOLEAN mxcdbls    = (PMIXERCONTROLDETAILS_BOOLEAN)malloc ( sizeof ( MIXERCONTROLDETAILS_BOOLEAN ) * mxc.cMultipleItems );
  120.     PMIXERCONTROLDETAILS_LISTTEXT mxcdlts   = (PMIXERCONTROLDETAILS_LISTTEXT)malloc ( sizeof ( MIXERCONTROLDETAILS_LISTTEXT ) * mxc.cMultipleItems );
  121.     MIXERCONTROLDETAILS mxcd                = { sizeof ( MIXERCONTROLDETAILS ), mxc.dwControlID, 1, (HWND)mxc.cMultipleItems, sizeof ( MIXERCONTROLDETAILS_LISTTEXT ), mxcdlts };       
  122.     if ( MMSYSERR_NOERROR == mixerGetControlDetails ( (HMIXEROBJ)hMixer, &mxcd, MIXER_GETCONTROLDETAILSF_LISTTEXT ) ){ 
  123.         memset ( mxcdbls, 0, sizeof ( MIXERCONTROLDETAILS_BOOLEAN ) * mxc.cMultipleItems );
  124.         for ( DWORD i = 0; i < mxc.cMultipleItems; i++ ){ 
  125.             if ( mxcdlts[i].dwParam1 == mxl.dwLineID ){
  126.                 mxcdbls[i].fValue       = 1;    
  127.                 mxcd.paDetails          = mxcdbls;
  128.                 mxcd.cbDetails          = sizeof ( MIXERCONTROLDETAILS_BOOLEAN );
  129.                 result  = (MMSYSERR_NOERROR == mixerSetControlDetails ( (HMIXEROBJ)hMixer, &mxcd, MIXER_SETCONTROLDETAILSF_VALUE ) ); break;
  130.             }
  131.         }
  132.     }
  133.     free ( mxcdbls );
  134.     free ( mxcdlts ); 
  135.     mixerClose ( hMixer );
  136.     return result;
  137. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值