gpt4 book ai didi

c# - 在 vb.net 中运行基于声音 VU 级别的脚本

转载 作者:太空狗 更新时间:2023-10-29 18:21:29 26 4
gpt4 key购买 nike

我正在设计一个应用程序来为我制作我的一款游戏。但是,我要采用的方法是在运行所需的宏组合之前,在声级 中聆听声音或尖峰。我要监视的音频是当前 PC 的音频输出,而不是麦克风或外部设备。

这个主题似乎在教程或信息上含糊不清,但据我所知,我发现了一个帖子 here这解释了使用 BuckSoft.DirectSound项目?

那么根据我找到的信息,我假设你做了类似下面的事情?

If AnalogSignalMeter1.LeftLevel > 0 Or AnalogSignalMeter1.RightLevel > 0 Then

' Do Something

End If

从个人角度来看,我很乐意帮助向公众阐明解决方案,因为这个主题没有得到很好的记录和回避。我对所有解决方案或建议持开放态度,但是,我的重点是 vb.net,如果需要会考虑 C#


我看到的另一个选项是 CoreAudio API。我已经在多个帖子中看到此 API 用于提取当前声级的能力,但是我还没有看到读取当前 Master VU meter and fader data/Levels 的示例。

Private Function GetVol() As Integer        'Function to read current volume setting
Dim MasterMinimum As Integer = 0
Dim DevEnum As New MMDeviceEnumerator()
Dim device As MMDevice = DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia)
Dim Vol As Integer = 0

With device.AudioEndpointVolume
Vol = CInt(.MasterVolumeLevelScalar * 100)
If Vol < MasterMinimum Then
Vol = MasterMinimum / 100.0F
End If
End With
Return Vol
End Function

我要收集的数据:

如下所示,我不希望收集物理声级 %,而是希望在 VU 级别从 0 飙升时运行操作。这意味着如果我播放视频或声音文件,应用程序将听到来自当前工作站的声音并执行所需的操作。

As Seen Here

下面将是我计划如何使用或收集潜在客户数据的粗略示例。在 vb.net 中使用计时器,我可以有一个语句始终在 "VUSoundLevels" 中寻找变化(不是真正的语句)并在发生变化/输入时运行脚本。

Private Sub Timer1_Tick()

If VUSoundLevels > 0 Then

' Run Code & Exit Loop

End IF

End Sub

最佳答案

这是一个使用 Windows Core Audio Library 的 C# 解决方案

此 API 具有 Session 的概念(这有点对应于您在 Volume Mixer 中看到的内容)。因此,我提供了一个 AudioSession 包装类,它可以为您提供有关 Windows 中所有当前 session 的各种信息,包括 session 进程 ID 以及可能的名称、显示名称、图标等详细信息。

此类还有一个 GetChannelsPeakValues() 方法,它使用 IAudioMeterInformation interface获得每个音频 channel 的峰值。

这是一个 C# 控制台应用程序示例(但 AudioSession 类支持任何 UI 技术),在运行时,将显示 chrome 浏览器 实例(运行一些视频或来自浏览器的声音和数字应该开始移动)。如果您没有 chrome,请使用您选择的其他进程。

class Program
{
static void Main(string[] args)
{
// Here, I'm just monitoring chrome, one of the possible sessions.
// If you want the whole speakers peak values, use the AudioSession.GetSpeakersChannelsPeakValues() method
foreach (var session in AudioSession.EnumerateAll())
{
if (session.Process?.ProcessName == "chrome")
{
do
{
var values = session.GetChannelsPeakValues();
if (values.Length == 0)
continue;

Console.WriteLine(string.Join(" ", values.Select(v => v.ToString("00%"))));
}
while (true);
}
session.Dispose();
}
}
}

下面是支持的 C# 代码:

