2781 lines · 76.0 KB · C++
RawDownload
/*----------------------------------------------------------------------------  ChucK Concurrent, On-the-fly Audio Programming Language    Compiler and Virtual Machine   Copyright (c) 2004 Ge Wang and Perry R. Cook.  All rights reserved.    http://chuck.stanford.edu/    http://chuck.cs.princeton.edu/   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., 59 Temple Place, Suite 330, Boston, MA 02111-1307  U.S.A.-----------------------------------------------------------------------------*/ //-----------------------------------------------------------------------------// file: uana_extract.cpp// desc: unit analyzer extract//// author: Ge Wang (ge@ccrma.stanford.edu | gewang@cs.princeton.edu)//         Rebecca Fiebrink (fiebrink@cs.princeton.edu)// date: Summer 2007//-----------------------------------------------------------------------------#include "uana_extract.h"#include "chuck_type.h"#include "chuck_vm.h"#include "chuck_compile.h"#include "chuck_instr.h"#include "chuck_lang.h"#include "chuck_errmsg.h"#include "util_math.h"#include "util_xforms.h"#include <iostream>    // CentroidCK_DLL_TICK( Centroid_tick );CK_DLL_TOCK( Centroid_tock );CK_DLL_PMSG( Centroid_pmsg );CK_DLL_SFUN( Centroid_compute ); // FluxCK_DLL_CTOR( Flux_ctor );CK_DLL_DTOR( Flux_dtor );CK_DLL_TICK( Flux_tick );CK_DLL_TOCK( Flux_tock );CK_DLL_PMSG( Flux_pmsg );CK_DLL_MFUN( Flux_ctrl_reset );CK_DLL_SFUN( Flux_compute );CK_DLL_SFUN( Flux_compute2 );// offsetstatic t_CKUINT Flux_offset_data = 0; // RMSCK_DLL_TICK( RMS_tick );CK_DLL_TOCK( RMS_tock );CK_DLL_PMSG( RMS_pmsg );CK_DLL_SFUN( RMS_compute ); // MFCC | 1.4.2.0 (yikai) addedCK_DLL_CTOR( MFCC_ctor );CK_DLL_DTOR( MFCC_dtor );CK_DLL_TICK( MFCC_tick );CK_DLL_TOCK( MFCC_tock );CK_DLL_PMSG( MFCC_pmsg );CK_DLL_MFUN( MFCC_compute );CK_DLL_MFUN( MFCC_ctrl_sample_rate );CK_DLL_MFUN( MFCC_cget_sample_rate );CK_DLL_MFUN( MFCC_ctrl_num_filters );CK_DLL_MFUN( MFCC_cget_num_filters );CK_DLL_MFUN( MFCC_ctrl_num_coeffs );CK_DLL_MFUN( MFCC_cget_num_coeffs );// offsetstatic t_CKUINT MFCC_offset_data = 0; // KurtosisCK_DLL_TICK( Kurtosis_tick );CK_DLL_TOCK( Kurtosis_tock );CK_DLL_PMSG( Kurtosis_pmsg );CK_DLL_SFUN( Kurtosis_compute ); // SFM | 1.4.2.1 (yikai) addedCK_DLL_CTOR( SFM_ctor );CK_DLL_DTOR( SFM_dtor );CK_DLL_TICK( SFM_tick );CK_DLL_TOCK( SFM_tock );CK_DLL_PMSG( SFM_pmsg );CK_DLL_MFUN( SFM_compute );CK_DLL_MFUN( SFM_ctrl_nr_bands );CK_DLL_MFUN( SFM_cget_nr_bands );// offsetstatic t_CKUINT SFM_offset_data = 0;  // Chroma | 1.4.2.1 (yikai) addedCK_DLL_CTOR( Chroma_ctor );CK_DLL_DTOR( Chroma_dtor );CK_DLL_TICK( Chroma_tick );CK_DLL_TOCK( Chroma_tock );CK_DLL_PMSG( Chroma_pmsg );CK_DLL_MFUN( Chroma_compute );CK_DLL_MFUN( Chroma_ctrl_sample_rate );CK_DLL_MFUN( Chroma_cget_sample_rate );CK_DLL_MFUN( Chroma_ctrl_low_oct_num );CK_DLL_MFUN( Chroma_cget_low_oct_num );CK_DLL_MFUN( Chroma_ctrl_high_oct_num );CK_DLL_MFUN( Chroma_cget_high_oct_num );// offsetstatic t_CKUINT Chroma_offset_data = 0; // RollOffCK_DLL_CTOR( RollOff_ctor );CK_DLL_DTOR( RollOff_dtor );CK_DLL_TICK( RollOff_tick );CK_DLL_TOCK( RollOff_tock );CK_DLL_PMSG( RollOff_pmsg );CK_DLL_SFUN( RollOff_compute );CK_DLL_MFUN( RollOff_ctrl_percent );CK_DLL_MFUN( RollOff_cget_percent );// offsetstatic t_CKUINT RollOff_offset_percent = 0; // Feature CollectorCK_DLL_TICK( FeatureCollector_tick );CK_DLL_TOCK( FeatureCollector_tock );CK_DLL_PMSG( FeatureCollector_pmsg ); // AutoCorrCK_DLL_CTOR( AutoCorr_ctor );CK_DLL_DTOR( AutoCorr_dtor );CK_DLL_TICK( AutoCorr_tick );CK_DLL_TOCK( AutoCorr_tock );CK_DLL_PMSG( AutoCorr_pmsg );CK_DLL_SFUN( AutoCorr_compute );CK_DLL_MFUN( AutoCorr_ctrl_normalize );CK_DLL_MFUN( AutoCorr_cget_normalize );// offsetstatic t_CKUINT AutoCorr_offset_data = 0; // XCorrCK_DLL_CTOR( XCorr_ctor );CK_DLL_DTOR( XCorr_dtor );CK_DLL_TICK( XCorr_tick );CK_DLL_TOCK( XCorr_tock );CK_DLL_PMSG( XCorr_pmsg );CK_DLL_SFUN( XCorr_compute );CK_DLL_MFUN( XCorr_ctrl_normalize );CK_DLL_MFUN( XCorr_cget_normalize );// offsetstatic t_CKUINT XCorr_offset_data = 0; // ZeroXCK_DLL_CTOR( ZeroX_ctor );CK_DLL_DTOR( ZeroX_dtor );CK_DLL_TICK( ZeroX_tick );CK_DLL_TOCK( ZeroX_tock );CK_DLL_PMSG( ZeroX_pmsg );CK_DLL_SFUN( ZeroX_compute );// offsetstatic t_CKUINT ZeroX_offset_data = 0; // LPCCK_DLL_CTOR( LPC_ctor );CK_DLL_DTOR( LPC_dtor );CK_DLL_TICK( LPC_tick );CK_DLL_TOCK( LPC_tock );CK_DLL_PMSG( LPC_pmsg );CK_DLL_SFUN( LPC_compute );CK_DLL_MFUN( LPC_ctrl_pitch );CK_DLL_MFUN( LPC_cget_pitch );CK_DLL_MFUN( LPC_ctrl_power );CK_DLL_MFUN( LPC_cget_power );CK_DLL_MFUN( LPC_ctrl_coefs );CK_DLL_MFUN( LPC_cget_coefs );// offset//static t_CKUINT LPC_offset_data = 0;  // utility functionsvoid xcorr_fft( SAMPLE * f, t_CKINT fs, SAMPLE * g, t_CKINT gs, SAMPLE * buffer, t_CKINT bs );void xcorr_normalize( SAMPLE * buffy, t_CKINT bs, SAMPLE * f, t_CKINT fs, SAMPLE * g, t_CKINT gs ); // 1.4.2.0 (ge) | local global sample rate variable (e.g., for MFCC)static t_CKUINT g_srate = 0;   //-----------------------------------------------------------------------------// name: extract_query()// desc: ...//-----------------------------------------------------------------------------DLL_QUERY extract_query( Chuck_DL_Query * QUERY ){    Chuck_Env * env = QUERY->env();    Chuck_DL_Func * func = NULL;     // 1.4.2.0 (ge) | store sample rate    g_srate = QUERY->srate;     std::string doc;     //---------------------------------------------------------------------    // init as base class: FeatureCollector    //---------------------------------------------------------------------     doc = "Turns UAna input into a single feature vector, upon .upchuck()";     if( !type_engine_import_uana_begin( env, "FeatureCollector", "UAna", env->global(),                                        NULL, NULL,                                        FeatureCollector_tick, FeatureCollector_tock, FeatureCollector_pmsg,                                        CK_NO_VALUE, CK_NO_VALUE, CK_NO_VALUE, CK_NO_VALUE,                                        doc.c_str() ) )        return FALSE;     // end the class import    type_engine_import_class_end( env );     //---------------------------------------------------------------------    // init as base class: Centroid    //---------------------------------------------------------------------     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.";     if( !type_engine_import_uana_begin( env, "Centroid", "UAna", env->global(),                                        NULL, NULL,                                        Centroid_tick, Centroid_tock, Centroid_pmsg,                                        CK_NO_VALUE, CK_NO_VALUE, CK_NO_VALUE, CK_NO_VALUE,                                        doc.c_str() ) )        return FALSE;     // compute    func = make_new_sfun( "float", "compute", Centroid_compute );    func->add_arg( "float[]", "input" );    func->doc = "Manually computes the centroid from a float array.";    if( !type_engine_import_sfun( env, func ) ) goto error;     // end the class import    type_engine_import_class_end( env );     //---------------------------------------------------------------------    // init as base class: Flux    //---------------------------------------------------------------------     doc =        "A unit analyzer that computes the spectral flux between successive magnitude spectra (via incoming UAna, or given manually), and outputs a single number.";     if( !type_engine_import_uana_begin( env, "Flux", "UAna", env->global(),                                        Flux_ctor, Flux_dtor,                                        Flux_tick, Flux_tock, Flux_pmsg,                                        CK_NO_VALUE, CK_NO_VALUE, CK_NO_VALUE, CK_NO_VALUE,                                        doc.c_str() ) )        return FALSE;     // data offset    Flux_offset_data = type_engine_import_mvar( env, "int", "@Flux_data", FALSE );    if( Flux_offset_data == CK_INVALID_OFFSET ) goto error;     // compute    func = make_new_mfun( "void", "reset", Flux_ctrl_reset );    func->doc = "Reset the extractor.";    if( !type_engine_import_mfun( env, func ) ) goto error;     // compute    func = make_new_sfun( "float", "compute", Flux_compute );    func->add_arg( "float[]", "lhs" );    func->add_arg( "float[]", "rhs" );    func->doc = "Manually computes the flux between two frames.";    if( !type_engine_import_sfun( env, func ) ) goto error;     // compute2    func = make_new_sfun( "float", "compute", Flux_compute2 );    func->add_arg( "float[]", "lhs" );    func->add_arg( "float[]", "rhs" );    func->add_arg( "float[]", "diff" );    func->doc = "Manually computes the flux between two frames, and stores the difference in a third array.";    if( !type_engine_import_sfun( env, func ) ) goto error;     // end the class import    type_engine_import_class_end( env );     //---------------------------------------------------------------------    // init as base class: RMS    //---------------------------------------------------------------------     doc =        "A unit analyzer that computes the RMS power mean from a magnitude spectrum (either from an incoming UAna, or given manually), and outputs a single number.";     if( !type_engine_import_uana_begin( env, "RMS", "UAna", env->global(),                                        NULL, NULL,                                        RMS_tick, RMS_tock, RMS_pmsg,                                        CK_NO_VALUE, CK_NO_VALUE, CK_NO_VALUE, CK_NO_VALUE,                                        doc.c_str() ) )        return FALSE;     // compute    func = make_new_sfun( "float", "compute", RMS_compute );    func->add_arg( "float[]", "input" );    func->doc = "Manually computes the RMS from a float array.";    if( !type_engine_import_sfun( env, func ) ) goto error;     // end the class import    type_engine_import_class_end( env );     //---------------------------------------------------------------------    // init as base class: MFCC    //---------------------------------------------------------------------     doc =        "A unit analyzer that computes Mel-frequency Cepstral Coefficients (MFCCs), and outputs a vector of coefficients.";     // 1.4.2.0 (yikai) | added    if( !type_engine_import_uana_begin( env, "MFCC", "UAna", env->global(),                                        MFCC_ctor, MFCC_dtor,                                        MFCC_tick, MFCC_tock, MFCC_pmsg,                                        CK_NO_VALUE, CK_NO_VALUE, CK_NO_VALUE, CK_NO_VALUE,                                        doc.c_str() ) )        return FALSE;     // data offset    MFCC_offset_data = type_engine_import_mvar( env, "float", "@MFCC_data", FALSE );    if( MFCC_offset_data == CK_INVALID_OFFSET ) goto error;     // sample rate    func = make_new_mfun( "int", "sampleRate", MFCC_ctrl_sample_rate );    func->add_arg( "int", "sr" );    func->doc = "Set the sample rate for MFCC analysis; NOTE: by default this is set to current ChucK sample rate.";    if( !type_engine_import_mfun( env, func ) ) goto error;     // sample rate    func = make_new_mfun( "int", "sampleRate", MFCC_cget_sample_rate );    func->doc = "Get the sample rate for MFCC analysis.";    if( !type_engine_import_mfun( env, func ) ) goto error;     // num filters    func = make_new_mfun( "int", "numFilters", MFCC_ctrl_num_filters );    func->doc = "Set the number of linearly spaced filters in MEL space.";    func->add_arg( "int", "n" );    if( !type_engine_import_mfun( env, func ) ) goto error;     // num filters    func = make_new_mfun( "int", "numFilters", MFCC_cget_num_filters );    func->doc = "Get the number of linearly spaced filters in MEL space.";    if( !type_engine_import_mfun( env, func ) ) goto error;     // num coeffs    func = make_new_mfun( "int", "numCoeffs", MFCC_ctrl_num_coeffs );    func->add_arg( "int", "n" );    func->doc = "Set the number of MFCC coefficients to compute.";    if( !type_engine_import_mfun( env, func ) ) goto error;     // num coeffs    func = make_new_mfun( "int", "numCoeffs", MFCC_cget_num_coeffs );    func->doc = "Get the number of MFCC coefficients to compute.";    if( !type_engine_import_mfun( env, func ) ) goto error;     // compute (manual alternative to calling upchuck())    func = make_new_mfun( "float", "compute", MFCC_compute );    func->add_arg( "float[]", "input" );    func->add_arg( "float[]", "output" );    func->doc =        "Manually computes the MFCC of the input (an FFT spectrum), and stores the results in the output array (MFCC coefficients).";    if( !type_engine_import_mfun( env, func ) ) goto error;     // end the class import    type_engine_import_class_end( env );     //---------------------------------------------------------------------    // init as base class: Kurtosis    //---------------------------------------------------------------------     doc =        "A unit analyzer that computes the kurtosis of a given input array, and outputs a single number.";     if( !type_engine_import_uana_begin( env, "Kurtosis", "UAna", env->global(),                                        NULL, NULL,                                        Kurtosis_tick, Kurtosis_tock, Kurtosis_pmsg,                                        CK_NO_VALUE, CK_NO_VALUE, CK_NO_VALUE, CK_NO_VALUE,                                        doc.c_str() ) )        return FALSE;     // compute    func = make_new_sfun( "float", "compute", Kurtosis_compute );    func->add_arg( "float[]", "input" );    func->doc = "Manually computes the kurtosis of a given input array.";    if( !type_engine_import_sfun( env, func ) ) goto error;     // end the class import    type_engine_import_class_end( env );     //---------------------------------------------------------------------    // init as base class: SFM    //---------------------------------------------------------------------     doc =        "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.";     if( !type_engine_import_uana_begin( env, "SFM", "UAna", env->global(),                                        SFM_ctor, SFM_dtor,                                        SFM_tick, SFM_tock, SFM_pmsg,                                        CK_NO_VALUE, CK_NO_VALUE, CK_NO_VALUE, CK_NO_VALUE,                                        doc.c_str() ) )        return FALSE;     // data offset    SFM_offset_data = type_engine_import_mvar( env, "float", "@SFM_data", FALSE );    if( SFM_offset_data == CK_INVALID_OFFSET ) goto error;     // nr_bands    func = make_new_mfun( "int", "nrBands", SFM_ctrl_nr_bands );    func->add_arg( "int", "nr_bands" );    func->doc = "Set the number of frequency bands to use for SFM analysis.";    if( !type_engine_import_mfun( env, func ) ) goto error;     // nr_bands    func = make_new_mfun( "int", "nrBands", SFM_cget_nr_bands );    func->doc = "Get the number of frequency bands to use for SFM analysis.";    if( !type_engine_import_mfun( env, func ) ) goto error;     // compute (manual alternative to calling upchuck())    func = make_new_mfun( "float", "compute", SFM_compute );    func->add_arg( "float[]", "input" );    func->add_arg( "float[]", "output" );    func->doc =        "Manually computes the SFM of the input (an FFT spectrum), and stores the results in the output array (SFM coefficients).";    if( !type_engine_import_sfun( env, func ) ) goto error;     // end the class import    type_engine_import_class_end( env );     //---------------------------------------------------------------------    // init as base class: Chroma    //---------------------------------------------------------------------     doc =        "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.";     if( !type_engine_import_uana_begin( env, "Chroma", "UAna", env->global(),                                        Chroma_ctor, Chroma_dtor,                                        Chroma_tick, Chroma_tock, Chroma_pmsg,                                        CK_NO_VALUE, CK_NO_VALUE, CK_NO_VALUE, CK_NO_VALUE,                                        doc.c_str() ) )        return FALSE;     // data offset    Chroma_offset_data = type_engine_import_mvar( env, "float", "@Chroma_data", FALSE );    if( Chroma_offset_data == CK_INVALID_OFFSET ) goto error;     // sample_rate    func = make_new_mfun( "float", "sampleRate", Chroma_ctrl_sample_rate );    func->add_arg( "float", "sample_rate" );    func->doc = "Set the sample rate for Chroma analysis.";    if( !type_engine_import_mfun( env, func ) ) goto error;     // sample_rate    func = make_new_mfun( "float", "sampleRate", Chroma_cget_sample_rate );    func->doc = "Get the sample rate for Chroma analysis.";    if( !type_engine_import_mfun( env, func ) ) goto error;     // low_oct_num    func = make_new_mfun( "int", "lowOctNum", Chroma_ctrl_low_oct_num );    func->add_arg( "int", "low_oct_num" );    func->doc = "Set the lowest octave number for Chroma analysis.";    if( !type_engine_import_mfun( env, func ) ) goto error;     // low_oct_num    func = make_new_mfun( "int", "lowOctNum", Chroma_cget_low_oct_num );    func->doc = "Get the lowest octave number for Chroma analysis.";    if( !type_engine_import_mfun( env, func ) ) goto error;     // high_oct_num    func = make_new_mfun( "int", "highOctNum", Chroma_ctrl_high_oct_num );    func->add_arg( "int", "high_oct_num" );    func->doc = "Set the highest octave number for Chroma analysis.";    if( !type_engine_import_mfun( env, func ) ) goto error;     // high_oct_num    func = make_new_mfun( "int", "highOctNum", Chroma_cget_high_oct_num );    func->doc = "Get the highest octave number for Chroma analysis.";    if( !type_engine_import_mfun( env, func ) ) goto error;     // compute (manual alternative to calling upchuck())    func = make_new_mfun( "float", "compute", Chroma_compute );    func->add_arg( "float[]", "input" );    func->add_arg( "float[]", "output" );    func->doc =        "Manually computes the Chroma of the input (an FFT spectrum), and stores the results in the output array (Chroma coefficients).";    if( !type_engine_import_sfun( env, func ) ) goto error; 

Lines 1–500 of 2781.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t