home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 2003 March / VPR0303A.ISO / AIBO / MoNet / SoundAgent / SoundAgent.cc < prev    next >
C/C++ Source or Header  |  2002-12-19  |  8KB  |  303 lines

  1. //
  2. // Copyright 2002 Sony Corporation 
  3. //
  4. // Permission to use, copy, modify, and redistribute this software for
  5. // non-commercial use is hereby granted.
  6. //
  7. // This software is provided "as is" without warranty of any kind,
  8. // either expressed or implied, including but not limited to the
  9. // implied warranties of fitness for a particular purpose.
  10. //
  11.  
  12. #include <OPENR/OPENRAPI.h>
  13. #include <OPENR/OSyslog.h>
  14. #include <OPENR/core_macro.h>
  15. #include "SoundAgent.h"
  16.  
  17. SoundAgent::SoundAgent() : soundAgentState(SAS_IDLE),
  18.                            speakerID(oprimitiveID_UNDEF),
  19.                            soundDataID(odesigndataID_UNDEF),
  20.                            soundODA(), playingIndex(-1), playingWAV()
  21. {
  22.     for (int i = 0; i < SOUND_NUM_BUFFER; i++) region[i] = 0;
  23. }
  24.  
  25. OStatus
  26. SoundAgent::DoInit(const OSystemEvent& event)
  27. {
  28.     NEW_ALL_SUBJECT_AND_OBSERVER;
  29.     REGISTER_ALL_ENTRY;
  30.     SET_ALL_READY_AND_NOTIFY_ENTRY;
  31.  
  32.     OpenSpeaker();
  33.     NewSoundVectorData();
  34.     LoadODA();
  35.     SetPowerAndVolume();
  36.  
  37.     return oSUCCESS;
  38. }
  39.  
  40. OStatus
  41. SoundAgent::DoStart(const OSystemEvent& event)
  42. {
  43.     soundAgentState = SAS_START;
  44.  
  45.     ENABLE_ALL_SUBJECT;
  46.     ASSERT_READY_TO_ALL_OBSERVER;
  47.  
  48.     return oSUCCESS;
  49. }
  50.  
  51. OStatus
  52. SoundAgent::DoStop(const OSystemEvent& event)
  53. {
  54.     soundAgentState = SAS_IDLE;
  55.  
  56.     DISABLE_ALL_SUBJECT;
  57.     DEASSERT_READY_TO_ALL_OBSERVER;
  58.  
  59.     return oSUCCESS;
  60. }
  61.  
  62. OStatus
  63. SoundAgent::DoDestroy(const OSystemEvent& event)
  64. {
  65.     OPENR::DeleteDesignData(soundDataID);
  66.  
  67.     DELETE_ALL_SUBJECT_AND_OBSERVER;
  68.  
  69.     return oSUCCESS;
  70. }
  71.  
  72. void
  73. SoundAgent::NotifyCommand(const ONotifyEvent& event)
  74. {
  75.     OSYSDEBUG(("SoundAgent::NotifyCommand()\n"));
  76.  
  77.     MoNetAgentCommand* cmd = (MoNetAgentCommand*)event.Data(0);
  78.     
  79.     if (soundAgentState == SAS_IDLE) {
  80.  
  81.         ; // do nothing
  82.         
  83.     } else if (soundAgentState == SAS_START) {
  84.  
  85.         byte* data = soundODA.GetData(cmd->index);
  86.         if (data == 0 || cmd->agent != monetagentSOUND) {
  87.             ReplyAgentResult(cmd->agent, cmd->index, monetINVALID_ARG);
  88.             observer[event.ObsIndex()]->AssertReady();
  89.             return;
  90.         }
  91.  
  92.         MoNetStatus st = Play(cmd->index, cmd->syncKey, data);
  93.         if (st != monetSUCCESS) {
  94.             ReplyAgentResult(cmd->agent, cmd->index, st);
  95.             observer[event.ObsIndex()]->AssertReady();
  96.             return;
  97.         }
  98.  
  99.         soundAgentState = SAS_PLAYING;
  100.         observer[event.ObsIndex()]->AssertReady();
  101.  
  102.     } else if (soundAgentState == SAS_PLAYING) {
  103.  
  104.         ReplyAgentResult(cmd->agent, cmd->index, monetBUSY);
  105.         observer[event.ObsIndex()]->AssertReady();
  106.  
  107.     }
  108. }
  109.  
  110. void
  111. SoundAgent::ReadyPlay(const OReadyEvent& event)
  112. {
  113.     if (soundAgentState == SAS_PLAYING) {
  114.  
  115.         RCRegion* rgn = FindFreeRegion();
  116.         if (CopyWAVTo(rgn) == WAV_SUCCESS) {
  117.             subject[sbjSpeaker]->SetData(rgn);
  118.             subject[sbjSpeaker]->NotifyObservers();
  119.         } else {
  120.             ReplyAgentResult(monetagentSOUND, playingIndex, monetCOMPLETION);
  121.             soundAgentState = SAS_START;
  122.         }
  123.  
  124.     }
  125. }
  126.  
  127. MoNetStatus
  128. SoundAgent::Play(int index, OVRSyncKey syncKey, byte* data)
  129. {
  130.     OSYSDEBUG(("SoundAgent::Play()\n"));
  131.  
  132.     playingIndex = index;
  133.     playingWAV.Set(data);
  134.     
  135.     RCRegion* rgn = FindFreeRegion();
  136.     OSoundVectorData* soundVec = (OSoundVectorData*)rgn->Base();
  137.     soundVec->vectorInfo.syncKey = syncKey;
  138.  
  139.     if (CopyWAVTo(rgn) == WAV_SUCCESS) {
  140.         subject[sbjSpeaker]->SetData(rgn);
  141.     } else {
  142.         OSYSLOG1((osyslogERROR, "SoundAgent::Play() : INVALID WAV"));
  143.         return monetINVALID_WAV;
  144.     }
  145.     
  146.     rgn = FindFreeRegion();
  147.     if (CopyWAVTo(rgn) == WAV_SUCCESS) {
  148.         subject[sbjSpeaker]->SetData(rgn);
  149.     }
  150.     
  151.     subject[sbjSpeaker]->NotifyObservers();
  152.     return monetSUCCESS;
  153. }
  154.  
  155. void
  156. SoundAgent::OpenSpeaker()
  157. {
  158.     OStatus result = OPENR::OpenPrimitive(SPEAKER_LOCATOR, &speakerID);
  159.     if (result != oSUCCESS) {
  160.         OSYSLOG1((osyslogERROR, "%s : %s %d",
  161.                   "SoundAgent::OpenSpeaker()",
  162.                   "OPENR::OpenPrimitive() FAILED", result));
  163.     }
  164. }
  165.  
  166. void
  167. SoundAgent::NewSoundVectorData()
  168. {
  169.     OStatus result;
  170.     MemoryRegionID    soundVecDataID;
  171.     OSoundVectorData* soundVecData;
  172.  
  173.     for (int i = 0; i < SOUND_NUM_BUFFER; i++) {
  174.  
  175.         result = OPENR::NewSoundVectorData(1, SOUND_UNIT_SIZE,
  176.                                            &soundVecDataID, &soundVecData);
  177.         if (result != oSUCCESS) {
  178.             OSYSLOG1((osyslogERROR, "%s : %s %d",
  179.                       "SoundAgent::NewSoundVectorData()",
  180.                       "OPENR::NewSoundVectorData() FAILED", result));
  181.             return;
  182.         }
  183.  
  184.         soundVecData->SetNumData(1);
  185.         soundVecData->GetInfo(0)->Set(odataSOUND_VECTOR,
  186.                                       speakerID, SOUND_UNIT_SIZE);
  187.  
  188.         region[i] = new RCRegion(soundVecData->vectorInfo.memRegionID,
  189.                                  soundVecData->vectorInfo.offset,
  190.                                  (void*)soundVecData,
  191.                                  soundVecData->vectorInfo.totalSize);
  192.     }
  193. }
  194.  
  195. void
  196. SoundAgent::LoadODA()
  197. {
  198.     OStatus result;
  199.     size_t size;
  200.     byte*  addr;
  201.  
  202.     result = OPENR::FindDesignData(MONET_SOUND_KEYWORD,
  203.                                    &soundDataID, &addr, &size);
  204.     if (result != oSUCCESS) {
  205.         OSYSLOG1((osyslogERROR, "%s : %s %d",
  206.                   "SoundAgent::LoadODA()",
  207.                   "OPENR::FindDesignData() FAILED", result));
  208.         return;
  209.     }
  210.  
  211.     soundODA.Set(addr);
  212. }
  213.  
  214. void
  215. SoundAgent::SetPowerAndVolume()
  216. {
  217.     OSYSDEBUG(("SoundAgent::SetPowerAndVolume()\n"));
  218.  
  219.     OStatus result;
  220.  
  221.     result = OPENR::ControlPrimitive(speakerID,
  222.                                      oprmreqSPEAKER_MUTE_ON, 0, 0, 0, 0);
  223.     if (result != oSUCCESS) {
  224.         OSYSLOG1((osyslogERROR, "%s : %s %d",
  225.                   "SoundAgent::SetPowerAndVolume()", 
  226.                   "OPENR::ControlPrimitive(oprmreqSPEAKER_MUTE_ON) FAILED",
  227.                   result));
  228.     }
  229.  
  230.     result = OPENR::SetMotorPower(opowerON);
  231.     if (result != oSUCCESS) {
  232.         OSYSLOG1((osyslogERROR, "%s : %s %d",
  233.                   "SoundAgent::SetPowerAndVolume()", 
  234.                   "OPENR::SetMotorPower() FAILED", result));
  235.     }
  236.  
  237.     result = OPENR::ControlPrimitive(speakerID,
  238.                                      oprmreqSPEAKER_MUTE_OFF, 0, 0, 0, 0);
  239.     if (result != oSUCCESS) {
  240.         OSYSLOG1((osyslogERROR, "%s : %s %d",
  241.                   "SoundAgent::SetPowerAndVolume()", 
  242.                   "OPENR::ControlPrimitive(oprmreqSPEAKER_MUTE_OFF) FAILED",
  243.                   result));
  244.     }
  245.  
  246.     OSpeakerVolume vol;
  247.     OVolumeSwitch volSW;
  248.     result = OPENR::GetVolumeSwitch(&volSW);
  249.     if (result != oSUCCESS) {
  250.         OSYSLOG1((osyslogERROR, "%s : %s %d",
  251.                   "SoundAgent::SetPowerAndVolume()", 
  252.                   "OPENR::GetVolumeSwitch() FAILED", result));
  253.         volSW = ovolumeSW0;
  254.     }
  255.  
  256.     OSYSDEBUG(("volSW is %d.\n", volSW));
  257.     if (volSW == ovolumeSW0) {
  258.         vol = ospkvolinfdB;
  259.     } else if (volSW == ovolumeSW1) {
  260.         vol = ospkvol25dB;
  261.     } else if (volSW == ovolumeSW2) {
  262.         vol = ospkvol18dB;
  263.     } else { // volSW == ovolumeSW3
  264.         vol = ospkvol10dB;
  265.     }
  266.  
  267.     OPrimitiveControl_SpeakerVolume volume(vol);
  268.     result = OPENR::ControlPrimitive(speakerID,
  269.                                      oprmreqSPEAKER_SET_VOLUME,
  270.                                      &volume, sizeof(volume), 0, 0);
  271.     if (result != oSUCCESS) {
  272.         OSYSLOG1((osyslogERROR, "%s : %s %d",
  273.                   "SoundAgent::SetPowerAndVolume()", 
  274.                   "OPENR::ControlPrimitive(oprmreqSPEAKER_SET_VOLUME) FAILED",
  275.                   result));
  276.     }
  277. }
  278.  
  279. WAVError
  280. SoundAgent::CopyWAVTo(RCRegion* rgn)
  281. {
  282.     OSoundVectorData* soundVecData = (OSoundVectorData*)rgn->Base();
  283.     return playingWAV.CopyTo(soundVecData);
  284. }
  285.  
  286. RCRegion*
  287. SoundAgent::FindFreeRegion()
  288. {
  289.     for (int i = 0; i < SOUND_NUM_BUFFER; i++) {
  290.         if (region[i]->NumberOfReference() == 1) return region[i];
  291.     }
  292.  
  293.     return 0;
  294. }
  295.  
  296. void
  297. SoundAgent::ReplyAgentResult(MoNetAgentID agt, int idx, MoNetStatus st)
  298. {
  299.     MoNetAgentResult result(agt, idx, st, monetpostureANY);
  300.     subject[sbjResult]->SetData(&result, sizeof(result));
  301.     subject[sbjResult]->NotifyObservers();
  302. }
  303.