SoundAgent.cc

Go to the documentation of this file.
00001 //
00002 // Copyright 2002,2004 Sony Corporation 
00003 //
00004 // Permission to use, copy, modify, and redistribute this software for
00005 // non-commercial use is hereby granted.
00006 //
00007 // This software is provided "as is" without warranty of any kind,
00008 // either expressed or implied, including but not limited to the
00009 // implied warranties of fitness for a particular purpose.
00010 //
00011 
00012 #include <OPENR/OPENRAPI.h>
00013 #include <OPENR/OSyslog.h>
00014 #include <OPENR/core_macro.h>
00015 #include "SoundAgent.h"
00016 
00017 SoundAgent::SoundAgent() : soundAgentState(SAS_IDLE),
00018                            speakerID(oprimitiveID_UNDEF),
00019                            soundDataID(odesigndataID_UNDEF),
00020                            soundODA(), playingIndex(-1), playingWAV()
00021 {
00022     for (int i = 0; i < SOUND_NUM_BUFFER; i++) region[i] = 0;
00023 }
00024 
00025 OStatus
00026 SoundAgent::DoInit(const OSystemEvent& event)
00027 {
00028     NEW_ALL_SUBJECT_AND_OBSERVER;
00029     REGISTER_ALL_ENTRY;
00030     SET_ALL_READY_AND_NOTIFY_ENTRY;
00031 
00032     OpenSpeaker();
00033     NewSoundVectorData();
00034     LoadODA();
00035     SetPowerAndVolume();
00036 
00037     return oSUCCESS;
00038 }
00039 
00040 OStatus
00041 SoundAgent::DoStart(const OSystemEvent& event)
00042 {
00043     soundAgentState = SAS_START;
00044 
00045     ENABLE_ALL_SUBJECT;
00046     ASSERT_READY_TO_ALL_OBSERVER;
00047 
00048     return oSUCCESS;
00049 }
00050 
00051 OStatus
00052 SoundAgent::DoStop(const OSystemEvent& event)
00053 {
00054     soundAgentState = SAS_IDLE;
00055 
00056     DISABLE_ALL_SUBJECT;
00057     DEASSERT_READY_TO_ALL_OBSERVER;
00058 
00059     return oSUCCESS;
00060 }
00061 
00062 OStatus
00063 SoundAgent::DoDestroy(const OSystemEvent& event)
00064 {
00065     OPENR::DeleteDesignData(soundDataID);
00066 
00067     DELETE_ALL_SUBJECT_AND_OBSERVER;
00068 
00069     return oSUCCESS;
00070 }
00071 
00072 void
00073 SoundAgent::NotifyCommand(const ONotifyEvent& event)
00074 {
00075     OSYSDEBUG(("SoundAgent::NotifyCommand()\n"));
00076 
00077     MoNetAgentCommand* cmd = (MoNetAgentCommand*)event.Data(0);
00078     
00079     if (soundAgentState == SAS_IDLE) {
00080 
00081         ; // do nothing
00082         
00083     } else if (soundAgentState == SAS_START) {
00084 
00085         byte* data = soundODA.GetData(cmd->index);
00086         if (data == 0 || cmd->agent != monetagentSOUND) {
00087             ReplyAgentResult(cmd->agent, cmd->index, monetINVALID_ARG);
00088             observer[event.ObsIndex()]->AssertReady();
00089             return;
00090         }
00091 
00092         MoNetStatus st = Play(cmd->index, cmd->syncKey, data);
00093         if (st != monetSUCCESS) {
00094             ReplyAgentResult(cmd->agent, cmd->index, st);
00095             observer[event.ObsIndex()]->AssertReady();
00096             return;
00097         }
00098 
00099         soundAgentState = SAS_PLAYING;
00100         observer[event.ObsIndex()]->AssertReady();
00101 
00102     } else if (soundAgentState == SAS_PLAYING) {
00103 
00104         ReplyAgentResult(cmd->agent, cmd->index, monetBUSY);
00105         observer[event.ObsIndex()]->AssertReady();
00106 
00107     }
00108 }
00109 
00110 void
00111 SoundAgent::ReadyPlay(const OReadyEvent& event)
00112 {
00113     if (soundAgentState == SAS_PLAYING) {
00114 
00115         RCRegion* rgn = FindFreeRegion();
00116         if (CopyWAVTo(rgn) == WAV_SUCCESS) {
00117             subject[sbjSpeaker]->SetData(rgn);
00118             subject[sbjSpeaker]->NotifyObservers();
00119         } else {
00120             ReplyAgentResult(monetagentSOUND, playingIndex, monetCOMPLETION);
00121             soundAgentState = SAS_START;
00122         }
00123 
00124     }
00125 }
00126 
00127 MoNetStatus
00128 SoundAgent::Play(int index, OVRSyncKey syncKey, byte* data)
00129 {
00130     OSYSDEBUG(("SoundAgent::Play()\n"));
00131 
00132     playingIndex = index;
00133     playingWAV.Set(data);
00134     
00135     OStatus result;
00136     if (playingWAV.GetSamplingRate() == 16000 && 
00137         playingWAV.GetBitsPerSample() == 16) {
00138         OPrimitiveControl_SpeakerSoundType soundType(ospksndMONO16K16B);
00139         result = OPENR::ControlPrimitive(speakerID,
00140                                          oprmreqSPEAKER_SET_SOUND_TYPE,
00141                                          &soundType, sizeof(soundType), 0, 0);
00142     } else {
00143         OPrimitiveControl_SpeakerSoundType soundType(ospksndMONO8K8B);
00144         result = OPENR::ControlPrimitive(speakerID,
00145                                          oprmreqSPEAKER_SET_SOUND_TYPE,
00146                                          &soundType, sizeof(soundType), 0, 0);
00147     }
00148     if (result != oSUCCESS) {
00149         OSYSLOG1((osyslogERROR, "%s : %s %d",
00150                   "SoundAgent::Play()", 
00151                   "OPENR::ControlPrimitive(SPEAKER_SET_SOUND_TYPE) FAILED",
00152                   result));
00153     }
00154 
00155     RCRegion* rgn = FindFreeRegion();
00156     OSoundVectorData* soundVec = (OSoundVectorData*)rgn->Base();
00157     soundVec->vectorInfo.syncKey = syncKey;
00158 
00159     if (CopyWAVTo(rgn) == WAV_SUCCESS) {
00160         subject[sbjSpeaker]->SetData(rgn);
00161     } else {
00162         OSYSLOG1((osyslogERROR, "SoundAgent::Play() : INVALID WAV"));
00163         return monetINVALID_WAV;
00164     }
00165     
00166     rgn = FindFreeRegion();
00167     if (CopyWAVTo(rgn) == WAV_SUCCESS) {
00168         subject[sbjSpeaker]->SetData(rgn);
00169     }
00170     
00171     subject[sbjSpeaker]->NotifyObservers();
00172     return monetSUCCESS;
00173 }
00174 
00175 void
00176 SoundAgent::OpenSpeaker()
00177 {
00178     OStatus result;
00179     char design[orobotdesignNAME_MAX+1];
00180 
00181     result = OPENR::GetRobotDesign(design);
00182     if (result != oSUCCESS) {
00183         OSYSLOG1((osyslogERROR, "%s : %s %d",
00184                   "SoundPlay::OpenSpeaker()",
00185                   "OPENR::GetRobotDesign() FAILED", result));
00186     }
00187 
00188     if (!strcmp(design, "ERS-7")) {
00189         result = OPENR::OpenPrimitive(SPEAKER_LOCATOR_ERS7, &speakerID);
00190     } else { // ERS-210 or ERS-220
00191         result = OPENR::OpenPrimitive(SPEAKER_LOCATOR, &speakerID);
00192     }
00193 
00194     if (result != oSUCCESS) {
00195         OSYSLOG1((osyslogERROR, "%s : %s %d",
00196                   "SoundAgent::OpenSpeaker()",
00197                   "OPENR::OpenPrimitive() FAILED", result));
00198     }
00199 }
00200 
00201 void
00202 SoundAgent::NewSoundVectorData()
00203 {
00204     OStatus result;
00205     MemoryRegionID    soundVecDataID;
00206     OSoundVectorData* soundVecData;
00207 
00208     for (int i = 0; i < SOUND_NUM_BUFFER; i++) {
00209 
00210         result = OPENR::NewSoundVectorData(1, SOUND_UNIT_SIZE,
00211                                            &soundVecDataID, &soundVecData);
00212         if (result != oSUCCESS) {
00213             OSYSLOG1((osyslogERROR, "%s : %s %d",
00214                       "SoundAgent::NewSoundVectorData()",
00215                       "OPENR::NewSoundVectorData() FAILED", result));
00216             return;
00217         }
00218 
00219         soundVecData->SetNumData(1);
00220         soundVecData->GetInfo(0)->Set(odataSOUND_VECTOR,
00221                                       speakerID, SOUND_UNIT_SIZE);
00222 
00223         region[i] = new RCRegion(soundVecData->vectorInfo.memRegionID,
00224                                  soundVecData->vectorInfo.offset,
00225                                  (void*)soundVecData,
00226                                  soundVecData->vectorInfo.totalSize);
00227     }
00228 }
00229 
00230 void
00231 SoundAgent::LoadODA()
00232 {
00233     OStatus result;
00234     size_t size;
00235     byte*  addr;
00236 
00237     result = OPENR::FindDesignData(MONET_SOUND_KEYWORD,
00238                                    &soundDataID, &addr, &size);
00239     if (result != oSUCCESS) {
00240         OSYSLOG1((osyslogERROR, "%s : %s %d",
00241                   "SoundAgent::LoadODA()",
00242                   "OPENR::FindDesignData() FAILED", result));
00243         return;
00244     }
00245 
00246     soundODA.Set(addr);
00247 }
00248 
00249 void
00250 SoundAgent::SetPowerAndVolume()
00251 {
00252     OSYSDEBUG(("SoundAgent::SetPowerAndVolume()\n"));
00253 
00254     OStatus result;
00255 
00256     result = OPENR::ControlPrimitive(speakerID,
00257                                      oprmreqSPEAKER_MUTE_ON, 0, 0, 0, 0);
00258     if (result != oSUCCESS) {
00259         OSYSLOG1((osyslogERROR, "%s : %s %d",
00260                   "SoundAgent::SetPowerAndVolume()", 
00261                   "OPENR::ControlPrimitive(oprmreqSPEAKER_MUTE_ON) FAILED",
00262                   result));
00263     }
00264 
00265     result = OPENR::SetMotorPower(opowerON);
00266     if (result != oSUCCESS) {
00267         OSYSLOG1((osyslogERROR, "%s : %s %d",
00268                   "SoundAgent::SetPowerAndVolume()", 
00269                   "OPENR::SetMotorPower() FAILED", result));
00270     }
00271 
00272     result = OPENR::ControlPrimitive(speakerID,
00273                                      oprmreqSPEAKER_MUTE_OFF, 0, 0, 0, 0);
00274     if (result != oSUCCESS) {
00275         OSYSLOG1((osyslogERROR, "%s : %s %d",
00276                   "SoundAgent::SetPowerAndVolume()", 
00277                   "OPENR::ControlPrimitive(oprmreqSPEAKER_MUTE_OFF) FAILED",
00278                   result));
00279     }
00280 
00281     OSpeakerVolume vol;
00282     OVolumeSwitch volSW;
00283     result = OPENR::GetVolumeSwitch(&volSW);
00284     if (result != oSUCCESS) {
00285         OSYSLOG1((osyslogERROR, "%s : %s %d",
00286                   "SoundAgent::SetPowerAndVolume()", 
00287                   "OPENR::GetVolumeSwitch() FAILED", result));
00288         volSW = ovolumeSW0;
00289     }
00290 
00291     OSYSDEBUG(("volSW is %d.\n", volSW));
00292     if (volSW == ovolumeSW0) {
00293         vol = ospkvolinfdB;
00294     } else if (volSW == ovolumeSW1) {
00295         vol = ospkvol25dB;
00296     } else if (volSW == ovolumeSW2) {
00297         vol = ospkvol18dB;
00298     } else { // volSW == ovolumeSW3
00299         vol = ospkvol10dB;
00300     }
00301 
00302     OPrimitiveControl_SpeakerVolume volume(vol);
00303     result = OPENR::ControlPrimitive(speakerID,
00304                                      oprmreqSPEAKER_SET_VOLUME,
00305                                      &volume, sizeof(volume), 0, 0);
00306     if (result != oSUCCESS) {
00307         OSYSLOG1((osyslogERROR, "%s : %s %d",
00308                   "SoundAgent::SetPowerAndVolume()", 
00309                   "OPENR::ControlPrimitive(oprmreqSPEAKER_SET_VOLUME) FAILED",
00310                   result));
00311     }
00312 }
00313 
00314 WAVError
00315 SoundAgent::CopyWAVTo(RCRegion* rgn)
00316 {
00317     OSoundVectorData* soundVecData = (OSoundVectorData*)rgn->Base();
00318     return playingWAV.CopyTo(soundVecData);
00319 }
00320 
00321 RCRegion*
00322 SoundAgent::FindFreeRegion()
00323 {
00324     for (int i = 0; i < SOUND_NUM_BUFFER; i++) {
00325         if (region[i]->NumberOfReference() == 1) return region[i];
00326     }
00327 
00328     return 0;
00329 }
00330 
00331 void
00332 SoundAgent::ReplyAgentResult(MoNetAgentID agt, int idx, MoNetStatus st)
00333 {
00334     MoNetAgentResult result(agt, idx, st, monetpostureANY);
00335     subject[sbjResult]->SetData(&result, sizeof(result));
00336     subject[sbjResult]->NotifyObservers();
00337 }

Generated on Sun Dec 2 23:04:30 2007 for openSDK by  doxygen 1.3.9.1