文档章节

在 Delphi 下使用 DirectSound (2): 查看设备性能

涂孟超
 涂孟超
发布于 2014/09/26 15:37
字数 1101
阅读 13
收藏 0

使用 DirectSound 前应先建立 IDirectSound8 对象(之前的版本是 IDirectSound):
function DirectSoundCreate8(
  pcGuidDevice: PGUID;      //设备的 GUID; 指定 nil 表示使用默认声卡
  out ppDS8: IDirectSound8; //要返回的 IDirectSound8 对象
  pUnkOuter: IUnknown       //未使用, 应为 nil
): HResult; stdcall; //返回错误码

//也可使用 GUID 常数指定设备:
DSDEVID_DefaultPlayback      //默认设备
DSDEVID_DefaultVoicePlayback //二级默认设备


然后通过 IDirectSound8 对象(它代表着一个播放设备)创建缓冲区, 以写入声音信息.

这里的缓冲区分 "主缓冲区" 和 "次缓冲区". 主缓冲区只有一个; 次缓冲区可以有多个, 以实现多个声音的同时播放.

DirectSound 会尽量使用硬件(声卡)提供的缓冲区; 如果硬件不支持则只好使用系统内存做缓冲区, 但这样效率不及前者.

通过 IDirectSound8.GetCaps() 方法可以获取指定设备的详细性能:
function GetCaps(
  out pDSCaps: TDSCaps //获取到的信息将放在 TDSCaps 结构体中
): HResult; stdcall;

//TDSCaps 结构:
TDSCaps = packed record
  dwSize                         : DWORD; //结构大小(字节)
  dwFlags                        : DWORD; //性能标志
  dwMinSecondarySampleRate       : DWORD; //次缓冲区的最小取样率
  dwMaxSecondarySampleRate       : DWORD; //次缓冲区的最大取样率
  dwPrimaryBuffers               : DWORD; //主缓冲区个数
  dwMaxHwMixingAllBuffers        : DWORD; //硬件缓冲区的最大数目
  dwMaxHwMixingStaticBuffers     : DWORD; //静态硬件缓冲区的最大数目
  dwMaxHwMixingStreamingBuffers  : DWORD; //流式硬件缓冲区的最大数目
  dwFreeHwMixingAllBuffers       : DWORD; //空闲的硬件缓冲区个数
  dwFreeHwMixingStaticBuffers    : DWORD; //空闲的静态硬件缓冲区个数
  dwFreeHwMixingStreamingBuffers : DWORD; //空闲的流式硬件缓冲区个数
  dwMaxHw3DAllBuffers            : DWORD; //3D 硬件缓冲区的最大数目
  dwMaxHw3DStaticBuffers         : DWORD; //3D 静态硬件缓冲区的最大数目
  dwMaxHw3DStreamingBuffers      : DWORD; //3D 流式硬件缓冲区的最大数目
  dwFreeHw3DAllBuffers           : DWORD; //空闲的 3D 硬件缓冲区的最大数目
  dwFreeHw3DStaticBuffers        : DWORD; //空闲的 3D 静态硬件缓冲区的最大数目
  dwFreeHw3DStreamingBuffers     : DWORD; //空闲的 3D 流式硬件缓冲区的最大数目
  dwTotalHwMemBytes              : DWORD; //硬件内存总量(字节)
  dwFreeHwMemBytes               : DWORD; //空闲的硬件内存总量(字节)
  dwMaxContigFreeHwMemBytes      : DWORD; //空闲的硬件内存最大连续块大小(字节)
  dwUnlockTransferRateHwBuffers  : DWORD; //硬件缓冲区的数据传输速率(千字节/秒)
  dwPlayCpuOverheadSwBuffers     : DWORD; //混合软件缓冲区占用的 CPU 周期的百分比
  dwReserved1                    : DWORD; //未使用
  dwReserved2                    : DWORD; //未使用
end;


