Использование libsamplerate с libsndfile

Этот код является частью моих попыток лучше понять кодирование звука. Здесь файл открывается с помощью libsndfile, преобразуется с помощью libsamplerate в новую частоту дискретизации, а результат воспроизводится с помощью libao.

При воспроизведении различных комбинаций битов, каналов и скоростей результаты следующие:

количество тестов, биты, каналы, скорость, результат

  1. 8, 1, 11025, OK
  2. 8, 2, 11025, дрожание звука. В остальном тон и скорость в порядке.
  3. 16, 1, 11025, OK
  4. 16, 2, 11025, Звук дрожит. В остальном тон и скорость в порядке.
  5. 8, 1, 44100, OK
  6. 8, 2, 44100, OK
  7. 16, 1, 44100, OK
  8. 16, 2, 44100, OK

Почему тесты 2 и 4 не проходят?

 /*
 * Objective: sample rate conversion
 * compile with
 * "gcc -o glurp glurp.c -lao -lsndfile -lsamplerate"
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <ao/ao.h>
#include <sndfile.h>
#include <samplerate.h>

#define DEFAULT_CONVERTER SRC_SINC_MEDIUM_QUALITY
#define NEW_RATE 44100

#define BUFFSIZE 4096
#define MAX(x,y) ((x)>(y)) ? (x) : (y)
#define MIN(x,y) ((x)<(y)) ? (x) : (y)

int playfile(FILE *, int);
void floattopcm16(short *, float *, int);
void pcm16tofloat(float *, short *, int);

int main(int argc, char *argv[])
{
    FILE *fp;
    int newrate;

    if (argc < 2) {
        printf("usage: %s <filename> <rate>\n", argv[0]);
    exit(1);
    }

    fp = fopen(argv[1], "rb");
    if (fp == NULL) {
        printf("Cannot open %s.\n", argv[1]);
    exit(1);
    }

    if (argv[2])
        newrate = atoi(argv[2]);
    else
        newrate = NEW_RATE;

    playfile(fp, newrate);

    return 0;
}

int playfile(FILE *fp, int newrate)
{
    int default_driver;
    int frames_read;
    int count;
    int toread;
    int readnow;
    float *floatbuffer;
    float *floatbuffer2;
    short *shortbuffer;
    long filestart;

    int volcount;

    ao_device *device;
    ao_sample_format format;
    SNDFILE     *sndfile;
    SF_INFO sf_info;

    SRC_STATE   *src_state;
    SRC_DATA    src_data;
    int     error;
    double  max = 0.0;
    sf_count_t  output_count = 0;

    ao_initialize();
    default_driver = ao_default_driver_id();

    sf_info.format = 0;

    filestart = ftell(fp);

    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.channels = sf_info.channels;
    format.rate = newrate;

    printf("Start sample rate:  %d\n", sf_info.samplerate);
    printf("Ending sample rate: %d\n", newrate);

    device = ao_open_live(default_driver, &format, NULL /* no options */);
    if (device == NULL) {
        printf("Error opening sound device.\n");
        return 1;
    }

    floatbuffer = malloc(BUFFSIZE * sf_info.channels * sizeof(float));
    floatbuffer2 = malloc(BUFFSIZE * sf_info.channels * sizeof(float));
    shortbuffer = malloc(BUFFSIZE * sf_info.channels * sizeof(short));
    frames_read = 0;
    toread = sf_info.frames * sf_info.channels;

    /* Set up for conversion */
    if ((src_state = src_new(DEFAULT_CONVERTER, sf_info.channels, &error)) == NULL) {
        printf("Error: src_new() failed: %s.\n", src_strerror(error));
        exit(1);
    }
    src_data.end_of_input = 0;
    src_data.input_frames = 0;
    src_data.data_in = floatbuffer;
    src_data.src_ratio = (1.0 * newrate) / sf_info.samplerate;
    src_data.data_out = floatbuffer2;
    src_data.output_frames = BUFFSIZE / sf_info.channels;

    while (1) {
         /* if floatbuffer is empty, refill it */
         if (src_data.input_frames == 0) {
             src_data.input_frames = sf_read_float(sndfile, floatbuffer, BUFFSIZE / sf_info.channels);
             src_data.data_in = floatbuffer;

             /* mark end of input */
             if (src_data.input_frames < BUFFSIZE / sf_info.channels)
             src_data.end_of_input = SF_TRUE;
         }

         if ((error = src_process(src_state, &src_data))) {
             printf("Error: %s\n", src_strerror(error));
             exit(1);
         }

         /* terminate if done */
         if (src_data.end_of_input && src_data.output_frames_gen == 0)
             break;

         /* write output */
         output_count += src_data.output_frames_gen;
         src_data.data_in += src_data.input_frames_used * sf_info.channels;
         src_data.input_frames -= src_data.input_frames_used;

         floattopcm16(shortbuffer, floatbuffer2, src_data.output_frames_gen);
         ao_play(device, (char *)shortbuffer, src_data.output_frames_gen * sizeof(short));

    }

    src_state = src_delete(src_state);

    free(shortbuffer);
    free(floatbuffer);
    free(floatbuffer2);
    fseek(fp, filestart, SEEK_SET);
    ao_close(device);
    sf_close(sndfile);
    ao_shutdown();
    printf("Finished\n");

    return 0;
}


