Debian Codegraph @

src/core/uana_extract.cpp

raw symbols SCIP
1 /*----------------------------------------------------------------------------
2 ChucK Strongly-timed Audio Programming Language
3 Compiler, Virtual Machine, and Synthesis Engine
4
5 Copyright (c) 2003 Ge Wang and Perry R. Cook. All rights reserved.
6 http://chuck.stanford.edu/
7 http://chuck.cs.princeton.edu/
8
9 This program is free software; you can redistribute it and/or modify
10 it under the dual-license terms of EITHER the MIT License OR the GNU
11 General Public License (the latter as published by the Free Software
12 Foundation; either version 2 of the License or, at your option, any
13 later version).
14
15 This program is distributed in the hope that it will be useful and/or
16 interesting, but WITHOUT ANY WARRANTY; without even the implied warranty
17 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 MIT Licence and/or the GNU General Public License for details.
19
20 You should have received a copy of the MIT License and the GNU General
21 Public License (GPL) along with this program; a copy of the GPL can also
22 be obtained by writing to the Free Software Foundation, Inc., 59 Temple
23 Place, Suite 330, Boston, MA 02111-1307 U.S.A.
24 -----------------------------------------------------------------------------*/
25
26 //-----------------------------------------------------------------------------
27 // file: uana_extract.cpp
28 // desc: unit analyzer extract
29 //
30 // author: Ge Wang ([email protected] | [email protected])
31 // Rebecca Fiebrink ([email protected])
32 // date: Summer 2007
33 //-----------------------------------------------------------------------------
34 #include "uana_extract.h"
35 #include "chuck_type.h"
36 #include "chuck_vm.h"
37 #include "chuck_compile.h"
38 #include "chuck_instr.h"
39 #include "chuck_lang.h"
40 #include "chuck_errmsg.h"
41 #include "util_math.h"
42 #include "util_xforms.h"
43 #include <iostream>
44
45
46
47
48 // Centroid
49 CK_DLL_TICK( Centroid_tick );
50 CK_DLL_TOCK( Centroid_tock );
51 CK_DLL_PMSG( Centroid_pmsg );
52 CK_DLL_SFUN( Centroid_compute );
53
54 // Flux
55 CK_DLL_CTOR( Flux_ctor );
56 CK_DLL_DTOR( Flux_dtor );
57 CK_DLL_TICK( Flux_tick );
58 CK_DLL_TOCK( Flux_tock );
59 CK_DLL_PMSG( Flux_pmsg );
60 CK_DLL_MFUN( Flux_ctrl_reset );
61 CK_DLL_SFUN( Flux_compute );
62 CK_DLL_SFUN( Flux_compute2 );
63 // offset
64 static t_CKUINT Flux_offset_data = 0;
65
66 // RMS
67 CK_DLL_TICK( RMS_tick );
68 CK_DLL_TOCK( RMS_tock );
69 CK_DLL_PMSG( RMS_pmsg );
70 CK_DLL_SFUN( RMS_compute );
71
72 // MFCC | 1.4.2.0 (yikai) added
73 CK_DLL_CTOR( MFCC_ctor );
74 CK_DLL_DTOR( MFCC_dtor );
75 CK_DLL_TICK( MFCC_tick );
76 CK_DLL_TOCK( MFCC_tock );
77 CK_DLL_PMSG( MFCC_pmsg );
78 CK_DLL_MFUN( MFCC_compute );
79 CK_DLL_MFUN( MFCC_ctrl_sample_rate );
80 CK_DLL_MFUN( MFCC_cget_sample_rate );
81 CK_DLL_MFUN( MFCC_ctrl_num_filters );
82 CK_DLL_MFUN( MFCC_cget_num_filters );
83 CK_DLL_MFUN( MFCC_ctrl_num_coeffs );
84 CK_DLL_MFUN( MFCC_cget_num_coeffs );
85 // offset
86 static t_CKUINT MFCC_offset_data = 0;
87
88 // Kurtosis
89 CK_DLL_TICK( Kurtosis_tick );
90 CK_DLL_TOCK( Kurtosis_tock );
91 CK_DLL_PMSG( Kurtosis_pmsg );
92 CK_DLL_SFUN( Kurtosis_compute );
93
94 // SFM | 1.5.0.0 (yikai) added
95 CK_DLL_CTOR( SFM_ctor );
96 CK_DLL_DTOR( SFM_dtor );
97 CK_DLL_TICK( SFM_tick );
98 CK_DLL_TOCK( SFM_tock );
99 CK_DLL_PMSG( SFM_pmsg );
100 CK_DLL_MFUN( SFM_compute );
101 CK_DLL_MFUN( SFM_ctrl_nr_bands );
102 CK_DLL_MFUN( SFM_cget_nr_bands );
103 // offset
104 static t_CKUINT SFM_offset_data = 0;
105
106
107 // Chroma | 1.5.0.0 (yikai) added
108 CK_DLL_CTOR( Chroma_ctor );
109 CK_DLL_DTOR( Chroma_dtor );
110 CK_DLL_TICK( Chroma_tick );
111 CK_DLL_TOCK( Chroma_tock );
112 CK_DLL_PMSG( Chroma_pmsg );
113 CK_DLL_MFUN( Chroma_compute );
114 CK_DLL_MFUN( Chroma_ctrl_sample_rate );
115 CK_DLL_MFUN( Chroma_cget_sample_rate );
116 CK_DLL_MFUN( Chroma_ctrl_low_oct_num );
117 CK_DLL_MFUN( Chroma_cget_low_oct_num );
118 CK_DLL_MFUN( Chroma_ctrl_high_oct_num );
119 CK_DLL_MFUN( Chroma_cget_high_oct_num );
120 // offset
121 static t_CKUINT Chroma_offset_data = 0;
122
123 // RollOff
124 CK_DLL_CTOR( RollOff_ctor );
125 CK_DLL_DTOR( RollOff_dtor );
126 CK_DLL_TICK( RollOff_tick );
127 CK_DLL_TOCK( RollOff_tock );
128 CK_DLL_PMSG( RollOff_pmsg );
129 CK_DLL_SFUN( RollOff_compute );
130 CK_DLL_MFUN( RollOff_ctrl_percent );
131 CK_DLL_MFUN( RollOff_cget_percent );
132 // offset
133 static t_CKUINT RollOff_offset_percent = 0;
134
135 // Feature Collector
136 CK_DLL_TICK( FeatureCollector_tick );
137 CK_DLL_TOCK( FeatureCollector_tock );
138 CK_DLL_PMSG( FeatureCollector_pmsg );
139
140 // AutoCorr
141 CK_DLL_CTOR( AutoCorr_ctor );
142 CK_DLL_DTOR( AutoCorr_dtor );
143 CK_DLL_TICK( AutoCorr_tick );
144 CK_DLL_TOCK( AutoCorr_tock );
145 CK_DLL_PMSG( AutoCorr_pmsg );
146 CK_DLL_SFUN( AutoCorr_compute );
147 CK_DLL_MFUN( AutoCorr_ctrl_normalize );
148 CK_DLL_MFUN( AutoCorr_cget_normalize );
149 // offset
150 static t_CKUINT AutoCorr_offset_data = 0;
151
152 // XCorr
153 CK_DLL_CTOR( XCorr_ctor );
154 CK_DLL_DTOR( XCorr_dtor );
155 CK_DLL_TICK( XCorr_tick );
156 CK_DLL_TOCK( XCorr_tock );
157 CK_DLL_PMSG( XCorr_pmsg );
158 CK_DLL_SFUN( XCorr_compute );
159 CK_DLL_MFUN( XCorr_ctrl_normalize );
160 CK_DLL_MFUN( XCorr_cget_normalize );
161 // offset
162 static t_CKUINT XCorr_offset_data = 0;
163
164 // ZeroX
165 CK_DLL_CTOR( ZeroX_ctor );
166 CK_DLL_DTOR( ZeroX_dtor );
167 CK_DLL_TICK( ZeroX_tick );
168 CK_DLL_TOCK( ZeroX_tock );
169 CK_DLL_PMSG( ZeroX_pmsg );
170 CK_DLL_SFUN( ZeroX_compute );
171 // offset
172 static t_CKUINT ZeroX_offset_data = 0;
173
174 // LPC
175 CK_DLL_CTOR( LPC_ctor );
176 CK_DLL_DTOR( LPC_dtor );
177 CK_DLL_TICK( LPC_tick );
178 CK_DLL_TOCK( LPC_tock );
179 CK_DLL_PMSG( LPC_pmsg );
180 CK_DLL_SFUN( LPC_compute );
181 CK_DLL_MFUN( LPC_ctrl_pitch );
182 CK_DLL_MFUN( LPC_cget_pitch );
183 CK_DLL_MFUN( LPC_ctrl_power );
184 CK_DLL_MFUN( LPC_cget_power );
185 CK_DLL_MFUN( LPC_ctrl_coefs );
186 CK_DLL_MFUN( LPC_cget_coefs );
187 // offset
188 //static t_CKUINT LPC_offset_data = 0;
189
190
191 // utility functions
192 void xcorr_fft( SAMPLE * f, t_CKINT fs, SAMPLE * g, t_CKINT gs, SAMPLE * buffer, t_CKINT bs );
193 void xcorr_normalize( SAMPLE * buffy, t_CKINT bs, SAMPLE * f, t_CKINT fs, SAMPLE * g, t_CKINT gs );
194
195 // 1.4.2.0 (ge) | local global sample rate variable (e.g., for MFCC)
196 static t_CKUINT g_uana_srate = 0;
197
198 //-----------------------------------------------------------------------------
199 // this is called for this module to know when sample rate changes | 1.5.4.2 (ge) added
200 //-----------------------------------------------------------------------------
201 void uana_srate_update_cb( t_CKUINT srate, void * userdata ) { g_uana_srate = srate; }
202
203
204
205
206 //-----------------------------------------------------------------------------
207 // name: extract_query()
208 // desc: ...
209 //-----------------------------------------------------------------------------
210 DLL_QUERY extract_query( Chuck_DL_Query * QUERY )
211 {
212 Chuck_Env * env = QUERY->env();
213 Chuck_DL_Func * func = NULL;
214
215 // 1.4.2.0 (ge) | store sample rate
216 g_uana_srate = QUERY->srate();
217 // register callback to be notified if/when sample rate changes | 1.5.4.2 (ge) added
218 QUERY->register_callback_on_srate_update( QUERY, uana_srate_update_cb, NULL );
219
220 std::string doc;
221
222 //---------------------------------------------------------------------
223 // init as base class: FeatureCollector
224 //---------------------------------------------------------------------
225
226 doc = "Turns UAna input into a single feature vector, upon .upchuck()";
227
228 if( !type_engine_import_uana_begin( env, "FeatureCollector", "UAna", env->global(),
229 NULL, NULL,
230 FeatureCollector_tick, FeatureCollector_tock, FeatureCollector_pmsg,
231 CK_NO_VALUE, CK_NO_VALUE, CK_NO_VALUE, CK_NO_VALUE,
232 doc.c_str() ) )
233 return FALSE;
234
235 // add examples
236 if( !type_engine_import_add_ex( env, "ai/genre-classify/feature-extract.ck" ) ) goto error;
237 if( !type_engine_import_add_ex( env, "ai/genre-classify/genre-classify.ck" ) ) goto error;
238 if( !type_engine_import_add_ex( env, "analysis/concat.ck" ) ) goto error;
239
240 // end the class import
241 type_engine_import_class_end( env );
242
243 //---------------------------------------------------------------------
244 // init as base class: Centroid
245 //---------------------------------------------------------------------
246
247 doc = "A unit analyzer that computes the spectral centroid from a magnitude spectrum (either from incoming UAna or manually given), and outputs a single number.";
248
249 if( !type_engine_import_uana_begin( env, "Centroid", "UAna", env->global(),
250 NULL, NULL,
251 Centroid_tick, Centroid_tock, Centroid_pmsg,
252 CK_NO_VALUE, CK_NO_VALUE, CK_NO_VALUE, CK_NO_VALUE,
253 doc.c_str() ) )
254 return FALSE;
255
256 // compute
257 func = make_new_sfun( "float", "compute", Centroid_compute );
258 func->add_arg( "float[]", "input" );
259 func->doc = "Manually computes the centroid from a float array.";
260 if( !type_engine_import_sfun( env, func ) ) goto error;
261
262 // add examples
263 if( !type_engine_import_add_ex( env, "ai/features/centroid.ck" ) ) goto error;
264 if( !type_engine_import_add_ex( env, "ai/genre-classify/feature-extract.ck" ) ) goto error;
265 if( !type_engine_import_add_ex( env, "ai/genre-classify/genre-classify.ck" ) ) goto error;
266
267 // end the class import
268 type_engine_import_class_end( env );
269
270 //---------------------------------------------------------------------
271 // init as base class: Flux
272 //---------------------------------------------------------------------
273
274 doc =
275 "A unit analyzer that computes the spectral flux between successive magnitude spectra (via incoming UAna, or given manually), and outputs a single number.";
276
277 if( !type_engine_import_uana_begin( env, "Flux", "UAna", env->global(),
278 Flux_ctor, Flux_dtor,
279 Flux_tick, Flux_tock, Flux_pmsg,
280 CK_NO_VALUE, CK_NO_VALUE, CK_NO_VALUE, CK_NO_VALUE,
281 doc.c_str() ) )
282 return FALSE;
283
284 // data offset
285 Flux_offset_data = type_engine_import_mvar( env, "int", "@Flux_data", FALSE );
286 if( Flux_offset_data == CK_INVALID_OFFSET ) goto error;
287
288 // compute
289 func = make_new_mfun( "void", "reset", Flux_ctrl_reset );
290 func->doc = "Reset the extractor.";
291 if( !type_engine_import_mfun( env, func ) ) goto error;
292
293 // compute
294 func = make_new_sfun( "float", "compute", Flux_compute );
295 func->add_arg( "float[]", "lhs" );
296 func->add_arg( "float[]", "rhs" );
297 func->doc = "Manually computes the flux between two frames.";
298 if( !type_engine_import_sfun( env, func ) ) goto error;
299
300 // compute2
301 func = make_new_sfun( "float", "compute", Flux_compute2 );
302 func->add_arg( "float[]", "lhs" );
303 func->add_arg( "float[]", "rhs" );
304 func->add_arg( "float[]", "diff" );
305 func->doc = "Manually computes the flux between two frames, and stores the difference in a third array.";
306 if( !type_engine_import_sfun( env, func ) ) goto error;
307
308 // add examples
309 if( !type_engine_import_add_ex( env, "ai/features/flux.ck" ) ) goto error;
310 if( !type_engine_import_add_ex( env, "ai/features/flux0.ck" ) ) goto error;
311
312 // end the class import
313 type_engine_import_class_end( env );
314
315 //---------------------------------------------------------------------
316 // init as base class: RMS
317 //---------------------------------------------------------------------
318
319 doc =
320 "A unit analyzer that computes the root-mean-square (RMS) power mean from a magnitude spectrum (either from an incoming UAna, or given manually), and outputs a single number.";
321
322 if( !type_engine_import_uana_begin( env, "RMS", "UAna", env->global(),
323 NULL, NULL,
324 RMS_tick, RMS_tock, RMS_pmsg,
325 CK_NO_VALUE, CK_NO_VALUE, CK_NO_VALUE, CK_NO_VALUE,
326 doc.c_str() ) )
327 return FALSE;
328
329 // compute
330 func = make_new_sfun( "float", "compute", RMS_compute );
331 func->add_arg( "float[]", "input" );
332 func->doc = "Manually computes the RMS from a float array.";
333 if( !type_engine_import_sfun( env, func ) ) goto error;
334
335 // add examples
336 if( !type_engine_import_add_ex( env, "ai/features/rms.ck" ) ) goto error;
337
338 // end the class import
339 type_engine_import_class_end( env );
340
341 //---------------------------------------------------------------------
342 // init as base class: MFCC
343 //---------------------------------------------------------------------
344
345 doc =
346 "A unit analyzer that computes Mel-frequency Cepstral Coefficients (MFCCs), and outputs a vector of coefficients.";
347
348 // 1.4.2.0 (yikai) | added
349 if( !type_engine_import_uana_begin( env, "MFCC", "UAna", env->global(),
350 MFCC_ctor, MFCC_dtor,
351 MFCC_tick, MFCC_tock, MFCC_pmsg,
352 CK_NO_VALUE, CK_NO_VALUE, CK_NO_VALUE, CK_NO_VALUE,
353 doc.c_str() ) )
354 return FALSE;
355
356 // data offset
357 MFCC_offset_data = type_engine_import_mvar( env, "float", "@MFCC_data", FALSE );
358 if( MFCC_offset_data == CK_INVALID_OFFSET ) goto error;
359
360 // sample rate
361 func = make_new_mfun( "int", "sampleRate", MFCC_ctrl_sample_rate );
362 func->add_arg( "int", "sr" );
363 func->doc = "Set the sample rate for MFCC analysis; NOTE: by default this is set to current ChucK sample rate.";
364 if( !type_engine_import_mfun( env, func ) ) goto error;
365
366 // sample rate
367 func = make_new_mfun( "int", "sampleRate", MFCC_cget_sample_rate );
368 func->doc = "Get the sample rate for MFCC analysis.";
369 if( !type_engine_import_mfun( env, func ) ) goto error;
370
371 // num filters
372 func = make_new_mfun( "int", "numFilters", MFCC_ctrl_num_filters );
373 func->doc = "Set the number of linearly spaced filters in MEL space.";
374 func->add_arg( "int", "n" );
375 if( !type_engine_import_mfun( env, func ) ) goto error;
376
377 // num filters
378 func = make_new_mfun( "int", "numFilters", MFCC_cget_num_filters );
379 func->doc = "Get the number of linearly spaced filters in MEL space.";
380 if( !type_engine_import_mfun( env, func ) ) goto error;
381
382 // num coeffs
383 func = make_new_mfun( "int", "numCoeffs", MFCC_ctrl_num_coeffs );
384 func->add_arg( "int", "n" );
385 func->doc = "Set the number of MFCC coefficients to compute.";
386 if( !type_engine_import_mfun( env, func ) ) goto error;
387
388 // num coeffs
389 func = make_new_mfun( "int", "numCoeffs", MFCC_cget_num_coeffs );
390 func->doc = "Get the number of MFCC coefficients to compute.";
391 if( !type_engine_import_mfun( env, func ) ) goto error;
392
393 // compute (manual alternative to calling upchuck())
394 func = make_new_mfun( "float", "compute", MFCC_compute );
395 func->add_arg( "float[]", "input" );
396 func->add_arg( "float[]", "output" );
397 func->doc =
398 "Manually computes the MFCC of the input (an FFT spectrum), and stores the results in the output array (MFCC coefficients).";
399 if( !type_engine_import_mfun( env, func ) ) goto error;
400
401 // add examples
402 if( !type_engine_import_add_ex( env, "ai/features/mfcc-basic.ck" ) ) goto error;
403 if( !type_engine_import_add_ex( env, "ai/features/mfcc-mean.ck" ) ) goto error;
404 if( !type_engine_import_add_ex( env, "ai/genre-classify/feature-extract.ck" ) ) goto error;
405 if( !type_engine_import_add_ex( env, "ai/genre-classify/genre-classify.ck" ) ) goto error;
406
407 // end the class import
408 type_engine_import_class_end( env );
409
410 //---------------------------------------------------------------------
411 // init as base class: Kurtosis
412 //---------------------------------------------------------------------
413
414 doc =
415 "A unit analyzer that computes the kurtosis of a given input array, and outputs a single number.";
416
417 if( !type_engine_import_uana_begin( env, "Kurtosis", "UAna", env->global(),
418 NULL, NULL,
419 Kurtosis_tick, Kurtosis_tock, Kurtosis_pmsg,
420 CK_NO_VALUE, CK_NO_VALUE, CK_NO_VALUE, CK_NO_VALUE,
421 doc.c_str() ) )
422 return FALSE;
423
424 // compute
425 func = make_new_sfun( "float", "compute", Kurtosis_compute );
426 func->add_arg( "float[]", "input" );
427 func->doc = "Manually computes the kurtosis of a given input array.";
428 if( !type_engine_import_sfun( env, func ) ) goto error;
429
430 // add examples
431 if( !type_engine_import_add_ex( env, "ai/features/kurtosis.ck" ) ) goto error;
432
433 // end the class import
434 type_engine_import_class_end( env );
435
436 //---------------------------------------------------------------------
437 // init as base class: SFM
438 //---------------------------------------------------------------------
439
440 doc =
441 "A unit analyzer that computes the Spectral Flatness Measure (SFM) from a magnitude spectrum (either from an incoming UAna, or given manually), and outputs a single number.";
442
443 if( !type_engine_import_uana_begin( env, "SFM", "UAna", env->global(),
444 SFM_ctor, SFM_dtor,
445 SFM_tick, SFM_tock, SFM_pmsg,
446 CK_NO_VALUE, CK_NO_VALUE, CK_NO_VALUE, CK_NO_VALUE,
447 doc.c_str() ) )
448 return FALSE;
449
450 // data offset
451 SFM_offset_data = type_engine_import_mvar( env, "float", "@SFM_data", FALSE );
452 if( SFM_offset_data == CK_INVALID_OFFSET ) goto error;
453
454 // nr_bands
455 func = make_new_mfun( "int", "nrBands", SFM_ctrl_nr_bands );
456 func->add_arg( "int", "nr_bands" );
457 func->doc = "Set the number of frequency bands to use for SFM analysis.";
458 if( !type_engine_import_mfun( env, func ) ) goto error;
459
460 // nr_bands
461 func = make_new_mfun( "int", "nrBands", SFM_cget_nr_bands );
462 func->doc = "Get the number of frequency bands to use for SFM analysis.";
463 if( !type_engine_import_mfun( env, func ) ) goto error;
464
465 // compute (manual alternative to calling upchuck())
466 func = make_new_mfun( "float", "compute", SFM_compute );
467 func->add_arg( "float[]", "input" );
468 func->add_arg( "float[]", "output" );
469 func->doc =
470 "Manually computes the SFM of the input (an FFT spectrum), and stores the results in the output array (SFM coefficients).";
471 if( !type_engine_import_mfun( env, func ) ) goto error;
472
473 // add examples
474 if( !type_engine_import_add_ex( env, "ai/features/sfm.ck" ) ) goto error;
475
476 // end the class import
477 type_engine_import_class_end( env );
478
479 //---------------------------------------------------------------------
480 // init as base class: Chroma
481 //---------------------------------------------------------------------
482
483 doc =
484 "A unit analyzer that computes the Chroma features from a magnitude spectrum (either from an incoming UAna, or given manually), and outputs a vector of coefficients.";
485
486 if( !type_engine_import_uana_begin( env, "Chroma", "UAna", env->global(),
487 Chroma_ctor, Chroma_dtor,
488 Chroma_tick, Chroma_tock, Chroma_pmsg,
489 CK_NO_VALUE, CK_NO_VALUE, CK_NO_VALUE, CK_NO_VALUE,
490 doc.c_str() ) )
491 return FALSE;
492
493 // data offset
494 Chroma_offset_data = type_engine_import_mvar( env, "float", "@Chroma_data", FALSE );
495 if( Chroma_offset_data == CK_INVALID_OFFSET ) goto error;
496
497 // sample_rate
498 func = make_new_mfun( "float", "sampleRate", Chroma_ctrl_sample_rate );
499 func->add_arg( "float", "sample_rate" );
500 func->doc = "Set the sample rate for Chroma analysis.";
501 if( !type_engine_import_mfun( env, func ) ) goto error;
502
503 // sample_rate
504 func = make_new_mfun( "float", "sampleRate", Chroma_cget_sample_rate );
505 func->doc = "Get the sample rate for Chroma analysis.";
506 if( !type_engine_import_mfun( env, func ) ) goto error;
507
508 // low_oct_num
509 func = make_new_mfun( "int", "lowOctNum", Chroma_ctrl_low_oct_num );
510 func->add_arg( "int", "low_oct_num" );
511 func->doc = "Set the lowest octave number for Chroma analysis.";
512 if( !type_engine_import_mfun( env, func ) ) goto error;
513
514 // low_oct_num
515 func = make_new_mfun( "int", "lowOctNum", Chroma_cget_low_oct_num );
516 func->doc = "Get the lowest octave number for Chroma analysis.";
517 if( !type_engine_import_mfun( env, func ) ) goto error;
518
519 // high_oct_num
520 func = make_new_mfun( "int", "highOctNum", Chroma_ctrl_high_oct_num );
521 func->add_arg( "int", "high_oct_num" );
522 func->doc = "Set the highest octave number for Chroma analysis.";
523 if( !type_engine_import_mfun( env, func ) ) goto error;
524
525 // high_oct_num
526 func = make_new_mfun( "int", "highOctNum", Chroma_cget_high_oct_num );
527 func->doc = "Get the highest octave number for Chroma analysis.";
528 if( !type_engine_import_mfun( env, func ) ) goto error;
529
530 // compute (manual alternative to calling upchuck())
531 func = make_new_mfun( "float", "compute", Chroma_compute );
532 func->add_arg( "float[]", "input" );
533 func->add_arg( "float[]", "output" );
534 func->doc =
535 "Manually computes the Chroma of the input (an FFT spectrum), and stores the results in the output array (Chroma coefficients).";
536 if( !type_engine_import_mfun( env, func ) ) goto error;
537
538 // add examples
539 if( !type_engine_import_add_ex( env, "ai/features/chroma.ck" ) ) goto error;
540
541 // end the class import
542 type_engine_import_class_end( env );
543
544 //---------------------------------------------------------------------
545 // init as base class: RollOff
546 //---------------------------------------------------------------------
547 doc =
548 "A unit analyzer that computes the spectral rolloff from a magnitude spectrum (either from incoming UAna, or given manually), and outputs a single number.";
549
550 if( !type_engine_import_uana_begin( env, "RollOff", "UAna", env->global(),
551 RollOff_ctor, RollOff_dtor,
552 RollOff_tick, RollOff_tock, RollOff_pmsg,
553 CK_NO_VALUE, CK_NO_VALUE, CK_NO_VALUE, CK_NO_VALUE,
554 doc.c_str() ) )
555 return FALSE;
556
557 // data offset
558 RollOff_offset_percent = type_engine_import_mvar( env, "float", "@RollOff_data", FALSE );
559 if( RollOff_offset_percent == CK_INVALID_OFFSET ) goto error;
560
561 // compute
562 func = make_new_mfun( "float", "percent", RollOff_ctrl_percent );
563 func->add_arg( "float", "percent" );
564 func->doc = "Set the percentage for computing rolloff.";
565 if( !type_engine_import_mfun( env, func ) ) goto error;
566
567 // compute
568 func = make_new_mfun( "float", "percent", RollOff_cget_percent );
569 func->doc = "Get the percentage specified for the rolloff.";
570 if( !type_engine_import_mfun( env, func ) ) goto error;
571
572 // compute
573 func = make_new_sfun( "float", "compute", RollOff_compute );
574 func->add_arg( "float[]", "input" );
575 func->add_arg( "float", "percent" );
576 func->doc = "Manually computes the rolloff from a float array.";
577 if( !type_engine_import_sfun( env, func ) ) goto error;
578
579 // add examples
580 if( !type_engine_import_add_ex( env, "ai/features/rolloff.ck" ) ) goto error;
581 if( !type_engine_import_add_ex( env, "ai/features/rolloff2.ck" ) ) goto error;
582
583 // end the class import
584 type_engine_import_class_end( env );
585
586 //---------------------------------------------------------------------
587 // init as base class: AutoCorr
588 //---------------------------------------------------------------------
589 doc =
590 "A unit analyzer that computes auto-correlation of the incoming analysis frame.";
591
592 if( !type_engine_import_uana_begin( env, "AutoCorr", "UAna", env->global(),
593 AutoCorr_ctor, AutoCorr_dtor,
594 AutoCorr_tick, AutoCorr_tock, AutoCorr_pmsg,
595 CK_NO_VALUE, CK_NO_VALUE, CK_NO_VALUE, CK_NO_VALUE,
596 doc.c_str() ) )
597 return FALSE;
598
599 // data offset
600 AutoCorr_offset_data = type_engine_import_mvar( env, "float", "@AutoCorr_data", FALSE );
601 if( AutoCorr_offset_data == CK_INVALID_OFFSET ) goto error;
602
603 // normalize
604 func = make_new_mfun( "int", "normalize", AutoCorr_ctrl_normalize );
605 func->add_arg( "int", "flag" );
606 if( !type_engine_import_mfun( env, func ) ) goto error;
607
608 // normalize
609 func = make_new_mfun( "int", "normalize", AutoCorr_cget_normalize );
610 if( !type_engine_import_mfun( env, func ) ) goto error;
611
612 // compute
613 func = make_new_sfun( "float[]", "compute", AutoCorr_compute );
614 func->add_arg( "float[]", "input" );
615 func->add_arg( "int", "normalize" );
616 func->add_arg( "float[]", "output" );
617 if( !type_engine_import_sfun( env, func ) ) goto error;
618
619 // add examples
620 if( !type_engine_import_add_ex( env, "analysis/autocorr.ck" ) ) goto error;
621
622 // end the class import
623 type_engine_import_class_end( env );
624
625 //---------------------------------------------------------------------
626 // init as base class: XCorr
627 //---------------------------------------------------------------------
628 doc =
629 "A unit analyzer that computes cross-correlation between two incoming analysis frames.";
630
631 if( !type_engine_import_uana_begin( env, "XCorr", "UAna", env->global(),
632 XCorr_ctor, XCorr_dtor,
633 XCorr_tick, XCorr_tock, XCorr_pmsg,
634 CK_NO_VALUE, CK_NO_VALUE, CK_NO_VALUE, CK_NO_VALUE,
635 doc.c_str() ) )
636 return FALSE;
637
638 // data offset
639 XCorr_offset_data = type_engine_import_mvar( env, "float", "@XCorr_data", FALSE );
640 if( XCorr_offset_data == CK_INVALID_OFFSET ) goto error;
641
642 // normalize
643 func = make_new_mfun( "int", "normalize", XCorr_ctrl_normalize );
644 func->add_arg( "int", "flag" );
645 if( !type_engine_import_mfun( env, func ) ) goto error;
646
647 // normalize
648 func = make_new_mfun( "int", "normalize", XCorr_cget_normalize );
649 if( !type_engine_import_mfun( env, func ) ) goto error;
650
651 // compute
652 func = make_new_sfun( "float[]", "compute", XCorr_compute );
653 func->add_arg( "float[]", "f" );
654 func->add_arg( "float[]", "g" );
655 func->add_arg( "int", "normalize" );
656 func->add_arg( "float[]", "y" );
657 if( !type_engine_import_sfun( env, func ) ) goto error;
658
659 // add examples
660 if( !type_engine_import_add_ex( env, "analysis/xcorr.ck" ) ) goto error;
661
662 // end the class import
663 type_engine_import_class_end( env );
664
665 //---------------------------------------------------------------------
666 // init as base class: zerox
667 //---------------------------------------------------------------------
668
669 doc = "Zero crossing detector.";
670
671 if( !type_engine_import_uana_begin( env, "ZeroX", "UAna", env->global(),
672 ZeroX_ctor, ZeroX_dtor,
673 ZeroX_tick, ZeroX_tock, NULL,
674 CK_NO_VALUE, CK_NO_VALUE, CK_NO_VALUE, CK_NO_VALUE,
675 doc.c_str() ) )
676 return FALSE;
677
678 // data offset
679 ZeroX_offset_data = type_engine_import_mvar( env, "int", "@ZeroX_data", FALSE );
680 if( ZeroX_offset_data == CK_INVALID_OFFSET ) goto error;
681
682 // compute
683 func = make_new_sfun( "float", "compute", ZeroX_compute );
684 func->add_arg( "float[]", "input" );
685 func->doc = "Manually computes the zero crossing rate for an array.";
686 if( !type_engine_import_sfun( env, func ) ) goto error;
687
688 // add examples
689 if( !type_engine_import_add_ex( env, "ai/features/zerox.ck" ) ) goto error;
690
691 // end import
692 if( !type_engine_import_class_end( env ) )
693 return FALSE;
694
695 return TRUE;
696
697 error:
698
699 // end the class import
700 type_engine_import_class_end( env );
701
702 return FALSE;
703 }
704
705 CK_DLL_TICK( FeatureCollector_tick )
706 {
707 // do nothing
708 return TRUE;
709 }
710
711 // FeatureCollector_tock creates a flat vector from its upstream UAnae
712 // TODO: Stick complex features in Blob, too? But what about fft? (don't want duplication)
713 CK_DLL_TOCK( FeatureCollector_tock )
714 {
715 //t_CKFLOAT * features;
716 t_CKINT num_feats = 0;
717 t_CKINT num_incoming = UANA->numIncomingUAnae();
718 t_CKINT i, j;
719
720
721 // Get all incoming features and agglomerate into one vector
722 if( num_incoming > 0 )
723 {
724 //count the number of features in the array we're making
725 for( i = 0; i < num_incoming; i++ )
726 {
727 // get next blob
728 Chuck_UAnaBlobProxy * BLOB_IN = UANA->getIncomingBlob( i );
729 // sanity check
730 assert( BLOB_IN != NULL );
731 // count number of features from this UAna
732 Chuck_ArrayFloat & these_fvals = BLOB_IN->fvals();
733 num_feats += these_fvals.size();
734 }
735
736 // get fvals of output BLOB
737 Chuck_ArrayFloat & fvals = BLOB->fvals();
738 if( fvals.size() != num_feats )
739 fvals.set_size( num_feats );
740
741 t_CKINT next_index = 0;
742 for( i = 0; i < num_incoming; i++ )
743 {
744 // get next blob
745 Chuck_UAnaBlobProxy * BLOB_IN = UANA->getIncomingBlob( i );
746 Chuck_ArrayFloat & these_fvals = BLOB_IN->fvals();
747 t_CKINT num_these = these_fvals.size();
748 for( j = 0; j < num_these; j++ )
749 {
750 t_CKFLOAT v;
751 these_fvals.get( j, &v );
752 fvals.set( next_index, v );
753 next_index++;
754 }
755 }
756 }
757 else
758 {
759 // no input to collect
760 BLOB->fvals().set_size( 0 );
761 }
762
763 return TRUE;
764 }
765
766 CK_DLL_PMSG( FeatureCollector_pmsg )
767 {
768 // do nothing
769 return TRUE;
770 }
771
772 static t_CKFLOAT compute_centroid( Chuck_ArrayFloat & buffer, t_CKUINT size )
773 {
774 t_CKFLOAT m0 = 0.0;
775 t_CKFLOAT m1 = 0.0;
776 t_CKFLOAT centroid;
777 t_CKFLOAT v;
778 t_CKUINT i;
779
780 // Compute centroid using moments
781 for( i = 0; i < size; i++ )
782 {
783 buffer.get( i, &v );
784 m1 += ( i * v );
785 m0 += v;
786 }
787
788 if( m0 != 0.0 )
789 centroid = m1 / m0;
790 else
791 centroid = size / 2.0; // Perfectly balanced
792
793 return centroid / size;
794 }
795
796 CK_DLL_TICK( Centroid_tick )
797 {
798 // do nothing
799 return TRUE;
800 }
801
802 CK_DLL_TOCK( Centroid_tock )
803 {
804 t_CKFLOAT result = 0.0;
805
806 // check if there are any incoming UAna (e.g., connected using =^)
807 if( UANA->numIncomingUAnae() > 0 )
808 {
809 // get first
810 Chuck_UAnaBlobProxy * BLOB_IN = UANA->getIncomingBlob( 0 );
811 // sanity check
812 assert( BLOB_IN != NULL );
813 // get the array
814 Chuck_ArrayFloat & mag = BLOB_IN->fvals();
815 // compute centroid
816 result = compute_centroid( mag, mag.size() );
817 }
818 // otherwise zero out
819 else
820 {
821 // no input!
822 result = 0.0;
823 }
824
825 // get fvals of output BLOB
826 Chuck_ArrayFloat & fvals = BLOB->fvals();
827 // ensure size == resulting size
828 if( fvals.size() != 1 )
829 fvals.set_size( 1 );
830 // copy the result in
831 fvals.set( 0, result );
832
833 return TRUE;
834 }
835
836 CK_DLL_PMSG( Centroid_pmsg )
837 {
838 // do nothing
839 return TRUE;
840 }
841
842 CK_DLL_SFUN( Centroid_compute )
843 {
844 // get array
845 Chuck_ArrayFloat * array = (Chuck_ArrayFloat *)GET_NEXT_OBJECT( ARGS );
846 // sanity check
847 if( !array )
848 {
849 // no centroid
850 RETURN->v_float = 0.0;
851 }
852 else
853 {
854 // do it
855 RETURN->v_float = compute_centroid( *array, array->size() );
856 }
857 }
858
859 // Flux state
860 struct StateOfFlux
861 {
862 Chuck_ArrayFloat prev;
863 Chuck_ArrayFloat norm;
864 t_CKBOOL initialized;
865
866 StateOfFlux()
867 {
868 initialized = FALSE;
869 }
870 };
871
872 // compute norm rms
873 static void compute_norm_rms( Chuck_ArrayFloat & curr, Chuck_ArrayFloat & norm )
874 {
875 t_CKUINT i;
876 t_CKFLOAT energy = 0.0;
877 t_CKFLOAT v;
878
879 // check size
880 if( norm.size() != curr.size() )
881 norm.set_size( curr.size() );
882
883 // get energy
884 for( i = 0; i < curr.size(); i++ )
885 {
886 curr.get( i, &v );
887 energy += v * v;
888 }
889
890 // check energy
891 if( energy == 0.0 )
892 {
893 // all zeros
894 norm.zero( 0, norm.size() );
895 return;
896 }
897 else
898 energy = ::sqrt( energy );
899
900 for( i = 0; i < curr.size(); i++ )
901 {
902 curr.get( i, &v );
903 if( v > 0.0 )
904 norm.set( i, v / energy );
905 else
906 norm.set( i, 0.0 );
907 }
908 }
909
910 // compute flux
911 static t_CKFLOAT compute_flux( Chuck_ArrayFloat & curr, Chuck_ArrayFloat & prev, Chuck_ArrayFloat * write )
912 {
913 // sanity check
914 assert( curr.size() == prev.size() );
915
916 // ensure size
917 if( write != NULL && ( write->size() != curr.size() ) )
918 write->set_size( curr.size() );
919
920 // find difference
921 t_CKFLOAT v, w, result = 0.0;
922 for( t_CKUINT i = 0; i < curr.size(); i++ )
923 {
924 curr.get( i, &v );
925 prev.get( i, &w );
926 // accumulate into flux
927 result += ( v - w ) * ( v - w );
928 // copy to write
929 if( write != NULL ) write->set( i, v );
930 }
931
932 // take sqrt of flux
933 return ::sqrt( result );
934 }
935
936 // compute flux
937 static t_CKFLOAT compute_flux( Chuck_ArrayFloat & curr, StateOfFlux & sof )
938 {
939 // flux
940 t_CKFLOAT result = 0.0;
941 t_CKFLOAT v;
942
943 // verify size
944 if( curr.size() != sof.prev.size() )
945 {
946 sof.initialized = FALSE;
947 // resize prev
948 sof.prev.set_size( curr.size() );
949 }
950
951 // check initialized
952 if( sof.initialized )
953 {
954 // compute normalize rms
955 compute_norm_rms( curr, sof.norm );
956 // do it
957 result = compute_flux( sof.norm, sof.prev, &sof.prev );
958 }
959
960 // copy curr to prev
961 for( t_CKUINT i = 0; i < curr.size(); i++ )
962 {
963 // get the value
964 sof.norm.get( i, &v );
965 // set it
966 sof.prev.set( i, v );
967 }
968
969 // initialize
970 sof.initialized = TRUE;
971
972 // return result
973 return result;
974 }
975
976 CK_DLL_CTOR( Flux_ctor )
977 {
978 // allocate the flux state
979 StateOfFlux * state = new StateOfFlux();
980 OBJ_MEMBER_UINT( SELF, Flux_offset_data ) = (t_CKUINT)state;
981 }
982
983
984 CK_DLL_DTOR( Flux_dtor )
985 {
986 // clean up
987 StateOfFlux * state = (StateOfFlux *)OBJ_MEMBER_UINT( SELF, Flux_offset_data );
988 CK_SAFE_DELETE( state );
989 OBJ_MEMBER_UINT( SELF, Flux_offset_data ) = 0;
990 }
991
992
993 CK_DLL_TICK( Flux_tick )
994 {
995 // do nothing
996 return TRUE;
997 }
998
999
1000 CK_DLL_TOCK( Flux_tock )
1001 {
1002 t_CKFLOAT result = 0.0;
1003
1004 // get state
1005 StateOfFlux * state = (StateOfFlux *)OBJ_MEMBER_UINT( SELF, Flux_offset_data );
1006
1007 // TODO: get buffer from stream, and set in SVM
1008 if( UANA->numIncomingUAnae() > 0 )
1009 {
1010 // get first
1011 Chuck_UAnaBlobProxy * BLOB_IN = UANA->getIncomingBlob( 0 );
1012 // sanity check
1013 assert( BLOB_IN != NULL );
1014 // get the array
1015 Chuck_ArrayFloat & mag = BLOB_IN->fvals();
1016 // compute flux
1017 result = compute_flux( mag, *state );
1018 }
1019 // otherwise zero out
1020 else
1021 {
1022 // no input!
1023 result = 0.0;
1024 }
1025
1026 // get fvals of output BLOB
1027 Chuck_ArrayFloat & fvals = BLOB->fvals();
1028 // ensure size == resulting size
1029 if( fvals.size() != 1 )
1030 fvals.set_size( 1 );
1031 // copy the result in
1032 fvals.set( 0, result );
1033
1034 return TRUE;
1035 }
1036
1037 CK_DLL_PMSG( Flux_pmsg )
1038 {
1039 // do nothing
1040 return TRUE;
1041 }
1042
1043 CK_DLL_MFUN( Flux_ctrl_reset )
1044 {
1045 // get state
1046 StateOfFlux * state = (StateOfFlux *)OBJ_MEMBER_UINT( SELF, Flux_offset_data );
1047 // set
1048 state->initialized = FALSE;
1049 }
1050
1051 CK_DLL_SFUN( Flux_compute )
1052 {
1053 // get inputs
1054 Chuck_ArrayFloat * lhs = (Chuck_ArrayFloat *)GET_NEXT_OBJECT( ARGS );
1055 Chuck_ArrayFloat * rhs = (Chuck_ArrayFloat *)GET_NEXT_OBJECT( ARGS );
1056
1057 // verify
1058 if( !lhs || !rhs )
1059 {
1060 // 0
1061 RETURN->v_float = 0.0;
1062 }
1063 else
1064 {
1065 // verify size
1066 if( lhs->size() != rhs->size() )
1067 {
1068 // message
1069 EM_error3( "(via Flux): compute() expects two arrays of equal size" );
1070 // 0
1071 RETURN->v_float = 0.0;
1072 }
1073 else
1074 {
1075 // flux
1076 RETURN->v_float = compute_flux( *lhs, *rhs, NULL );
1077 }
1078 }
1079 }
1080
1081 CK_DLL_SFUN( Flux_compute2 )
1082 {
1083 // get inputs
1084 Chuck_ArrayFloat * lhs = (Chuck_ArrayFloat *)GET_NEXT_OBJECT( ARGS );
1085 Chuck_ArrayFloat * rhs = (Chuck_ArrayFloat *)GET_NEXT_OBJECT( ARGS );
1086 Chuck_ArrayFloat * diff = (Chuck_ArrayFloat *)GET_NEXT_OBJECT( ARGS );
1087
1088 // verify
1089 if( !lhs || !rhs )
1090 {
1091 // 0
1092 RETURN->v_float = 0.0;
1093 }
1094 else
1095 {
1096 // verify size
1097 if( lhs->size() != rhs->size() )
1098 {
1099 // message
1100 EM_error3( "(via Flux): compute() expects two arrays of equal size" );
1101 // 0
1102 RETURN->v_float = 0.0;
1103 }
1104 else
1105 {
1106 // flux
1107 RETURN->v_float = compute_flux( *lhs, *rhs, diff );
1108 }
1109 }
1110 }
1111
1112 static t_CKFLOAT compute_rms( Chuck_ArrayFloat & buffer, t_CKUINT size )
1113 {
1114 t_CKFLOAT rms = 0.0;
1115 t_CKFLOAT v;
1116 t_CKUINT i;
1117
1118 // get sum of squares
1119 for( i = 0; i < size; i++ )
1120 {
1121 buffer.get( i, &v );
1122 rms += ( v * v );
1123 }
1124
1125 // go
1126 rms /= size;
1127 rms = ::sqrt( rms );
1128
1129 return rms;
1130 }
1131
1132 CK_DLL_TICK( RMS_tick )
1133 {
1134 // do nothing
1135 return TRUE;
1136 }
1137
1138 CK_DLL_TOCK( RMS_tock )
1139 {
1140 t_CKFLOAT result = 0.0;
1141
1142 // TODO: get buffer from stream, and set in SVM
1143 if( UANA->numIncomingUAnae() > 0 )
1144 {
1145 // get first
1146 Chuck_UAnaBlobProxy * BLOB_IN = UANA->getIncomingBlob( 0 );
1147 // sanity check
1148 assert( BLOB_IN != NULL );
1149 // get the array
1150 Chuck_ArrayFloat & mag = BLOB_IN->fvals();
1151 // compute RMS
1152 result = compute_rms( mag, mag.size() );
1153 }
1154 // otherwise zero out
1155 else
1156 {
1157 // no input!
1158 result = 0.0;
1159 }
1160
1161 // get fvals of output BLOB
1162 Chuck_ArrayFloat & fvals = BLOB->fvals();
1163 // ensure size == resulting size
1164 if( fvals.size() != 1 )
1165 fvals.set_size( 1 );
1166 // copy the result in
1167 fvals.set( 0, result );
1168
1169 return TRUE;
1170 }
1171
1172 CK_DLL_PMSG( RMS_pmsg )
1173 {
1174 // do nothing
1175 return TRUE;
1176 }
1177
1178 CK_DLL_SFUN( RMS_compute )
1179 {
1180 // get array
1181 Chuck_ArrayFloat * array = (Chuck_ArrayFloat *)GET_NEXT_OBJECT( ARGS );
1182 // sanity check
1183 if( !array )
1184 {
1185 // no RMS
1186 RETURN->v_float = 0.0;
1187 }
1188 else
1189 {
1190 // do it
1191 RETURN->v_float = compute_rms( *array, array->size() );
1192 }
1193 }
1194
1195 // 1.4.2.0 (yikai) | MFCC implementation
1196 // Yikai Li, Fall 2022
1197 struct MFCC_Object
1198 {
1199 t_CKINT size;
1200 t_CKINT sample_rate;
1201 t_CKINT num_filters;
1202 t_CKINT num_coeffs;
1203 t_CKINT curr_sample_rate;
1204 t_CKINT curr_num_filters;
1205 t_CKINT curr_num_coeffs;
1206
1207 t_CKFLOAT * filterbank;
1208 t_CKINT * filterpoints;
1209 t_CKFLOAT * filterfreqs;
1210 t_CKFLOAT * dct;
1211
1212 t_CKFLOAT * spectrum;
1213 t_CKFLOAT * filtered;
1214 t_CKFLOAT * result;
1215
1216 // static mfcc instance
1217 static MFCC_Object * ourMFCC;
1218
1219 // constructor
1220 MFCC_Object()
1221 {
1222 size = 1024;
1223 sample_rate = g_uana_srate;
1224 num_filters = 10;
1225 num_coeffs = 40;
1226 curr_sample_rate = 0.0;
1227 curr_num_filters = 0;
1228 curr_num_coeffs = 0;
1229 filterbank = NULL;
1230 filterpoints = NULL;
1231 filterfreqs = NULL;
1232 dct = NULL;
1233 spectrum = NULL;
1234 filtered = NULL;
1235 result = NULL;
1236 this->prepare( size );
1237 }
1238
1239 // destructor
1240 ~MFCC_Object()
1241 {
1242 // done
1243 this->reset();
1244 }
1245
1246 // reset
1247 void reset()
1248 {
1249 // delete
1250 CK_SAFE_DELETE_ARRAY( filterbank );
1251 CK_SAFE_DELETE_ARRAY( filterpoints );
1252 CK_SAFE_DELETE_ARRAY( filterfreqs );
1253 CK_SAFE_DELETE_ARRAY( dct );
1254 CK_SAFE_DELETE_ARRAY( spectrum );
1255 CK_SAFE_DELETE_ARRAY( filtered );
1256 CK_SAFE_DELETE_ARRAY( result );
1257 // zero out
1258 size = 0;
1259 sample_rate = 0.0;
1260 num_filters = 0;
1261 num_coeffs = 0;
1262 curr_sample_rate = 0.0;
1263 curr_num_filters = 0;
1264 curr_num_coeffs = 0;
1265 }
1266
1267 // clear
1268 void clear()
1269 {
1270 if( spectrum ) memset( spectrum, 0, sizeof( t_CKFLOAT ) * size );
1271 if( filtered ) memset( filtered, 0, sizeof( t_CKFLOAT ) * num_filters );
1272 if( result ) memset( result, 0, sizeof( t_CKFLOAT ) * num_coeffs );
1273 if( filterbank ) memset( filterbank, 0, sizeof( t_CKFLOAT ) * num_filters * size );
1274 if( filterpoints ) memset( filterpoints, 0, sizeof( t_CKINT ) * num_filters );
1275 if( filterfreqs ) memset( filterfreqs, 0, sizeof( t_CKFLOAT ) * num_filters );
1276 if( dct ) memset( dct, 0, sizeof( t_CKFLOAT ) * num_filters * num_coeffs );
1277 }
1278
1279 static t_CKFLOAT freq2mel( t_CKFLOAT freq )
1280 {
1281 return 2595.0 * ::log10( 1.0 + freq / 700.0 );
1282 }
1283
1284 static t_CKFLOAT mel2freq( t_CKFLOAT mel )
1285 {
1286 return 700.0 * ( ::pow( 10.0, mel / 2595.0 ) - 1.0 );
1287 }
1288
1289 // prepare filterband and dct
1290 void prepare( t_CKINT theSize )
1291 {
1292 if( this->size == theSize
1293 && this->sample_rate == this->curr_sample_rate
1294 && this->num_filters == this->curr_num_filters
1295 && this->num_coeffs == this->curr_num_coeffs )
1296 return;
1297
1298 this->size = theSize;
1299 this->curr_sample_rate = this->sample_rate;
1300 this->curr_num_filters = this->num_filters;
1301 this->curr_num_coeffs = this->num_coeffs;
1302
1303 // filter bank
1304 CK_SAFE_DELETE_ARRAY( this->filterbank );
1305 this->filterbank = new t_CKFLOAT[num_filters * theSize];
1306 memset( this->filterbank, 0, sizeof( t_CKFLOAT ) * num_filters * theSize);
1307
1308 CK_SAFE_DELETE_ARRAY( this->filterpoints );
1309 this->filterpoints = new t_CKINT[num_filters + 2];
1310 CK_SAFE_DELETE_ARRAY( this->filterfreqs );
1311 this->filterfreqs = new t_CKFLOAT[num_filters + 2];
1312
1313 t_CKFLOAT filterwidth = ::round( this->freq2mel( this->sample_rate / 2.0 ) / ( this->num_filters + 1.0 ) );
1314 for( int i = 0; i < this->num_filters + 1; i++ )
1315 {
1316 this->filterfreqs[i] = this->mel2freq( i * filterwidth );
1317 this->filterpoints[i] = (t_CKINT)( this->filterfreqs[i] / this->sample_rate * ( this->size * 2 + 1 ) );
1318 }
1319 this->filterfreqs[num_filters + 1] = this->sample_rate / 2.0;
1320 this->filterpoints[num_filters + 1] = this->size;
1321
1322 t_CKFLOAT energy;
1323 for( int i = 0; i < this->num_filters; i++ )
1324 {
1325 energy = 2.0 / ( this->filterfreqs[i + 2] - this->filterfreqs[i] );
1326 for( t_CKINT j = this->filterpoints[i]; j < this->filterpoints[i + 1]; j++ )
1327 {
1328 this->filterbank[i * theSize + j] =
1329 energy * ( j - this->filterpoints[i] ) / ( this->filterpoints[i + 1] - this->filterpoints[i] );
1330 }
1331 for( t_CKINT j = this->filterpoints[i + 1]; j < this->filterpoints[i + 2]; j++ )
1332 {
1333 this->filterbank[i * theSize + j] = energy * ( this->filterpoints[i + 2] - j )
1334 / ( this->filterpoints[i + 2] - this->filterpoints[i + 1] );
1335 }
1336 }
1337
1338 // dct
1339 CK_SAFE_DELETE_ARRAY( this->dct );
1340 this->dct = new t_CKFLOAT[num_coeffs * num_filters];
1341 for( int i = 0; i < this->num_coeffs; i++ )
1342 {
1343 if( i == 0 )
1344 {
1345 for( int j = 0; j < this->num_filters; j++ )
1346 {
1347 this->dct[i * num_filters + j] = 1.0 / sqrt( (t_CKFLOAT)this->num_filters );
1348 }
1349 }
1350 else
1351 {
1352 for( int j = 0; j < this->num_filters; j++ )
1353 {
1354 this->dct[i * num_filters + j] =
1355 sqrt( 2.0 / this->num_filters ) * cos( CK_ONE_PI * i * ( j + 0.5 ) / this->num_filters );
1356 }
1357 }
1358 }
1359
1360 // spectrum
1361 CK_SAFE_DELETE_ARRAY( this->spectrum );
1362 this->spectrum = new t_CKFLOAT[theSize];
1363
1364 // filtered
1365 CK_SAFE_DELETE_ARRAY( this->filtered );
1366 this->filtered = new t_CKFLOAT[num_filters];
1367
1368 // result
1369 CK_SAFE_DELETE_ARRAY( this->result );
1370 this->result = new t_CKFLOAT[num_coeffs];
1371 }
1372
1373 // compute
1374 void compute()
1375 {
1376 // compute spectrum
1377 for( int i = 0; i < this->size; i++ )
1378 {
1379 this->spectrum[i] = this->spectrum[i] * this->spectrum[i];
1380 }
1381
1382 // compute filterbank
1383 for( int i = 0; i < this->num_filters; i++ )
1384 {
1385 this->filtered[i] = 1e-10;
1386 for( int j = 0; j < this->size; j++ )
1387 {
1388 this->filtered[i] += this->spectrum[j] * this->filterbank[i * this->size + j];
1389 }
1390 this->filtered[i] = 10.0 * ::log10( this->filtered[i] );
1391 }
1392
1393 // compute dct
1394 for( int i = 0; i < this->num_coeffs; i++ )
1395 {
1396 this->result[i] = 0.0;
1397 for( int j = 0; j < this->num_filters; j++ )
1398 {
1399 this->result[i] += this->filtered[j] * this->dct[i * this->num_filters + j];
1400 }
1401 }
1402 }
1403
1404 // get our singleton
1405 // static MFCC_Object * getOurObject()
1406 // {
1407 // // instantiate, if needed
1408 // if (ourMFCC == NULL)
1409 // {
1410 // ourMFCC = new MFCC_Object();
1411 // assert(ourMFCC != NULL);
1412 // }
1413 //
1414 // // return new instance
1415 // return ourMFCC;
1416 // }
1417 };
1418
1419 // static initialization
1420 MFCC_Object * MFCC_Object::ourMFCC = NULL;
1421
1422 // compute mfcc
1423 static void compute_mfcc( MFCC_Object * mfcc, Chuck_ArrayFloat & f, t_CKUINT fs, Chuck_ArrayFloat & buffy )
1424 {
1425 t_CKUINT i;
1426 t_CKFLOAT v;
1427 t_CKINT size;
1428
1429 mfcc->prepare( fs );
1430
1431 // copy into buffers
1432 for( i = 0; i < fs; i++ )
1433 {
1434 f.get( i, &v );
1435 mfcc->spectrum[i] = v;
1436 }
1437
1438 // compute
1439 mfcc->compute();
1440
1441 // copy into result
1442 size = mfcc->num_coeffs;
1443 buffy.set_size( size );
1444 for( i = 0; i < size; i++ )
1445 {
1446 buffy.set( i, mfcc->result[i] );
1447 }
1448 }
1449
1450 // MFCC
1451 CK_DLL_CTOR( MFCC_ctor )
1452 {
1453 MFCC_Object * mfcc = new MFCC_Object();
1454 OBJ_MEMBER_UINT( SELF, MFCC_offset_data ) = (t_CKUINT)mfcc;
1455 }
1456
1457 CK_DLL_DTOR( MFCC_dtor )
1458 {
1459 MFCC_Object * mfcc = (MFCC_Object *)OBJ_MEMBER_UINT( SELF, MFCC_offset_data );
1460 CK_SAFE_DELETE( mfcc );
1461 OBJ_MEMBER_UINT( SELF, MFCC_offset_data ) = 0;
1462 }
1463
1464
1465 CK_DLL_TICK( MFCC_tick )
1466 {
1467 // do nothing
1468 return TRUE;
1469 }
1470
1471 CK_DLL_TOCK( MFCC_tock )
1472 {
1473 // get object
1474 MFCC_Object * mfcc = (MFCC_Object *)OBJ_MEMBER_UINT( SELF, MFCC_offset_data );
1475
1476 // TODO: get buffer from stream, and set in SVM
1477 if( UANA->numIncomingUAnae() > 0 )
1478 {
1479 // get first
1480 Chuck_UAnaBlobProxy * BLOB_IN = UANA->getIncomingBlob( 0 );
1481 // sanity check
1482 assert( BLOB_IN != NULL );
1483 // get the array
1484 Chuck_ArrayFloat & mag = BLOB_IN->fvals();
1485 // get fvals of output BLOB
1486 Chuck_ArrayFloat & fvals = BLOB->fvals();
1487 // compute MFCC
1488 compute_mfcc( mfcc, mag, mag.size(), fvals );
1489 }
1490 // otherwise zero out
1491 else
1492 {
1493 // get fvals of output BLOB
1494 Chuck_ArrayFloat & fvals = BLOB->fvals();
1495 // resize
1496 fvals.set_size( 0 );
1497 }
1498
1499 return TRUE;
1500 }
1501
1502 CK_DLL_PMSG( MFCC_pmsg )
1503 {
1504 // do nothing
1505 return TRUE;
1506 }
1507
1508 CK_DLL_CTRL( MFCC_ctrl_sample_rate )
1509 {
1510 // get object
1511 MFCC_Object * mfcc = (MFCC_Object *)OBJ_MEMBER_UINT( SELF, MFCC_offset_data );
1512 // get sample_rate
1513 mfcc->sample_rate = GET_NEXT_FLOAT( ARGS );
1514 // return it
1515 RETURN->v_float = mfcc->sample_rate;
1516 }
1517
1518 CK_DLL_CGET( MFCC_cget_sample_rate )
1519 {
1520 // get object
1521 MFCC_Object * mfcc = (MFCC_Object *)OBJ_MEMBER_UINT( SELF, MFCC_offset_data );
1522 // return it
1523 RETURN->v_float = mfcc->sample_rate;
1524 }
1525
1526 CK_DLL_CTRL( MFCC_ctrl_num_filters )
1527 {
1528 // get object
1529 MFCC_Object * mfcc = (MFCC_Object *)OBJ_MEMBER_UINT( SELF, MFCC_offset_data );
1530 // get num_filters
1531 mfcc->num_filters = GET_NEXT_INT( ARGS );
1532 // return it
1533 RETURN->v_int = mfcc->num_filters;
1534 }
1535
1536 CK_DLL_CGET( MFCC_cget_num_filters )
1537 {
1538 // get object
1539 MFCC_Object * mfcc = (MFCC_Object *)OBJ_MEMBER_UINT( SELF, MFCC_offset_data );
1540 // return it
1541 RETURN->v_int = mfcc->num_filters;
1542 }
1543
1544 CK_DLL_CTRL( MFCC_ctrl_num_coeffs )
1545 {
1546 // get object
1547 MFCC_Object * mfcc = (MFCC_Object *)OBJ_MEMBER_UINT( SELF, MFCC_offset_data );
1548 // get num_coeffs
1549 mfcc->num_coeffs = GET_NEXT_INT( ARGS );
1550 // return it
1551 RETURN->v_int = mfcc->num_coeffs;
1552 }
1553
1554 CK_DLL_CGET( MFCC_cget_num_coeffs )
1555 {
1556 // get object
1557 MFCC_Object * mfcc = (MFCC_Object *)OBJ_MEMBER_UINT( SELF, MFCC_offset_data );
1558 // return it
1559 RETURN->v_int = mfcc->num_coeffs;
1560 }
1561
1562 CK_DLL_MFUN( MFCC_compute )
1563 {
1564 // get object
1565 MFCC_Object * mfcc = (MFCC_Object *)OBJ_MEMBER_UINT( SELF, MFCC_offset_data );
1566 // get input
1567 Chuck_ArrayFloat * input = (Chuck_ArrayFloat *)GET_NEXT_OBJECT( ARGS );
1568 // get output
1569 Chuck_ArrayFloat * output = (Chuck_ArrayFloat *)GET_NEXT_OBJECT( ARGS );
1570 // compute mfcc
1571 compute_mfcc( mfcc, *input, input->size(), *output );
1572 }
1573
1574 static t_CKFLOAT compute_kurtosis( Chuck_ArrayFloat & input )
1575 {
1576 t_CKINT i;
1577 t_CKFLOAT z = 0.0, mean = 0.0, q = 0.0, b;
1578 for( i = 0; i < input.size(); i++ )
1579 {
1580 mean += input.m_vector[i];
1581 }
1582 mean /= input.size();
1583 for( i = 0; i < input.size(); i++ )
1584 {
1585 input.m_vector[i] -= mean;
1586 b = input.m_vector[i];
1587
1588 z += ( b * b * b * b );
1589 q += ( b * b );
1590 }
1591 q = q * q;
1592 q = q / input.size();
1593
1594 if( ( z < 1.0e-45 ) || ( q < 1.0e-45 ) )
1595 return 0.5;
1596 else
1597 {
1598 return ( z / q ) - 3.0;
1599 }
1600 }
1601
1602 CK_DLL_TICK( Kurtosis_tick )
1603 {
1604 // do nothing
1605 return TRUE;
1606 }
1607
1608 CK_DLL_TOCK( Kurtosis_tock )
1609 {
1610 t_CKFLOAT result = 0.0;
1611
1612 // TODO: get buffer from stream, and set in SVM
1613 if( UANA->numIncomingUAnae() > 0 )
1614 {
1615 // get first
1616 Chuck_UAnaBlobProxy * BLOB_IN = UANA->getIncomingBlob( 0 );
1617 // sanity check
1618 assert( BLOB_IN != NULL );
1619 // get the array
1620 Chuck_ArrayFloat & mag = BLOB_IN->fvals();
1621 // compute Kurtosis
1622 result = compute_kurtosis( mag );
1623 }
1624 // otherwise zero out
1625 else
1626 {
1627 // no input!
1628 result = 0.0;
1629 }
1630
1631 // get fvals of output BLOB
1632 Chuck_ArrayFloat & fvals = BLOB->fvals();
1633 // ensure size == resulting size
1634 if( fvals.size() != 1 )
1635 fvals.set_size( 1 );
1636 // copy the result in
1637 fvals.set( 0, result );
1638
1639 return TRUE;
1640 }
1641
1642 CK_DLL_PMSG( Kurtosis_pmsg )
1643 {
1644 // do nothing
1645 return TRUE;
1646 }
1647
1648 CK_DLL_SFUN( Kurtosis_compute )
1649 {
1650 // get array
1651 Chuck_ArrayFloat * array = (Chuck_ArrayFloat *)GET_NEXT_OBJECT( ARGS );
1652 // sanity check
1653 if( !array )
1654 {
1655 // no Kurtosis
1656 RETURN->v_float = 0.0;
1657 }
1658 else
1659 {
1660 // do it
1661 RETURN->v_float = compute_kurtosis( *array );
1662 }
1663 }
1664
1665 // 1.5.0.0 (yikai) | SFM implementation
1666 // Yikai Li, Winter 2023
1667 struct SFM_Object
1668 {
1669 t_CKINT size;
1670 t_CKFLOAT sample_rate;
1671 t_CKINT nr_bands;
1672 t_CKINT nr_valid_bands;
1673
1674 t_CKFLOAT * edge;
1675 t_CKFLOAT * bandLoEdge;
1676 t_CKFLOAT * bandHiEdge;
1677 t_CKINT * il;
1678 t_CKINT * ih;
1679
1680 // static sfm instance
1681 static SFM_Object * ourSFM;
1682
1683 // constructor
1684 SFM_Object()
1685 {
1686 size = 0;
1687 sample_rate = g_uana_srate;
1688 nr_bands = 0;
1689 nr_valid_bands = 0;
1690 edge = NULL;
1691 bandLoEdge = NULL;
1692 bandHiEdge = NULL;
1693 il = NULL;
1694 ih = NULL;
1695 this->update( size );
1696 }
1697
1698 // destructor
1699 ~SFM_Object()
1700 {
1701 size = 0;
1702 sample_rate = 0;
1703 nr_bands = 0;
1704 nr_valid_bands = 0;
1705 CK_SAFE_DELETE_ARRAY( edge );
1706 CK_SAFE_DELETE_ARRAY( bandLoEdge );
1707 CK_SAFE_DELETE_ARRAY( bandHiEdge );
1708 CK_SAFE_DELETE_ARRAY( il );
1709 CK_SAFE_DELETE_ARRAY( ih );
1710 }
1711
1712 // update
1713 void update( t_CKINT theSize )
1714 {
1715 if( theSize == this->size ) return;
1716
1717 //MPEG-7 audio standard:
1718 //assumes an 1/4 octave frequency resolution,
1719 //resulting in 24 frequency bands between 250Hz and 16kHz.
1720 //If the signal under analysis does not contain frequencies
1721 //above a determined value (e.g. due to signal sampling rate or
1722 //bandwidth limitations), the nr of bands should be reduced.
1723
1724 t_CKINT i;
1725
1726 this->nr_bands = 24;
1727 this->nr_valid_bands = this->nr_bands;
1728
1729 CK_SAFE_DELETE_ARRAY( edge );
1730 edge = new t_CKFLOAT[nr_bands + 1];
1731 memset( edge, 0, sizeof( t_CKFLOAT ) * ( nr_bands + 1 ) );
1732 CK_SAFE_DELETE( bandLoEdge );
1733 bandLoEdge = new t_CKFLOAT[nr_bands];
1734 memset( bandLoEdge, 0, sizeof( t_CKFLOAT ) * nr_bands );
1735 CK_SAFE_DELETE( bandHiEdge );
1736 bandHiEdge = new t_CKFLOAT[nr_bands];
1737 memset( bandHiEdge, 0, sizeof( t_CKFLOAT ) * nr_bands );
1738
1739 //nominal band edges (Hz)
1740 for( i = 0; i < this->nr_bands + 1; ++i )
1741 {
1742 edge[i] = 1000.0 * ::pow( 2.0, ( 0.25 * ( i - 8 ) ) ); // 1/4 octave resolution (MPEG7)
1743 }
1744 // overlapped low and high band edges (Hz)
1745 for( i = 0; i < this->nr_bands; ++i )
1746 {
1747 bandLoEdge[i] = edge[i] * 0.95f; //band overlapping (MPEG7)
1748 bandHiEdge[i] = edge[i + 1] * 1.05f; //band overlapping (MPEG7)
1749 }
1750
1751 // spectrum sampling rate - not audio TODO: check this
1752 // this->sample_rate = ctrl_israte_->to<t_CKFLOAT>();
1753 t_CKFLOAT df = this->sample_rate / theSize;
1754
1755 //calculate FFT bin indexes for each band's edges
1756 CK_SAFE_DELETE( il );
1757 il = new t_CKINT[nr_bands];
1758 memset( il, 0, sizeof( t_CKINT ) * nr_bands );
1759 CK_SAFE_DELETE( ih );
1760 ih = new t_CKINT[nr_bands];
1761 memset( ih, 0, sizeof( t_CKINT ) * nr_bands );
1762 for( i = 0; i < this->nr_bands; ++i )
1763 {
1764
1765 il[i] = (t_CKINT)( bandLoEdge[i] / df + 0.5f ); //round to nearest int (MPEG7)
1766 ih[i] = (t_CKINT)( bandHiEdge[i] / df + 0.5f ); //round to nearest int (MPEG7)
1767
1768 //must verify if sampling rate is enough
1769 //for the specified nr of bands. If not,
1770 //reduce nr of valid freq. bands
1771 if( ih[i] >= theSize) //if ih[i] >= N/2+1 = spectrumSize_ = inObservations ...
1772 {
1773 this->nr_valid_bands = i;
1774 break;
1775 }
1776 }
1777 }
1778 };
1779
1780 // static initialization
1781 SFM_Object * SFM_Object::ourSFM = NULL;
1782
1783 // compute sfm
1784 static void compute_sfm( SFM_Object * sfm, Chuck_ArrayFloat & input, Chuck_ArrayFloat & output )
1785 {
1786 // update
1787 sfm->update( input.size() );
1788 t_CKINT i, k, bandwidth;
1789 t_CKFLOAT c, geoMean, aritMean;
1790
1791 //default SFM value = 1.0; (MPEG7 defines SFM=1.0 for silence)
1792 output.set_size( sfm->nr_valid_bands );
1793 for( i = 0; i < sfm->nr_valid_bands; ++i )
1794 {
1795 output.m_vector[i] = 1.0;
1796 }
1797
1798 //MPEG7 defines a grouping mechanism for the frequency bands above 1KHz
1799 //in order to reduce computational effort of the following calculation.
1800 //For now such grouping mechanism is not implemented...
1801 for( i = 0; i < sfm->nr_valid_bands; ++i )
1802 {
1803 geoMean = 1.0;
1804 aritMean = 0.0;
1805 bandwidth = sfm->ih[i] - sfm->il[i] + 1;
1806
1807 for( k = sfm->il[i]; k <= sfm->ih[i]; k++ )
1808 {
1809 c = input.m_vector[k]; //power spectrum coeff
1810 aritMean += c / bandwidth;
1811 geoMean *= ::pow( c, (t_CKFLOAT)1.0 / bandwidth );
1812 }
1813 if( aritMean != 0.0 )
1814 {
1815 output.m_vector[i] = geoMean / aritMean;
1816 }
1817 //else //mean power = 0 => silence...
1818 // out(i) = 1.0; //MPEG-7
1819 }
1820
1821 //for freq bands above the nyquist freq
1822 //return SFM value defined in MPEG7 for silence
1823 //for(i = nrValidBands_; i < this->nr_bands; ++i)
1824 // out(i) = 1.0;
1825 }
1826
1827
1828 // SFM
1829 CK_DLL_CTOR( SFM_ctor )
1830 {
1831 SFM_Object * sfm = new SFM_Object();
1832 OBJ_MEMBER_UINT( SELF, SFM_offset_data ) = (t_CKUINT)sfm;
1833 }
1834
1835 CK_DLL_DTOR( SFM_dtor )
1836 {
1837 SFM_Object * sfm = (SFM_Object *)OBJ_MEMBER_UINT( SELF, SFM_offset_data );
1838 CK_SAFE_DELETE( sfm );
1839 OBJ_MEMBER_UINT( SELF, SFM_offset_data ) = 0;
1840 }
1841
1842 CK_DLL_TICK( SFM_tick )
1843 {
1844 // do nothing
1845 return TRUE;
1846 }
1847
1848 CK_DLL_TOCK( SFM_tock )
1849 {
1850 // get object
1851 SFM_Object * sfm = (SFM_Object *)OBJ_MEMBER_UINT( SELF, SFM_offset_data );
1852
1853 if( UANA->numIncomingUAnae() > 0 )
1854 {
1855 // get first
1856 Chuck_UAnaBlobProxy * BLOB_IN = UANA->getIncomingBlob( 0 );
1857 // sanity check
1858 assert( BLOB_IN != NULL );
1859 // get the array
1860 Chuck_ArrayFloat & mag = BLOB_IN->fvals();
1861 // get fvals of output BLOB
1862 Chuck_ArrayFloat & fvals = BLOB->fvals();
1863 // compute SFM
1864 compute_sfm( sfm, mag, fvals );
1865 }
1866 // otherwise zero out
1867 else
1868 {
1869 // get fvals of output BLOB
1870 Chuck_ArrayFloat & fvals = BLOB->fvals();
1871 // resize
1872 fvals.set_size( 0 );
1873 }
1874
1875 return TRUE;
1876 }
1877
1878
1879 CK_DLL_PMSG( SFM_pmsg )
1880 {
1881 // do nothing
1882 return TRUE;
1883 }
1884 CK_DLL_CTRL( SFM_ctrl_nr_bands )
1885 {
1886 // get object
1887 SFM_Object * sfm = (SFM_Object *)OBJ_MEMBER_UINT( SELF, SFM_offset_data );
1888 // get nr_bands
1889 sfm->nr_bands = GET_NEXT_INT( ARGS );
1890 // return it
1891 RETURN->v_int = sfm->nr_bands;
1892 }
1893
1894 CK_DLL_CGET( SFM_cget_nr_bands )
1895 {
1896 // get object
1897 SFM_Object * sfm = (SFM_Object *)OBJ_MEMBER_UINT( SELF, SFM_offset_data );
1898 // return it
1899 RETURN->v_int = sfm->nr_bands;
1900 }
1901
1902 CK_DLL_MFUN( SFM_compute )
1903 {
1904 // get object
1905 SFM_Object * sfm = (SFM_Object *)OBJ_MEMBER_UINT( SELF, SFM_offset_data );
1906 // get input
1907 Chuck_ArrayFloat * input = (Chuck_ArrayFloat *)GET_NEXT_OBJECT( ARGS );
1908 // get output
1909 Chuck_ArrayFloat * output = (Chuck_ArrayFloat *)GET_NEXT_OBJECT( ARGS );
1910 // compute sfm
1911 compute_sfm( sfm, *input, *output );
1912 }
1913
1914 // 1.5.0.0 (yikai) | Chroma implementation
1915 // Yikai Li, Winter 2023
1916 struct Chroma_Object
1917 {
1918 t_CKINT size;
1919 t_CKFLOAT sample_rate;
1920 t_CKINT low_oct_num;
1921 t_CKINT high_oct_num;
1922 t_CKFLOAT curr_sample_rate;
1923 t_CKINT curr_low_oct_num;
1924 t_CKINT curr_high_oct_num;
1925
1926 t_CKFLOAT * m;
1927 t_CKFLOAT * freq;
1928 t_CKFLOAT * filter;
1929 t_CKFLOAT * chord;
1930
1931 // static chroma instance
1932 static Chroma_Object * ourChroma;
1933
1934 // constructor
1935 Chroma_Object()
1936 {
1937 size = 1024;
1938 sample_rate = g_uana_srate;
1939 low_oct_num = 0;
1940 high_oct_num = 8;
1941 curr_sample_rate = 0;
1942 curr_high_oct_num = 0;
1943 curr_low_oct_num = 0;
1944 m = NULL;
1945 freq = NULL;
1946 filter = NULL;
1947 chord = NULL;
1948 this->update( size );
1949 }
1950
1951 // destructor
1952 ~Chroma_Object()
1953 {
1954 // delete
1955 CK_SAFE_DELETE_ARRAY( m );
1956 CK_SAFE_DELETE_ARRAY( freq );
1957 CK_SAFE_DELETE_ARRAY( filter );
1958 CK_SAFE_DELETE_ARRAY( chord );
1959 // zero out
1960 size = 0;
1961 sample_rate = 0;
1962 low_oct_num = 0;
1963 high_oct_num = 0;
1964 curr_sample_rate = 0;
1965 curr_high_oct_num = 0;
1966 curr_low_oct_num = 0;
1967 }
1968
1969 // update
1970 void update( t_CKINT theSize )
1971 {
1972 if( this->size == theSize
1973 && this->curr_sample_rate == this->sample_rate
1974 && this->curr_low_oct_num == this->low_oct_num
1975 && this->curr_high_oct_num == this->high_oct_num )
1976 return;
1977
1978 this->size = theSize;
1979 this->curr_sample_rate = this->sample_rate;
1980 this->curr_low_oct_num = this->low_oct_num;
1981 this->curr_high_oct_num = this->high_oct_num;
1982
1983 t_CKINT i, j, k;
1984 t_CKFLOAT tmp;
1985
1986 // memory allocation and initialization
1987 CK_SAFE_DELETE_ARRAY( m );
1988 m = new t_CKFLOAT[9];
1989 memset( m, 0, sizeof( t_CKFLOAT ) * 9 );
1990 CK_SAFE_DELETE_ARRAY( freq );
1991 freq = new t_CKFLOAT[theSize];
1992 memset( freq, 0, sizeof( t_CKFLOAT ) * theSize);
1993 CK_SAFE_DELETE_ARRAY( filter );
1994 filter = new t_CKFLOAT[14 * theSize];
1995 memset( filter, 0, sizeof( t_CKFLOAT ) * 14 * theSize);
1996 CK_SAFE_DELETE_ARRAY( chord );
1997 chord = new t_CKFLOAT[14];
1998 memset( chord, 0, sizeof( t_CKFLOAT ) * 14 );
1999 chord[1] = 261.625565; // C4
2000 chord[2] = 277.182630; // C♯/D♭4
2001 chord[3] = 293.664747; // D4
2002 chord[4] = 311.126983; // D♯/E♭4
2003 chord[5] = 329.627556; // E4
2004 chord[6] = 349.228231; // F4
2005 chord[7] = 369.994422; // F♯/G♭4
2006 chord[8] = 391.995435; // G4
2007 chord[9] = 415.304697; // G♯/A♭4
2008 chord[10] = 440.000000; // A4
2009 chord[11] = 466.163761; // A♯/B♭4
2010 chord[12] = 493.883301; // B4
2011 chord[0] = 0.5 * chord[12];
2012 chord[13] = 2.0 * chord[1];
2013
2014 for( i = 0; i < 9; ++i )
2015 {
2016 m[i] = ::pow( 2.0, (t_CKFLOAT)i - 3.0 );
2017 }
2018 for( i = 0; i < theSize; ++i )
2019 {
2020 freq[i] =
2021 this->sample_rate * (t_CKFLOAT)i / ( 2.0 * (t_CKFLOAT)(theSize - 1) );
2022 }
2023
2024 // create filter
2025 for( i = 1; i < 13; ++i )
2026 {
2027 for( k = 0; k < theSize - 1; k++ )
2028 {
2029 for( j = this->low_oct_num; j < this->high_oct_num + 1; j++ )
2030 {
2031 if( freq[k] < m[j] * chord[i] && freq[k + 1] >= m[j] * chord[i] )
2032 {
2033 filter[i * theSize + k] += ( freq[k + 1] - m[j] * chord[i] ) / ( freq[k + 1] - freq[k] );
2034 filter[i * theSize + k + 1] += ( m[j] * chord[i] - freq[k] ) / ( freq[k + 1] - freq[k] );
2035 }
2036 if( ( m[j] * chord[i] + m[j] * chord[i - 1] ) / 2.0 < freq[k]
2037 && freq[k] <= ( m[j] * chord[i + 1] + m[j] * chord[i] ) / 2.0 )
2038 {
2039 filter[i * theSize + k] += 1.0;
2040 }
2041 }
2042 }
2043 }
2044
2045 for( k = 0; k < theSize; k++ )
2046 {
2047 tmp = 0.0;
2048 for( i = 1; i < 13; ++i )
2049 {
2050 tmp += filter[i * theSize + k];
2051 }
2052 if( tmp > 0 )
2053 {
2054 for( i = 1; i < 13; ++i )
2055 {
2056 filter[i * theSize + k] /= tmp;
2057 }
2058 }
2059 }
2060
2061 }
2062 };
2063
2064 // static initialization
2065 Chroma_Object * Chroma_Object::ourChroma = NULL;
2066
2067 // compute chroma
2068 static void compute_chroma( Chroma_Object * chroma, Chuck_ArrayFloat & input, Chuck_ArrayFloat & output )
2069 {
2070 // prepare
2071 chroma->update( input.size() );
2072 // compute
2073 output.set_size( 12 );
2074 for( t_CKINT i = 0; i < 12; ++i )
2075 {
2076 output.m_vector[i] = 0.0;
2077 for( t_CKINT j = 0; j < input.size(); ++j )
2078 {
2079 output.m_vector[i] += input.m_vector[j] * chroma->filter[( i + 1 ) * input.size() + j];
2080 }
2081 }
2082 }
2083
2084
2085 // Chroma
2086 CK_DLL_CTOR( Chroma_ctor )
2087 {
2088 Chroma_Object * chroma = new Chroma_Object();
2089 OBJ_MEMBER_UINT( SELF, Chroma_offset_data ) = (t_CKUINT)chroma;
2090 }
2091
2092 CK_DLL_DTOR( Chroma_dtor )
2093 {
2094 Chroma_Object * chroma = (Chroma_Object *)OBJ_MEMBER_UINT( SELF, Chroma_offset_data );
2095 CK_SAFE_DELETE( chroma );
2096 OBJ_MEMBER_UINT( SELF, Chroma_offset_data ) = 0;
2097 }
2098
2099 CK_DLL_TICK( Chroma_tick )
2100 {
2101 // do nothing
2102 return TRUE;
2103 }
2104
2105 CK_DLL_TOCK( Chroma_tock )
2106 {
2107 // get object
2108 Chroma_Object * chroma = (Chroma_Object *)OBJ_MEMBER_UINT( SELF, Chroma_offset_data );
2109
2110 if( UANA->numIncomingUAnae() > 0 )
2111 {
2112 // get first
2113 Chuck_UAnaBlobProxy * BLOB_IN = UANA->getIncomingBlob( 0 );
2114 // sanity check
2115 assert( BLOB_IN != NULL );
2116 // get the array
2117 Chuck_ArrayFloat & mag = BLOB_IN->fvals();
2118 // get fvals of output BLOB
2119 Chuck_ArrayFloat & fvals = BLOB->fvals();
2120 // compute Chroma
2121 compute_chroma( chroma, mag, fvals );
2122 }
2123 // otherwise zero out
2124 else
2125 {
2126 // get fvals of output BLOB
2127 Chuck_ArrayFloat & fvals = BLOB->fvals();
2128 // resize
2129 fvals.set_size( 0 );
2130 }
2131
2132 return TRUE;
2133 }
2134
2135
2136 CK_DLL_PMSG( Chroma_pmsg )
2137 {
2138 // do nothing
2139 return TRUE;
2140 }
2141 CK_DLL_CTRL( Chroma_ctrl_sample_rate )
2142 {
2143 // get object
2144 Chroma_Object * chroma = (Chroma_Object *)OBJ_MEMBER_UINT( SELF, Chroma_offset_data );
2145 // get sample_rate
2146 chroma->sample_rate = GET_NEXT_FLOAT( ARGS );
2147 RETURN->v_float = chroma->sample_rate;
2148 }
2149
2150 CK_DLL_CGET( Chroma_cget_sample_rate )
2151 {
2152 // get object
2153 Chroma_Object * chroma = (Chroma_Object *)OBJ_MEMBER_UINT( SELF, Chroma_offset_data );
2154 // return it
2155 RETURN->v_float = chroma->sample_rate;
2156 }
2157
2158 CK_DLL_CTRL( Chroma_ctrl_low_oct_num )
2159 {
2160 // get object
2161 Chroma_Object * chroma = (Chroma_Object *)OBJ_MEMBER_UINT( SELF, Chroma_offset_data );
2162 // get low_oct_num
2163 t_CKINT low_oct_num = GET_NEXT_INT( ARGS );
2164 if( low_oct_num < 0 )
2165 {
2166 low_oct_num = 0;
2167 }
2168 chroma->low_oct_num = low_oct_num;
2169 // return it
2170 RETURN->v_int = chroma->low_oct_num;
2171 }
2172
2173 CK_DLL_CGET( Chroma_cget_low_oct_num )
2174 {
2175 // get object
2176 Chroma_Object * chroma = (Chroma_Object *)OBJ_MEMBER_UINT( SELF, Chroma_offset_data );
2177 // return it
2178 RETURN->v_int = chroma->low_oct_num;
2179 }
2180
2181 CK_DLL_CTRL( Chroma_ctrl_high_oct_num )
2182 {
2183 // get object
2184 Chroma_Object * chroma = (Chroma_Object *)OBJ_MEMBER_UINT( SELF, Chroma_offset_data );
2185 // get high_oct_num
2186 t_CKINT high_oct_num = GET_NEXT_INT( ARGS );
2187 if( high_oct_num > 8 )
2188 {
2189 high_oct_num = 8;
2190 }
2191 chroma->high_oct_num = high_oct_num;
2192 // return it
2193 RETURN->v_int = chroma->high_oct_num;
2194 }
2195
2196 CK_DLL_CGET( Chroma_cget_high_oct_num )
2197 {
2198 // get object
2199 Chroma_Object * chroma = (Chroma_Object *)OBJ_MEMBER_UINT( SELF, Chroma_offset_data );
2200 // return it
2201 RETURN->v_int = chroma->high_oct_num;
2202 }
2203
2204 CK_DLL_MFUN( Chroma_compute )
2205 {
2206 // get object
2207 Chroma_Object * chroma = (Chroma_Object *)OBJ_MEMBER_UINT( SELF, Chroma_offset_data );
2208 // get input
2209 Chuck_ArrayFloat * input = (Chuck_ArrayFloat *)GET_NEXT_OBJECT( ARGS );
2210 // get output
2211 Chuck_ArrayFloat * output = (Chuck_ArrayFloat *)GET_NEXT_OBJECT( ARGS );
2212 // compute chroma
2213 compute_chroma( chroma, *input, *output );
2214 }
2215
2216 static t_CKFLOAT compute_rolloff( Chuck_ArrayFloat & buffer, t_CKUINT size, t_CKFLOAT percent )
2217 {
2218 t_CKFLOAT sum = 0.0, v, target;
2219 t_CKINT i;
2220
2221 // sanity check
2222 assert( percent >= 0 && percent <= 1 );
2223
2224 // iterate
2225 for( i = 0; i < size; i++ )
2226 {
2227 buffer.get( i, &v );
2228 sum += v;
2229 }
2230
2231 // the target
2232 target = sum * percent;
2233 sum = 0.0;
2234
2235 // iterate
2236 for( i = 0; i < size; i++ )
2237 {
2238 buffer.get( i, &v );
2239 sum += v;
2240 if( sum >= target ) break;
2241 }
2242
2243 return i / (t_CKFLOAT)size;
2244 }
2245
2246 CK_DLL_CTOR( RollOff_ctor )
2247 {
2248 OBJ_MEMBER_FLOAT( SELF, RollOff_offset_percent ) = .85;
2249 }
2250
2251 CK_DLL_DTOR( RollOff_dtor )
2252 {
2253 }
2254
2255 CK_DLL_TICK( RollOff_tick )
2256 {
2257 // do nothing
2258 return TRUE;
2259 }
2260
2261 CK_DLL_TOCK( RollOff_tock )
2262 {
2263 t_CKFLOAT result = 0.0;
2264
2265 // get percent
2266 t_CKFLOAT percent = OBJ_MEMBER_FLOAT( SELF, RollOff_offset_percent );
2267
2268 // TODO: get buffer from stream, and set in SVM
2269 if( UANA->numIncomingUAnae() > 0 )
2270 {
2271 // get first
2272 Chuck_UAnaBlobProxy * BLOB_IN = UANA->getIncomingBlob( 0 );
2273 // sanity check
2274 assert( BLOB_IN != NULL );
2275 // get the array
2276 Chuck_ArrayFloat & mag = BLOB_IN->fvals();
2277 // compute rolloff
2278 result = compute_rolloff( mag, mag.size(), percent );
2279 }
2280 // otherwise zero out
2281 else
2282 {
2283 // no input!
2284 result = 0.0;
2285 }
2286
2287 // get fvals of output BLOB
2288 Chuck_ArrayFloat & fvals = BLOB->fvals();
2289 // ensure size == resulting size
2290 if( fvals.size() != 1 )
2291 fvals.set_size( 1 );
2292 // copy the result in
2293 fvals.set( 0, result );
2294
2295 return TRUE;
2296 }
2297
2298 CK_DLL_PMSG( RollOff_pmsg )
2299 {
2300 // do nothing
2301 return TRUE;
2302 }
2303
2304 CK_DLL_CTRL( RollOff_ctrl_percent )
2305 {
2306 // get percent
2307 t_CKFLOAT percent = GET_NEXT_FLOAT( ARGS );
2308 // check it
2309 if( percent < 0.0 ) percent = 0.0;
2310 else if( percent > 1.0 ) percent = 1.0;
2311 // set it
2312 OBJ_MEMBER_FLOAT( SELF, RollOff_offset_percent ) = percent;
2313 // return it
2314 RETURN->v_float = percent;
2315 }
2316
2317 CK_DLL_CGET( RollOff_cget_percent )
2318 {
2319 // return it
2320 RETURN->v_float = OBJ_MEMBER_FLOAT( SELF, RollOff_offset_percent );
2321 }
2322
2323 CK_DLL_SFUN( RollOff_compute )
2324 {
2325 // get array
2326 Chuck_ArrayFloat * array = (Chuck_ArrayFloat *)GET_NEXT_OBJECT( ARGS );
2327 // get percent
2328 t_CKFLOAT percent = GET_NEXT_FLOAT( ARGS );
2329
2330 // sanity check
2331 if( !array )
2332 {
2333 // no RollOff
2334 RETURN->v_float = 0.0;
2335 }
2336 else
2337 {
2338 // do it
2339 RETURN->v_float = compute_rolloff( *array, array->size(), percent );
2340 }
2341 }
2342
2343 // struct
2344 struct Corr_Object
2345 {
2346 // whether to normalize
2347 t_CKBOOL normalize;
2348 // f
2349 SAMPLE * fbuf;
2350 t_CKINT fcap;
2351 // g
2352 SAMPLE * gbuf;
2353 t_CKINT gcap;
2354 // result
2355 SAMPLE * buffy;
2356 t_CKINT bufcap;
2357
2358 // static corr instance
2359 static Corr_Object * ourCorr;
2360
2361 // constructor
2362 Corr_Object()
2363 {
2364 // default: do normalize
2365 normalize = FALSE;
2366 // zero out pointers
2367 fbuf = gbuf = buffy = NULL;
2368 fcap = gcap = bufcap = 0;
2369 // TODO: default
2370 resize( 512, 512 );
2371 }
2372
2373 // destructor
2374 ~Corr_Object()
2375 {
2376 // done
2377 this->reset();
2378 }
2379
2380 // reset
2381 void reset()
2382 {
2383 CK_SAFE_DELETE_ARRAY( fbuf );
2384 CK_SAFE_DELETE_ARRAY( gbuf );
2385 CK_SAFE_DELETE_ARRAY( buffy );
2386 fcap = gcap = bufcap = 0;
2387 }
2388
2389 // clear
2390 void clear()
2391 {
2392 if( fbuf ) memset( fbuf, 0, fcap * sizeof( SAMPLE ) );
2393 if( gbuf ) memset( gbuf, 0, gcap * sizeof( SAMPLE ) );
2394 if( buffy ) memset( buffy, 0, bufcap * sizeof( SAMPLE ) );
2395 }
2396
2397 // resize inputs
2398 t_CKBOOL resize( t_CKINT fs, t_CKINT gs )
2399 {
2400 // minimum size
2401 t_CKINT mincap = fs + gs - 1;
2402 // ensure power of two
2403 mincap = ck_ensurepow2( mincap );
2404 // log
2405 EM_log( CK_LOG_FINE, "Corr resizing to %d-element buffers...", mincap );
2406
2407 // verify
2408 if( fcap < mincap )
2409 {
2410 CK_SAFE_DELETE_ARRAY( fbuf );
2411 fbuf = new SAMPLE[mincap];
2412 fcap = mincap;
2413 }
2414 if( gcap < mincap )
2415 {
2416 CK_SAFE_DELETE_ARRAY( gbuf );
2417 gbuf = new SAMPLE[mincap];
2418 gcap = mincap;
2419 }
2420 if( bufcap < mincap )
2421 {
2422 CK_SAFE_DELETE_ARRAY( buffy );
2423 buffy = new SAMPLE[mincap];
2424 bufcap = mincap;
2425 }
2426
2427 // hopefully
2428 if( fbuf == NULL || gbuf == NULL || buffy == NULL )
2429 {
2430 // error
2431 CK_FPRINTF_STDERR( "[chuck]: Corr failed to allocate %ld-element buffer(s)...",
2432 mincap );
2433 // clean up
2434 this->reset();
2435 // done
2436 return FALSE;
2437 }
2438
2439 // clear it
2440 this->clear();
2441
2442 return TRUE;
2443 }
2444
2445 // get our singleton
2446 static Corr_Object * getOurObject()
2447 {
2448 // instantiate, if needed
2449 if( ourCorr == NULL )
2450 {
2451 ourCorr = new Corr_Object();
2452 assert( ourCorr != NULL );
2453 }
2454
2455 // return new instance
2456 return ourCorr;
2457 }
2458 };
2459
2460 // static initialization
2461 Corr_Object * Corr_Object::ourCorr = NULL;
2462
2463 // compute correlation
2464 static void compute_corr( Corr_Object * corr, Chuck_ArrayFloat & f, t_CKINT fs,
2465 Chuck_ArrayFloat & g, t_CKINT gs, Chuck_ArrayFloat & buffy )
2466 {
2467 t_CKINT i;
2468 t_CKFLOAT v;
2469 t_CKINT size;
2470
2471 // ensure size
2472 corr->resize( fs, gs );
2473
2474 // copy into buffers
2475 for( i = 0; i < fs; i++ )
2476 {
2477 f.get( i, &v );
2478 corr->fbuf[i] = v;
2479 }
2480 for( i = 0; i < gs; i++ )
2481 {
2482 g.get( i, &v );
2483 corr->gbuf[i] = v;
2484 }
2485
2486 // compute
2487 xcorr_fft( corr->fbuf, corr->fcap, corr->gbuf, corr->gcap,
2488 corr->buffy, corr->bufcap );
2489
2490 // check flags
2491 if( corr->normalize )
2492 {
2493 // normalize
2494 xcorr_normalize( corr->buffy, corr->bufcap,
2495 corr->fbuf, corr->fcap, corr->gbuf, corr->gcap );
2496 }
2497
2498 // copy into result
2499 size = fs + gs - 1;
2500 buffy.set_size( size );
2501 for( i = 0; i < size; i++ )
2502 {
2503 buffy.set( i, corr->buffy[i] );
2504 }
2505 }
2506
2507 // AutoCorr
2508 CK_DLL_CTOR( AutoCorr_ctor )
2509 {
2510 Corr_Object * ac = new Corr_Object();
2511 OBJ_MEMBER_UINT( SELF, AutoCorr_offset_data ) = (t_CKUINT)ac;
2512 }
2513
2514 CK_DLL_DTOR( AutoCorr_dtor )
2515 {
2516 Corr_Object * ac = (Corr_Object *)OBJ_MEMBER_UINT( SELF, AutoCorr_offset_data );
2517 CK_SAFE_DELETE( ac );
2518 OBJ_MEMBER_UINT( SELF, AutoCorr_offset_data ) = 0;
2519 }
2520
2521 CK_DLL_TICK( AutoCorr_tick )
2522 {
2523 // do nothing
2524 return TRUE;
2525 }
2526
2527 CK_DLL_TOCK( AutoCorr_tock )
2528 {
2529 // get object
2530 Corr_Object * ac = (Corr_Object *)OBJ_MEMBER_UINT( SELF, AutoCorr_offset_data );
2531
2532 // TODO: get buffer from stream, and set
2533 if( UANA->numIncomingUAnae() > 0 )
2534 {
2535 // get first
2536 Chuck_UAnaBlobProxy * BLOB_IN = UANA->getIncomingBlob( 0 );
2537 // sanity check
2538 assert( BLOB_IN != NULL );
2539 // get the array
2540 Chuck_ArrayFloat & mag = BLOB_IN->fvals();
2541 // get fvals of output BLOB
2542 Chuck_ArrayFloat & fvals = BLOB->fvals();
2543 // compute autocorr
2544 compute_corr( ac, mag, mag.size(), mag, mag.size(), fvals );
2545 }
2546 // otherwise zero out
2547 else
2548 {
2549 // get fvals of output BLOB
2550 Chuck_ArrayFloat & fvals = BLOB->fvals();
2551 // resize
2552 fvals.set_size( 0 );
2553 }
2554
2555 return TRUE;
2556 }
2557
2558 CK_DLL_PMSG( AutoCorr_pmsg )
2559 {
2560 // do nothing
2561 return TRUE;
2562 }
2563
2564 CK_DLL_CTRL( AutoCorr_ctrl_normalize )
2565 {
2566 // get object
2567 Corr_Object * ac = (Corr_Object *)OBJ_MEMBER_UINT( SELF, AutoCorr_offset_data );
2568 // get percent
2569 ac->normalize = GET_NEXT_INT( ARGS ) != 0;
2570 // return it
2571 RETURN->v_int = ac->normalize;
2572 }
2573
2574 CK_DLL_CGET( AutoCorr_cget_normalize )
2575 {
2576 // get object
2577 Corr_Object * ac = (Corr_Object *)OBJ_MEMBER_UINT( SELF, AutoCorr_offset_data );
2578 // return it
2579 RETURN->v_int = ac->normalize;
2580 }
2581
2582 CK_DLL_SFUN( AutoCorr_compute )
2583 {
2584 // get input
2585 Chuck_ArrayFloat * input = (Chuck_ArrayFloat *)GET_NEXT_OBJECT( ARGS );
2586 // get normalize flag
2587 t_CKINT normalize = GET_NEXT_INT( ARGS ) != 0;
2588 // get input
2589 Chuck_ArrayFloat * output = (Chuck_ArrayFloat *)GET_NEXT_OBJECT( ARGS );
2590
2591 // set normalize
2592 Corr_Object::getOurObject()->normalize = normalize;
2593 // compute autocrr
2594 compute_corr( Corr_Object::getOurObject(), *input, input->size(),
2595 *input, input->size(), *output );
2596 }
2597
2598
2599 // XCorr
2600 CK_DLL_CTOR( XCorr_ctor )
2601 {
2602 Corr_Object * xc = new Corr_Object();
2603 OBJ_MEMBER_UINT( SELF, XCorr_offset_data ) = (t_CKUINT)xc;
2604 }
2605
2606 CK_DLL_DTOR( XCorr_dtor )
2607 {
2608 Corr_Object * xc = (Corr_Object *)OBJ_MEMBER_UINT( SELF, XCorr_offset_data );
2609 CK_SAFE_DELETE( xc );
2610 OBJ_MEMBER_UINT( SELF, XCorr_offset_data ) = 0;
2611 }
2612
2613 CK_DLL_TICK( XCorr_tick )
2614 {
2615 // do nothing
2616 return TRUE;
2617 }
2618
2619 CK_DLL_TOCK( XCorr_tock )
2620 {
2621 // get object
2622 Corr_Object * xc = (Corr_Object *)OBJ_MEMBER_UINT( SELF, XCorr_offset_data );
2623
2624 // TODO: get buffer from stream, and set
2625 if( UANA->numIncomingUAnae() > 1 )
2626 {
2627 // get first
2628 Chuck_UAnaBlobProxy * BLOB_F = UANA->getIncomingBlob( 0 );
2629 // get second
2630 Chuck_UAnaBlobProxy * BLOB_G = UANA->getIncomingBlob( 1 );
2631 // sanity check
2632 assert( BLOB_F != NULL && BLOB_G != NULL );
2633 // get the array
2634 Chuck_ArrayFloat & mag_f = BLOB_F->fvals();
2635 Chuck_ArrayFloat & mag_g = BLOB_G->fvals();
2636 // get fvals of output BLOB
2637 Chuck_ArrayFloat & fvals = BLOB->fvals();
2638 // compute xcorr
2639 compute_corr( xc, mag_f, mag_f.size(), mag_g, mag_g.size(), fvals );
2640 }
2641 // otherwise zero out
2642 else
2643 {
2644 // get fvals of output BLOB
2645 Chuck_ArrayFloat & fvals = BLOB->fvals();
2646 // resize
2647 fvals.set_size( 0 );
2648 }
2649
2650 return TRUE;
2651 }
2652
2653 CK_DLL_PMSG( XCorr_pmsg )
2654 {
2655 // do nothing
2656 return TRUE;
2657 }
2658
2659 CK_DLL_CTRL( XCorr_ctrl_normalize )
2660 {
2661 // get object
2662 Corr_Object * ac = (Corr_Object *)OBJ_MEMBER_UINT( SELF, XCorr_offset_data );
2663 // get percent
2664 ac->normalize = GET_NEXT_INT( ARGS ) != 0;
2665 // return it
2666 RETURN->v_int = ac->normalize;
2667 }
2668
2669 CK_DLL_CGET( XCorr_cget_normalize )
2670 {
2671 // get object
2672 Corr_Object * ac = (Corr_Object *)OBJ_MEMBER_UINT( SELF, XCorr_offset_data );
2673 // return it
2674 RETURN->v_int = ac->normalize;
2675 }
2676
2677 CK_DLL_SFUN( XCorr_compute )
2678 {
2679 // get input
2680 Chuck_ArrayFloat * f = (Chuck_ArrayFloat *)GET_NEXT_OBJECT( ARGS );
2681 Chuck_ArrayFloat * g = (Chuck_ArrayFloat *)GET_NEXT_OBJECT( ARGS );
2682 // get normalize flag
2683 t_CKINT normalize = GET_NEXT_INT( ARGS ) != 0;
2684 // get output
2685 Chuck_ArrayFloat * output = (Chuck_ArrayFloat *)GET_NEXT_OBJECT( ARGS );
2686
2687 // set normalize
2688 Corr_Object::getOurObject()->normalize = normalize;
2689 // compute autocrr
2690 compute_corr( Corr_Object::getOurObject(), *f, f->size(),
2691 *g, g->size(), *output );
2692 }
2693
2694 //-----------------------------------------------------------------------------
2695 // name: xcorr_fft()
2696 // desc: FFT-based cross correlation
2697 //-----------------------------------------------------------------------------
2698 void xcorr_fft( SAMPLE * f, t_CKINT fsize, SAMPLE * g, t_CKINT gsize, SAMPLE * buffy, t_CKINT size )
2699 {
2700 // sanity check
2701 assert( fsize == gsize && gsize == size );
2702
2703 // take fft
2704 rfft( f, size / 2, FFT_FORWARD );
2705 rfft( g, size / 2, FFT_FORWARD );
2706
2707 // complex
2708 t_CKCOMPLEX_SAMPLE * F = (t_CKCOMPLEX_SAMPLE *)f;
2709 t_CKCOMPLEX_SAMPLE * G = (t_CKCOMPLEX_SAMPLE *)g;
2710 t_CKCOMPLEX_SAMPLE * Y = (t_CKCOMPLEX_SAMPLE *)buffy;
2711
2712 // loop
2713 for( t_CKINT i = 0; i < size / 2; i++ )
2714 {
2715 // conjugate F
2716 F[i].im = -F[i].im;
2717 // complex multiply
2718 Y[i].re = F[i].re * G[i].re - F[i].im * G[i].im;
2719 Y[i].im = F[i].im * G[i].re + F[i].re * G[i].im;
2720 }
2721
2722 // inverse fft
2723 rfft( buffy, size / 2, FFT_INVERSE );
2724 }
2725
2726 //-----------------------------------------------------------------------------
2727 // name: xcorr_normalize()
2728 // desc: ...
2729 //-----------------------------------------------------------------------------
2730 void xcorr_normalize( SAMPLE * buffy, t_CKINT size, SAMPLE * f, t_CKINT fs, SAMPLE * g, t_CKINT gs )
2731 {
2732 float sum = 0.000001f;
2733
2734 // f^2(t)
2735 for( long i = 0; i < fs; i++ )
2736 sum += f[i] * f[i];
2737 // g^2(t)
2738 for( long j = 0; j < gs; j++ )
2739 sum += g[j] * g[j];
2740 // normalize: taking coherence into account
2741 for( long k = 0; k < size; k++ )
2742 buffy[k] /= sum;
2743 }
2744
2745
2746
2747
2748 // ZeroX
2749 #define __SGN( x ) (x >= 0.0f ? 1.0f : -1.0f )
2750 static t_CKINT compute_zerox( Chuck_ArrayFloat & buffer, t_CKUINT size )
2751 {
2752 t_CKUINT i, xings = 0;
2753 t_CKFLOAT v = 0, p = 0;
2754 buffer.get( 0, &p );
2755
2756 // count number of zero crossings
2757 for( i = 0; i < size; i++ )
2758 {
2759 buffer.get( i, &v );
2760 xings += __SGN( v ) != __SGN( p );
2761 p = v;
2762 }
2763
2764 return xings;
2765 }
2766
2767 CK_DLL_CTOR( ZeroX_ctor )
2768 {
2769 OBJ_MEMBER_UINT( SELF, ZeroX_offset_data ) = (t_CKUINT)new SAMPLE( 0.0f );
2770 }
2771
2772 CK_DLL_DTOR( ZeroX_dtor )
2773 {
2774 delete (SAMPLE *)OBJ_MEMBER_UINT( SELF, ZeroX_offset_data );
2775 OBJ_MEMBER_UINT( SELF, ZeroX_offset_data ) = 0;
2776 }
2777
2778 CK_DLL_TICK( ZeroX_tick )
2779 {
2780 SAMPLE * d = (SAMPLE *)OBJ_MEMBER_UINT( SELF, ZeroX_offset_data );
2781 *out = __SGN( in ) != __SGN( *d );
2782 *out *= __SGN( in );
2783 *d = in;
2784
2785 return TRUE;
2786 }
2787
2788 CK_DLL_TOCK( ZeroX_tock )
2789 {
2790 t_CKFLOAT result = 0.0;
2791
2792 // TODO: get buffer from stream, and set in SVM
2793 if( UANA->numIncomingUAnae() > 0 )
2794 {
2795 // get first
2796 Chuck_UAnaBlobProxy * BLOB_IN = UANA->getIncomingBlob( 0 );
2797 // sanity check
2798 assert( BLOB_IN != NULL );
2799 // get the array
2800 Chuck_ArrayFloat & mag = BLOB_IN->fvals();
2801 // compute ZeroX
2802 result = (t_CKFLOAT)( compute_zerox( mag, mag.size() ) + .5 );
2803 }
2804 // otherwise zero out
2805 else
2806 {
2807 // no input!
2808 result = 0.0;
2809 }
2810
2811 // get fvals of output BLOB
2812 Chuck_ArrayFloat & fvals = BLOB->fvals();
2813 // ensure size == resulting size
2814 if( fvals.size() != 1 )
2815 fvals.set_size( 1 );
2816 // copy the result in
2817 fvals.set( 0, result );
2818
2819 return TRUE;
2820 }
2821
2822 CK_DLL_PMSG( ZeroX_pmsg )
2823 {
2824 // do nothing
2825 return TRUE;
2826 }
2827
2828 CK_DLL_SFUN( ZeroX_compute )
2829 {
2830 // get array
2831 Chuck_ArrayFloat * array = (Chuck_ArrayFloat *)GET_NEXT_OBJECT( ARGS );
2832 // sanity check
2833 if( !array )
2834 {
2835 // input buffer was empty, so no zero crossings
2836 RETURN->v_float = 0.0;
2837 }
2838 else
2839 {
2840 // do it
2841 RETURN->v_float = (t_CKFLOAT)( compute_zerox( *array, array->size() ) + .5 );
2842 }
2843 }