00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
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
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
00078 if (recording == false && recordaudio == true)
00079 { rec.clear(); recording = true; }
00080
00081
00082 if (recording == true && recordaudio == false)
00083 {
00084 if(subName != "empty" && stimName != "empty")
00085 {
00086 pthread_mutex_lock(&fileName);
00087
00088
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
00096 recnb ++; recording = false;
00097
00098 pthread_mutex_unlock(&fileName);
00099 }
00100 }
00101
00102
00103
00104
00105
00106
00107
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
00117 if (recording == true && recordaudio == true && data.nsamples() == 256U)
00118
00119 rec.push_back(data);
00120 }
00121
00122
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;
00132
00133
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
00143 nub::soft_ref<AudioGrabber> agb(new AudioGrabber(manager));
00144 manager.addSubComponent(agb);
00145
00146
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
00155 if (manager.parseCommandLine(argc, argv,
00156 " subjnum <img1.ppm> ... <imgN.ppm>", 2, -2)==false)
00157 return(1);
00158
00159
00160 nub::soft_ref<EyeTracker> et = etc->getET();
00161 d->setEyeTracker(et);
00162 d->setEventLog(el);
00163 et->setEventLog(el);
00164
00165
00166 manager.start();
00167
00168
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
00187 d->clearScreen();
00188 d->displayISCANcalib();
00189 d->waitForKey();
00190
00191
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
00200 pthread_t runner;
00201 pthread_create(&runner, NULL, &audiorecorder, (void *)(agb.get()));
00202 LINFO("after pthread created");
00203 char txt[100];
00204
00205
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
00216 d->displayText("You may take a break press space to continue when ready");
00217 d->waitForKey();
00218
00219
00220 d->clearScreen();
00221 d->displayISCANcalib();
00222 d->waitForKey();
00223
00224
00225
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
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
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
00270 d->waitForKey();
00271 d->waitNextRequestedVsync(false, true);
00272 d->pushEvent(std::string("===== Showing image: ") +
00273 manager.getExtraArg(index[i]) + " =====");
00274
00275
00276 et->track(true);
00277
00278
00279 d->displayFixationBlink();
00280
00281
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
00286
00287
00288 d->displaySurface(surf, -2);
00289 usleep(3000000);
00290
00291
00292 SDL_FreeSurface(surf);
00293
00294
00295 d->clearScreen();
00296
00297
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
00302
00303
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
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
00327 manager.stop();
00328 LINFO("made it to manager.stop");
00329
00330 return 0;
00331
00332 }
00333
00334
00335 extern "C" int main(const int argc, char** argv)
00336 {
00337
00338
00339
00340
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
00355
00356
00357