Search code examples
clinuxaudiopcmlibsndfile

In C, how do I convert 1-channel PCM into 2-channel PCM?


I am using libao and libsndfile to read and play audio. I would like to convert a monaural stream into a stereo stream by copying one channel into two. This test code will play a stereo clip correctly, but will play a monaural one very fast and high-pitched. Additionally I'm getting a "double free or corruption" on the free(output); call. What am I doing wrong?

/* 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;
    }
}

Solution

  • The statement stereoize(output, buffer, count * sizeof(short));, is most probably causing the problem. stereoize function expects number of samples as length parameter, but you are passing the total block size in bytes. Change it to stereoize(output, buffer, count);, See if it solves the problem. Also check @Joachim Pileborg's comment.