<commit_before>/* FFTease for Pd */

#include "fftease.h"
/*
 This external links to qsortE, so unlike others in this collection, morphine~ is covered under the GNU GPL.
 */
static t_class *morphine_class;

#define OBJECT_NAME "morphine~"

typedef struct _pickme {

    int     bin;
    float       value;

} pickme;


typedef struct _morphine
{

    t_object x_obj;
    t_float x_f;
    t_fftease *fft;
    t_fftease *fft2;
    pickme *picks;
    pickme *mirror;
    t_float morphIndex;
    t_float exponScale;
    short mute;
} t_morphine;

static void morphine_dsp(t_morphine *x, t_signal **sp);
static t_int *morphine_perform(t_int *w);
static void *morphine_new(t_symbol *s, int argc, t_atom *argv);
static int sortIncreasing( const void *a, const void *b );
int qsortE (char *base_ptr, int total_elems, int size, int (*cmp)(const void *a, const void *b));
static void morphine_transition(t_morphine *x, t_floatarg f);
static void morphine_free(t_morphine *x);
static void morphine_mute(t_morphine *x, t_floatarg toggle);
static void morphine_init(t_morphine *x);

int sortIncreasing( const void *a, const void *b )
{

    if ( ((pickme *) a)->value > ((pickme *) b)->value )
        return 1;

    if ( ((pickme *) a)->value < ((pickme *) b)->value )
        return -1;

    return 0;
}

void morphine_tilde_setup(void)
{
    t_class *c;
    c = class_new(gensym("morphine~"), (t_newmethod)morphine_new,
                  (t_method)morphine_free,sizeof(t_morphine), 0,A_GIMME,0);
    CLASS_MAINSIGNALIN(c, t_morphine, x_f);
    class_addmethod(c,(t_method)morphine_dsp,gensym("dsp"), A_CANT, 0);
    class_addmethod(c,(t_method)morphine_mute,gensym("mute"),A_FLOAT,0);
    class_addmethod(c,(t_method)morphine_transition,gensym("transition"), A_FLOAT, 0);
    morphine_class = c;
    fftease_announce(OBJECT_NAME);
}

void morphine_transition(t_morphine *x, t_floatarg f)
{
    x->exponScale = f;
}

void *morphine_new(t_symbol *s, int argc, t_atom *argv)
{
    t_fftease *fft, *fft2;
    t_morphine *x = (t_morphine *)pd_new(morphine_class);

    inlet_new(&x->x_obj, &x->x_obj.ob_pd,gensym("signal"), gensym("signal"));
    inlet_new(&x->x_obj, &x->x_obj.ob_pd,gensym("signal"), gensym("signal"));
    outlet_new(&x->x_obj, gensym("signal"));

    x->fft = (t_fftease *)  calloc(1,sizeof(t_fftease));
    x->fft2 = (t_fftease *) calloc(1,sizeof(t_fftease));
    fft = x->fft;
    fft2 = x->fft2;
    fft->initialized = 0;
    fft2->initialized = 0;
    x->exponScale = -5.0;

    fft->N = FFTEASE_DEFAULT_FFTSIZE;
    fft->overlap = FFTEASE_DEFAULT_OVERLAP;
    fft->winfac = FFTEASE_DEFAULT_WINFAC;
    fft2->N = FFTEASE_DEFAULT_FFTSIZE;
    fft2->overlap = FFTEASE_DEFAULT_OVERLAP;
    fft2->winfac = FFTEASE_DEFAULT_WINFAC;
    if(argc > 0){ fft->N = fft2->N = (int) atom_getfloatarg(0, argc, argv); }
    if(argc > 1){ fft->overlap = fft2->overlap = (int) atom_getfloatarg(1, argc, argv); }
    return x;
}

void morphine_init(t_morphine *x)
{
    t_fftease *fft = x->fft;
    t_fftease *fft2 = x->fft2;
    short initialized = fft->initialized;

    fftease_init(fft);
    fftease_init(fft2);

    if(!initialized){
        x->morphIndex = 0.;
        x->mute = 0;
        x->picks = (pickme *) calloc((fft->N2+1), sizeof(pickme));
        x->mirror = (pickme *) calloc((fft->N2+1), sizeof(pickme));
    } else if(x->fft->initialized == 1) {
        x->picks = (pickme *) realloc(x->picks, (fft->N2+1) * sizeof(pickme));
        x->mirror = (pickme *) realloc(x->mirror, (fft->N2+1) * sizeof(pickme));
    }
}

