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