c# - 如何将直接声音添加到 FilterGraph 2?

标签 c# com directshow naudio directshow.net

我已经用谷歌搜索/StackOverflowing 好几天了,但无法弄明白。

我有一个 WPF 项目,我想在其中使用单独的音频系统同时在多个屏幕上播放视频。对于播放视频,我们使用 WPFMediaKit ,它依赖于 DirectShow Lib .NET .对于其他系统,我们想使用更现代的东西,例如通过 NAudio 提供的 Core Audio API。 .我们控制环境,只需要支持 Windows 7(将来可能会迁移到 8)。

WPFMediaKit 的 MediaUriElement 允许您设置它的 AudioRenderer 但由于 DirectSound 施加的 31 个字符限制,它会出现问题?我不完全确定限制从何而来,但这是一个问题,因为我们所有的音频设备都有非常相似的名称(很可能在 31 个字符的空间内不会是唯一的)。因此,计划是修改源并允许它接收表示 DirectSound 设备的 GUID 并更改其行为以通过它查找它。向用户显示音频设备端点列表的代码将依赖于核心音频 API 中的 NAudio 的 MMDeviceEnumerator

我已经走到这一步了:

private static void DemonstrateTheIssue() {
    SpeechSynthesizer speech = new SpeechSynthesizer();
    MemoryStream stream = new MemoryStream();
    SpeechAudioFormatInfo synthFormat = new SpeechAudioFormatInfo(EncodingFormat.Pcm, 88200, 16, 1, 16000, 2, null);
    speech.SetOutputToAudioStream(stream, synthFormat);
    speech.Speak("This is a test. This is only a test.");
    stream.Position = 0;
    RawSourceWaveStream reader = new RawSourceWaveStream(stream, new WaveFormat());

    MMDeviceEnumerator coreAudioDeviceEnumerator = new MMDeviceEnumerator();
    MMDeviceCollection coreAudioAudioEndPoints = coreAudioDeviceEnumerator.EnumerateAudioEndPoints(DataFlow.Render, DeviceState.Active);
    MMDevice coreAudioSpdif = coreAudioAudioEndPoints.First(ep => ep.FriendlyName.Contains("S/PDIF")); // just an example. In the real application, would be selected by user and the GUID would be stored instead of the FriendlyName
    string spdifGuidValue = coreAudioSpdif.Properties[PropertyKeys.PKEY_AudioEndpoint_GUID].Value as string;
    Guid spdifGuid = Guid.Parse(spdifGuidValue);

    DirectSoundOut directSoundSpdif = new DirectSoundOut(spdifGuid);
    directSoundSpdif.Init(reader);
    directSoundSpdif.Play();
}

此代码有效。当我检查 NAudio 的 DirectSoundOut 源时,我可以看到它从 dsound.dll 调用 DirectSoundOut.DirectSoundCreate,它初始化一个 IDirectSound 实例。我不完全确定我能用那个实例做什么。那么,回到这个谜题的另一面......

让我们看看当我给它一个 "Digital Audio (S/PDIF) (High De")AudioRenderer 时 WPFMediaKit 做了什么。通过挖掘源代码,我最终遇到 AddFilterByName(IGraphBuilder graphBuilder, Guid deviceCategory, string friendlyName) 最终调用 AddFilterByDevice(IGraphBuilder graphBuilder, DsDevice device)。好的,所以它使用了自己的包装类,DsDevice... 它包装了 IMoniker 和其他一些东西,包括附加到 IMonikerIPropertyBag 中的一些属性实例。在我的踪迹消失在 COM 中之前,我看到的最后一行 C# 代码调用了这个:filterGraph.AddSourceFilterForMoniker(device.Mon, null, device.Name, out filter)filterGraph 是一个 IFilterGraph2 实例,device.Name 从名字对象的属性包中返回 "FriendlyName"

我觉得我很接近,但我就是无法将这些点联系起来。我如何获取一个 IDirectSound 实例并告诉 filterGraph 在其上播放音频?我需要为 IDirectSound 取一个名字吗?如果是这样,如何?还有其他我不知道的方法吗?或者...我可以创建/编写/获取对我自己的过滤器的引用并将其告知 filterGraph 吗?如果是这样,我该怎么做?

请像我还是个 child 一样回答这个问题——这些东西真的让我难以理解。

最佳答案

DirectSound 和 DirectShow 是两个不同的 API。没有直接将 DirectSound 对象添加到 DirectShow 过滤器图中,因此您无法做到不可能。

有两种方法可以解决这个问题:您要么坚持您已经拥有一个IDirectSound 接口(interface)指针,要么您想要在DirectShow 中使用它。在这种情况下,您必须实现一个自定义音频渲染器过滤器,它将由这个 DirectSound 接口(interface)支持,并且您的渲染器会将所有音频转发到您感兴趣的设备。你真的不想走这条路。特别是,您将无法仅在 C# 中实现它。

第二种方法是找到一个现有的 DirectShow 音频渲染器,它正在使用您感兴趣的设备。然后使用它,它将通过相同的 DirectSound 设备播放,只是与您最初手上的指针不同。

关于第二种方法的更多细节。 DirectShow 有一个特殊类别,其中列出了所有音频渲染器:CLSID_AudioRendererCategory .在那里枚举过滤器,你枚举 DirectSound Renderer Filter 的实例为每个现有设备创建。

This filter acts as a wrapper for an audio device. To enumerate the audio devices available on the user's system, use the ICreateDevEnum interface with the audio renderer category (CLSID_AudioRendererCategory). For each audio device, the audio renderer category contains two filter instances. One of these corresponds to the DirectSound Renderer, and the other corresponds to the Audio Renderer (WaveOut) filter. The DirectSound instance has the friendly name "DirectSound: DeviceName," where DeviceName is the name of the device. The WaveOut instance has the friendly name DeviceName.

您可以在那里找到合适的设备,然后您可以将此过滤器用作常规音频渲染器(也就是说,您将它添加到您的图形中,在那里连接它)。但是,过滤器不会公开它在内部使用的 IDirectSound 指针。

关于c# - 如何将直接声音添加到 FilterGraph 2?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20554182/

相关文章:

c# - 现代 UI 如何从另一个链接转到另一个页面

c# - 如何在 C# 中从 aspx.cs 页面设置多行按钮文本?

c# - ComAddin.Object.SomeMethod 在一个项目中有效,但在另一个项目中无效

directshow - 是否有任何现代 Windows PC 中没有至少一个 DirectShow 捕获设备?

directshow - 如何唯一标识 DirectShow 音频渲染器过滤器?

c++ - YUV -> RGB 转换可以硬件加速吗?

c# - NVP 请求 - CreateRecurringPaymentsProfile

c# - 广域网通讯

c++ - ATL COM 类的单个实例

c++ - 是否有用于 Shell 扩展的点击处理程序