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

最佳答案

语句stereoize(output, buffer, count * sizeof(short));,最有可能导致此问题。 stereoize函数期望样本数量作为length参数,但是您正在传递总块大小(以字节为单位)。将其更改为stereoize(output, buffer, count);,看看是否可以解决问题。
还要检查@Joachim Pileborg的评论。

09-10 17:14