/* Convert back to shorts */
void floattopcm16(short *outbuf, float *inbuf, int length)
{
    int   count;

    const float mul = (32768.0f);
    for (count = 0; count <= length; count++) {
        int32_t tmp = (int32_t)(mul * inbuf[count]);
        tmp = MAX( tmp, -32768 ); // CLIP < 32768
        tmp = MIN( tmp, 32767 );  // CLIP > 32767
        outbuf[count] = tmp;
    }
}

person Frotz    schedule 31.12.2013    source источник


Ответы (2)


С любезной помощью Эрика я наконец-то заработал этот тестовый код. Моя проблема заключалась в непонимании аудиокадров и аудиосэмплов. Кадр состоит из одной выборки на канал. Образец — это просто число, обозначающее звуковой сигнал в данный момент. Я думал, что знаю разницу, но забыл, применяя примеры кода Эрика, видимые в основном цикле. Этот код взят из sndfile-resample.c каталога examples в архиве дистрибутива libsamplerate. Следствием этого недоразумения было то, что в стереосэмплах последние несколько сэмплов (от 23 до 60 в зависимости от размера буфера) были бы нулевыми. Это вызвало дрожание воспроизведения. Если я уменьшил размер буфера до 512, то получил искажение, похожее на кольцевой модулятор аналогового синтезатора. Обратите внимание на изменение с sf_read_float() на sf_readf_float(). Цикл в floattopcm16() неправильно тестировал count <= length. Я исправил его на count < length.

Для тех, у кого тоже проблемы, вот код, который работает и проходит -Wall.