查看默认设备性能的测试程序:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Memo1: TMemo; //用于显示信息
    procedure FormCreate(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses DirectSound;

procedure TForm1.FormCreate(Sender: TObject);
var
  myDSound: IDirectSound8;
  capInfo: TDSCaps;
  p: PDSCaps;
begin
  Memo1.ScrollBars := ssBoth;
  Memo1.Align := alLeft;
  Memo1.Clear;

  FillChar(capInfo, SizeOf(TDSCaps), 0);
  capInfo.dwSize := SizeOf(TDSCaps);

  DirectSoundCreate8(nil, myDSound, nil);
  myDSound.GetCaps(capInfo);

  with Memo1.Lines do begin
    Add(Format('%d'#9'dwSize',                         [capInfo.dwSize                        ]));
    Add(Format('%d'#9'dwFlags',                        [capInfo.dwFlags                       ]));
    Add(Format('%d'#9'dwMinSecondarySampleRate',       [capInfo.dwMinSecondarySampleRate      ]));
    Add(Format('%d'#9'dwMaxSecondarySampleRate',       [capInfo.dwMaxSecondarySampleRate      ]));
    Add(Format('%d'#9'dwPrimaryBuffers',               [capInfo.dwPrimaryBuffers              ]));
    Add(Format('%d'#9'dwMaxHwMixingAllBuffers',        [capInfo.dwMaxHwMixingAllBuffers       ]));
    Add(Format('%d'#9'dwMaxHwMixingStaticBuffers',     [capInfo.dwMaxHwMixingStaticBuffers    ]));
    Add(Format('%d'#9'dwMaxHwMixingStreamingBuffers',  [capInfo.dwMaxHwMixingStreamingBuffers ]));
    Add(Format('%d'#9'dwFreeHwMixingAllBuffers',       [capInfo.dwFreeHwMixingAllBuffers      ]));
    Add(Format('%d'#9'dwFreeHwMixingStaticBuffers',    [capInfo.dwFreeHwMixingStaticBuffers   ]));
    Add(Format('%d'#9'dwFreeHwMixingStreamingBuffers', [capInfo.dwFreeHwMixingStreamingBuffers]));
    Add(Format('%d'#9'dwMaxHw3DAllBuffers',            [capInfo.dwMaxHw3DAllBuffers           ]));
    Add(Format('%d'#9'dwMaxHw3DStaticBuffers',         [capInfo.dwMaxHw3DStaticBuffers        ]));
    Add(Format('%d'#9'dwMaxHw3DStreamingBuffers',      [capInfo.dwMaxHw3DStreamingBuffers     ]));
    Add(Format('%d'#9'dwFreeHw3DAllBuffers',           [capInfo.dwFreeHw3DAllBuffers          ]));
    Add(Format('%d'#9'dwFreeHw3DStaticBuffers',        [capInfo.dwFreeHw3DStaticBuffers       ]));
    Add(Format('%d'#9'dwFreeHw3DStreamingBuffers',     [capInfo.dwFreeHw3DStreamingBuffers    ]));
    Add(Format('%d'#9'dwTotalHwMemBytes',              [capInfo.dwTotalHwMemBytes             ]));
    Add(Format('%d'#9'dwFreeHwMemBytes',               [capInfo.dwFreeHwMemBytes              ]));
    Add(Format('%d'#9'dwMaxContigFreeHwMemBytes',      [capInfo.dwMaxContigFreeHwMemBytes     ]));
    Add(Format('%d'#9'dwUnlockTransferRateHwBuffers',  [capInfo.dwUnlockTransferRateHwBuffers ]));
    Add(Format('%d'#9'dwPlayCpuOverheadSwBuffers',     [capInfo.dwPlayCpuOverheadSwBuffers    ]));
  end;
end;

end.


查看所有设备性能的测试程序:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    ComboBox1: TComboBox; //用于设备列表
    procedure FormCreate(Sender: TObject);
    procedure ComboBox1Change(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses DirectSound;

var
  guidArr: array of TGUID;

function EnumCallback(lpGuid: PGUID; lpcstrDescription, lpcstrModule: PChar;
    lpContext: Pointer): BOOL; stdcall;
begin
  if lpGuid <> nil then
  begin
    TStrings(lpContext).Add(lpcstrDescription);
    SetLength(guidArr, Length(guidArr) + 1);
    guidArr[Length(guidArr) - 1] := lpGuid^;
  end;
  Result := True;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  ComboBox1.Align := alTop;
  Memo1.ScrollBars := ssBoth;
  Memo1.Align := alClient;
  Memo1.Clear;
  DirectSoundEnumerate(EnumCallback, ComboBox1.Items);
  ComboBox1.ItemIndex := 0;
  ComboBox1.OnChange(nil);
end;

procedure TForm1.ComboBox1Change(Sender: TObject);
var
  myDSound: IDirectSound8;
  capInfo: TDSCaps;
  p: PDSCaps;
begin
  Memo1.Clear;
  ZeroMemory(@capInfo, SizeOf(TDSCaps));
  capInfo.dwSize := SizeOf(TDSCaps);

  DirectSoundCreate8(@guidArr[ComboBox1.ItemIndex], myDSound, nil);
  Caption := GUIDToString(guidArr[ComboBox1.ItemIndex]);
  myDSound.GetCaps(capInfo);

  with Memo1.Lines do begin
    Add(Format('%d'#9'dwSize',                         [capInfo.dwSize                        ]));
    Add(Format('%d'#9'dwFlags',                        [capInfo.dwFlags                       ]));
    Add(Format('%d'#9'dwMinSecondarySampleRate',       [capInfo.dwMinSecondarySampleRate      ]));
    Add(Format('%d'#9'dwMaxSecondarySampleRate',       [capInfo.dwMaxSecondarySampleRate      ]));
    Add(Format('%d'#9'dwPrimaryBuffers',               [capInfo.dwPrimaryBuffers              ]));
    Add(Format('%d'#9'dwMaxHwMixingAllBuffers',        [capInfo.dwMaxHwMixingAllBuffers       ]));
    Add(Format('%d'#9'dwMaxHwMixingStaticBuffers',     [capInfo.dwMaxHwMixingStaticBuffers    ]));
    Add(Format('%d'#9'dwMaxHwMixingStreamingBuffers',  [capInfo.dwMaxHwMixingStreamingBuffers ]));
    Add(Format('%d'#9'dwFreeHwMixingAllBuffers',       [capInfo.dwFreeHwMixingAllBuffers      ]));
    Add(Format('%d'#9'dwFreeHwMixingStaticBuffers',    [capInfo.dwFreeHwMixingStaticBuffers   ]));
    Add(Format('%d'#9'dwFreeHwMixingStreamingBuffers', [capInfo.dwFreeHwMixingStreamingBuffers]));
    Add(Format('%d'#9'dwMaxHw3DAllBuffers',            [capInfo.dwMaxHw3DAllBuffers           ]));
    Add(Format('%d'#9'dwMaxHw3DStaticBuffers',         [capInfo.dwMaxHw3DStaticBuffers        ]));
    Add(Format('%d'#9'dwMaxHw3DStreamingBuffers',      [capInfo.dwMaxHw3DStreamingBuffers     ]));
    Add(Format('%d'#9'dwFreeHw3DAllBuffers',           [capInfo.dwFreeHw3DAllBuffers          ]));
    Add(Format('%d'#9'dwFreeHw3DStaticBuffers',        [capInfo.dwFreeHw3DStaticBuffers       ]));
    Add(Format('%d'#9'dwFreeHw3DStreamingBuffers',     [capInfo.dwFreeHw3DStreamingBuffers    ]));
    Add(Format('%d'#9'dwTotalHwMemBytes',              [capInfo.dwTotalHwMemBytes             ]));
    Add(Format('%d'#9'dwFreeHwMemBytes',               [capInfo.dwFreeHwMemBytes              ]));
    Add(Format('%d'#9'dwMaxContigFreeHwMemBytes',      [capInfo.dwMaxContigFreeHwMemBytes     ]));
    Add(Format('%d'#9'dwUnlockTransferRateHwBuffers',  [capInfo.dwUnlockTransferRateHwBuffers ]));
    Add(Format('%d'#9'dwPlayCpuOverheadSwBuffers',     [capInfo.dwPlayCpuOverheadSwBuffers    ]));
  end;
end;

end.

本文转载自:http://www.cnblogs.com/del/archive/2011/01/13/1934863.html

共有 人打赏支持
涂孟超
粉丝 12
博文 2011
码字总数 14107
作品 0
深圳
程序员
DirectSound入门

文档内容: (费话篇) 关于DirectSound DirectSound是DirectX API的音频(waveaudio)组件之一,它可以提供快速的混音、硬件加速功能,并且可以直接访问相关设备,当然,最主要的是它提供的...

长平狐
2012/08/13
824
0
C#中使用DirectX编程

我感觉声音的播放比较简单。我们从播放声音开始。为什么我这么觉得?我也不知道。 这里是展示最最最最最简单的DirectX播放声音的例子,我尽量省略了无关的代码。最后的代码只有19行,够简单了...

晨曦之光
2012/03/09
150
0
Windows上的音频采集技术(转)

转自:http://shanewfx.github.io/blog/2013/08/14/caprure-audio-on-windows/前一段时间接到一个任务,需要采集到声卡的输出信号,以便与麦克风的输入信号进行混音。 之前一直没有研究过音频...

元谷
2013/12/05
0
0
使用UniDAC在Delphi XE4中开发iOS应用

使用UniDAC在Delphi XE4中开发iOS应用   核心提示:Delphi XE4的发布让很多Delphi开发者欣喜若狂,毫无疑问它使得Delphi开发更简单且更容易被理解,而且让开发无论是iPhone、iPad还是iPod的...

vga
2014/06/01
0
0
SDL2源代码分析1:初始化(SDL_Init())

===================================================== SDL源代码分析系列文章列表: SDL2源代码分析1:初始化(SDLInit()) SDL2源代码分析2:窗口(SDLWindow) SDL2源代码分析3:渲染器...

leixiaohua1020
2014/11/01
0
0

没有更多内容

加载失败,请刷新页面

加载更多

下一页

python3.6 取整除法

python3.6 中取整除法运算逻辑如下: d 非零,那么商 q 满足这样的关系: a = qd + r ,且0 ≤ r n1=7//3#7 = 3*2 +1n2=-6.1//3#-7 = 3*(-3)+2'{},{}'.format(n1,n2) 从运行结果可以...

colinux
31分钟前
3
0
阶段总结——用虚拟机搭建一个高可用负载均衡集群架构

[toc] linux基本知识已经介绍完,现有一个业务需要操作,通过对这个项目的操作,可以复习、总结、巩固之前的知识点; ** 用13台虚拟机搭建一个高可用负载均衡集群架构出来,并运行三个站点,...

feng-01
35分钟前
0
0
mysql 设置utf8字符集 (CentOS)

1.查看数据库及mysql应用目前使用的编码方式 (1)链接mysql 客户端 (2)执行:status 结果: 2.修改mysql 应用的字符编码(server characterset ) (1)打开配置文件:vim /etc/mysql/my...

qimh
35分钟前
0
0
windows无法格式化u盘解决方法

1。点开始-运行-输入cmd-format f: /fs: fat32 (这里f:是指U盘所在盘符) 这个格式化会很慢 请耐心等待

大灰狼wow
46分钟前
0
0
MySql 8.0连接失败

原来,MySql 8.0.11 换了新的身份验证插件(caching_sha2_password), 原来的身份验证插件为(mysql_native_password)。而客户端工具Navicat Premium12 中找不到新的身份验证插件(caching_s...

放飞E梦想O
今天
1
0

没有更多内容

加载失败,请刷新页面

加载更多

下一页

返回顶部
顶部