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);
00022 TO_ENDIAN( head.info.id );
00023 head.type = Swap32(head.type);
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));
00039 file->Read(&dataHead.comp, chunk.size);
00040 chunk.id = Swap32(chunk.id);
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
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
00085
00086
00087
00088
00089
00090
00091
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
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
00108 }
00109 }
00110 }
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139