我使用libao和libsndfile来读取和播放音频。我想通过将一个频道复制为两个来将单声道流转换为立体声流。此测试代码将正确播放立体声剪辑,但会以非常快速和高音调播放单声道剪辑。另外,我得到了一个双重免费或腐败"在free(output);
电话。我做错了什么?
/* Converting a 1-channel stream to 2-channels
* compile with "gcc -o stereoize stereoize.c -lao -lsndfile"
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <ao/ao.h>
#include <sndfile.h>
#define BUFFSIZE 512
void stereoize(short *, short *, size_t);
void playfile(FILE *);
int main(int argc, char *argv[])
{
FILE *fp;
if (argc != 2) {
printf("usage: %s <filename>\n", argv[0]);
exit(1);
}
fp = fopen(argv[1], "rb");
if (fp == NULL) {
printf("Cannot open %s.\n", argv[1]);
exit(2);
}
playfile(fp);
fclose(fp);
printf("Finished.\n");
return 0;
}
void playfile(FILE *fp)
{
int default_driver;
int frames_read;
int count;
int toread;
short *buffer;
short *output;
ao_device *device;
ao_sample_format format;
SNDFILE *sndfile;
SF_INFO sf_info;
ao_initialize();
default_driver = ao_default_driver_id();
sf_info.format = 0;
sndfile = sf_open_fd(fileno(fp), SFM_READ, &sf_info, 0);
memset(&format, 0, sizeof(ao_sample_format));
format.byte_format = AO_FMT_NATIVE;
format.bits = 16;
format.rate = sf_info.samplerate;
// format.channels = sf_info.channels;
format.channels = 2;
printf("Channels: %d\n", sf_info.channels);
printf("Samplerate: %d\n", sf_info.samplerate);
if (sf_info.channels > 2 || sf_info.channels < 1) {
printf("Sorry. Only 1 or 2 channels, please.\n");
exit(1);
}
device = ao_open_live(default_driver, &format, NULL);
if (device == NULL) {
printf("Error opening sound device.\n");
exit(1);
}
buffer = malloc(BUFFSIZE * sf_info.channels * sizeof(short));
output = malloc(BUFFSIZE * 2 * sizeof(short));
frames_read = 0;
toread = sf_info.frames * sf_info.channels;
while (toread > 0) {
if (toread < BUFFSIZE * sf_info.channels)
count = toread;
else
count = BUFFSIZE * sf_info.channels;
frames_read = sf_read_short(sndfile, buffer, count);
if (sf_info.channels == 1)
stereoize(output, buffer, count);
else
memcpy(output, buffer, count * sizeof(short));
if (sf_info.channels == 1)
ao_play(device, (char *)output, 2 * frames_read * sizeof(short));
else
ao_play(device, (char *)output, frames_read * sizeof(short));
toread = toread - frames_read;
}
free(buffer);
free(output);
ao_close(device);
sf_close(sndfile);
ao_shutdown();
return;
}
void stereoize(short *outbuf, short *inbuf, size_t length)
{
int count;
int outcount;
outcount = 0;
for (count = 0; count < length; count++) {
outbuf[outcount] = outbuf[outcount+1] = inbuf[count];
outcount += 2;
}
}
答案 0 :(得分:1)
声明stereoize(output, buffer, count * sizeof(short));
很可能导致问题。 stereoize
函数期望样本数为length
参数,但是您传递的是总块大小(以字节为单位)。将其更改为stereoize(output, buffer, count);
,看看它是否解决了问题。
另请查看@Joachim Pileborg的评论。