psycho-still-audio.C

Go to the documentation of this file.
00001 /*!@file AppPsycho/psycho-still-audio.C Psychophysics display of still images */
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: Bella Rozenkrants <rozenkra@usc.edu>
00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/AppPsycho/psycho-still-audio.C $
00035 // $Id: psycho-still-audio.C 13712 2010-07-28 21:00:40Z itti $
00036 //
00037 
00038 #include "Component/ModelManager.H"
00039 #include "Audio/AudioWavFile.H"
00040 #include "Devices/AudioGrabber.H"
00041 #include "Devices/AudioMixer.H"
00042 #include "Devices/DeviceOpts.H"
00043 #include "Image/Image.H"
00044 #include "Psycho/PsychoDisplay.H"
00045 #include "Psycho/EyeTrackerConfigurator.H"
00046 #include "Psycho/EyeTracker.H"
00047 #include "Psycho/PsychoOpts.H"
00048 #include "Component/EventLog.H"
00049 #include "Component/ComponentOpts.H"
00050 #include "Raster/Raster.H"
00051 #include "Util/MathFunctions.H"
00052 #include "Util/Types.H"
00053 #include <vector>
00054 #include <pthread.h>
00055 #include <string>
00056 #include <math.h>
00057 using std::string;
00058 
00059 volatile bool recordaudio = false;
00060 volatile bool keepgoing = true;
00061 volatile int recnb = 0;
00062 std::string subName = "empty";
00063 std::string stimName = "empty";
00064 int repNum = 0;
00065 // thread mutex to control file naming
00066 static pthread_mutex_t fileName= PTHREAD_MUTEX_INITIALIZER;
00067 // ######################################################################
00068 
00069 static void *audiorecorder(void *agbv)
00070 {
00071   bool recording = false;
00072   std::vector<AudioBuffer<byte> > rec;
00073 
00074 
00075   while(keepgoing)
00076     {
00077       // start recording?
00078       if (recording == false && recordaudio == true)
00079         { rec.clear(); recording = true; }
00080 
00081       // stop recording?
00082       if (recording == true && recordaudio == false)
00083         {
00084           if(subName != "empty" && stimName != "empty")
00085             {
00086               pthread_mutex_lock(&fileName);
00087 
00088               // save our current records
00089               char fname[100];
00090 
00091               sprintf(fname, "/lab/ilab19/bella/audiofiles/r%d_sub%s_%s_rn%02d.wav",repNum,subName.c_str(), stimName.c_str(),recnb);
00092 
00093               writeAudioWavFile(fname, rec);
00094 
00095               // ready for next recording:
00096               recnb ++; recording = false;
00097 
00098                           pthread_mutex_unlock(&fileName);
00099             }
00100         }
00101 
00102       // we grab the audio data all the time, so that it does not
00103       // start piling up into the internal buffers of the audio
00104       // grabber; but we will save it only if we are in recording
00105       // mode. Note: the call to grab() is blocking and hence sets
00106       // the pace of our loop here. With 256 samples at 11.025kHz
00107       // that's about 23ms:
00108       AudioGrabber *agb = (AudioGrabber *)agbv;
00109       AudioBuffer<byte> data;
00110 
00111       agb->grab(data);
00112 
00113       if (data.nsamples() != 256U)
00114         LERROR("Recorded len %u is not 256!", data.nsamples());
00115 
00116       // continue recording?
00117       if (recording == true && recordaudio == true && data.nsamples() == 256U)
00118         // store in our queue of records:
00119         rec.push_back(data);
00120     }
00121 
00122   // terminate thread:
00123   pthread_exit(0);
00124   return NULL;
00125 }
00126 
00127 // ######################################################################
00128 extern int submain(const int argc, char** argv)
00129 {
00130   LINFO("starting program");
00131   MYLOGVERB = LOG_FULLTRACE;  // suppress debug messages
00132 
00133   // Instantiate a ModelManager:
00134   ModelManager manager("Psycho Still Audio");
00135 
00136   nub::soft_ref<EyeTrackerConfigurator> etc(new EyeTrackerConfigurator(manager));
00137   manager.addSubComponent(etc);
00138 
00139   nub::soft_ref<EventLog> el(new EventLog(manager));
00140   manager.addSubComponent(el);
00141 
00142   //added from psycho-movie2
00143   nub::soft_ref<AudioGrabber> agb(new AudioGrabber(manager));
00144   manager.addSubComponent(agb);
00145 
00146   // Instantiate our various ModelComponents:
00147   nub::soft_ref<PsychoDisplay> d(new PsychoDisplay(manager));
00148   manager.addSubComponent(d);
00149 
00150   manager.setOptionValString(&OPT_EyeTrackerType, "ISCAN");
00151 
00152   LINFO("audio grabber initialized");
00153 
00154   // Parse command-line:
00155   if (manager.parseCommandLine(argc, argv,
00156                                " subjnum <img1.ppm> ... <imgN.ppm>", 2, -2)==false)
00157     return(1);
00158 
00159   // hook our various babies up and do post-command-line configs:
00160   nub::soft_ref<EyeTracker> et = etc->getET();
00161   d->setEyeTracker(et);
00162   d->setEventLog(el);
00163   et->setEventLog(el);
00164 
00165   // let's get all our ModelComponent instances started:
00166   manager.start();
00167 
00168   // setup array of image indices:
00169 
00170   int nbimgs = manager.numExtraArgs();
00171   nbimgs = nbimgs - 1;
00172   int index[nbimgs];
00173 
00174   for (int i = 0; i < nbimgs; i ++)
00175     {
00176       index[i] = i+1;
00177       LINFO("%d=%s", i,manager.getExtraArg(i).c_str());
00178     }
00179 
00180    LINFO("Randomizing %d images...",nbimgs); randShuffle(index, nbimgs);
00181 
00182   for (int i=0;i< nbimgs;i++)
00183     LINFO("after randShuffle index[%d]=%d",i,index[i]);
00184 
00185 
00186   // let's display an ISCAN calibration grid:
00187   d->clearScreen();
00188   d->displayISCANcalib();
00189   d->waitForKey();
00190 
00191   // let's do an eye tracker calibration:
00192   d->displayText("<SPACE> to calibrate; other key to skip");
00193   int c = d->waitForKey();
00194   if (c == ' ') d->displayEyeTrackerCalibration(3, 3);
00195   d->displayText("<SPACE> to start experiment ");
00196   d->waitForKey();
00197 
00198 
00199  // get the audio going: (from psycho-movie2)
00200   pthread_t runner;
00201   pthread_create(&runner, NULL, &audiorecorder, (void *)(agb.get()));
00202   LINFO("after pthread created");
00203   char txt[100];
00204 
00205   // main loop:
00206 
00207     for (int rept = 1; rept < 3; rept ++)
00208     {
00209         if (rept == 2)
00210         {
00211 
00212           LINFO("Randomizing %d images...",nbimgs);
00213           randShuffle(index, nbimgs);
00214 
00215           //allow for a break after 100 trials and then recalibrate.
00216           d->displayText("You may take a break press space to continue when ready");
00217           d->waitForKey();
00218 
00219           // let's display an ISCAN calibration grid:
00220           d->clearScreen();
00221           d->displayISCANcalib();
00222           d->waitForKey();
00223 
00224 
00225           // let's do an eye tracker calibration:
00226           d ->displayText("<SPACE> to calibrate");
00227           int c = d->waitForKey();
00228           if (c == ' ') d->displayEyeTrackerCalibration(3, 3);
00229           d ->displayText("<SPACE> to continue with experiment");
00230           d->waitForKey();
00231            }
00232 
00233         for (int i = 0; i < nbimgs; i ++)
00234           {
00235             // recalibration every 20 trials
00236             if (i == 20 || i == 40 || i == 60 || i == 80)
00237               {
00238                 d->displayText("Ready for quick recalibration?(press space)");
00239                 int c = d->waitForKey();
00240                 if (c == ' ') d->displayEyeTrackerCalibration(3, 3);
00241                 d->clearScreen();
00242                 d ->displayText("<SPACE> to continue with experiment");
00243                 d->waitForKey();
00244               }
00245 
00246 
00247           // load up the frame and show a fixation cross on a blank screen:
00248 
00249           d->clearScreen();
00250           LINFO("Loading '%s'...", manager.getExtraArg(index[i]).c_str());
00251           Image< PixRGB<byte> > image =
00252             Raster::ReadRGB(manager.getExtraArg(index[i]));
00253 
00254           SDL_Surface *surf = d->makeBlittableSurface(image, true);
00255 
00256           LINFO("'%s' ready.", manager.getExtraArg(index[i]).c_str());
00257           d->displayFixation();
00258 
00259            pthread_mutex_lock(&fileName);
00260           repNum = rept;
00261           subName = std::string(manager.getExtraArg(0));
00262           stimName = std::string(manager.getExtraArg(index[i]));
00263           int len = stimName.length();
00264           len = len-26;
00265           stimName=stimName.substr(26,len);
00266           LINFO("stim name given = %s",stimName.c_str());
00267           pthread_mutex_unlock(&fileName);
00268 
00269           // ready to go whenever the user is ready:
00270           d->waitForKey();
00271           d->waitNextRequestedVsync(false, true);
00272           d->pushEvent(std::string("===== Showing image: ") +
00273                        manager.getExtraArg(index[i]) + " =====");
00274 
00275           // start the eye tracker:
00276           et->track(true);
00277 
00278           // blink the fixation:
00279           d->displayFixationBlink();
00280 
00281           // start audio recording, unless we were flagged to record later:
00282           sprintf(txt, "Start audio recording:  rep%d_%s_%s_%d_audio.wav",repNum, manager.getExtraArg(0).c_str(), manager.getExtraArg(index[i]).c_str(), recnb);
00283 
00284           d->pushEvent(txt);
00285           //recordaudio = true;
00286 
00287           // show the image:
00288           d->displaySurface(surf, -2);
00289           usleep(3000000);
00290 
00291           // free the image:
00292           SDL_FreeSurface(surf);
00293 
00294           // make sure display is off before we stop the tracker:
00295           d->clearScreen();
00296 
00297           // stop audio recording:
00298           sprintf(txt, "Stop audio recording: rep%d_%s_%s_%d_audio.wav",repNum, manager.getExtraArg(0).c_str(), manager.getExtraArg(index[i]).c_str(), recnb);
00299 
00300           d->pushEvent(txt);
00301           //recordaudio = false;
00302 
00303           // stop the eye tracker:
00304           usleep(30000);
00305           et->track(false);
00306           LINFO("i=%d index[0]=%d %s",i,index[0],manager.getExtraArg(index[0]).c_str());
00307           }
00308 
00309          }
00310   // kill the audio recording thread:
00311   keepgoing = false;
00312   LINFO("made it to keepgoing=false %d",(int)keepgoing);
00313   sleep(1);
00314   LINFO("made it to sleep");
00315   if (system("sync")) LERROR("Error sync()'ing");;
00316   LINFO("made it to sync");
00317 
00318   d->clearScreen();
00319   d->displayText("Experiment complete. Thank you!");
00320   LINFO("made it to display text");
00321   d->waitForKey();
00322   LINFO("made it to wait for key");
00323 
00324 
00325 
00326   // stop all our ModelComponents
00327   manager.stop();
00328   LINFO("made it to manager.stop");
00329   // all done!
00330   return 0;
00331 
00332 }
00333 
00334 // ######################################################################
00335 extern "C" int main(const int argc, char** argv)
00336 {
00337   // simple wrapper around submain() to catch exceptions (because we
00338   // want to allow PsychoDisplay to shut down cleanly; otherwise if we
00339   // abort while SDL is in fullscreen mode, the X server won't return
00340   // to its original resolution)
00341   try
00342     {
00343       return submain(argc, argv);
00344     }
00345   catch (...)
00346     {
00347       REPORT_CURRENT_EXCEPTION;
00348     }
00349 
00350   return 1;
00351 }
00352 // ######################################################################
00353 
00354 /* So things look consistent in everyone's emacs... */
00355 /* Local Variables: */
00356 /* indent-tabs-mode: nil */
00357 /* End: */
Generated on Sun May 8 08:04:14 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3