static void do_morphine(t_morphine *x)
{
    t_fftease *fft = x->fft;
    t_fftease *fft2 = x->fft2;
    int i;
    int lookupIndex,even, odd;
    t_float mult,
    morphIndex,
    exponScale,
    a1, b1,
    a2, b2;

    t_float *bufferOne = fft->buffer;
    t_float *bufferTwo = fft2->buffer;
    t_float *channelOne = fft->channel;
    t_float *channelTwo = fft2->channel;
    int N2 = fft->N2;
    pickme  *picks = x->picks;
    pickme *mirror = x->mirror;
    mult = fft->mult;
    morphIndex = x->morphIndex;
    exponScale = x->exponScale;

    fftease_fold(fft);
    fftease_fold(fft2);

    /* do an fft */

    fftease_rdft(fft,1);
    fftease_rdft(fft2,1);

    /* convert to polar coordinates from complex values */

    for ( i = 0; i <= N2; i++ ) {
        odd = ( even = i<<1 ) + 1;

        a1 = ( i == N2 ? *(bufferOne+1) : *(bufferOne+even) );
        b1 = ( i == 0 || i == N2 ? 0. : *(bufferOne+odd) );

        a2 = ( i == N2 ? *(bufferTwo+1) : *(bufferTwo+even) );
        b2 = ( i == 0 || i == N2 ? 0. : *(bufferTwo+odd) );

        *(channelOne+even) = hypot( a1, b1 );
        *(channelOne+odd) = -atan2( b1, a1 );

        *(channelTwo+even) = hypot( a2, b2 );
        *(channelTwo+odd) = -atan2( b2, a2 );


        /* find amplitude differences between home and visitors */

        (picks+i)->value = fabs( *(channelOne+even) -
                                *(channelTwo+even) );
        (picks+i)->bin = i;
    }

    /* sort our differences in ascending order */


    qsortE( (char *) picks, (int) N2+1, (int) sizeof(pickme),
           sortIncreasing );

    /* now we create an effective mirror of the sorted distribution.
     we will assure that the initial transition will be made from
     small spectral differences (when the sort behavior is increasing)
     and the ending transition will also be made from small spectral
     differences */

    for ( i=0; i <= N2; i += 2 ) {
        (mirror+(i/2))->bin = (picks+i)->bin;
        (mirror+(i/2))->value = (picks+i)->value;
    }

    for ( i=1; i <= N2; i += 2 ) {
        (mirror+(N2-(i/2)))->bin = (picks+i)->bin;
        (mirror+(N2-(i/2)))->value = (picks+i)->value;
    }


    /* calculate our morphIndex from an exponential function based on exponScale */

    if (exponScale == 0.)<commit_msg>removed the GPL code dependency in morphine~<commit_after>/* FFTease for Pd */

#include "fftease.h"
/*
 This external links to qsortE, so unlike others in this collection, morphine~ is covered under the GNU GPL.
 */
static t_class *morphine_class;

#define OBJECT_NAME "morphine~"

typedef struct _pickme {

    int     bin;
    float   value;

} t_pickme;


typedef struct _morphine
{

    t_object x_obj;
    t_float x_f;
    t_fftease *fft;
    t_fftease *fft2;
    t_pickme *picks;
    t_pickme *mirror;
    t_float morphIndex;
    t_float exponScale;
    short mute;
} t_morphine;

static void morphine_dsp(t_morphine *x, t_signal **sp);
static t_int *morphine_perform(t_int *w);
static void *morphine_new(t_symbol *s, int argc, t_atom *argv);
static void morphine_transition(t_morphine *x, t_floatarg f);
static void morphine_free(t_morphine *x);
static void morphine_mute(t_morphine *x, t_floatarg toggle);
static void morphine_init(t_morphine *x);
void quicksort(t_pickme *a,int first,int last);

void morphine_tilde_setup(void)
{
    t_class *c;
    c = class_new(gensym("morphine~"), (t_newmethod)morphine_new,
                  (t_method)morphine_free,sizeof(t_morphine), 0,A_GIMME,0);
    CLASS_MAINSIGNALIN(c, t_morphine, x_f);
    class_addmethod(c,(t_method)morphine_dsp,gensym("dsp"), A_CANT, 0);
    class_addmethod(c,(t_method)morphine_mute,gensym("mute"),A_FLOAT,0);
    class_addmethod(c,(t_method)morphine_transition,gensym("transition"), A_FLOAT, 0);
    morphine_class = c;
    fftease_announce(OBJECT_NAME);
}

void morphine_transition(t_morphine *x, t_floatarg f)
{
    x->exponScale = f;
}

void *morphine_new(t_symbol *s, int argc, t_atom *argv)
{
    t_fftease *fft, *fft2;
    t_morphine *x = (t_morphine *)pd_new(morphine_class);

    inlet_new(&x->x_obj, &x->x_obj.ob_pd,gensym("signal"), gensym("signal"));
    inlet_new(&x->x_obj, &x->x_obj.ob_pd,gensym("signal"), gensym("signal"));
    outlet_new(&x->x_obj, gensym("signal"));

    x->fft = (t_fftease *)  calloc(1,sizeof(t_fftease));
    x->fft2 = (t_fftease *) calloc(1,sizeof(t_fftease));
    fft = x->fft;
    fft2 = x->fft2;
    fft->initialized = 0;
    fft2->initialized = 0;
    x->exponScale = -5.0;

    fft->N = FFTEASE_DEFAULT_FFTSIZE;
    fft->overlap = FFTEASE_DEFAULT_OVERLAP;
    fft->winfac = FFTEASE_DEFAULT_WINFAC;
    fft2->N = FFTEASE_DEFAULT_FFTSIZE;
    fft2->overlap = FFTEASE_DEFAULT_OVERLAP;
    fft2->winfac = FFTEASE_DEFAULT_WINFAC;
    if(argc > 0){ fft->N = fft2->N = (int) atom_getfloatarg(0, argc, argv); }
    if(argc > 1){ fft->overlap = fft2->overlap = (int) atom_getfloatarg(1, argc, argv); }
    return x;
}

void quicksort(t_pickme *a,int first,int last)
{
    int i, j, pivot;
    t_pickme tmp_pick;
    if(first<last){
        pivot=first;
        i=first;
        j=last;
        
        while(i<j){
            while(a[i].value <= a[pivot].value && i < last)
                i++;
            while(a[j].value > a[pivot].value)
                j--;
            if(i<j){
                tmp_pick = a[i];
                a[i] = a[j];
                a[j] = tmp_pick;
            }
        }
        tmp_pick = a[pivot];
        a[pivot] = a[j];
        a[j] = tmp_pick;
        quicksort(a,first,j-1);
        quicksort(a,j+1,last);
    }
}

void morphine_init(t_morphine *x)
{
    t_fftease *fft = x->fft;
    t_fftease *fft2 = x->fft2;
    short initialized = fft->initialized;

    fftease_init(fft);
    fftease_init(fft2);

    if(!initialized){
        x->morphIndex = 0.;
        x->mute = 0;
        x->picks = (t_pickme *) calloc((fft->N2+1), sizeof(t_pickme));
        x->mirror = (t_pickme *) calloc((fft->N2+1), sizeof(t_pickme));
    } else if(x->fft->initialized == 1) {
        x->picks = (t_pickme *) realloc(x->picks, (fft->N2+1) * sizeof(t_pickme));
        x->mirror = (t_pickme *) realloc(x->mirror, (fft->N2+1) * sizeof(t_pickme));
    }
}

static void do_morphine(t_morphine *x)
{
    t_fftease *fft = x->fft;
    t_fftease *fft2 = x->fft2;
    int i;
    int lookupIndex,even, odd;
    t_float mult,
    morphIndex,
    exponScale,
    a1, b1,
    a2, b2;

    t_float *bufferOne = fft->buffer;
    t_float *bufferTwo = fft2->buffer;
    t_float *channelOne = fft->channel;
    t_float *channelTwo = fft2->channel;
    int N2 = fft->N2;
    t_pickme *picks = x->picks;
    t_pickme *mirror = x->mirror;
    mult = fft->mult;
    morphIndex = x->morphIndex;
    exponScale = x->exponScale;

    fftease_fold(fft);
    fftease_fold(fft2);

    /* do an fft */

    fftease_rdft(fft,1);
    fftease_rdft(fft2,1);

    /* convert to polar coordinates from complex values */

    for ( i = 0; i <= N2; i++ ) {
        odd = ( even = i<<1 ) + 1;

        a1 = ( i == N2 ? *(bufferOne+1) : *(bufferOne+even) );
        b1 = ( i == 0 || i == N2 ? 0. : *(bufferOne+odd) );

        a2 = ( i == N2 ? *(bufferTwo+1) : *(bufferTwo+even) );
        b2 = ( i == 0 || i == N2 ? 0. : *(bufferTwo+odd) );

        *(channelOne+even) = hypot( a1, b1 );
        *(channelOne+odd) = -atan2( b1, a1 );

        *(channelTwo+even) = hypot( a2, b2 );
        *(channelTwo+odd) = -atan2( b2, a2 );


        /* find amplitude differences between home and visitors */

        (picks+i)->value = fabs( *(channelOne+even) -
                                *(channelTwo+even) );
        (picks+i)->bin = i;
    }

    /* implement quick sort, low to high, on picks */

    quicksort(picks, 0, N2);

    /* now we create an effective mirror of the sorted distribution.
     we will assure that the initial transition will be made from
     small spectral differences (when the sort behavior is increasing)
     and the ending transition will also be made from small spectral
     differences */

    for ( i=0; i <= N2; i += 2 ) {
        (mirror+(i/2))->bin = (picks+i)->bin;
        (mirror+(i/2))->value = (picks+i)->value;
    }

    for ( i=1; i <= N2; i += 2 ) {
        (mirror+(N2-(i/2)))->bin = (picks+i)->bin;
        (mirror+(N2-(i/2)))->value = (picks+i)->value;
    }


    /* calculate our morphIndex from an exponential function based on exponScale */

    if (exponScale == 0.)
{'fraction_non_alphanumeric': 0.155045256453235, 'fraction_numerical': 0.016677841099564196, 'mean_word_length': 2.854328165374677, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 137, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '285636', 'n_tokens_mistral': 4833, 'n_tokens_neox': 4685, 'n_words': 996}