gpt4 book ai didi

c++ - 从 Pulse Audio 运行简单的 C 程序时出错

转载 作者:行者123 更新时间:2023-11-30 18:42:47 28 4
gpt4 key购买 nike

我使用了简单的脉冲音频C代码来播放和录制,效果很好。但是当我将它转换为 C++ 时,它不起作用。我正在粘贴这两个代码。请帮忙。 C++ 代码不显示任何错误,但不播放任何声音。但 C++ 代码会播放录制的声音。注意:我使用的是 64 位 CentOS 6.2

C++ 代码:

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <iostream>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <stdint.h>

#include "pulse/simple.h"
#include "pulse/error.h"
using namespace std;
#define BUFSIZE 32
int error;

/* The Sample format to use */


class AudioCapt
{

public:
AudioCapt();
void RecordSound(int argc,char *argv[],pa_simple *s_in,pa_sample_spec &ss,pa_simple *s_out,uint8_t buf[],ssize_t r);
void PlaybackSound(int argc, char*argv[],pa_simple *s_out,pa_sample_spec &ss,uint8_t buf[],ssize_t r);
};




void AudioCapt::RecordSound(int argc, char*argv[],pa_simple *s_in,pa_sample_spec &ss,pa_simple *s_out,uint8_t buf[],ssize_t r)
{

printf("Audio Capturing \n");
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));
}

if (pa_simple_read(s_in, buf, sizeof(buf), &error) < 0) {
fprintf(stderr, __FILE__": read() failed: %s\n", strerror(errno));
}
printf("Buffer :::: %d\n",buf[0]);

}

void AudioCapt::PlaybackSound(int argc, char*argv[],pa_simple *s_out,pa_sample_spec &ss,uint8_t buf[],ssize_t r)
{
printf("Audio PlayBack \n");
printf("Play Buffer::: %d\n",buf[0]);
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));
}

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

/* 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));
}
}

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

pa_sample_spec ss;
ss.format = PA_SAMPLE_S16LE;
ss.rate = 44100;
ss.channels = 2;

pa_simple *s_in, *s_out = NULL;

AudioCapt *m_pMyObject;

for(;;)
{
uint8_t buf[BUFSIZE];
ssize_t r;
m_pMyObject->RecordSound(argc,argv,s_in,ss,s_out,buf,r);
m_pMyObject->PlaybackSound(argc,argv,s_out,ss,buf,r);
}
return 0;
}

C 代码:

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

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <fcntl.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,
.rate = 44100,
.channels = 2
};

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


/* 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;
}

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 (;;) {
uint8_t buf[BUFSIZE];
ssize_t r;

#if 1
pa_usec_t latency;

if ((latency = pa_simple_get_latency(s_in, &error)) == (pa_usec_t) -1) {
fprintf(stderr, __FILE__": pa_simple_get_latency() failed: %s\n", pa_strerror(error));
goto finish;
}

fprintf(stderr, "In: %0.0f usec \r\n", (float)latency);

if ((latency = pa_simple_get_latency(s_out, &error)) == (pa_usec_t) -1) {
fprintf(stderr, __FILE__": pa_simple_get_latency() failed: %s\n", pa_strerror(error));
goto finish;
}

fprintf(stderr, "Out: %0.0f usec \r\n", (float)latency);
#endif

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

fprintf(stderr, __FILE__": read() failed: %s\n", strerror(errno));
goto finish;
}
printf("Buffer :::: %d\n",buf[0]);

/* ... 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_in)
pa_simple_free(s_in);
if (s_out)
pa_simple_free(s_out);

return ret;
}

最佳答案

在 RecordSound 和 PlaybackSound 中,您使用 pa_simple_new 初始化临时变量。一旦函数返回,并且将 NULL 传递给下一个函数,该值就会丢失。

关于c++ - 从 Pulse Audio 运行简单的 C 程序时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14596310/

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