int main() { int i, x, y; double ThisTime; double LastFPSTime = OGGetAbsoluteTime(); double LastFrameTime = OGGetAbsoluteTime(); double SecToWait; int linesegs = 0; CNFGBGColor = 0x800000; CNFGDialogColor = 0x444444; CNFGSetup( "Test Bench", 640, 480 ); for( x = 0; x < HMX; x++ ) for( y = 0; y < HMY; y++ ) { Heightmap[x+y*HMX] = tdPerlin2D( x, y )*8.; } while(1) { int i, pos; float f; iframeno++; RDPoint pto[3]; CNFGHandleInput(); CNFGClearFrame(); CNFGColor( 0xFFFFFF ); CNFGGetDimensions( &screenx, &screeny ); DrawFrame(); /* pto[0].x = 100; pto[0].y = 100; pto[1].x = 200; pto[1].y = 100; pto[2].x = 100; pto[2].y = 200; CNFGTackPoly( &pto[0], 3 ); CNFGColor( 0xFF00FF ); */ /* CNFGTackSegment( pto[0].x, pto[0].y, pto[1].x, pto[1].y ); CNFGTackSegment( pto[1].x, pto[1].y, pto[2].x, pto[2].y ); CNFGTackSegment( pto[2].x, pto[2].y, pto[0].x, pto[0].y ); */ /* CNFGDrawBox( 0, 0, 260, 260 ); CNFGPenX = 10; CNFGPenY = 10; pos = 0; for( i = 0; i < 1; i++ ) { int c; char tw[2] = { 0, 0 }; for( c = 0; c < 256; c++ ) { tw[0] = c; CNFGPenX = ( c % 16 ) * 16+5; CNFGPenY = ( c / 16 ) * 16+5; CNFGDrawText( tw, 2 ); } } CNFGPenX = 0; CNFGPenY = 0; for( i = 0; i < 400; i++ ) { RDPoint pp[3]; CNFGColor( 0x00FF00 ); pp[0].x = (short)(50*sin((float)(i+iframeno)*.01) + (i%20)*30); pp[0].y = (short)(50*cos((float)(i+iframeno)*.01) + (i/20)*20); pp[1].x = (short)(20*sin((float)(i+iframeno)*.01) + (i%20)*30); pp[1].y = (short)(50*cos((float)(i+iframeno)*.01) + (i/20)*20); pp[2].x = (short)(10*sin((float)(i+iframeno)*.01) + (i%20)*30); pp[2].y = (short)(30*cos((float)(i+iframeno)*.01) + (i/20)*20); CNFGTackPoly( pp, 3 ); }*/ frames++; CNFGSwapBuffers(); ThisTime = OGGetAbsoluteTime(); if( ThisTime > LastFPSTime + 1 ) { printf( "FPS: %d\n", frames ); frames = 0; linesegs = 0; LastFPSTime+=1; } SecToWait = .016 - ( ThisTime - LastFrameTime ); LastFrameTime += .016; if( SecToWait > 0 ) OGUSleep( (int)( SecToWait * 1000000 ) ); } return(0); }
int main(int argc, char ** argv) { int i; #ifdef WIN32 WSADATA wsaData; WSAStartup(0x202, &wsaData); strcpy( sound_source, "WIN" ); #else strcpy( sound_source, "PULSE" ); #endif gargc = argc; gargv = argv; InitialFile[0] = "default.conf"; ProcessArgs(); //Initialize Rawdraw int frames = 0; double ThisTime; double LastFPSTime = OGGetAbsoluteTime(); double LastFrameTime = OGGetAbsoluteTime(); double SecToWait; CNFGBGColor = 0x800000; CNFGDialogColor = 0x444444; char title[1024]; char * tp = title; memcpy( tp, "ColorChord ", strlen( "ColorChord " ) ); tp += strlen( "ColorChord " ); for( i = 1; i < argc; i++ ) { memcpy( tp, argv[i], strlen( argv[i] ) ); tp += strlen( argv[i] ); *tp = ' '; tp++; } *tp = 0; if( !headless ) CNFGSetup( title, set_screenx, set_screeny ); char * OutDriverNames = strdup( GetParameterS( "outdrivers", "null" ) ); char * ThisDriver = OutDriverNames; char * TDStart; for( i = 0; i < MAX_OUT_DRIVERS; i++ ) { while( *ThisDriver == ' ' || *ThisDriver == '\t' ) ThisDriver++; if( !*ThisDriver ) break; TDStart = ThisDriver; while( *ThisDriver != 0 && *ThisDriver != ',' ) { if( *ThisDriver == '\t' || *ThisDriver == ' ' ) *ThisDriver = 0; ThisDriver++; } if( *ThisDriver ) { *ThisDriver = 0; ThisDriver++; } printf( "Loading: %s\n", TDStart ); outdriver[i] = SetupOutDriver( TDStart ); } free(OutDriverNames); //Initialize Sound sd = InitSound( sound_source, &SoundCB ); if( !sd ) { fprintf( stderr, "ERROR: Failed to initialize sound output device\n" ); return -1; } nf = CreateNoteFinder( sd->spsRec ); while(1) { char stt[1024]; //Handle Rawdraw frame swappign if( !headless ) { CNFGHandleInput(); CNFGClearFrame(); CNFGColor( 0xFFFFFF ); CNFGGetDimensions( &screenx, &screeny ); } RunNoteFinder( nf, sound, (soundhead-1+SOUNDCBSIZE)%SOUNDCBSIZE, SOUNDCBSIZE ); //Done all ColorChord work. VisTimeStart = OGGetAbsoluteTime(); for( i = 0; i < MAX_OUT_DRIVERS; i++ ) { if( force_white ) { memset( OutLEDs, 0x7f, MAX_LEDS*3 ); } if( outdriver[i] ) outdriver[i]->Func( outdriver[i]->id, nf ); } VisTimeEnd = OGGetAbsoluteTime(); if( !headless ) { //Handle outputs. int freqbins = nf->freqbins; int note_peaks = freqbins/2; int freqs = freqbins * nf->octaves; //int maxdists = freqbins/2; //Do a bunch of debugging. if( show_debug_basic ) { for( i = 0; i < nf->dists; i++ ) { CNFGPenX = (nf->dist_means[i] + 0.5) / freqbins * screenx; //Move over 0.5 for visual purposes. The means is correct. CNFGPenY = 400-nf->dist_amps[i] * 150.0 / nf->dist_sigmas[i]; //printf( "%f %f\n", dist_means[i], dist_amps[i] ); sprintf( stt, "%f\n%f\n", nf->dist_means[i], nf->dist_amps[i] ); CNFGDrawText( stt, 2 ); } CNFGColor( 0xffffff ); //Draw the folded bins for( i = 0; i < freqbins; i++ ) { float x0 = i / (float)freqbins * (float)screenx; float x1 = (i+1) / (float)freqbins * (float)screenx; float amp = nf->folded_bins[i] * 250.0; CNFGDialogColor = CCtoHEX( ((float)(i+0.5) / freqbins), 1.0, 1.0 ); CNFGDrawBox( x0, 400-amp, x1, 400 ); } CNFGDialogColor = 0xf0f000; for( i = 0; i < note_peaks; i++ ) { //printf( "%f %f /", note_positions[i], note_amplitudes[i] ); if( nf->note_amplitudes_out[i] < 0 ) continue; CNFGDialogColor = CCtoHEX( (nf->note_positions[i] / freqbins), 1.0, 1.0 ); CNFGDrawBox( ((float)i / note_peaks) * screenx, 480 - nf->note_amplitudes_out[i] * 100, ((float)(i+1) / note_peaks) * screenx, 480 ); CNFGPenX = ((float)(i+.4) / note_peaks) * screenx; CNFGPenY = screeny - 30; sprintf( stt, "%d\n%0.0f", nf->enduring_note_id[i], nf->note_amplitudes2[i]*1000.0 ); CNFGDrawText( stt, 2 ); } //Let's draw the o-scope. int thissoundhead = soundhead; thissoundhead = (thissoundhead-1+SOUNDCBSIZE)%SOUNDCBSIZE; int lasty = sound[thissoundhead] * 128 + 128; thissoundhead = (thissoundhead-1+SOUNDCBSIZE)%SOUNDCBSIZE; int thisy = sound[thissoundhead] * 128 + 128; thissoundhead = (thissoundhead-1+SOUNDCBSIZE)%SOUNDCBSIZE; for( i = 0; i < screenx; i++ ) { if( thisy < 0 || thisy > 256 ) printf( "%d/%d\n", thisy,thissoundhead ); CNFGTackSegment( i, lasty, i+1, thisy ); lasty = thisy; thisy = sound[thissoundhead] * 128 + 128; thissoundhead = (thissoundhead-1+SOUNDCBSIZE)%SOUNDCBSIZE; } } //Extra debugging? if( show_debug ) { //Draw the histogram float lasthistval; CNFGColor( 0xffffff ); for( i = -1; i < screenx; i++ ) { float thishistval = CalcHistAt( (float)i/(float)screenx*freqbins-0.5, nf->freqbins, nf->dist_means, nf->dist_amps, nf->dist_sigmas, nf->dists ); if( i >= 0 ) CNFGTackSegment( i, 400-lasthistval * 250.0, i+1, 400-thishistval * 250.0 ); lasthistval = thishistval; } CNFGColor( 0xffffff ); //Draw the bins for( i = 0; i < freqs; i++ ) { float x0 = i / (float)freqs * (float)screenx; float x1 = (i+1) / (float)freqs * (float)screenx; float amp = nf->outbins[i] * 250.0; CNFGDialogColor = CCtoHEX( ((float)i / freqbins), 1.0, 1.0 ); CNFGDrawBox( x0, 0, x1, amp ); } CNFGDialogColor = 0x0f0f0f; char stdebug[1024]; sprintf( stdebug, "DFT:%8.2fms\nFLT:%8.2f\nDEC:%8.2f\nFNL:%8.2f\nDPY:%8.2f", (nf->DFTTime - nf->StartTime)*1000, (nf->FilterTime - nf->DFTTime)*1000, (nf->DecomposeTime - nf->FilterTime)*1000, (nf->FinalizeTime - nf->DecomposeTime)*1000, (VisTimeEnd - VisTimeStart)*1000 ); CNFGPenX = 50; CNFGPenY = 50; CNFGDrawText( stdebug, 2 ); } CNFGColor( show_debug?0xffffff:0x000000 ); CNFGPenX = 0; CNFGPenY = screeny-10; CNFGDrawText( "Extra Debug (D)", 2 ); CNFGColor( show_debug_basic?0xffffff:0x000000 ); CNFGPenX = 120; CNFGPenY = screeny-10; CNFGDrawText( "Basic Debug (E)", 2 ); CNFGColor( show_debug_basic?0xffffff:0x000000 ); CNFGPenX = 240; CNFGPenY = screeny-10; sprintf( stt, "[9] Key: %d [0] (%3.1f) [-]", gKey, nf->base_hz ); CNFGDrawText( stt, 2 ); CNFGColor( 0xffffff ); CNFGPenX = 440; CNFGPenY = screeny-10; sprintf( stt, "FPS: %d", lastfps ); CNFGDrawText( stt, 2 ); CNFGSwapBuffers(); } //Finish Rawdraw with FPS counter, and a nice delay loop. frames++; ThisTime = OGGetAbsoluteTime(); if( ThisTime > LastFPSTime + 1 ) { // printf( "FPS: %d\n", frames ); lastfps = frames; frames = 0; LastFPSTime+=1; } if( cpu_autolimit ) { SecToWait = .016 - ( ThisTime - LastFrameTime ); LastFrameTime += .016; if( SecToWait < -.1 ) LastFrameTime = ThisTime - .1; if( SecToWait > 0 ) OGUSleep( (int)( SecToWait * 1000000 ) ); } SetEnvValues(); } }