common/MoNet/SoundAgent/WAV.cc

Go to the documentation of this file.
00001 //
00002 // Copyright 2002,2003 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/OPENR.h>
00013 #include <OPENR/OSyslog.h>
00014 #include "WAV.h"
00015 
00016 WAV::WAV() : soundInfo(), dataStart(0), dataEnd(0), dataCurrent(0)
00017 {
00018 }
00019 
00020 WAV::WAV(byte* addr) : dataStart(0), dataEnd(0), dataCurrent(0)
00021 {
00022     Set(addr);
00023 }
00024 
00025 WAVError
00026 WAV::Set(byte *addr)
00027 {
00028     //
00029     // Check Wav Header
00030     //
00031     if (strncmp((char *)addr, "RIFF", 4)) return WAV_NOT_RIFF;
00032     addr += 4;
00033 
00034     longword length = get_longword(addr);
00035     addr += sizeof(longword);
00036     OSYSDEBUG(( "length = %x\n", length));
00037 
00038     if (strncmp((char *)addr, "WAVE", 4)) return WAV_NOT_WAV;
00039     length -= 4;
00040     addr += 4;
00041 
00042     //
00043     // Check Chunk
00044     //
00045     while (length > 8) {
00046 
00047         size_t chunksize;
00048         char *buf = (char *)addr;
00049     
00050         addr += 4;
00051 
00052         chunksize = get_longword(addr);
00053         addr += sizeof(longword);
00054         length -= chunksize + 8;
00055 
00056         if (!strncmp(buf, "fmt ", 4)) {
00057 
00058             //
00059             // Format Chunk
00060             //
00061 
00062             //
00063             // Check WAV Type
00064             //
00065             soundInfo.format = (OSoundFormat)get_word(addr);
00066             addr += sizeof(word);
00067             if (soundInfo.format != osoundformatPCM) {
00068                 OSYSDEBUG(("WAV_FORMAT_NOT_SUPPORTED\n"));
00069                 return WAV_FORMAT_NOT_SUPPORTED;
00070             }
00071 
00072             //
00073             // Channel
00074             //
00075             soundInfo.channel = (OSoundChannel)get_word(addr);
00076             addr += sizeof(word);
00077             if (soundInfo.channel != osoundchannelMONO) {
00078                 OSYSDEBUG(("WAV_CHANNEL_NOT_SUPPORTED\n"));
00079                 return WAV_CHANNEL_NOT_SUPPORTED;
00080             }
00081 
00082             //
00083             // Sampling Rate
00084             //
00085             longword frq = get_longword(addr);
00086             addr += sizeof(longword);
00087             soundInfo.samplingRate = (word)frq;
00088             if (soundInfo.samplingRate != 8000 &&
00089                 soundInfo.samplingRate != 16000) {
00090                 OSYSDEBUG(("WAV_SAMPLINGRATE_NOT_SUPPORTED\n"));
00091                 return WAV_SAMPLINGRATE_NOT_SUPPORTED;
00092             }
00093 
00094             //
00095             // DataSize Per sec
00096             //
00097             addr += sizeof(longword);
00098 
00099             //
00100             // Block Size
00101             //
00102             addr += sizeof(word);
00103 
00104             //
00105             // Bits Of Sample
00106             //
00107             soundInfo.bitsPerSample = get_word(addr);
00108             addr += sizeof(word);
00109             soundInfo.bitsPerSample *= soundInfo.channel;
00110             if (soundInfo.bitsPerSample != 8 &&
00111                 soundInfo.bitsPerSample != 16) {
00112                 OSYSDEBUG(("WAV_BITSPERSAMPLE_NOT_SUPPORTED\n"));
00113                 return WAV_BITSPERSAMPLE_NOT_SUPPORTED;
00114             }
00115 
00116             //
00117             // Skip Extentded Infomation
00118             //
00119             addr += chunksize - FMTSIZE_WITHOUT_EXTINFO;
00120             
00121             OSYSDEBUG(( "fmt chunksize = %d\n", chunksize));
00122             OSYSDEBUG(( "samplingRate  = %d\n", soundInfo.samplingRate));
00123             OSYSDEBUG(( "bitsPerSample = %d\n", soundInfo.bitsPerSample));
00124             
00125         } else if (!strncmp(buf, "data", 4)) {
00126 
00127             //
00128             // Data Chunk
00129             //
00130             OSYSDEBUG(( "data chunksize = %d\n", chunksize));
00131             soundInfo.dataSize = chunksize;
00132             dataStart = dataCurrent = addr;
00133             dataEnd = dataStart + soundInfo.dataSize;
00134             break;
00135 
00136         } else {
00137 
00138             //
00139             // Fact Chunk
00140             //
00141             addr += chunksize;
00142         }
00143     }
00144 
00145     int rate = soundInfo.samplingRate;
00146     int bits = soundInfo.bitsPerSample;
00147     if (rate == 8000 & bits == 8) {
00148         soundUnitSize = MONO8K8B_UNIT_SIZE;
00149     } else if (rate == 16000 & bits == 16) {
00150         soundUnitSize = MONO16K16B_UNIT_SIZE;
00151     }
00152     
00153     return WAV_SUCCESS;
00154 }
00155 
00156 WAVError
00157 WAV::CopyTo(OSoundVectorData* data)
00158 {
00159     if (dataCurrent >= dataEnd) return WAV_FAIL;
00160     
00161     OSoundInfo* sinfo = data->GetInfo(0);
00162     if (soundUnitSize > sinfo->maxDataSize) {
00163         OSYSDEBUG(("WAV_SIZE_NOT_ENOUGH "));
00164         return WAV_SIZE_NOT_ENOUGH;
00165     }
00166 
00167     sinfo->dataSize      = soundUnitSize;
00168     sinfo->format        = soundInfo.format;
00169     sinfo->channel       = soundInfo.channel;
00170     sinfo->samplingRate  = soundInfo.samplingRate;
00171     sinfo->bitsPerSample = soundInfo.bitsPerSample;
00172 
00173     byte* src  = dataCurrent;
00174     byte* dest = data->GetData(0);
00175     byte* end;
00176     int num = (int)(dataEnd - dataCurrent);
00177 
00178     if (soundUnitSize <= num) {
00179     
00180         end = dest + soundUnitSize;
00181         if (soundUnitSize == MONO8K8B_UNIT_SIZE) {
00182             while (dest < end) {
00183                 *dest++ = *src++ ^ 0x80; // offset binary -> signed char
00184             }
00185         } else { // MONO16K16B_UNIT_SIZE
00186             while (dest < end) {
00187                 *dest++ = *src++;
00188             }
00189         }
00190         dataCurrent += soundUnitSize;
00191 
00192     } else {
00193 
00194         end = dest + num;
00195         if (soundUnitSize == MONO8K8B_UNIT_SIZE) {
00196             while (dest < end) {
00197                 *dest++ = *src++ ^ 0x80; // offset binary -> signed char
00198             }
00199         } else { // MONO16K16B_UNIT_SIZE
00200             while (dest < end) {
00201                 *dest++ = *src++;
00202             }
00203         }
00204         memset(dest, 0x0, soundUnitSize - num);
00205         dataCurrent = dataEnd;
00206 
00207     }
00208 
00209     return WAV_SUCCESS;
00210 }
00211 
00212 WAVError
00213 WAV::Rewind()
00214 {
00215     dataCurrent = dataStart;
00216     return WAV_SUCCESS;
00217 }
00218 
00219 longword
00220 WAV::get_longword(byte* ptr)
00221 {
00222     longword lw0 = (longword)ptr[0];
00223     longword lw1 = (longword)ptr[1] << 8;
00224     longword lw2 = (longword)ptr[2] << 16;
00225     longword lw3 = (longword)ptr[3] << 24;
00226     return lw0 + lw1 + lw2 + lw3;
00227 }
00228 
00229 word
00230 WAV::get_word(byte* ptr)
00231 {
00232     word w0 = (word)ptr[0];
00233     word w1 = (word)ptr[1] << 8;
00234     return w0 + w1;
00235 }

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