Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

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

Tekkotsu v5.1CVS
Generated Mon May 9 04:58:52 2016 by Doxygen 1.6.3