-2

我使用脉冲音频的简单 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;
}
4

2 回答 2

2

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

于 2013-01-30T03:50:17.497 回答
1

我建议打开编译器检查并修复编译器在此代码上发出的​​所有错误和警告。

首先,m_pMyObject 从未初始化,因此在调用 RecordSound 时使用它意味着将未初始化的值作为“this”传递给方法。这通常是一件坏事。

在 RecordSound 和 PlaybackSound 中,您使用 size(buf) 来告诉库要读取/写入多少字节。参数 buf 是一个指向 uint8_t 的指针。所以编译器会填入指针的大小(在 64 位机器上可能是 8)。在这些方法中,您应该使用您拥有的尺寸参数。在调用中,将 sizeof(buf) 传递给该参数。

我不知道库在内存/资源耗尽之前可以创建多少个流。对 RecordSound 的每次调用都会创建一个录制流,而 PlaybackSound 会创建一个播放流。这些流永远不会被释放。

综上所述,如果使用未初始化的值调用 RecordSound 方法不会导致程序崩溃,它将创建一个录制流并录制几个样本,然后它将创建一个播放流并播放这两个样本。然后它将尝试再次执行所有这些操作。

于 2017-07-19T18:53:37.690 回答