public class AudioSession : IDisposable
{
private readonly Lazy<Icon> _icon;
private readonly Lazy<Process> _process;
private readonly IAudioSessionControl2 _control;

public AudioSession(IAudioSessionControl2 control)
{
_control = control;
control.GetState(out var state);
State = state;
control.GetGroupingParam(out var guid);
GroupingParam = guid;
IconPath = GetString(control.GetIconPath);
DisplayName = GetString(control.GetDisplayName);
_icon = new Lazy<Icon>(GetIcon, true);
_process = new Lazy<Process>(() => Process.GetProcessById(ProcessId), true);
Id = GetString(control.GetSessionIdentifier);
InstanceId = GetString(control.GetSessionInstanceIdentifier);
control.GetProcessId(out var pid);
ProcessId = pid;
IsSystemSounds = control.IsSystemSoundsSession() == 0;
}

public AudioSessionState State { get; }
public string IconPath { get; }
public string DisplayName { get; }
public Guid GroupingParam { get; }
public Icon Icon => _icon.Value;
public string Id { get; }
public string InstanceId { get; }
public int ProcessId { get; }
public Process Process => _process.Value;
public bool IsSystemSounds { get; }

public float[] GetChannelsPeakValues()
{
var meter = (IAudioMeterInformation)_control;
meter.GetMeteringChannelCount(out var channelCount);
var values = new float[channelCount];
meter.GetChannelsPeakValues(channelCount, values);
return values;
}

private delegate int GetStringFn(out IntPtr ptr);
private static string GetString(GetStringFn fn)
{
fn(out var ptr);
if (ptr == IntPtr.Zero)
return null;

try
{
var s = Marshal.PtrToStringUni(ptr);
if (!string.IsNullOrWhiteSpace(s) && s.StartsWith("@"))
{
var sb = new StringBuilder(256);
if (SHLoadIndirectString(s, sb, sb.Capacity, IntPtr.Zero) == 0)
{
s = sb.ToString();
}
}
return s;
}
finally
{
Marshal.FreeCoTaskMem(ptr);
}
}

private Icon GetIcon()
{
if (string.IsNullOrWhiteSpace(IconPath))
return null;

var index = ParseIconLocationPath(IconPath, out var path);
// note this may only work if the OS bitness is the same as this process bitness
var hIcon = ExtractIcon(IntPtr.Zero, path, index);
return hIcon == IntPtr.Zero ? null : Icon.FromHandle(hIcon);
}

public override string ToString() => DisplayName;
public void Dispose() => _icon.Value?.Dispose();

public static float[] GetSpeakersChannelsPeakValues()
{
// get the speakers (1st render + multimedia) device
var deviceEnumerator = (IMMDeviceEnumerator)(new MMDeviceEnumerator());
deviceEnumerator.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia, out IMMDevice speakers);
if (speakers == null)
return new float[0];

// get meter information
speakers.Activate(typeof(IAudioMeterInformation).GUID, 0, IntPtr.Zero, out object o);
var meter = (IAudioMeterInformation)o;
if (meter == null)
return new float[0];

meter.GetMeteringChannelCount(out var count);
if (count == 0)
return new float[0];

var values = new float[count];
meter.GetChannelsPeakValues(count, values);
return values;
}

public static IEnumerable<AudioSession> EnumerateAll()
{
// get the speakers (1st render + multimedia) device
var deviceEnumerator = (IMMDeviceEnumerator)(new MMDeviceEnumerator());
deviceEnumerator.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia, out IMMDevice speakers);
if (speakers == null)
yield break;

// activate the session manager, we need the enumerator
speakers.Activate(typeof(IAudioSessionManager2).GUID, 0, IntPtr.Zero, out object o);
var sessionManager = (IAudioSessionManager2)o;
if (sessionManager == null)
yield break;

// enumerate sessions for on this device
sessionManager.GetSessionEnumerator(out IAudioSessionEnumerator sessionEnumerator);
sessionEnumerator.GetCount(out int count);

for (int i = 0; i < count; i++)
{
sessionEnumerator.GetSession(i, out var sessionControl);
if (sessionControl != null)
{
var meter = sessionControl as IAudioMeterInformation;
yield return new AudioSession(sessionControl);
}
}
}

[DllImport("shlwapi", CharSet = CharSet.Unicode)]
private extern static int SHLoadIndirectString(string pszSource, StringBuilder pszOutBuf, int cchOutBuf, IntPtr ppvReserved);

[DllImport("shlwapi", CharSet = CharSet.Unicode)]
private static extern int PathParseIconLocation(string pszIconFile);

[DllImport("shell32", CharSet = CharSet.Unicode)]
private static extern IntPtr ExtractIcon(IntPtr ptr, string pszExeFileName, int nIconIndex);

private static int ParseIconLocationPath(string location, out string path)
{
if (location == null)
throw new ArgumentNullException(nameof(location));

path = string.Copy(location);
int index = PathParseIconLocation(path);
int pos = path.LastIndexOf('\0');
if (pos >= 0)
{
path = path.Substring(0, pos);
}

if (path.StartsWith("@"))
{
path = path.Substring(1);
}
return index;
}
}

[ComImport]
[Guid("BCDE0395-E52F-467C-8E3D-C4579291692E")]
public class MMDeviceEnumerator
{
}

public enum EDataFlow
{
eRender,
eCapture,
eAll,
EDataFlow_enum_count
}

public enum ERole
{
eConsole,
eMultimedia,
eCommunications,
ERole_enum_count
}

