好文档就是一把金锄头!
欢迎来到金锄头文库![会员中心]
电子文档交易市场
安卓APP | ios版本
电子文档交易市场
安卓APP | ios版本

用directx audio和directshow播放声音和音乐.doc

209页
  • 卖家[上传人]:mg****85
  • 文档编号:34175951
  • 上传时间:2018-02-21
  • 文档格式:DOC
  • 文档大小:1.99MB
  • / 209 举报 版权申诉 马上下载
  • 文本预览
  • 下载提示
  • 常见问题
    • 用 DirectX Audio 和 DirectShow 播放声音和音乐(1) 音乐就是一系列的音符,这些音符在不同的时间用不同的幅度被播放或者停止有非常多的指令被用来播放音乐,但是这些指令的操作基本相同,都在使用各种各样不同的音符在计算机上进行作曲,实际上是存储了很多组音乐,回放时由音频硬件将这些音符播放出来Midi 格式(文件扩展名是 .MID)是存储数字音乐的标准格式DirectMusic 音乐片段(music segments)使用.SGT 文件扩展名,其他的相关文件包括乐队文件(band file .BND),这种文件里面包含乐器信息;弦映射表文件(chordmaps file .CDM)包含在回放时修改音乐的和弦指令;样式文件(styles file .STY)包含回放样式信息;模板文件(templates file .TPL)包含创造音乐片段的模板Midi 是一种非常强大的音乐格式,惟一的不利因素是音乐品质依赖于音乐合成器的性能,因为Midi 仅仅记录了音符,其播放的品质由播放音乐的软硬件决定MP3 文件(文件后缀为.MP3)是一种类似于波表文件的文件格式,但是 MP3 文件和 WAV 文件最大的区别在于 MP3 文件将声音压缩到了最小的程度,但是音质却基本不变。

      可以用 DirectShow 组件播放 MP3 文件,DirectShow 组件是一个非常强大的多媒体组件,用 DirectShow 几乎可以播放任何媒体文件,包括声音和音频文件,部分声音文件我们只能用 DirectShow 播放 Direct Audio 是一个复合组件,它由 DirectSound 和 DirectMusic 两个组件组成,如下图所示: DirectMusic 在 DirectX8 中得到了巨大的增强,但是 DirectSound 基本保持原有的状态DirectSound 是主要的数字声音回放组件DirectMusic 处理所有的乐曲格式,包括 MIDI、DirectMusic 本地格式文件和波表文件DirectMusic 处理完之后将它们送入 DirectSound中做其他处理,这意味着回放 MIDI 的时候可以使用数字化的乐器使用 DirectSound使用时需要创建一个和声卡通讯的 COM 对象,用这个 COM 对象再创造一些独立的声音数据缓冲区(被称之为辅助音频缓冲区 secondary sound buffers)来存储音频数据缓冲区中的这些数据在主混音缓存(称之为主音频缓存 primary sound buffer)中被混合,然后可以用指定的任何格式播放出来。

      回放格式通过采样频率、声道数、采样精度排列,可能的采样频率有8000HZ, 11025HZ,22050HZ 和 44100HZ(CD 音质)对于声道数可以有两个选择:单通道的单声道声音和双通道的立体声声音采样精度被限制在两种选择上:8 位的低质量声音和 16 位的高保真声音在没有修改的情况下, DirectSound主缓冲区的默认设置是 22025HZ 采样率、8 位精度、立体声在 DirectSound 中可以调整声音的播放速度(这同样会改变声音的音调),调整音量、循环播放等甚至还可以在一个虚拟的 3D 环境中播放,以模拟一个实际环绕在周围的声音需要做的是将声音数据充满缓冲区,如果声音数据太大的话,必须创建流播放方法,加载声音数据中的一小块,当这一小块播放完毕以后,再加载另外的小块数据进缓冲区,一直持续这个过程,直到声音被处理完毕在缓冲区中调整播放位置可以实现流式音频,当播放完成通知应用程序更新音频数据这个通知更新的过程我们称之为“通告” 在同一时间被播放的缓存数目虽然没有限制,但是仍然需要保证缓冲区数目不要太多,因为每增加一个缓冲区,就要消耗很多内存和 CPU 资源在项目中使用 DirectSound 和 DirectMusic,需要添加头文件 dsound.h 和 dmsuic.h,并且需要链接 DSound.lib 到包含库中,添加 DXGuid.lib 库可以让 DirectSound 更容易使用。

      以下是 DirectSound COM 接口:IDirectSound8:DirectSound 接口IDirectSoundBuffer8:主缓冲区和辅助缓冲区接口,保存数据并控制回放IDirectSoundNotify8:通知对象,通知应用程序指定播放位置已经达到各个对象间的关系如下图所示:IDirectSound8 是主接口,用它来创建缓冲区(IDirectSoundBuffer8),然后用缓冲区接口创建通告接口(IDirectSoundNotify8),通告接口告诉应用程序指定的位置已经到达,通告接口在流化音频文件时非常有用初始化 DirectSound使用 DirectSound 的第一步是创建 IDirectSound8 对象, IDirectSound8 起到控制音频硬件设备的作用,可以通过 DirectSoundCreate8 函数来创建The DirectSoundCreate8 function creates and initializes an object that supports the IDirectSound8 interface. HRESULT DirectSoundCreate8(LPCGUID lpcGuidDevice,LPDIRECTSOUND8 * ppDS8,LPUNKNOWN pUnkOuter);ParameterslpcGuidDevice Address of the GUID that identifies the sound device. The value of this parameter must be one of the GUIDs returned by DirectSoundEnumerate, or NULL for the default device, or one of the following values. Value DescriptionDSDEVID_DefaultPlaybackSystem-wide default audio playback device. Equivalent to NULL.DSDEVID_DefaultVoicePlaybackDefault voice playback device.ppDS8 Address of a variable to receive an IDirectSound8 interface pointer. pUnkOuter Address of the controlling object's IUnknown interface for COM aggregation. Must be NULL, because aggregation is not supported. Return ValuesIf the function succeeds, it returns DS_OK. If it fails, the return value may be one of the following.Return CodeDSERR_ALLOCATEDDSERR_INVALIDPARAMDSERR_NOAGGREGATIONDSERR_NODRIVERDSERR_OUTOFMEMORYRemarksThe application must call the IDirectSound8::SetCooperativeLevel method immediately after creating a device object.创建主音频缓冲区用 IDirectSoundBuffer 对象控制主音频缓冲区,创建主缓冲区不需要 DirectX8 的接口,因为这个接口从来没有改变。

      用来创建音频缓冲区的函数是 IDirectSound8::CreateSoundBufferThe CreateSoundBuffer method creates a sound buffer object to manage audio samples.HRESULT CreateSoundBuffer(LPCDSBUFFERDESC pcDSBufferDesc,LPDIRECTSOUNDBUFFER * ppDSBuffer,LPUNKNOWN pUnkOuter);ParameterspcDSBufferDesc Address of a DSBUFFERDESC structure that describes the sound buffer to create. ppDSBuffer Address of a variable that receives the IDirectSoundBuffer interface of the new buffer object. Use QueryInterface to obtain IDirectSoundBuffer8. IDirectSoundBuffer8 is not available for the primary buffer. pUnkOuter Address of the controlling object's IUnknown interface for COM aggregation. Must be NULL. Return ValuesIf the method succeeds, the return value is DS_OK, or DS_NO_VIRTUALIZATION if a requested 3D algorithm was not available and stereo panning was substituted. See the description of the guid3DAlgorithm member of DSBUFFERDESC. If the method fails, the return value may be one of the error values shown in the following table.Return codeDSERR_ALLOCATEDDSERR_BADFORMAT DSERR_BUFFERTOOSMALLDSERR_CONTROLUNAVAILDSERR_DS8_REQUIREDDSERR_INVALIDCALLDSERR_INVALIDPARAMDSERR_NOAGGREGATIONDSERR_OUTOFMEMORYDSERR_UNINITIALIZEDDSERR_UNSUPPORTEDRemarksDirectSound does not initialize the contents of the buffer, and the application cannot assume that it contains silence.If an attempt is made to create a buffer with the DSBCAPS_LOCHARDWARE flag on a system where hardware acceleration is not available, the method fails with either DSERR_CONTROLUNAVAIL or DSERR_INVALIDCALL, depending on the operating system.pcDSBufferDesc 是一个指向 DSBUFFERDESC 结构的指针,保存所创建的缓冲区的信息。

      点击阅读更多内容
      关于金锄头网 - 版权申诉 - 免责声明 - 诚邀英才 - 联系我们
      手机版 | 川公网安备 51140202000112号 | 经营许可证(蜀ICP备13022795号)
      ©2008-2016 by Sichuan Goldhoe Inc. All Rights Reserved.