| 1 |
/* Copyright (c) 2002 Yves Degoyon. */ |
| 2 |
/* For information on usage and redistribution, and for a DISCLAIMER OF ALL */ |
| 3 |
/* WARRANTIES, see the file, "COPYING" in this distribution. */ |
| 4 |
/* */ |
| 5 |
/* beatify~ -- modulates an audio signal amplitude, */ |
| 6 |
/* making it sounding like drums (sometimes) */ |
| 7 |
/* */ |
| 8 |
/* This program is free software; you can redistribute it and/or */ |
| 9 |
/* modify it under the terms of the GNU General Public License */ |
| 10 |
/* as published by the Free Software Foundation; either version 2 */ |
| 11 |
/* of the License, or (at your option) any later version. */ |
| 12 |
/* */ |
| 13 |
/* See file LICENSE for further informations on licensing terms. */ |
| 14 |
/* */ |
| 15 |
/* This program is distributed in the hope that it will be useful, */ |
| 16 |
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */ |
| 17 |
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ |
| 18 |
/* GNU General Public License for more details. */ |
| 19 |
/* */ |
| 20 |
/* You should have received a copy of the GNU General Public License */ |
| 21 |
/* along with this program; if not, write to the Free Software */ |
| 22 |
/* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
| 23 |
/* */ |
| 24 |
/* Based on PureData by Miller Puckette and others. */ |
| 25 |
/* */ |
| 26 |
/* Made while listening to : */ |
| 27 |
/* */ |
| 28 |
/* "We wanted succes" */ |
| 29 |
/* "We got lost into the night" */ |
| 30 |
/* "Take life as it comes" */ |
| 31 |
/* PJ Harvey -- We Float */ |
| 32 |
/* ---------------------------------------------------------------------------- */ |
| 33 |
|
| 34 |
#include "m_pd.h" |
| 35 |
#include <stdlib.h> |
| 36 |
#include <math.h> |
| 37 |
|
| 38 |
static char *beatify_version = "beatify~: an audio beatify, version 0.1 ([email protected])"; |
| 39 |
|
| 40 |
typedef struct _beatify |
| 41 |
{ |
| 42 |
t_object x_obj; |
| 43 |
t_float x_attack; |
| 44 |
t_float x_sustain; |
| 45 |
t_float x_decay; |
| 46 |
t_float x_size; |
| 47 |
t_float x_gamplitude; |
| 48 |
t_float x_vol; |
| 49 |
t_int x_current; |
| 50 |
t_float x_f; |
| 51 |
} t_beatify; |
| 52 |
|
| 53 |
static t_class *beatify_class; |
| 54 |
|
| 55 |
static void beatify_attack(t_beatify *x, t_floatarg fattack ) |
| 56 |
{ |
| 57 |
if (fattack < 1.0) |
| 58 |
{ |
| 59 |
x->x_attack = 1.0; |
| 60 |
} |
| 61 |
else |
| 62 |
{ |
| 63 |
x->x_attack = fattack; |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
static void beatify_sustain(t_beatify *x, t_floatarg fsustain ) |
| 68 |
{ |
| 69 |
if (fsustain < 0.0) |
| 70 |
{ |
| 71 |
x->x_sustain = 0.0; |
| 72 |
} |
| 73 |
else |
| 74 |
{ |
| 75 |
x->x_sustain = fsustain; |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
static void beatify_decay(t_beatify *x, t_floatarg fdecay ) |
| 80 |
{ |
| 81 |
if (fdecay < 1.0) |
| 82 |
{ |
| 83 |
x->x_decay = 1.0; |
| 84 |
} |
| 85 |
else |
| 86 |
{ |
| 87 |
x->x_decay = fdecay; |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
static void beatify_size(t_beatify *x, t_floatarg fsize ) |
| 92 |
{ |
| 93 |
if (fsize < 100.0) |
| 94 |
{ |
| 95 |
x->x_size = 100.0; |
| 96 |
} |
| 97 |
else |
| 98 |
{ |
| 99 |
x->x_size = fsize; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
static void beatify_gamplitude(t_beatify *x, t_floatarg fgamplitude ) |
| 104 |
{ |
| 105 |
if (fgamplitude < 0.0) |
| 106 |
{ |
| 107 |
x->x_gamplitude = 0.0; |
| 108 |
} |
| 109 |
else if (fgamplitude > 1.0) |
| 110 |
{ |
| 111 |
x->x_gamplitude = 1.0; |
| 112 |
} |
| 113 |
else |
| 114 |
{ |
| 115 |
x->x_gamplitude = fgamplitude; |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
static void *beatify_new(void) |
| 120 |
{ |
| 121 |
t_beatify *x = (t_beatify *)pd_new(beatify_class); |
| 122 |
inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("attack")); |
| 123 |
inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("sustain")); |
| 124 |
inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("decay")); |
| 125 |
inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("size")); |
| 126 |
inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("gamplitude")); |
| 127 |
x->x_attack = 10; |
| 128 |
x->x_decay = 50; |
| 129 |
x->x_sustain = 2; |
| 130 |
x->x_size = 700; |
| 131 |
x->x_gamplitude = 0.5; |
| 132 |
x->x_current = x->x_size; |
| 133 |
outlet_new(&x->x_obj, &s_signal); |
| 134 |
return (x); |
| 135 |
} |
| 136 |
|
| 137 |
static t_int *beatify_perform(t_int *w) |
| 138 |
{ |
| 139 |
t_float *in = (t_float *)(w[1]); |
| 140 |
t_float *out = (t_float *)(w[2]); |
| 141 |
t_int n = (int)(w[3]); |
| 142 |
t_beatify *x = (t_beatify*)(w[4]); |
| 143 |
t_float adelta = 0., ddelta = 0.; |
| 144 |
|
| 145 |
while (n--) |
| 146 |
{ |
| 147 |
if ( x->x_current>=x->x_size ) |
| 148 |
{ |
| 149 |
adelta = (x->x_gamplitude-x->x_vol)/x->x_attack; |
| 150 |
ddelta = x->x_gamplitude/x->x_decay; |
| 151 |
x->x_current = 0; |
| 152 |
} |
| 153 |
if ( x->x_current<x->x_attack ) x->x_vol+= adelta; |
| 154 |
if ( x->x_current>x->x_attack+x->x_sustain && x->x_current<x->x_attack+x->x_sustain+x->x_decay ) |
| 155 |
{ |
| 156 |
x->x_vol-= ddelta; |
| 157 |
} |
| 158 |
x->x_current++; |
| 159 |
|
| 160 |
*(out) = *(in)*x->x_vol; |
| 161 |
out++; |
| 162 |
in++; |
| 163 |
} |
| 164 |
return (w+5); |
| 165 |
} |
| 166 |
|
| 167 |
static void beatify_dsp(t_beatify *x, t_signal **sp) |
| 168 |
{ |
| 169 |
dsp_add(beatify_perform, 4, sp[0]->s_vec, sp[1]->s_vec, sp[0]->s_n, x ); |
| 170 |
} |
| 171 |
|
| 172 |
void beatify_tilde_setup(void) |
| 173 |
{ |
| 174 |
logpost(NULL, 4, beatify_version ); |
| 175 |
beatify_class = class_new(gensym("beatify~"), (t_newmethod)beatify_new, 0, |
| 176 |
sizeof(t_beatify), 0, 0); |
| 177 |
CLASS_MAINSIGNALIN( beatify_class, t_beatify, x_f ); |
| 178 |
class_addmethod(beatify_class, (t_method)beatify_dsp, gensym("dsp"), 0); |
| 179 |
class_addmethod(beatify_class, (t_method)beatify_attack, gensym("attack"), A_FLOAT, 0); |
| 180 |
class_addmethod(beatify_class, (t_method)beatify_sustain, gensym("sustain"), A_FLOAT, 0); |
| 181 |
class_addmethod(beatify_class, (t_method)beatify_decay, gensym("decay"), A_FLOAT, 0); |
| 182 |
class_addmethod(beatify_class, (t_method)beatify_size, gensym("size"), A_FLOAT, 0); |
| 183 |
class_addmethod(beatify_class, (t_method)beatify_gamplitude, gensym("gamplitude"), A_FLOAT, 0); |
| 184 |
} |