gpt4 book ai didi

c - PulseAudio:录制声音但播放烦人的声音

转载 作者:行者123 更新时间:2023-11-30 17:08:14 26 4
gpt4 key购买 nike

我是 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: %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循环只是为了记录一些东西的时间(无法弄清楚如何设置计时器),我知道我不应该使用goto,但它是出于教育目的(PulseAudio网站上提供的示例)。我尝试了 xma.bin 的十六进制转储,它给了我完全不同的输出比 printf("%x", buf); 基本上 printf 只会重复返回 bf9fe15c 并且发出烦人的声音。希望你能帮忙。谢谢。

最佳答案

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

关于c - PulseAudio:录制声音但播放烦人的声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33777417/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com