SoundRec.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 <stdlib.h>
00013 #include <OPENR/OPENRAPI.h>
00014 #include <OPENR/OSyslog.h>
00015 #include <OPENR/core_macro.h>
00016 #include "SoundRec.h"
00017 
00018 SoundRec::SoundRec(void) : soundRecState(SRS_IDLE),
00019                            soundBuf(0), soundBufPtr(0)
00020 {
00021 }
00022 
00023 OStatus
00024 SoundRec::DoInit(const OSystemEvent& event)
00025 {
00026     NEW_ALL_SUBJECT_AND_OBSERVER;
00027     REGISTER_ALL_ENTRY;
00028     SET_ALL_READY_AND_NOTIFY_ENTRY;
00029 
00030     char design[orobotdesignNAME_MAX+1];
00031     OStatus result = OPENR::GetRobotDesign(design);
00032     if (result != oSUCCESS) {
00033         OSYSLOG1((osyslogERROR, "%s : %s %d",
00034                   "SoundRec::DoInit()",
00035                   "OPENR::GetRobotDesign() FAILED", result));
00036     }
00037 
00038     if (!strcmp(design, "ERS-210") || !strcmp(design, "ERS-220")) {
00039         ChooseMic(oprmreqMIC_OMNI); // oprmreqMIC_OMNI or oprmreqMIC_UNI
00040     }
00041 
00042     NewSoundBuffer();
00043 
00044     return oSUCCESS;
00045 }
00046 
00047 OStatus
00048 SoundRec::DoStart(const OSystemEvent& event)
00049 {
00050     OSYSPRINT(("START RECORDING (about 16sec) ... \n"));
00051     if (soundBuf != 0) soundRecState = SRS_START;
00052 
00053     ENABLE_ALL_SUBJECT;
00054     ASSERT_READY_TO_ALL_OBSERVER;
00055 
00056     return oSUCCESS;
00057 }
00058 
00059 OStatus
00060 SoundRec::DoStop(const OSystemEvent& event)
00061 {
00062     soundRecState = SRS_IDLE;
00063 
00064     DISABLE_ALL_SUBJECT;
00065     DEASSERT_READY_TO_ALL_OBSERVER;
00066 
00067     return oSUCCESS;
00068 }
00069 
00070 OStatus
00071 SoundRec::DoDestroy(const OSystemEvent& event)
00072 {
00073     DeleteSoundBuffer();
00074     DELETE_ALL_SUBJECT_AND_OBSERVER;
00075     return oSUCCESS;
00076 }
00077 
00078 void
00079 SoundRec::Notify(const ONotifyEvent& event)
00080 {
00081     // 
00082     // SoundRec::Notify() is invoked : every 16ms (ERS-7)
00083     //                               : every 32ms (ERS-210/220)
00084     //
00085 
00086     if (soundRecState == SRS_IDLE) {
00087 
00088         ; // do nothing
00089         return;
00090 
00091     } else {
00092 
00093         OSoundVectorData* soundVecData = (OSoundVectorData*)event.Data(0);
00094         int size = CopyToSoundBuffer(soundVecData);
00095 
00096         if (size < SOUND_BUFFER_SIZE) {
00097             observer[event.ObsIndex()]->AssertReady(event.SenderID());
00098         } else {
00099             OSYSPRINT(("SAVE SOUNDREC.WAV. WAIT ... "));
00100             SaveSoundBufferAsWAV("/MS/OPEN-R/MW/DATA/P/SOUNDREC.WAV");
00101             OSYSPRINT(("DONE\n"));
00102         }
00103 
00104     }
00105 }
00106 
00107 void
00108 SoundRec::ChooseMic(OPrimitiveRequest req)
00109 {
00110     OStatus result;
00111     OPrimitiveID micID;
00112     
00113     result = OPENR::OpenPrimitive(MIC_LOCATOR, &micID);
00114     if (result != oSUCCESS) {
00115         OSYSLOG1((osyslogERROR, "%s : %s %d",
00116                   "SoundRec::ChooseMic()",
00117                   "OPENR::OpenPrimitive() FAILED", result));
00118         return;
00119     }
00120 
00121     result = OPENR::ControlPrimitive(micID, req, 0, 0, 0, 0);
00122     if (result != oSUCCESS) {
00123         OSYSLOG1((osyslogERROR, "%s : %s %d",
00124                   "SoundRec::ChooseMic()",
00125                   "OPENR::ControlPrimitive() FAILED", result));
00126     }
00127 }
00128 
00129 void
00130 SoundRec::NewSoundBuffer()
00131 {
00132     soundBuf = (byte*)malloc(SOUND_BUFFER_SIZE);
00133     if (soundBuf == 0) {
00134         OSYSLOG1((osyslogERROR,
00135                   "SoundRec::NewSoundBuffer() : malloc() failed"));
00136     }
00137 
00138     soundBufPtr = soundBuf;    
00139 }
00140 
00141 void
00142 SoundRec::DeleteSoundBuffer()
00143 {
00144     if (soundBuf != 0) {
00145         free(soundBuf);
00146         soundBuf = soundBufPtr = 0;
00147     }
00148 }
00149 
00150 int
00151 SoundRec::CopyToSoundBuffer(OSoundVectorData* soundVecData)
00152 {
00153     OSoundInfo* info = soundVecData->GetInfo(0);
00154     byte* data = soundVecData->GetData(0);
00155     memcpy(soundBufPtr, data, info->dataSize);
00156     soundBufPtr += info->dataSize;
00157 
00158     return (int)(soundBufPtr - soundBuf);
00159 }
00160 
00161 void
00162 SoundRec::SaveSoundBufferAsWAV(char* path)
00163 {
00164     longword lwd;
00165     word     wd;
00166 
00167     FILE* fp = fopen(path, "w");
00168     if (fp == 0) {
00169         OSYSLOG1((osyslogERROR,
00170                   "SoundRec::SaveSoundBufferAsWAV() : can't open %s", path));
00171         return;
00172     }
00173 
00174     fwrite("RIFF", 1, 4, fp);
00175 
00176     // RIFF data size
00177     lwd = WAVE_HEADER_SIZE + SOUND_BUFFER_SIZE;
00178     fwrite(&lwd, sizeof(lwd), 1, fp);
00179 
00180     // "WAVE"
00181     fwrite("WAVE", 1, 4, fp);
00182 
00183     // "fmt "
00184     fwrite("fmt ", 1, 4, fp);
00185 
00186     // Length of the fmt chunk
00187     lwd = FMTSIZE_WITHOUT_EXTINFO;
00188     fwrite(&lwd, sizeof(lwd), 1, fp);
00189 
00190     // WAVE File Encoding Tag (PCM)
00191     wd = 0x01;
00192     fwrite(&wd, sizeof(wd), 1, fp);
00193 
00194     // Channels (STEREO)
00195     wd = 0x02;
00196     fwrite(&wd, sizeof(wd), 1, fp);
00197 
00198     // Sampling Rate (16kHz)
00199     lwd = 16000;
00200     fwrite(&lwd, sizeof(lwd), 1, fp);
00201 
00202     // bytes/sec (16KHz * 16bits * STEREO)
00203     lwd = 16000 * 2 * 2;
00204     fwrite(&lwd, sizeof(lwd), 1, fp);
00205 
00206     // Block Align (channels * bits/sample / 8)
00207     wd = 0x04;
00208     fwrite(&wd, sizeof(wd), 1, fp);
00209 
00210     // bits/sample
00211     wd = 16;
00212     fwrite(&wd, sizeof(wd), 1, fp);
00213 
00214     // "data"
00215     fwrite("data", 1, 4, fp);
00216 
00217     // data size
00218     lwd = SOUND_BUFFER_SIZE;
00219     fwrite(&lwd, sizeof(lwd), 1, fp);
00220 
00221     // data
00222     size_t nwrite = fwrite(soundBuf, 1, lwd, fp);
00223     if (nwrite != lwd) {
00224         OSYSLOG1((osyslogERROR, "%s : %s %d",
00225                   "SoundRec::SaveSoundBufferAsWAV()",
00226                   "fwrite() FAILED", nwrite));
00227     }
00228 
00229     fclose(fp);
00230 }

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