test-audioGrab.C

Go to the documentation of this file.
00001 /*!@file AppDevices/test-audioGrab.C Test audio digitizer */
00002 
00003 // //////////////////////////////////////////////////////////////////// //
00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the //
00005 // University of Southern California (USC) and the iLab at USC.         //
00006 // See http://iLab.usc.edu for information about this project.          //
00007 // //////////////////////////////////////////////////////////////////// //
00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected //
00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency //
00010 // in Visual Environments, and Applications'' by Christof Koch and      //
00011 // Laurent Itti, California Institute of Technology, 2001 (patent       //
00012 // pending; application number 09/912,225 filed July 23, 2001; see      //
00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status).     //
00014 // //////////////////////////////////////////////////////////////////// //
00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit.       //
00016 //                                                                      //
00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can   //
00018 // redistribute it and/or modify it under the terms of the GNU General  //
00019 // Public License as published by the Free Software Foundation; either  //
00020 // version 2 of the License, or (at your option) any later version.     //
00021 //                                                                      //
00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope  //
00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the   //
00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
00025 // PURPOSE.  See the GNU General Public License for more details.       //
00026 //                                                                      //
00027 // You should have received a copy of the GNU General Public License    //
00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write   //
00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,   //
00030 // Boston, MA 02111-1307 USA.                                           //
00031 // //////////////////////////////////////////////////////////////////// //
00032 //
00033 // Primary maintainer for this file: Laurent Itti <itti@usc.edu>
00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/AppDevices/test-audioGrab.C $
00035 // $Id: test-audioGrab.C 14682 2011-04-05 01:11:05Z farhan $
00036 //
00037 
00038 #include "Audio/AudioWavFile.H"
00039 #include "Component/ModelManager.H"
00040 #include "Devices/AudioGrabber.H"
00041 #include "Devices/AudioMixer.H"
00042 #include "GUI/XWindow.H"
00043 #include "Image/DrawOps.H"
00044 #include "Image/Image.H"
00045 #include "Util/log.H"
00046 #include "fstream"
00047 #include <signal.h>
00048 
00049 static bool goforever = true;  //!< Will turn false on interrupt signal
00050 #define SCALE 4
00051 
00052 //! Signal handler (e.g., for control-C)
00053 void terminate(int s) { LERROR("*** INTERRUPT ***"); goforever = false; }
00054 
00055 int main(int argc, const char **argv)
00056 {
00057   // setup signal handling:
00058   signal(SIGHUP, terminate); signal(SIGINT, terminate);
00059   signal(SIGQUIT, terminate); signal(SIGTERM, terminate);
00060 
00061   // Instantiate a ModelManager:
00062   ModelManager manager("Test Model for AudioGrabber Class");
00063 
00064   // Instantiate our various ModelComponents:
00065   nub::soft_ref<AudioMixer> mix(new AudioMixer(manager));
00066   manager.addSubComponent(mix);
00067 
00068   nub::soft_ref<AudioGrabber> agb(new AudioGrabber(manager));
00069   manager.addSubComponent(agb);
00070 
00071   // Parse command-line:
00072   if (manager.parseCommandLine(argc, argv, "", 0, 0) == false) return(1);
00073 
00074   std::ofstream outFile("bit8data.dat");
00075 
00076   // Let's get some of the option values, to configure our window:
00077   const uint nsamples = agb->getModelParamVal<uint>("AudioGrabberBufSamples");
00078   int nchan = agb -> getModelParamVal<uint>("AudioGrabberChans");
00079   
00080   XWindow win(Dims(nsamples, 256 / SCALE * nchan),
00081               -1, -1, "test-audioGrab window");
00082   Image<byte> ima(nsamples, 256 / SCALE * nchan, NO_INIT);
00083 
00084   if (agb->getModelParamVal<uint>("AudioGrabberBits") != 8U)
00085     LFATAL("Sorry, this app only supports 8-bit audio grabbing.");
00086 
00087   // let's get all our ModelComponent instances started:
00088   manager.start();
00089 
00090   std::vector<AudioBuffer<byte> > rec;
00091   int totalSamp =0;
00092 
00093   // main loop:
00094   while(goforever) {
00095 
00096     // grab a buffer:
00097     AudioBuffer<byte> buffer;
00098     agb->grab(buffer);
00099     rec.push_back(buffer);
00100 
00101     // clear image:
00102     ima.clear();
00103 
00104     // draw the buffer into the image:
00105     Point2D<int> p1, p2;
00106     for (uint i = 1; i < nsamples; i ++)
00107       {
00108         totalSamp++;
00109         p1.i = i - 1;
00110         p1.j = (255 - buffer.getVal(i - 1, 0)) / SCALE;
00111         p2.i = i;
00112         p2.j = (255 - buffer.getVal(i, 0)) / SCALE;
00113         outFile << p1.j << std::endl;
00114 
00115         drawLine(ima, p1, p2, byte(255));
00116 
00117         if (nchan > 1)
00118           {
00119             p1.i = i - 1;
00120             p1.j = (511 - buffer.getVal(i - 1, 1)) / SCALE;
00121             p2.i = i;
00122             p2.j = (511 - buffer.getVal(i, 1)) / SCALE;
00123             drawLine(ima, p1, p2, byte(255));
00124           }
00125       }
00126 
00127     // display the image:
00128     win.drawImage(ima);
00129   }
00130 
00131   LINFO("writing out audio file: testAudio.wav");
00132   LINFO("total samps recorded is %d", totalSamp);
00133   // save our current records
00134   writeAudioWavFile("testAudio.wav", rec);
00135 
00136   // stop all our ModelComponents
00137   manager.stop();
00138 
00139   // all done!
00140   return 0;
00141 }
00142 
00143 // ######################################################################
00144 /* So things look consistent in everyone's emacs... */
00145 /* Local Variables: */
00146 /* indent-tabs-mode: nil */
00147 /* End: */
Generated on Sun May 8 08:40:07 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3