Homepage Demos Overview Downloads Tutorials Reference
Credits
Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members | Related Pages | Search

WAV.cc

Go to the documentation of this file.
00001 /*! @file
00002  * @brief Allows you to load WAV files from the memory stick
00003  * @author Sony (Creator)
00004  *
00005  * This file is from the SoundPlay example from the Sony sample code, with a few if any modifications.
00006  * Here's the license Sony provided with it:
00007  *
00008  * Copyright 2002,2003 Sony Corporation 
00009  *
00010  * Permission to use, copy, modify, and redistribute this software for
00011  * non-commercial use is hereby granted.
00012  *
00013  * This software is provided "as is" without warranty of any kind,
00014  * either expressed or implied, including but not limited to the
00015  * implied warranties of fitness for a particular purpose.
00016  *
00017  * $Author: ejt $
00018  * $Name: tekkotsu-1_4_1 $
00019  * $Revision: 1.3 $
00020  * $State: Exp $
00021  * $Date: 2003/06/12 23:41:41 $
00022  */
00023 
00024 #include <OPENR/OPENR.h>
00025 #include <OPENR/OSyslog.h>
00026 #include "WAV.h"
00027 
00028 WAV::WAV() : soundInfo(), dataStart(0), dataEnd(0), dataCurrent(0)
00029 {
00030 }
00031 
00032 WAV::WAV(byte* addr) : dataStart(0), dataEnd(0), dataCurrent(0)
00033 {
00034     Set(addr);
00035 }
00036 
00037 WAVError
00038 WAV::Set(byte *addr)
00039 {
00040     //
00041     // Check Wav Header
00042     //
00043     if (strncmp((char *)addr, "RIFF", 4)) return WAV_NOT_RIFF;
00044     addr += 4;
00045 
00046     longword length = get_longword(addr);
00047     addr += sizeof(longword);
00048     //    OSYSDEBUG(( "length = %x\n", length));
00049 
00050     if (strncmp((char *)addr, "WAVE", 4)) return WAV_NOT_WAV;
00051     length -= 4;
00052     addr += 4;
00053 
00054     //
00055     // Check Chunk
00056     //
00057     while (length > 8) {
00058 
00059         size_t chunksize;
00060         char *buf = (char *)addr;
00061     
00062         addr += 4;
00063 
00064         chunksize = get_longword(addr);
00065         addr += sizeof(longword);
00066         length -= chunksize + 8;
00067 
00068         if (!strncmp(buf, "fmt ", 4)) {
00069 
00070             //
00071             // Format Chunk
00072             //
00073 
00074             //
00075             // Check WAV Type
00076             //
00077             soundInfo.format = (OSoundFormat)get_word(addr);
00078             addr += sizeof(word);
00079             if (soundInfo.format != osoundformatPCM) {
00080                 OSYSDEBUG(("WAV_FORMAT_NOT_SUPPORTED\n"));
00081                 return WAV_FORMAT_NOT_SUPPORTED;
00082             }
00083 
00084             //
00085             // Channel
00086             //
00087             soundInfo.channel = (OSoundChannel)get_word(addr);
00088             addr += sizeof(word);
00089             if (soundInfo.channel != osoundchannelMONO) {
00090                 OSYSDEBUG(("WAV_CHANNEL_NOT_SUPPORTED\n"));
00091                 return WAV_CHANNEL_NOT_SUPPORTED;
00092             }
00093 
00094             //
00095             // Sampling Rate
00096             //
00097             longword frq = get_longword(addr);
00098             addr += sizeof(longword);
00099             soundInfo.samplingRate = (word)frq;
00100             if (soundInfo.samplingRate != 8000 &&
00101                 soundInfo.samplingRate != 16000) {
00102                 OSYSDEBUG(("WAV_SAMPLINGRATE_NOT_SUPPORTED\n"));
00103                 return WAV_SAMPLINGRATE_NOT_SUPPORTED;
00104             }
00105 
00106             //
00107             // DataSize Per sec
00108             //
00109             addr += sizeof(longword);
00110 
00111             //
00112             // Block Size
00113             //
00114             addr += sizeof(word);
00115 
00116             //
00117             // Bits Of Sample
00118             //
00119             soundInfo.bitsPerSample = get_word(addr);
00120             addr += sizeof(word);
00121             soundInfo.bitsPerSample *= soundInfo.channel;
00122             if (soundInfo.bitsPerSample != 8 &&
00123                 soundInfo.bitsPerSample != 16) {
00124                 OSYSDEBUG(("WAV_BITSPERSAMPLE_NOT_SUPPORTED\n"));
00125                 return WAV_BITSPERSAMPLE_NOT_SUPPORTED;
00126             }
00127 
00128             //
00129             // Skip Extentded Infomation
00130             //
00131             addr += chunksize - FMTSIZE_WITHOUT_EXTINFO;
00132             
00133             //            OSYSDEBUG(( "fmt chunksize = %d\n", chunksize));
00134             //            OSYSDEBUG(( "samplingRate  = %d\n", soundInfo.samplingRate));
00135             //            OSYSDEBUG(( "bitsPerSample = %d\n", soundInfo.bitsPerSample));
00136             
00137         } else if (!strncmp(buf, "data", 4)) {
00138 
00139             //
00140             // Data Chunk
00141             //
00142           //            OSYSDEBUG(( "data chunksize = %d\n", chunksize));
00143             soundInfo.dataSize = chunksize;
00144             dataStart = dataCurrent = addr;
00145             dataEnd = dataStart + soundInfo.dataSize;
00146             break;
00147 
00148         } else {
00149 
00150             //
00151             // Fact Chunk
00152             //
00153             addr += chunksize;
00154         }
00155     }
00156 
00157     int rate = soundInfo.samplingRate;
00158     int bits = soundInfo.bitsPerSample;
00159     if (rate == 8000 & bits == 8) {
00160         soundUnitSize = MONO8K8B_UNIT_SIZE;
00161     } else if (rate == 16000 & bits == 16) {
00162         soundUnitSize = MONO16K16B_UNIT_SIZE;
00163     }
00164     
00165     return WAV_SUCCESS;
00166 }
00167 
00168 WAVError
00169 WAV::CopyTo(OSoundVectorData* data)
00170 {
00171     if (dataCurrent >= dataEnd) return WAV_FAIL;
00172     
00173     OSoundInfo* sinfo = data->GetInfo(0);
00174     if (soundUnitSize > sinfo->maxDataSize) {
00175         OSYSDEBUG(("WAV_SIZE_NOT_ENOUGH "));
00176         return WAV_SIZE_NOT_ENOUGH;
00177     }
00178 
00179     sinfo->dataSize      = soundUnitSize;
00180     sinfo->format        = soundInfo.format;
00181     sinfo->channel       = soundInfo.channel;
00182     sinfo->samplingRate  = soundInfo.samplingRate;
00183     sinfo->bitsPerSample = soundInfo.bitsPerSample;
00184 
00185     byte* src  = dataCurrent;
00186     byte* dest = data->GetData(0);
00187     byte* end;
00188     int num = (int)(dataEnd - dataCurrent);
00189 
00190     if (soundUnitSize <= num) {
00191     
00192         end = dest + soundUnitSize;
00193         if (soundUnitSize == MONO8K8B_UNIT_SIZE) {
00194             while (dest < end) {
00195                 *dest++ = *src++ ^ 0x80; // offset binary -> signed char
00196             }
00197         } else { // MONO16K16B_UNIT_SIZE
00198             while (dest < end) {
00199                 *dest++ = *src++;
00200             }
00201         }
00202         dataCurrent += soundUnitSize;
00203 
00204     } else {
00205 
00206         end = dest + num;
00207         if (soundUnitSize == MONO8K8B_UNIT_SIZE) {
00208             while (dest < end) {
00209                 *dest++ = *src++ ^ 0x80; // offset binary -> signed char
00210             }
00211         } else { // MONO16K16B_UNIT_SIZE
00212             while (dest < end) {
00213                 *dest++ = *src++;
00214             }
00215         }
00216         memset(dest, 0x0, soundUnitSize - num);
00217         dataCurrent = dataEnd;
00218 
00219     }
00220 
00221     return WAV_SUCCESS;
00222 }
00223 
00224 WAVError
00225 WAV::Rewind()
00226 {
00227     dataCurrent = dataStart;
00228     return WAV_SUCCESS;
00229 }
00230 
00231 longword
00232 WAV::get_longword(byte* ptr)
00233 {
00234     longword lw0 = (longword)ptr[0];
00235     longword lw1 = (longword)ptr[1] << 8;
00236     longword lw2 = (longword)ptr[2] << 16;
00237     longword lw3 = (longword)ptr[3] << 24;
00238     return lw0 + lw1 + lw2 + lw3;
00239 }
00240 
00241 word
00242 WAV::get_word(byte* ptr)
00243 {
00244     word w0 = (word)ptr[0];
00245     word w1 = (word)ptr[1] << 8;
00246     return w0 + w1;
00247 }

Tekkotsu v1.4
Generated Sat Jul 19 00:06:32 2003 by Doxygen 1.3.2