PulseAudio:sound 已录制但播放烦人的声音

PulseAudio:sound recorded but plays annoying sound

我是 PulseAudio 的新手。我正在尝试制作简单的程序。一个人会录制声音并将其保存在普通文件中,另一个人应该打开它并播放。这是我的录音代码:

#ifdef HAVE_CONFIG_H
   #include <config.h>
#endif

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <pulse/simple.h>
#include <pulse/error.h>

#define BUFSIZE 32

int main(int argc, char*argv[])
{

     /* The Sample format to use */
    static const pa_sample_spec ss = {
               .format = PA_SAMPLE_S16LE, //16bit iqneba agwerili tito sample
               .rate = 44100, //number of samples played in each second
               .channels = 2
            };

    pa_simple *s_in = NULL;
    int ret = 1;
    int error;
    int siri =0;


    //file info
    FILE* pFile;
    char* yourFilePath  = "xma.bin";
    pFile = fopen(yourFilePath,"wb");





    if (!(s_in = pa_simple_new(NULL, argv[0], PA_STREAM_RECORD, NULL, "record", &ss, NULL, NULL, &error)))
    {
        fprintf(stderr, __FILE__": pa_simple_new() failed: %s\n", pa_strerror(error));
        goto finish;
    }

    for (;siri<10000;siri+=1) 
    {
        uint8_t buf[BUFSIZE];
        ssize_t r;

        int   yorBufferSize = strlen(buf) + 1;


        /* Write your buffer to disk. */


        if (pa_simple_read(s_in, buf, sizeof(buf), &error) < 0) 
        {

            fprintf(stderr, __FILE__": read() failed: %s\n", strerror(errno));
            goto finish;
        }



       if (pFile)
       {
            fwrite(buf, yorBufferSize, 1, pFile);
            puts("Wrote to file!");
       }
       else
       {
            puts("Something wrong writing to File.");
       }


    }


    ret = 0;

    finish:

    if (s_in)
        pa_simple_free(s_in);


    return ret;
    fclose(pFile);
}

这是我的录音程序:

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <pulse/simple.h>
#include <pulse/error.h>

#define BUFSIZE 32

int main(int argc, char*argv[]) 
{

    /* The Sample format to use */
    static const pa_sample_spec ss = {
        .format = PA_SAMPLE_S16LE, //16bit iqneba agwerili tito sample
        .rate = 44100, //number of samples played in each second
        .channels = 2
    };

    pa_simple *s_out = NULL;
    int ret = 1;
    int error;


    //file info
    FILE* pFile;
    char* yourFilePath  = "xma.bin";
    pFile = fopen(yourFilePath, "rb");



     /* Create a new playback stream */
     if (!(s_out = pa_simple_new(NULL, argv[0], PA_STREAM_PLAYBACK, NULL, "playback", &ss, NULL, NULL, &error))) 
     {
         fprintf(stderr, __FILE__": pa_simple_new() failed: %s\n", pa_strerror(error));
         goto finish;
     }

    for (;;) 
    {
        uint8_t buf[BUFSIZE];
        fread(buf, sizeof(buf), 1, pFile);
        ssize_t r;

        if(feof(pFile))
        {
            break;
        }


        printf("%x", buf);

        /* ... and play it */
        if (pa_simple_write(s_out, buf, sizeof(buf), &error) < 0) 
        {
            fprintf(stderr, __FILE__": pa_simple_write() failed: %s\n", pa_strerror(error));
            goto finish;
        }


    }


    /* Make sure that every single sample was played */
    if (pa_simple_drain(s_out, &error) < 0) 
    {
        fprintf(stderr, __FILE__": pa_simple_drain() failed: %s\n", pa_strerror(error));
        goto finish;
    }

    ret = 0;

    finish:

    if (s_out)
        pa_simple_free(s_out);

    return ret;
    fclose(pFile);
}

For loop in record program 只是为了记录一些东西的时间(无法弄清楚如何设置计时器)而且我知道我不应该使用 gotos 但它用于教育目的(PulseAudio 网站上提供的示例)。我尝试了 xma.bin 的 hexdump,它给了我完全不同的输出 比 printf("%x", buf); 基本上 printf 只会反复返回 bf9fe15c 并且会发出烦人的声音。希望你能帮忙。谢谢。

我从录音程序中删除了pa_simple_drain()(我在录音程序中使用这个功能是我的错误)功能,现在它可以工作了。但是在 printf("%x", buf) 它仍然一遍又一遍地给我相同的十六进制值。但是程序运行良好。有人可以解释为什么它打印相同的值吗?