urbisendsound.cc

00001 #include "urbi/uclient.hh"
00002 #include <sys/types.h>
00003 #include "libport/sys/stat.h"
00004 
00005 urbi::USound snd;
00006 
00007 urbi::UCallbackAction
00008 endProgram(const urbi::UMessage&)
00009 {
00010   printf("done\n");
00011   urbi::exit(0);
00012   return urbi::URBI_REMOVE;
00013 }
00014 
00015 urbi::UCallbackAction
00016 soundFormat(const urbi::UMessage &msg)
00017 {
00018   urbi::UMessage smsg(msg.client, 0, "",
00019                       msg.message.c_str(), std::list<urbi::BinaryData>());
00020   snd = smsg.value->binary->sound;
00021   //sem_post(&sem);
00022   return urbi::URBI_REMOVE;
00023 }
00024 
00025 int
00026 main(int argc, char * argv [])
00027 {
00028   if (argc<3)
00029     {
00030       printf("usage: urbisendsound robot file\n\t file must be in the WAV format\n");
00031       exit(1);
00032     }
00033 
00034   urbi::UClient uc(argv[1]);
00035   if (uc.error())
00036     exit(2);
00037 
00038   FILE *f;
00039   if (STREQ(argv[2],"-"))
00040     f = stdin;
00041   else
00042     f = fopen(argv[2],"r");
00043   if (!f)
00044     {
00045       printf("error opening file\n");
00046       exit(3);
00047     }
00048 
00049   //sem_init(&sem, false, 0);
00050   //uc->sendCommand(&soundFormat, "speaker.formatlist;");
00051   soundFormat(urbi::UMessage(uc,0,"a",
00052                              "*** BIN 0 raw 2 16000 16 1",
00053                              std::list<urbi::BinaryData>()));  //forcing sound format
00054   //sem_wait(&sem);
00055 
00056   urbi::USound s;
00057 
00058   if (f!=stdin)
00059     {
00060       struct stat st;
00061       stat(argv[2],&st);
00062       s.data = static_cast<char *> (malloc (st.st_size));
00063       s.soundFormat = urbi::SOUND_WAV;
00064       s.size = st.st_size;
00065       fread(s.data, 1,st.st_size, f);
00066       snd.data = 0;
00067       convert(s, snd);
00068 
00069       uc.setCallback(endProgram,"end");
00070       printf("sending %d bytes\n", static_cast<int>(st.st_size));
00071       uc.sendSound("speaker", snd,"end");
00072       printf("done, waiting for end of play notification\n");
00073     }
00074   else
00075     {
00076       s.data = static_cast<char *> (malloc (130000));
00077       s.soundFormat = urbi::SOUND_WAV;
00078       fread(s.data, 44, 1, f);
00079       int sz=1;
00080 
00081       while (sz)
00082       {
00083         sz=fread(s.data+44,1,128000,f);
00084         s.size = sz+44;
00085         convert(s, snd);
00086         uc.sendSound("speaker", snd,sz<128000?"end":"void");
00087         printf("sending %d bytes\n",sz);
00088       }
00089     }
00090   urbi::execute();
00091 }

Generated on Tue Apr 10 17:45:45 2007 for URBISDK by  doxygen 1.5.1