[Guid("a95664d2-9614-4f35-a746-de8db63617e6"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public partial interface IMMDeviceEnumerator
{
[PreserveSig]
int EnumAudioEndpoints(EDataFlow dataFlow, uint dwStateMask, out IntPtr ppDevices);

[PreserveSig]
int GetDefaultAudioEndpoint(EDataFlow dataFlow, ERole role, out IMMDevice ppEndpoint);

[PreserveSig]
int GetDevice([MarshalAs(UnmanagedType.LPWStr)] string pwstrId, out IMMDevice ppDevice);

[PreserveSig]
int RegisterEndpointNotificationCallback(IntPtr pClient);

[PreserveSig]
int UnregisterEndpointNotificationCallback(IntPtr pClient);
}

[Guid("d666063f-1587-4e43-81f1-b948e807363f"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public partial interface IMMDevice
{
[PreserveSig]
int Activate([MarshalAs(UnmanagedType.LPStruct)] Guid iid, uint dwClsCtx, [In, Out] IntPtr pActivationParams, [MarshalAs(UnmanagedType.IUnknown)] out object ppInterface);

[PreserveSig]
int OpenPropertyStore(uint stgmAccess, out IntPtr ppProperties);

[PreserveSig]
int GetId(out IntPtr ppstrId);

[PreserveSig]
int GetState(out uint pdwState);
}

[Guid("C02216F6-8C67-4B5B-9D00-D008E73E0064"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IAudioMeterInformation
{
[PreserveSig]
int GetPeakValue(out float pfPeak);

[PreserveSig]
int GetMeteringChannelCount(out int pnChannelCount);

[PreserveSig]
int GetChannelsPeakValues(int u32ChannelCount, [Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] float[] afPeakValues);

[PreserveSig]
int QueryHardwareSupport(out int pdwHardwareSupportMask);
}

[Guid("77aa99a0-1bd6-484f-8bc7-2c654c9a9b6f"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public partial interface IAudioSessionManager2
{
// IAudioSessionManager
[PreserveSig]
int GetAudioSessionControl(IntPtr AudioSessionGuid, uint StreamFlags, out IAudioSessionControl2 SessionControl);

[PreserveSig]
int GetSimpleAudioVolume(IntPtr AudioSessionGuid, uint StreamFlags, out IntPtr AudioVolume);

// IAudioSessionManager2
[PreserveSig]
int GetSessionEnumerator(out IAudioSessionEnumerator SessionEnum);

[PreserveSig]
int RegisterSessionNotification(IntPtr SessionNotification);

[PreserveSig]
int UnregisterSessionNotification(IntPtr SessionNotification);

[PreserveSig]
int RegisterDuckNotification([MarshalAs(UnmanagedType.LPWStr)] string sessionID, IntPtr duckNotification);

[PreserveSig]
int UnregisterDuckNotification(IntPtr duckNotification);
}

[Guid("E2F5BB11-0570-40CA-ACDD-3AA01277DEE8"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IAudioSessionEnumerator
{
[PreserveSig]
int GetCount(out int SessionCount);

[PreserveSig]
int GetSession(int SessionCount, out IAudioSessionControl2 Session);
}

public enum AudioSessionState
{
AudioSessionStateInactive = 0,
AudioSessionStateActive = 1,
AudioSessionStateExpired = 2,
}

[Guid("bfb7ff88-7239-4fc9-8fa2-07c950be9c6d"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public partial interface IAudioSessionControl2
{
// IAudioSessionControl
[PreserveSig]
int GetState(out AudioSessionState pRetVal);

[PreserveSig]
int GetDisplayName(out IntPtr pRetVal);

[PreserveSig]
int SetDisplayName([MarshalAs(UnmanagedType.LPWStr)] string Value, [MarshalAs(UnmanagedType.LPStruct)] Guid EventContext);

[PreserveSig]
int GetIconPath(out IntPtr pRetVal);

[PreserveSig]
int SetIconPath([MarshalAs(UnmanagedType.LPWStr)] string Value, [MarshalAs(UnmanagedType.LPStruct)] Guid EventContext);

[PreserveSig]
int GetGroupingParam(out Guid pRetVal);

[PreserveSig]
int SetGroupingParam([MarshalAs(UnmanagedType.LPStruct)] Guid Override, [MarshalAs(UnmanagedType.LPStruct)] Guid EventContext);

[PreserveSig]
int RegisterAudioSessionNotification(IntPtr NewNotifications);

[PreserveSig]
int UnregisterAudioSessionNotification(IntPtr NewNotifications);

// IAudioSessionControl2
[PreserveSig]
int GetSessionIdentifier(out IntPtr pRetVal);

[PreserveSig]
int GetSessionInstanceIdentifier(out IntPtr pRetVal);

[PreserveSig]
int GetProcessId(out int pRetVal);

[PreserveSig]
int IsSystemSoundsSession();

[PreserveSig]
int SetDuckingPreference(bool optOut);
}

关于c# - 在 vb.net 中运行基于声音 VU 级别的脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54498561/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com