// Runs update logic
void KissFileSampleApp::update() 
{

	// Check if track is playing and has a PCM buffer available
	if (mTrack->isPlaying() && mTrack->isPcmBuffering())
	{

		// Get buffer
		mBuffer = mTrack->getPcmBuffer();
		if (mBuffer && mBuffer->getInterleavedData())
		{

			// Get sample count
			uint32_t mSampleCount = mBuffer->getInterleavedData()->mSampleCount;
			if (mSampleCount > 0)
			{

				// Initialize analyzer, if needed
				if (!mFftInit)
				{
					mFftInit = true;
					mFft.setDataSize(mSampleCount);
				}

				// Analyze data
				if (mBuffer->getInterleavedData()->mData != 0) 
					mFft.setData(mBuffer->getInterleavedData()->mData);

			}

		}

	}

}
//*************************************************************************
void BeatDetectorApp::update() 
{
	if(write_frames)
	{
		if(curr_sample < p_sample->m_SampleCount)
		{
			// Initialize analyzer, if needed
			if (!mFftInit)
			{
				Init(samples_per_frame);
			}
			
			mFft.setData(p_sample->mp_Buffer + (curr_sample));
			curr_sample += samples_per_frame;

			CSoundAnalyzer::Get().ProcessData(mFft.getAmplitude(), mFft.getData());
		}
		else
		{
			shutdown();
		}
	}
	else
	{
		// Check if track is playing and has a PCM buffer available
		if (mTrack->isPlaying() && mTrack->isPcmBuffering())
		{
			// Get buffer
			mBuffer = mTrack->getPcmBuffer();
			if (mBuffer && mBuffer->getInterleavedData())
			{
				// Get sample count
				uint32_t mSampleCount = mBuffer->getChannelData(CHANNEL_FRONT_LEFT)->mSampleCount;
				
				if (mSampleCount > 0)
				{
					// Initialize analyzer, if needed
					if (!mFftInit)
					{
						Init(samples_per_frame);
					}
					
					// Analyze data
					if (mBuffer->getChannelData(CHANNEL_FRONT_LEFT)->mData != 0) 
						mFft.setData(mBuffer->getChannelData(CHANNEL_FRONT_LEFT)->mData);

					CSoundAnalyzer::Get().ProcessData(mFft.getAmplitude(), mFft.getData());
				}
			}
		}
	}
}
void RoboticaProjectApp::update()
{
    //get the latest pcm buffer from the track
	mPcmBuffer = mTrack->getPcmBuffer();
    
    if( mPcmBuffer ){
       // mPcmBuffer->getChannelData( audio::CHANNEL_FRONT_LEFT )
        fftRef = audio::calculateFft( mPcmBuffer->getChannelData( audio::CHANNEL_FRONT_LEFT ), bandCount );
        if(mAddParticle){
            particleC->addParticle(mAddPos);
            mAddParticle = false;
        }
        
        particleC->update(fftRef, bandCount, getElapsedSeconds());
    }    
        
    
    
}
void AudioAnalysisSampleApp::drawWaveForm( audio::TrackRef track )
{


    audio::PcmBuffer32fRef aPcmBuffer = track->getPcmBuffer();
    if( ! aPcmBuffer ) {
        return;
    }



    uint32_t bufferSamples = aPcmBuffer->getSampleCount();


    audio::Buffer32fRef leftBuffer = aPcmBuffer->getChannelData( audio::CHANNEL_FRONT_LEFT );
    audio::Buffer32fRef rightBuffer = aPcmBuffer->getChannelData( audio::CHANNEL_FRONT_RIGHT );

    int displaySize = getWindowWidth();
    int endIdx = bufferSamples;

    float scale = displaySize / (float)endIdx;

    glColor3f( 1.0f, 0.5f, 0.25f );
    glBegin( GL_LINE_STRIP );
    for( int i = 0; i < endIdx; i++ ) {
        float y = ( ( leftBuffer->mData[i] - 1 ) * - 100 );
        glVertex2f( ( i * scale ) , y );
    }
    glEnd();

    glColor3f( 1.0f, 0.96f, 0.0f );
    glBegin( GL_LINE_STRIP );
    for( int i = 0; i < endIdx; i++ ) {
        float y = ( ( rightBuffer->mData[i] - 1 ) * - 100 );
        glVertex2f( ( i * scale ) , y );
    }
    glEnd();
}
void AudioAnalysisSampleApp::drawFft( audio::TrackRef track )
{


    float ht = (float)getWindowHeight();

    float dutyCycle = 0.8;
    float barWidth = (float)getWindowWidth()/(float)kBandCount;

    audio::PcmBuffer32fRef aPcmBuffer = track->getPcmBuffer();
    if( ! aPcmBuffer ) {
        return;
    }
    boost::shared_ptr<float> fftRef = audio::calculateFft( aPcmBuffer->getChannelData( audio::CHANNEL_FRONT_LEFT ), kBandCount );
    if( ! fftRef ) {
        return;
    }

    float * fftBuffer = fftRef.get();

    welch->addFft(fftBuffer);

    float * fftDisplayBuffer = welch->getFft();

// float *fftDisplayBuffer = fftRef.get();

    for( int i = 0; i < ( kBandCount ); i++ ) {
        float barY = 10.0f*kFFTHistory*fftDisplayBuffer[i] / kBandCount * ht;
        glBegin( GL_QUADS );
        glColor3f( 255.0f, 255.0f, 0.0f );
        glVertex2f( i * barWidth, ht );
        glVertex2f( i * barWidth + barWidth*dutyCycle, ht );
        glColor3f( 0.0f, 255.0f, 0.0f );
        glVertex2f( i * barWidth + barWidth*dutyCycle, ht - barY );
        glVertex2f( i * barWidth, ht - barY );
        glEnd();
    }
}
void SoundScapingApp::update() {
    //get the latest pcm buffer from the track
	mPcmBuffer = mTrack->getPcmBuffer();
}