00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include <OPENR/OPENRAPI.h>
00013 #include <OPENR/OSyslog.h>
00014 #include <OPENR/core_macro.h>
00015 #include "SoundPlay.h"
00016
00017 SoundPlay::SoundPlay() : soundPlayState(SPS_IDLE),
00018 speakerID(oprimitiveID_UNDEF),
00019 wavID(odesigndataID_UNDEF), wav()
00020 {
00021 for (int i = 0; i < SOUND_NUM_BUFFER; i++) region[i] = 0;
00022 }
00023
00024 OStatus
00025 SoundPlay::DoInit(const OSystemEvent& event)
00026 {
00027 OSYSDEBUG(("SoundPlay::DoInit()\n"));
00028
00029 NEW_ALL_SUBJECT_AND_OBSERVER;
00030 REGISTER_ALL_ENTRY;
00031 SET_ALL_READY_AND_NOTIFY_ENTRY;
00032
00033 LoadWAV();
00034
00035 OpenSpeaker();
00036 NewSoundVectorData();
00037 SetPowerAndVolume();
00038
00039 return oSUCCESS;
00040 }
00041
00042 OStatus
00043 SoundPlay::DoStart(const OSystemEvent& event)
00044 {
00045 OSYSDEBUG(("SoundPlay::DoStart()\n"));
00046
00047 for (int i = 0; i < SOUND_NUM_BUFFER; i++) {
00048 if (CopyWAVTo(region[i]) == WAV_SUCCESS)
00049 subject[sbjSpeaker]->SetData(region[i]);
00050 }
00051 subject[sbjSpeaker]->NotifyObservers();
00052 soundPlayState = SPS_START;
00053
00054 ENABLE_ALL_SUBJECT;
00055 ASSERT_READY_TO_ALL_OBSERVER;
00056
00057 return oSUCCESS;
00058 }
00059
00060 OStatus
00061 SoundPlay::DoStop(const OSystemEvent& event)
00062 {
00063 OSYSDEBUG(("SoundPlay::DoStop()\n"));
00064
00065 soundPlayState = SPS_IDLE;
00066
00067 DISABLE_ALL_SUBJECT;
00068 DEASSERT_READY_TO_ALL_OBSERVER;
00069
00070 return oSUCCESS;
00071 }
00072
00073 OStatus
00074 SoundPlay::DoDestroy(const OSystemEvent& event)
00075 {
00076 OPENR::DeleteDesignData(wavID);
00077 DELETE_ALL_SUBJECT_AND_OBSERVER;
00078 return oSUCCESS;
00079 }
00080
00081 void
00082 SoundPlay::Ready(const OReadyEvent& event)
00083 {
00084 OSYSDEBUG(("SoundPlay::Ready()\n"));
00085
00086 if (soundPlayState == SPS_START) {
00087
00088 RCRegion* rgn = FindFreeRegion();
00089 if (CopyWAVTo(rgn) == WAV_SUCCESS) {
00090 subject[sbjSpeaker]->SetData(rgn);
00091 subject[sbjSpeaker]->NotifyObservers();
00092 }
00093
00094 }
00095 }
00096
00097 void
00098 SoundPlay::OpenSpeaker()
00099 {
00100 OStatus result;
00101 char design[orobotdesignNAME_MAX+1];
00102
00103 result = OPENR::GetRobotDesign(design);
00104 if (result != oSUCCESS) {
00105 OSYSLOG1((osyslogERROR, "%s : %s %d",
00106 "SoundPlay::OpenSpeaker()",
00107 "OPENR::GetRobotDesign() FAILED", result));
00108 }
00109
00110 if (strcmp(design, "ERS-7") == 0) {
00111 result = OPENR::OpenPrimitive(SPEAKER_LOCATOR_ERS7, &speakerID);
00112 } else {
00113 result = OPENR::OpenPrimitive(SPEAKER_LOCATOR, &speakerID);
00114 }
00115
00116 if (result != oSUCCESS) {
00117 OSYSLOG1((osyslogERROR, "%s : %s %d",
00118 "SoundPlay::OpenSpeaker()",
00119 "OPENR::OpenPrimitive() FAILED", result));
00120 }
00121 }
00122
00123 void
00124 SoundPlay::NewSoundVectorData()
00125 {
00126 OStatus result;
00127 MemoryRegionID soundVecDataID;
00128 OSoundVectorData* soundVecData;
00129
00130 size_t soundUnitSize = wav.GetSoundUnitSize();
00131
00132 for (int i = 0; i < SOUND_NUM_BUFFER; i++) {
00133
00134 result = OPENR::NewSoundVectorData(1, soundUnitSize,
00135 &soundVecDataID, &soundVecData);
00136 if (result != oSUCCESS) {
00137 OSYSLOG1((osyslogERROR, "%s : %s %d",
00138 "SoundPlay::NewSoundVectorData()",
00139 "OPENR::NewSoundVectorData() FAILED", result));
00140 return;
00141 }
00142
00143 soundVecData->SetNumData(1);
00144 soundVecData->GetInfo(0)->Set(odataSOUND_VECTOR,
00145 speakerID, soundUnitSize);
00146
00147 region[i] = new RCRegion(soundVecData->vectorInfo.memRegionID,
00148 soundVecData->vectorInfo.offset,
00149 (void*)soundVecData,
00150 soundVecData->vectorInfo.totalSize);
00151 }
00152 }
00153
00154 void
00155 SoundPlay::LoadWAV()
00156 {
00157 byte* addr;
00158 size_t size;
00159
00160 OStatus result = OPENR::FindDesignData("SOUNDPLAY_WAV",
00161 &wavID, &addr, &size);
00162 if (result != oSUCCESS) {
00163 OSYSLOG1((osyslogERROR, "%s : %s %d",
00164 "SoundPlay::LoadWAV()",
00165 "OPENR::FindDesignData() FAILED", result));
00166 return;
00167 }
00168
00169 WAVError error = wav.Set(addr);
00170 if (error != WAV_SUCCESS) {
00171 OSYSLOG1((osyslogERROR, "%s : %s %d",
00172 "SoundPlay::LoadWAV()",
00173 "wav.Set() FAILED", error));
00174 }
00175 }
00176
00177 void
00178 SoundPlay::SetPowerAndVolume()
00179 {
00180 OStatus result;
00181
00182 result = OPENR::ControlPrimitive(speakerID,
00183 oprmreqSPEAKER_MUTE_ON, 0, 0, 0, 0);
00184 if (result != oSUCCESS) {
00185 OSYSLOG1((osyslogERROR, "%s : %s %d",
00186 "SoundPlay::SetPowerAndVolume()",
00187 "OPENR::ControlPrimitive(SPEAKER_MUTE_ON) FAILED", result));
00188 }
00189
00190 result = OPENR::SetMotorPower(opowerON);
00191 if (result != oSUCCESS) {
00192 OSYSLOG1((osyslogERROR, "%s : %s %d",
00193 "SoundPlay::SetPowerAndVolume()",
00194 "OPENR::SetMotorPower() FAILED", result));
00195 }
00196
00197 result = OPENR::ControlPrimitive(speakerID,
00198 oprmreqSPEAKER_MUTE_OFF, 0, 0, 0, 0);
00199 if (result != oSUCCESS) {
00200 OSYSLOG1((osyslogERROR, "%s : %s %d",
00201 "SoundPlay::SetPowerAndVolume()",
00202 "OPENR::ControlPrimitive(SPEAKER_MUTE_OFF) FAILED", result));
00203 }
00204
00205 OPrimitiveControl_SpeakerVolume volume(ospkvol10dB);
00206 result = OPENR::ControlPrimitive(speakerID,
00207 oprmreqSPEAKER_SET_VOLUME,
00208 &volume, sizeof(volume), 0, 0);
00209 if (result != oSUCCESS) {
00210 OSYSLOG1((osyslogERROR, "%s : %s %d",
00211 "SoundPlay::SetPowerAndVolume()",
00212 "OPENR::ControlPrimitive(SPEAKER_SET_VOLUME) FAILED",
00213 result));
00214 }
00215
00216 if (wav.GetSamplingRate() == 16000 && wav.GetBitsPerSample() == 16) {
00217 OPrimitiveControl_SpeakerSoundType soundType(ospksndMONO16K16B);
00218 result = OPENR::ControlPrimitive(speakerID,
00219 oprmreqSPEAKER_SET_SOUND_TYPE,
00220 &soundType, sizeof(soundType), 0, 0);
00221 if (result != oSUCCESS) {
00222 OSYSLOG1((osyslogERROR, "%s : %s %d",
00223 "SoundPlay::SetPowerAndVolume()",
00224 "OPENR::ControlPrimitive(SPEAKER_SET_SOUND_TYPE) FAILED",
00225 result));
00226 }
00227 }
00228 }
00229
00230 WAVError
00231 SoundPlay::CopyWAVTo(RCRegion* rgn)
00232 {
00233 OSoundVectorData* soundVecData = (OSoundVectorData*)rgn->Base();
00234
00235 WAVError error = wav.CopyTo(soundVecData);
00236 if (error != WAV_SUCCESS) {
00237 wav.Rewind();
00238 error = wav.CopyTo(soundVecData);
00239 }
00240
00241 return error;
00242 }
00243
00244 RCRegion*
00245 SoundPlay::FindFreeRegion()
00246 {
00247 for (int i = 0; i < SOUND_NUM_BUFFER; i++) {
00248 if (region[i]->NumberOfReference() == 1) return region[i];
00249 }
00250
00251 return 0;
00252 }