如何找到正确的RAW格式

时间:2013-07-09 11:29:43

标签: c pulseaudio libsndfile

我有这段代码

SNDFILE *sf;
SF_INFO info;
int num_channels;
int num, num_items;
int *buf;
int f,sr,c;
int i,j;
FILE *out;

/* Open the WAV file. */
info.format     = (SF_FORMAT_RAW | SF_FORMAT_PCM_16);  
info.samplerate = 44100;
info.channels = 2;  

sf = sf_open("test.raw",SFM_READ,&info);

if (sf == NULL)
    {
    printf("Failed to open the file. ( %d )\n",sf_perror(sf));
    exit(-1);
    }
/* Print some of the info, and figure out how much data to read. */
f = info.frames;
sr = info.samplerate;
c = info.channels;
num_items = f*c;
/* Allocate space for the data to be read, then read it. */
buf = (int *) malloc(num_items*sizeof(int));
num = sf_read_int(sf,buf,num_items);
sf_close(sf);
printf("Read %d items\n",num);
/* Write the data to filedata.out. */
out = fopen("test.txt","w");  
int links;
int rechts;
for (i = 0; i < num; i += c)
{
  for (j = 0; j < c; ++j)
      fprintf(out,"%d ",buf[i+j]);
  fprintf(out,"\n");
}  
fclose(out);

它的目的是阅读&#34; test.raw&#34;,将其转换为数组并将其写入&#34; test.txt&#34;。 &#34; test.raw&#34;是由

创建的原始pcm
...    
static const pa_sample_spec ss = 
{
    .format = PA_SAMPLE_S16LE,
    .rate = 44100,
    .channels = 2
};
pa_simple *s = NULL;
int ret = 1;
int error;
s = pa_simple_new(NULL, 
        "rec", 
        PA_STREAM_RECORD, 
        "bluez_source.00_00_00_00_00_00", 
        "rec", 
        &ss, 
        NULL, 
        NULL, 
        &error)  
...     

来自pulseaudio录音样本(download)。

事情是,我得到类似

的东西
219676672 -131072 
219676672 327680 
219611136 655360 
219217920 327680 
218955776 -131072 
219152384 -393216 
218693632 -720896 

在test.txt中。我添加了标题,以获取

SAMPLES:    365568
BITSPERSAMPLE:  16
CHANNELS:   2
SAMPLERATE: 44100
NORMALIZED: FALSE
219676672 -131072 
219676672 327680 
219611136 655360 
219217920 327680 
218955776 -131072 
219152384 -393216 
218693632 -720896 

并作为ascii文件导入adobe试听。我使用44100,16位和立体声以及英特尔(也试过摩托罗拉)。

每次我只得到&#34;酒吧&#34;即恒定的时期。阅读&#34; test.raw&#34;在试镜中我看到数据,因为它应该使用英特尔属性。

我需要调整什么才能使其正常工作?

1 个答案:

答案 0 :(得分:0)

通过查看数据,您似乎对16位和32位有符号整数数据感到困惑。您的.raw数据是16位的,可能还可以,但是您的.txt数据中的数字如219676672, -131072显然超出了16位数据范围(最小值为-32768 ,最大值为32767)。

您所描述的每次我只得到“条”,即恒定体积的时间段,这称为饱和度:您试图显示超出限制的数字,以便将值替换为限制。 Audition检测到您的数据是32位的,因此可以正确显示。

如何解决?

我认为您的int是32位的,因此您不应将读取的数据存储到intshort或等效的16位。

short *buf;

. . .

/* Allocate space for the data to be read, then read it. */
buf = (short *) malloc(num_items*sizeof(short));
num = sf_read_short(sf,buf,num_items);