| 1 |
/* ------------------------ randomblock~ -------------------------------------- */ |
| 2 |
/* */ |
| 3 |
/* Generates a random signal block */ |
| 4 |
/* Written by Yves Degoyon ([email protected]). */ |
| 5 |
/* */ |
| 6 |
/* This program is free software; you can redistribute it and/or */ |
| 7 |
/* modify it under the terms of the GNU General Public License */ |
| 8 |
/* as published by the Free Software Foundation; either version 2 */ |
| 9 |
/* of the License, or (at your option) any later version. */ |
| 10 |
/* */ |
| 11 |
/* See file LICENSE for further informations on licensing terms. */ |
| 12 |
/* */ |
| 13 |
/* This program is distributed in the hope that it will be useful, */ |
| 14 |
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */ |
| 15 |
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ |
| 16 |
/* GNU General Public License for more details. */ |
| 17 |
/* */ |
| 18 |
/* You should have received a copy of the GNU General Public License */ |
| 19 |
/* along with this program; if not, write to the Free Software */ |
| 20 |
/* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
| 21 |
/* */ |
| 22 |
/* Based on PureData by Miller Puckette and others. */ |
| 23 |
/* */ |
| 24 |
/* ---------------------------------------------------------------------------- */ |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
#include <sys/types.h> |
| 29 |
#include <string.h> |
| 30 |
#include <stdio.h> |
| 31 |
#include <errno.h> |
| 32 |
#include <stdlib.h> |
| 33 |
#include <fcntl.h> |
| 34 |
#include <sys/stat.h> |
| 35 |
#ifndef __APPLE__ |
| 36 |
#include <malloc.h> |
| 37 |
#endif |
| 38 |
#include <ctype.h> |
| 39 |
#include <unistd.h> |
| 40 |
#include <time.h> |
| 41 |
#include <sys/time.h> |
| 42 |
|
| 43 |
#include "m_pd.h" /* standard pd stuff */ |
| 44 |
|
| 45 |
static char *randomblock_version = "randomblock~: generates a random audio block : author : [email protected]"; |
| 46 |
|
| 47 |
static t_class *randomblock_class; |
| 48 |
|
| 49 |
typedef struct _randomblock |
| 50 |
{ |
| 51 |
t_object x_obj; |
| 52 |
t_int x_limit; |
| 53 |
} t_randomblock; |
| 54 |
|
| 55 |
/* clean up */ |
| 56 |
static void randomblock_free(t_randomblock *x) |
| 57 |
{ |
| 58 |
} |
| 59 |
|
| 60 |
static void *randomblock_new(t_float flimit) |
| 61 |
{ |
| 62 |
t_randomblock *x = (t_randomblock *)pd_new(randomblock_class); |
| 63 |
outlet_new(&x->x_obj, &s_signal); |
| 64 |
inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("limit")); |
| 65 |
if ( flimit <= 0 || flimit > RAND_MAX ) |
| 66 |
{ |
| 67 |
post( "randomblock~: wrong creation argument" ); |
| 68 |
return NULL; |
| 69 |
} |
| 70 |
x->x_limit = (int) flimit; |
| 71 |
return(x); |
| 72 |
} |
| 73 |
|
| 74 |
static void *randomblock_limit(t_randomblock* x, t_float flimit) |
| 75 |
{ |
| 76 |
if ( flimit < 0 || flimit > RAND_MAX ) |
| 77 |
{ |
| 78 |
post( "randomblock~: wrong random limit" ); |
| 79 |
return; |
| 80 |
} |
| 81 |
else |
| 82 |
{ |
| 83 |
x->x_limit=(int)flimit; |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
static t_int *randomblock_perform(t_int *w) |
| 88 |
{ |
| 89 |
t_float *out = (t_float*) w[1]; |
| 90 |
int n = (int)(w[2]); |
| 91 |
t_randomblock *x = (t_randomblock*) w[3]; |
| 92 |
|
| 93 |
int rvalue = rand(); |
| 94 |
// post("random value : %d", rvalue ); |
| 95 |
rvalue = rvalue%(x->x_limit-n); |
| 96 |
// post("modulated by %d : %d", (x->x_limit-n), rvalue ); |
| 97 |
if ( rvalue < 0 ) rvalue=0; |
| 98 |
|
| 99 |
while (n--) |
| 100 |
{ |
| 101 |
*(out)++=(float)rvalue++; |
| 102 |
} |
| 103 |
|
| 104 |
return (w+4); |
| 105 |
} |
| 106 |
|
| 107 |
static void randomblock_dsp(t_randomblock *x, t_signal **sp) |
| 108 |
{ |
| 109 |
dsp_add( randomblock_perform, 3, sp[0]->s_vec, sp[0]->s_n, x ) ; |
| 110 |
} |
| 111 |
|
| 112 |
void randomblock_tilde_setup(void) |
| 113 |
{ |
| 114 |
logpost(NULL, 4, randomblock_version); |
| 115 |
randomblock_class = class_new(gensym("randomblock~"), (t_newmethod)randomblock_new, |
| 116 |
(t_method)randomblock_free, |
| 117 |
sizeof(t_randomblock), 0, A_DEFFLOAT, 0); |
| 118 |
class_addmethod( randomblock_class, (t_method)randomblock_dsp, gensym("dsp"), 0); |
| 119 |
class_addmethod( randomblock_class, (t_method)randomblock_limit, gensym("limit"), A_FLOAT, 0); |
| 120 |
} |