DirectX 9 - 体验 DirectSound






4.87/5 (27投票s)
2003年12月1日
3分钟阅读

123231

1863
使用 DirectSound 创建一个环境音。
引言
没有什么比一边听着你最喜欢的音乐一边编写代码更美妙的了,但有时候这会有点让人分心。这个应用程序非常适合创建一种声音环境来封装你的情绪。有客人要来吗?打开你的虚拟夏威夷,他们会喜欢的。创建一个声音环境再简单不过了;添加你自己的声音文件,控制每个声音的出现频率、音量和平移。准备好切换到另一个环境了吗?保存你当前的播放列表,并加载你创建的任何其他现有播放列表。我能够创建的一些环境包括:田野和溪流、海滨宝塔、午夜雷暴、纽约市中心、农场动物(我不知道为什么)、地牢爬行和 NASCAR 一日游。尽情享受吧!
背景
最近,我开始使用新的 DirectX 9 开发一款儿童 RTS 游戏,并再次面临大多数新的 Microsoft 技术缺乏文档这一不健康的问题。收集足够关于 DirectSound 的信息以使应用程序工作非常困难,但我做到了,所以这是我入门的项目。我最初的想法来自一个名为“Environ”的旧应用程序,或者类似的,它非常酷,但严重缺乏可扩展性。虽然我没有深入研究 3D 缓冲区控制功能,但我确实涵盖了基础知识。我希望你们中的大多数人都能在这里找到你们想要的东西。
代码审查
虽然我只会回顾 DirectSound 的功能,但该项目有很好的注释,你应该能够毫无问题地弄清楚正在发生什么以及如何扩展它;或者将它拆开。
以下是关于应用程序的类和一般流程的简要说明。
ApplicationManager
- 一个管理器类,包含应用程序的主入口点,并充当用户交互和应用程序方法之间的中介。Interface
- 一个视图类,处理用户交互和呈现AmbientSounds
控件的集合。AmbientSounds
- 一个视图/模态类,描述单个声音的结构,并处理用户交互。
让我们开始吧。(从 AmbientSounds
中提取 DirectSound 并将其放入您自己的代码中的步骤。)
您需要在 ApplicationManager
类中声明主要的 DirectSound Device
。
// Create a new DirectSound manager.
private Microsoft.DirectX.DirectSound.Device m_SoundDevice;
Device
在使用前需要初始化,这在 ApplicationManager.Initialize()
中完成。在我们初始化 Device
之后,我们需要将其分配给一个控件并设置其 CooperativeLevel
。因为 DirectSound 的 DirectX 驱动程序可能被同时运行的不同应用程序使用,这是不可避免的。关于 CooperativeLevels 的更多信息。
// Instantiate the DirectSound manager.
m_SoundDevice = new Device();
m_SoundDevice.SetCooperativeLevel(m_Interface, CooperativeLevel.Priority);
现在我们的 Device 准备好 rock
了,我们需要创建一个声音缓冲区来保存我们的 wav 文件。我们将跳转到 AmbientSound
控件类。修改缓冲区设置是通过 BufferDescription
完成的,并将其传递到缓冲区实例化中。
// Holds a DirectSound SecondaryBuffer.
private Microsoft.DirectX.DirectSound.Buffer m_SoundBuffer;
// Describe the capabilities of this buffer.
BufferDescription bufferDescription = new BufferDescription();
bufferDescription.ControlVolume = true; // Let's us control the volume.
bufferDescription.ControlPan = true; // Let's us control the panning.
bufferDescription.ControlFrequency = true; // Let's us control the frequency.
// Tells the primary buffer to continue
// play this buffer, even if the app loses focus.
bufferDescription.GlobalFocus = true;
// Pass in the path to the wav file, the buffer description,
// and the Device we created above.
m_SoundBuffer = new SecondaryBuffer(m_FilePath,
bufferDescription, m_ApplicationManager.SoundDevice);
现在我们有了 Device
和缓冲区设置,播放 wav 非常简单。您可以使用 BufferPlayFlags
中的几个选项:Default
,它播放 wav 一次并结束;或者 Looping
,它会不断地重复播放 wav 文件。
m_SoundBuffer.Play(0, BufferPlayFlags.Default);
//or
m_SoundBuffer.Play(0, BufferPlayFlags.Looping);
要停止在 Default
模式或 Looping
模式下播放 wav,只需调用 Stop()
。
m_SoundBuffer.Stop();
更改音量和平移也非常简单。
// int between 0, as the loudest, and -3000 as silent.
// It will except smaller, but I couldn't here anything at this point.
m_SoundBuffer.Volume = value;
// int between -1000, as all left, and 1000 as all right,
// with 0 being in the middle.
m_SoundBuffer.Pan = value;
就是这样!
扩展应用程序
显然,我已经介绍了 DirectSound 最基本的部分。Microsoft 在屏蔽我们复杂的代码以便启动并运行某些东西方面做得很好,同时仍然通过主缓冲区和 3D 控件的使用提供了一些更高级的实现。
我认为一些功能可以很好地补充该应用程序...
- 能够将播放列表保存为声音包;包括所有使用的声音文件和播放列表文件。这将允许共享环境。
- 能够加载 MP3 声音文件。
请随时与我联系并告诉我进展如何。访问 Southern Oregon .NET Group。