shithub: soundpipe

ref: 0df3039072e1eb47d710348117063fa45cfaf2dd
dir: /modules/scrambler.c/

View raw version
/* This code is placed in the public domain. */

#include <math.h>
#include "soundpipe.h"
#include "kiss_fftr.h"

#ifndef M_PI
#define M_PI		3.14159265358979323846
#endif

int sp_gen_scrambler(sp_data *sp, sp_ftbl *src, sp_ftbl **dest)
{

    uint32_t size;
    sp_ftbl *dst;
    kiss_fftr_cfg fft, ifft;
    kiss_fft_cpx *tmp;
    uint32_t i;
    SPFLOAT mag, phs;
    SPFLOAT max;
    SPFLOAT val;

    size = (src->size % 2 == 0) ? src->size : src->size - 1;
    sp_ftbl_create(sp, &dst, size);

    /* set up kissfft */
    fft = kiss_fftr_alloc(size, 0, NULL, NULL);
    ifft = kiss_fftr_alloc(size, 1, NULL, NULL);
    tmp = malloc(sizeof(kiss_fft_cpx) * size);
    memset(tmp, 0, sizeof(SPFLOAT) * size);
    kiss_fftr(fft, src->tbl, tmp);

    for(i = 0; i < size / 2; i++) {
        mag = sqrt(tmp[i].r * tmp[i].r + tmp[i].i * tmp[i].i) / size;
        phs = ((SPFLOAT)sp_rand(sp) / SP_RANDMAX) * 2 * M_PI;
        tmp[i].r = mag * cos(phs);
        tmp[i].i = mag * sin(phs);
    }

    tmp[0].r = 0;
    tmp[0].i = 0;
    tmp[size / 2 - 1].r = 0;
    tmp[size / 2 - 1].i = 0;

    kiss_fftri(ifft, tmp, dst->tbl);
    max = -1;
    for(i = 0; i < size; i++) {
        val = fabs(dst->tbl[i]);
        if(val > max) {
            max = val;
        }
    }

    for(i = 0; i < size; i++) {
       dst->tbl[i] /= max;
    }

    kiss_fftr_free(fft);
    kiss_fftr_free(ifft);
    KISS_FFT_FREE(tmp);

    *dest = dst;
    return SP_OK;
}