Home Catalog Evaluate Download

An Example C++ Program

Here is an example of what a program in the C++ language looks like. This particular program loads a .WAV file and then begins playing this song through the speakers. While the song plays the left-channel and right-channel audio samples are provided in real-time to the program. Via a loop that executes every .1 seconds, we walk through the latest batch of audio samples and determine the peak amplitude. Ten times a second we then display the detected peak volume on a stereo LED bar graph.

////////////////////////////////////////////////////////////////////////////// // WinMain() // ////////////////////////////////////////////////////////////////////////////// int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR pszCmdLine, int iCmdShow ) { // Ensure that the common control DLL is loaded. InitCommonControls(); // InitCommonControlsEx() for the newer controls // Create an instance of the cWinApp class. Every program // that employs the CSL framework requires this object // to provide infrastructure. cWinApp theApp( hInstance, pszCmdLine, APPNAME ); // Instantiate an instance of whatever class provides your // intended main window. The window will not be visible // until you call the class's Create() member function. cPeakDetectorDbg peak; peak.Create(); char szSongTitle[] = "Its Only Money.wav"; peak.LoadSong( szSongTitle ); peak.printf( "Loaded %s\n", szSongTitle ); peak.printf( "click Play pushbutton\n" ); int iLeftChannelSamples[MAXNUMWAVESAMPLES]; int iRightChannelSamples[MAXNUMWAVESAMPLES]; UINT nSamples; while ( theApp.PumpMessages() ) { nSamples = peak.GetSamples( iLeftChannelSamples, iRightChannelSamples, MAXNUMWAVESAMPLES ); if ( nSamples > 0 ) { UINT i; int iMaxLeftSample = 0; int iMaxRightSample = 0; for ( i=0; i < nSamples; i++ ) { if ( iLeftChannelSamples[i] > iMaxLeftSample ) iMaxLeftSample = iLeftChannelSamples[i]; if ( iRightChannelSamples[i] > iMaxRightSample ) iMaxRightSample = iRightChannelSamples[i]; } peak.SetLevel( iMaxLeftSample, iMaxRightSample ); // write to the 2 channel LED bar graph } } // Enter the message loop. return theApp.Run(); // cWinApp::Run() does not return until a WM_QUIT message is received. } // WinMain()

Return to the Catalog of Programs