代码之家  ›  专栏  ›  技术社区  ›  Alon Gubkin

以C为单位获取主音量#

  •  20
  • Alon Gubkin  · 技术社区  · 15 年前

    我需要把当前输出的音量加到声卡上。

    有什么办法吗?

    7 回复  |  直到 7 年前
        1
  •  12
  •   Mark Heath    15 年前

    您可以使用 IAudioMeterInformation 在Vista和Win 7的CoreAudio API中。

    托管包装可用于 NAudio (从mmdevice获取听力计信息)。

        2
  •  3
  •   Basil    11 年前
        static int PlayerVolume()
        {
            RecordPlayer rp = new RecordPlayer();
            rp.PlayerID = -1;
            int playerVolume = rp.PlayerVolume;
            return playerVolume;
        }
    

    从修改 Microphone Volume in c# 文章

        3
  •  2
  •   VitalyVal    15 年前

    在msdn信息中查找:

    • immdeviceCollection、immdevice和iaudioendpointvolume(仅限Windows Vista、Windows 7)。
    • mixergetnumdevs,mixergetlinecontrols,…

    这是“常见”信息。C可能有更方便的方法(我不知道)。

        4
  •  2
  •   Morten Bergfall    7 年前

    当我在一个应用程序上工作时(还没有发布…)解决了这个问题,该应用程序在没有其他声音的情况下启动某种“电梯音乐”。

    按照马克·希思的绝妙建议,我得到了我想要的:

     using NAudio.CoreAudioApi; 
     MMDeviceEnumerator devEnum = new MMDeviceEnumerator();
     MMDevice defaultDevice = devEnum.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia);
     string currVolume = "MasterPeakVolume : " + defaultDevice.AudioMeterInformation.MasterPeakValue.ToString();
    
        5
  •  1
  •   JoeBilly    15 年前

    也许winmm.dll可以帮助您:

    来自Eddykt(vb):

    Private Const HIGHEST_VOLUME_SETTING = 100 '%
    Private Const AUX_MAPPER = -1&
    Private Const MAXPNAMELEN = 32
    Private Const AUXCAPS_CDAUDIO = 1   '  audio from internal CD-ROM drive
    Private Const AUXCAPS_AUXIN = 2   '  audio from auxiliary input jacks
    Private Const AUXCAPS_VOLUME = &H1          '  supports volume control
    Private Const AUXCAPS_LRVOLUME = &H2          '  separate left-right volume control
    Private Const MMSYSERR_NOERROR = 0
    Private Const MMSYSERR_BASE = 0
    Private Const MMSYSERR_BADDEVICEID = (MMSYSERR_BASE + 2)
    Private Type AUXCAPS
           wMid As Integer
           wPid As Integer
           vDriverVersion As Long
           szPname As String * MAXPNAMELEN
           wTechnology As Integer
           dwSupport As Long
    End Type
    Private Type VolumeSetting
        LeftVol As Integer
        RightVol As Integer
    End Type
    Private Declare Function auxGetNumDevs Lib "winmm.dll" () As Long
    Private Declare Function auxGetDevCaps Lib "winmm.dll" Alias "auxGetDevCapsA" (ByVal uDeviceID As Long, lpCaps As AUXCAPS, ByVal uSize As Long) As Long
    Private Declare Function auxSetVolume Lib "winmm.dll" (ByVal uDeviceID As Long, ByVal dwVolume As Long) As Long
    Private Declare Function auxGetVolume Lib "winmm.dll" (ByVal uDeviceID As Long, ByRef lpdwVolume As VolumeSetting) As Long
    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)
    Private Function nSigned(ByVal lUnsignedInt As Long) As Integer
        Dim nReturnVal As Integer                          ' Return value from Function
        If lUnsignedInt > 65535 Or lUnsignedInt < 0 Then
            MsgBox "Error in conversion from Unsigned to nSigned Integer"
            nSignedInt = 0
            Exit Function
        End If
        If lUnsignedInt > 32767 Then
            nReturnVal = lUnsignedInt - 65536
        Else
            nReturnVal = lUnsignedInt
        End If
        nSigned = nReturnVal
    End Function
    Private Function lUnsigned(ByVal nSignedInt As Integer) As Long
        Dim lReturnVal As Long                          ' Return value from Function
        If nSignedInt < 0 Then
            lReturnVal = nSignedInt + 65536
        Else
            lReturnVal = nSignedInt
        End If
        If lReturnVal > 65535 Or lReturnVal < 0 Then
            MsgBox "Error in conversion from nSigned to Unsigned Integer"
            lReturnVal = 0
        End If
        lUnsigned = lReturnVal
    End Function
    Private Function lSetVolume(ByRef lLeftVol As Long, ByRef lRightVol As Long, lDeviceID As Long) As Long
        Dim Volume As VolumeSetting, lBothVolumes As Long
        Volume.LeftVol = nSigned(lLeftVol * 65535 / HIGHEST_VOLUME_SETTING)
        Volume.RightVol = nSigned(lRightVol * 65535 / HIGHEST_VOLUME_SETTING)
        'copy our Volume-variable to a long
        CopyMemory lBothVolumes, Volume.LeftVol, Len(Volume)
        'call the SetVolume-function
        lSetVolume = auxSetVolume(lDeviceID, lBothVolumes)
    End Function
    Private Sub Form_Load()
        'KPD-Team 2000
        'URL: http://www.allapi.net/
        'E-Mail: KPDTeam@Allapi.net
        Dim Volume As VolumeSetting, Cnt As Long, AC As AUXCAPS
        'set the output to a persistent graphic
        Me.AutoRedraw = True
        'loop through all the devices
        For Cnt = 0 To auxGetNumDevs - 1 'auxGetNumDevs is zero-based
            'get the volume
            auxGetVolume Cnt, Volume
            'get the device capabilities
            auxGetDevCaps Cnt, AC, Len(AC)
            'print the name on the form
            Me.Print "Device #" + Str$(Cnt + 1) + ":  " + Left(AC.szPname, InStr(AC.szPname, vbNullChar) - 1)
            'print the left- and right volume on the form
            Me.Print "Left volume:" + Str$(HIGHEST_VOLUME_SETTING * lUnsigned(Volume.LeftVol) / 65535)
            Me.Print "Right volume:" + Str$(HIGHEST_VOLUME_SETTING * lUnsigned(Volume.RightVol) / 65535)
            'set the left- and right-volume to 50%
            lSetVolume 50, 50, Cnt
            Me.Print "Both volumes now set to 50%"
            'empty line
            Me.Print
        Next
    End Sub
    

    或者这个: http://blackbeltvb.com/index.htm?free/mcisamp.htm

        6
  •  0
  •   GalacticJello    15 年前

    从代码项目中签出此代码: LED Style Volume Meter Using DirectX

    本文用作使用手册 对于我创建的用户控件,调用 模拟信号计。此控件使用 Direct3D绘制控件,以及 DirectSound采样音频 信号。

    它有一个模拟信号表对象,它被触发一个事件,该事件将报告当前的左右扬声器级别。

        7
  •  0
  •   Community CDub    8 年前

    我不相信有一种简单的方法可以在xp下获得当前的峰值。MixerControl_ControlType_峰值计存在,但我相信它在很大程度上是不受支持的(它在我当前的机器上)。我猜你会有自己的方法来分析当前的音频输出,看看DSP部分 here .

    您可以在运行时决定要使用哪种方法,xp和vista/7有非常不同的处理音频的方法。我以前写过的关于这件事的一些可能有用的信息 here.

    在我看来,msdn文档和LarryOsterman的博客(他也是so的成员)可能是当前Windows音频基础结构的两个最有用的来源。