Ninja Jamm - a revolutionary new musix remix app from Ninja Tune and Seeper, 
for iPhone and iPad
http://www.ninjajamm.com/


Gemnotes-0.2: Live music notation for Pure Data, now with dynamics!
http://sharktracks.co.uk/ 
#include "m_pd.h"
#include <math.h>

#define UNITBIT32 1572864.  /* 3*2^19; bit 32 has place value 1 */

    /* machine-dependent definitions.  These ifdefs really
    should have been by CPU type and not by operating system! */
#ifdef IRIX
    /* big-endian.  Most significant byte is at low address in memory */
#define HIOFFSET 0    /* word offset to find MSB */
#define LOWOFFSET 1    /* word offset to find LSB */
#define int32 long  /* a data type that has 32 bits */
#endif /* IRIX */

#ifdef MSW
    /* little-endian; most significant byte is at highest address */
#define HIOFFSET 1
#define LOWOFFSET 0
#define int32 long
#endif

#if defined(__FreeBSD__) || defined(__APPLE__)
#include <machine/endian.h>
#endif

#ifdef __linux__
#include <endian.h>
#endif

#if defined(__unix__) || defined(__APPLE__)
#if !defined(BYTE_ORDER) || !defined(LITTLE_ENDIAN)                         
#error No byte order defined                                                    
#endif                                                                          

#if BYTE_ORDER == LITTLE_ENDIAN                                             
#define HIOFFSET 1                                                              
#define LOWOFFSET 0                                                             
#else                                                                           
#define HIOFFSET 0    /* word offset to find MSB */                             
#define LOWOFFSET 1    /* word offset to find LSB */                            
#endif /* __BYTE_ORDER */                                                       
#include <sys/types.h>
#define int32 int32_t
#endif /* __unix__ or __APPLE__*/

union tabfudge
{
    double tf_d;
    int32 tf_i[2];
};

/* -------------------------- phasorbars~ ------------------------------ */
static t_class *phasorbars_class;

typedef struct _phasorbars
{
  t_object x_obj;
  double x_phase;

  t_float value, prevalue, ivalue, incr, cycle, length, pflag, nextreset;
  //  t_float count16, count24;
  float x_conv; /* 1 / the sample rate */
  float x_f;      /* scalar frequency */
  float looping;

  t_outlet *x_bar, *x_8a, *x_8b, *x_16, *x_24, *evens;

  int semiquavers, sqtriplets;
} t_phasorbars;

t_int *phasorbars_perform(t_int *w)
{
    t_phasorbars *x = (t_phasorbars *)(w[1]);
    t_float *in = (t_float *)(w[2]);
    t_float *out = (t_float *)(w[3]);
    int n = (int)(w[4]);
    double dphase = x->x_phase + UNITBIT32;
    union tabfudge tf;
    int normhipart;

    float conv = x->x_conv;

    tf.tf_d = UNITBIT32;
    normhipart = tf.tf_i[HIOFFSET];
    tf.tf_d = dphase;
    
    while (n--)
    {
      tf.tf_i[HIOFFSET] = normhipart;;
      x->ivalue = *in++;
      dphase += x->ivalue * conv; //increment
      x->value = tf.tf_d - UNITBIT32;
      if(x->pflag) {
	if(x->value < x->prevalue) {
	  if(x->ivalue > 0) {
	    if(x->nextreset >= 0) {
	      x->cycle = x->nextreset;
	      outlet_float(x->x_bar, x->cycle);
	      x->nextreset = -1;
	    } else {
	      x->cycle++;
	      if(x->cycle >= x->length) x->cycle = 0;
	      outlet_float(x->x_bar, x->cycle);
	    }
	  }
	}
	else if(x->ivalue < 0) {
	  if(x->value > x->prevalue) {
	    if(x->nextreset >= 0) {
	      x->cycle = x->nextreset;
	      outlet_float(x->x_bar, x->cycle);
	      x->nextreset = -1;
	    } else {
	      x->cycle++;
	      if(x->cycle >= x->length) x->cycle = 0;
	      outlet_float(x->x_bar, x->cycle);
	    }
	  }
	}
      }
      x->pflag = 1;
      *out++ = x->value + x->cycle;
      // here are the CLOCKS
      // and you can use the EVENS outlet to make SWING happen
      int squs = (int)(x->value * 16);
      int squts = (int)(x->value * 24);
      if(squs != x->semiquavers)
	{
	  int even = squs % 2;
	  int get8ths = squs * 0.5;
	  float eighths = (float)get8ths;
	  if (!even)
	    {
	      outlet_float(x->x_8b, eighths);
	      outlet_float(x->x_8a, eighths);
	      outlet_float(x->evens, 0);
	    }
	  else
	    {
	      outlet_float(x->evens, 1);
	    }
	  outlet_float(x->x_16, (t_float)squs);
	}
      if(squts != x->sqtriplets)
	{
	  outlet_float(x->x_24, (float)squts);
	}
      x->semiquavers = squs;
      x->sqtriplets = squts;
      tf.tf_d = dphase;
      x->prevalue = x->value;
    } 
    tf.tf_i[HIOFFSET] = normhipart;
    x->x_phase = tf.tf_d - UNITBIT32;
    return (w+5);
}

 void phasorbars_dsp(t_phasorbars *y, t_signal **sp)
{
  y->x_conv = 1./sp[0]->s_sr;
  dsp_add(phasorbars_perform, 4, y, sp[0]->s_vec, sp[1]->s_vec, sp[0]->s_n);
}

