WaveLoader.cpp

Go to the documentation of this file.
00001 #include "WaveLoader.h"
00002 #include "MercuryFiles.h"
00003 #include "MercuryLog.h"
00004 #include "MercuryUtil.h"
00005 
00006 void LoadWav(const MString& path, RawAudioPacket& audio)
00007 {
00008     RiffChunk chunk;
00009     WaveHead head;
00010     FmtChunk dataHead;
00011     char* data = NULL;
00012 
00013     MercuryFile* file = FILEMAN.Open(path);
00014     if (!file)
00015     {
00016         LOG.Log("File " + path + " not opened.");
00017         return;
00018     }
00019 
00020     file->Read(&head, sizeof(WaveHead));
00021     head.info.id = Swap32(head.info.id); //to little endian
00022     TO_ENDIAN( head.info.id );
00023     head.type = Swap32(head.type); //to little endian
00024     TO_ENDIAN( head.type );
00025 
00026     if (head.info.id != 0x52494646)
00027     {
00028         LOG.Log(path + " not a valid RIFF");
00029         return;
00030     }
00031 
00032     if (head.type != 0x57415645)
00033     {
00034         LOG.Log("Only type WAVE supported");
00035         return;
00036     }
00037 
00038     file->Read(&chunk, sizeof(RiffChunk)); //The compiler pads this one, manually set size
00039     file->Read(&dataHead.comp, chunk.size); //The compiler pads this one, manually set size
00040     chunk.id = Swap32(chunk.id); //to little endian
00041     TO_ENDIAN( chunk.id );
00042 
00043     if (chunk.id != 0x666D7420)
00044     {
00045         LOG.Log("Invalid chunk, only fmt chunks supported");
00046         return;
00047     }
00048 
00049     TO_ENDIAN2(dataHead.comp );
00050     TO_ENDIAN2(dataHead.channels );
00051     TO_ENDIAN( dataHead.srate );
00052     if (dataHead.comp != 1)
00053     {
00054         LOG.Log("WAV not PCM");
00055         return;
00056     }
00057 
00058     if (dataHead.channels > 2)
00059     {
00060         LOG.Log("Only mono or stereo is supported");
00061         return;
00062     }
00063 
00064     while (!file->Eof())
00065     {
00066         char str[5];
00067         str[4] = '\0';
00068         file->Read(&chunk, sizeof(RiffChunk));
00069         memcpy(str, &chunk.id, 4);
00070         chunk.id = Swap32(chunk.id);
00071         TO_ENDIAN( chunk.id );
00072         TO_ENDIAN( chunk.size );
00073 
00074         //We don't need "fact" chunks
00075         if (chunk.id == 0x66616374)
00076         {
00077             data = new char[chunk.size];
00078             file->Read(data, chunk.size);
00079             SAFE_DELETE(data);
00080             printf("fact\n");
00081         }
00082         else if (chunk.id == 0x64617461)
00083         {
00084             //the data chunk has the raw data we need
00085 //          if (chunk.size > 51200)
00086 //          {
00087                 //stream larger wav sounds
00088 //          }
00089 //          else
00090 //          {
00091                 //load the data into something since it is small
00092                 data = new char[chunk.size];
00093                 file->Read(data, chunk.size);
00094                 audio.channels = dataHead.channels;
00095                 audio.sampleRate = dataHead.srate;
00096                 audio.size = chunk.size;
00097                 audio.bits = dataHead.sigBPS;
00098                 audio.raw = data;
00099 //          }
00100 
00101             return;
00102         }
00103         else
00104         {
00105 //          unsigned int i = file->Tell() + chunk.size;
00106             LOG.Log(ssprintf("Unexpected chunk ID %x at %x : %s. Skipping %d", chunk.id, file->Tell(), str,chunk.size));    file->Seek( file->Tell() + chunk.size );
00107 //          return;
00108         }
00109     }
00110 }
00111 
00112 /* 
00113  * Copyright (c) 2006 Joshua Allen
00114  * All rights reserved.
00115  *
00116  * Redistribution and use in source and binary forms, with or
00117  * without modification, are permitted provided that the following
00118  * conditions are met:
00119  *  -   Redistributions of source code must retain the above
00120  *      copyright notice, this list of conditions and the following disclaimer.
00121  *  -   Redistributions in binary form must reproduce the above copyright
00122  *      notice, this list of conditions and the following disclaimer in
00123  *      the documentation and/or other materials provided with the distribution.
00124  *  -   Neither the name of the Mercury Engine nor the names of its
00125  *      contributors may be used to endorse or promote products derived from
00126  *      this software without specific prior written permission.
00127  *
00128  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00129  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00130  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00131  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
00132  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00133  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00134  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00135  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00136  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00137  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00138  */
00139 

Hosted by SourceForge.net Logo