From e15f97c761bb69e9fba0c16ddc86ec0ead7dde03 Mon Sep 17 00:00:00 2001 From: Ed Kelly Date: Fri, 21 Jan 2011 12:16:46 +0000 Subject: bmt~ is a spectral band measurement tool svn path=/trunk/externals/ekext/; revision=14769 --- bmt~-help.pd | 281 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ bmt~.c | 157 +++++++++++++++++++++++++++++++++ 2 files changed, 438 insertions(+) create mode 100644 bmt~-help.pd create mode 100644 bmt~.c diff --git a/bmt~-help.pd b/bmt~-help.pd new file mode 100644 index 0000000..519bbfc --- /dev/null +++ b/bmt~-help.pd @@ -0,0 +1,281 @@ +#N canvas 0 0 652 582 10; +#N canvas 0 0 450 300 bmt 0; +#X obj 109 103 rfft~; +#X obj 109 255 outlet; +#X obj 150 224 outlet; +#X obj 109 71 inlet~; +#X obj 109 196 prepend set; +#X obj 150 177 prepend set; +#X obj 109 139 bmt~ 5; +#X obj 109 224 t b a; +#X obj 246 130 block~ 1024; +#X obj 232 71 inlet; +#X connect 0 0 6 0; +#X connect 0 1 6 1; +#X connect 3 0 0 0; +#X connect 4 0 7 0; +#X connect 5 0 2 0; +#X connect 6 0 4 0; +#X connect 6 1 5 0; +#X connect 7 0 1 0; +#X connect 7 1 1 0; +#X connect 9 0 6 0; +#X restore 113 137 pd bmt; +#N canvas 66 322 450 300 player 0; +#X obj 222 112 tabplay~ \$0-input; +#X obj 339 92 spigot; +#X obj 222 74 inlet; +#X obj 222 93 sel 1; +#X obj 51 63 inlet; +#X obj 51 185 soundfiler; +#X obj 51 82 openpanel; +#X msg 51 166 read -resize \$1 \$2; +#X obj 51 101 t a b; +#X obj 96 127 symbol \$0-input; +#X obj 51 147 pack s s; +#X obj 222 131 outlet~; +#X connect 0 0 11 0; +#X connect 0 1 1 0; +#X connect 1 0 0 0; +#X connect 2 0 1 1; +#X connect 2 0 3 0; +#X connect 3 0 0 0; +#X connect 4 0 6 0; +#X connect 6 0 8 0; +#X connect 7 0 5 0; +#X connect 8 0 10 0; +#X connect 8 1 9 0; +#X connect 9 0 10 1; +#X connect 10 0 7 0; +#X restore 112 115 pd player; +#N canvas 0 0 450 300 (subpatch) 0; +#X array \$0-input 666229 float 2; +#X coords 0 1 666229 -1 200 60 1; +#X restore 227 34 graph; +#X obj 112 96 bng 15 250 50 0 empty empty load 17 7 0 10 -262144 -1 +-1; +#X obj 163 96 tgl 15 0 empty empty play 17 7 0 10 -262144 -1 -1 0 1 +; +#X msg 113 190 98.4992 6.67565 4.14764 3.2092 2.85022; +#X msg 146 163 102 102 103 102 101; +#X obj 88 278 vu 12 120 empty empty -1 -8 0 10 -66577 -1 0 0; +#X obj 105 278 vu 12 120 empty empty -1 -8 0 10 -66577 -1 0 0; +#N canvas 672 68 450 300 graphic 0; +#X obj 70 52 inlet; +#X obj 70 90 unpack f f f f f; +#X obj 278 50 inlet; +#N canvas 0 0 296 300 scale 0; +#X obj 115 107 f; +#X obj 89 107 max; +#X msg 189 74 0; +#X obj 189 55 inlet; +#X obj 70 56 inlet; +#X obj 89 193 expr 100 / $f1; +#X obj 70 215 *; +#X obj 70 234 - 100; +#X obj 70 75 t f f b; +#X obj 70 253 outlet; +#X connect 0 0 1 1; +#X connect 1 0 0 1; +#X connect 1 0 5 0; +#X connect 2 0 0 0; +#X connect 3 0 2 0; +#X connect 4 0 8 0; +#X connect 5 0 6 1; +#X connect 6 0 7 0; +#X connect 7 0 9 0; +#X connect 8 0 6 0; +#X connect 8 1 1 0; +#X connect 8 2 0 0; +#X restore 21 165 pd scale; +#N canvas 0 0 296 300 scale 0; +#X obj 115 107 f; +#X obj 89 107 max; +#X msg 189 74 0; +#X obj 189 55 inlet; +#X obj 70 56 inlet; +#X obj 89 193 expr 100 / $f1; +#X obj 70 215 *; +#X obj 70 234 - 100; +#X obj 70 75 t f f b; +#X obj 70 253 outlet; +#X connect 0 0 1 1; +#X connect 1 0 0 1; +#X connect 1 0 5 0; +#X connect 2 0 0 0; +#X connect 3 0 2 0; +#X connect 4 0 8 0; +#X connect 5 0 6 1; +#X connect 6 0 7 0; +#X connect 7 0 9 0; +#X connect 8 0 6 0; +#X connect 8 1 1 0; +#X connect 8 2 0 0; +#X restore 81 165 pd scale; +#N canvas 0 0 296 300 scale 0; +#X obj 115 107 f; +#X obj 89 107 max; +#X msg 189 74 0; +#X obj 189 55 inlet; +#X obj 70 56 inlet; +#X obj 89 193 expr 100 / $f1; +#X obj 70 215 *; +#X obj 70 234 - 100; +#X obj 70 75 t f f b; +#X obj 70 253 outlet; +#X connect 0 0 1 1; +#X connect 1 0 0 1; +#X connect 1 0 5 0; +#X connect 2 0 0 0; +#X connect 3 0 2 0; +#X connect 4 0 8 0; +#X connect 5 0 6 1; +#X connect 6 0 7 0; +#X connect 7 0 9 0; +#X connect 8 0 6 0; +#X connect 8 1 1 0; +#X connect 8 2 0 0; +#X restore 141 165 pd scale; +#N canvas 0 0 296 300 scale 0; +#X obj 115 107 f; +#X obj 89 107 max; +#X msg 189 74 0; +#X obj 189 55 inlet; +#X obj 70 56 inlet; +#X obj 89 193 expr 100 / $f1; +#X obj 70 215 *; +#X obj 70 234 - 100; +#X obj 70 75 t f f b; +#X obj 70 253 outlet; +#X connect 0 0 1 1; +#X connect 1 0 0 1; +#X connect 1 0 5 0; +#X connect 2 0 0 0; +#X connect 3 0 2 0; +#X connect 4 0 8 0; +#X connect 5 0 6 1; +#X connect 6 0 7 0; +#X connect 7 0 9 0; +#X connect 8 0 6 0; +#X connect 8 1 1 0; +#X connect 8 2 0 0; +#X restore 201 165 pd scale; +#N canvas 0 0 296 300 scale 0; +#X obj 115 107 f; +#X obj 89 107 max; +#X msg 189 74 0; +#X obj 189 55 inlet; +#X obj 70 56 inlet; +#X obj 89 193 expr 100 / $f1; +#X obj 70 215 *; +#X obj 70 234 - 100; +#X obj 70 75 t f f b; +#X obj 70 253 outlet; +#X connect 0 0 1 1; +#X connect 1 0 0 1; +#X connect 1 0 5 0; +#X connect 2 0 0 0; +#X connect 3 0 2 0; +#X connect 4 0 8 0; +#X connect 5 0 6 1; +#X connect 6 0 7 0; +#X connect 7 0 9 0; +#X connect 8 0 6 0; +#X connect 8 1 1 0; +#X connect 8 2 0 0; +#X restore 261 165 pd scale; +#X obj 21 184 outlet; +#X obj 81 184 outlet; +#X obj 141 184 outlet; +#X obj 201 184 outlet; +#X obj 261 184 outlet; +#X floatatom 68 145 5 0 0 0 - - -; +#X text 20 213 This will rescale the inputs so that they automatically +span the range -100 to 0 Since bass frequencies are usually louder +than treble \, this is useful in order to make the bands consistent +with one another in their output.; +#X connect 0 0 1 0; +#X connect 1 0 3 0; +#X connect 1 0 13 0; +#X connect 1 1 4 0; +#X connect 1 2 5 0; +#X connect 1 3 6 0; +#X connect 1 4 7 0; +#X connect 2 0 7 1; +#X connect 2 0 6 1; +#X connect 2 0 5 1; +#X connect 2 0 4 1; +#X connect 2 0 3 1; +#X connect 3 0 8 0; +#X connect 4 0 9 0; +#X connect 5 0 10 0; +#X connect 6 0 11 0; +#X connect 7 0 12 0; +#X restore 87 254 pd graphic; +#X obj 144 213 bng 32 250 50 0 empty empty reset 35 16 0 10 -262144 +-1 -1; +#X obj 122 278 vu 12 120 empty empty -1 -8 0 10 -66577 -1 0 0; +#X obj 139 278 vu 12 120 empty empty -1 -8 0 10 -66577 -1 0 0; +#X obj 156 278 vu 12 120 empty empty -1 -8 0 10 -66577 -1 0 0; +#X obj 48 143 dac~; +#X text 400 192 <--- alpha (magnitude) of loudest bin; +#X obj 225 284 cnv 15 400 80 empty empty BassMidTreble_(bmt~) 50 12 +0 18 -233017 -65745 0; +#X text 160 139 <--- [rfft~] and [bmt~] are in here!; +#X obj 231 289 bmt~; +#X obj 223 103 tgl 15 0 empty empty empty 17 7 0 10 -262144 -1 -1 0 +1; +#X msg 223 121 mode \$1; +#X obj 73 498 s bass; +#X obj 99 534 s bassmid; +#X obj 120 507 s mid; +#X obj 223 501 s highmid; +#X obj 163 486 s high; +#X obj 53 444 mavg 10; +#X obj 99 414 mavg 10; +#X obj 121 436 mavg 10; +#X obj 171 433 mavg 10; +#X obj 236 430 mavg 10; +#X text 278 104 <--- mode 0 is maximum magnitude bin in the band \, +mode 1 is sum of all fft bins in the band.; +#X msg 52 43 \; pd dsp 1; +#X text 237 309 Splits a spectral analysis [rfft~] into n bands \, +where n is the creation argument (default 3 \, range 2-128). Reports +the maximum bin magnitude and its corresponding bin number for each +band.; +#X obj 49 89 adc~; +#X obj 49 109 *~ 0; +#X obj 80 89 tgl 15 0 empty empty empty 17 7 0 10 -262144 -1 -1 0 1 +; +#X text 270 163 <--- bin offset in band of loudest (highest magnitude) +bin; +#X text 290 433 <--- smoothing (optional); +#X connect 0 0 5 0; +#X connect 0 1 6 0; +#X connect 1 0 0 0; +#X connect 1 0 14 0; +#X connect 1 0 14 1; +#X connect 3 0 1 0; +#X connect 4 0 1 1; +#X connect 5 0 9 0; +#X connect 7 0 26 0; +#X connect 8 0 27 0; +#X connect 9 0 7 0; +#X connect 9 1 8 0; +#X connect 9 2 11 0; +#X connect 9 3 12 0; +#X connect 9 4 13 0; +#X connect 10 0 9 1; +#X connect 11 0 28 0; +#X connect 12 0 29 0; +#X connect 13 0 30 0; +#X connect 19 0 20 0; +#X connect 20 0 0 1; +#X connect 26 0 21 0; +#X connect 27 0 22 0; +#X connect 28 0 23 0; +#X connect 29 0 25 0; +#X connect 30 0 24 0; +#X connect 34 0 35 0; +#X connect 35 0 0 0; +#X connect 36 0 35 1; diff --git a/bmt~.c b/bmt~.c new file mode 100644 index 0000000..5b9498d --- /dev/null +++ b/bmt~.c @@ -0,0 +1,157 @@ +/* + * bmt~ : bassmidtreble, according to threshold + * Copyright (C) 2010 Edward Kelly + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "m_pd.h" +#include + +#ifdef NT +#pragma warning( disable : 4244 ) +#pragma warning( disable : 4305 ) +#endif + +static t_class *bmt_tilde_class; + +typedef struct _bmt_tilde +{ + t_object x_obj; + t_atom levels[128]; // a ridiculous number of bands!!! + t_atom binmost[128]; + // t_atom xovers[127]; // and their crossover band points. + // Make this so the bands can be log! or pow x! + + t_float f; + t_float mode; + t_float length; // how many do we have? + t_outlet *bmt, *bins; +} t_bmt_tilde; + +t_int *bmt_tilde_perform(t_int *w) +{ + t_bmt_tilde *x = (t_bmt_tilde *)(w[1]); + t_sample *real = (t_sample *)(w[2]); + t_sample *imag = (t_sample *)(w[3]); + int n = (int)(w[4]); + float incr = 0; + float max = 0; + float vectorr, vectori; + float alpha; + int n_real = n / 2; + float bsize = n_real / x->length; + float block = 0; + float reblock = 0; + int ilength = (int)(x->length); + int iblock = 0; + + while (n--) + { + vectorr = (*real++); + vectori = (*imag++); + if (n > n_real) + { + if (x->mode == 0) + { + reblock = block; + alpha = sqrt((vectorr * vectorr) + (vectori * vectori)); + block = (int)(incr / bsize); + iblock = (int)(block); + if (block != reblock) + { + max = alpha; + } + else if (alpha > max) + { + max = alpha; + SETFLOAT(&x->levels[iblock], max); + SETFLOAT(&x->binmost[iblock], incr-((int)(block*bsize))); + } + } + else if (x->mode == 1) + { + reblock = block; + alpha = sqrt((vectorr * vectorr) + (vectori * vectori)); + block = (int)(incr / bsize); + iblock = (int)(block); + if (block != reblock) + { + max = alpha; + } + else if (block == reblock) + { + max += alpha; + SETFLOAT(&x->levels[iblock], max); + SETFLOAT(&x->binmost[iblock], incr-((int)(block*bsize))); + } + } + } + incr++; + } + outlet_list(x->bins, gensym("list"), ilength, x->binmost); + outlet_list(x->bmt, gensym("list"), ilength, x->levels); + //outlet_list(x->f_levs, (float)max); + //outlet_list(x->f_bmt, x->f_topbin); + + return(w+5); +} + +void bmt_tilde_mode(t_bmt_tilde *x, t_floatarg n) +{ + x->mode = n; +} + +void bmt_tilde_dsp(t_bmt_tilde *x, t_signal **sp) +{ + dsp_add(bmt_tilde_perform, 4, x, + sp[0]->s_vec, sp[1]->s_vec, sp[0]->s_n); +} + +void *bmt_tilde_new(t_floatarg nbands) +{ + t_bmt_tilde *x = (t_bmt_tilde *)pd_new(bmt_tilde_class); + + x->length = nbands > 1 ? nbands > 128 ? 128 : nbands : 3; + // set min max nbands + inlet_new (&x->x_obj, &x->x_obj.ob_pd, &s_signal, &s_signal); + // floatinlet_new (&x->x_obj, &x->f_thresh); + int i; + x->mode = 0; //make this a creation arg...Ed + for(i=0;i<128;i++) + { + SETFLOAT(&x->levels[i], 0); + SETFLOAT(&x->binmost[i], 0); + } + x->bmt = outlet_new(&x->x_obj, gensym("list")); + x->bins = outlet_new(&x->x_obj, gensym("list")); + return (void *)x; +} + +void bmt_tilde_setup(void) +{ + bmt_tilde_class = class_new(gensym("bmt~"), + (t_newmethod)bmt_tilde_new, + 0, sizeof(t_bmt_tilde), + CLASS_DEFAULT, A_DEFFLOAT, 0); + + post("|=======bmt~========|"); + post("|=bass==mid==treble=|"); + post("|=ed==kelly===2010==|"); + + class_addmethod(bmt_tilde_class, (t_method)bmt_tilde_dsp, + gensym("dsp"), 0); + class_addmethod(bmt_tilde_class, (t_method)bmt_tilde_mode, gensym("mode"), A_DEFFLOAT, 0); + CLASS_MAINSIGNALIN(bmt_tilde_class, t_bmt_tilde, f); +} -- cgit v1.2.1