我正在使用 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;
}
}