Port Audio:如何解决在PortAudio(Windows)中查找默认设备的错误?

时间:2016-03-13 07:01:42

标签: c portaudio

我想运行一个生成和播放正弦波的PortAudio测试程序。为此我已经包含了所有必需的头文件。事实上,程序编译得很好,但是当我运行它时,在控制台中显示“找不到默认设备”。 所以,任何人都可以给我解决方案。

操作系统:Windows 8.1

编码语言:C

以下是该计划: -

typedef struct
{
    float sine[TABLE_SIZE];
    int left_phase;
    int right_phase;
    char message[20];
}
paTestData;

static int patestCallback(...)//Callback Function
{....
}

static void StreamFinished( void* userData )
{
    paTestData *data = (paTestData *) userData;
    printf( "Stream Completed: %s\n", data->message );
}


int main(void)//main function
{
    PaStreamParameters outputParameters;
    PaStream *stream;
    PaError err;
    paTestData data;

    int i;

    printf("PortAudio Test: output sine wave. SR = %d, BufSize = %d\n",       SAMPLE_RATE, FRAMES_PER_BUFFER);

    /* initialise sinusoidal wavetable */

    for( i=0; i<TABLE_SIZE; i++ )
    {
        data.sine[i] = (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. );
    }

    data.left_phase = data.right_phase = 0;

    err = Pa_Initialize();

    if( err != paNoError )
    {
        printf("Error in Initialize:-",err);
        goto error;
    }

    outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output     device */

    if (outputParameters.device == paNoDevice)
    {
        fprintf(stderr,"Error: No default output device.\n");
        goto error;
    }

    outputParameters.channelCount = 2;       /* stereo output */
    outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */
    outputParameters.suggestedLatency = Pa_GetDeviceInfo(      outputParameters.device )->defaultLowOutputLatency;
    outputParameters.hostApiSpecificStreamInfo = NULL;

    err = Pa_OpenStream
    (
       &stream,
       NULL, /* no input */
       &outputParameters,
       SAMPLE_RATE,
       FRAMES_PER_BUFFER,
       paClipOff,      /* we won't output out of range samples so don't bother clipping them */
       patestCallback,
       &data
    );

    if( err != paNoError )
        goto error;

    sprintf( data.message, "No Message" );
    err = Pa_SetStreamFinishedCallback( stream, &StreamFinished );

    if( err != paNoError )
        goto error;

    err = Pa_StartStream( stream );

    if( err != paNoError )
        goto error;

    printf("Play for %d seconds.\n", NUM_SECONDS );
    Pa_Sleep( NUM_SECONDS * 1000 );

    err = Pa_StopStream( stream );

    if( err != paNoError )
        goto error;

    err = Pa_CloseStream( stream );

    if( err != paNoError )
        goto error;

    Pa_Terminate();
    printf("Test finished.\n");

    return err;

    error:
        Pa_Terminate();

    fprintf( stderr, "An error occured while using the portaudio stream\n" );
    fprintf( stderr, "Error number: %d\n", err );
    fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );

    return err;
}

在编译和运行后,我在控制台中得到以下输出: -

PortAudio Test: output sine wave. SR = 44100, BufSize = 64
Error: No default output device.
An error occured while using the portaudio stream
Error number: 0
Error message: Success

Process returned 0 (0x0)   execution time : 0.016 s
Press any key to continue.

任何人都可以解决问题。

1 个答案:

答案 0 :(得分:3)

首先,检查PortAudio是否检测到任何设备。您可以通过将调用结果打印到Pa_GetDeviceCount()来执行此操作,也可以编译并运行examples/pa_devs.cpa_devs将为您提供PortAudio检测到的设备列表。如果列表非空,您可以尝试手动将设备索引替换为outputParameters.device

但是,如果没有可用的默认设备,我怀疑您会发现根本没有注册设备。在这种情况下,最可能的原因是主机API未编译。您没有说明如何编译PortAudio,但通常在从源代码编译它时,您需要通过定义预处理器符号来指定要构建的主机API as PA_USE_WMMEPA_USE_WASAPI(例如,对编译器使用-D标志)。这里有supported Windows preprocessor defines的完整列表。

文档中有各种工具链detailed build instructions