前面我们说到了如何用Visual Studio编译portAudio,下面我们开始讲下如何使用PortAudio,先从最简单的开始,这篇文章我们主要讲如何枚举音频相关设备。代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
#include <iostream> #include <string> #include <vector> using namespace std; #include "portaudio.h" #pragma comment(lib,"portaudio_x86.lib") int main() { int i, iNumDevices, defaultDisplayed; const PaDeviceInfo *deviceInfo; //PaStreamParameters inputParameters, outputParameters; PaError err; //初始化 err = Pa_Initialize(); if( err != paNoError ) { cout<<"Pa_Initialize error\r\n"; } //获得设备数量 iNumDevices = Pa_GetDeviceCount(); if (iNumDevices < 0) { cout<<"Get device count error\r\n"; } for (i = 0;i<iNumDevices;i++) { deviceInfo = Pa_GetDeviceInfo(i); cout<<deviceInfo->name<<endl; //打印设备名 } getchar(); return 0; } |
如下是deviceinfo对应结构体声明:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
/** A structure providing information and capabilities of PortAudio devices. Devices may support input, output or both input and output. */ typedef struct PaDeviceInfo { int structVersion; /* this is struct version 2 */ const char *name; PaHostApiIndex hostApi; /**< note this is a host API index, not a type id*/ int maxInputChannels; int maxOutputChannels; /** Default latency values for interactive performance. */ PaTime defaultLowInputLatency; PaTime defaultLowOutputLatency; /** Default latency values for robust non-interactive applications (eg. playing sound files). */ PaTime defaultHighInputLatency; PaTime defaultHighOutputLatency; double defaultSampleRate; } PaDeviceInfo; |
编译运行得到如下结果:
在最后几行打印的结果中出现的一些设备名汉字乱码是因为有些设备名以UTF-8编码,我没做转换,转化后即可正常显示。
文章评论