/*
 *  analysis_example.c
 *
 *  Use the Loris procedural interface to perform a
 *  Reassigned Bandwidth-Enhanced analysis of a 
 *  clarinet tone, having fundamental frequency of
 *  approximately 415 Hz.
 */
#include <stdio.h>
#include <string.h>
#include <loris.h>

int main( void )
{
    const double FUNDAMENTAL = 415.0; 
                   /* G#4 */
    const unsigned long BUFSZ = 44100 * 4; 
                   /* approx. 4 seconds */

    PartialList * partials = NULL;
    double samples[ BUFSZ ];
    double sr;
    unsigned int nsamps;
    
    /* import the clarinet samples */
    nsamps = importAiff("clarinet.aiff", samples, BUFSZ, &sr);
    
    /* configure the Loris analyzer, use frequency
       resolution equal to 80% of the fundamental
       frequency, main lobe width equal to the 
       fundamental frequency, and frequency drift 
       equal to 20% of the fundamental frequency
    */
    analyzer_configure( .8 * FUNDAMENTAL, FUNDAMENTAL );
    analyzer_setFreqDrift( .2 * FUNDAMENTAL );  
                   /* approx. 83 Hz */
    
    /* analyze and store partials */
    partials = createPartialList();
    analyze( samples, nsamps, sr, partials );
  
    /* export to SDIF file */
    exportSdif("clarinet.sdif", partials );
    
    /* synthesize */
    memset( samples, 0, BUFSZ * sizeof(double) );
    nsamps = synthesize( partials, samples, BUFSZ, sr );
    
    /* export samples to AIFF file */
    exportAiff( "synth.aiff", samples, nsamps, sr, 16 );
    
    /* cleanup */
    destroyPartialList( partials );
    
    return 0;
} 