/*
 * Objective: sample rate conversion
 * compile with
 * "gcc -o glurp glurp.c -lao -lsndfile -lsamplerate"
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <ao/ao.h>
#include <sndfile.h>
#include <samplerate.h>

#define DEFAULT_CONVERTER SRC_SINC_MEDIUM_QUALITY
#define NEW_RATE 44100

#define BUFFSIZE 4096
#define MAX(x,y) ((x)>(y)) ? (x) : (y)
#define MIN(x,y) ((x)<(y)) ? (x) : (y)

int playfile(FILE *);
void floattopcm16(short *, float *, int);
void pcm16tofloat(float *, short *, int);

int main(int argc, char *argv[])
{
    FILE *fp;

    if (argc != 2) {
        printf("usage: %s <input>\n", argv[0]);
    exit(1);
    }

    fp = fopen(argv[1], "rb");
    if (fp == NULL) {
        printf("Cannot open %s.\n", argv[1]);
    exit(1);
    }

    playfile(fp);
    fclose(fp);

    return 0;
}

int playfile(FILE *fp)
{
    int default_driver;
    float *floatbuffer;
    float *floatbuffer2;
    short *shortbuffer;
    long filestart;

    int newrate = NEW_RATE;

    ao_device *device;
    ao_sample_format format;
    SNDFILE     *sndfile;
    SF_INFO sf_info;

    SRC_STATE   *src_state;
    SRC_DATA    src_data;
    int     error;
    sf_count_t  output_count = 0;

    ao_initialize();
    default_driver = ao_default_driver_id();

    sf_info.format = 0;

    filestart = ftell(fp);

    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.channels = sf_info.channels;
    format.rate = newrate;

    printf("Channels:           %d\n", sf_info.channels);
    printf("Start sample rate:  %d\n", sf_info.samplerate);
    printf("Ending sample rate: %d\n", newrate);

    device = ao_open_live(default_driver, &format, NULL /* no options */);
    if (device == NULL) {
        printf("Error opening sound device.\n");
        return 1;
    }

    floatbuffer = malloc(BUFFSIZE * sf_info.channels * sizeof(float));
    floatbuffer2 = malloc(BUFFSIZE * sf_info.channels * sizeof(float));
    shortbuffer = malloc(BUFFSIZE * sf_info.channels * sizeof(short));

    /* Set up for conversion */
    if ((src_state = src_new(DEFAULT_CONVERTER, sf_info.channels, &error)) == NULL) {
        printf("Error: src_new() failed: %s.\n", src_strerror(error));
        exit(1);
    }
    src_data.end_of_input = 0;
    src_data.input_frames = 0;
    src_data.data_in = floatbuffer;
    src_data.src_ratio = (1.0 * newrate) / sf_info.samplerate;
    src_data.data_out = floatbuffer2;
    src_data.output_frames = BUFFSIZE / sf_info.channels;

    while (1) {
        /* if floatbuffer is empty, refill it */
        if (src_data.input_frames == 0) {
            src_data.input_frames = sf_readf_float(sndfile, floatbuffer, BUFFSIZE / sf_info.channels);
            src_data.data_in = floatbuffer;

            /* mark end of input */
            if (src_data.input_frames < BUFFSIZE / sf_info.channels)
                src_data.end_of_input = SF_TRUE;
        }

        if ((error = src_process(src_state, &src_data))) {
            printf("Error: %s\n", src_strerror(error));
            exit(1);
        }

        /* terminate if done */
        if (src_data.end_of_input && src_data.output_frames_gen == 0)
            break;

        /* write output */
        floattopcm16(shortbuffer, floatbuffer2, src_data.output_frames_gen * sf_info.channels);
        ao_play(device, (char *)shortbuffer, src_data.output_frames_gen * sizeof(short) * sf_info.channels);

        output_count += src_data.output_frames_gen;
        src_data.data_in += src_data.input_frames_used * sf_info.channels;
        src_data.input_frames -= src_data.input_frames_used;
    }

    src_state = src_delete(src_state);

    free(shortbuffer);
    free(floatbuffer);
    free(floatbuffer2);
    fseek(fp, filestart, SEEK_SET);
    ao_close(device);
    sf_close(sndfile);
    ao_shutdown();
    printf("Finished\n");

    return 0;
}


/* Convert back to shorts */
void floattopcm16(short *outbuf, float *inbuf, int length)
{
    int   count;

    const float mul = (32768.0f);
    for (count = 0; count < length; count++) {
        int32_t tmp = (int32_t)(mul * inbuf[count]);
        tmp = MAX( tmp, -32768 ); // CLIP < 32768
        tmp = MIN( tmp, 32767 );  // CLIP > 32767
        outbuf[count] = tmp;
    }
}
person Frotz    schedule 24.04.2015

Libsndfile не выполняет деинтерлейсинг стереозвука, вам придется делать это вручную.

person MarcusJ    schedule 20.06.2014
comment
Извините но нет. Согласно xiph.org/ao/doc/ao_play.html ao_play() принимает чередование данные. - person Erik de Castro Lopo; 24.04.2015