void phasorbars_debug(t_phasorbars *y)
{
  post("phase = %f, cycles = %f",y->x_phase,y->cycle);
}

void phasorbars_nextbar(t_phasorbars *y, float f)
{
  y->nextreset = (int)f;
}

void phasorbars_ft1(t_phasorbars *y, float f)
{
    float fcycles = 0;
    int cycles = 0;
    if (f < 0) f = 0;
    cycles = (int)f;
    fcycles = (t_float)cycles;
    f = f - fcycles;
    y->x_phase = f;
    y->cycle = fcycles;
    y->pflag = 0;

    outlet_float(y->x_bar, fcycles);
    post("phase = %f, cycles = %f",y->x_phase,fcycles);
}

void phasorbars_loop(t_phasorbars *y, t_float f)
{
  y->loop = f;
}

void phasorbars_length(t_phasorbars *y, t_float f)
{
  y->length = f;
}

void *phasorbars_new(t_symbol *s,int argc,t_atom* argv)
{
    t_phasorbars *y = (t_phasorbars *)pd_new(phasorbars_class);
    
    y->x_f = 0;
    if (argc) y->x_f = atom_getfloat(argv++),argc--;
    
    inlet_new(&y->x_obj, &y->x_obj.ob_pd, &s_float, gensym("ft1"));
    
    y->x_phase = 0;
    y->x_conv = 0;
    y->cycle = 0;
    y->pflag = 1;
    y->nextreset = -1;
    y->semiquavers = -1;
    y->sqtriplets = -1;

    y->ivalue = 10;
    y->value = -1;
    y->prevalue = -2;
    y->incr = 0;
    y->length = 8;
   
    outlet_new(&y->x_obj, gensym("signal"));
    
    inlet_new(&y->x_obj, &y->x_obj.ob_pd, &s_float, gensym("loop"));
    y->loop = 0;
    
    if (argc) y->loop = atom_getfloat(argv++),argc--;
    if (argc) y->length = atom_getfloat(argv++),argc--;
     
    //  y->state = 0;

    y->x_24 = outlet_new(&y->x_obj, &s_float);
    y->x_16 = outlet_new(&y->x_obj, &s_float);
    y->evens = outlet_new(&y->x_obj, &s_float);
    y->x_8a = outlet_new(&y->x_obj, &s_float);
    y->x_8b = outlet_new(&y->x_obj, &s_float);
    y->x_bar = outlet_new(&y->x_obj, &s_float);
    inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("float"), gensym("setbar"));
    inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("float"), gensym("resetbar"));

    return (y);
}


/*void phasorbars_free(t_phasorbars *x)
{
    clock_free(x->x_16ths);
    clock_free(x->x_24ths);
    }*/


 void phasorbars_tilde_setup(void)
{
    phasorbars_class = class_new(gensym("phasorbars~"), (t_newmethod)phasorbars_new, 0, sizeof(t_phasorbars), 0, A_GIMME, 0);
    CLASS_MAINSIGNALIN(phasorbars_class, t_phasorbars, x_f);
    class_addmethod(phasorbars_class, (t_method)phasorbars_dsp, gensym("dsp"), 0);
    class_addmethod(phasorbars_class, (t_method)phasorbars_ft1,
        gensym("ft1"), A_FLOAT, 0);
    //    class_addmethod(phasorbars_class, (t_method)phasorbars_blimit,
    //        gensym("blimit"), A_FLOAT, 0);
    class_addmethod(phasorbars_class, (t_method)phasorbars_loop,
        gensym("loop"), A_FLOAT, 0);
    class_addmethod(phasorbars_class, (t_method)phasorbars_length,
        gensym("length"), A_FLOAT, 0);
    class_addmethod(phasorbars_class, (t_method)phasorbars_debug,
        gensym("debug"), A_FLOAT, 0);
    class_addmethod(phasorbars_class, (t_method)phasorbars_nextbar,
        gensym("nextbar"), A_FLOAT, 0);
}

_______________________________________________
Pd-dev mailing list
Pd-dev@iem.at
http://lists.puredata.info/listinfo/pd-dev

Reply via email to