c-脉冲音频:录音但播放恼人的声音



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

#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: %sn", 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: %sn", 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: %sn", 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: %sn", 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: %sn", pa_strerror(error));
        goto finish;
    }
    ret = 0;
    finish:
    if (s_out)
        pa_simple_free(s_out);
    return ret;
    fclose(pFile);
}

因为循环录制程序只是用来录制一些东西的时间(不知道如何设置计时器),我知道我不应该使用gotos,但它是用于教育目的(PulseAudio网站上提供的示例)。我尝试了xma.bin的hexdump,它给了我完全不同的输出大于printf("%x",buf)基本上printf只会重复返回bf9fe15c,并且会发出恼人的声音。希望你能帮忙。谢谢

我从录制程序中删除了pa_simple_drain()(在录制程序中使用此函数是我的错误)函数,现在它可以工作了。但在printf("%x",buf)中,它仍然一次又一次地给我返回相同的十六进制值。但是程序运行良好。有人能举例说明为什么它打印出相同的值吗?

最新更新